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