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.assertNotNull;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.net.Socket;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.SingleProcessHBaseCluster;
029import org.apache.hadoop.hbase.testclassification.ClientTests;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.apache.hadoop.hbase.util.FutureUtils;
032import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
033import org.junit.After;
034import org.junit.Before;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.junit.runner.RunWith;
039import org.junit.runners.Parameterized;
040
041/**
042 * Testcase for HBASE-29214
043 */
044@RunWith(Parameterized.class)
045@Category({ ClientTests.class, MediumTests.class })
046public class TestAsyncAdminClearMasterStubCache extends TestAsyncAdminBase {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050    HBaseClassTestRule.forClass(TestAsyncAdminClearMasterStubCache.class);
051
052  @Before
053  public void waitMasterReady() throws Exception {
054    assertTrue(TEST_UTIL.getHBaseCluster().waitForActiveAndReadyMaster(30000));
055  }
056
057  @After
058  public void clearPortConfig() {
059    TEST_UTIL.getHBaseCluster().getConf().setInt(HConstants.MASTER_PORT, 0);
060  }
061
062  @Test
063  public void testClearMasterStubCache() throws Exception {
064    // cache master stub
065    assertNotNull(FutureUtils.get(admin.getClusterMetrics()));
066    // stop the active master
067    SingleProcessHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
068    MasterThread mt = cluster.getMasterThread();
069    ServerName sn = mt.getMaster().getServerName();
070    mt.getMaster().abort("for testing");
071    mt.join();
072    // wait for new active master
073    assertTrue(TEST_UTIL.getHBaseCluster().waitForActiveAndReadyMaster(30000));
074    // restart master on the same port, this is important for getting a RemoteException
075    cluster.getConf().setInt(HConstants.MASTER_PORT, sn.getPort());
076    cluster.startMaster();
077    // make sure the master is up so we will not get a connect exception
078    TEST_UTIL.waitFor(30000, () -> {
079      try (Socket socket = new Socket(sn.getHostname(), sn.getPort())) {
080        return true;
081      } catch (IOException e) {
082        return false;
083      }
084    });
085    // we should switch to the new active master
086    assertNotNull(FutureUtils.get(admin.getClusterMetrics()));
087  }
088}