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