1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.replication.regionserver;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23
24 import com.google.common.collect.Sets;
25
26 import java.io.IOException;
27 import java.net.URLEncoder;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.SortedMap;
33 import java.util.SortedSet;
34 import java.util.TreeSet;
35 import java.util.UUID;
36 import java.util.concurrent.CountDownLatch;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.hadoop.conf.Configuration;
41 import org.apache.hadoop.fs.FileSystem;
42 import org.apache.hadoop.fs.Path;
43 import org.apache.hadoop.hbase.ChoreService;
44 import org.apache.hadoop.hbase.ClusterId;
45 import org.apache.hadoop.hbase.CoordinatedStateManager;
46 import org.apache.hadoop.hbase.HBaseConfiguration;
47 import org.apache.hadoop.hbase.HBaseTestingUtility;
48 import org.apache.hadoop.hbase.HColumnDescriptor;
49 import org.apache.hadoop.hbase.HConstants;
50 import org.apache.hadoop.hbase.HRegionInfo;
51 import org.apache.hadoop.hbase.HTableDescriptor;
52 import org.apache.hadoop.hbase.KeyValue;
53 import org.apache.hadoop.hbase.Server;
54 import org.apache.hadoop.hbase.ServerName;
55 import org.apache.hadoop.hbase.TableName;
56 import org.apache.hadoop.hbase.client.ClusterConnection;
57 import org.apache.hadoop.hbase.regionserver.MultiVersionConcurrencyControl;
58 import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener;
59 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
60 import org.apache.hadoop.hbase.replication.ReplicationFactory;
61 import org.apache.hadoop.hbase.replication.ReplicationPeers;
62 import org.apache.hadoop.hbase.replication.ReplicationQueueInfo;
63 import org.apache.hadoop.hbase.replication.ReplicationQueues;
64 import org.apache.hadoop.hbase.replication.ReplicationQueuesClient;
65 import org.apache.hadoop.hbase.replication.ReplicationSourceDummy;
66 import org.apache.hadoop.hbase.replication.ReplicationStateZKBase;
67 import org.apache.hadoop.hbase.replication.regionserver.ReplicationSourceManager.NodeFailoverWorker;
68 import org.apache.hadoop.hbase.testclassification.MediumTests;
69 import org.apache.hadoop.hbase.util.Bytes;
70 import org.apache.hadoop.hbase.util.FSUtils;
71 import org.apache.hadoop.hbase.wal.WAL;
72 import org.apache.hadoop.hbase.wal.WALFactory;
73 import org.apache.hadoop.hbase.wal.WALKey;
74 import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
75 import org.apache.hadoop.hbase.zookeeper.ZKClusterId;
76 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
77 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
78 import org.junit.After;
79 import org.junit.AfterClass;
80 import org.junit.Before;
81 import org.junit.BeforeClass;
82 import org.junit.Rule;
83 import org.junit.Test;
84 import org.junit.experimental.categories.Category;
85 import org.junit.rules.TestName;
86
87 @Category(MediumTests.class)
88 public class TestReplicationSourceManager {
89
90 private static final Log LOG =
91 LogFactory.getLog(TestReplicationSourceManager.class);
92
93 private static Configuration conf;
94
95 private static HBaseTestingUtility utility;
96
97 private static Replication replication;
98
99 private static ReplicationSourceManager manager;
100
101 private static ZooKeeperWatcher zkw;
102
103 private static HTableDescriptor htd;
104
105 private static HRegionInfo hri;
106
107 private static final byte[] r1 = Bytes.toBytes("r1");
108
109 private static final byte[] r2 = Bytes.toBytes("r2");
110
111 private static final byte[] f1 = Bytes.toBytes("f1");
112
113 private static final TableName test =
114 TableName.valueOf("test");
115
116 private static final String slaveId = "1";
117
118 private static FileSystem fs;
119
120 private static Path oldLogDir;
121
122 private static Path logDir;
123
124 private static CountDownLatch latch;
125
126 private static List<String> files = new ArrayList<String>();
127
128 @BeforeClass
129 public static void setUpBeforeClass() throws Exception {
130
131 conf = HBaseConfiguration.create();
132 conf.set("replication.replicationsource.implementation",
133 ReplicationSourceDummy.class.getCanonicalName());
134 conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY,
135 HConstants.REPLICATION_ENABLE_DEFAULT);
136 conf.setLong("replication.sleep.before.failover", 2000);
137 conf.setInt("replication.source.maxretriesmultiplier", 10);
138 utility = new HBaseTestingUtility(conf);
139 utility.startMiniZKCluster();
140
141 zkw = new ZooKeeperWatcher(conf, "test", null);
142 ZKUtil.createWithParents(zkw, "/hbase/replication");
143 ZKUtil.createWithParents(zkw, "/hbase/replication/peers/1");
144 ZKUtil.setData(zkw, "/hbase/replication/peers/1",
145 Bytes.toBytes(conf.get(HConstants.ZOOKEEPER_QUORUM) + ":"
146 + conf.get(HConstants.ZOOKEEPER_CLIENT_PORT) + ":/1"));
147 ZKUtil.createWithParents(zkw, "/hbase/replication/peers/1/peer-state");
148 ZKUtil.setData(zkw, "/hbase/replication/peers/1/peer-state",
149 ReplicationStateZKBase.ENABLED_ZNODE_BYTES);
150 ZKUtil.createWithParents(zkw, "/hbase/replication/state");
151 ZKUtil.setData(zkw, "/hbase/replication/state", ReplicationStateZKBase.ENABLED_ZNODE_BYTES);
152
153 ZKClusterId.setClusterId(zkw, new ClusterId());
154 FSUtils.setRootDir(utility.getConfiguration(), utility.getDataTestDir());
155 fs = FileSystem.get(conf);
156 oldLogDir = new Path(utility.getDataTestDir(),
157 HConstants.HREGION_OLDLOGDIR_NAME);
158 logDir = new Path(utility.getDataTestDir(),
159 HConstants.HREGION_LOGDIR_NAME);
160 replication = new Replication(new DummyServer(), fs, logDir, oldLogDir);
161 manager = replication.getReplicationManager();
162
163 manager.addSource(slaveId);
164
165 htd = new HTableDescriptor(test);
166 HColumnDescriptor col = new HColumnDescriptor("f1");
167 col.setScope(HConstants.REPLICATION_SCOPE_GLOBAL);
168 htd.addFamily(col);
169 col = new HColumnDescriptor("f2");
170 col.setScope(HConstants.REPLICATION_SCOPE_LOCAL);
171 htd.addFamily(col);
172
173 hri = new HRegionInfo(htd.getTableName(), r1, r2);
174 }
175
176 @AfterClass
177 public static void tearDownAfterClass() throws Exception {
178 manager.join();
179 utility.shutdownMiniCluster();
180 }
181
182 @Rule
183 public TestName testName = new TestName();
184
185 private void cleanLogDir() throws IOException {
186 fs.delete(logDir, true);
187 fs.delete(oldLogDir, true);
188 }
189
190 @Before
191 public void setUp() throws Exception {
192 LOG.info("Start " + testName.getMethodName());
193 cleanLogDir();
194 }
195
196 @After
197 public void tearDown() throws Exception {
198 LOG.info("End " + testName.getMethodName());
199 cleanLogDir();
200 }
201
202 @Test
203 public void testLogRoll() throws Exception {
204 long baseline = 1000;
205 long time = baseline;
206 MultiVersionConcurrencyControl mvcc = new MultiVersionConcurrencyControl();
207 KeyValue kv = new KeyValue(r1, f1, r1);
208 WALEdit edit = new WALEdit();
209 edit.add(kv);
210
211 List<WALActionsListener> listeners = new ArrayList<WALActionsListener>();
212 listeners.add(replication);
213 final WALFactory wals = new WALFactory(utility.getConfiguration(), listeners,
214 URLEncoder.encode("regionserver:60020", "UTF8"));
215 final WAL wal = wals.getWAL(hri.getEncodedNameAsBytes());
216 manager.init();
217 HTableDescriptor htd = new HTableDescriptor();
218 htd.addFamily(new HColumnDescriptor(f1));
219
220 for(long i = 1; i < 101; i++) {
221 if(i > 1 && i % 20 == 0) {
222 wal.rollWriter();
223 }
224 LOG.info(i);
225 final long txid = wal.append(htd,
226 hri,
227 new WALKey(hri.getEncodedNameAsBytes(), test, System.currentTimeMillis(), mvcc),
228 edit,
229 true);
230 wal.sync(txid);
231 }
232
233
234
235 LOG.info(baseline + " and " + time);
236 baseline += 101;
237 time = baseline;
238 LOG.info(baseline + " and " + time);
239
240 for (int i = 0; i < 3; i++) {
241 wal.append(htd, hri,
242 new WALKey(hri.getEncodedNameAsBytes(), test, System.currentTimeMillis(), mvcc),
243 edit,
244 true);
245 }
246 wal.sync();
247
248 int logNumber = 0;
249 for (Map.Entry<String, SortedSet<String>> entry : manager.getWALs().get(slaveId).entrySet()) {
250 logNumber += entry.getValue().size();
251 }
252 assertEquals(6, logNumber);
253
254 wal.rollWriter();
255
256 manager.logPositionAndCleanOldLogs(manager.getSources().get(0).getCurrentPath(),
257 "1", 0, false, false);
258
259 wal.append(htd, hri,
260 new WALKey(hri.getEncodedNameAsBytes(), test, System.currentTimeMillis(), mvcc),
261 edit,
262 true);
263 wal.sync();
264
265 assertEquals(1, manager.getWALs().size());
266
267
268
269 }
270
271 @Test
272 public void testClaimQueues() throws Exception {
273 conf.setBoolean(HConstants.ZOOKEEPER_USEMULTI, true);
274 final Server server = new DummyServer("hostname0.example.org");
275 ReplicationQueues rq =
276 ReplicationFactory.getReplicationQueues(server.getZooKeeper(), server.getConfiguration(),
277 server);
278 rq.init(server.getServerName().toString());
279
280 files.add("log1");
281 files.add("log2");
282 for (String file : files) {
283 rq.addLog("1", file);
284 }
285
286 Server s1 = new DummyServer("dummyserver1.example.org");
287 Server s2 = new DummyServer("dummyserver2.example.org");
288 Server s3 = new DummyServer("dummyserver3.example.org");
289
290
291 DummyNodeFailoverWorker w1 = new DummyNodeFailoverWorker(
292 server.getServerName().getServerName(), s1);
293 DummyNodeFailoverWorker w2 = new DummyNodeFailoverWorker(
294 server.getServerName().getServerName(), s2);
295 DummyNodeFailoverWorker w3 = new DummyNodeFailoverWorker(
296 server.getServerName().getServerName(), s3);
297
298 latch = new CountDownLatch(3);
299
300 w1.start();
301 w2.start();
302 w3.start();
303
304 int populatedMap = 0;
305
306 latch.await();
307 populatedMap += w1.isLogZnodesMapPopulated() + w2.isLogZnodesMapPopulated()
308 + w3.isLogZnodesMapPopulated();
309 assertEquals(1, populatedMap);
310 server.abort("", null);
311 }
312
313 @Test
314 public void testCleanupFailoverQueues() throws Exception {
315 final Server server = new DummyServer("hostname1.example.org");
316 ReplicationQueues rq =
317 ReplicationFactory.getReplicationQueues(server.getZooKeeper(), server.getConfiguration(),
318 server);
319 rq.init(server.getServerName().toString());
320
321 SortedSet<String> files = new TreeSet<String>();
322 String group = "testgroup";
323 String file1 = group + ".log1";
324 String file2 = group + ".log2";
325 files.add(file1);
326 files.add(file2);
327 for (String file : files) {
328 rq.addLog("1", file);
329 }
330 Server s1 = new DummyServer("dummyserver1.example.org");
331 ReplicationQueues rq1 =
332 ReplicationFactory.getReplicationQueues(s1.getZooKeeper(), s1.getConfiguration(), s1);
333 rq1.init(s1.getServerName().toString());
334 ReplicationPeers rp1 =
335 ReplicationFactory.getReplicationPeers(s1.getZooKeeper(), s1.getConfiguration(), s1);
336 rp1.init();
337 NodeFailoverWorker w1 =
338 manager.new NodeFailoverWorker(server.getServerName().getServerName(), rq1, rp1, new UUID(
339 new Long(1), new Long(2)));
340 w1.start();
341 w1.join(5000);
342 assertEquals(1, manager.getWalsByIdRecoveredQueues().size());
343 String id = "1-" + server.getServerName().getServerName();
344 assertEquals(files, manager.getWalsByIdRecoveredQueues().get(id).get(group));
345 manager.cleanOldLogs(file2, id, true);
346
347 assertEquals(Sets.newHashSet(file2), manager.getWalsByIdRecoveredQueues().get(id).get(group));
348 }
349
350 @Test
351 public void testNodeFailoverDeadServerParsing() throws Exception {
352 LOG.debug("testNodeFailoverDeadServerParsing");
353 conf.setBoolean(HConstants.ZOOKEEPER_USEMULTI, true);
354 final Server server = new DummyServer("ec2-54-234-230-108.compute-1.amazonaws.com");
355 ReplicationQueues repQueues =
356 ReplicationFactory.getReplicationQueues(server.getZooKeeper(), conf, server);
357 repQueues.init(server.getServerName().toString());
358
359 files.add("log1");
360 files.add("log2");
361 for (String file : files) {
362 repQueues.addLog("1", file);
363 }
364
365
366 Server s1 = new DummyServer("ip-10-8-101-114.ec2.internal");
367 Server s2 = new DummyServer("ec2-107-20-52-47.compute-1.amazonaws.com");
368 Server s3 = new DummyServer("ec2-23-20-187-167.compute-1.amazonaws.com");
369
370
371 ReplicationQueues rq1 =
372 ReplicationFactory.getReplicationQueues(s1.getZooKeeper(), s1.getConfiguration(), s1);
373 rq1.init(s1.getServerName().toString());
374 SortedMap<String, SortedSet<String>> testMap =
375 rq1.claimQueues(server.getServerName().getServerName());
376 ReplicationQueues rq2 =
377 ReplicationFactory.getReplicationQueues(s2.getZooKeeper(), s2.getConfiguration(), s2);
378 rq2.init(s2.getServerName().toString());
379 testMap = rq2.claimQueues(s1.getServerName().getServerName());
380 ReplicationQueues rq3 =
381 ReplicationFactory.getReplicationQueues(s3.getZooKeeper(), s3.getConfiguration(), s3);
382 rq3.init(s3.getServerName().toString());
383 testMap = rq3.claimQueues(s2.getServerName().getServerName());
384
385 ReplicationQueueInfo replicationQueueInfo = new ReplicationQueueInfo(testMap.firstKey());
386 List<String> result = replicationQueueInfo.getDeadRegionServers();
387
388
389 assertTrue(result.contains(server.getServerName().getServerName()));
390 assertTrue(result.contains(s1.getServerName().getServerName()));
391 assertTrue(result.contains(s2.getServerName().getServerName()));
392
393 server.abort("", null);
394 }
395
396 @Test
397 public void testFailoverDeadServerCversionChange() throws Exception {
398 LOG.debug("testFailoverDeadServerCversionChange");
399
400 conf.setBoolean(HConstants.ZOOKEEPER_USEMULTI, true);
401 final Server s0 = new DummyServer("cversion-change0.example.org");
402 ReplicationQueues repQueues =
403 ReplicationFactory.getReplicationQueues(s0.getZooKeeper(), conf, s0);
404 repQueues.init(s0.getServerName().toString());
405
406 files.add("log1");
407 files.add("log2");
408 for (String file : files) {
409 repQueues.addLog("1", file);
410 }
411
412 Server s1 = new DummyServer("cversion-change1.example.org");
413 ReplicationQueues rq1 =
414 ReplicationFactory.getReplicationQueues(s1.getZooKeeper(), s1.getConfiguration(), s1);
415 rq1.init(s1.getServerName().toString());
416
417 ReplicationQueuesClient client =
418 ReplicationFactory.getReplicationQueuesClient(s1.getZooKeeper(), s1.getConfiguration(), s1);
419
420 int v0 = client.getQueuesZNodeCversion();
421 rq1.claimQueues(s0.getServerName().getServerName());
422 int v1 = client.getQueuesZNodeCversion();
423
424 assertEquals(v0 + 1, v1);
425
426 s0.abort("", null);
427 }
428
429 @Test
430 public void testCleanupUnknownPeerZNode() throws Exception {
431 final Server server = new DummyServer("hostname2.example.org");
432 ReplicationQueues rq =
433 ReplicationFactory.getReplicationQueues(server.getZooKeeper(), server.getConfiguration(),
434 server);
435 rq.init(server.getServerName().toString());
436
437
438 String group = "testgroup";
439 rq.addLog("2", group + ".log1");
440 rq.addLog("2", group + ".log2");
441
442 NodeFailoverWorker w1 = manager.new NodeFailoverWorker(server.getServerName().getServerName());
443 w1.run();
444
445
446 for (String peer : manager.getAllQueues()) {
447 assertTrue(peer.startsWith("1"));
448 }
449 }
450
451 static class DummyNodeFailoverWorker extends Thread {
452 private SortedMap<String, SortedSet<String>> logZnodesMap;
453 Server server;
454 private String deadRsZnode;
455 ReplicationQueues rq;
456
457 public DummyNodeFailoverWorker(String znode, Server s) throws Exception {
458 this.deadRsZnode = znode;
459 this.server = s;
460 this.rq =
461 ReplicationFactory.getReplicationQueues(server.getZooKeeper(), server.getConfiguration(),
462 server);
463 this.rq.init(this.server.getServerName().toString());
464 }
465
466 @Override
467 public void run() {
468 try {
469 logZnodesMap = rq.claimQueues(deadRsZnode);
470 server.abort("Done with testing", null);
471 } catch (Exception e) {
472 LOG.error("Got exception while running NodeFailoverWorker", e);
473 } finally {
474 latch.countDown();
475 }
476 }
477
478
479
480
481 private int isLogZnodesMapPopulated() {
482 Collection<SortedSet<String>> sets = logZnodesMap.values();
483 if (sets.size() > 1) {
484 throw new RuntimeException("unexpected size of logZnodesMap: " + sets.size());
485 }
486 if (sets.size() == 1) {
487 SortedSet<String> s = sets.iterator().next();
488 for (String file : files) {
489
490 if (!s.contains(file)) {
491 return 0;
492 }
493 }
494 return 1;
495 }
496 return 0;
497 }
498 }
499
500 static class DummyServer implements Server {
501 String hostname;
502
503 DummyServer() {
504 hostname = "hostname.example.org";
505 }
506
507 DummyServer(String hostname) {
508 this.hostname = hostname;
509 }
510
511 @Override
512 public Configuration getConfiguration() {
513 return conf;
514 }
515
516 @Override
517 public ZooKeeperWatcher getZooKeeper() {
518 return zkw;
519 }
520
521 @Override
522 public CoordinatedStateManager getCoordinatedStateManager() {
523 return null;
524 }
525 @Override
526 public ClusterConnection getConnection() {
527 return null;
528 }
529
530 @Override
531 public MetaTableLocator getMetaTableLocator() {
532 return null;
533 }
534
535 @Override
536 public ServerName getServerName() {
537 return ServerName.valueOf(hostname, 1234, 1L);
538 }
539
540 @Override
541 public void abort(String why, Throwable e) {
542
543 }
544
545 @Override
546 public boolean isAborted() {
547 return false;
548 }
549
550 @Override
551 public void stop(String why) {
552
553 }
554
555 @Override
556 public boolean isStopped() {
557 return false;
558 }
559
560 @Override
561 public ChoreService getChoreService() {
562 return null;
563 }
564 }
565 }