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  //TODO: revisit this class for online parameter updating (both in xml and on the CF)
44  @InterfaceAudience.Private
45  public class CompactionConfiguration {
46  
47    static final Log LOG = LogFactory.getLog(CompactionConfiguration.class);
48  
49    private static final String CONFIG_PREFIX = "hbase.hstore.compaction.";
50    public static final String HBASE_HSTORE_MIN_LOCALITY_TO_SKIP_MAJOR_COMPACT =
51        "hbase.hstore.min.locality.to.skip.major.compact";
52    public static final String RATIO_KEY = CONFIG_PREFIX + "ratio";
53    public static final String MIN_KEY = CONFIG_PREFIX + "min";
54    public static final String MAX_KEY = CONFIG_PREFIX + "max";
55  
56    Configuration conf;
57    StoreConfigInformation storeConfigInfo;
58  
59    long maxCompactSize;
60    long minCompactSize;
61    int minFilesToCompact;
62    int maxFilesToCompact;
63    double compactionRatio;
64    double offPeekCompactionRatio;
65    long throttlePoint;
66    long majorCompactionPeriod;
67    float majorCompactionJitter;
68    final float minLocalityToForceCompact;
69  
70  
71    CompactionConfiguration(Configuration conf, StoreConfigInformation storeConfigInfo) {
72      this.conf = conf;
73      this.storeConfigInfo = storeConfigInfo;
74  
75      maxCompactSize = conf.getLong(CONFIG_PREFIX + "max.size", Long.MAX_VALUE);
76      minCompactSize = conf.getLong(CONFIG_PREFIX + "min.size",
77          storeConfigInfo.getMemstoreFlushSize());
78      minFilesToCompact = Math.max(2, conf.getInt(MIN_KEY,
79            /*old name*/ conf.getInt("hbase.hstore.compactionThreshold", 3)));
80      maxFilesToCompact = conf.getInt(MAX_KEY, 10);
81      compactionRatio = conf.getFloat(RATIO_KEY, 1.2F);
82      offPeekCompactionRatio = conf.getFloat(CONFIG_PREFIX + "ratio.offpeak", 5.0F);
83  
84      throttlePoint =  conf.getLong("hbase.regionserver.thread.compaction.throttle",
85            2 * maxFilesToCompact * storeConfigInfo.getMemstoreFlushSize());
86      majorCompactionPeriod = conf.getLong(HConstants.MAJOR_COMPACTION_PERIOD, 1000*60*60*24*7);
87      // Make it 0.5 so jitter has us fall evenly either side of when the compaction should run
88      majorCompactionJitter = conf.getFloat("hbase.hregion.majorcompaction.jitter", 0.50F);
89      minLocalityToForceCompact = conf.getFloat(HBASE_HSTORE_MIN_LOCALITY_TO_SKIP_MAJOR_COMPACT, 0f);
90  
91      LOG.info(this);
92    }
93  
94    @Override
95    public String toString() {
96      return String.format(
97        "size [%d, %d); files [%d, %d); ratio %f; off-peak ratio %f; throttle point %d;"
98        + " major period %d, major jitter %f, min locality to compact %f\"",
99        minCompactSize,
100       maxCompactSize,
101       minFilesToCompact,
102       maxFilesToCompact,
103       compactionRatio,
104       offPeekCompactionRatio,
105       throttlePoint,
106       majorCompactionPeriod,
107       majorCompactionJitter,
108       minLocalityToForceCompact
109     );
110   }
111 
112   /**
113    * @return lower bound below which compaction is selected without ratio test
114    */
115   long getMinCompactSize() {
116     return minCompactSize;
117   }
118 
119   /**
120    * @return upper bound on file size to be included in minor compactions
121    */
122   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   int getMaxFilesToCompact() {
137     return maxFilesToCompact;
138   }
139 
140   /**
141    * @return Ratio used for compaction
142    */
143   double getCompactionRatio() {
144     return compactionRatio;
145   }
146 
147   /**
148    * @return Off peak Ratio used for compaction
149    */
150   double getCompactionRatioOffPeak() {
151     return offPeekCompactionRatio;
152   }
153 
154   /**
155    * @return ThrottlePoint used for classifying small and large compactions
156    */
157   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   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   float getMajorCompactionJitter() {
174     return majorCompactionJitter;
175   }
176 
177   /**
178    * @return Block locality ratio, the ratio at which we will include old regions with a single
179    * store file for major compaction.  Used to improve block locality for regions that
180    * haven't had writes in a while but are still being read.
181    */
182   float getMinLocalityToForceCompact() {
183     return minLocalityToForceCompact;
184   }
185 }