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
23 import java.io.IOException;
24 import java.util.List;
25 import java.util.concurrent.atomic.AtomicBoolean;
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 private static AtomicBoolean firstRS = new AtomicBoolean(true);
54
55
56
57
58
59
60 @Test(timeout = 180000)
61 public void testRSTermnationAfterRegisteringToMasterBeforeCreatingEphemeralNod() throws Exception {
62
63 final int NUM_MASTERS = 1;
64 final int NUM_RS = 2;
65 firstRS.set(true);
66
67 Configuration conf = HBaseConfiguration.create();
68
69
70 final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
71 TEST_UTIL.startMiniDFSCluster(3);
72 TEST_UTIL.startMiniZKCluster();
73 TEST_UTIL.createRootDir();
74 final LocalHBaseCluster cluster =
75 new LocalHBaseCluster(conf, NUM_MASTERS, NUM_RS, HMaster.class, MockedRegionServer.class);
76 final MasterThread master = cluster.getMasters().get(0);
77 master.start();
78 try {
79 long startTime = System.currentTimeMillis();
80 while (!master.getMaster().isActiveMaster()) {
81 try {
82 Thread.sleep(100);
83 } catch (InterruptedException ignored) {
84 }
85 if (System.currentTimeMillis() > startTime + 30000) {
86 throw new RuntimeException("Master not active after 30 seconds");
87 }
88 }
89 masterActive = true;
90 cluster.getRegionServers().get(0).start();
91 cluster.getRegionServers().get(1).start();
92 Thread.sleep(10000);
93 List<ServerName> onlineServersList =
94 master.getMaster().getServerManager().getOnlineServersList();
95 while (onlineServersList.size() != 1) {
96 Thread.sleep(100);
97 onlineServersList = master.getMaster().getServerManager().getOnlineServersList();
98 }
99 assertEquals(onlineServersList.size(), 1);
100 cluster.shutdown();
101 } finally {
102 masterActive = false;
103 firstRS.set(true);
104 TEST_UTIL.shutdownMiniCluster();
105 }
106 }
107
108 public static class MockedRegionServer extends MiniHBaseCluster.MiniHBaseClusterRegionServer {
109
110 public MockedRegionServer(Configuration conf) throws IOException, InterruptedException {
111 super(conf);
112 }
113
114 @Override
115 protected void handleReportForDutyResponse(RegionServerStartupResponse c) throws IOException {
116 if (firstRS.getAndSet(false)) {
117 for (NameStringPair e : c.getMapEntriesList()) {
118 String key = e.getName();
119
120 if (key.equals(HConstants.KEY_FOR_HOSTNAME_SEEN_BY_MASTER)) {
121 String hostnameFromMasterPOV = e.getValue();
122 assertEquals(super.getRpcServer().getListenerAddress().getHostName(),
123 hostnameFromMasterPOV);
124 }
125 }
126 while (!masterActive) {
127 Threads.sleep(100);
128 }
129 super.kill();
130 } else {
131 super.handleReportForDutyResponse(c);
132 }
133 }
134 }
135 }