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