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.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     * Unit tests that uses ZooKeeper but does not use the master-side methods
55     * but rather acts directly on ZK.
56     * @throws Exception
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      // Should not have a master yet
66      MasterAddressTracker addressTracker = new MasterAddressTracker(zk, null);
67      addressTracker.start();
68      assertFalse(addressTracker.hasMaster());
69      zk.registerListener(addressTracker);
70  
71      // Use a listener to capture when the node is actually created
72      NodeCreationListener listener = new NodeCreationListener(zk, zk.getMasterAddressZNode());
73      zk.registerListener(listener);
74  
75      // Create the master node with a dummy address
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      // Wait for the node to be created
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