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    private static final double EPSILON = 1E-6;
43  
44    private long desiredMaxFileSize;
45    private double jitterRate;
46  
47    @Override
48    protected void configureForRegion(HRegion region) {
49      super.configureForRegion(region);
50      Configuration conf = getConf();
51      HTableDescriptor desc = region.getTableDesc();
52      if (desc != null) {
53        this.desiredMaxFileSize = desc.getMaxFileSize();
54      }
55      if (this.desiredMaxFileSize <= 0) {
56        this.desiredMaxFileSize = conf.getLong(HConstants.HREGION_MAX_FILESIZE,
57          HConstants.DEFAULT_MAX_FILE_SIZE);
58      }
59      double jitter = conf.getDouble("hbase.hregion.max.filesize.jitter", 0.25D);
60      this.jitterRate = (RANDOM.nextFloat() - 0.5D) * jitter;
61      long jitterValue = (long) (this.desiredMaxFileSize * this.jitterRate);
62      // make sure the long value won't overflow with jitter
63      if (this.jitterRate > EPSILON && jitterValue > (Long.MAX_VALUE - this.desiredMaxFileSize)) {
64        this.desiredMaxFileSize = Long.MAX_VALUE;
65      } else {
66        this.desiredMaxFileSize += jitterValue;
67      }
68    }
69  
70    @Override
71    protected boolean shouldSplit() {
72      boolean force = region.shouldForceSplit();
73      boolean foundABigStore = false;
74  
75      for (Store store : region.getStores()) {
76        // If any of the stores are unable to split (eg they contain reference files)
77        // then don't split
78        if ((!store.canSplit())) {
79          return false;
80        }
81  
82        // Mark if any store is big enough
83        if (store.getSize() > desiredMaxFileSize) {
84          foundABigStore = true;
85        }
86      }
87  
88      return foundABigStore || force;
89    }
90  
91    long getDesiredMaxFileSize() {
92      return desiredMaxFileSize;
93    }
94  
95    @VisibleForTesting
96    public boolean positiveJitterRate() {
97      return this.jitterRate > EPSILON;
98    }
99  }