1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import static org.junit.Assert.assertTrue;
22
23 import org.apache.hadoop.hbase.testclassification.MediumTests;
24 import org.apache.hadoop.hbase.testclassification.MiscTests;
25 import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
26 import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.experimental.categories.Category;
31
32
33
34
35 @Category({MiscTests.class, MediumTests.class})
36 public class TestClusterBootOrder {
37
38 private static final long SLEEP_INTERVAL = 1000;
39 private static final long SLEEP_TIME = 4000;
40
41 private HBaseTestingUtility testUtil;
42 private LocalHBaseCluster cluster;
43 private RegionServerThread rs;
44 private MasterThread master;
45
46 @Before
47 public void setUp() throws Exception {
48 testUtil = new HBaseTestingUtility();
49 testUtil.startMiniDFSCluster(1);
50 testUtil.startMiniZKCluster(1);
51 testUtil.createRootDir();
52 cluster = new LocalHBaseCluster(testUtil.getConfiguration(), 0, 0);
53 }
54
55 @After
56 public void tearDown() throws Exception {
57 cluster.shutdown();
58 cluster.join();
59 testUtil.shutdownMiniZKCluster();
60 testUtil.shutdownMiniDFSCluster();
61 }
62
63 private void startRegionServer() throws Exception {
64 rs = cluster.addRegionServer();
65 rs.start();
66
67 for (int i=0; i * SLEEP_INTERVAL < SLEEP_TIME ;i++) {
68
69 Thread.sleep(SLEEP_INTERVAL);
70 assertTrue(rs.isAlive());
71 }
72 }
73
74 private void startMaster() throws Exception {
75 master = cluster.addMaster();
76 master.start();
77
78 for (int i=0; i * SLEEP_INTERVAL < SLEEP_TIME ;i++) {
79 Thread.sleep(SLEEP_INTERVAL);
80 assertTrue(master.isAlive());
81 }
82 }
83
84 private void waitForClusterOnline() {
85 while (true) {
86 if (master.getMaster().isInitialized()) {
87 break;
88 }
89 try {
90 Thread.sleep(100);
91 } catch (InterruptedException ignored) {
92
93 }
94 }
95 rs.waitForServerOnline();
96 }
97
98
99
100
101
102 @Test
103 public void testBootRegionServerFirst() throws Exception {
104 startRegionServer();
105 startMaster();
106 waitForClusterOnline();
107 }
108
109
110
111
112
113 @Test
114 public void testBootMasterFirst() throws Exception {
115 startMaster();
116 startRegionServer();
117 waitForClusterOnline();
118 }
119
120 }