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