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