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.apache.hadoop.hbase.client.AsyncConnectionConfiguration.START_LOG_ERRORS_AFTER_COUNT_KEY;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.util.concurrent.TimeUnit;
025import java.util.function.Supplier;
026import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.master.HMaster;
029import org.apache.hadoop.hbase.regionserver.HRegionServer;
030import org.apache.hadoop.hbase.testclassification.ClientTests;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.junit.jupiter.api.AfterAll;
033import org.junit.jupiter.api.AfterEach;
034import org.junit.jupiter.api.BeforeAll;
035import org.junit.jupiter.api.BeforeEach;
036import org.junit.jupiter.api.Tag;
037import org.junit.jupiter.api.TestInfo;
038import org.junit.jupiter.api.TestTemplate;
039
040import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
041
042/**
043 * Only used to test stopMaster/stopRegionServer/shutdown methods.
044 */
045@Tag(ClientTests.TAG)
046@Tag(MediumTests.TAG)
047@HBaseParameterizedTestTemplate(name = "{index}: policy = {0}")
048public class TestAsyncClusterAdminApi2 extends TestAsyncAdminBase {
049
050  public TestAsyncClusterAdminApi2(Supplier<AsyncAdmin> admin) {
051    super(admin);
052  }
053
054  @BeforeAll
055  public static void setUpBeforeClass() throws Exception {
056    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, 60000);
057    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, 120000);
058    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
059    TEST_UTIL.getConfiguration().setInt(START_LOG_ERRORS_AFTER_COUNT_KEY, 0);
060  }
061
062  @AfterAll
063  public static void tearDownAfterClass() throws Exception {
064    // do nothing
065  }
066
067  @BeforeEach
068  @Override
069  public void setUp(TestInfo testInfo) throws Exception {
070    TEST_UTIL.startMiniCluster(3);
071    ASYNC_CONN = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get();
072    admin = ASYNC_CONN.getAdmin();
073  }
074
075  @AfterEach
076  @Override
077  public void tearDown() throws Exception {
078    Closeables.close(ASYNC_CONN, true);
079    TEST_UTIL.shutdownMiniCluster();
080  }
081
082  @TestTemplate
083  public void testStop() throws Exception {
084    HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0);
085    assertFalse(rs.isStopped());
086    admin.stopRegionServer(rs.getServerName()).join();
087    assertTrue(rs.isStopped());
088
089    HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
090    assertFalse(master.isStopped());
091    admin.stopMaster().join();
092    assertTrue(master.isStopped());
093  }
094
095  @TestTemplate
096  public void testShutdown() throws Exception {
097    TEST_UTIL.getMiniHBaseCluster().getMasterThreads().forEach(thread -> {
098      assertFalse(thread.getMaster().isStopped());
099    });
100    TEST_UTIL.getMiniHBaseCluster().getRegionServerThreads().forEach(thread -> {
101      assertFalse(thread.getRegionServer().isStopped());
102    });
103
104    admin.shutdown().join();
105    TEST_UTIL.getMiniHBaseCluster().getMasterThreads().forEach(thread -> {
106      while (!thread.getMaster().isStopped()) {
107        trySleep(100, TimeUnit.MILLISECONDS);
108      }
109    });
110    TEST_UTIL.getMiniHBaseCluster().getRegionServerThreads().forEach(thread -> {
111      while (!thread.getRegionServer().isStopped()) {
112        trySleep(100, TimeUnit.MILLISECONDS);
113      }
114    });
115  }
116
117  private void trySleep(long timeout, TimeUnit unit) {
118    try {
119      unit.sleep(timeout);
120    } catch (InterruptedException e) {
121    }
122  }
123}