1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24
25 import java.util.concurrent.Semaphore;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.hbase.*;
30 import org.apache.hadoop.hbase.testclassification.MediumTests;
31 import org.apache.hadoop.hbase.zookeeper.MasterAddressTracker;
32 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
33 import org.apache.hadoop.hbase.zookeeper.ZooKeeperListener;
34 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.junit.experimental.categories.Category;
39
40 @Category(MediumTests.class)
41 public class TestMasterAddressTracker {
42 private static final Log LOG = LogFactory.getLog(TestMasterAddressTracker.class);
43
44 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
45
46 @BeforeClass
47 public static void setUpBeforeClass() throws Exception {
48 TEST_UTIL.startMiniZKCluster();
49 }
50
51 @AfterClass
52 public static void tearDownAfterClass() throws Exception {
53 TEST_UTIL.shutdownMiniZKCluster();
54 }
55
56
57
58
59
60 @Test
61 public void testMasterAddressTrackerFromZK() throws Exception {
62
63 ZooKeeperWatcher zk = new ZooKeeperWatcher(TEST_UTIL.getConfiguration(),
64 "testMasterAddressTrackerFromZK", null);
65 ZKUtil.createAndFailSilent(zk, zk.baseZNode);
66
67
68 MasterAddressTracker addressTracker = new MasterAddressTracker(zk, null);
69 addressTracker.start();
70 assertFalse(addressTracker.hasMaster());
71 zk.registerListener(addressTracker);
72
73
74 NodeCreationListener listener = new NodeCreationListener(zk, zk.getMasterAddressZNode());
75 zk.registerListener(listener);
76
77
78 String host = "localhost";
79 int port = 1234;
80 int infoPort = 1235;
81 ServerName sn = ServerName.valueOf(host, port, System.currentTimeMillis());
82 LOG.info("Creating master node");
83 MasterAddressTracker.setMasterAddress(zk, zk.getMasterAddressZNode(), sn, infoPort);
84
85
86 LOG.info("Waiting for master address manager to be notified");
87 listener.waitForCreation();
88 LOG.info("Master node created");
89 assertTrue(addressTracker.hasMaster());
90 ServerName pulledAddress = addressTracker.getMasterAddress();
91 assertTrue(pulledAddress.equals(sn));
92 assertEquals(infoPort, addressTracker.getMasterInfoPort());
93 }
94
95 public static class NodeCreationListener extends ZooKeeperListener {
96 private static final Log LOG = LogFactory.getLog(NodeCreationListener.class);
97
98 private Semaphore lock;
99 private String node;
100
101 public NodeCreationListener(ZooKeeperWatcher watcher, String node) {
102 super(watcher);
103 lock = new Semaphore(0);
104 this.node = node;
105 }
106
107 @Override
108 public void nodeCreated(String path) {
109 if(path.equals(node)) {
110 LOG.debug("nodeCreated(" + path + ")");
111 lock.release();
112 }
113 }
114
115 public void waitForCreation() throws InterruptedException {
116 lock.acquire();
117 }
118 }
119
120 }
121