1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.master;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.List;
27
28 import org.apache.hadoop.hbase.*;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
33 import org.junit.Test;
34 import org.junit.experimental.categories.Category;
35
36 @Category(LargeTests.class)
37 public class TestMasterShutdown {
38 private static final Log LOG = LogFactory.getLog(TestMasterShutdown.class);
39
40
41
42
43
44
45
46
47 @Test (timeout=240000)
48 public void testMasterShutdown() throws Exception {
49
50 final int NUM_MASTERS = 3;
51 final int NUM_RS = 3;
52
53
54 Configuration conf = HBaseConfiguration.create();
55
56
57 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
58 TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
59 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
60
61
62 List<MasterThread> masterThreads = cluster.getMasterThreads();
63
64
65 for (MasterThread mt : masterThreads) {
66 assertTrue(mt.isAlive());
67 }
68
69
70 HMaster active = null;
71 for (int i = 0; i < masterThreads.size(); i++) {
72 if (masterThreads.get(i).getMaster().isActiveMaster()) {
73 active = masterThreads.get(i).getMaster();
74 break;
75 }
76 }
77 assertNotNull(active);
78
79 ClusterStatus status = active.getClusterStatus();
80 assertEquals(2, status.getBackupMastersSize());
81 assertEquals(2, status.getBackupMasters().size());
82
83
84 active.shutdown();
85
86 for (int i = NUM_MASTERS - 1; i >= 0 ;--i) {
87 cluster.waitOnMaster(i);
88 }
89
90 assertEquals(0,masterThreads.size());
91
92 TEST_UTIL.shutdownMiniCluster();
93 }
94
95 @Test(timeout = 180000)
96 public void testMasterShutdownBeforeStartingAnyRegionServer() throws Exception {
97
98 final int NUM_MASTERS = 1;
99 final int NUM_RS = 0;
100
101
102 Configuration conf = HBaseConfiguration.create();
103
104
105 final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
106 TEST_UTIL.startMiniDFSCluster(3);
107 TEST_UTIL.startMiniZKCluster();
108 TEST_UTIL.createRootDir();
109 final LocalHBaseCluster cluster =
110 new LocalHBaseCluster(conf, NUM_MASTERS, NUM_RS, HMaster.class,
111 MiniHBaseCluster.MiniHBaseClusterRegionServer.class);
112 final MasterThread master = cluster.getMasters().get(0);
113 master.start();
114 Thread shutdownThread = new Thread() {
115 public void run() {
116 try {
117 TEST_UTIL.getHBaseAdmin().shutdown();
118 cluster.waitOnMaster(0);
119 } catch (Exception e) {
120 }
121 };
122 };
123 shutdownThread.start();
124 master.join();
125 shutdownThread.join();
126
127 List<MasterThread> masterThreads = cluster.getMasters();
128
129 assertEquals(0, masterThreads.size());
130
131 TEST_UTIL.shutdownMiniZKCluster();
132 TEST_UTIL.shutdownMiniDFSCluster();
133 TEST_UTIL.cleanupTestDir();
134 }
135
136 }
137