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