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