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