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.assertFalse;
24
25 import java.io.IOException;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.HBaseTestingUtility;
31 import org.apache.hadoop.hbase.HColumnDescriptor;
32 import org.apache.hadoop.hbase.HConstants;
33 import org.apache.hadoop.hbase.HRegionInfo;
34 import org.apache.hadoop.hbase.HTableDescriptor;
35 import org.apache.hadoop.hbase.MediumTests;
36 import org.apache.hadoop.hbase.MiniHBaseCluster;
37 import org.apache.hadoop.hbase.ServerName;
38 import org.apache.hadoop.hbase.client.HBaseAdmin;
39 import org.apache.hadoop.hbase.client.HTable;
40 import org.apache.hadoop.hbase.client.Put;
41 import org.apache.hadoop.hbase.client.Result;
42 import org.apache.hadoop.hbase.client.ResultScanner;
43 import org.apache.hadoop.hbase.client.Scan;
44 import org.apache.hadoop.hbase.util.Bytes;
45 import org.apache.hadoop.hbase.zookeeper.ZKAssign;
46 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
47 import org.apache.zookeeper.KeeperException;
48 import org.junit.After;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.experimental.categories.Category;
52
53
54
55
56 @Category(MediumTests.class)
57 public class TestMasterZKSessionRecovery {
58 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
59
60
61
62
63
64 static {
65 Configuration conf = TEST_UTIL.getConfiguration();
66 conf.setLong("hbase.master.zksession.recover.timeout", 50000);
67 conf.setClass(HConstants.HBASE_MASTER_LOADBALANCER_CLASS,
68 MockLoadBalancer.class, LoadBalancer.class);
69 }
70
71 @Before
72 public void setUp() throws Exception {
73
74 TEST_UTIL.startMiniCluster(1);
75 }
76
77 @After
78 public void tearDown() throws Exception {
79 TEST_UTIL.shutdownMiniCluster();
80 }
81
82
83
84
85
86
87 @Test
88 public void testRegionAssignmentAfterMasterRecoveryDueToZKExpiry() throws Exception {
89 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
90 cluster.startRegionServer();
91 HMaster m = cluster.getMaster();
92 ZooKeeperWatcher zkw = m.getZooKeeperWatcher();
93 int expectedNumOfListeners = zkw.getNumberOfListeners();
94
95 HBaseAdmin admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
96 byte[][] SPLIT_KEYS = new byte[][] { Bytes.toBytes("a"), Bytes.toBytes("b"),
97 Bytes.toBytes("c"), Bytes.toBytes("d"), Bytes.toBytes("e"), Bytes.toBytes("f"),
98 Bytes.toBytes("g"), Bytes.toBytes("h"), Bytes.toBytes("i"), Bytes.toBytes("j") };
99
100 String tableName = "testRegionAssignmentAfterMasterRecoveryDueToZKExpiry";
101 admin.createTable(new HTableDescriptor(tableName), SPLIT_KEYS);
102 ZooKeeperWatcher zooKeeperWatcher = HBaseTestingUtility.getZooKeeperWatcher(TEST_UTIL);
103 ZKAssign.blockUntilNoRIT(zooKeeperWatcher);
104 m.getZooKeeperWatcher().close();
105 MockLoadBalancer.retainAssignCalled = false;
106 m.abort("Test recovery from zk session expired", new KeeperException.SessionExpiredException());
107 assertFalse(m.isStopped());
108
109
110 assertFalse("Retain assignment should not be called", MockLoadBalancer.retainAssignCalled);
111
112 assertEquals(expectedNumOfListeners, zkw.getNumberOfListeners());
113 }
114
115 static class MockLoadBalancer extends DefaultLoadBalancer {
116 static boolean retainAssignCalled = false;
117
118 @Override
119 public Map<ServerName, List<HRegionInfo>> retainAssignment(
120 Map<HRegionInfo, ServerName> regions, List<ServerName> servers) {
121 retainAssignCalled = true;
122 return super.retainAssignment(regions, servers);
123 }
124 }
125
126
127
128
129
130 @Test(timeout = 60000)
131 public void testLogSplittingAfterMasterRecoveryDueToZKExpiry() throws IOException,
132 KeeperException, InterruptedException {
133 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
134 cluster.startRegionServer();
135 HMaster m = cluster.getMaster();
136
137 HBaseAdmin admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
138 byte[][] SPLIT_KEYS = new byte[][] { Bytes.toBytes("1"), Bytes.toBytes("2"),
139 Bytes.toBytes("3"), Bytes.toBytes("4"), Bytes.toBytes("5") };
140
141 String tableName = "testLogSplittingAfterMasterRecoveryDueToZKExpiry";
142 HTableDescriptor htd = new HTableDescriptor(tableName);
143 HColumnDescriptor hcd = new HColumnDescriptor("col");
144 htd.addFamily(hcd);
145 admin.createTable(htd, SPLIT_KEYS);
146 ZooKeeperWatcher zooKeeperWatcher = HBaseTestingUtility.getZooKeeperWatcher(TEST_UTIL);
147 ZKAssign.blockUntilNoRIT(zooKeeperWatcher);
148 HTable table = new HTable(TEST_UTIL.getConfiguration(), tableName);
149
150 Put p = null;
151 int numberOfPuts = 0;
152 for (numberOfPuts = 0; numberOfPuts < 6; numberOfPuts++) {
153 p = new Put(Bytes.toBytes(numberOfPuts));
154 p.add(Bytes.toBytes("col"), Bytes.toBytes("ql"), Bytes.toBytes("value" + numberOfPuts));
155 table.put(p);
156 }
157 m.getZooKeeperWatcher().close();
158 m.abort("Test recovery from zk session expired", new KeeperException.SessionExpiredException());
159 assertFalse(m.isStopped());
160 cluster.getRegionServer(0).abort("Aborting");
161
162
163 Scan scan = new Scan();
164 int numberOfRows = 0;
165 ResultScanner scanner = table.getScanner(scan);
166 Result[] result = scanner.next(1);
167 while (result != null && result.length > 0) {
168 numberOfRows++;
169 result = scanner.next(1);
170 }
171 assertEquals("Number of rows should be equal to number of puts.", numberOfPuts, numberOfRows);
172 }
173 }
174