1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.IOException;
25 import java.util.List;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.HBaseConfiguration;
31 import org.apache.hadoop.hbase.HBaseTestingUtility;
32 import org.apache.hadoop.hbase.HConstants;
33 import org.apache.hadoop.hbase.LargeTests;
34 import org.apache.hadoop.hbase.LocalHBaseCluster;
35 import org.apache.hadoop.hbase.MiniHBaseCluster;
36 import org.apache.hadoop.hbase.ServerName;
37 import org.apache.hadoop.hbase.master.HMaster;
38 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameStringPair;
39 import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.RegionServerStartupResponse;
40 import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
41 import org.apache.hadoop.hbase.util.Threads;
42 import org.junit.Test;
43 import org.junit.experimental.categories.Category;
44
45
46
47
48 @Category(LargeTests.class)
49 public class TestRSKilledWhenInitializing {
50 private static final Log LOG = LogFactory.getLog(TestRSKilledWhenInitializing.class);
51
52 private static boolean masterActive = false;
53
54
55
56
57
58
59 @Test(timeout = 180000)
60 public void testRSTermnationAfterRegisteringToMasterBeforeCreatingEphemeralNod() throws Exception {
61
62 final int NUM_MASTERS = 1;
63 final int NUM_RS = 1;
64
65 Configuration conf = HBaseConfiguration.create();
66
67
68 final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
69 TEST_UTIL.startMiniDFSCluster(3);
70 TEST_UTIL.startMiniZKCluster();
71 TEST_UTIL.createRootDir();
72 final LocalHBaseCluster cluster =
73 new LocalHBaseCluster(conf, NUM_MASTERS, NUM_RS, HMaster.class, MockedRegionServer.class);
74 final MasterThread master = cluster.getMasters().get(0);
75 master.start();
76 try {
77 long startTime = System.currentTimeMillis();
78 while (!master.getMaster().isActiveMaster()) {
79 try {
80 Thread.sleep(100);
81 } catch (InterruptedException ignored) {
82 }
83 if (System.currentTimeMillis() > startTime + 30000) {
84 throw new RuntimeException("Master not active after 30 seconds");
85 }
86 }
87 masterActive = true;
88 cluster.getRegionServers().get(0).start();
89 Thread.sleep(10000);
90 List<ServerName> onlineServersList =
91 master.getMaster().getServerManager().getOnlineServersList();
92 while (!onlineServersList.isEmpty()) {
93 Thread.sleep(100);
94 onlineServersList = master.getMaster().getServerManager().getOnlineServersList();
95 }
96 assertTrue(onlineServersList.isEmpty());
97 master.getMaster().stop("stopping master");
98 master.join();
99 } finally {
100 masterActive = false;
101 TEST_UTIL.shutdownMiniZKCluster();
102 TEST_UTIL.shutdownMiniDFSCluster();
103 TEST_UTIL.cleanupTestDir();
104 }
105 }
106
107 public static class MockedRegionServer extends MiniHBaseCluster.MiniHBaseClusterRegionServer {
108
109 public MockedRegionServer(Configuration conf) throws IOException, InterruptedException {
110 super(conf);
111 }
112
113 @Override
114 protected void handleReportForDutyResponse(RegionServerStartupResponse c) throws IOException {
115 for (NameStringPair e : c.getMapEntriesList()) {
116 String key = e.getName();
117
118 if (key.equals(HConstants.KEY_FOR_HOSTNAME_SEEN_BY_MASTER)) {
119 String hostnameFromMasterPOV = e.getValue();
120 assertEquals(super.getRpcServer().getListenerAddress().getHostName(),
121 hostnameFromMasterPOV);
122 }
123 }
124 while (!masterActive) {
125 Threads.sleep(100);
126 }
127 super.kill();
128 }
129 }
130 }