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.MatcherAssert.assertThat;
021import static org.hamcrest.Matchers.containsString;
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertFalse;
024import static org.junit.Assert.assertNotEquals;
025import static org.junit.Assert.assertThrows;
026import static org.junit.Assert.assertTrue;
027
028import java.io.IOException;
029import java.util.ArrayList;
030import java.util.List;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.ServerName;
033import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
034import org.apache.hadoop.hbase.testclassification.ClientTests;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.zookeeper.ZKUtil;
037import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
038import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043@Category({ MediumTests.class, ClientTests.class })
044public class TestAdmin4 extends TestAdminBase {
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestAdmin4.class);
047
048  // For HBASE-24208
049  @Test
050  public void testDecommissionAndStopRegionServers() throws Exception {
051    List<ServerName> decommissionedRegionServers = ADMIN.listDecommissionedRegionServers();
052    assertTrue(decommissionedRegionServers.isEmpty());
053
054    ArrayList<ServerName> clusterRegionServers = new ArrayList<>(ADMIN.getRegionServers(true));
055
056    List<ServerName> serversToDecommission = new ArrayList<ServerName>();
057    serversToDecommission.add(clusterRegionServers.get(0));
058
059    // Decommission
060    ADMIN.decommissionRegionServers(serversToDecommission, true);
061    assertEquals(1, ADMIN.listDecommissionedRegionServers().size());
062
063    // Stop decommissioned region server and verify it is removed from draining znode
064    ServerName serverName = serversToDecommission.get(0);
065    ADMIN.stopRegionServer(serverName.getHostname() + ":" + serverName.getPort());
066    assertNotEquals("RS not removed from decommissioned list", -1,
067      TEST_UTIL.waitFor(10000, () -> ADMIN.listDecommissionedRegionServers().isEmpty()));
068    ZKWatcher zkw = TEST_UTIL.getZooKeeperWatcher();
069    assertEquals(-1, ZKUtil.checkExists(zkw,
070      ZNodePaths.joinZNode(zkw.getZNodePaths().drainingZNode, serverName.getServerName())));
071  }
072
073  @Test
074  public void testReplicationPeerModificationSwitch() throws Exception {
075    assertTrue(ADMIN.isReplicationPeerModificationEnabled());
076    try {
077      // disable modification, should returns true as it is enabled by default and the above
078      // assertion has confirmed it
079      assertTrue(ADMIN.replicationPeerModificationSwitch(false));
080      IOException error =
081        assertThrows(IOException.class, () -> ADMIN.addReplicationPeer("peer", ReplicationPeerConfig
082          .newBuilder().setClusterKey(TEST_UTIL.getRpcConnnectionURI()).build()));
083      assertThat(error.getCause().getMessage(),
084        containsString("Replication peer modification disabled"));
085      // enable again, and the previous value should be false
086      assertFalse(ADMIN.replicationPeerModificationSwitch(true));
087    } finally {
088      // always reset to avoid mess up other tests
089      ADMIN.replicationPeerModificationSwitch(true);
090    }
091  }
092}