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