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