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