001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import java.util.Arrays;
021import org.apache.yetus.audience.InterfaceAudience;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025/**
026 * A custom RegionSplitPolicy implementing a SplitPolicy that groups rows by a prefix of the row-key
027 * This ensures that a region is not split "inside" a prefix of a row key. I.e. rows can be
028 * co-located in a region by their prefix.
029 * @deprecated since 3.0.0 and will be removed in 4.0.0. Use {@link RegionSplitRestriction},
030 *             instead.
031 */
032@Deprecated
033@InterfaceAudience.Private
034public class KeyPrefixRegionSplitPolicy extends IncreasingToUpperBoundRegionSplitPolicy {
035  private static final Logger LOG = LoggerFactory.getLogger(KeyPrefixRegionSplitPolicy.class);
036  @Deprecated
037  public static final String PREFIX_LENGTH_KEY_DEPRECATED = "prefix_split_key_policy.prefix_length";
038  public static final String PREFIX_LENGTH_KEY = "KeyPrefixRegionSplitPolicy.prefix_length";
039
040  private int prefixLength = 0;
041
042  @Override
043  public String toString() {
044    return "KeyPrefixRegionSplitPolicy{" + "prefixLength=" + prefixLength + ", " + super.toString()
045      + '}';
046  }
047
048  @Override
049  protected void configureForRegion(HRegion region) {
050    super.configureForRegion(region);
051    prefixLength = 0;
052
053    // read the prefix length from the table descriptor
054    String prefixLengthString = region.getTableDescriptor().getValue(PREFIX_LENGTH_KEY);
055    if (prefixLengthString == null) {
056      // read the deprecated value
057      prefixLengthString = region.getTableDescriptor().getValue(PREFIX_LENGTH_KEY_DEPRECATED);
058      if (prefixLengthString == null) {
059        LOG.error(PREFIX_LENGTH_KEY + " not specified for table "
060          + region.getTableDescriptor().getTableName() + ". Using default RegionSplitPolicy");
061        return;
062      }
063    }
064    try {
065      prefixLength = Integer.parseInt(prefixLengthString);
066    } catch (NumberFormatException nfe) {
067      /* Differentiate NumberFormatException from an invalid value range reported below. */
068      LOG.error("Number format exception when parsing " + PREFIX_LENGTH_KEY + " for table "
069        + region.getTableDescriptor().getTableName() + ":" + prefixLengthString + ". " + nfe);
070      return;
071    }
072    if (prefixLength <= 0) {
073      LOG.error("Invalid value for " + PREFIX_LENGTH_KEY + " for table "
074        + region.getTableDescriptor().getTableName() + ":" + prefixLengthString
075        + ". Using default RegionSplitPolicy");
076    }
077  }
078
079  @Override
080  protected byte[] getSplitPoint() {
081    byte[] splitPoint = super.getSplitPoint();
082    if (prefixLength > 0 && splitPoint != null && splitPoint.length > 0) {
083      // group split keys by a prefix
084      return Arrays.copyOf(splitPoint, Math.min(prefixLength, splitPoint.length));
085    } else {
086      return splitPoint;
087    }
088  }
089}