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.client.replication;
019
020import static org.junit.Assert.assertTrue;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseConfiguration;
025import org.apache.hadoop.hbase.HBaseTestingUtility;
026import org.apache.hadoop.hbase.HColumnDescriptor;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.HTableDescriptor;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
031import org.apache.hadoop.hbase.testclassification.ClientTests;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.AfterClass;
035import org.junit.BeforeClass;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039
040@Category({ MediumTests.class, ClientTests.class })
041public class TestReplicationAdminWithTwoDifferentZKClusters {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045    HBaseClassTestRule.forClass(TestReplicationAdminWithTwoDifferentZKClusters.class);
046
047  private static Configuration conf1 = HBaseConfiguration.create();
048  private static Configuration conf2;
049
050  private static HBaseTestingUtility utility1;
051  private static HBaseTestingUtility utility2;
052  private static ReplicationAdmin admin;
053
054  private static final TableName tableName = TableName.valueOf("test");
055  private static final byte[] famName = Bytes.toBytes("f");
056  private static final String peerId = "peer1";
057
058  @BeforeClass
059  public static void setUpBeforeClass() throws Exception {
060    utility1 = new HBaseTestingUtility(conf1);
061    utility1.startMiniCluster();
062    admin = new ReplicationAdmin(conf1);
063
064    conf2 = HBaseConfiguration.create(conf1);
065    conf2.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
066    conf2.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, 2182);
067
068    utility2 = new HBaseTestingUtility(conf2);
069    utility2.startMiniCluster();
070
071    ReplicationPeerConfig config = new ReplicationPeerConfig();
072    config.setClusterKey(utility2.getClusterKey());
073    admin.addPeer(peerId, config, null);
074
075    HTableDescriptor table = new HTableDescriptor(tableName);
076    HColumnDescriptor fam = new HColumnDescriptor(famName);
077    fam.setScope(HConstants.REPLICATION_SCOPE_GLOBAL);
078    table.addFamily(fam);
079
080    utility1.getAdmin().createTable(table, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
081    utility1.waitUntilAllRegionsAssigned(tableName);
082  }
083
084  @AfterClass
085  public static void tearDownAfterClass() throws Exception {
086    admin.removePeer(peerId);
087    admin.close();
088    utility1.deleteTable(tableName);
089    utility2.deleteTable(tableName);
090    utility2.shutdownMiniCluster();
091    utility1.shutdownMiniCluster();
092  }
093
094  /*
095   * Test for HBASE-15393
096   */
097  @Test
098  public void testEnableTableReplication() throws Exception {
099    admin.enableTableRep(tableName);
100    assertTrue(utility2.getAdmin().tableExists(tableName));
101  }
102
103  @Test
104  public void testDisableTableReplication() throws Exception {
105    admin.disableTableRep(tableName);
106    assertTrue(utility2.getAdmin().tableExists(tableName));
107  }
108}