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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.hadoop
.hbase
.regionserver
;
20 import java
.io
.IOException
;
21 import java
.util
.List
;
22 import java
.util
.Optional
;
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
.client
.TableDescriptor
;
29 import org
.apache
.hadoop
.util
.ReflectionUtils
;
30 import org
.apache
.yetus
.audience
.InterfaceAudience
;
31 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.base
.Preconditions
;
35 * A split policy determines when a Region should be split.
37 * @see SteppingSplitPolicy Default split policy since 2.0.0
38 * @see IncreasingToUpperBoundRegionSplitPolicy Default split policy since
40 * @see ConstantSizeRegionSplitPolicy Default split policy before 0.94.0
42 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience
.CONFIG
)
43 public abstract class RegionSplitPolicy
extends Configured
{
44 private static final Class
<?
extends RegionSplitPolicy
>
45 DEFAULT_SPLIT_POLICY_CLASS
= SteppingSplitPolicy
.class;
48 * The region configured for this split policy.
49 * As of hbase-2.0.0, RegionSplitPolicy can be instantiated on the Master-side so the
50 * Phoenix local-indexer can block default hbase behavior. This is an exotic usage. Should not
51 * trouble any other users of RegionSplitPolicy.
53 protected HRegion region
;
56 * Upon construction, this method will be called with the region
57 * to be governed. It will be called once and only once.
59 protected void configureForRegion(HRegion region
) {
60 Preconditions
.checkState(
62 "Policy already configured for region {}",
69 * @return true if the specified region should be split.
71 protected abstract boolean shouldSplit();
74 * @return the key at which the region should be split, or null
75 * if it cannot be split. This will only be called if shouldSplit
76 * previously returned true.
78 protected byte[] getSplitPoint() {
79 byte[] explicitSplitPoint
= this.region
.getExplicitSplitPoint();
80 if (explicitSplitPoint
!= null) {
81 return explicitSplitPoint
;
83 List
<HStore
> stores
= region
.getStores();
85 byte[] splitPointFromLargestStore
= null;
86 long largestStoreSize
= 0;
87 for (HStore s
: stores
) {
88 Optional
<byte[]> splitPoint
= s
.getSplitPoint();
89 // Store also returns null if it has references as way of indicating it is not splittable
90 long storeSize
= s
.getSize();
91 if (splitPoint
.isPresent() && largestStoreSize
< storeSize
) {
92 splitPointFromLargestStore
= splitPoint
.get();
93 largestStoreSize
= storeSize
;
97 return splitPointFromLargestStore
;
101 * Create the RegionSplitPolicy configured for the given table.
104 * @return a RegionSplitPolicy
105 * @throws IOException
107 public static RegionSplitPolicy
create(HRegion region
,
108 Configuration conf
) throws IOException
{
109 Preconditions
.checkNotNull(region
, "Region should not be null.");
110 Class
<?
extends RegionSplitPolicy
> clazz
= getSplitPolicyClass(
111 region
.getTableDescriptor(), conf
);
112 RegionSplitPolicy policy
= ReflectionUtils
.newInstance(clazz
, conf
);
113 policy
.configureForRegion(region
);
117 public static Class
<?
extends RegionSplitPolicy
> getSplitPolicyClass(
118 TableDescriptor htd
, Configuration conf
) throws IOException
{
119 String className
= htd
.getRegionSplitPolicyClassName();
120 if (className
== null) {
121 className
= conf
.get(HConstants
.HBASE_REGION_SPLIT_POLICY_KEY
,
122 DEFAULT_SPLIT_POLICY_CLASS
.getName());
126 Class
<?
extends RegionSplitPolicy
> clazz
=
127 Class
.forName(className
).asSubclass(RegionSplitPolicy
.class);
129 } catch (Exception e
) {
130 throw new IOException(
131 "Unable to load configured region split policy '" +
132 className
+ "' for table '" + htd
.getTableName() + "'",
138 * In {@link HRegionFileSystem#splitStoreFile(org.apache.hadoop.hbase.client.RegionInfo, String,
139 * HStoreFile, byte[], boolean, RegionSplitPolicy)} we are not creating the split reference
140 * if split row does not lie inside the StoreFile range. But in some use cases we may need to
141 * create the split reference even when the split row does not lie inside the StoreFile range.
142 * This method can be used to decide, whether to skip the the StoreFile range check or not.
144 * <p>This method is not for general use. It is a mechanism put in place by Phoenix
145 * local indexing to defeat standard hbase behaviors. Phoenix local indices are very likely
146 * the only folks who would make use of this method. On the Master-side, we will instantiate
147 * a RegionSplitPolicy instance and run this method ONLY... none of the others make sense
148 * on the Master-side.</p>
150 * TODO: Shutdown this phoenix specialization or do it via some other means.
151 * @return whether to skip the StoreFile range check or not
153 protected boolean skipStoreFileRangeCheck(String familyName
) {