HBASE-26567 Remove IndexType from ChunkCreator (#3947)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / regionserver / TestRegionReplicasWithModifyTable.java
blobbd0bbd77c8144822ce75765ccc07560e57db77de
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.regionserver;
20 import static org.junit.Assert.assertEquals;
22 import java.io.IOException;
23 import java.util.Arrays;
24 import java.util.List;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.HBaseClassTestRule;
27 import org.apache.hadoop.hbase.HBaseTestingUtil;
28 import org.apache.hadoop.hbase.HConstants;
29 import org.apache.hadoop.hbase.TableName;
30 import org.apache.hadoop.hbase.TableNameTestRule;
31 import org.apache.hadoop.hbase.client.TableDescriptor;
32 import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
33 import org.apache.hadoop.hbase.testclassification.MediumTests;
34 import org.apache.hadoop.hbase.testclassification.RegionServerTests;
35 import org.apache.hadoop.hbase.util.Bytes;
36 import org.apache.hadoop.hbase.util.RegionSplitter;
37 import org.junit.After;
38 import org.junit.AfterClass;
39 import org.junit.BeforeClass;
40 import org.junit.ClassRule;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.experimental.categories.Category;
44 import org.junit.runner.RunWith;
45 import org.junit.runners.Parameterized;
46 import org.junit.runners.Parameterized.Parameter;
47 import org.junit.runners.Parameterized.Parameters;
49 @RunWith(Parameterized.class)
50 @Category({ RegionServerTests.class, MediumTests.class })
51 public class TestRegionReplicasWithModifyTable {
53 @ClassRule
54 public static final HBaseClassTestRule CLASS_RULE =
55 HBaseClassTestRule.forClass(TestRegionReplicasWithModifyTable.class);
57 private static final int NB_SERVERS = 3;
59 private static final HBaseTestingUtil HTU = new HBaseTestingUtil();
60 private static final byte[] f = HConstants.CATALOG_FAMILY;
62 @Parameter
63 public boolean disableBeforeModifying;
65 @Rule
66 public TableNameTestRule name = new TableNameTestRule();
68 @Parameters
69 public static List<Object[]> params() {
70 return Arrays.asList(new Object[] { true }, new Object[] { false });
73 @BeforeClass
74 public static void before() throws Exception {
75 HTU.startMiniCluster(NB_SERVERS);
78 private void enableReplicationByModification(boolean withReplica, int initialReplicaCount,
79 int enableReplicaCount, int splitCount) throws IOException, InterruptedException {
80 TableName tableName = name.getTableName();
81 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
82 if (withReplica) {
83 builder.setRegionReplication(initialReplicaCount);
85 TableDescriptor htd = builder.build();
86 if (splitCount > 0) {
87 byte[][] splits = getSplits(splitCount);
88 HTU.createTable(htd, new byte[][] { f }, splits, new Configuration(HTU.getConfiguration()));
89 } else {
90 HTU.createTable(htd, new byte[][] { f }, (byte[][]) null,
91 new Configuration(HTU.getConfiguration()));
93 if (disableBeforeModifying) {
94 HTU.getAdmin().disableTable(tableName);
96 HBaseTestingUtil.setReplicas(HTU.getAdmin(), tableName, enableReplicaCount);
97 if (disableBeforeModifying) {
98 HTU.getAdmin().enableTable(tableName);
100 int expectedRegionCount;
101 if (splitCount > 0) {
102 expectedRegionCount = enableReplicaCount * splitCount;
103 } else {
104 expectedRegionCount = enableReplicaCount;
106 assertTotalRegions(expectedRegionCount);
109 private static byte[][] getSplits(int numRegions) {
110 RegionSplitter.UniformSplit split = new RegionSplitter.UniformSplit();
111 split.setFirstRow(Bytes.toBytes(0L));
112 split.setLastRow(Bytes.toBytes(Long.MAX_VALUE));
113 return split.split(numRegions);
116 @AfterClass
117 public static void afterClass() throws Exception {
118 HTU.shutdownMiniCluster();
121 @After
122 public void tearDown() throws IOException {
123 TableName tableName = name.getTableName();
124 HTU.getAdmin().disableTable(tableName);
125 HTU.getAdmin().deleteTable(tableName);
128 private void assertTotalRegions(int expected) {
129 int actual = HTU.getHBaseCluster().getRegions(name.getTableName()).size();
130 assertEquals(expected, actual);
133 @Test
134 public void testRegionReplicasUsingEnableTable() throws Exception {
135 enableReplicationByModification(false, 0, 3, 0);
138 @Test
139 public void testRegionReplicasUsingEnableTableForMultipleRegions() throws Exception {
140 enableReplicationByModification(false, 0, 3, 10);
143 @Test
144 public void testRegionReplicasByEnableTableWhenReplicaCountIsIncreased() throws Exception {
145 enableReplicationByModification(true, 2, 3, 0);
148 @Test
149 public void testRegionReplicasByEnableTableWhenReplicaCountIsDecreased() throws Exception {
150 enableReplicationByModification(true, 3, 2, 0);
153 @Test
154 public void testRegionReplicasByEnableTableWhenReplicaCountIsDecreasedWithMultipleRegions()
155 throws Exception {
156 enableReplicationByModification(true, 3, 2, 20);
159 @Test
160 public void testRegionReplicasByEnableTableWhenReplicaCountIsIncreasedWithmultipleRegions()
161 throws Exception {
162 enableReplicationByModification(true, 2, 3, 15);