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    private static final String CONFIG_PREFIX = "hbase.hstore.compaction.";
50  
51    Configuration conf;
52    StoreConfigInformation storeConfigInfo;
53  
54    long maxCompactSize;
55    long minCompactSize;
56    int minFilesToCompact;
57    int maxFilesToCompact;
58    double compactionRatio;
59    double offPeekCompactionRatio;
60    long throttlePoint;
61    boolean shouldDeleteExpired;
62    long majorCompactionPeriod;
63    float majorCompactionJitter;
64  
65    CompactionConfiguration(Configuration conf, StoreConfigInformation storeConfigInfo) {
66      this.conf = conf;
67      this.storeConfigInfo = storeConfigInfo;
68  
69      maxCompactSize = conf.getLong(CONFIG_PREFIX + "max.size", Long.MAX_VALUE);
70      minCompactSize = conf.getLong(CONFIG_PREFIX + "min.size",
71          storeConfigInfo.getMemstoreFlushSize());
72      minFilesToCompact = Math.max(2, conf.getInt(CONFIG_PREFIX + "min",
73            /*old name*/ conf.getInt("hbase.hstore.compactionThreshold", 3)));
74      maxFilesToCompact = conf.getInt(CONFIG_PREFIX + "max", 10);
75      compactionRatio = conf.getFloat(CONFIG_PREFIX + "ratio", 1.2F);
76      offPeekCompactionRatio = conf.getFloat(CONFIG_PREFIX + "ratio.offpeak", 5.0F);
77  
78      throttlePoint =  conf.getLong("hbase.regionserver.thread.compaction.throttle",
79            2 * maxFilesToCompact * storeConfigInfo.getMemstoreFlushSize());
80      shouldDeleteExpired = conf.getBoolean("hbase.store.delete.expired.storefile", true);
81      majorCompactionPeriod = conf.getLong(HConstants.MAJOR_COMPACTION_PERIOD, 1000*60*60*24*7);
82      // Make it 0.5 so jitter has us fall evenly either side of when the compaction should run
83      majorCompactionJitter = conf.getFloat("hbase.hregion.majorcompaction.jitter", 0.50F);
84  
85      LOG.info(this);
86    }
87  
88    @Override
89    public String toString() {
90      return String.format(
91        "size [%d, %d); files [%d, %d); ratio %f; off-peak ratio %f; throttle point %d;"
92        + "%s delete expired; major period %d, major jitter %f",
93        minCompactSize,
94        maxCompactSize,
95        minFilesToCompact,
96        maxFilesToCompact,
97        compactionRatio,
98        offPeekCompactionRatio,
99        throttlePoint,
100       shouldDeleteExpired ? "" : " don't",
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 
170   /**
171    * @return Whether expired files should be deleted ASAP using compactions
172    */
173   boolean shouldDeleteExpired() {
174     return shouldDeleteExpired;
175   }
176 }