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  package org.apache.hadoop.hbase.client.replication;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertFalse;
15  import static org.junit.Assert.assertTrue;
16  import static org.junit.Assert.fail;
17  
18  import org.apache.hadoop.hbase.HColumnDescriptor;
19  import org.apache.hadoop.hbase.HConstants;
20  import org.apache.hadoop.hbase.HTableDescriptor;
21  import org.apache.hadoop.hbase.TableName;
22  import org.apache.hadoop.hbase.TableNotFoundException;
23  import org.apache.hadoop.hbase.client.HBaseAdmin;
24  import org.apache.hadoop.hbase.client.HConnection;
25  import org.apache.hadoop.hbase.client.HConnectionManager;
26  import org.apache.hadoop.hbase.replication.TestReplicationBase;
27  import org.apache.hadoop.hbase.testclassification.MediumTests;
28  import org.junit.AfterClass;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  /**
34   * Unit testing of ReplicationAdmin with clusters
35   */
36  @Category({ MediumTests.class })
37  public class TestReplicationAdminWithClusters extends TestReplicationBase {
38  
39    static HConnection connection1;
40    static HConnection connection2;
41    static HBaseAdmin admin1;
42    static HBaseAdmin admin2;
43  
44    @BeforeClass
45    public static void setUpBeforeClass() throws Exception {
46      TestReplicationBase.setUpBeforeClass();
47      connection1 = HConnectionManager.createConnection(conf1);
48      connection2 = HConnectionManager.createConnection(conf2);
49      admin1 = new HBaseAdmin(connection1.getConfiguration());
50      admin2 = new HBaseAdmin(connection2.getConfiguration());
51    }
52  
53    @AfterClass
54    public static void tearDownAfterClass() throws Exception {
55      admin1.close();
56      admin2.close();
57      connection1.close();
58      connection2.close();
59      TestReplicationBase.tearDownAfterClass();
60    }
61  
62    @Test(timeout = 300000)
63    public void testEnableReplicationWhenSlaveClusterDoesntHaveTable() throws Exception {
64      admin2.disableTable(tableName);
65      admin2.deleteTable(tableName);
66      assertFalse(admin2.tableExists(tableName));
67      ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
68      adminExt.enableTableRep(TableName.valueOf(tableName));
69      assertTrue(admin2.tableExists(tableName));
70    }
71  
72    @Test(timeout = 300000)
73    public void testEnableReplicationWhenReplicationNotEnabled() throws Exception {
74      HTableDescriptor table = admin1.getTableDescriptor(tableName);
75      for (HColumnDescriptor fam : table.getColumnFamilies()) {
76        fam.setScope(HConstants.REPLICATION_SCOPE_LOCAL);
77      }
78      admin1.disableTable(tableName);
79      admin1.modifyTable(tableName, table);
80      admin1.enableTable(tableName);
81  
82      admin2.disableTable(tableName);
83      admin2.modifyTable(tableName, table);
84      admin2.enableTable(tableName);
85  
86      ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
87      adminExt.enableTableRep(TableName.valueOf(tableName));
88      table = admin1.getTableDescriptor(tableName);
89      for (HColumnDescriptor fam : table.getColumnFamilies()) {
90        assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_GLOBAL);
91      }
92    }
93  
94    @Test(timeout = 300000)
95    public void testEnableReplicationWhenTableDescriptorIsNotSameInClusters() throws Exception {
96      HTableDescriptor table = admin2.getTableDescriptor(tableName);
97      HColumnDescriptor f = new HColumnDescriptor("newFamily");
98      table.addFamily(f);
99      admin2.disableTable(tableName);
100     admin2.modifyTable(tableName, table);
101     admin2.enableTable(tableName);
102 
103     ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
104     try {
105       adminExt.enableTableRep(TableName.valueOf(tableName));
106       fail("Exception should be thrown if table descriptors in the clusters are not same.");
107     } catch (RuntimeException ignored) {
108 
109     }
110     admin1.disableTable(tableName);
111     admin1.modifyTable(tableName, table);
112     admin1.enableTable(tableName);
113     adminExt.enableTableRep(TableName.valueOf(tableName));
114     table = admin1.getTableDescriptor(tableName);
115     for (HColumnDescriptor fam : table.getColumnFamilies()) {
116       assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_GLOBAL);
117     }
118   }
119 
120   @Test(timeout = 300000)
121   public void testDisableAndEnableReplication() throws Exception {
122     ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
123     adminExt.disableTableRep(TableName.valueOf(tableName));
124     HTableDescriptor table = admin1.getTableDescriptor(tableName);
125     for (HColumnDescriptor fam : table.getColumnFamilies()) {
126       assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_LOCAL);
127     }
128     table = admin2.getTableDescriptor(tableName);
129     for (HColumnDescriptor fam : table.getColumnFamilies()) {
130       assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_LOCAL);
131     }
132     adminExt.enableTableRep(TableName.valueOf(tableName));
133     table = admin1.getTableDescriptor(tableName);
134     for (HColumnDescriptor fam : table.getColumnFamilies()) {
135       assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_GLOBAL);
136     }
137   }
138 
139   @Test(timeout = 300000, expected = TableNotFoundException.class)
140   public void testDisableReplicationForNonExistingTable() throws Exception {
141     ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
142     adminExt.disableTableRep(TableName.valueOf("nonExistingTable"));
143   }
144 
145   @Test(timeout = 300000, expected = TableNotFoundException.class)
146   public void testEnableReplicationForNonExistingTable() throws Exception {
147     ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
148     adminExt.enableTableRep(TableName.valueOf("nonExistingTable"));
149   }
150 
151   @Test(timeout = 300000, expected = IllegalArgumentException.class)
152   public void testDisableReplicationWhenTableNameAsNull() throws Exception {
153     ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
154     adminExt.disableTableRep(null);
155   }
156 
157   @Test(timeout = 300000, expected = IllegalArgumentException.class)
158   public void testEnableReplicationWhenTableNameAsNull() throws Exception {
159     ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
160     adminExt.enableTableRep(null);
161   }
162 }