001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.replication;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.fs.FileSystem;
023import org.apache.hadoop.hbase.ClusterId;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseZKTestingUtility;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.testclassification.ReplicationTests;
029import org.apache.hadoop.hbase.zookeeper.ZKClusterId;
030import org.apache.hadoop.hbase.zookeeper.ZKConfig;
031import org.apache.hadoop.hbase.zookeeper.ZKUtil;
032import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
033import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
034import org.apache.zookeeper.KeeperException;
035import org.junit.After;
036import org.junit.AfterClass;
037import org.junit.Before;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.experimental.categories.Category;
041
042@Category({ ReplicationTests.class, MediumTests.class })
043public class TestReplicationStateZKImpl extends TestReplicationStateBasic {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047    HBaseClassTestRule.forClass(TestReplicationStateZKImpl.class);
048
049  private static Configuration conf;
050  private static HBaseZKTestingUtility utility;
051  private static ZKWatcher zkw;
052  private static String replicationZNode;
053
054  @BeforeClass
055  public static void setUpBeforeClass() throws Exception {
056    utility = new HBaseZKTestingUtility();
057    utility.startMiniZKCluster();
058    conf = utility.getConfiguration();
059    conf.setBoolean(HConstants.REPLICATION_BULKLOAD_ENABLE_KEY, true);
060    zkw = utility.getZooKeeperWatcher();
061    String replicationZNodeName = conf.get("zookeeper.znode.replication", "replication");
062    replicationZNode = ZNodePaths.joinZNode(zkw.getZNodePaths().baseZNode, replicationZNodeName);
063    KEY_ONE = initPeerClusterState("/hbase1");
064    KEY_TWO = initPeerClusterState("/hbase2");
065  }
066
067  private static String initPeerClusterState(String baseZKNode)
068    throws IOException, KeeperException {
069    // Add a dummy region server and set up the cluster id
070    Configuration testConf = new Configuration(conf);
071    testConf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, baseZKNode);
072    ZKWatcher zkw1 = new ZKWatcher(testConf, "test1", null);
073    String fakeRs =
074      ZNodePaths.joinZNode(zkw1.getZNodePaths().rsZNode, "hostname1.example.org:1234");
075    ZKUtil.createWithParents(zkw1, fakeRs);
076    ZKClusterId.setClusterId(zkw1, new ClusterId());
077    return ZKConfig.getZooKeeperClusterKey(testConf);
078  }
079
080  @Before
081  public void setUp() throws IOException {
082    zkTimeoutCount = 0;
083    rqs = ReplicationStorageFactory.getReplicationQueueStorage(zkw, conf);
084    rp =
085      ReplicationFactory.getReplicationPeers(FileSystem.get(utility.getConfiguration()), zkw, conf);
086    OUR_KEY = ZKConfig.getZooKeeperClusterKey(conf);
087  }
088
089  @After
090  public void tearDown() throws KeeperException, IOException {
091    ZKUtil.deleteNodeRecursively(zkw, replicationZNode);
092  }
093
094  @AfterClass
095  public static void tearDownAfterClass() throws Exception {
096    utility.shutdownMiniZKCluster();
097  }
098}