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 java.util.Collection;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.apache.hadoop.hbase.HColumnDescriptor;
23  import org.apache.hadoop.hbase.HConstants;
24  import org.apache.hadoop.hbase.HTableDescriptor;
25  import org.apache.hadoop.hbase.TableName;
26  import org.apache.hadoop.hbase.TableNotFoundException;
27  import org.apache.hadoop.hbase.client.Admin;
28  import org.apache.hadoop.hbase.client.Connection;
29  import org.apache.hadoop.hbase.client.ConnectionFactory;
30  import org.apache.hadoop.hbase.replication.TestReplicationBase;
31  import org.apache.hadoop.hbase.testclassification.MediumTests;
32  import org.junit.AfterClass;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  /**
38   * Unit testing of ReplicationAdmin with clusters
39   */
40  @Category({ MediumTests.class })
41  public class TestReplicationAdminWithClusters extends TestReplicationBase {
42  
43    static Connection connection1;
44    static Connection connection2;
45    static Admin admin1;
46    static Admin admin2;
47    static ReplicationAdmin adminExt;
48  
49    @BeforeClass
50    public static void setUpBeforeClass() throws Exception {
51      TestReplicationBase.setUpBeforeClass();
52      connection1 = ConnectionFactory.createConnection(conf1);
53      connection2 = ConnectionFactory.createConnection(conf2);
54      admin1 = connection1.getAdmin();
55      admin2 = connection2.getAdmin();
56      adminExt = new ReplicationAdmin(conf1);
57    }
58  
59    @AfterClass
60    public static void tearDownAfterClass() throws Exception {
61      admin1.close();
62      admin2.close();
63      adminExt.close();
64      connection1.close();
65      connection2.close();
66      TestReplicationBase.tearDownAfterClass();
67    }
68  
69    @Test(timeout = 300000)
70    public void disableNotFullReplication() throws Exception {
71      HTableDescriptor table = admin2.getTableDescriptor(tableName);
72      HColumnDescriptor f = new HColumnDescriptor("notReplicatedFamily");
73      table.addFamily(f);
74      admin1.disableTable(tableName);
75      admin1.modifyTable(tableName, table);
76      admin1.enableTable(tableName);
77  
78  
79      admin.disableTableRep(tableName);
80      table = admin1.getTableDescriptor(tableName);
81      for (HColumnDescriptor fam : table.getColumnFamilies()) {
82        assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_LOCAL);
83      }
84    }
85  
86    @Test(timeout = 300000)
87    public void testEnableReplicationWhenSlaveClusterDoesntHaveTable() throws Exception {
88      admin2.disableTable(tableName);
89      admin2.deleteTable(tableName);
90      assertFalse(admin2.tableExists(tableName));
91      adminExt.enableTableRep(tableName);
92      assertTrue(admin2.tableExists(tableName));
93    }
94  
95    @Test(timeout = 300000)
96    public void testEnableReplicationWhenReplicationNotEnabled() throws Exception {
97      HTableDescriptor table = admin1.getTableDescriptor(tableName);
98      for (HColumnDescriptor fam : table.getColumnFamilies()) {
99        fam.setScope(HConstants.REPLICATION_SCOPE_LOCAL);
100     }
101     admin1.disableTable(tableName);
102     admin1.modifyTable(tableName, table);
103     admin1.enableTable(tableName);
104 
105     admin2.disableTable(tableName);
106     admin2.modifyTable(tableName, table);
107     admin2.enableTable(tableName);
108 
109     adminExt.enableTableRep(tableName);
110     table = admin1.getTableDescriptor(tableName);
111     for (HColumnDescriptor fam : table.getColumnFamilies()) {
112       assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_GLOBAL);
113     }
114   }
115 
116   @Test(timeout = 300000)
117   public void testEnableReplicationWhenTableDescriptorIsNotSameInClusters() throws Exception {
118     HTableDescriptor table = admin2.getTableDescriptor(tableName);
119     HColumnDescriptor f = new HColumnDescriptor("newFamily");
120     table.addFamily(f);
121     admin2.disableTable(tableName);
122     admin2.modifyTable(tableName, table);
123     admin2.enableTable(tableName);
124 
125     try {
126       adminExt.enableTableRep(tableName);
127       fail("Exception should be thrown if table descriptors in the clusters are not same.");
128     } catch (RuntimeException ignored) {
129 
130     }
131     admin1.disableTable(tableName);
132     admin1.modifyTable(tableName, table);
133     admin1.enableTable(tableName);
134     adminExt.enableTableRep(tableName);
135     table = admin1.getTableDescriptor(tableName);
136     for (HColumnDescriptor fam : table.getColumnFamilies()) {
137       assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_GLOBAL);
138     }
139   }
140 
141   @Test(timeout = 300000)
142   public void testDisableAndEnableReplication() throws Exception {
143     adminExt.disableTableRep(tableName);
144     HTableDescriptor table = admin1.getTableDescriptor(tableName);
145     for (HColumnDescriptor fam : table.getColumnFamilies()) {
146       assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_LOCAL);
147     }
148     table = admin2.getTableDescriptor(tableName);
149     for (HColumnDescriptor fam : table.getColumnFamilies()) {
150       assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_LOCAL);
151     }
152     adminExt.enableTableRep(tableName);
153     table = admin1.getTableDescriptor(tableName);
154     for (HColumnDescriptor fam : table.getColumnFamilies()) {
155       assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_GLOBAL);
156     }
157   }
158 
159   @Test(timeout = 300000, expected = TableNotFoundException.class)
160   public void testDisableReplicationForNonExistingTable() throws Exception {
161     adminExt.disableTableRep(TableName.valueOf("nonExistingTable"));
162   }
163 
164   @Test(timeout = 300000, expected = TableNotFoundException.class)
165   public void testEnableReplicationForNonExistingTable() throws Exception {
166     adminExt.enableTableRep(TableName.valueOf("nonExistingTable"));
167   }
168 
169   @Test(timeout = 300000, expected = IllegalArgumentException.class)
170   public void testDisableReplicationWhenTableNameAsNull() throws Exception {
171     adminExt.disableTableRep(null);
172   }
173 
174   @Test(timeout = 300000, expected = IllegalArgumentException.class)
175   public void testEnableReplicationWhenTableNameAsNull() throws Exception {
176     adminExt.enableTableRep(null);
177   }
178   
179   /*
180    * Test enable table replication should create table only in user explicit specified table-cfs.
181    * HBASE-14717
182    */
183   @Test(timeout = 300000)
184   public void testEnableReplicationForExplicitSetTableCfs() throws Exception {
185     TableName tn = TableName.valueOf("testEnableReplicationForSetTableCfs");
186     String peerId = "2";
187     if (admin2.isTableAvailable(tableName)) {
188       admin2.disableTable(tableName);
189       admin2.deleteTable(tableName);
190     }
191     assertFalse("Table should not exists in the peer cluster", admin2.isTableAvailable(tableName));
192 
193     Map<TableName, ? extends Collection<String>> tableCfs =
194         new HashMap<TableName, Collection<String>>();
195     tableCfs.put(tn, null);
196     try {
197       adminExt.setPeerTableCFs(peerId, tableCfs);
198       adminExt.enableTableRep(tableName);
199       assertFalse("Table should not be created if user has set table cfs explicitly for the "
200           + "peer and this is not part of that collection",
201         admin2.isTableAvailable(tableName));
202 
203       tableCfs.put(tableName, null);
204       adminExt.setPeerTableCFs(peerId, tableCfs);
205       adminExt.enableTableRep(tableName);
206       assertTrue(
207         "Table should be created if user has explicitly added table into table cfs collection",
208         admin2.isTableAvailable(tableName));
209     } finally {
210       adminExt.removePeerTableCFs(peerId, adminExt.getPeerTableCFs(peerId));
211       adminExt.disableTableRep(tableName);
212     }
213   }
214 }