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