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
{
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
;
63 public boolean disableBeforeModifying
;
66 public TableNameTestRule name
= new TableNameTestRule();
69 public static List
<Object
[]> params() {
70 return Arrays
.asList(new Object
[] { true }, new Object
[] { false });
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
);
83 builder
.setRegionReplication(initialReplicaCount
);
85 TableDescriptor htd
= builder
.build();
87 byte[][] splits
= getSplits(splitCount
);
88 HTU
.createTable(htd
, new byte[][] { f
}, splits
, new Configuration(HTU
.getConfiguration()));
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
;
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
);
117 public static void afterClass() throws Exception
{
118 HTU
.shutdownMiniCluster();
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
);
134 public void testRegionReplicasUsingEnableTable() throws Exception
{
135 enableReplicationByModification(false, 0, 3, 0);
139 public void testRegionReplicasUsingEnableTableForMultipleRegions() throws Exception
{
140 enableReplicationByModification(false, 0, 3, 10);
144 public void testRegionReplicasByEnableTableWhenReplicaCountIsIncreased() throws Exception
{
145 enableReplicationByModification(true, 2, 3, 0);
149 public void testRegionReplicasByEnableTableWhenReplicaCountIsDecreased() throws Exception
{
150 enableReplicationByModification(true, 3, 2, 0);
154 public void testRegionReplicasByEnableTableWhenReplicaCountIsDecreasedWithMultipleRegions()
156 enableReplicationByModification(true, 3, 2, 20);
160 public void testRegionReplicasByEnableTableWhenReplicaCountIsIncreasedWithmultipleRegions()
162 enableReplicationByModification(true, 2, 3, 15);