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.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.util.ArrayList;
024import java.util.EnumSet;
025import java.util.HashMap;
026import java.util.List;
027import java.util.stream.Collectors;
028import org.apache.hadoop.hbase.ClusterMetrics.Option;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.ServerName;
031import org.apache.hadoop.hbase.testclassification.ClientTests;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036import org.junit.runner.RunWith;
037import org.junit.runners.Parameterized;
038
039@RunWith(Parameterized.class)
040@Category({ ClientTests.class, MediumTests.class })
041public class TestAsyncDecommissionAdminApi extends TestAsyncAdminBase {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045      HBaseClassTestRule.forClass(TestAsyncDecommissionAdminApi.class);
046
047  @Test
048  public void testAsyncDecommissionRegionServers() throws Exception {
049    admin.balancerSwitch(false, true);
050    List<ServerName> decommissionedRegionServers = admin.listDecommissionedRegionServers().get();
051    assertTrue(decommissionedRegionServers.isEmpty());
052
053    TEST_UTIL.createMultiRegionTable(tableName, FAMILY, 4);
054
055    ArrayList<ServerName> clusterRegionServers =
056        new ArrayList<>(admin.getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS)).get()
057          .getLiveServerMetrics().keySet());
058
059    assertEquals(TEST_UTIL.getHBaseCluster().getLiveRegionServerThreads().size(),
060      clusterRegionServers.size());
061
062    HashMap<ServerName, List<RegionInfo>> serversToDecommssion = new HashMap<>();
063    // Get a server that has regions. We will decommission one of the servers,
064    // leaving one online.
065    int i;
066    for (i = 0; i < clusterRegionServers.size(); i++) {
067      List<RegionInfo> regionsOnServer = admin.getRegions(clusterRegionServers.get(i)).get();
068      if (regionsOnServer.size() > 0) {
069        serversToDecommssion.put(clusterRegionServers.get(i), regionsOnServer);
070        break;
071      }
072    }
073
074    clusterRegionServers.remove(i);
075    ServerName remainingServer = clusterRegionServers.get(0);
076
077    // Decommission
078    admin.decommissionRegionServers(new ArrayList<ServerName>(serversToDecommssion.keySet()),
079        true).get();
080    assertEquals(1, admin.listDecommissionedRegionServers().get().size());
081
082    // Verify the regions have been off the decommissioned servers, all on the remaining server.
083    for (ServerName server : serversToDecommssion.keySet()) {
084      for (RegionInfo region : serversToDecommssion.get(server)) {
085        TEST_UTIL.assertRegionOnServer(region, remainingServer, 10000);
086      }
087    }
088
089    // Maybe the TRSP is still not finished at master side, since the reportRegionTransition just
090    // updates the procedure store, and we still need to wake up the procedure and execute it in the
091    // procedure executor, which is asynchronous
092    TEST_UTIL.waitUntilNoRegionsInTransition(10000);
093
094    // Recommission and load regions
095    for (ServerName server : serversToDecommssion.keySet()) {
096      List<byte[]> encodedRegionNames = serversToDecommssion.get(server).stream()
097          .map(region -> region.getEncodedNameAsBytes()).collect(Collectors.toList());
098      admin.recommissionRegionServer(server, encodedRegionNames).get();
099    }
100    assertTrue(admin.listDecommissionedRegionServers().get().isEmpty());
101    // Verify the regions have been moved to the recommissioned servers
102    for (ServerName server : serversToDecommssion.keySet()) {
103      for (RegionInfo region : serversToDecommssion.get(server)) {
104        TEST_UTIL.assertRegionOnServer(region, server, 10000);
105      }
106    }
107  }
108}