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