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.testclassification.ClientTests;
036import org.apache.hadoop.hbase.testclassification.LargeTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042import org.junit.runner.RunWith;
043import org.junit.runners.Parameterized;
044
045@RunWith(Parameterized.class)
046@Category({ LargeTests.class, ClientTests.class })
047public class TestAsyncAdminWithRegionReplicas extends TestAsyncAdminBase {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestAsyncAdminWithRegionReplicas.class);
052
053  @BeforeClass
054  public static void setUpBeforeClass() throws Exception {
055    TestAsyncAdminBase.setUpBeforeClass();
056    HBaseTestingUtility.setReplicas(TEST_UTIL.getAdmin(), TableName.META_TABLE_NAME, 3);
057    try (ConnectionRegistry registry =
058      ConnectionRegistryFactory.getRegistry(TEST_UTIL.getConfiguration())) {
059      RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(TEST_UTIL, registry);
060    }
061  }
062
063  private void testMoveNonDefaultReplica(TableName tableName)
064    throws InterruptedException, ExecutionException {
065    AsyncTableRegionLocator locator = ASYNC_CONN.getRegionLocator(tableName);
066    List<HRegionLocation> locs = locator.getAllRegionLocations().get();
067    // try region name
068    admin.move(locs.get(1).getRegion().getRegionName()).get();
069    assertNotEquals(locs.get(1).getServerName(),
070      locator.getRegionLocation(HConstants.EMPTY_START_ROW, 1, true).get());
071    // try encoded region name
072    admin.move(locs.get(2).getRegion().getEncodedNameAsBytes()).get();
073    assertNotEquals(locs.get(2).getServerName(),
074      locator.getRegionLocation(HConstants.EMPTY_START_ROW, 2, true).get());
075  }
076
077  @Test
078  public void testMoveNonDefaultReplica()
079    throws InterruptedException, ExecutionException, IOException {
080    createTableWithDefaultConf(tableName, 3);
081    testMoveNonDefaultReplica(tableName);
082    testMoveNonDefaultReplica(TableName.META_TABLE_NAME);
083  }
084
085  @Test
086  public void testSplitNonDefaultReplica()
087    throws InterruptedException, ExecutionException, IOException {
088    createTableWithDefaultConf(tableName, 3);
089    List<HRegionLocation> locs =
090      ASYNC_CONN.getRegionLocator(tableName).getAllRegionLocations().get();
091    try {
092      admin.splitRegion(locs.get(1).getRegion().getRegionName()).get();
093    } catch (ExecutionException e) {
094      assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
095    }
096    try {
097      admin.splitRegion(locs.get(2).getRegion().getEncodedNameAsBytes()).get();
098    } catch (ExecutionException e) {
099      assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
100    }
101  }
102
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));
120    }
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));
128    }
129  }
130
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();
136  }
137
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());
146    }
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());
154    }
155  }
156}