1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.replication;
20  
21  import java.io.IOException;
22  import java.util.concurrent.atomic.AtomicBoolean;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseTestingUtility;
26  import org.apache.hadoop.hbase.HConstants;
27  import org.apache.hadoop.hbase.MediumTests;
28  import org.apache.hadoop.hbase.Server;
29  import org.apache.hadoop.hbase.ServerName;
30  import org.apache.hadoop.hbase.catalog.CatalogTracker;
31  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
32  import org.apache.zookeeper.KeeperException;
33  import org.junit.AfterClass;
34  import org.junit.BeforeClass;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  import static org.junit.Assert.assertEquals;
39  
40  @Category(MediumTests.class)
41  public class TestReplicationZookeeper {
42  
43    private static Configuration conf;
44  
45    private static HBaseTestingUtility utility;
46  
47    private static ZooKeeperWatcher zkw;
48  
49    private static ReplicationZookeeper repZk;
50  
51    private static String slaveClusterKey;
52  
53    @BeforeClass
54    public static void setUpBeforeClass() throws Exception {
55      utility = new HBaseTestingUtility();
56      utility.startMiniZKCluster();
57      conf = utility.getConfiguration();
58      zkw = HBaseTestingUtility.getZooKeeperWatcher(utility);
59      DummyServer server = new DummyServer();
60      repZk = new ReplicationZookeeper(server, new AtomicBoolean());
61      slaveClusterKey = conf.get(HConstants.ZOOKEEPER_QUORUM) + ":" +
62        conf.get("hbase.zookeeper.property.clientPort") + ":/1";
63    }
64  
65    @AfterClass
66    public static void tearDownAfterClass() throws Exception {
67      utility.shutdownMiniZKCluster();
68    }
69  
70    @Test
71    public void testGetAddressesMissingSlave()
72      throws IOException, KeeperException {
73      repZk.addPeer("1", slaveClusterKey);
74      // HBASE-5586 used to get an NPE
75      assertEquals(0, repZk.getSlavesAddresses("1").size());
76    }
77  
78    static class DummyServer implements Server {
79  
80      @Override
81      public Configuration getConfiguration() {
82        return conf;
83      }
84  
85      @Override
86      public ZooKeeperWatcher getZooKeeper() {
87        return zkw;
88      }
89  
90      @Override
91      public CatalogTracker getCatalogTracker() {
92        return null;
93      }
94  
95      @Override
96      public ServerName getServerName() {
97        return new ServerName("hostname.example.org", 1234, -1L);
98      }
99  
100     @Override
101     public void abort(String why, Throwable e) {
102     }
103 
104     @Override
105     public boolean isAborted() {
106       return false;
107     }
108 
109     @Override
110     public void stop(String why) {
111     }
112 
113     @Override
114     public boolean isStopped() {
115       return false;
116     }
117   }
118 }