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