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.conf.Configuration;
25  import org.apache.hadoop.hbase.HConstants;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
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_DISCHARGER_THREAD_COUNT =
59        "hbase.hstore.compaction.discharger.thread.count";
60    public static final String HBASE_HSTORE_COMPACTION_MAX_SIZE_KEY =
61      "hbase.hstore.compaction.max.size";
62    public static final String HBASE_HSTORE_COMPACTION_MAX_SIZE_OFFPEAK_KEY =
63        "hbase.hstore.compaction.max.size.offpeak";
64    public static final String HBASE_HSTORE_OFFPEAK_END_HOUR = "hbase.offpeak.end.hour";
65    public static final String HBASE_HSTORE_OFFPEAK_START_HOUR = "hbase.offpeak.start.hour";
66    public static final String HBASE_HSTORE_MIN_LOCALITY_TO_SKIP_MAJOR_COMPACT =
67        "hbase.hstore.min.locality.to.skip.major.compact";
68  
69    public static final String HBASE_HFILE_COMPACTION_DISCHARGER_THREAD_COUNT =
70        "hbase.hfile.compaction.discharger.thread.count";
71    public static final String HBASE_HFILE_COMPACTION_DISCHARGER_INTERVAL =
72        "hbase.hfile.compaction.discharger.interval";
73  
74    /*
75     * The epoch time length for the windows we no longer compact
76     */
77    public static final String DATE_TIERED_MAX_AGE_MILLIS_KEY =
78      "hbase.hstore.compaction.date.tiered.max.storefile.age.millis";
79    public static final String DATE_TIERED_INCOMING_WINDOW_MIN_KEY =
80      "hbase.hstore.compaction.date.tiered.incoming.window.min";
81    public static final String COMPACTION_POLICY_CLASS_FOR_DATE_TIERED_WINDOWS_KEY =
82      "hbase.hstore.compaction.date.tiered.window.policy.class";
83    public static final String DATE_TIERED_SINGLE_OUTPUT_FOR_MINOR_COMPACTION_KEY =
84      "hbase.hstore.compaction.date.tiered.single.output.for.minor.compaction";
85  
86    private static final Class<? extends RatioBasedCompactionPolicy>
87      DEFAULT_COMPACTION_POLICY_CLASS_FOR_DATE_TIERED_WINDOWS = ExploringCompactionPolicy.class;
88  
89    public static final String DATE_TIERED_COMPACTION_WINDOW_FACTORY_CLASS_KEY =
90      "hbase.hstore.compaction.date.tiered.window.factory.class";
91  
92    private static final Class<? extends CompactionWindowFactory>
93      DEFAULT_DATE_TIERED_COMPACTION_WINDOW_FACTORY_CLASS = ExponentialCompactionWindowFactory.class;
94  
95    Configuration conf;
96    StoreConfigInformation storeConfigInfo;
97  
98    private final double offPeakCompactionRatio;
99    /** Since all these properties can change online, they are volatile **/
100   private final long maxCompactSize;
101   private final long offPeakMaxCompactSize;
102   private final long minCompactSize;
103   /** This one can be update **/
104   private int minFilesToCompact;
105   private final int maxFilesToCompact;
106   private final double compactionRatio;
107   private final long throttlePoint;
108   private final long majorCompactionPeriod;
109   private final float majorCompactionJitter;
110   private final float minLocalityToForceCompact;
111   private final long dateTieredMaxStoreFileAgeMillis;
112   private final int dateTieredIncomingWindowMin;
113   private final String compactionPolicyForDateTieredWindow;
114   private final boolean dateTieredSingleOutputForMinorCompaction;
115   private final String dateTieredCompactionWindowFactory;
116 
117   CompactionConfiguration(Configuration conf, StoreConfigInformation storeConfigInfo) {
118     this.conf = conf;
119     this.storeConfigInfo = storeConfigInfo;
120 
121     maxCompactSize = conf.getLong(HBASE_HSTORE_COMPACTION_MAX_SIZE_KEY, Long.MAX_VALUE);
122     offPeakMaxCompactSize = conf.getLong(HBASE_HSTORE_COMPACTION_MAX_SIZE_OFFPEAK_KEY, 
123       maxCompactSize);      
124     minCompactSize = conf.getLong(HBASE_HSTORE_COMPACTION_MIN_SIZE_KEY,
125         storeConfigInfo.getMemstoreFlushSize());
126     minFilesToCompact = Math.max(2, conf.getInt(HBASE_HSTORE_COMPACTION_MIN_KEY,
127           /*old name*/ conf.getInt("hbase.hstore.compactionThreshold", 3)));
128     maxFilesToCompact = conf.getInt(HBASE_HSTORE_COMPACTION_MAX_KEY, 10);
129     compactionRatio = conf.getFloat(HBASE_HSTORE_COMPACTION_RATIO_KEY, 1.2F);
130     offPeakCompactionRatio = conf.getFloat(HBASE_HSTORE_COMPACTION_RATIO_OFFPEAK_KEY, 5.0F);
131 
132     throttlePoint = conf.getLong("hbase.regionserver.thread.compaction.throttle",
133           2 * maxFilesToCompact * storeConfigInfo.getMemstoreFlushSize());
134     majorCompactionPeriod = conf.getLong(HConstants.MAJOR_COMPACTION_PERIOD, 1000*60*60*24*7);
135     // Make it 0.5 so jitter has us fall evenly either side of when the compaction should run
136     majorCompactionJitter = conf.getFloat("hbase.hregion.majorcompaction.jitter", 0.50F);
137     minLocalityToForceCompact = conf.getFloat(HBASE_HSTORE_MIN_LOCALITY_TO_SKIP_MAJOR_COMPACT, 0f);
138 
139     dateTieredMaxStoreFileAgeMillis = conf.getLong(DATE_TIERED_MAX_AGE_MILLIS_KEY, Long.MAX_VALUE);
140     dateTieredIncomingWindowMin = conf.getInt(DATE_TIERED_INCOMING_WINDOW_MIN_KEY, 6);
141     compactionPolicyForDateTieredWindow = conf.get(
142       COMPACTION_POLICY_CLASS_FOR_DATE_TIERED_WINDOWS_KEY,
143       DEFAULT_COMPACTION_POLICY_CLASS_FOR_DATE_TIERED_WINDOWS.getName());
144     dateTieredSingleOutputForMinorCompaction = conf
145         .getBoolean(DATE_TIERED_SINGLE_OUTPUT_FOR_MINOR_COMPACTION_KEY, true);
146     this.dateTieredCompactionWindowFactory = conf.get(
147       DATE_TIERED_COMPACTION_WINDOW_FACTORY_CLASS_KEY,
148       DEFAULT_DATE_TIERED_COMPACTION_WINDOW_FACTORY_CLASS.getName());
149     LOG.info(this);
150   }
151 
152   @Override
153   public String toString() {
154     return String.format(
155       "size [minCompactSize:%s, maxCompactSize:%s, offPeakMaxCompactSize:%s);"
156       + " files [minFilesToCompact:%d, maxFilesToCompact:%d);"
157       + " ratio %f; off-peak ratio %f; throttle point %d;"
158       + " major period %d, major jitter %f, min locality to compact %f;"
159       + " tiered compaction: max_age %d, incoming window min %d,"
160       + " compaction policy for tiered window %s, single output for minor %b,"
161       + " compaction window factory %s",
162       minCompactSize,
163       maxCompactSize,
164       offPeakMaxCompactSize,
165       minFilesToCompact,
166       maxFilesToCompact,
167       compactionRatio,
168       offPeakCompactionRatio,
169       throttlePoint,
170       majorCompactionPeriod,
171       majorCompactionJitter,
172       minLocalityToForceCompact,
173       dateTieredMaxStoreFileAgeMillis,
174       dateTieredIncomingWindowMin,
175       compactionPolicyForDateTieredWindow,
176       dateTieredSingleOutputForMinorCompaction,
177       dateTieredCompactionWindowFactory
178       );
179   }
180 
181   /**
182    * @return lower bound below which compaction is selected without ratio test
183    */
184   public long getMinCompactSize() {
185     return minCompactSize;
186   }
187 
188   /**
189    * @return upper bound on file size to be included in minor compactions
190    */
191   public long getMaxCompactSize() {
192     return maxCompactSize;
193   }
194 
195   /**
196    * @return upper bound on number of files to be included in minor compactions
197    */
198   public int getMinFilesToCompact() {
199     return minFilesToCompact;
200   }
201 
202   /**
203    * Set upper bound on number of files to be included in minor compactions
204    * @param threshold value to set to
205    */
206   public void setMinFilesToCompact(int threshold) {
207     minFilesToCompact = threshold;
208   }
209 
210   /**
211    * @return upper bound on number of files to be included in minor compactions
212    */
213   public int getMaxFilesToCompact() {
214     return maxFilesToCompact;
215   }
216 
217   /**
218    * @return Ratio used for compaction
219    */
220   public double getCompactionRatio() {
221     return compactionRatio;
222   }
223 
224   /**
225    * @return Off peak Ratio used for compaction
226    */
227   public double getCompactionRatioOffPeak() {
228     return offPeakCompactionRatio;
229   }
230 
231   /**
232    * @return ThrottlePoint used for classifying small and large compactions
233    */
234   public long getThrottlePoint() {
235     return throttlePoint;
236   }
237 
238   /**
239    * @return Major compaction period from compaction.
240    *   Major compactions are selected periodically according to this parameter plus jitter
241    */
242   public long getMajorCompactionPeriod() {
243     return majorCompactionPeriod;
244   }
245 
246   /**
247    * @return Major the jitter fraction, the fraction within which the major compaction
248    *    period is randomly chosen from the majorCompactionPeriod in each store.
249    */
250   public float getMajorCompactionJitter() {
251     return majorCompactionJitter;
252   }
253 
254   /**
255    * @return Block locality ratio, the ratio at which we will include old regions with a single
256    *   store file for major compaction.  Used to improve block locality for regions that
257    *   haven't had writes in a while but are still being read.
258    */
259   public float getMinLocalityToForceCompact() {
260     return minLocalityToForceCompact;
261   }
262 
263   public long getOffPeakMaxCompactSize() {
264     return offPeakMaxCompactSize;
265   }
266 
267   public long getMaxCompactSize(boolean mayUseOffpeak) {
268     if (mayUseOffpeak) {
269       return getOffPeakMaxCompactSize();
270     } else {
271       return getMaxCompactSize();
272     }
273   }
274 
275   public long getDateTieredMaxStoreFileAgeMillis() {
276     return dateTieredMaxStoreFileAgeMillis;
277   }
278 
279   public int getDateTieredIncomingWindowMin() {
280     return dateTieredIncomingWindowMin;
281   }
282 
283   public String getCompactionPolicyForDateTieredWindow() {
284     return compactionPolicyForDateTieredWindow;
285   }
286 
287   public boolean useDateTieredSingleOutputForMinorCompaction() {
288     return dateTieredSingleOutputForMinorCompaction;
289   }
290 
291   public String getDateTieredCompactionWindowFactory() {
292     return dateTieredCompactionWindowFactory;
293   }
294 }