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.List;
22  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.conf.Configured;
26  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.util.ReflectionUtils;
30  
31  import com.google.common.base.Preconditions;
32  
33  /**
34   * A split policy determines when a region should be split.
35   * @see IncreasingToUpperBoundRegionSplitPolicy Default split policy since
36   *      0.94.0
37   * @see ConstantSizeRegionSplitPolicy Default split policy before 0.94.0
38   */
39  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
40  public abstract class RegionSplitPolicy extends Configured {
41    private static final Class<? extends RegionSplitPolicy>
42      DEFAULT_SPLIT_POLICY_CLASS = IncreasingToUpperBoundRegionSplitPolicy.class;
43  
44    /**
45     * The region configured for this split policy.
46     */
47    protected HRegion region;
48  
49    /**
50     * Upon construction, this method will be called with the region
51     * to be governed. It will be called once and only once.
52     */
53    protected void configureForRegion(HRegion region) {
54      Preconditions.checkState(
55          this.region == null,
56          "Policy already configured for region {}",
57          this.region);
58  
59      this.region = region;
60    }
61  
62    /**
63     * @return true if the specified region should be split.
64     */
65    protected abstract boolean shouldSplit();
66  
67    /**
68     * @return the key at which the region should be split, or null
69     * if it cannot be split. This will only be called if shouldSplit
70     * previously returned true.
71     */
72    protected byte[] getSplitPoint() {
73      byte[] explicitSplitPoint = this.region.getExplicitSplitPoint();
74      if (explicitSplitPoint != null) {
75        return explicitSplitPoint;
76      }
77      List<Store> stores = region.getStores();
78  
79      byte[] splitPointFromLargestStore = null;
80      long largestStoreSize = 0;
81      for (Store s : stores) {
82        byte[] splitPoint = s.getSplitPoint();
83        long storeSize = s.getSize();
84        if (splitPoint != null && largestStoreSize < storeSize) {
85          splitPointFromLargestStore = splitPoint;
86          largestStoreSize = storeSize;
87        }
88      }
89  
90      return splitPointFromLargestStore;
91    }
92  
93    /**
94     * Create the RegionSplitPolicy configured for the given table.
95     * @param region
96     * @param conf
97     * @return a RegionSplitPolicy
98     * @throws IOException
99     */
100   public static RegionSplitPolicy create(HRegion region,
101       Configuration conf) throws IOException {
102     Class<? extends RegionSplitPolicy> clazz = getSplitPolicyClass(
103         region.getTableDesc(), conf);
104     RegionSplitPolicy policy = ReflectionUtils.newInstance(clazz, conf);
105     policy.configureForRegion(region);
106     return policy;
107   }
108 
109   public static Class<? extends RegionSplitPolicy> getSplitPolicyClass(
110       HTableDescriptor htd, Configuration conf) throws IOException {
111     String className = htd.getRegionSplitPolicyClassName();
112     if (className == null) {
113       className = conf.get(HConstants.HBASE_REGION_SPLIT_POLICY_KEY,
114           DEFAULT_SPLIT_POLICY_CLASS.getName());
115     }
116 
117     try {
118       Class<? extends RegionSplitPolicy> clazz =
119         Class.forName(className).asSubclass(RegionSplitPolicy.class);
120       return clazz;
121     } catch (Exception  e) {
122       throw new IOException(
123           "Unable to load configured region split policy '" +
124           className + "' for table '" + htd.getTableName() + "'",
125           e);
126     }
127   }
128 
129   /**
130    * In {@link HRegionFileSystem#splitStoreFile(org.apache.hadoop.hbase.HRegionInfo, String,
131    * StoreFile, byte[], boolean, RegionSplitPolicy)} we are not creating the split reference
132    * if split row not lies in the StoreFile range. But in some use cases we may need to create
133    * the split reference even when the split row not lies in the range. This method can be used
134    * to decide, whether to skip the the StoreFile range check or not.
135    * @return whether to skip the StoreFile range check or not
136    * @deprecated Use {@link #skipStoreFileRangeCheck(String)}} instead
137    */
138   @Deprecated
139   protected boolean skipStoreFileRangeCheck() {
140     return false;
141   }
142 
143   /**
144    * See {@link #skipStoreFileRangeCheck()} javadoc.
145    * @param familyName
146    * @return whether to skip the StoreFile range check or not
147    */
148   protected boolean skipStoreFileRangeCheck(String familyName) {
149     return skipStoreFileRangeCheck();
150   }
151 }