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.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    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  
59    Configuration conf;
60    StoreConfigInformation storeConfigInfo;
61  
62    long maxCompactSize;
63    long minCompactSize;
64    int minFilesToCompact;
65    int maxFilesToCompact;
66    double compactionRatio;
67    double offPeekCompactionRatio;
68    long throttlePoint;
69    long majorCompactionPeriod;
70    float majorCompactionJitter;
71  
72    CompactionConfiguration(Configuration conf, StoreConfigInformation storeConfigInfo) {
73      this.conf = conf;
74      this.storeConfigInfo = storeConfigInfo;
75  
76      maxCompactSize = conf.getLong(HBASE_HSTORE_COMPACTION_MAX_SIZE_KEY, Long.MAX_VALUE);
77      minCompactSize = conf.getLong(HBASE_HSTORE_COMPACTION_MIN_SIZE_KEY,
78          storeConfigInfo.getMemstoreFlushSize());
79      minFilesToCompact = Math.max(2, conf.getInt(HBASE_HSTORE_COMPACTION_MIN_KEY,
80            /*old name*/ conf.getInt("hbase.hstore.compactionThreshold", 3)));
81      maxFilesToCompact = conf.getInt(HBASE_HSTORE_COMPACTION_MAX_KEY, 10);
82      compactionRatio = conf.getFloat(HBASE_HSTORE_COMPACTION_RATIO_KEY, 1.2F);
83      offPeekCompactionRatio = conf.getFloat(HBASE_HSTORE_COMPACTION_RATIO_OFFPEAK_KEY, 5.0F);
84  
85      throttlePoint =  conf.getLong("hbase.regionserver.thread.compaction.throttle",
86            2 * maxFilesToCompact * storeConfigInfo.getMemstoreFlushSize());
87      majorCompactionPeriod = conf.getLong(HConstants.MAJOR_COMPACTION_PERIOD, 1000*60*60*24*7);
88      // Make it 0.5 so jitter has us fall evenly either side of when the compaction should run
89      majorCompactionJitter = conf.getFloat("hbase.hregion.majorcompaction.jitter", 0.50F);
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",
99        minCompactSize,
100       maxCompactSize,
101       minFilesToCompact,
102       maxFilesToCompact,
103       compactionRatio,
104       offPeekCompactionRatio,
105       throttlePoint,
106       majorCompactionPeriod,
107       majorCompactionJitter);
108   }
109 
110   /**
111    * @return lower bound below which compaction is selected without ratio test
112    */
113   long getMinCompactSize() {
114     return minCompactSize;
115   }
116 
117   /**
118    * @return upper bound on file size to be included in minor compactions
119    */
120   long getMaxCompactSize() {
121     return maxCompactSize;
122   }
123 
124   /**
125    * @return upper bound on number of files to be included in minor compactions
126    */
127   int getMinFilesToCompact() {
128     return minFilesToCompact;
129   }
130 
131   /**
132    * @return upper bound on number of files to be included in minor compactions
133    */
134   int getMaxFilesToCompact() {
135     return maxFilesToCompact;
136   }
137 
138   /**
139    * @return Ratio used for compaction
140    */
141   double getCompactionRatio() {
142     return compactionRatio;
143   }
144 
145   /**
146    * @return Off peak Ratio used for compaction
147    */
148   double getCompactionRatioOffPeak() {
149     return offPeekCompactionRatio;
150   }
151 
152   /**
153    * @return ThrottlePoint used for classifying small and large compactions
154    */
155   long getThrottlePoint() {
156     return throttlePoint;
157   }
158 
159   /**
160    * @return Major compaction period from compaction.
161    * Major compactions are selected periodically according to this parameter plus jitter
162    */
163   long getMajorCompactionPeriod() {
164     return majorCompactionPeriod;
165   }
166 
167   /**
168    * @return Major the jitter fraction, the fraction within which the major compaction
169    *  period is randomly chosen from the majorCompactionPeriod in each store.
170    */
171   float getMajorCompactionJitter() {
172     return majorCompactionJitter;
173   }
174 }