View Javadoc

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  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.ClusterId;
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.testclassification.MediumTests;
30  import org.apache.hadoop.hbase.Server;
31  import org.apache.hadoop.hbase.ServerName;
32  import org.apache.hadoop.hbase.catalog.CatalogTracker;
33  import org.apache.hadoop.hbase.zookeeper.ZKClusterId;
34  import org.apache.hadoop.hbase.zookeeper.ZKConfig;
35  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
36  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
37  import org.apache.zookeeper.KeeperException;
38  import org.junit.After;
39  import org.junit.AfterClass;
40  import org.junit.Test;
41  
42  import static org.junit.Assert.*;
43  
44  import org.junit.Before;
45  import org.junit.BeforeClass;
46  import org.junit.experimental.categories.Category;
47  
48  @Category(MediumTests.class)
49  public class TestReplicationStateZKImpl extends TestReplicationStateBasic {
50  
51    private static final Log LOG = LogFactory.getLog(TestReplicationStateZKImpl.class);
52  
53    private static Configuration conf;
54    private static HBaseTestingUtility utility;
55    private static ZooKeeperWatcher zkw;
56    private static String replicationZNode;
57    private ReplicationQueuesZKImpl rqZK;
58  
59    @BeforeClass
60    public static void setUpBeforeClass() throws Exception {
61      utility = new HBaseTestingUtility();
62      utility.startMiniZKCluster();
63      conf = utility.getConfiguration();
64      zkw = HBaseTestingUtility.getZooKeeperWatcher(utility);
65      String replicationZNodeName = conf.get("zookeeper.znode.replication", "replication");
66      replicationZNode = ZKUtil.joinZNode(zkw.baseZNode, replicationZNodeName);
67      KEY_ONE = initPeerClusterState("/hbase1");
68      KEY_TWO = initPeerClusterState("/hbase2");
69    }
70  
71    private static String initPeerClusterState(String baseZKNode)
72        throws IOException, KeeperException {
73      // Add a dummy region server and set up the cluster id
74      Configuration testConf = new Configuration(conf);
75      testConf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, baseZKNode);
76      ZooKeeperWatcher zkw1 = new ZooKeeperWatcher(testConf, "test1", null);
77      String fakeRs = ZKUtil.joinZNode(zkw1.rsZNode, "hostname1.example.org:1234");
78      ZKUtil.createWithParents(zkw1, fakeRs);
79      ZKClusterId.setClusterId(zkw1, new ClusterId());
80      return ZKConfig.getZooKeeperClusterKey(testConf);
81    }
82  
83    @Before
84    @Override
85    public void setUp() {
86      super.setUp();
87      DummyServer ds1 = new DummyServer(server1);
88      DummyServer ds2 = new DummyServer(server2);
89      DummyServer ds3 = new DummyServer(server3);
90      rq1 = ReplicationFactory.getReplicationQueues(zkw, conf, ds1);
91      rq2 = ReplicationFactory.getReplicationQueues(zkw, conf, ds2);
92      rq3 = ReplicationFactory.getReplicationQueues(zkw, conf, ds3);
93      rqc = ReplicationFactory.getReplicationQueuesClient(zkw, conf, ds1);
94      rp = ReplicationFactory.getReplicationPeers(zkw, conf, zkw);
95      OUR_KEY = ZKConfig.getZooKeeperClusterKey(conf);
96      rqZK = new ReplicationQueuesZKImpl(zkw, conf, ds1);
97    }
98  
99    @After
100   public void tearDown() throws KeeperException, IOException {
101     ZKUtil.deleteNodeRecursively(zkw, replicationZNode);
102   }
103 
104   @AfterClass
105   public static void tearDownAfterClass() throws Exception {
106     utility.shutdownMiniZKCluster();
107   }
108 
109   @Test
110   public void testIsPeerPath_PathToParentOfPeerNode() {
111     assertFalse(rqZK.isPeerPath(rqZK.peersZNode));
112   }
113 
114   @Test
115   public void testIsPeerPath_PathToChildOfPeerNode() {
116     String peerChild = ZKUtil.joinZNode(ZKUtil.joinZNode(rqZK.peersZNode, "1"), "child");
117     assertFalse(rqZK.isPeerPath(peerChild));
118   }
119 
120   @Test
121   public void testIsPeerPath_ActualPeerPath() {
122     String peerPath = ZKUtil.joinZNode(rqZK.peersZNode, "1");
123     assertTrue(rqZK.isPeerPath(peerPath));
124   }
125 
126   static class DummyServer implements Server {
127     private String serverName;
128     private boolean isAborted = false;
129     private boolean isStopped = false;
130 
131     public DummyServer(String serverName) {
132       this.serverName = serverName;
133     }
134 
135     @Override
136     public Configuration getConfiguration() {
137       return conf;
138     }
139 
140     @Override
141     public ZooKeeperWatcher getZooKeeper() {
142       return zkw;
143     }
144 
145     @Override
146     public CatalogTracker getCatalogTracker() {
147       return null;
148     }
149 
150     @Override
151     public ServerName getServerName() {
152       return ServerName.valueOf(this.serverName);
153     }
154 
155     @Override
156     public void abort(String why, Throwable e) {
157       LOG.info("Aborting " + serverName);
158       this.isAborted = true;
159     }
160 
161     @Override
162     public boolean isAborted() {
163       return this.isAborted;
164     }
165 
166     @Override
167     public void stop(String why) {
168       this.isStopped = true;
169     }
170 
171     @Override
172     public boolean isStopped() {
173       return this.isStopped;
174     }
175   }
176 }
177