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