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.Admin;
24  import org.apache.hadoop.hbase.client.Connection;
25  import org.apache.hadoop.hbase.client.ConnectionFactory;
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 Connection connection1;
40    static Connection connection2;
41    static Admin admin1;
42    static Admin admin2;
43  
44    @BeforeClass
45    public static void setUpBeforeClass() throws Exception {
46      TestReplicationBase.setUpBeforeClass();
47      connection1 = ConnectionFactory.createConnection(conf1);
48      connection2 = ConnectionFactory.createConnection(conf2);
49      admin1 = connection1.getAdmin();
50      admin2 = connection2.getAdmin();
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 disableNotFullReplication() throws Exception {
64      HTableDescriptor table = admin2.getTableDescriptor(tableName);
65      HColumnDescriptor f = new HColumnDescriptor("notReplicatedFamily");
66      table.addFamily(f);
67      admin1.disableTable(tableName);
68      admin1.modifyTable(tableName, table);
69      admin1.enableTable(tableName);
70  
71  
72      admin.disableTableRep(tableName);
73      table = admin1.getTableDescriptor(tableName);
74      for (HColumnDescriptor fam : table.getColumnFamilies()) {
75        assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_LOCAL);
76      }
77    }
78  
79    @Test(timeout = 300000)
80    public void testEnableReplicationWhenSlaveClusterDoesntHaveTable() throws Exception {
81      admin.disableTableRep(tableName);
82      admin2.disableTable(tableName);
83      admin2.deleteTable(tableName);
84      assertFalse(admin2.tableExists(tableName));
85      ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
86      adminExt.enableTableRep(tableName);
87      assertTrue(admin2.tableExists(tableName));
88    }
89  
90    @Test(timeout = 300000)
91    public void testEnableReplicationWhenReplicationNotEnabled() throws Exception {
92      HTableDescriptor table = admin1.getTableDescriptor(tableName);
93      for (HColumnDescriptor fam : table.getColumnFamilies()) {
94        fam.setScope(HConstants.REPLICATION_SCOPE_LOCAL);
95      }
96      admin1.disableTable(tableName);
97      admin1.modifyTable(tableName, table);
98      admin1.enableTable(tableName);
99  
100     admin2.disableTable(tableName);
101     admin2.modifyTable(tableName, table);
102     admin2.enableTable(tableName);
103 
104     ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
105     adminExt.enableTableRep(tableName);
106     table = admin1.getTableDescriptor(tableName);
107     for (HColumnDescriptor fam : table.getColumnFamilies()) {
108       assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_GLOBAL);
109     }
110   }
111 
112   @Test(timeout = 300000)
113   public void testEnableReplicationWhenTableDescriptorIsNotSameInClusters() throws Exception {
114     HTableDescriptor table = admin2.getTableDescriptor(tableName);
115     HColumnDescriptor f = new HColumnDescriptor("newFamily");
116     table.addFamily(f);
117     admin2.disableTable(tableName);
118     admin2.modifyTable(tableName, table);
119     admin2.enableTable(tableName);
120 
121     ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
122     try {
123       adminExt.enableTableRep(tableName);
124       fail("Exception should be thrown if table descriptors in the clusters are not same.");
125     } catch (RuntimeException ignored) {
126 
127     }
128     admin1.disableTable(tableName);
129     admin1.modifyTable(tableName, table);
130     admin1.enableTable(tableName);
131     adminExt.enableTableRep(tableName);
132     table = admin1.getTableDescriptor(tableName);
133     for (HColumnDescriptor fam : table.getColumnFamilies()) {
134       assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_GLOBAL);
135     }
136   }
137 
138   @Test(timeout = 300000)
139   public void testDisableAndEnableReplication() throws Exception {
140     ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
141     adminExt.disableTableRep(tableName);
142     HTableDescriptor table = admin1.getTableDescriptor(tableName);
143     for (HColumnDescriptor fam : table.getColumnFamilies()) {
144       assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_LOCAL);
145     }
146     table = admin2.getTableDescriptor(tableName);
147     for (HColumnDescriptor fam : table.getColumnFamilies()) {
148       assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_LOCAL);
149     }
150     adminExt.enableTableRep(tableName);
151     table = admin1.getTableDescriptor(tableName);
152     for (HColumnDescriptor fam : table.getColumnFamilies()) {
153       assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_GLOBAL);
154     }
155   }
156 
157   @Test(timeout = 300000, expected = TableNotFoundException.class)
158   public void testDisableReplicationForNonExistingTable() throws Exception {
159     ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
160     adminExt.disableTableRep(TableName.valueOf("nonExistingTable"));
161   }
162 
163   @Test(timeout = 300000, expected = TableNotFoundException.class)
164   public void testEnableReplicationForNonExistingTable() throws Exception {
165     ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
166     adminExt.enableTableRep(TableName.valueOf("nonExistingTable"));
167   }
168 
169   @Test(timeout = 300000, expected = IllegalArgumentException.class)
170   public void testDisableReplicationWhenTableNameAsNull() throws Exception {
171     ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
172     adminExt.disableTableRep(null);
173   }
174 
175   @Test(timeout = 300000, expected = IllegalArgumentException.class)
176   public void testEnableReplicationWhenTableNameAsNull() throws Exception {
177     ReplicationAdmin adminExt = new ReplicationAdmin(conf1);
178     adminExt.enableTableRep(null);
179   }
180 }