View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.regionserver.compactions;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.HConstants;
27  import org.apache.hadoop.hbase.regionserver.StoreConfigInformation;
28  
29  /**
30   * Compaction configuration for a particular instance of HStore.
31   * Takes into account both global settings and ones set on the column family/store.
32   * Control knobs for default compaction algorithm:
33   * <p/>
34   * maxCompactSize - upper bound on file size to be included in minor compactions
35   * minCompactSize - lower bound below which compaction is selected without ratio test
36   * minFilesToCompact - lower bound on number of files in any minor compaction
37   * maxFilesToCompact - upper bound on number of files in any minor compaction
38   * compactionRatio - Ratio used for compaction
39   * <p/>
40   * Set parameter as "hbase.hstore.compaction.<attribute>"
41   */
42  
43  @InterfaceAudience.Private
44  public class CompactionConfiguration {
45  
46    static final Log LOG = LogFactory.getLog(CompactionConfiguration.class);
47  
48    public static final String HBASE_HSTORE_COMPACTION_RATIO_KEY = "hbase.hstore.compaction.ratio";
49    public static final String HBASE_HSTORE_COMPACTION_RATIO_OFFPEAK_KEY =
50      "hbase.hstore.compaction.ratio.offpeak";
51    public static final String HBASE_HSTORE_COMPACTION_MIN_KEY = "hbase.hstore.compaction.min";
52    public static final String HBASE_HSTORE_COMPACTION_MIN_SIZE_KEY =
53      "hbase.hstore.compaction.min.size";
54    public static final String HBASE_HSTORE_COMPACTION_MAX_KEY = "hbase.hstore.compaction.max";
55    public static final String HBASE_HSTORE_COMPACTION_MAX_SIZE_KEY =
56      "hbase.hstore.compaction.max.size";
57    public static final String HBASE_HSTORE_OFFPEAK_END_HOUR = "hbase.offpeak.end.hour";
58    public static final String HBASE_HSTORE_OFFPEAK_START_HOUR = "hbase.offpeak.start.hour";
59  
60    Configuration conf;
61    StoreConfigInformation storeConfigInfo;
62  
63    private final double offPeakCompactionRatio;
64    /** Since all these properties can change online, they are volatile **/
65    private final long maxCompactSize;
66    private final long minCompactSize;
67    private final int minFilesToCompact;
68    private final int maxFilesToCompact;
69    private final double compactionRatio;
70    private final long throttlePoint;
71    private final long majorCompactionPeriod;
72    private final float majorCompactionJitter;
73  
74    CompactionConfiguration(Configuration conf, StoreConfigInformation storeConfigInfo) {
75      this.conf = conf;
76      this.storeConfigInfo = storeConfigInfo;
77  
78      maxCompactSize = conf.getLong(HBASE_HSTORE_COMPACTION_MAX_SIZE_KEY, Long.MAX_VALUE);
79      minCompactSize = conf.getLong(HBASE_HSTORE_COMPACTION_MIN_SIZE_KEY,
80          storeConfigInfo.getMemstoreFlushSize());
81      minFilesToCompact = Math.max(2, conf.getInt(HBASE_HSTORE_COMPACTION_MIN_KEY,
82            /*old name*/ conf.getInt("hbase.hstore.compactionThreshold", 3)));
83      maxFilesToCompact = conf.getInt(HBASE_HSTORE_COMPACTION_MAX_KEY, 10);
84      compactionRatio = conf.getFloat(HBASE_HSTORE_COMPACTION_RATIO_KEY, 1.2F);
85      offPeakCompactionRatio = conf.getFloat(HBASE_HSTORE_COMPACTION_RATIO_OFFPEAK_KEY, 5.0F);
86  
87      throttlePoint = conf.getLong("hbase.regionserver.thread.compaction.throttle",
88            2 * maxFilesToCompact * storeConfigInfo.getMemstoreFlushSize());
89      majorCompactionPeriod = conf.getLong(HConstants.MAJOR_COMPACTION_PERIOD, 1000*60*60*24*7);
90      // Make it 0.5 so jitter has us fall evenly either side of when the compaction should run
91      majorCompactionJitter = conf.getFloat("hbase.hregion.majorcompaction.jitter", 0.50F);
92  
93      LOG.info(this);
94    }
95  
96    @Override
97    public String toString() {
98      return String.format(
99        "size [%d, %d); files [%d, %d); ratio %f; off-peak ratio %f; throttle point %d;"
100       + " major period %d, major jitter %f",
101       minCompactSize,
102       maxCompactSize,
103       minFilesToCompact,
104       maxFilesToCompact,
105       compactionRatio,
106       offPeakCompactionRatio,
107       throttlePoint,
108       majorCompactionPeriod,
109       majorCompactionJitter);
110   }
111 
112   /**
113    * @return lower bound below which compaction is selected without ratio test
114    */
115   public long getMinCompactSize() {
116     return minCompactSize;
117   }
118 
119   /**
120    * @return upper bound on file size to be included in minor compactions
121    */
122   public long getMaxCompactSize() {
123     return maxCompactSize;
124   }
125 
126   /**
127    * @return upper bound on number of files to be included in minor compactions
128    */
129   public int getMinFilesToCompact() {
130     return minFilesToCompact;
131   }
132 
133   /**
134    * @return upper bound on number of files to be included in minor compactions
135    */
136   public int getMaxFilesToCompact() {
137     return maxFilesToCompact;
138   }
139 
140   /**
141    * @return Ratio used for compaction
142    */
143   public double getCompactionRatio() {
144     return compactionRatio;
145   }
146 
147   /**
148    * @return Off peak Ratio used for compaction
149    */
150   public double getCompactionRatioOffPeak() {
151     return offPeakCompactionRatio;
152   }
153 
154   /**
155    * @return ThrottlePoint used for classifying small and large compactions
156    */
157   public long getThrottlePoint() {
158     return throttlePoint;
159   }
160 
161   /**
162    * @return Major compaction period from compaction.
163    * Major compactions are selected periodically according to this parameter plus jitter
164    */
165   public long getMajorCompactionPeriod() {
166     return majorCompactionPeriod;
167   }
168 
169   /**
170    * @return Major the jitter fraction, the fraction within which the major compaction
171    *  period is randomly chosen from the majorCompactionPeriod in each store.
172    */
173   public float getMajorCompactionJitter() {
174     return majorCompactionJitter;
175   }
176 }