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