HBASE-26481 Consider rolling upgrading from old region replication framework (#3880)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / client / TestAsyncAdminWithRegionReplicas.java
blob81dafae9f9759037e0f41e8ad7ecbe36ad57da9a
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.client;
20 import static org.hamcrest.CoreMatchers.instanceOf;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotEquals;
25 import java.io.IOException;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.concurrent.ExecutionException;
29 import java.util.stream.Collectors;
30 import org.apache.hadoop.hbase.HBaseClassTestRule;
31 import org.apache.hadoop.hbase.HBaseTestingUtil;
32 import org.apache.hadoop.hbase.HConstants;
33 import org.apache.hadoop.hbase.HRegionLocation;
34 import org.apache.hadoop.hbase.TableName;
35 import org.apache.hadoop.hbase.testclassification.ClientTests;
36 import org.apache.hadoop.hbase.testclassification.LargeTests;
37 import org.apache.hadoop.hbase.util.Bytes;
38 import org.junit.BeforeClass;
39 import org.junit.ClassRule;
40 import org.junit.Test;
41 import org.junit.experimental.categories.Category;
42 import org.junit.runner.RunWith;
43 import org.junit.runners.Parameterized;
45 @RunWith(Parameterized.class)
46 @Category({ LargeTests.class, ClientTests.class })
47 public class TestAsyncAdminWithRegionReplicas extends TestAsyncAdminBase {
49 @ClassRule
50 public static final HBaseClassTestRule CLASS_RULE =
51 HBaseClassTestRule.forClass(TestAsyncAdminWithRegionReplicas.class);
53 @BeforeClass
54 public static void setUpBeforeClass() throws Exception {
55 TestAsyncAdminBase.setUpBeforeClass();
56 HBaseTestingUtil.setReplicas(TEST_UTIL.getAdmin(), TableName.META_TABLE_NAME, 3);
57 try (ConnectionRegistry registry =
58 ConnectionRegistryFactory.getRegistry(TEST_UTIL.getConfiguration())) {
59 RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(TEST_UTIL, registry);
63 private void testMoveNonDefaultReplica(TableName tableName)
64 throws InterruptedException, ExecutionException {
65 AsyncTableRegionLocator locator = ASYNC_CONN.getRegionLocator(tableName);
66 List<HRegionLocation> locs = locator.getAllRegionLocations().get();
67 // try region name
68 admin.move(locs.get(1).getRegion().getRegionName()).get();
69 assertNotEquals(locs.get(1).getServerName(),
70 locator.getRegionLocation(HConstants.EMPTY_START_ROW, 1, true).get());
71 // try encoded region name
72 admin.move(locs.get(2).getRegion().getEncodedNameAsBytes()).get();
73 assertNotEquals(locs.get(2).getServerName(),
74 locator.getRegionLocation(HConstants.EMPTY_START_ROW, 2, true).get());
77 @Test
78 public void testMoveNonDefaultReplica()
79 throws InterruptedException, ExecutionException, IOException {
80 createTableWithDefaultConf(tableName, 3);
81 testMoveNonDefaultReplica(tableName);
82 testMoveNonDefaultReplica(TableName.META_TABLE_NAME);
85 @Test
86 public void testSplitNonDefaultReplica()
87 throws InterruptedException, ExecutionException, IOException {
88 createTableWithDefaultConf(tableName, 3);
89 List<HRegionLocation> locs =
90 ASYNC_CONN.getRegionLocator(tableName).getAllRegionLocations().get();
91 try {
92 admin.splitRegion(locs.get(1).getRegion().getRegionName()).get();
93 } catch (ExecutionException e) {
94 assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
96 try {
97 admin.splitRegion(locs.get(2).getRegion().getEncodedNameAsBytes()).get();
98 } catch (ExecutionException e) {
99 assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
103 @Test
104 public void testMergeNonDefaultReplicas()
105 throws InterruptedException, ExecutionException, IOException {
106 byte[][] splitRows = new byte[][] { Bytes.toBytes(0) };
107 createTableWithDefaultConf(tableName, 3, splitRows);
108 List<HRegionLocation> locs =
109 ASYNC_CONN.getRegionLocator(tableName).getAllRegionLocations().get();
110 assertEquals(6, locs.size());
111 Map<Integer, List<RegionInfo>> replicaId2RegionInfo = locs.stream()
112 .map(HRegionLocation::getRegion).collect(Collectors.groupingBy(RegionInfo::getReplicaId));
113 List<RegionInfo> replicaOnes = replicaId2RegionInfo.get(1);
114 try {
115 admin
116 .mergeRegions(replicaOnes.get(0).getRegionName(), replicaOnes.get(1).getRegionName(), false)
117 .get();
118 } catch (ExecutionException e) {
119 assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
121 List<RegionInfo> replicaTwos = replicaId2RegionInfo.get(2);
122 try {
123 admin
124 .mergeRegions(replicaTwos.get(0).getRegionName(), replicaTwos.get(1).getRegionName(), false)
125 .get();
126 } catch (ExecutionException e) {
127 assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
131 @Test
132 public void testCloneTableSchema() throws IOException, InterruptedException, ExecutionException {
133 createTableWithDefaultConf(tableName, 3);
134 admin.cloneTableSchema(tableName, TableName.valueOf(tableName.getNameAsString() + "_new"), true)
135 .get();
138 @Test
139 public void testGetTableRegions() throws InterruptedException, ExecutionException, IOException {
140 List<RegionInfo> metaRegions = admin.getRegions(TableName.META_TABLE_NAME).get();
141 assertEquals(3, metaRegions.size());
142 for (int i = 0; i < 3; i++) {
143 RegionInfo metaRegion = metaRegions.get(i);
144 assertEquals(TableName.META_TABLE_NAME, metaRegion.getTable());
145 assertEquals(i, metaRegion.getReplicaId());
147 createTableWithDefaultConf(tableName, 3);
148 List<RegionInfo> regions = admin.getRegions(tableName).get();
149 assertEquals(3, metaRegions.size());
150 for (int i = 0; i < 3; i++) {
151 RegionInfo region = regions.get(i);
152 assertEquals(tableName, region.getTable());
153 assertEquals(i, region.getReplicaId());