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   * <p>
31   * Compaction configuration for a particular instance of HStore.
32   * Takes into account both global settings and ones set on the column family/store.
33   * Control knobs for default compaction algorithm:
34   * </p>
35   * <p>
36   * maxCompactSize - upper bound on file size to be included in minor compactions
37   * minCompactSize - lower bound below which compaction is selected without ratio test
38   * minFilesToCompact - lower bound on number of files in any minor compaction
39   * maxFilesToCompact - upper bound on number of files in any minor compaction
40   * compactionRatio - Ratio used for compaction
41   * minLocalityToForceCompact - Locality threshold for a store file to major compact (HBASE-11195)
42   * </p>
43   * Set parameter as "hbase.hstore.compaction.&lt;attribute&gt;"
44   */
45  
46  @InterfaceAudience.Private
47  public class CompactionConfiguration {
48  
49    private static final Log LOG = LogFactory.getLog(CompactionConfiguration.class);
50  
51    public static final String HBASE_HSTORE_COMPACTION_RATIO_KEY = "hbase.hstore.compaction.ratio";
52    public static final String HBASE_HSTORE_COMPACTION_RATIO_OFFPEAK_KEY =
53      "hbase.hstore.compaction.ratio.offpeak";
54    public static final String HBASE_HSTORE_COMPACTION_MIN_KEY = "hbase.hstore.compaction.min";
55    public static final String HBASE_HSTORE_COMPACTION_MIN_SIZE_KEY =
56      "hbase.hstore.compaction.min.size";
57    public static final String HBASE_HSTORE_COMPACTION_MAX_KEY = "hbase.hstore.compaction.max";
58    public static final String HBASE_HSTORE_COMPACTION_MAX_SIZE_KEY =
59      "hbase.hstore.compaction.max.size";
60    public static final String HBASE_HSTORE_OFFPEAK_END_HOUR = "hbase.offpeak.end.hour";
61    public static final String HBASE_HSTORE_OFFPEAK_START_HOUR = "hbase.offpeak.start.hour";
62    public static final String HBASE_HSTORE_MIN_LOCALITY_TO_SKIP_MAJOR_COMPACT =
63        "hbase.hstore.min.locality.to.skip.major.compact";
64  
65    Configuration conf;
66    StoreConfigInformation storeConfigInfo;
67  
68    private final double offPeakCompactionRatio;
69    /** Since all these properties can change online, they are volatile **/
70    private final long maxCompactSize;
71    private final long minCompactSize;
72    private final int minFilesToCompact;
73    private final int maxFilesToCompact;
74    private final double compactionRatio;
75    private final long throttlePoint;
76    private final long majorCompactionPeriod;
77    private final float majorCompactionJitter;
78    private final float minLocalityToForceCompact;
79  
80    CompactionConfiguration(Configuration conf, StoreConfigInformation storeConfigInfo) {
81      this.conf = conf;
82      this.storeConfigInfo = storeConfigInfo;
83  
84      maxCompactSize = conf.getLong(HBASE_HSTORE_COMPACTION_MAX_SIZE_KEY, Long.MAX_VALUE);
85      minCompactSize = conf.getLong(HBASE_HSTORE_COMPACTION_MIN_SIZE_KEY,
86          storeConfigInfo.getMemstoreFlushSize());
87      minFilesToCompact = Math.max(2, conf.getInt(HBASE_HSTORE_COMPACTION_MIN_KEY,
88            /*old name*/ conf.getInt("hbase.hstore.compactionThreshold", 3)));
89      maxFilesToCompact = conf.getInt(HBASE_HSTORE_COMPACTION_MAX_KEY, 10);
90      compactionRatio = conf.getFloat(HBASE_HSTORE_COMPACTION_RATIO_KEY, 1.2F);
91      offPeakCompactionRatio = conf.getFloat(HBASE_HSTORE_COMPACTION_RATIO_OFFPEAK_KEY, 5.0F);
92  
93      throttlePoint = conf.getLong("hbase.regionserver.thread.compaction.throttle",
94            2 * maxFilesToCompact * storeConfigInfo.getMemstoreFlushSize());
95      majorCompactionPeriod = conf.getLong(HConstants.MAJOR_COMPACTION_PERIOD, 1000*60*60*24*7);
96      // Make it 0.5 so jitter has us fall evenly either side of when the compaction should run
97      majorCompactionJitter = conf.getFloat("hbase.hregion.majorcompaction.jitter", 0.50F);
98      minLocalityToForceCompact = conf.getFloat(HBASE_HSTORE_MIN_LOCALITY_TO_SKIP_MAJOR_COMPACT, 0f);
99      LOG.info(this);
100   }
101 
102   @Override
103   public String toString() {
104     return String.format(
105       "size [%d, %d); files [%d, %d); ratio %f; off-peak ratio %f; throttle point %d;"
106       + " major period %d, major jitter %f, min locality to compact %f",
107       minCompactSize,
108       maxCompactSize,
109       minFilesToCompact,
110       maxFilesToCompact,
111       compactionRatio,
112       offPeakCompactionRatio,
113       throttlePoint,
114       majorCompactionPeriod,
115       majorCompactionJitter,
116       minLocalityToForceCompact);
117   }
118 
119   /**
120    * @return lower bound below which compaction is selected without ratio test
121    */
122   public long getMinCompactSize() {
123     return minCompactSize;
124   }
125 
126   /**
127    * @return upper bound on file size to be included in minor compactions
128    */
129   public long getMaxCompactSize() {
130     return maxCompactSize;
131   }
132 
133   /**
134    * @return upper bound on number of files to be included in minor compactions
135    */
136   public int getMinFilesToCompact() {
137     return minFilesToCompact;
138   }
139 
140   /**
141    * @return upper bound on number of files to be included in minor compactions
142    */
143   public int getMaxFilesToCompact() {
144     return maxFilesToCompact;
145   }
146 
147   /**
148    * @return Ratio used for compaction
149    */
150   public double getCompactionRatio() {
151     return compactionRatio;
152   }
153 
154   /**
155    * @return Off peak Ratio used for compaction
156    */
157   public double getCompactionRatioOffPeak() {
158     return offPeakCompactionRatio;
159   }
160 
161   /**
162    * @return ThrottlePoint used for classifying small and large compactions
163    */
164   public long getThrottlePoint() {
165     return throttlePoint;
166   }
167 
168   /**
169    * @return Major compaction period from compaction.
170    * Major compactions are selected periodically according to this parameter plus jitter
171    */
172   public long getMajorCompactionPeriod() {
173     return majorCompactionPeriod;
174   }
175 
176   /**
177    * @return Major the jitter fraction, the fraction within which the major compaction
178    *  period is randomly chosen from the majorCompactionPeriod in each store.
179    */
180   public float getMajorCompactionJitter() {
181     return majorCompactionJitter;
182   }
183 
184   /**
185    * @return Block locality ratio, the ratio at which we will include old regions with a single
186    * store file for major compaction.  Used to improve block locality for regions that
187    * haven't had writes in a while but are still being read.
188    */
189   public float getMinLocalityToForceCompact() {
190     return minLocalityToForceCompact;
191   }
192 }