HBASE-26412 Handle sink failure in RegionReplicationSink (#3815)
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / regionserver / RegionSplitPolicy.java
blobb59e0ac7083c20cf9d34d79c2f85d11647783f8c
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
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;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.conf.Configured;
25 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
26 import org.apache.hadoop.hbase.HConstants;
27 import org.apache.hadoop.hbase.client.TableDescriptor;
28 import org.apache.hadoop.util.ReflectionUtils;
29 import org.apache.yetus.audience.InterfaceAudience;
30 import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
33 /**
34 * A split policy determines when a Region should be split.
36 * @see SteppingSplitPolicy Default split policy since 2.0.0
37 * @see IncreasingToUpperBoundRegionSplitPolicy Default split policy since
38 * 0.94.0
39 * @see ConstantSizeRegionSplitPolicy Default split policy before 0.94.0
41 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
42 public abstract class RegionSplitPolicy extends Configured {
43 private static final Class<? extends RegionSplitPolicy>
44 DEFAULT_SPLIT_POLICY_CLASS = SteppingSplitPolicy.class;
46 /**
47 * The region configured for this split policy.
48 * As of hbase-2.0.0, RegionSplitPolicy can be instantiated on the Master-side so the
49 * Phoenix local-indexer can block default hbase behavior. This is an exotic usage. Should not
50 * trouble any other users of RegionSplitPolicy.
52 protected HRegion region;
54 /**
55 * Upon construction, this method will be called with the region
56 * to be governed. It will be called once and only once.
58 protected void configureForRegion(HRegion region) {
59 Preconditions.checkState(
60 this.region == null,
61 "Policy already configured for region {}",
62 this.region);
64 this.region = region;
67 /**
68 * @return true if the specified region should be split.
70 protected abstract boolean shouldSplit();
72 /**
73 * @return {@code true} if the specified region can be split.
75 protected boolean canSplit() {
76 return !region.getRegionInfo().isMetaRegion() && region.isAvailable() &&
77 region.getStores().stream().allMatch(HStore::canSplit);
80 /**
81 * @return the key at which the region should be split, or null
82 * if it cannot be split. This will only be called if shouldSplit
83 * previously returned true.
85 protected byte[] getSplitPoint() {
86 List<HStore> stores = region.getStores();
88 byte[] splitPointFromLargestStore = null;
89 long largestStoreSize = 0;
90 for (HStore s : stores) {
91 Optional<byte[]> splitPoint = s.getSplitPoint();
92 // Store also returns null if it has references as way of indicating it is not splittable
93 long storeSize = s.getSize();
94 if (splitPoint.isPresent() && largestStoreSize < storeSize) {
95 splitPointFromLargestStore = splitPoint.get();
96 largestStoreSize = storeSize;
100 return splitPointFromLargestStore;
104 * Create the RegionSplitPolicy configured for the given table.
105 * @param region
106 * @param conf
107 * @return a RegionSplitPolicy
108 * @throws IOException
110 public static RegionSplitPolicy create(HRegion region,
111 Configuration conf) throws IOException {
112 Preconditions.checkNotNull(region, "Region should not be null.");
113 Class<? extends RegionSplitPolicy> clazz = getSplitPolicyClass(
114 region.getTableDescriptor(), conf);
115 RegionSplitPolicy policy = ReflectionUtils.newInstance(clazz, conf);
116 policy.configureForRegion(region);
117 return policy;
120 public static Class<? extends RegionSplitPolicy> getSplitPolicyClass(
121 TableDescriptor htd, Configuration conf) throws IOException {
122 String className = htd.getRegionSplitPolicyClassName();
123 if (className == null) {
124 className = conf.get(HConstants.HBASE_REGION_SPLIT_POLICY_KEY,
125 DEFAULT_SPLIT_POLICY_CLASS.getName());
128 try {
129 Class<? extends RegionSplitPolicy> clazz =
130 Class.forName(className).asSubclass(RegionSplitPolicy.class);
131 return clazz;
132 } catch (Exception e) {
133 throw new IOException(
134 "Unable to load configured region split policy '" +
135 className + "' for table '" + htd.getTableName() + "'",
141 * In {@link HRegionFileSystem#splitStoreFile(org.apache.hadoop.hbase.client.RegionInfo, String,
142 * HStoreFile, byte[], boolean, RegionSplitPolicy)} we are not creating the split reference
143 * if split row does not lie inside the StoreFile range. But in some use cases we may need to
144 * create the split reference even when the split row does not lie inside the StoreFile range.
145 * This method can be used to decide, whether to skip the the StoreFile range check or not.
147 * <p>This method is not for general use. It is a mechanism put in place by Phoenix
148 * local indexing to defeat standard hbase behaviors. Phoenix local indices are very likely
149 * the only folks who would make use of this method. On the Master-side, we will instantiate
150 * a RegionSplitPolicy instance and run this method ONLY... none of the others make sense
151 * on the Master-side.</p>
153 * TODO: Shutdown this phoenix specialization or do it via some other means.
154 * @return whether to skip the StoreFile range check or not
156 protected boolean skipStoreFileRangeCheck(String familyName) {
157 return false;