View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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     * Unit tests that uses ZooKeeper but does not use the master-side methods
57     * but rather acts directly on ZK.
58     * @throws Exception
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      // Should not have a master yet
68      MasterAddressTracker addressTracker = new MasterAddressTracker(zk, null);
69      addressTracker.start();
70      assertFalse(addressTracker.hasMaster());
71      zk.registerListener(addressTracker);
72  
73      // Use a listener to capture when the node is actually created
74      NodeCreationListener listener = new NodeCreationListener(zk, zk.getMasterAddressZNode());
75      zk.registerListener(listener);
76  
77      // Create the master node with a dummy address
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      // Wait for the node to be created
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