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.HStore;
28  import org.apache.hadoop.hbase.regionserver.StoreConfigInformation;
29  
30  /**
31   * Compaction configuration for a particular instance of HStore.
32   * Takes into account both global settings and ones set on the column family/store.
33   * Control knobs for default compaction algorithm:
34   * <p/>
35   * maxCompactSize - upper bound on file size to be included in minor compactions
36   * minCompactSize - lower bound below which compaction is selected without ratio test
37   * minFilesToCompact - lower bound on number of files in any minor compaction
38   * maxFilesToCompact - upper bound on number of files in any minor compaction
39   * compactionRatio - Ratio used for compaction
40   * <p/>
41   * Set parameter as "hbase.hstore.compaction.<attribute>"
42   */
43  
44  //TODO: revisit this class for online parameter updating (both in xml and on the CF)
45  @InterfaceAudience.Private
46  public class CompactionConfiguration {
47  
48    static final Log LOG = LogFactory.getLog(CompactionConfiguration.class);
49  
50    private static final String CONFIG_PREFIX = "hbase.hstore.compaction.";
51  
52    Configuration conf;
53    StoreConfigInformation storeConfigInfo;
54  
55    long maxCompactSize;
56    long minCompactSize;
57    int minFilesToCompact;
58    int maxFilesToCompact;
59    double compactionRatio;
60    double offPeekCompactionRatio;
61    long throttlePoint;
62    boolean shouldDeleteExpired;
63    long majorCompactionPeriod;
64    float majorCompactionJitter;
65  
66    CompactionConfiguration(Configuration conf, StoreConfigInformation storeConfigInfo) {
67      this.conf = conf;
68      this.storeConfigInfo = storeConfigInfo;
69  
70      maxCompactSize = conf.getLong(CONFIG_PREFIX + "max.size", Long.MAX_VALUE);
71      minCompactSize = conf.getLong(CONFIG_PREFIX + "min.size",
72          storeConfigInfo.getMemstoreFlushSize());
73      minFilesToCompact = Math.max(2, conf.getInt(CONFIG_PREFIX + "min",
74            /*old name*/ conf.getInt("hbase.hstore.compactionThreshold", 3)));
75      maxFilesToCompact = conf.getInt(CONFIG_PREFIX + "max", 10);
76      compactionRatio = conf.getFloat(CONFIG_PREFIX + "ratio", 1.2F);
77      offPeekCompactionRatio = conf.getFloat(CONFIG_PREFIX + "ratio.offpeak", 5.0F);
78  
79      throttlePoint =  conf.getLong("hbase.regionserver.thread.compaction.throttle",
80            2 * maxFilesToCompact * storeConfigInfo.getMemstoreFlushSize());
81      shouldDeleteExpired = conf.getBoolean("hbase.store.delete.expired.storefile", true);
82      majorCompactionPeriod = conf.getLong(HConstants.MAJOR_COMPACTION_PERIOD, 1000*60*60*24);
83      majorCompactionJitter = conf.getFloat("hbase.hregion.majorcompaction.jitter", 0.20F);
84  
85      LOG.info("Compaction configuration " + this.toString());
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 }