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.jupiter.api.Assertions.assertNotNull; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022 023import java.io.IOException; 024import java.net.Socket; 025import java.util.function.Supplier; 026import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate; 027import org.apache.hadoop.hbase.HConstants; 028import org.apache.hadoop.hbase.ServerName; 029import org.apache.hadoop.hbase.SingleProcessHBaseCluster; 030import org.apache.hadoop.hbase.testclassification.ClientTests; 031import org.apache.hadoop.hbase.testclassification.MediumTests; 032import org.apache.hadoop.hbase.util.FutureUtils; 033import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread; 034import org.junit.jupiter.api.AfterAll; 035import org.junit.jupiter.api.AfterEach; 036import org.junit.jupiter.api.BeforeAll; 037import org.junit.jupiter.api.BeforeEach; 038import org.junit.jupiter.api.Tag; 039import org.junit.jupiter.api.TestTemplate; 040 041/** 042 * Testcase for HBASE-29214 043 */ 044@Tag(ClientTests.TAG) 045@Tag(MediumTests.TAG) 046@HBaseParameterizedTestTemplate(name = "{index}: policy = {0}") 047public class TestAsyncAdminClearMasterStubCache extends TestAsyncAdminBase { 048 049 public TestAsyncAdminClearMasterStubCache(Supplier<AsyncAdmin> admin) { 050 super(admin); 051 } 052 053 @BeforeAll 054 public static void setUpBeforeClass() throws Exception { 055 TestAsyncAdminBase.setUpBeforeClass(); 056 } 057 058 @AfterAll 059 public static void tearDownAfterClass() throws Exception { 060 TestAsyncAdminBase.tearDownAfterClass(); 061 } 062 063 @BeforeEach 064 public void waitMasterReady() throws Exception { 065 assertTrue(TEST_UTIL.getHBaseCluster().waitForActiveAndReadyMaster(30000)); 066 } 067 068 @AfterEach 069 public void clearPortConfig() { 070 TEST_UTIL.getHBaseCluster().getConf().setInt(HConstants.MASTER_PORT, 0); 071 } 072 073 @TestTemplate 074 public void testClearMasterStubCache() throws Exception { 075 // cache master stub 076 assertNotNull(FutureUtils.get(admin.getClusterMetrics())); 077 // stop the active master 078 SingleProcessHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); 079 MasterThread mt = cluster.getMasterThread(); 080 ServerName sn = mt.getMaster().getServerName(); 081 mt.getMaster().abort("for testing"); 082 mt.join(); 083 // wait for new active master 084 assertTrue(TEST_UTIL.getHBaseCluster().waitForActiveAndReadyMaster(30000)); 085 // restart master on the same port, this is important for getting a RemoteException 086 cluster.getConf().setInt(HConstants.MASTER_PORT, sn.getPort()); 087 cluster.startMaster(); 088 // make sure the master is up so we will not get a connect exception 089 TEST_UTIL.waitFor(30000, () -> { 090 try (Socket socket = new Socket(sn.getHostname(), sn.getPort())) { 091 return true; 092 } catch (IOException e) { 093 return false; 094 } 095 }); 096 // we should switch to the new active master 097 assertNotNull(FutureUtils.get(admin.getClusterMetrics())); 098 } 099}