View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver;
19  
20  import com.google.common.annotations.VisibleForTesting;
21  
22  import java.util.Random;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
26  import org.apache.hadoop.hbase.HConstants;
27  import org.apache.hadoop.hbase.HTableDescriptor;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  
30  /**
31   * A {@link RegionSplitPolicy} implementation which splits a region
32   * as soon as any of its store files exceeds a maximum configurable
33   * size.
34   * <p>
35   * This is the default split policy. From 0.94.0 on the default split policy has
36   * changed to {@link IncreasingToUpperBoundRegionSplitPolicy}
37   * </p>
38   */
39  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
40  public class ConstantSizeRegionSplitPolicy extends RegionSplitPolicy {
41    private static final Random RANDOM = new Random();
42  
43    private long desiredMaxFileSize;
44    private double jitterRate;
45  
46    @Override
47    protected void configureForRegion(HRegion region) {
48      super.configureForRegion(region);
49      Configuration conf = getConf();
50      HTableDescriptor desc = region.getTableDesc();
51      if (desc != null) {
52        this.desiredMaxFileSize = desc.getMaxFileSize();
53      }
54      if (this.desiredMaxFileSize <= 0) {
55        this.desiredMaxFileSize = conf.getLong(HConstants.HREGION_MAX_FILESIZE,
56          HConstants.DEFAULT_MAX_FILE_SIZE);
57      }
58      double jitter = conf.getDouble("hbase.hregion.max.filesize.jitter", 0.25D);
59      this.jitterRate = (RANDOM.nextFloat() - 0.5D) * jitter;
60      long jitterValue = (long) (this.desiredMaxFileSize * this.jitterRate);
61      // make sure the long value won't overflow with jitter
62      if (this.jitterRate > 0 && jitterValue > (Long.MAX_VALUE - this.desiredMaxFileSize)) {
63        this.desiredMaxFileSize = Long.MAX_VALUE;
64      } else {
65        this.desiredMaxFileSize += jitterValue;
66      }
67    }
68  
69    @Override
70    protected boolean shouldSplit() {
71      boolean force = region.shouldForceSplit();
72      boolean foundABigStore = false;
73  
74      for (Store store : region.getStores()) {
75        // If any of the stores are unable to split (eg they contain reference files)
76        // then don't split
77        if ((!store.canSplit())) {
78          return false;
79        }
80  
81        // Mark if any store is big enough
82        if (store.getSize() > desiredMaxFileSize) {
83          foundABigStore = true;
84        }
85      }
86  
87      return foundABigStore || force;
88    }
89  
90    long getDesiredMaxFileSize() {
91      return desiredMaxFileSize;
92    }
93  
94    @VisibleForTesting
95    public boolean positiveJitterRate() {
96      return this.jitterRate > 0;
97    }
98  }