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.client; 019 020import static org.hamcrest.CoreMatchers.instanceOf; 021import static org.hamcrest.MatcherAssert.assertThat; 022import static org.junit.Assert.assertEquals; 023import static org.junit.Assert.assertNotEquals; 024 025import java.io.IOException; 026import java.util.List; 027import java.util.Map; 028import java.util.concurrent.ExecutionException; 029import java.util.stream.Collectors; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseTestingUtility; 032import org.apache.hadoop.hbase.HConstants; 033import org.apache.hadoop.hbase.HRegionLocation; 034import org.apache.hadoop.hbase.TableName; 035import org.apache.hadoop.hbase.security.User; 036import org.apache.hadoop.hbase.testclassification.ClientTests; 037import org.apache.hadoop.hbase.testclassification.LargeTests; 038import org.apache.hadoop.hbase.util.Bytes; 039import org.junit.BeforeClass; 040import org.junit.ClassRule; 041import org.junit.Test; 042import org.junit.experimental.categories.Category; 043import org.junit.runner.RunWith; 044import org.junit.runners.Parameterized; 045 046@RunWith(Parameterized.class) 047@Category({ LargeTests.class, ClientTests.class }) 048public class TestAsyncAdminWithRegionReplicas extends TestAsyncAdminBase { 049 050 @ClassRule 051 public static final HBaseClassTestRule CLASS_RULE = 052 HBaseClassTestRule.forClass(TestAsyncAdminWithRegionReplicas.class); 053 054 @BeforeClass 055 public static void setUpBeforeClass() throws Exception { 056 TestAsyncAdminBase.setUpBeforeClass(); 057 HBaseTestingUtility.setReplicas(TEST_UTIL.getAdmin(), TableName.META_TABLE_NAME, 3); 058 try (ConnectionRegistry registry = 059 ConnectionRegistryFactory.getRegistry(TEST_UTIL.getConfiguration(), User.getCurrent())) { 060 RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(TEST_UTIL, registry); 061 } 062 } 063 064 private void testMoveNonDefaultReplica(TableName tableName) 065 throws InterruptedException, ExecutionException { 066 AsyncTableRegionLocator locator = ASYNC_CONN.getRegionLocator(tableName); 067 List<HRegionLocation> locs = locator.getAllRegionLocations().get(); 068 // try region name 069 admin.move(locs.get(1).getRegion().getRegionName()).get(); 070 assertNotEquals(locs.get(1).getServerName(), 071 locator.getRegionLocation(HConstants.EMPTY_START_ROW, 1, true).get()); 072 // try encoded region name 073 admin.move(locs.get(2).getRegion().getEncodedNameAsBytes()).get(); 074 assertNotEquals(locs.get(2).getServerName(), 075 locator.getRegionLocation(HConstants.EMPTY_START_ROW, 2, true).get()); 076 } 077 078 @Test 079 public void testMoveNonDefaultReplica() 080 throws InterruptedException, ExecutionException, IOException { 081 createTableWithDefaultConf(tableName, 3); 082 testMoveNonDefaultReplica(tableName); 083 testMoveNonDefaultReplica(TableName.META_TABLE_NAME); 084 } 085 086 @Test 087 public void testSplitNonDefaultReplica() 088 throws InterruptedException, ExecutionException, IOException { 089 createTableWithDefaultConf(tableName, 3); 090 List<HRegionLocation> locs = 091 ASYNC_CONN.getRegionLocator(tableName).getAllRegionLocations().get(); 092 try { 093 admin.splitRegion(locs.get(1).getRegion().getRegionName()).get(); 094 } catch (ExecutionException e) { 095 assertThat(e.getCause(), instanceOf(IllegalArgumentException.class)); 096 } 097 try { 098 admin.splitRegion(locs.get(2).getRegion().getEncodedNameAsBytes()).get(); 099 } catch (ExecutionException e) { 100 assertThat(e.getCause(), instanceOf(IllegalArgumentException.class)); 101 } 102 } 103 104 @Test 105 public void testMergeNonDefaultReplicas() 106 throws InterruptedException, ExecutionException, IOException { 107 byte[][] splitRows = new byte[][] { Bytes.toBytes(0) }; 108 createTableWithDefaultConf(tableName, 3, splitRows); 109 List<HRegionLocation> locs = 110 ASYNC_CONN.getRegionLocator(tableName).getAllRegionLocations().get(); 111 assertEquals(6, locs.size()); 112 Map<Integer, List<RegionInfo>> replicaId2RegionInfo = locs.stream() 113 .map(HRegionLocation::getRegion).collect(Collectors.groupingBy(RegionInfo::getReplicaId)); 114 List<RegionInfo> replicaOnes = replicaId2RegionInfo.get(1); 115 try { 116 admin 117 .mergeRegions(replicaOnes.get(0).getRegionName(), replicaOnes.get(1).getRegionName(), false) 118 .get(); 119 } catch (ExecutionException e) { 120 assertThat(e.getCause(), instanceOf(IllegalArgumentException.class)); 121 } 122 List<RegionInfo> replicaTwos = replicaId2RegionInfo.get(2); 123 try { 124 admin 125 .mergeRegions(replicaTwos.get(0).getRegionName(), replicaTwos.get(1).getRegionName(), false) 126 .get(); 127 } catch (ExecutionException e) { 128 assertThat(e.getCause(), instanceOf(IllegalArgumentException.class)); 129 } 130 } 131 132 @Test 133 public void testCloneTableSchema() throws IOException, InterruptedException, ExecutionException { 134 createTableWithDefaultConf(tableName, 3); 135 admin.cloneTableSchema(tableName, TableName.valueOf(tableName.getNameAsString() + "_new"), true) 136 .get(); 137 } 138 139 @Test 140 public void testGetTableRegions() throws InterruptedException, ExecutionException, IOException { 141 List<RegionInfo> metaRegions = admin.getRegions(TableName.META_TABLE_NAME).get(); 142 assertEquals(3, metaRegions.size()); 143 for (int i = 0; i < 3; i++) { 144 RegionInfo metaRegion = metaRegions.get(i); 145 assertEquals(TableName.META_TABLE_NAME, metaRegion.getTable()); 146 assertEquals(i, metaRegion.getReplicaId()); 147 } 148 createTableWithDefaultConf(tableName, 3); 149 List<RegionInfo> regions = admin.getRegions(tableName).get(); 150 assertEquals(3, metaRegions.size()); 151 for (int i = 0; i < 3; i++) { 152 RegionInfo region = regions.get(i); 153 assertEquals(tableName, region.getTable()); 154 assertEquals(i, region.getReplicaId()); 155 } 156 } 157}