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