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.hadoop.hbase.util.Bytes; 025import org.apache.yetus.audience.InterfaceAudience; 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028 029/** 030 * A {@link RegionSplitRestriction} implementation that groups rows by a prefix of the row-key with 031 * a delimiter. Only the first delimiter for the row key will define the prefix of the row key that 032 * is used for grouping. 033 * <p> 034 * This ensures that a region is not split "inside" a prefix of a row key. I.e. rows can be 035 * co-located in a region by their prefix. As an example, if you have row keys delimited with 036 * <code>_</code>, like <code>userid_eventtype_eventid</code>, and use prefix delimiter _, this 037 * split policy ensures that all rows starting with the same userid, belongs to the same region. 038 */ 039@InterfaceAudience.Private 040public class DelimitedKeyPrefixRegionSplitRestriction extends RegionSplitRestriction { 041 private static final Logger LOG = 042 LoggerFactory.getLogger(DelimitedKeyPrefixRegionSplitRestriction.class); 043 044 public static final String DELIMITER_KEY = 045 "hbase.regionserver.region.split_restriction.delimiter"; 046 047 private byte[] delimiter = null; 048 049 @Override 050 public void initialize(TableDescriptor tableDescriptor, Configuration conf) throws IOException { 051 String delimiterString = tableDescriptor.getValue(DELIMITER_KEY); 052 if (delimiterString == null || delimiterString.length() == 0) { 053 delimiterString = conf.get(DELIMITER_KEY); 054 if (delimiterString == null || delimiterString.length() == 0) { 055 LOG.error("{} not specified for table {}. " + "Using the default RegionSplitRestriction", 056 DELIMITER_KEY, tableDescriptor.getTableName()); 057 return; 058 } 059 } 060 delimiter = Bytes.toBytes(delimiterString); 061 } 062 063 @Override 064 public byte[] getRestrictedSplitPoint(byte[] splitPoint) { 065 if (delimiter != null) { 066 // find the first occurrence of delimiter in split point 067 int index = org.apache.hbase.thirdparty.com.google.common.primitives.Bytes.indexOf(splitPoint, 068 delimiter); 069 if (index < 0) { 070 LOG.warn("Delimiter {} not found for split key {}", Bytes.toString(delimiter), 071 Bytes.toStringBinary(splitPoint)); 072 return splitPoint; 073 } 074 075 // group split keys by a prefix 076 return Arrays.copyOf(splitPoint, Math.min(index, splitPoint.length)); 077 } else { 078 return splitPoint; 079 } 080 } 081}