1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.replication;
20
21 import static org.junit.Assert.*;
22
23 import java.util.List;
24 import java.util.SortedMap;
25 import java.util.SortedSet;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.hbase.ServerName;
30 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
31 import org.junit.Before;
32 import org.junit.Test;
33
34
35
36
37
38 public abstract class TestReplicationStateBasic {
39
40 protected ReplicationQueues rq1;
41 protected ReplicationQueues rq2;
42 protected ReplicationQueues rq3;
43 protected ReplicationQueuesClient rqc;
44 protected String server1 = ServerName.valueOf("hostname1.example.org", 1234, -1L).toString();
45 protected String server2 = ServerName.valueOf("hostname2.example.org", 1234, -1L).toString();
46 protected String server3 = ServerName.valueOf("hostname3.example.org", 1234, -1L).toString();
47 protected ReplicationPeers rp;
48 protected static final String ID_ONE = "1";
49 protected static final String ID_TWO = "2";
50 protected static String KEY_ONE;
51 protected static String KEY_TWO;
52
53
54 protected String OUR_ID = "3";
55 protected String OUR_KEY;
56
57 protected static int zkTimeoutCount;
58 protected static final int ZK_MAX_COUNT = 300;
59 protected static final int ZK_SLEEP_INTERVAL = 100;
60
61 private static final Log LOG = LogFactory.getLog(TestReplicationStateBasic.class);
62
63 @Before
64 public void setUp() {
65 zkTimeoutCount = 0;
66 }
67
68 @Test
69 public void testReplicationQueuesClient() throws ReplicationException {
70 rqc.init();
71
72 assertEquals(0, rqc.getListOfReplicators().size());
73 assertNull(rqc.getLogsInQueue(server1, "qId1"));
74 assertNull(rqc.getAllQueues(server1));
75
76
77
78
79
80 rq1.init(server1);
81 rq2.init(server2);
82 rq1.addLog("qId1", "trash");
83 rq1.removeLog("qId1", "trash");
84 rq1.addLog("qId2", "filename1");
85 rq1.addLog("qId3", "filename2");
86 rq1.addLog("qId3", "filename3");
87 rq2.addLog("trash", "trash");
88 rq2.removeQueue("trash");
89
90 List<String> reps = rqc.getListOfReplicators();
91 assertEquals(2, reps.size());
92 assertTrue(server1, reps.contains(server1));
93 assertTrue(server2, reps.contains(server2));
94
95 assertNull(rqc.getLogsInQueue("bogus", "bogus"));
96 assertNull(rqc.getLogsInQueue(server1, "bogus"));
97 assertEquals(0, rqc.getLogsInQueue(server1, "qId1").size());
98 assertEquals(1, rqc.getLogsInQueue(server1, "qId2").size());
99 assertEquals("filename1", rqc.getLogsInQueue(server1, "qId2").get(0));
100
101 assertNull(rqc.getAllQueues("bogus"));
102 assertEquals(0, rqc.getAllQueues(server2).size());
103 List<String> list = rqc.getAllQueues(server1);
104 assertEquals(3, list.size());
105 assertTrue(list.contains("qId2"));
106 assertTrue(list.contains("qId3"));
107 }
108
109 @Test
110 public void testReplicationQueues() throws ReplicationException {
111 rq1.init(server1);
112 rq2.init(server2);
113 rq3.init(server3);
114
115 rp.init();
116
117
118 assertEquals(3, rq1.getListOfReplicators().size());
119 rq1.removeQueue("bogus");
120 rq1.removeLog("bogus", "bogus");
121 rq1.removeAllQueues();
122 assertNull(rq1.getAllQueues());
123 assertEquals(0, rq1.getLogPosition("bogus", "bogus"));
124 assertNull(rq1.getLogsInQueue("bogus"));
125 assertEquals(0, rq1.claimQueues(ServerName.valueOf("bogus", 1234, -1L).toString()).size());
126
127 rq1.setLogPosition("bogus", "bogus", 5L);
128
129 populateQueues();
130
131 assertEquals(3, rq1.getListOfReplicators().size());
132 assertEquals(0, rq2.getLogsInQueue("qId1").size());
133 assertEquals(5, rq3.getLogsInQueue("qId5").size());
134 assertEquals(0, rq3.getLogPosition("qId1", "filename0"));
135 rq3.setLogPosition("qId5", "filename4", 354L);
136 assertEquals(354L, rq3.getLogPosition("qId5", "filename4"));
137
138 assertEquals(5, rq3.getLogsInQueue("qId5").size());
139 assertEquals(0, rq2.getLogsInQueue("qId1").size());
140 assertEquals(0, rq1.getAllQueues().size());
141 assertEquals(1, rq2.getAllQueues().size());
142 assertEquals(5, rq3.getAllQueues().size());
143
144 assertEquals(0, rq3.claimQueues(server1).size());
145 assertEquals(2, rq3.getListOfReplicators().size());
146
147 SortedMap<String, SortedSet<String>> queues = rq2.claimQueues(server3);
148 assertEquals(5, queues.size());
149 assertEquals(1, rq2.getListOfReplicators().size());
150
151
152 assertEquals(0, rq2.claimQueues(server2).size());
153
154 assertEquals(6, rq2.getAllQueues().size());
155
156 rq2.removeAllQueues();
157
158 assertEquals(0, rq2.getListOfReplicators().size());
159 }
160
161 @Test
162 public void testReplicationPeers() throws Exception {
163 rp.init();
164
165
166 try {
167 rp.removePeer("bogus");
168 fail("Should have thrown an IllegalArgumentException when passed a bogus peerId");
169 } catch (IllegalArgumentException e) {
170 }
171 try {
172 rp.enablePeer("bogus");
173 fail("Should have thrown an IllegalArgumentException when passed a bogus peerId");
174 } catch (IllegalArgumentException e) {
175 }
176 try {
177 rp.disablePeer("bogus");
178 fail("Should have thrown an IllegalArgumentException when passed a bogus peerId");
179 } catch (IllegalArgumentException e) {
180 }
181 try {
182 rp.getStatusOfPeer("bogus");
183 fail("Should have thrown an IllegalArgumentException when passed a bogus peerId");
184 } catch (IllegalArgumentException e) {
185 }
186 assertFalse(rp.peerAdded("bogus"));
187 rp.peerRemoved("bogus");
188
189 assertNull(rp.getPeerConf("bogus"));
190 assertNumberOfPeers(0);
191
192
193 rp.addPeer(ID_ONE, new ReplicationPeerConfig().setClusterKey(KEY_ONE), null);
194 assertNumberOfPeers(1);
195 rp.addPeer(ID_TWO, new ReplicationPeerConfig().setClusterKey(KEY_TWO), null);
196 assertNumberOfPeers(2);
197
198
199 try {
200 rp.getStatusOfPeer(ID_ONE);
201 fail("There are no connected peers, should have thrown an IllegalArgumentException");
202 } catch (IllegalArgumentException e) {
203 }
204 assertEquals(KEY_ONE, ZKUtil.getZooKeeperClusterKey(rp.getPeerConf(ID_ONE).getSecond()));
205 rp.removePeer(ID_ONE);
206 rp.peerRemoved(ID_ONE);
207 assertNumberOfPeers(1);
208
209
210 rp.addPeer(ID_ONE, new ReplicationPeerConfig().setClusterKey(KEY_ONE), null);
211 rp.peerAdded(ID_ONE);
212 assertNumberOfPeers(2);
213 assertTrue(rp.getStatusOfPeer(ID_ONE));
214 rp.disablePeer(ID_ONE);
215 assertConnectedPeerStatus(false, ID_ONE);
216 rp.enablePeer(ID_ONE);
217 assertConnectedPeerStatus(true, ID_ONE);
218
219
220 rp.peerRemoved(ID_ONE);
221 assertNumberOfPeers(2);
222 try {
223 rp.getStatusOfPeer(ID_ONE);
224 fail("There are no connected peers, should have thrown an IllegalArgumentException");
225 } catch (IllegalArgumentException e) {
226 }
227 }
228
229 protected void assertConnectedPeerStatus(boolean status, String peerId) throws Exception {
230
231 if (status != rp.getStatusOfPeerFromBackingStore(peerId)) {
232 fail("ConnectedPeerStatus was " + !status + " but expected " + status + " in ZK");
233 }
234 while (true) {
235 if (status == rp.getStatusOfPeer(peerId)) {
236 return;
237 }
238 if (zkTimeoutCount < ZK_MAX_COUNT) {
239 LOG.debug("ConnectedPeerStatus was " + !status + " but expected " + status
240 + ", sleeping and trying again.");
241 Thread.sleep(ZK_SLEEP_INTERVAL);
242 } else {
243 fail("Timed out waiting for ConnectedPeerStatus to be " + status);
244 }
245 }
246 }
247
248 protected void assertNumberOfPeers(int total) {
249 assertEquals(total, rp.getAllPeerConfigs().size());
250 assertEquals(total, rp.getAllPeerIds().size());
251 assertEquals(total, rp.getAllPeerIds().size());
252 }
253
254
255
256
257
258 protected void populateQueues() throws ReplicationException {
259 rq1.addLog("trash", "trash");
260 rq1.removeQueue("trash");
261
262 rq2.addLog("qId1", "trash");
263 rq2.removeLog("qId1", "trash");
264
265 for (int i = 1; i < 6; i++) {
266 for (int j = 0; j < i; j++) {
267 rq3.addLog("qId" + i, "filename" + j);
268 }
269
270 rp.addPeer("qId" + i, new ReplicationPeerConfig().setClusterKey("bogus" + i), null);
271 }
272 }
273 }
274