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