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
93 HBaseAdmin admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
94 byte[][] SPLIT_KEYS = new byte[][] { Bytes.toBytes("a"), Bytes.toBytes("b"),
95 Bytes.toBytes("c"), Bytes.toBytes("d"), Bytes.toBytes("e"), Bytes.toBytes("f"),
96 Bytes.toBytes("g"), Bytes.toBytes("h"), Bytes.toBytes("i"), Bytes.toBytes("j") };
97
98 String tableName = "testRegionAssignmentAfterMasterRecoveryDueToZKExpiry";
99 admin.createTable(new HTableDescriptor(tableName), SPLIT_KEYS);
100 ZooKeeperWatcher zooKeeperWatcher = HBaseTestingUtility.getZooKeeperWatcher(TEST_UTIL);
101 ZKAssign.blockUntilNoRIT(zooKeeperWatcher);
102 m.getZooKeeperWatcher().close();
103 MockLoadBalancer.retainAssignCalled = false;
104 m.abort("Test recovery from zk session expired", new KeeperException.SessionExpiredException());
105 assertFalse(m.isStopped());
106
107
108 assertFalse("Retain assignment should not be called", MockLoadBalancer.retainAssignCalled);
109 }
110
111 static class MockLoadBalancer extends DefaultLoadBalancer {
112 static boolean retainAssignCalled = false;
113
114 @Override
115 public Map<ServerName, List<HRegionInfo>> retainAssignment(
116 Map<HRegionInfo, ServerName> regions, List<ServerName> servers) {
117 retainAssignCalled = true;
118 return super.retainAssignment(regions, servers);
119 }
120 }
121
122
123
124
125
126 @Test(timeout = 60000)
127 public void testLogSplittingAfterMasterRecoveryDueToZKExpiry() throws IOException,
128 KeeperException, InterruptedException {
129 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
130 cluster.startRegionServer();
131 HMaster m = cluster.getMaster();
132
133 HBaseAdmin admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
134 byte[][] SPLIT_KEYS = new byte[][] { Bytes.toBytes("1"), Bytes.toBytes("2"),
135 Bytes.toBytes("3"), Bytes.toBytes("4"), Bytes.toBytes("5") };
136
137 String tableName = "testLogSplittingAfterMasterRecoveryDueToZKExpiry";
138 HTableDescriptor htd = new HTableDescriptor(tableName);
139 HColumnDescriptor hcd = new HColumnDescriptor("col");
140 htd.addFamily(hcd);
141 admin.createTable(htd, SPLIT_KEYS);
142 ZooKeeperWatcher zooKeeperWatcher = HBaseTestingUtility.getZooKeeperWatcher(TEST_UTIL);
143 ZKAssign.blockUntilNoRIT(zooKeeperWatcher);
144 HTable table = new HTable(TEST_UTIL.getConfiguration(), tableName);
145
146 Put p = null;
147 int numberOfPuts = 0;
148 for (numberOfPuts = 0; numberOfPuts < 6; numberOfPuts++) {
149 p = new Put(Bytes.toBytes(numberOfPuts));
150 p.add(Bytes.toBytes("col"), Bytes.toBytes("ql"), Bytes.toBytes("value" + numberOfPuts));
151 table.put(p);
152 }
153 m.getZooKeeperWatcher().close();
154 m.abort("Test recovery from zk session expired", new KeeperException.SessionExpiredException());
155 assertFalse(m.isStopped());
156 cluster.getRegionServer(0).abort("Aborting");
157
158
159 Scan scan = new Scan();
160 int numberOfRows = 0;
161 ResultScanner scanner = table.getScanner(scan);
162 Result[] result = scanner.next(1);
163 while (result != null && result.length > 0) {
164 numberOfRows++;
165 result = scanner.next(1);
166 }
167 assertEquals("Number of rows should be equal to number of puts.", numberOfPuts, numberOfRows);
168 }
169 }
170