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.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.conf.Configuration;
31 import org.apache.hadoop.hbase.ClusterStatus;
32 import org.apache.hadoop.hbase.HBaseConfiguration;
33 import org.apache.hadoop.hbase.HBaseTestingUtility;
34 import org.apache.hadoop.hbase.testclassification.LargeTests;
35 import org.apache.hadoop.hbase.LocalHBaseCluster;
36 import org.apache.hadoop.hbase.MiniHBaseCluster;
37 import org.apache.hadoop.hbase.client.Admin;
38 import org.apache.hadoop.hbase.client.Connection;
39 import org.apache.hadoop.hbase.client.ConnectionFactory;
40 import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
41 import org.junit.Test;
42 import org.junit.experimental.categories.Category;
43
44 @Category(LargeTests.class)
45 public class TestMasterShutdown {
46 public static final Log LOG = LogFactory.getLog(TestMasterShutdown.class);
47
48
49
50
51
52
53
54
55 @Test (timeout=120000)
56 public void testMasterShutdown() throws Exception {
57 final int NUM_MASTERS = 3;
58 final int NUM_RS = 3;
59
60
61 Configuration conf = HBaseConfiguration.create();
62
63
64 HBaseTestingUtility htu = new HBaseTestingUtility(conf);
65 htu.startMiniCluster(NUM_MASTERS, NUM_RS);
66 MiniHBaseCluster cluster = htu.getHBaseCluster();
67
68
69 List<MasterThread> masterThreads = cluster.getMasterThreads();
70
71
72 for (MasterThread mt : masterThreads) {
73 assertTrue(mt.isAlive());
74 }
75
76
77 HMaster active = null;
78 for (int i = 0; i < masterThreads.size(); i++) {
79 if (masterThreads.get(i).getMaster().isActiveMaster()) {
80 active = masterThreads.get(i).getMaster();
81 break;
82 }
83 }
84 assertNotNull(active);
85
86 ClusterStatus status = active.getClusterStatus();
87 assertEquals(2, status.getBackupMastersSize());
88 assertEquals(2, status.getBackupMasters().size());
89
90
91 active.shutdown();
92
93 for (int i = NUM_MASTERS - 1; i >= 0 ;--i) {
94 cluster.waitOnMaster(i);
95 }
96
97 assertEquals(0, masterThreads.size());
98
99 htu.shutdownMiniCluster();
100 }
101
102 @Test(timeout = 60000)
103 public void testMasterShutdownBeforeStartingAnyRegionServer() throws Exception {
104 final int NUM_MASTERS = 1;
105 final int NUM_RS = 0;
106
107
108 Configuration conf = HBaseConfiguration.create();
109 conf.setInt("hbase.ipc.client.failed.servers.expiry", 200);
110 conf.setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1);
111
112
113 final HBaseTestingUtility util = new HBaseTestingUtility(conf);
114 util.startMiniDFSCluster(3);
115 util.startMiniZKCluster();
116 util.createRootDir();
117 final LocalHBaseCluster cluster =
118 new LocalHBaseCluster(conf, NUM_MASTERS, NUM_RS, HMaster.class,
119 MiniHBaseCluster.MiniHBaseClusterRegionServer.class);
120 final int MASTER_INDEX = 0;
121 final MasterThread master = cluster.getMasters().get(MASTER_INDEX);
122 master.start();
123 LOG.info("Called master start on " + master.getName());
124 Thread shutdownThread = new Thread() {
125 public void run() {
126 LOG.info("Before call to shutdown master");
127 try {
128 try (Connection connection =
129 ConnectionFactory.createConnection(util.getConfiguration())) {
130 try (Admin admin = connection.getAdmin()) {
131 admin.shutdown();
132 }
133 }
134 LOG.info("After call to shutdown master");
135 cluster.waitOnMaster(MASTER_INDEX);
136 } catch (Exception e) {
137 }
138 };
139 };
140 shutdownThread.start();
141 LOG.info("Called master join on " + master.getName());
142 master.join();
143 shutdownThread.join();
144
145 List<MasterThread> masterThreads = cluster.getMasters();
146
147 assertEquals(0, masterThreads.size());
148
149 util.shutdownMiniZKCluster();
150 util.shutdownMiniDFSCluster();
151 util.cleanupTestDir();
152 }
153 }