HBASE-25660 Print split policy in use on Region open (as well as split policy vitals...
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / regionserver / KeyPrefixRegionSplitPolicy.java
blob29c7d11ac1d47feaf8782b2231aaeeb14e7725e2
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.util.Arrays;
22 import org.apache.yetus.audience.InterfaceAudience;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 /**
27 * A custom RegionSplitPolicy implementing a SplitPolicy that groups
28 * rows by a prefix of the row-key
30 * This ensures that a region is not split "inside" a prefix of a row key.
31 * I.e. rows can be co-located in a region by their prefix.
33 @InterfaceAudience.Private
34 public class KeyPrefixRegionSplitPolicy extends IncreasingToUpperBoundRegionSplitPolicy {
35 private static final Logger LOG = LoggerFactory
36 .getLogger(KeyPrefixRegionSplitPolicy.class);
37 @Deprecated
38 public static final String PREFIX_LENGTH_KEY_DEPRECATED = "prefix_split_key_policy.prefix_length";
39 public static final String PREFIX_LENGTH_KEY = "KeyPrefixRegionSplitPolicy.prefix_length";
41 private int prefixLength = 0;
43 @Override
44 public String toString() {
45 return "KeyPrefixRegionSplitPolicy{" + "prefixLength=" + prefixLength + ", " +
46 super.toString() + '}';
49 @Override
50 protected void configureForRegion(HRegion region) {
51 super.configureForRegion(region);
52 prefixLength = 0;
54 // read the prefix length from the table descriptor
55 String prefixLengthString = region.getTableDescriptor().getValue(
56 PREFIX_LENGTH_KEY);
57 if (prefixLengthString == null) {
58 //read the deprecated value
59 prefixLengthString = region.getTableDescriptor().getValue(PREFIX_LENGTH_KEY_DEPRECATED);
60 if (prefixLengthString == null) {
61 LOG.error(PREFIX_LENGTH_KEY + " not specified for table "
62 + region.getTableDescriptor().getTableName()
63 + ". Using default RegionSplitPolicy");
64 return;
67 try {
68 prefixLength = Integer.parseInt(prefixLengthString);
69 } catch (NumberFormatException nfe) {
70 /* Differentiate NumberFormatException from an invalid value range reported below. */
71 LOG.error("Number format exception when parsing " + PREFIX_LENGTH_KEY + " for table "
72 + region.getTableDescriptor().getTableName() + ":"
73 + prefixLengthString + ". " + nfe);
74 return;
76 if (prefixLength <= 0) {
77 LOG.error("Invalid value for " + PREFIX_LENGTH_KEY + " for table "
78 + region.getTableDescriptor().getTableName() + ":"
79 + prefixLengthString + ". Using default RegionSplitPolicy");
83 @Override
84 protected byte[] getSplitPoint() {
85 byte[] splitPoint = super.getSplitPoint();
86 if (prefixLength > 0 && splitPoint != null && splitPoint.length > 0) {
87 // group split keys by a prefix
88 return Arrays.copyOf(splitPoint,
89 Math.min(prefixLength, splitPoint.length));
90 } else {
91 return splitPoint;