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 = new ArrayList<>(admin
056      .getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS)).get().getLiveServerMetrics().keySet());
057
058    assertEquals(TEST_UTIL.getHBaseCluster().getLiveRegionServerThreads().size(),
059      clusterRegionServers.size());
060
061    HashMap<ServerName, List<RegionInfo>> serversToDecommssion = new HashMap<>();
062    // Get a server that has regions. We will decommission one of the servers,
063    // leaving one online.
064    int i;
065    for (i = 0; i < clusterRegionServers.size(); i++) {
066      List<RegionInfo> regionsOnServer = admin.getRegions(clusterRegionServers.get(i)).get();
067      if (regionsOnServer.size() > 0) {
068        serversToDecommssion.put(clusterRegionServers.get(i), regionsOnServer);
069        break;
070      }
071    }
072
073    clusterRegionServers.remove(i);
074    ServerName remainingServer = clusterRegionServers.get(0);
075
076    // Decommission
077    admin.decommissionRegionServers(new ArrayList<ServerName>(serversToDecommssion.keySet()), true)
078      .get();
079    assertEquals(1, admin.listDecommissionedRegionServers().get().size());
080
081    // Verify the regions have been off the decommissioned servers, all on the remaining server.
082    for (ServerName server : serversToDecommssion.keySet()) {
083      for (RegionInfo region : serversToDecommssion.get(server)) {
084        TEST_UTIL.assertRegionOnServer(region, remainingServer, 10000);
085      }
086    }
087
088    // Maybe the TRSP is still not finished at master side, since the reportRegionTransition just
089    // updates the procedure store, and we still need to wake up the procedure and execute it in the
090    // procedure executor, which is asynchronous
091    TEST_UTIL.waitUntilNoRegionsInTransition(10000);
092
093    // Recommission and load regions
094    for (ServerName server : serversToDecommssion.keySet()) {
095      List<byte[]> encodedRegionNames = serversToDecommssion.get(server).stream()
096        .map(region -> region.getEncodedNameAsBytes()).collect(Collectors.toList());
097      admin.recommissionRegionServer(server, encodedRegionNames).get();
098    }
099    assertTrue(admin.listDecommissionedRegionServers().get().isEmpty());
100    // Verify the regions have been moved to the recommissioned servers
101    for (ServerName server : serversToDecommssion.keySet()) {
102      for (RegionInfo region : serversToDecommssion.get(server)) {
103        TEST_UTIL.assertRegionOnServer(region, server, 10000);
104      }
105    }
106  }
107}