1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.client.replication;
19
20 import java.util.List;
21 import java.util.Map;
22 import java.util.TreeMap;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.HBaseTestingUtility;
28 import org.apache.hadoop.hbase.HConstants;
29 import org.apache.hadoop.hbase.TableName;
30 import org.apache.hadoop.hbase.replication.ReplicationException;
31 import org.apache.hadoop.hbase.replication.ReplicationPeer;
32 import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
33 import org.apache.hadoop.hbase.replication.ReplicationQueuesZKImpl;
34 import org.apache.hadoop.hbase.testclassification.MediumTests;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.junit.experimental.categories.Category;
39
40 import com.google.common.collect.Lists;
41
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertFalse;
44 import static org.junit.Assert.assertNotNull;
45 import static org.junit.Assert.assertTrue;
46 import static org.junit.Assert.fail;
47
48
49
50
51
52 @Category(MediumTests.class)
53 public class TestReplicationAdmin {
54
55 private static final Log LOG =
56 LogFactory.getLog(TestReplicationAdmin.class);
57 private final static HBaseTestingUtility TEST_UTIL =
58 new HBaseTestingUtility();
59
60 private final String ID_ONE = "1";
61 private final String KEY_ONE = "127.0.0.1:2181:/hbase";
62 private final String ID_SECOND = "2";
63 private final String KEY_SECOND = "127.0.0.1:2181:/hbase2";
64
65 private static ReplicationAdmin admin;
66
67
68
69
70 @BeforeClass
71 public static void setUpBeforeClass() throws Exception {
72 TEST_UTIL.startMiniZKCluster();
73 Configuration conf = TEST_UTIL.getConfiguration();
74 conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
75 admin = new ReplicationAdmin(conf);
76 }
77
78 @AfterClass
79 public static void tearDownAfterClass() throws Exception {
80 if (admin != null) {
81 admin.close();
82 }
83 TEST_UTIL.shutdownMiniZKCluster();
84 }
85
86
87
88
89
90
91 @Test
92 public void testAddRemovePeer() throws Exception {
93
94 admin.addPeer(ID_ONE, KEY_ONE);
95
96 try {
97 admin.addPeer(ID_ONE, KEY_ONE);
98 } catch (IllegalArgumentException iae) {
99
100 }
101 assertEquals(1, admin.getPeersCount());
102
103
104 try {
105 admin.addPeer(ID_ONE + "-" + ID_SECOND, KEY_ONE);
106 } catch (IllegalArgumentException iae) {
107
108 }
109 assertEquals(1, admin.getPeersCount());
110
111 try {
112 admin.addPeer(ReplicationQueuesZKImpl.RS_LOCK_ZNODE, KEY_ONE);
113 } catch (IllegalArgumentException iae) {
114
115 }
116 assertEquals(1, admin.getPeersCount());
117
118
119 try {
120 admin.removePeer(ID_SECOND);
121 fail();
122 } catch (IllegalArgumentException iae) {
123
124 }
125 assertEquals(1, admin.getPeersCount());
126
127 try {
128 admin.addPeer(ID_SECOND, KEY_SECOND);
129 } catch (IllegalStateException iae) {
130 fail();
131 }
132 assertEquals(2, admin.getPeersCount());
133
134 admin.removePeer(ID_ONE);
135 assertEquals(1, admin.getPeersCount());
136 admin.removePeer(ID_SECOND);
137 assertEquals(0, admin.getPeersCount());
138 }
139
140
141
142
143
144 @Test
145 public void testPeerConfig() throws Exception {
146 ReplicationPeerConfig config = new ReplicationPeerConfig();
147 config.setClusterKey(KEY_ONE);
148 config.getConfiguration().put("key1", "value1");
149 config.getConfiguration().put("key2", "value2");
150 admin.addPeer(ID_ONE, config, null);
151
152 List<ReplicationPeer> peers = admin.listReplicationPeers();
153 assertEquals(1, peers.size());
154 ReplicationPeer peerOne = peers.get(0);
155 assertNotNull(peerOne);
156 assertEquals("value1", peerOne.getConfiguration().get("key1"));
157 assertEquals("value2", peerOne.getConfiguration().get("key2"));
158
159 admin.removePeer(ID_ONE);
160 }
161
162
163
164
165
166 @Test
167 public void testEnableDisable() throws Exception {
168 admin.addPeer(ID_ONE, KEY_ONE);
169 assertEquals(1, admin.getPeersCount());
170 assertTrue(admin.getPeerState(ID_ONE));
171 admin.disablePeer(ID_ONE);
172
173 assertFalse(admin.getPeerState(ID_ONE));
174 try {
175 admin.getPeerState(ID_SECOND);
176 } catch (IllegalArgumentException iae) {
177
178 }
179 admin.removePeer(ID_ONE);
180 }
181
182 @Test
183 public void testGetTableCfsStr() {
184
185
186 Map<TableName, List<String>> tabCFsMap = null;
187
188
189 assertEquals(null, ReplicationAdmin.getTableCfsStr(tabCFsMap));
190
191
192
193 tabCFsMap = new TreeMap<TableName, List<String>>();
194 tabCFsMap.put(TableName.valueOf("tab1"), null);
195 assertEquals("tab1", ReplicationAdmin.getTableCfsStr(tabCFsMap));
196
197 tabCFsMap = new TreeMap<TableName, List<String>>();
198 tabCFsMap.put(TableName.valueOf("tab1"), Lists.newArrayList("cf1"));
199 assertEquals("tab1:cf1", ReplicationAdmin.getTableCfsStr(tabCFsMap));
200
201 tabCFsMap = new TreeMap<TableName, List<String>>();
202 tabCFsMap.put(TableName.valueOf("tab1"), Lists.newArrayList("cf1", "cf3"));
203 assertEquals("tab1:cf1,cf3", ReplicationAdmin.getTableCfsStr(tabCFsMap));
204
205
206 tabCFsMap = new TreeMap<TableName, List<String>>();
207 tabCFsMap.put(TableName.valueOf("tab1"), null);
208 tabCFsMap.put(TableName.valueOf("tab2"), Lists.newArrayList("cf1"));
209 tabCFsMap.put(TableName.valueOf("tab3"), Lists.newArrayList("cf1", "cf3"));
210 assertEquals("tab1;tab2:cf1;tab3:cf1,cf3", ReplicationAdmin.getTableCfsStr(tabCFsMap));
211 }
212
213 @Test
214 public void testAppendPeerTableCFs() throws Exception {
215
216 admin.addPeer(ID_ONE, KEY_ONE);
217
218 admin.appendPeerTableCFs(ID_ONE, "t1");
219 assertEquals("t1", admin.getPeerTableCFs(ID_ONE));
220
221
222 admin.appendPeerTableCFs(ID_ONE, "t2");
223 String peerTablesOne = admin.getPeerTableCFs(ID_ONE);
224
225
226
227
228
229 assertTrue("Should contain t1", peerTablesOne.contains("t1"));
230 assertTrue("Should contain t2", peerTablesOne.contains("t2"));
231 assertTrue("Should contain ; as the seperator", peerTablesOne.contains(";"));
232
233
234 admin.appendPeerTableCFs(ID_ONE, "t3:f1");
235 String peerTablesTwo = admin.getPeerTableCFs(ID_ONE);
236 assertTrue("Should contain t1", peerTablesTwo.contains("t1"));
237 assertTrue("Should contain t2", peerTablesTwo.contains("t2"));
238 assertTrue("Should contain t3:f1", peerTablesTwo.contains("t3:f1"));
239 assertTrue("Should contain ; as the seperator", peerTablesTwo.contains(";"));
240 admin.removePeer(ID_ONE);
241 }
242
243 @Test
244 public void testRemovePeerTableCFs() throws Exception {
245
246 admin.addPeer(ID_ONE, KEY_ONE);
247 try {
248 admin.removePeerTableCFs(ID_ONE, "t3");
249 assertTrue(false);
250 } catch (ReplicationException e) {
251 }
252 assertEquals("", admin.getPeerTableCFs(ID_ONE));
253
254 admin.setPeerTableCFs(ID_ONE, "t1;t2:cf1");
255 try {
256 admin.removePeerTableCFs(ID_ONE, "t3");
257 assertTrue(false);
258 } catch (ReplicationException e) {
259 }
260 assertEquals("t1;t2:cf1", admin.getPeerTableCFs(ID_ONE));
261
262 try {
263 admin.removePeerTableCFs(ID_ONE, "t1:f1");
264 assertTrue(false);
265 } catch (ReplicationException e) {
266 }
267 admin.removePeerTableCFs(ID_ONE, "t1");
268 assertEquals("t2:cf1", admin.getPeerTableCFs(ID_ONE));
269
270 try {
271 admin.removePeerTableCFs(ID_ONE, "t2");
272 assertTrue(false);
273 } catch (ReplicationException e) {
274 }
275 admin.removePeerTableCFs(ID_ONE, "t2:cf1");
276 assertEquals("", admin.getPeerTableCFs(ID_ONE));
277 admin.removePeer(ID_ONE);
278 }
279 }