2020import static org .hamcrest .CoreMatchers .hasItems ;
2121import static org .hamcrest .MatcherAssert .assertThat ;
2222import static org .junit .Assert .assertEquals ;
23+ import static org .junit .Assert .fail ;
2324
2425import java .io .IOException ;
26+ import java .util .ArrayList ;
2527import java .util .Arrays ;
2628import java .util .Collections ;
2729import java .util .List ;
2830import org .apache .hadoop .hbase .HBaseClassTestRule ;
2931import org .apache .hadoop .hbase .HBaseTestingUtil ;
32+ import org .apache .hadoop .hbase .HConstants ;
3033import org .apache .hadoop .hbase .HRegionLocation ;
3134import org .apache .hadoop .hbase .ServerName ;
35+ import org .apache .hadoop .hbase .StartTestingClusterOption ;
3236import org .apache .hadoop .hbase .TableName ;
3337import org .apache .hadoop .hbase .master .HMaster ;
38+ import org .apache .hadoop .hbase .regionserver .RSRpcServices ;
3439import org .apache .hadoop .hbase .testclassification .ClientTests ;
3540import org .apache .hadoop .hbase .testclassification .MediumTests ;
3641import org .junit .After ;
4045import org .junit .ClassRule ;
4146import org .junit .Test ;
4247import org .junit .experimental .categories .Category ;
48+ import org .junit .runner .RunWith ;
49+ import org .junit .runners .Parameterized ;
50+ import org .junit .runners .Parameterized .Parameter ;
51+ import org .junit .runners .Parameterized .Parameters ;
4352
4453import org .apache .hbase .thirdparty .com .google .common .io .Closeables ;
4554
55+ @ RunWith (Parameterized .class )
4656@ Category ({ MediumTests .class , ClientTests .class })
4757public class TestRpcConnectionRegistry {
4858
@@ -54,13 +64,38 @@ public class TestRpcConnectionRegistry {
5464
5565 private RpcConnectionRegistry registry ;
5666
67+ @ Parameter
68+ public RSRpcServices .ConnectionRegistryBootstrapNodeType bootstrapNodeType ;
69+
70+ @ Parameters (name = "{index}: bootstrapNodeType={0}" )
71+ public static List <Object []> params () {
72+ List <Object []> params = new ArrayList <>();
73+ for (RSRpcServices .ConnectionRegistryBootstrapNodeType type : RSRpcServices .ConnectionRegistryBootstrapNodeType
74+ .values ()) {
75+ params .add (new Object [] { type });
76+ }
77+ return params ;
78+ }
79+
5780 @ BeforeClass
5881 public static void setUpBeforeClass () throws Exception {
5982 // allow refresh immediately so we will switch to use region servers soon.
6083 UTIL .getConfiguration ().setLong (RpcConnectionRegistry .PERIODIC_REFRESH_INTERVAL_SECS , 1 );
6184 UTIL .getConfiguration ().setLong (RpcConnectionRegistry .MIN_SECS_BETWEEN_REFRESHES , 0 );
62- UTIL .startMiniCluster (3 );
85+ UTIL .startMiniCluster (StartTestingClusterOption .builder ().numMasters (1 )
86+ .numAlwaysStandByMasters (1 ).numRegionServers (3 ).build ());
6387 HBaseTestingUtil .setReplicas (UTIL .getAdmin (), TableName .META_TABLE_NAME , 3 );
88+ try (ConnectionRegistry registry =
89+ ConnectionRegistryFactory .getRegistry (UTIL .getConfiguration ())) {
90+ // Add wait on all replicas being assigned before proceeding w/ test. Failed on occasion
91+ // because not all replicas had made it up before test started.
92+ RegionReplicaTestHelper .waitUntilAllMetaReplicasAreReady (UTIL , registry );
93+ }
94+ // set only the active master as bootstrap node, so it is easy to make sure we have refreshed
95+ // the bootstrap nodes by check the number of bootstrap nodes.
96+ ServerName activeMasterAddr = UTIL .getMiniHBaseCluster ().getMaster ().getServerName ();
97+ UTIL .getConfiguration ().set (HConstants .MASTER_ADDRS_KEY ,
98+ activeMasterAddr .getHostname () + ":" + activeMasterAddr .getPort ());
6499 }
65100
66101 @ AfterClass
@@ -70,6 +105,12 @@ public static void tearDownAfterClass() throws Exception {
70105
71106 @ Before
72107 public void setUp () throws IOException {
108+ UTIL .getMiniHBaseCluster ().getMasterThreads ().stream ()
109+ .map (t -> t .getMaster ().getConfiguration ()).forEach (
110+ conf -> conf .set (RSRpcServices .CLIENT_BOOTSTRAP_NODE_TYPE , bootstrapNodeType .name ()));
111+ UTIL .getMiniHBaseCluster ().getRegionServerThreads ().stream ()
112+ .map (t -> t .getRegionServer ().getConfiguration ()).forEach (
113+ conf -> conf .set (RSRpcServices .CLIENT_BOOTSTRAP_NODE_TYPE , bootstrapNodeType .name ()));
73114 registry = new RpcConnectionRegistry (UTIL .getConfiguration ());
74115 }
75116
@@ -78,17 +119,39 @@ public void tearDown() throws IOException {
78119 Closeables .close (registry , true );
79120 }
80121
122+ private void assertHasMasters () {
123+ assertThat (registry .getParsedServers (), hasItems (UTIL .getMiniHBaseCluster ().getMasterThreads ()
124+ .stream ().map (t -> t .getMaster ().getServerName ()).toArray (ServerName []::new )));
125+ }
126+
127+ private void assertHasRegionServers () {
128+ assertThat (registry .getParsedServers (),
129+ hasItems (UTIL .getMiniHBaseCluster ().getRegionServerThreads ().stream ()
130+ .map (t -> t .getRegionServer ().getServerName ()).toArray (ServerName []::new )));
131+ }
132+
81133 @ Test
82134 public void testRegistryRPCs () throws Exception {
83135 HMaster activeMaster = UTIL .getHBaseCluster ().getMaster ();
84- // wait until we switch to use region servers
85- UTIL .waitFor (10000 , () -> registry .getParsedServers ().size () == 3 );
86- assertThat (registry .getParsedServers (),
87- hasItems (activeMaster .getServerManager ().getOnlineServersList ().toArray (new ServerName [0 ])));
136+ // wait until we switch to use refresh bootstrap nodes
137+ switch (bootstrapNodeType ) {
138+ case MASTER :
139+ UTIL .waitFor (10000 , () -> registry .getParsedServers ().size () == 2 );
140+ assertHasMasters ();
141+ break ;
142+ case REGIONSERVER :
143+ UTIL .waitFor (10000 , () -> registry .getParsedServers ().size () == 3 );
144+ assertHasRegionServers ();
145+ break ;
146+ case ALL :
147+ UTIL .waitFor (10000 , () -> registry .getParsedServers ().size () == 5 );
148+ assertHasMasters ();
149+ assertHasRegionServers ();
150+ break ;
151+ default :
152+ fail ("Unknown bootstrap node type: " + bootstrapNodeType );
153+ }
88154
89- // Add wait on all replicas being assigned before proceeding w/ test. Failed on occasion
90- // because not all replicas had made it up before test started.
91- RegionReplicaTestHelper .waitUntilAllMetaReplicasAreReady (UTIL , registry );
92155
93156 assertEquals (registry .getClusterId ().get (), activeMaster .getClusterId ());
94157 assertEquals (registry .getActiveMaster ().get (), activeMaster .getServerName ());
0 commit comments