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.AsyncProcess.START_LOG_ERRORS_AFTER_COUNT_KEY;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.util.concurrent.TimeUnit;
025import org.apache.commons.io.IOUtils;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
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.After;
033import org.junit.AfterClass;
034import org.junit.Before;
035import org.junit.BeforeClass;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039
040/**
041 * Only used to test stopMaster/stopRegionServer/shutdown methods.
042 */
043@Category({ ClientTests.class, MediumTests.class })
044public class TestAsyncClusterAdminApi2 extends TestAsyncAdminBase {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048      HBaseClassTestRule.forClass(TestAsyncClusterAdminApi2.class);
049
050  @BeforeClass
051  public static void setUpBeforeClass() throws Exception {
052    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, 60000);
053    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, 120000);
054    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
055    TEST_UTIL.getConfiguration().setInt(START_LOG_ERRORS_AFTER_COUNT_KEY, 0);
056  }
057
058  @AfterClass
059  public static void tearDownAfterClass() throws Exception {
060    // do nothing
061  }
062
063  @Before
064  @Override
065  public void setUp() throws Exception {
066    TEST_UTIL.startMiniCluster(1, 3);
067    ASYNC_CONN = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get();
068    admin = ASYNC_CONN.getAdmin();
069  }
070
071  @After
072  @Override
073  public void tearDown() throws Exception {
074    IOUtils.closeQuietly(ASYNC_CONN);
075    TEST_UTIL.shutdownMiniCluster();
076  }
077
078  @Test
079  public void testStop() throws Exception {
080    HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0);
081    assertFalse(rs.isStopped());
082    admin.stopRegionServer(rs.getServerName()).join();
083    assertTrue(rs.isStopped());
084
085    HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
086    assertFalse(master.isStopped());
087    admin.stopMaster().join();
088    assertTrue(master.isStopped());
089  }
090
091  @Test
092  public void testShutdown() throws Exception {
093    TEST_UTIL.getMiniHBaseCluster().getMasterThreads().forEach(thread -> {
094      assertFalse(thread.getMaster().isStopped());
095    });
096    TEST_UTIL.getMiniHBaseCluster().getRegionServerThreads().forEach(thread -> {
097      assertFalse(thread.getRegionServer().isStopped());
098    });
099
100    admin.shutdown().join();
101    TEST_UTIL.getMiniHBaseCluster().getMasterThreads().forEach(thread -> {
102      while (!thread.getMaster().isStopped()) {
103        trySleep(100, TimeUnit.MILLISECONDS);
104      }
105    });
106    TEST_UTIL.getMiniHBaseCluster().getRegionServerThreads().forEach(thread -> {
107      while (!thread.getRegionServer().isStopped()) {
108        trySleep(100, TimeUnit.MILLISECONDS);
109      }
110    });
111  }
112
113  private void trySleep(long timeout, TimeUnit unit) {
114    try {
115      unit.sleep(timeout);
116    } catch (InterruptedException e) {
117    }
118  }
119}