/
FlushTest.java
513 lines (421 loc) · 17.5 KB
/
FlushTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package org.jgroups.tests;
import org.jgroups.*;
import org.jgroups.protocols.FD;
import org.jgroups.protocols.FD_ALL;
import org.jgroups.protocols.pbcast.FLUSH;
import org.jgroups.util.Util;
import org.testng.annotations.Test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
/**
* Tests the FLUSH protocol. Adds a FLUSH layer on top of the stack unless already present. Should
* work with any stack.
*
* @author Bela Ban
*/
@Test(groups = Global.FLUSH, sequential = false)
public class FlushTest extends ChannelTestBase {
@Test
public void testSingleChannel() throws Exception {
Semaphore s = new Semaphore(1);
FlushTestReceiver receivers[] = new FlushTestReceiver[] { new FlushTestReceiver("c1", s, 0,
FlushTestReceiver.CONNECT_ONLY) };
receivers[0].start();
s.release(1);
// Make sure everyone is in sync
Channel[] tmp = new Channel[receivers.length];
for (int i = 0; i < receivers.length; i++)
tmp[i] = receivers[i].getChannel();
Util.blockUntilViewsReceived(60000, 1000, tmp);
// Reacquire the semaphore tickets; when we have them all
// we know the threads are done
s.tryAcquire(1, 60, TimeUnit.SECONDS);
receivers[0].cleanup();
Util.sleep(1000);
checkEventStateTransferSequence(receivers[0]);
}
/**
* Tests issue #1 in http://jira.jboss.com/jira/browse/JGRP-335
*
* @throws Exception
*/
@Test
public void testJoinFollowedByUnicast() throws Exception {
JChannel c1 = null;
JChannel c2 = null;
try {
c1 = createChannel(true, 2);
c1.setReceiver(new SimpleReplier(c1, true));
c1.connect("testJoinFollowedByUnicast");
Address target = c1.getAddress();
Message unicast_msg = new Message(target);
c2 = createChannel(c1);
c2.setReceiver(new SimpleReplier(c2, false));
c2.connect("testJoinFollowedByUnicast");
// now send unicast, this might block as described in the case
c2.send(unicast_msg);
// if we don't get here this means we'd time out
} finally {
Util.close(c2, c1);
}
}
/**
* Tests issue #2 in http://jira.jboss.com/jira/browse/JGRP-335
*
* @throws Exception
*/
@Test
public void testStateTransferFollowedByUnicast() throws Exception {
JChannel c1 = null;
JChannel c2 = null;
try {
c1 = createChannel(true, 2);
c1.setReceiver(new SimpleReplier(c1, true));
c1.connect("testStateTransferFollowedByUnicast");
Address target = c1.getAddress();
Message unicast_msg = new Message(target);
c2 = createChannel(c1);
c2.setReceiver(new SimpleReplier(c2, false));
c2.connect("testStateTransferFollowedByUnicast");
log.info("\n** Getting the state **");
c2.getState(null, 10000);
// now send unicast, this might block as described in the case
c2.send(unicast_msg);
} finally {
Util.close(c2, c1);
}
}
@Test
public void testSequentialFlushInvocation() throws Exception {
Channel channel = null, channel2 = null, channel3 = null;
try {
channel = createChannel(true, 3);
channel.setName("A");
channel2 = createChannel((JChannel) channel);
channel2.setName("B");
channel3 = createChannel((JChannel) channel);
channel3.setName("C");
channel.connect("x");
channel2.connect("x");
channel3.connect("x");
//we need to sleep here since coordinator (channel)
//might be unblocked before channel3.connect() returns
Util.sleep(500);
for (int i = 0; i < 100; i++) {
System.out.print("flush #" + i + ": ");
long start = System.currentTimeMillis();
boolean status = channel.startFlush(false);
channel.stopFlush();
long diff = System.currentTimeMillis() - start;
System.out.println(status ? " OK (in " + diff + " ms)" : " FAIL");
assert status;
}
} finally {
Util.close(channel, channel2, channel3);
}
}
@Test
public void testFlushWithCrashedFlushCoordinator() throws Exception {
JChannel c1 = null;
JChannel c2 = null;
JChannel c3 = null;
try {
c1 = createChannel(true, 3, "C1"); changeProps(c1);
c1.connect("testFlushWithCrashedFlushCoordinator");
c2 = createChannel(c1, "C2"); changeProps(c2);
c2.connect("testFlushWithCrashedFlushCoordinator");
c3 = createChannel(c1, "C3"); changeProps(c3);
c3.connect("testFlushWithCrashedFlushCoordinator");
System.out.println("shutting down flush coordinator C2");
// send out START_FLUSH and then return
c2.down(new Event(Event.SUSPEND_BUT_FAIL));
// now shut down C2. This means, after failure detection kicks in and the new coordinator takes over
// (either C1 or C3), that the current flush started by C2 will be cancelled and a new flush (by C1 or C3)
// will be started
Util.shutdown(c2);
c1.getProtocolStack().findProtocol(FLUSH.class).setLevel("trace");
c3.getProtocolStack().findProtocol(FLUSH.class).setLevel("trace");
Util.blockUntilViewsReceived(10000, 500, c1, c3);
// cluster should not hang and two remaining members should have a correct view
assertTrue("correct view size", c1.getView().size() == 2);
assertTrue("correct view size", c3.getView().size() == 2);
c1.getProtocolStack().findProtocol(FLUSH.class).setLevel("warn");
c3.getProtocolStack().findProtocol(FLUSH.class).setLevel("warn");
} finally {
Util.close(c3, c2, c1);
}
}
@Test
public void testFlushWithCrashedParticipant() throws Exception {
JChannel c1 = null;
JChannel c2 = null;
JChannel c3 = null;
try {
c1 = createChannel(true, 3, "C1"); changeProps(c1);
c1.connect("testFlushWithCrashedParticipant");
c2 = createChannel(c1, "C2"); changeProps(c2);
c2.connect("testFlushWithCrashedParticipant");
c3 = createChannel(c1, "C3"); changeProps(c3);
c3.connect("testFlushWithCrashedParticipant");
System.out.println("shutting down C3");
Util.shutdown(c3); // kill a flush participant
System.out.println("C2: starting flush");
boolean rc=Util.startFlush(c2);
System.out.println("flush " + (rc? " was successful" : "failed"));
assert rc;
System.out.println("stopping flush");
c2.stopFlush();
System.out.println("waiting for view to contain C1 and C2");
Util.blockUntilViewsReceived(10000, 500, c1, c2);
// cluster should not hang and two remaining members should have a correct view
System.out.println("C1: view=" + c1.getView() + "\nC2: view=" + c2.getView());
assertTrue("correct view size", c1.getView().size() == 2);
assertTrue("correct view size", c2.getView().size() == 2);
} finally {
Util.close(c3, c2, c1);
}
}
@Test
public void testFlushWithCrashedParticipants() throws Exception {
JChannel c1 = null;
JChannel c2 = null;
JChannel c3 = null;
try {
c1 = createChannel(true, 3, "C1"); changeProps(c1);
c1.connect("testFlushWithCrashedFlushCoordinator");
c2 = createChannel(c1, "C2"); changeProps(c2);
c2.connect("testFlushWithCrashedFlushCoordinator");
c3 = createChannel(c1, "C3"); changeProps(c3);
c3.connect("testFlushWithCrashedFlushCoordinator");
// and then kill members other than flush coordinator
Util.shutdown(c3);
Util.shutdown(c1);
// start flush
Util.startFlush(c2);
c2.stopFlush();
Util.blockUntilViewsReceived(10000, 500, c2);
// cluster should not hang and one remaining member should have a correct view
assertTrue("correct view size", c2.getView().size() == 1);
} finally {
Util.close(c3, c2, c1);
}
}
/**
* Tests http://jira.jboss.com/jira/browse/JGRP-661
*
* @throws Exception
*/
@Test
public void testPartialFlush() throws Exception {
JChannel c1 = null;
JChannel c2 = null;
try {
c1 = createChannel(true, 2);
c1.setReceiver(new SimpleReplier(c1, true));
c1.connect("testPartialFlush");
c2 = createChannel(c1);
c2.setReceiver(new SimpleReplier(c2, false));
c2.connect("testPartialFlush");
List<Address> members = new ArrayList<Address>();
members.add(c2.getLocalAddress());
boolean flushedOk = Util.startFlush(c2, members);
assertTrue("Partial flush worked", flushedOk);
c2.stopFlush(members);
} finally {
Util.close(c2, c1);
}
}
/** Tests the emition of block/unblock/get|set state events */
@Test
public void testBlockingNoStateTransfer() {
String[] names = { "A", "B", "C", "D" };
_testChannels(names, FlushTestReceiver.CONNECT_ONLY);
}
/** Tests the emition of block/unblock/get|set state events */
@Test
public void testBlockingWithStateTransfer() {
String[] names = { "A", "B", "C", "D" };
_testChannels(names, FlushTestReceiver.CONNECT_AND_SEPARATE_GET_STATE);
}
/** Tests the emition of block/unblock/get|set state events */
@Test
public void testBlockingWithConnectAndStateTransfer() {
String[] names = { "A", "B", "C", "D" };
_testChannels(names, FlushTestReceiver.CONNECT_AND_GET_STATE);
}
private void _testChannels(String names[], int connectType) {
int count = names.length;
List<FlushTestReceiver> channels = new ArrayList<FlushTestReceiver>(count);
try {
// Create a semaphore and take all its permits
Semaphore semaphore = new Semaphore(count);
semaphore.acquire(count);
// Create channels and their threads that will block on the
// semaphore
boolean first = true;
for (String channelName : names) {
FlushTestReceiver channel = null;
if (first)
channel = new FlushTestReceiver(channelName, semaphore, 0, connectType);
else {
channel = new FlushTestReceiver((JChannel) channels.get(0).getChannel(),
channelName, semaphore, 0, connectType);
}
channels.add(channel);
// Release one ticket at a time to allow the thread to start working
channel.start();
semaphore.release(1);
if (first)
Util.sleep(3000); // minimize changes of a merge happening
first = false;
}
Channel[] tmp = new Channel[channels.size()];
int cnt = 0;
for (FlushTestReceiver receiver : channels)
tmp[cnt++] = receiver.getChannel();
Util.blockUntilViewsReceived(30000, 1000, tmp);
// Reacquire the semaphore tickets; when we have them all
// we know the threads are done
semaphore.tryAcquire(count, 40, TimeUnit.SECONDS);
} catch (Exception ex) {
log.warn("Exception encountered during test", ex);
assert false : "Exception encountered during test execution: " + ex;
} finally {
//let all events propagate...
Util.sleep(1000);
for (FlushTestReceiver app : channels)
app.getChannel().setReceiver(null);
for (FlushTestReceiver app : channels)
app.cleanup();
// verify block/unblock/view/get|set state sequences for all members
for (FlushTestReceiver receiver : channels) {
checkEventStateTransferSequence(receiver);
System.out.println("event sequence is OK");
}
}
}
private static void changeProps(JChannel ... channels) {
for(JChannel ch: channels) {
FD fd=(FD)ch.getProtocolStack().findProtocol(FD.class);
if(fd != null) {
fd.setTimeout(1000);
fd.setMaxTries(2);
}
FD_ALL fd_all=(FD_ALL)ch.getProtocolStack().findProtocol(FD_ALL.class);
if(fd_all != null) {
fd_all.setTimeout(2000);
fd_all.setInterval(800);
}
}
}
private class FlushTestReceiver extends PushChannelApplicationWithSemaphore {
private int connectMethod;
public static final int CONNECT_ONLY = 1;
public static final int CONNECT_AND_SEPARATE_GET_STATE = 2;
public static final int CONNECT_AND_GET_STATE = 3;
int msgCount = 0;
protected FlushTestReceiver(String name, Semaphore semaphore, int msgCount,
int connectMethod) throws Exception {
super(name, semaphore);
this.connectMethod = connectMethod;
this.msgCount = msgCount;
events = Collections.synchronizedList(new LinkedList<Object>());
if (connectMethod == CONNECT_ONLY || connectMethod == CONNECT_AND_SEPARATE_GET_STATE)
channel.connect("FlushTestReceiver");
if (connectMethod == CONNECT_AND_GET_STATE) {
channel.connect("FlushTestReceiver", null, null, 25000);
}
}
protected FlushTestReceiver(JChannel ch, String name, Semaphore semaphore, int msgCount,
int connectMethod) throws Exception {
super(ch, name, semaphore);
this.connectMethod = connectMethod;
this.msgCount = msgCount;
events = Collections.synchronizedList(new LinkedList<Object>());
if (connectMethod == CONNECT_ONLY || connectMethod == CONNECT_AND_SEPARATE_GET_STATE)
channel.connect("FlushTestReceiver");
if (connectMethod == CONNECT_AND_GET_STATE) {
channel.connect("FlushTestReceiver", null, null, 25000);
}
}
public List<Object> getEvents() {
return new LinkedList<Object>(events);
}
public byte[] getState() {
events.add(new GetStateEvent(null, null));
return new byte[] { 'b', 'e', 'l', 'a' };
}
public void getState(OutputStream ostream) {
super.getState(ostream);
byte[] payload = new byte[] { 'b', 'e', 'l', 'a' };
try {
ostream.write(payload);
} catch (IOException e) {
e.printStackTrace();
} finally {
Util.close(ostream);
}
}
public void setState(InputStream istream) {
super.setState(istream);
byte[] payload = new byte[4];
try {
istream.read(payload);
} catch (IOException e) {
e.printStackTrace();
} finally {
Util.close(istream);
}
}
protected void useChannel() throws Exception {
if (connectMethod == CONNECT_AND_SEPARATE_GET_STATE) {
channel.getState(null, 25000);
}
if (msgCount > 0) {
for (int i = 0; i < msgCount; i++) {
channel.send(new Message());
Util.sleep(100);
}
}
}
}
private class SimpleReplier extends ExtendedReceiverAdapter {
Channel channel;
boolean handle_requests = false;
public SimpleReplier(Channel channel, boolean handle_requests) {
this.channel = channel;
this.handle_requests = handle_requests;
}
public void receive(Message msg) {
Message reply = new Message(msg.getSrc());
try {
log.info("-- MySimpleReplier[" + channel.getAddress() + "]: received message from "
+ msg.getSrc());
if (handle_requests) {
log.info(", sending reply");
channel.send(reply);
} else
System.out.println("\n");
} catch (Exception e) {
e.printStackTrace();
}
}
public void viewAccepted(View new_view) {
log.info("-- MySimpleReplier[" + channel.getAddress() + "]: viewAccepted(" + new_view
+ ")");
}
public void block() {
log.info("-- MySimpleReplier[" + channel.getAddress() + "]: block()");
}
public void unblock() {
log.info("-- MySimpleReplier[" + channel.getAddress() + "]: unblock()");
}
}
}