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.io.IOException; 021import java.util.Arrays; 022import org.apache.hadoop.conf.Configuration; 023import org.apache.hadoop.hbase.client.TableDescriptor; 024import org.apache.yetus.audience.InterfaceAudience; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028/** 029 * A {@link RegionSplitRestriction} implementation that groups rows by a prefix of the row-key. 030 * <p> 031 * This ensures that a region is not split "inside" a prefix of a row key. I.e. rows can be 032 * co-located in a region by their prefix. 033 */ 034@InterfaceAudience.Private 035public class KeyPrefixRegionSplitRestriction extends RegionSplitRestriction { 036 private static final Logger LOG = LoggerFactory.getLogger(KeyPrefixRegionSplitRestriction.class); 037 038 public static final String PREFIX_LENGTH_KEY = 039 "hbase.regionserver.region.split_restriction.prefix_length"; 040 041 private int prefixLength; 042 043 @Override 044 public void initialize(TableDescriptor tableDescriptor, Configuration conf) throws IOException { 045 String prefixLengthString = tableDescriptor.getValue(PREFIX_LENGTH_KEY); 046 if (prefixLengthString == null) { 047 prefixLengthString = conf.get(PREFIX_LENGTH_KEY); 048 if (prefixLengthString == null) { 049 LOG.error("{} not specified for table {}. " + "Using the default RegionSplitRestriction", 050 PREFIX_LENGTH_KEY, tableDescriptor.getTableName()); 051 return; 052 } 053 } 054 try { 055 prefixLength = Integer.parseInt(prefixLengthString); 056 } catch (NumberFormatException ignored) { 057 } 058 if (prefixLength <= 0) { 059 LOG.error( 060 "Invalid value for {} for table {}:{}. " + "Using the default RegionSplitRestriction", 061 PREFIX_LENGTH_KEY, tableDescriptor.getTableName(), prefixLengthString); 062 } 063 } 064 065 @Override 066 public byte[] getRestrictedSplitPoint(byte[] splitPoint) { 067 if (prefixLength > 0) { 068 // group split keys by a prefix 069 return Arrays.copyOf(splitPoint, Math.min(prefixLength, splitPoint.length)); 070 } else { 071 return splitPoint; 072 } 073 } 074}