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 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 import java.util.UUID;
33 import org.apache.hadoop.hbase.replication.BaseReplicationEndpoint;
34 import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
35
36
37
38
39 @Category({ MediumTests.class })
40 public class TestReplicationAdminWithClusters extends TestReplicationBase {
41
42 static HConnection connection1;
43 static HConnection connection2;
44 static HBaseAdmin admin1;
45 static HBaseAdmin admin2;
46 static ReplicationAdmin adminExt;
47
48 @BeforeClass
49 public static void setUpBeforeClass() throws Exception {
50 TestReplicationBase.setUpBeforeClass();
51 connection1 = HConnectionManager.createConnection(conf1);
52 connection2 = HConnectionManager.createConnection(conf2);
53 admin1 = new HBaseAdmin(connection1.getConfiguration());
54 admin2 = new HBaseAdmin(connection2.getConfiguration());
55 adminExt = new ReplicationAdmin(conf1);
56 }
57
58 @AfterClass
59 public static void tearDownAfterClass() throws Exception {
60 admin1.close();
61 admin2.close();
62 connection1.close();
63 connection2.close();
64 TestReplicationBase.tearDownAfterClass();
65 }
66
67 @Test(timeout = 300000)
68 public void testEnableReplicationWhenSlaveClusterDoesntHaveTable() throws Exception {
69 admin2.disableTable(tableName);
70 admin2.deleteTable(tableName);
71 assertFalse(admin2.tableExists(tableName));
72 adminExt.enableTableRep(TableName.valueOf(tableName));
73 assertTrue(admin2.tableExists(tableName));
74 }
75
76 @Test(timeout = 300000)
77 public void testEnableReplicationWhenReplicationNotEnabled() throws Exception {
78 HTableDescriptor table = admin1.getTableDescriptor(tableName);
79 for (HColumnDescriptor fam : table.getColumnFamilies()) {
80 fam.setScope(HConstants.REPLICATION_SCOPE_LOCAL);
81 }
82 admin1.disableTable(tableName);
83 admin1.modifyTable(tableName, table);
84 admin1.enableTable(tableName);
85
86 admin2.disableTable(tableName);
87 admin2.modifyTable(tableName, table);
88 admin2.enableTable(tableName);
89
90 adminExt.enableTableRep(TableName.valueOf(tableName));
91 table = admin1.getTableDescriptor(tableName);
92 for (HColumnDescriptor fam : table.getColumnFamilies()) {
93 assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_GLOBAL);
94 }
95 }
96
97 @Test(timeout = 300000)
98 public void testEnableReplicationWhenTableDescriptorIsNotSameInClusters() throws Exception {
99 HTableDescriptor table = admin2.getTableDescriptor(tableName);
100 HColumnDescriptor f = new HColumnDescriptor("newFamily");
101 table.addFamily(f);
102 admin2.disableTable(tableName);
103 admin2.modifyTable(tableName, table);
104 admin2.enableTable(tableName);
105
106 try {
107 adminExt.enableTableRep(TableName.valueOf(tableName));
108 fail("Exception should be thrown if table descriptors in the clusters are not same.");
109 } catch (RuntimeException ignored) {
110
111 }
112 admin1.disableTable(tableName);
113 admin1.modifyTable(tableName, table);
114 admin1.enableTable(tableName);
115 adminExt.enableTableRep(TableName.valueOf(tableName));
116 table = admin1.getTableDescriptor(tableName);
117 for (HColumnDescriptor fam : table.getColumnFamilies()) {
118 assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_GLOBAL);
119 }
120 }
121
122 @Test(timeout = 300000)
123 public void testDisableAndEnableReplication() throws Exception {
124 adminExt.disableTableRep(TableName.valueOf(tableName));
125 HTableDescriptor table = admin1.getTableDescriptor(tableName);
126 for (HColumnDescriptor fam : table.getColumnFamilies()) {
127 assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_LOCAL);
128 }
129 table = admin2.getTableDescriptor(tableName);
130 for (HColumnDescriptor fam : table.getColumnFamilies()) {
131 assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_LOCAL);
132 }
133 adminExt.enableTableRep(TableName.valueOf(tableName));
134 table = admin1.getTableDescriptor(tableName);
135 for (HColumnDescriptor fam : table.getColumnFamilies()) {
136 assertEquals(fam.getScope(), HConstants.REPLICATION_SCOPE_GLOBAL);
137 }
138 }
139
140 @Test(timeout = 300000, expected = TableNotFoundException.class)
141 public void testDisableReplicationForNonExistingTable() throws Exception {
142 adminExt.disableTableRep(TableName.valueOf("nonExistingTable"));
143 }
144
145 @Test(timeout = 300000, expected = TableNotFoundException.class)
146 public void testEnableReplicationForNonExistingTable() throws Exception {
147 adminExt.enableTableRep(TableName.valueOf("nonExistingTable"));
148 }
149
150 @Test(timeout = 300000, expected = IllegalArgumentException.class)
151 public void testDisableReplicationWhenTableNameAsNull() throws Exception {
152 adminExt.disableTableRep(null);
153 }
154
155 @Test(timeout = 300000, expected = IllegalArgumentException.class)
156 public void testEnableReplicationWhenTableNameAsNull() throws Exception {
157 adminExt.enableTableRep(null);
158 }
159
160 @Test(timeout=300000)
161 public void testReplicationPeerConfigUpdateCallback() throws Exception {
162 String peerId = "1";
163 ReplicationPeerConfig rpc = new ReplicationPeerConfig();
164 rpc.setClusterKey(utility2.getClusterKey());
165 rpc.setReplicationEndpointImpl(TestUpdatableReplicationEndpoint.class.getName());
166 rpc.getConfiguration().put("key1", "value1");
167 adminExt.addPeer(peerId, rpc, null);
168 adminExt.peerAdded(peerId);
169 rpc.getConfiguration().put("key1", "value2");
170 adminExt.updatePeerConfig(peerId, rpc);
171 if (!TestUpdatableReplicationEndpoint.hasCalledBack()) {
172 synchronized(TestUpdatableReplicationEndpoint.class) {
173 TestUpdatableReplicationEndpoint.class.wait(2000L);
174 }
175 }
176 assertEquals(true, TestUpdatableReplicationEndpoint.hasCalledBack());
177 adminExt.removePeer(peerId);
178 }
179
180 public static class TestUpdatableReplicationEndpoint extends BaseReplicationEndpoint {
181 private static boolean calledBack = false;
182
183 public static boolean hasCalledBack() {
184 return calledBack;
185 }
186
187 @Override
188 public synchronized void peerConfigUpdated(ReplicationPeerConfig rpc) {
189 calledBack = true;
190 notifyAll();
191 }
192
193 @Override
194 protected void doStart() {
195 notifyStarted();
196 }
197
198 @Override
199 protected void doStop() {
200 notifyStopped();
201 }
202
203 @Override
204 public UUID getPeerUUID() {
205 return UUID.randomUUID();
206 }
207
208 @Override
209 public boolean replicate(ReplicateContext replicateContext) {
210 return false;
211 }
212 }
213 }