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 static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022import static org.mockito.Mockito.when;
023
024import java.io.IOException;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.client.TableDescriptor;
027import org.apache.hadoop.hbase.testclassification.RegionServerTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.junit.jupiter.api.BeforeEach;
031import org.junit.jupiter.api.Tag;
032import org.junit.jupiter.api.Test;
033import org.mockito.Mock;
034import org.mockito.MockitoAnnotations;
035
036@Tag(RegionServerTests.TAG)
037@Tag(SmallTests.TAG)
038public class TestRegionSplitRestriction {
039
040  Configuration conf;
041
042  @Mock
043  TableDescriptor tableDescriptor;
044
045  @BeforeEach
046  public void setup() {
047    MockitoAnnotations.initMocks(this);
048
049    conf = new Configuration();
050  }
051
052  @Test
053  public void testWhenTableDescriptorReturnsNoneType() throws IOException {
054    when(tableDescriptor.getValue(RegionSplitRestriction.RESTRICTION_TYPE_KEY))
055      .thenReturn(RegionSplitRestriction.RESTRICTION_TYPE_NONE);
056
057    RegionSplitRestriction splitRestriction = RegionSplitRestriction.create(tableDescriptor, conf);
058    assertTrue(splitRestriction instanceof NoRegionSplitRestriction);
059  }
060
061  @Test
062  public void testWhenTableDescriptorReturnsKeyPrefixType() throws IOException {
063    when(tableDescriptor.getValue(RegionSplitRestriction.RESTRICTION_TYPE_KEY))
064      .thenReturn(RegionSplitRestriction.RESTRICTION_TYPE_KEY_PREFIX);
065
066    RegionSplitRestriction splitRestriction = RegionSplitRestriction.create(tableDescriptor, conf);
067    assertTrue(splitRestriction instanceof KeyPrefixRegionSplitRestriction);
068  }
069
070  @Test
071  public void testWhenTableDescriptorReturnsDelimitedKeyPrefixType() throws IOException {
072    when(tableDescriptor.getValue(RegionSplitRestriction.RESTRICTION_TYPE_KEY))
073      .thenReturn(RegionSplitRestriction.RESTRICTION_TYPE_DELIMITED_KEY_PREFIX);
074
075    RegionSplitRestriction splitRestriction = RegionSplitRestriction.create(tableDescriptor, conf);
076    assertTrue(splitRestriction instanceof DelimitedKeyPrefixRegionSplitRestriction);
077  }
078
079  @Test
080  public void testWhenConfigurationReturnsNoneType() throws IOException {
081    conf.set(RegionSplitRestriction.RESTRICTION_TYPE_KEY,
082      RegionSplitRestriction.RESTRICTION_TYPE_NONE);
083
084    RegionSplitRestriction splitRestriction = RegionSplitRestriction.create(tableDescriptor, conf);
085    assertTrue(splitRestriction instanceof NoRegionSplitRestriction);
086  }
087
088  @Test
089  public void testWhenConfigurationReturnsKeyPrefixType() throws IOException {
090    conf.set(RegionSplitRestriction.RESTRICTION_TYPE_KEY,
091      RegionSplitRestriction.RESTRICTION_TYPE_KEY_PREFIX);
092
093    RegionSplitRestriction splitRestriction = RegionSplitRestriction.create(tableDescriptor, conf);
094    assertTrue(splitRestriction instanceof KeyPrefixRegionSplitRestriction);
095  }
096
097  @Test
098  public void testWhenConfigurationReturnsDelimitedKeyPrefixType() throws IOException {
099    conf.set(RegionSplitRestriction.RESTRICTION_TYPE_KEY,
100      RegionSplitRestriction.RESTRICTION_TYPE_DELIMITED_KEY_PREFIX);
101
102    RegionSplitRestriction splitRestriction = RegionSplitRestriction.create(tableDescriptor, conf);
103    assertTrue(splitRestriction instanceof DelimitedKeyPrefixRegionSplitRestriction);
104  }
105
106  @Test
107  public void testWhenTableDescriptorAndConfigurationReturnNull() throws IOException {
108    RegionSplitRestriction splitRestriction = RegionSplitRestriction.create(tableDescriptor, conf);
109    assertTrue(splitRestriction instanceof NoRegionSplitRestriction);
110  }
111
112  @Test
113  public void testWhenTableDescriptorReturnsInvalidType() throws IOException {
114    when(tableDescriptor.getValue(RegionSplitRestriction.RESTRICTION_TYPE_KEY))
115      .thenReturn("Invalid");
116
117    RegionSplitRestriction splitRestriction = RegionSplitRestriction.create(tableDescriptor, conf);
118    assertTrue(splitRestriction instanceof NoRegionSplitRestriction);
119  }
120
121  @Test
122  public void testNoneRegionSplitRestriction() throws IOException {
123    when(tableDescriptor.getValue(RegionSplitRestriction.RESTRICTION_TYPE_KEY))
124      .thenReturn(RegionSplitRestriction.RESTRICTION_TYPE_NONE);
125
126    NoRegionSplitRestriction noRegionSplitRestriction =
127      (NoRegionSplitRestriction) RegionSplitRestriction.create(tableDescriptor, conf);
128
129    byte[] restrictedSplit =
130      noRegionSplitRestriction.getRestrictedSplitPoint(Bytes.toBytes("abcd"));
131    assertEquals("abcd", Bytes.toString(restrictedSplit));
132  }
133
134  @Test
135  public void testKeyPrefixRegionSplitRestriction() throws IOException {
136    when(tableDescriptor.getValue(RegionSplitRestriction.RESTRICTION_TYPE_KEY))
137      .thenReturn(RegionSplitRestriction.RESTRICTION_TYPE_KEY_PREFIX);
138    when(tableDescriptor.getValue(KeyPrefixRegionSplitRestriction.PREFIX_LENGTH_KEY))
139      .thenReturn("2");
140
141    KeyPrefixRegionSplitRestriction keyPrefixRegionSplitRestriction =
142      (KeyPrefixRegionSplitRestriction) RegionSplitRestriction.create(tableDescriptor, conf);
143
144    byte[] restrictedSplit =
145      keyPrefixRegionSplitRestriction.getRestrictedSplitPoint(Bytes.toBytes("abcd"));
146    assertEquals("ab", Bytes.toString(restrictedSplit));
147
148    restrictedSplit = keyPrefixRegionSplitRestriction.getRestrictedSplitPoint(Bytes.toBytes("a"));
149    assertEquals("a", Bytes.toString(restrictedSplit));
150  }
151
152  @Test
153  public void testDelimitedKeyPrefixRegionSplitRestriction() throws IOException {
154    when(tableDescriptor.getValue(RegionSplitRestriction.RESTRICTION_TYPE_KEY))
155      .thenReturn(RegionSplitRestriction.RESTRICTION_TYPE_DELIMITED_KEY_PREFIX);
156    when(tableDescriptor.getValue(DelimitedKeyPrefixRegionSplitRestriction.DELIMITER_KEY))
157      .thenReturn(",");
158
159    DelimitedKeyPrefixRegionSplitRestriction delimitedKeyPrefixRegionSplitRestriction =
160      (DelimitedKeyPrefixRegionSplitRestriction) RegionSplitRestriction.create(tableDescriptor,
161        conf);
162
163    byte[] restrictedSplit =
164      delimitedKeyPrefixRegionSplitRestriction.getRestrictedSplitPoint(Bytes.toBytes("ab,cd"));
165    assertEquals("ab", Bytes.toString(restrictedSplit));
166
167    restrictedSplit =
168      delimitedKeyPrefixRegionSplitRestriction.getRestrictedSplitPoint(Bytes.toBytes("ijk"));
169    assertEquals("ijk", Bytes.toString(restrictedSplit));
170  }
171}