1
2
3
4
5
6
7
8
9
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
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 testEnableReplicationWhenSlaveClusterDoesntHaveTable() throws Exception {
71 admin2.disableTable(tableName);
72 admin2.deleteTable(tableName);
73 assertFalse(admin2.tableExists(tableName));
74 adminExt.enableTableRep(tableName);
75 assertTrue(admin2.tableExists(tableName));
76 }
77
78 @Test(timeout = 300000)
79 public void testEnableReplicationWhenReplicationNotEnabled() throws Exception {
80 HTableDescriptor table = admin1.getTableDescriptor(tableName);
81 for (HColumnDescriptor fam : table.getColumnFamilies()) {
82 fam.setScope(HConstants.REPLICATION_SCOPE_LOCAL);
83 }
84 admin1.disableTable(tableName);
85 admin1.modifyTable(tableName, table);
86 admin1.enableTable(tableName);
87
88 admin2.disableTable(tableName);
89 admin2.modifyTable(tableName, table);
90 admin2.enableTable(tableName);
91
92 adminExt.enableTableRep(tableName);
93 table = admin1.getTableDescriptor(tableName);
94 for (HColumnDescriptor fam : table.getColumnFamilies()) {
95 assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_GLOBAL);
96 }
97 }
98
99 @Test(timeout = 300000)
100 public void testEnableReplicationWhenTableDescriptorIsNotSameInClusters() throws Exception {
101 HTableDescriptor table = admin2.getTableDescriptor(tableName);
102 HColumnDescriptor f = new HColumnDescriptor("newFamily");
103 table.addFamily(f);
104 admin2.disableTable(tableName);
105 admin2.modifyTable(tableName, table);
106 admin2.enableTable(tableName);
107
108 try {
109 adminExt.enableTableRep(tableName);
110 fail("Exception should be thrown if table descriptors in the clusters are not same.");
111 } catch (RuntimeException ignored) {
112
113 }
114 admin1.disableTable(tableName);
115 admin1.modifyTable(tableName, table);
116 admin1.enableTable(tableName);
117 adminExt.enableTableRep(tableName);
118 table = admin1.getTableDescriptor(tableName);
119 for (HColumnDescriptor fam : table.getColumnFamilies()) {
120 assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_GLOBAL);
121 }
122 }
123
124 @Test(timeout = 300000)
125 public void testDisableAndEnableReplication() throws Exception {
126 adminExt.disableTableRep(tableName);
127 HTableDescriptor table = admin1.getTableDescriptor(tableName);
128 for (HColumnDescriptor fam : table.getColumnFamilies()) {
129 assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_LOCAL);
130 }
131 table = admin2.getTableDescriptor(tableName);
132 for (HColumnDescriptor fam : table.getColumnFamilies()) {
133 assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_LOCAL);
134 }
135 adminExt.enableTableRep(tableName);
136 table = admin1.getTableDescriptor(tableName);
137 for (HColumnDescriptor fam : table.getColumnFamilies()) {
138 assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_GLOBAL);
139 }
140 }
141
142 @Test(timeout = 300000, expected = TableNotFoundException.class)
143 public void testDisableReplicationForNonExistingTable() throws Exception {
144 adminExt.disableTableRep(TableName.valueOf("nonExistingTable"));
145 }
146
147 @Test(timeout = 300000, expected = TableNotFoundException.class)
148 public void testEnableReplicationForNonExistingTable() throws Exception {
149 adminExt.enableTableRep(TableName.valueOf("nonExistingTable"));
150 }
151
152 @Test(timeout = 300000, expected = IllegalArgumentException.class)
153 public void testDisableReplicationWhenTableNameAsNull() throws Exception {
154 adminExt.disableTableRep(null);
155 }
156
157 @Test(timeout = 300000, expected = IllegalArgumentException.class)
158 public void testEnableReplicationWhenTableNameAsNull() throws Exception {
159 adminExt.enableTableRep(null);
160 }
161
162
163
164
165
166 @Test(timeout = 300000)
167 public void testEnableReplicationForExplicitSetTableCfs() throws Exception {
168 TableName tn = TableName.valueOf("testEnableReplicationForSetTableCfs");
169 String peerId = "2";
170 if (admin2.isTableAvailable(tableName)) {
171 admin2.disableTable(tableName);
172 admin2.deleteTable(tableName);
173 }
174 assertFalse("Table should not exists in the peer cluster", admin2.isTableAvailable(tableName));
175
176 Map<TableName, ? extends Collection<String>> tableCfs =
177 new HashMap<TableName, Collection<String>>();
178 tableCfs.put(tn, null);
179 try {
180 adminExt.setPeerTableCFs(peerId, tableCfs);
181 adminExt.enableTableRep(tableName);
182 assertFalse("Table should not be created if user has set table cfs explicitly for the "
183 + "peer and this is not part of that collection",
184 admin2.isTableAvailable(tableName));
185
186 tableCfs.put(tableName, null);
187 adminExt.setPeerTableCFs(peerId, tableCfs);
188 adminExt.enableTableRep(tableName);
189 assertTrue(
190 "Table should be created if user has explicitly added table into table cfs collection",
191 admin2.isTableAvailable(tableName));
192 } finally {
193 adminExt.removePeerTableCFs(peerId, adminExt.getPeerTableCFs(peerId));
194 adminExt.disableTableRep(tableName);
195 }
196 }
197 }