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 java.io.IOException;
21  import java.util.Map;
22  
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.conf.Configured;
25  import org.apache.hadoop.hbase.HConstants;
26  import org.apache.hadoop.hbase.HTableDescriptor;
27  import org.apache.hadoop.util.ReflectionUtils;
28  
29  import com.google.common.base.Preconditions;
30  
31  /**
32   * A split policy determines when a region should be split.
33   * @see IncreasingToUpperBoundRegionSplitPolicy Default split policy since
34   *      0.94.0
35   * @see ConstantSizeRegionSplitPolicy Default split policy before 0.94.0
36   */
37  public abstract class RegionSplitPolicy extends Configured {
38    private static final Class<? extends RegionSplitPolicy>
39      DEFAULT_SPLIT_POLICY_CLASS = IncreasingToUpperBoundRegionSplitPolicy.class;
40  
41    /**
42     * The region configured for this split policy.
43     */
44    protected HRegion region;
45  
46    /**
47     * Upon construction, this method will be called with the region
48     * to be governed. It will be called once and only once.
49     */
50    protected void configureForRegion(HRegion region) {
51      Preconditions.checkState(
52          this.region == null,
53          "Policy already configured for region {}",
54          this.region);
55  
56      this.region = region;
57    }
58  
59    /**
60     * @return true if the specified region should be split.
61     */
62    protected abstract boolean shouldSplit();
63  
64    /**
65     * @return the key at which the region should be split, or null
66     * if it cannot be split. This will only be called if shouldSplit
67     * previously returned true.
68     */
69    protected byte[] getSplitPoint() {
70      byte[] explicitSplitPoint = this.region.getExplicitSplitPoint();
71      if (explicitSplitPoint != null) {
72        return explicitSplitPoint;
73      }
74      Map<byte[], Store> stores = region.getStores();
75  
76      byte[] splitPointFromLargestStore = null;
77      long largestStoreSize = 0;
78      for (Store s : stores.values()) {
79        byte[] splitPoint = s.getSplitPoint();
80        long storeSize = s.getSize();
81        if (splitPoint != null && largestStoreSize < storeSize) {
82          splitPointFromLargestStore = splitPoint;
83          largestStoreSize = storeSize;
84        }
85      }
86  
87      return splitPointFromLargestStore;
88    }
89  
90    /**
91     * Create the RegionSplitPolicy configured for the given table.
92     * Each
93     * @param htd
94     * @param conf
95     * @return
96     * @throws IOException
97     */
98    public static RegionSplitPolicy create(HRegion region,
99        Configuration conf) throws IOException {
100     Class<? extends RegionSplitPolicy> clazz = getSplitPolicyClass(
101         region.getTableDesc(), conf);
102     RegionSplitPolicy policy = ReflectionUtils.newInstance(clazz, conf);
103     policy.configureForRegion(region);
104     return policy;
105   }
106 
107   static Class<? extends RegionSplitPolicy> getSplitPolicyClass(
108       HTableDescriptor htd, Configuration conf) throws IOException {
109     String className = htd.getRegionSplitPolicyClassName();
110     if (className == null) {
111       className = conf.get(HConstants.HBASE_REGION_SPLIT_POLICY_KEY,
112           DEFAULT_SPLIT_POLICY_CLASS.getName());
113     }
114 
115     try {
116       Class<? extends RegionSplitPolicy> clazz =
117         Class.forName(className).asSubclass(RegionSplitPolicy.class);
118       return clazz;
119     } catch (Exception  e) {
120       throw new IOException(
121           "Unable to load configured region split policy '" +
122           className + "' for table '" + htd.getNameAsString() + "'",
123           e);
124     }
125   }
126 
127 }