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    List<ServerName> decommissionedRegionServers = admin.listDecommissionedRegionServers().get();
050    assertTrue(decommissionedRegionServers.isEmpty());
051
052    TEST_UTIL.createMultiRegionTable(tableName, FAMILY, 4);
053
054    ArrayList<ServerName> clusterRegionServers =
055        new ArrayList<>(admin.getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS)).get()
056          .getLiveServerMetrics().keySet());
057
058    assertEquals(2, clusterRegionServers.size());
059
060    HashMap<ServerName, List<RegionInfo>> serversToDecommssion = new HashMap<>();
061    // Get a server that has regions. We will decommission one of the servers,
062    // leaving one online.
063    int i;
064    for (i = 0; i < clusterRegionServers.size(); i++) {
065      List<RegionInfo> regionsOnServer = admin.getRegions(clusterRegionServers.get(i)).get();
066      if (regionsOnServer.size() > 0) {
067        serversToDecommssion.put(clusterRegionServers.get(i), regionsOnServer);
068        break;
069      }
070    }
071
072    clusterRegionServers.remove(i);
073    ServerName remainingServer = clusterRegionServers.get(0);
074
075    // Decommission
076    admin.decommissionRegionServers(new ArrayList<ServerName>(serversToDecommssion.keySet()),
077        true).get();
078    assertEquals(1, admin.listDecommissionedRegionServers().get().size());
079
080    // Verify the regions have been off the decommissioned servers, all on the remaining server.
081    for (ServerName server : serversToDecommssion.keySet()) {
082      for (RegionInfo region : serversToDecommssion.get(server)) {
083        TEST_UTIL.assertRegionOnServer(region, remainingServer, 10000);
084      }
085    }
086
087    // Recommission and load regions
088    for (ServerName server : serversToDecommssion.keySet()) {
089      List<byte[]> encodedRegionNames = serversToDecommssion.get(server).stream()
090          .map(region -> region.getEncodedNameAsBytes()).collect(Collectors.toList());
091      admin.recommissionRegionServer(server, encodedRegionNames).get();
092    }
093    assertTrue(admin.listDecommissionedRegionServers().get().isEmpty());
094    // Verify the regions have been moved to the recommissioned servers
095    for (ServerName server : serversToDecommssion.keySet()) {
096      for (RegionInfo region : serversToDecommssion.get(server)) {
097        TEST_UTIL.assertRegionOnServer(region, server, 10000);
098      }
099    }
100  }
101}