1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.master;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23
24 import java.util.List;
25 import java.util.NavigableSet;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.HBaseConfiguration;
31 import org.apache.hadoop.hbase.HBaseTestingUtility;
32 import org.apache.hadoop.hbase.MiniHBaseCluster;
33 import org.apache.hadoop.hbase.TableName;
34 import org.apache.hadoop.hbase.client.Admin;
35 import org.apache.hadoop.hbase.client.HBaseAdmin;
36 import org.apache.hadoop.hbase.client.HTable;
37 import org.apache.hadoop.hbase.client.RegionLocator;
38 import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
39 import org.apache.hadoop.hbase.testclassification.LargeTests;
40 import org.apache.hadoop.hbase.util.Bytes;
41 import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
42 import org.apache.hadoop.hbase.zookeeper.ZKAssign;
43 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
44 import org.apache.zookeeper.KeeperException;
45 import org.junit.Test;
46 import org.junit.experimental.categories.Category;
47
48 @Category(LargeTests.class)
49 public class TestMasterRestartAfterDisablingTable {
50
51 private static final Log LOG = LogFactory.getLog(TestMasterRestartAfterDisablingTable.class);
52
53 @Test
54 public void testForCheckingIfEnableAndDisableWorksFineAfterSwitch()
55 throws Exception {
56 final int NUM_MASTERS = 2;
57 final int NUM_RS = 1;
58 final int NUM_REGIONS_TO_CREATE = 4;
59
60
61 log("Starting cluster");
62 Configuration conf = HBaseConfiguration.create();
63 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
64 TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
65 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
66 log("Waiting for active/ready master");
67 cluster.waitForActiveAndReadyMaster();
68 ZooKeeperWatcher zkw = new ZooKeeperWatcher(conf, "testmasterRestart", null);
69 HMaster master = cluster.getMaster();
70
71
72 TableName table = TableName.valueOf("tableRestart");
73 byte[] family = Bytes.toBytes("family");
74 log("Creating table with " + NUM_REGIONS_TO_CREATE + " regions");
75 HTable ht = TEST_UTIL.createMultiRegionTable(table, family, NUM_REGIONS_TO_CREATE);
76 int numRegions = -1;
77 try (RegionLocator r = ht.getRegionLocator()) {
78 numRegions = r.getStartKeys().length;
79 }
80 numRegions += 1;
81 log("Waiting for no more RIT\n");
82 blockUntilNoRIT(zkw, master);
83 log("Disabling table\n");
84 TEST_UTIL.getHBaseAdmin().disableTable(table);
85
86 NavigableSet<String> regions = HBaseTestingUtility.getAllOnlineRegions(cluster);
87 for (String region : regions) {
88 assertTrue(
89 "The number of regions for the table tableRestart should be 0 and only"
90 + "the catalog and namespace tables should be present.",
91 !region.startsWith(table.getNameAsString()));
92 }
93
94 List<MasterThread> masterThreads = cluster.getMasterThreads();
95 MasterThread activeMaster = null;
96 if (masterThreads.get(0).getMaster().isActiveMaster()) {
97 activeMaster = masterThreads.get(0);
98 } else {
99 activeMaster = masterThreads.get(1);
100 }
101 activeMaster.getMaster().stop(
102 "stopping the active master so that the backup can become active");
103 cluster.hbaseCluster.waitOnMaster(activeMaster);
104 cluster.waitForActiveAndReadyMaster();
105
106 assertTrue("The table should not be in enabled state", cluster.getMaster()
107 .getAssignmentManager().getTableStateManager().isTableState(
108 TableName.valueOf("tableRestart"), ZooKeeperProtos.Table.State.DISABLED,
109 ZooKeeperProtos.Table.State.DISABLING));
110 log("Enabling table\n");
111
112 Admin admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
113 admin.enableTable(table);
114 admin.close();
115 log("Waiting for no more RIT\n");
116 blockUntilNoRIT(zkw, master);
117 log("Verifying there are " + numRegions + " assigned on cluster\n");
118 regions = HBaseTestingUtility.getAllOnlineRegions(cluster);
119 assertEquals("The assigned regions were not onlined after master"
120 + " switch except for the catalog and namespace tables.",
121 6, regions.size());
122 assertTrue("The table should be in enabled state", cluster.getMaster()
123 .getAssignmentManager().getTableStateManager()
124 .isTableState(TableName.valueOf("tableRestart"), ZooKeeperProtos.Table.State.ENABLED));
125 ht.close();
126 TEST_UTIL.shutdownMiniCluster();
127 }
128
129 private void log(String msg) {
130 LOG.debug("\n\nTRR: " + msg + "\n");
131 }
132
133 private void blockUntilNoRIT(ZooKeeperWatcher zkw, HMaster master)
134 throws KeeperException, InterruptedException {
135 ZKAssign.blockUntilNoRIT(zkw);
136 master.assignmentManager.waitUntilNoRegionsInTransition(60000);
137 }
138 }
139