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.*;
021
022import java.util.ArrayList;
023import java.util.List;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.ServerName;
026import org.apache.hadoop.hbase.testclassification.ClientTests;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.zookeeper.ZKUtil;
029import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
030import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035@Category({ MediumTests.class, ClientTests.class })
036public class TestAdmin4 extends TestAdminBase {
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestAdmin4.class);
039
040  // For HBASE-24208
041  @Test
042  public void testDecommissionAndStopRegionServers() throws Exception {
043    List<ServerName> decommissionedRegionServers = ADMIN.listDecommissionedRegionServers();
044    assertTrue(decommissionedRegionServers.isEmpty());
045
046    ArrayList<ServerName> clusterRegionServers = new ArrayList<>(ADMIN.getRegionServers(true));
047
048    List<ServerName> serversToDecommission = new ArrayList<ServerName>();
049    serversToDecommission.add(clusterRegionServers.get(0));
050
051    // Decommission
052    ADMIN.decommissionRegionServers(serversToDecommission, true);
053    assertEquals(1, ADMIN.listDecommissionedRegionServers().size());
054
055    // Stop decommissioned region server and verify it is removed from draining znode
056    ServerName serverName = serversToDecommission.get(0);
057    ADMIN.stopRegionServer(serverName.getHostname() + ":" + serverName.getPort());
058    assertNotEquals("RS not removed from decommissioned list", -1,
059      TEST_UTIL.waitFor(10000, () -> ADMIN.listDecommissionedRegionServers().isEmpty()));
060    ZKWatcher zkw = TEST_UTIL.getZooKeeperWatcher();
061    assertEquals(-1, ZKUtil.checkExists(zkw,
062      ZNodePaths.joinZNode(zkw.getZNodePaths().drainingZNode, serverName.getServerName())));
063  }
064}