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.hbase.ClusterId;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseZKTestingUtil;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.testclassification.MediumTests;
027import org.apache.hadoop.hbase.testclassification.ReplicationTests;
028import org.apache.hadoop.hbase.zookeeper.ZKClusterId;
029import org.apache.hadoop.hbase.zookeeper.ZKConfig;
030import org.apache.hadoop.hbase.zookeeper.ZKUtil;
031import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
032import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
033import org.apache.zookeeper.KeeperException;
034import org.junit.After;
035import org.junit.AfterClass;
036import org.junit.Before;
037import org.junit.BeforeClass;
038import org.junit.ClassRule;
039import org.junit.experimental.categories.Category;
040
041@Category({ ReplicationTests.class, MediumTests.class })
042public class TestReplicationStateZKImpl extends TestReplicationStateBasic {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046      HBaseClassTestRule.forClass(TestReplicationStateZKImpl.class);
047
048  private static Configuration conf;
049  private static HBaseZKTestingUtil utility;
050  private static ZKWatcher zkw;
051  private static String replicationZNode;
052
053  @BeforeClass
054  public static void setUpBeforeClass() throws Exception {
055    utility = new HBaseZKTestingUtil();
056    utility.startMiniZKCluster();
057    conf = utility.getConfiguration();
058    conf.setBoolean(HConstants.REPLICATION_BULKLOAD_ENABLE_KEY, true);
059    zkw = utility.getZooKeeperWatcher();
060    String replicationZNodeName = conf.get("zookeeper.znode.replication", "replication");
061    replicationZNode = ZNodePaths.joinZNode(zkw.getZNodePaths().baseZNode, replicationZNodeName);
062    KEY_ONE = initPeerClusterState("/hbase1");
063    KEY_TWO = initPeerClusterState("/hbase2");
064  }
065
066  private static String initPeerClusterState(String baseZKNode)
067      throws IOException, KeeperException {
068    // Add a dummy region server and set up the cluster id
069    Configuration testConf = new Configuration(conf);
070    testConf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, baseZKNode);
071    ZKWatcher zkw1 = new ZKWatcher(testConf, "test1", null);
072    String fakeRs = ZNodePaths.joinZNode(zkw1.getZNodePaths().rsZNode,
073            "hostname1.example.org:1234");
074    ZKUtil.createWithParents(zkw1, fakeRs);
075    ZKClusterId.setClusterId(zkw1, new ClusterId());
076    return ZKConfig.getZooKeeperClusterKey(testConf);
077  }
078
079  @Before
080  public void setUp() {
081    zkTimeoutCount = 0;
082    rqs = ReplicationStorageFactory.getReplicationQueueStorage(zkw, conf);
083    rp = ReplicationFactory.getReplicationPeers(zkw, conf);
084    OUR_KEY = ZKConfig.getZooKeeperClusterKey(conf);
085  }
086
087  @After
088  public void tearDown() throws KeeperException, IOException {
089    ZKUtil.deleteNodeRecursively(zkw, replicationZNode);
090  }
091
092  @AfterClass
093  public static void tearDownAfterClass() throws Exception {
094    utility.shutdownMiniZKCluster();
095  }
096}