View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
8    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
9    * for the specific language governing permissions and limitations under the License.
10   */
11  
12  package org.apache.hadoop.hbase.client.replication;
13  
14  import static org.junit.Assert.assertTrue;
15  
16  import org.apache.hadoop.conf.Configuration;
17  import org.apache.hadoop.hbase.HBaseConfiguration;
18  import org.apache.hadoop.hbase.HBaseTestingUtility;
19  import org.apache.hadoop.hbase.HColumnDescriptor;
20  import org.apache.hadoop.hbase.HConstants;
21  import org.apache.hadoop.hbase.HTableDescriptor;
22  import org.apache.hadoop.hbase.TableName;
23  import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
24  import org.apache.hadoop.hbase.testclassification.ClientTests;
25  import org.apache.hadoop.hbase.testclassification.MediumTests;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.junit.AfterClass;
28  import org.junit.BeforeClass;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  @Category({ MediumTests.class, ClientTests.class })
33  public class TestReplicationAdminWithTwoDifferentZKClusters {
34  
35    private static Configuration conf1 = HBaseConfiguration.create();
36    private static Configuration conf2;
37  
38    private static HBaseTestingUtility utility1;
39    private static HBaseTestingUtility utility2;
40    private static ReplicationAdmin admin;
41  
42    private static final TableName tableName = TableName.valueOf("test");
43    private static final byte[] famName = Bytes.toBytes("f");
44    private static final String peerId = "peer1";
45  
46    @BeforeClass
47    public static void setUpBeforeClass() throws Exception {
48      conf1.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
49      utility1 = new HBaseTestingUtility(conf1);
50      utility1.startMiniCluster();
51      admin = new ReplicationAdmin(conf1);
52  
53      conf2 = HBaseConfiguration.create(conf1);
54      conf2.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
55      conf2.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, 2182);
56  
57      utility2 = new HBaseTestingUtility(conf2);
58      utility2.startMiniCluster();
59  
60      ReplicationPeerConfig config = new ReplicationPeerConfig();
61      config.setClusterKey(utility2.getClusterKey());
62      admin.addPeer(peerId, config, null);
63  
64      HTableDescriptor table = new HTableDescriptor(tableName);
65      HColumnDescriptor fam = new HColumnDescriptor(famName);
66      fam.setScope(HConstants.REPLICATION_SCOPE_GLOBAL);
67      table.addFamily(fam);
68  
69      utility1.getHBaseAdmin().createTable(table, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
70      utility1.waitUntilAllRegionsAssigned(tableName);
71    }
72  
73    @AfterClass
74    public static void tearDownAfterClass() throws Exception {
75      admin.removePeer(peerId);
76      admin.close();
77      utility1.deleteTable(tableName);
78      utility2.deleteTable(tableName);
79      utility2.shutdownMiniCluster();
80      utility1.shutdownMiniCluster();
81    }
82  
83    /*
84     * Test for HBASE-15393
85     */
86    @Test
87    public void testEnableTableReplication() throws Exception {
88      admin.enableTableRep(tableName);
89      assertTrue(utility2.getHBaseAdmin().tableExists(tableName));
90    }
91  
92    @Test
93    public void testDisableTableReplication() throws Exception {
94      admin.disableTableRep(tableName);
95      assertTrue(utility2.getHBaseAdmin().tableExists(tableName));
96    }
97  }