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