-
Notifications
You must be signed in to change notification settings - Fork 683
/
SlowRecDUnitTest.java
1523 lines (1373 loc) · 55.5 KB
/
SlowRecDUnitTest.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.apache.geode.cache30;
import static java.lang.Runtime.getRuntime;
import static java.lang.System.currentTimeMillis;
import static org.apache.geode.cache.DataPolicy.REPLICATE;
import static org.apache.geode.cache.Scope.DISTRIBUTED_NO_ACK;
import static org.apache.geode.distributed.ConfigurationProperties.ASYNC_DISTRIBUTION_TIMEOUT;
import static org.apache.geode.distributed.ConfigurationProperties.ASYNC_MAX_QUEUE_SIZE;
import static org.apache.geode.distributed.ConfigurationProperties.ASYNC_QUEUE_TIMEOUT;
import static org.apache.geode.internal.tcp.Connection.FORCE_ASYNC_QUEUE;
import static org.apache.geode.test.dunit.LogWriterUtils.getLogWriter;
import static org.apache.geode.test.dunit.Wait.pause;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Properties;
import java.util.Set;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.apache.geode.cache.AttributesFactory;
import org.apache.geode.cache.CacheException;
import org.apache.geode.cache.CacheListener;
import org.apache.geode.cache.DataPolicy;
import org.apache.geode.cache.EntryEvent;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.Region.Entry;
import org.apache.geode.cache.RegionEvent;
import org.apache.geode.cache.Scope;
import org.apache.geode.cache.util.CacheListenerAdapter;
import org.apache.geode.distributed.internal.DMStats;
import org.apache.geode.distributed.internal.DistributionManager;
import org.apache.geode.test.awaitility.GeodeAwaitility;
import org.apache.geode.test.dunit.Host;
import org.apache.geode.test.dunit.LogWriterUtils;
import org.apache.geode.test.dunit.SerializableRunnable;
import org.apache.geode.test.dunit.ThreadUtils;
import org.apache.geode.test.dunit.VM;
import org.apache.geode.test.dunit.Wait;
import org.apache.geode.test.dunit.WaitCriterion;
import org.apache.geode.test.dunit.cache.internal.JUnit4CacheTestCase;
import org.apache.geode.test.junit.categories.MembershipTest;
/**
* Test to make sure slow receiver queuing is working
*
* @since GemFire 4.2.1
*/
@Category({MembershipTest.class})
@Ignore("Test was disabled by renaming to DisabledTest")
public class SlowRecDUnitTest extends JUnit4CacheTestCase {
protected static Object lastCallback = null;
// this test has special config of its distributed system so
// the setUp and tearDown methods need to make sure we don't
// use the ds from previous test and that we don't leave ours around
// for the next test to use.
@Override
public final void preSetUp() throws Exception {
disconnectAllFromDS();
}
@Override
public final void postTearDownCacheTestCase() throws Exception {
disconnectAllFromDS();
}
private VM getOtherVm() {
Host host = Host.getHost(0);
return host.getVM(0);
}
private void doCreateOtherVm(final Properties p, final boolean addListener) {
VM vm = getOtherVm();
vm.invoke(new CacheSerializableRunnable("create root") {
@Override
public void run2() throws CacheException {
getSystem(p);
createAckRegion(true, false);
AttributesFactory af = new AttributesFactory();
af.setScope(Scope.DISTRIBUTED_NO_ACK);
af.setDataPolicy(DataPolicy.REPLICATE);
if (addListener) {
CacheListener cl = new CacheListenerAdapter() {
@Override
public void afterUpdate(EntryEvent event) {
// make the slow receiver event slower!
try {
Thread.sleep(500);
} catch (InterruptedException shuttingDown) {
fail("interrupted");
}
}
};
af.setCacheListener(cl);
} else {
CacheListener cl = new CacheListenerAdapter() {
@Override
public void afterCreate(EntryEvent event) {
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
if (event.getKey().equals("sleepkey")) {
int sleepMs = ((Integer) event.getNewValue()).intValue();
try {
Thread.sleep(sleepMs);
} catch (InterruptedException ignore) {
fail("interrupted");
}
}
}
@Override
public void afterUpdate(EntryEvent event) {
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
if (event.getKey().equals("sleepkey")) {
int sleepMs = ((Integer) event.getNewValue()).intValue();
try {
Thread.sleep(sleepMs);
} catch (InterruptedException ignore) {
fail("interrupted");
}
}
}
@Override
public void afterInvalidate(EntryEvent event) {
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
}
@Override
public void afterDestroy(EntryEvent event) {
if (event.getCallbackArgument() != null) {
lastCallback = event.getCallbackArgument();
}
}
};
af.setCacheListener(cl);
}
Region r1 = createRootRegion("slowrec", af.create());
// place holder so we receive updates
r1.create("key", "value");
}
});
}
protected static final String CHECK_INVALID = "CHECK_INVALID";
private void checkLastValueInOtherVm(final String lastValue, final Object lcb) {
VM vm = getOtherVm();
vm.invoke(new CacheSerializableRunnable("check last value") {
@Override
public void run2() throws CacheException {
Region r1 = getRootRegion("slowrec");
if (lcb != null) {
WaitCriterion ev = new WaitCriterion() {
@Override
public boolean done() {
return lcb.equals(lastCallback);
}
@Override
public String description() {
return "waiting for callback";
}
};
GeodeAwaitility.await().untilAsserted(ev);
assertEquals(lcb, lastCallback);
}
if (lastValue == null) {
final Region r = r1;
WaitCriterion ev = new WaitCriterion() {
@Override
public boolean done() {
return r.getEntry("key") == null;
}
@Override
public String description() {
return "waiting for key to become null";
}
};
GeodeAwaitility.await().untilAsserted(ev);
assertEquals(null, r1.getEntry("key"));
} else if (CHECK_INVALID.equals(lastValue)) {
// should be invalid
{
final Region r = r1;
WaitCriterion ev = new WaitCriterion() {
@Override
public boolean done() {
Entry e = r.getEntry("key");
if (e == null) {
return false;
}
return e.getValue() == null;
}
@Override
public String description() {
return "waiting for invalidate";
}
};
GeodeAwaitility.await().untilAsserted(ev);
}
} else {
{
int retryCount = 1000;
Region.Entry re = null;
Object value = null;
while (retryCount-- > 0) {
re = r1.getEntry("key");
if (re != null) {
value = re.getValue();
if (value != null && value.equals(lastValue)) {
break;
}
}
try {
Thread.sleep(50);
} catch (InterruptedException ignore) {
fail("interrupted");
}
}
assertNotNull(re);
assertNotNull(value);
assertEquals(lastValue, value);
}
}
}
});
}
private void forceQueueFlush() {
FORCE_ASYNC_QUEUE = false;
final DMStats stats = getSystem().getDistributionManager().getStats();
WaitCriterion ev = new WaitCriterion() {
@Override
public boolean done() {
return stats.getAsyncThreads() == 0;
}
@Override
public String description() {
return "Waiting for async threads to disappear";
}
};
GeodeAwaitility.await().untilAsserted(ev);
}
private void forceQueuing(final Region r) throws CacheException {
FORCE_ASYNC_QUEUE = true;
final DMStats stats = getSystem().getDistributionManager().getStats();
r.put("forcekey", "forcevalue");
// wait for the flusher to get its first flush in progress
WaitCriterion ev = new WaitCriterion() {
@Override
public boolean done() {
return stats.getAsyncQueueFlushesInProgress() != 0;
}
@Override
public String description() {
return "waiting for flushes to start";
}
};
GeodeAwaitility.await().untilAsserted(ev);
}
/**
* Make sure that noack puts to a receiver will eventually queue and then catch up.
*/
@Test
public void testNoAck() throws Exception {
final AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_NO_ACK);
final Region r = createRootRegion("slowrec", factory.create());
final DMStats stats = getSystem().getDistributionManager().getStats();
// create receiver in vm0 with queuing enabled
Properties p = new Properties();
p.setProperty(ASYNC_DISTRIBUTION_TIMEOUT, "1");
doCreateOtherVm(p, false);
int repeatCount = 2;
int count = 0;
while (repeatCount-- > 0) {
forceQueuing(r);
final Object key = "key";
long queuedMsgs = stats.getAsyncQueuedMsgs();
long dequeuedMsgs = stats.getAsyncDequeuedMsgs();
long queueSize = stats.getAsyncQueueSize();
String lastValue = "";
final long intialQueuedMsgs = queuedMsgs;
long curQueuedMsgs = queuedMsgs - dequeuedMsgs;
try {
// loop while we still have queued the initially queued msgs
// OR the cur # of queued msgs < 6
while (dequeuedMsgs < intialQueuedMsgs || curQueuedMsgs <= 6) {
String value = "count=" + count;
lastValue = value;
r.put(key, value);
count++;
queueSize = stats.getAsyncQueueSize();
queuedMsgs = stats.getAsyncQueuedMsgs();
dequeuedMsgs = stats.getAsyncDequeuedMsgs();
curQueuedMsgs = queuedMsgs - dequeuedMsgs;
}
getLogWriter()
.info("After " + count + " " + " puts slowrec mode kicked in by queuing " + queuedMsgs
+ " for a total size of " + queueSize);
} finally {
forceQueueFlush();
}
WaitCriterion ev = new WaitCriterion() {
@Override
public boolean done() {
return stats.getAsyncQueueSize() == 0;
}
@Override
public String description() {
return "Waiting for queues to empty";
}
};
final long start = currentTimeMillis();
GeodeAwaitility.await().untilAsserted(ev);
final long finish = currentTimeMillis();
getLogWriter()
.info("After " + (finish - start) + " ms async msgs where flushed. A total of "
+ stats.getAsyncDequeuedMsgs() + " were flushed. lastValue=" + lastValue);
checkLastValueInOtherVm(lastValue, null);
}
}
/**
* Create a region named AckRegion with ACK scope
*/
protected Region createAckRegion(boolean mirror, boolean conflate) throws CacheException {
final AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
if (mirror) {
factory.setDataPolicy(DataPolicy.REPLICATE);
}
if (conflate) {
factory.setEnableAsyncConflation(true);
}
final Region r = createRootRegion("AckRegion", factory.create());
return r;
}
/**
* Make sure that noack puts to a receiver will eventually queue and then catch up with conflation
*/
@Test
public void testNoAckConflation() throws Exception {
final AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_NO_ACK);
factory.setEnableAsyncConflation(true);
final Region r = createRootRegion("slowrec", factory.create());
final DMStats stats = getSystem().getDistributionManager().getStats();
// create receiver in vm0 with queuing enabled
Properties p = new Properties();
p.setProperty(ASYNC_DISTRIBUTION_TIMEOUT, "1");
doCreateOtherVm(p, false);
forceQueuing(r);
final Object key = "key";
int count = 0;
final long initialConflatedMsgs = stats.getAsyncConflatedMsgs();
String lastValue = "";
final long intialDeQueuedMsgs = stats.getAsyncDequeuedMsgs();
long start = 0;
try {
while ((stats.getAsyncConflatedMsgs() - initialConflatedMsgs) < 1000) {
String value = "count=" + count;
lastValue = value;
r.put(key, value);
count++;
}
start = System.currentTimeMillis();
} finally {
forceQueueFlush();
}
final long finish = System.currentTimeMillis();
LogWriterUtils.getLogWriter()
.info("After " + (finish - start) + " ms async msgs where flushed. A total of "
+ (stats.getAsyncDequeuedMsgs() - intialDeQueuedMsgs)
+ " were flushed. Leaving a queue size of " + stats.getAsyncQueueSize()
+ ". The lastValue was " + lastValue);
checkLastValueInOtherVm(lastValue, null);
}
/**
* make sure ack does not hang make sure two ack updates do not conflate but are both queued
*/
@Test
public void testAckConflation() throws Exception {
final AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_NO_ACK);
factory.setEnableAsyncConflation(true);
final Region r = createRootRegion("slowrec", factory.create());
final Region ar = createAckRegion(false, true);
ar.create("ackKey", "ackValue");
final DMStats stats = getSystem().getDistributionManager().getStats();
// create receiver in vm0 with queuing enabled
Properties p = new Properties();
p.setProperty(ASYNC_DISTRIBUTION_TIMEOUT, "2");
doCreateOtherVm(p, false);
forceQueuing(r);
{
// make sure ack does not hang
// make sure two ack updates do not conflate but are both queued
long startQueuedMsgs = stats.getAsyncQueuedMsgs();
long startConflatedMsgs = stats.getAsyncConflatedMsgs();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
ar.put("ackKey", "ackValue");
}
});
t.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
ar.put("ackKey", "ackValue");
}
});
t2.start();
// give threads a chance to get queued
try {
Thread.sleep(100);
} catch (InterruptedException ignore) {
fail("interrupted");
}
forceQueueFlush();
ThreadUtils.join(t, 2 * 1000);
ThreadUtils.join(t2, 2 * 1000);
long endQueuedMsgs = stats.getAsyncQueuedMsgs();
long endConflatedMsgs = stats.getAsyncConflatedMsgs();
assertEquals(startConflatedMsgs, endConflatedMsgs);
// queue should be flushed by the time we get an ack
assertEquals(endQueuedMsgs, stats.getAsyncDequeuedMsgs());
assertEquals(startQueuedMsgs + 2, endQueuedMsgs);
}
}
/**
* Make sure that only sequences of updates are conflated Also checks that sending to a conflating
* region and non-conflating region does the correct thing. Test disabled because it
* intermittently fails due to race conditions in test. This has been fixed in congo's tests. See
* bug 35357.
*/
@Test
public void testConflationSequence() throws Exception {
final AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_NO_ACK);
factory.setEnableAsyncConflation(true);
final Region r = createRootRegion("slowrec", factory.create());
factory.setEnableAsyncConflation(false);
final Region noConflate = createRootRegion("noConflate", factory.create());
final DMStats stats = getSystem().getDistributionManager().getStats();
// create receiver in vm0 with queuing enabled
Properties p = new Properties();
p.setProperty(ASYNC_DISTRIBUTION_TIMEOUT, "1");
doCreateOtherVm(p, false);
{
VM vm = getOtherVm();
vm.invoke(new CacheSerializableRunnable("create noConflate") {
@Override
public void run2() throws CacheException {
AttributesFactory af = new AttributesFactory();
af.setScope(Scope.DISTRIBUTED_NO_ACK);
af.setDataPolicy(DataPolicy.REPLICATE);
createRootRegion("noConflate", af.create());
}
});
}
// now make sure update+destroy does not conflate
final Object key = "key";
LogWriterUtils.getLogWriter().info("[testConflationSequence] about to force queuing");
forceQueuing(r);
int count = 0;
String value = "";
String lastValue = value;
Object mylcb = null;
long initialConflatedMsgs = stats.getAsyncConflatedMsgs();
int endCount = count + 60;
LogWriterUtils.getLogWriter().info("[testConflationSequence] about to build up queue");
long begin = System.currentTimeMillis();
while (count < endCount) {
value = "count=" + count;
lastValue = value;
r.create(key, value);
count++;
value = "count=" + count;
lastValue = value;
r.put(key, value);
count++;
mylcb = value;
r.destroy(key, mylcb);
count++;
lastValue = null;
assertTrue(System.currentTimeMillis() < begin + 1000 * 60 * 2);
}
assertEquals(initialConflatedMsgs, stats.getAsyncConflatedMsgs());
forceQueueFlush();
checkLastValueInOtherVm(lastValue, mylcb);
// now make sure create+update+localDestroy does not conflate
LogWriterUtils.getLogWriter()
.info("[testConflationSequence] force queuing create-update-destroy");
forceQueuing(r);
initialConflatedMsgs = stats.getAsyncConflatedMsgs();
endCount = count + 40;
LogWriterUtils.getLogWriter().info("[testConflationSequence] create-update-destroy");
begin = System.currentTimeMillis();
while (count < endCount) {
value = "count=" + count;
lastValue = value;
r.create(key, value);
count++;
value = "count=" + count;
lastValue = value;
r.put(key, value);
count++;
r.localDestroy(key);
assertTrue(System.currentTimeMillis() < begin + 1000 * 60 * 2);
}
assertEquals(initialConflatedMsgs, stats.getAsyncConflatedMsgs());
forceQueueFlush();
checkLastValueInOtherVm(lastValue, null);
// now make sure update+invalidate does not conflate
LogWriterUtils.getLogWriter().info("[testConflationSequence] force queuing update-invalidate");
forceQueuing(r);
initialConflatedMsgs = stats.getAsyncConflatedMsgs();
value = "count=" + count;
lastValue = value;
r.create(key, value);
count++;
endCount = count + 40;
LogWriterUtils.getLogWriter().info("[testConflationSequence] update-invalidate");
begin = System.currentTimeMillis();
while (count < endCount) {
value = "count=" + count;
lastValue = value;
r.put(key, value);
count++;
r.invalidate(key);
count++;
lastValue = CHECK_INVALID;
assertTrue(System.currentTimeMillis() < begin + 1000 * 60 * 2);
}
assertEquals(initialConflatedMsgs, stats.getAsyncConflatedMsgs());
forceQueueFlush();
LogWriterUtils.getLogWriter().info("[testConflationSequence] assert other vm");
checkLastValueInOtherVm(lastValue, null);
r.destroy(key);
// now make sure updates to a conflating region are conflated even while
// updates to a non-conflating are not.
LogWriterUtils.getLogWriter().info("[testConflationSequence] conflate & no-conflate regions");
forceQueuing(r);
final long initialAsyncSocketWrites = stats.getAsyncSocketWrites();
value = "count=" + count;
lastValue = value;
long conflatedMsgs = stats.getAsyncConflatedMsgs();
long queuedMsgs = stats.getAsyncQueuedMsgs();
r.create(key, value);
queuedMsgs++;
assertEquals(queuedMsgs, stats.getAsyncQueuedMsgs());
assertEquals(conflatedMsgs, stats.getAsyncConflatedMsgs());
r.put(key, value);
queuedMsgs++;
assertEquals(queuedMsgs, stats.getAsyncQueuedMsgs());
assertEquals(conflatedMsgs, stats.getAsyncConflatedMsgs());
noConflate.create(key, value);
queuedMsgs++;
assertEquals(queuedMsgs, stats.getAsyncQueuedMsgs());
assertEquals(conflatedMsgs, stats.getAsyncConflatedMsgs());
noConflate.put(key, value);
queuedMsgs++;
assertEquals(queuedMsgs, stats.getAsyncQueuedMsgs());
assertEquals(conflatedMsgs, stats.getAsyncConflatedMsgs());
count++;
endCount = count + 80;
begin = System.currentTimeMillis();
LogWriterUtils.getLogWriter()
.info("[testConflationSequence:DEBUG] count=" + count + " queuedMsgs="
+ stats.getAsyncQueuedMsgs() + " conflatedMsgs=" + stats.getAsyncConflatedMsgs()
+ " dequeuedMsgs=" + stats.getAsyncDequeuedMsgs() + " asyncSocketWrites="
+ stats.getAsyncSocketWrites());
while (count < endCount) {
// make sure we continue to have a flush in progress
assertEquals(1, stats.getAsyncThreads());
assertEquals(1, stats.getAsyncQueues());
assertTrue(stats.getAsyncQueueFlushesInProgress() > 0);
// make sure we are not completing any flushing while this loop is in progress
assertEquals(initialAsyncSocketWrites, stats.getAsyncSocketWrites());
value = "count=" + count;
lastValue = value;
r.put(key, value);
count++;
// make sure it was conflated and not queued
assertEquals(queuedMsgs, stats.getAsyncQueuedMsgs());
conflatedMsgs++;
assertEquals(conflatedMsgs, stats.getAsyncConflatedMsgs());
noConflate.put(key, value);
// make sure it was queued and not conflated
queuedMsgs++;
assertEquals(queuedMsgs, stats.getAsyncQueuedMsgs());
assertEquals(conflatedMsgs, stats.getAsyncConflatedMsgs());
assertTrue(System.currentTimeMillis() < begin + 1000 * 60 * 2);
}
forceQueueFlush();
LogWriterUtils.getLogWriter().info("[testConflationSequence] assert other vm");
checkLastValueInOtherVm(lastValue, null);
}
/**
* Make sure that exceeding the queue size limit causes a disconnect.
*/
@Test
public void testSizeDisconnect() throws Exception {
final String expected =
"org.apache.geode.internal.tcp.ConnectionException: Forced disconnect sent to"
+ "||java.io.IOException: Broken pipe";
final String addExpected = "<ExpectedException action=add>" + expected + "</ExpectedException>";
final String removeExpected =
"<ExpectedException action=remove>" + expected + "</ExpectedException>";
final AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_NO_ACK);
final Region r = createRootRegion("slowrec", factory.create());
final DistributionManager dm = getSystem().getDistributionManager();
final DMStats stats = dm.getStats();
// set others before vm0 connects
final Set others = dm.getOtherDistributionManagerIds();
// create receiver in vm0 with queuing enabled
Properties p = new Properties();
p.setProperty(ASYNC_DISTRIBUTION_TIMEOUT, "5");
p.setProperty(ASYNC_MAX_QUEUE_SIZE, "1"); // 1 meg
doCreateOtherVm(p, false);
final Object key = "key";
final int VALUE_SIZE = 1024 * 100; // .1M async-max-queue-size should give us 10 of these 100K
// msgs before queue full
final byte[] value = new byte[VALUE_SIZE];
int count = 0;
forceQueuing(r);
long queuedMsgs = stats.getAsyncQueuedMsgs();
long queueSize = stats.getAsyncQueueSize();
getCache().getLogger().info(addExpected);
try {
while (stats.getAsyncQueueSizeExceeded() == 0 && stats.getAsyncQueueTimeouts() == 0) {
r.put(key, value);
count++;
if (stats.getAsyncQueueSize() > 0) {
queuedMsgs = stats.getAsyncQueuedMsgs();
queueSize = stats.getAsyncQueueSize();
}
if (count > 100) {
fail("should have exceeded max-queue-size by now");
}
}
getLogWriter()
.info("After " + count + " " + VALUE_SIZE
+ " byte puts slowrec mode kicked in but the queue filled when its size reached "
+ queueSize + " with " + queuedMsgs + " msgs");
// make sure we lost a connection to vm0
WaitCriterion ev = new WaitCriterion() {
@Override
public boolean done() {
return dm.getOtherDistributionManagerIds().size() <= others.size()
&& stats.getAsyncQueueSize() == 0;
}
@Override
public String description() {
return "waiting for connection loss";
}
};
GeodeAwaitility.await().untilAsserted(ev);
} finally {
forceQueueFlush();
getCache().getLogger().info(removeExpected);
}
assertEquals(others, dm.getOtherDistributionManagerIds());
assertEquals(0, stats.getAsyncQueueSize());
}
/**
* Make sure that exceeding the async-queue-timeout causes a disconnect.
* <p>
* [bruce] This test was disabled when the SlowRecDUnitTest was re-enabled in build.xml in the
* splitbrainNov07 branch. It had been disabled since June 2006 due to hangs. Some of the tests,
* like this one, still need work because the periodically (some quite often) fail.
*/
@Test
public void testTimeoutDisconnect() throws Exception {
final String expected =
"org.apache.geode.internal.tcp.ConnectionException: Forced disconnect sent to"
+ "||java.io.IOException: Broken pipe";
final String addExpected = "<ExpectedException action=add>" + expected + "</ExpectedException>";
final String removeExpected =
"<ExpectedException action=remove>" + expected + "</ExpectedException>";
final AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_NO_ACK);
final Region r = createRootRegion("slowrec", factory.create());
final DistributionManager dm = getSystem().getDistributionManager();
final DMStats stats = dm.getStats();
// set others before vm0 connects
final Set others = dm.getOtherDistributionManagerIds();
// create receiver in vm0 with queuing enabled
Properties p = new Properties();
p.setProperty(ASYNC_DISTRIBUTION_TIMEOUT, "5");
p.setProperty(ASYNC_QUEUE_TIMEOUT, "500"); // 500 ms
doCreateOtherVm(p, true);
final Object key = "key";
final int VALUE_SIZE = 1024; // 1k
final byte[] value = new byte[VALUE_SIZE];
int count = 0;
long queuedMsgs = stats.getAsyncQueuedMsgs();
long queueSize = stats.getAsyncQueueSize();
final long timeoutLimit = System.currentTimeMillis() + 5000;
getCache().getLogger().info(addExpected);
try {
while (stats.getAsyncQueueTimeouts() == 0) {
r.put(key, value);
count++;
if (stats.getAsyncQueueSize() > 0) {
queuedMsgs = stats.getAsyncQueuedMsgs();
queueSize = stats.getAsyncQueueSize();
}
if (currentTimeMillis() > timeoutLimit) {
fail("should have exceeded async-queue-timeout by now");
}
}
getLogWriter()
.info("After " + count + " " + VALUE_SIZE
+ " byte puts slowrec mode kicked in but the queue filled when its size reached "
+ queueSize + " with " + queuedMsgs + " msgs");
// make sure we lost a connection to vm0
WaitCriterion ev = new WaitCriterion() {
@Override
public boolean done() {
if (dm.getOtherDistributionManagerIds().size() > others.size()) {
return false;
}
return stats.getAsyncQueueSize() == 0;
}
@Override
public String description() {
return "waiting for departure";
}
};
GeodeAwaitility.await().untilAsserted(ev);
} finally {
getCache().getLogger().info(removeExpected);
}
assertEquals(others, dm.getOtherDistributionManagerIds());
assertEquals(0, stats.getAsyncQueueSize());
}
private static final String KEY_SLEEP = "KEY_SLEEP";
private static final String KEY_WAIT = "KEY_WAIT";
private static final String KEY_DISCONNECT = "KEY_DISCONNECT";
protected static final int CALLBACK_CREATE = 0;
protected static final int CALLBACK_UPDATE = 1;
protected static final int CALLBACK_INVALIDATE = 2;
protected static final int CALLBACK_DESTROY = 3;
protected static final int CALLBACK_REGION_INVALIDATE = 4;
protected static final Integer CALLBACK_CREATE_INTEGER = new Integer(CALLBACK_CREATE);
protected static final Integer CALLBACK_UPDATE_INTEGER = new Integer(CALLBACK_UPDATE);
protected static final Integer CALLBACK_INVALIDATE_INTEGER = new Integer(CALLBACK_INVALIDATE);
protected static final Integer CALLBACK_DESTROY_INTEGER = new Integer(CALLBACK_DESTROY);
protected static final Integer CALLBACK_REGION_INVALIDATE_INTEGER =
new Integer(CALLBACK_REGION_INVALIDATE);
private static class CallbackWrapper {
public final Object callbackArgument;
public final int callbackType;
public CallbackWrapper(Object callbackArgument, int callbackType) {
this.callbackArgument = callbackArgument;
this.callbackType = callbackType;
}
public String toString() {
return "CallbackWrapper: " + callbackArgument.toString() + " of type " + callbackType;
}
}
protected static class ControlListener extends CacheListenerAdapter {
public final LinkedList callbackArguments = new LinkedList();
public final LinkedList callbackTypes = new LinkedList();
public final Object CONTROL_LOCK = new Object();
@Override
public void afterCreate(EntryEvent event) {
LogWriterUtils.getLogWriter()
.info(event.getRegion().getName() + " afterCreate " + event.getKey());
synchronized (this.CONTROL_LOCK) {
if (event.getCallbackArgument() != null) {
this.callbackArguments
.add(new CallbackWrapper(event.getCallbackArgument(), CALLBACK_CREATE));
this.callbackTypes.add(CALLBACK_CREATE_INTEGER);
this.CONTROL_LOCK.notifyAll();
}
}
processEvent(event);
}
@Override
public void afterUpdate(EntryEvent event) {
LogWriterUtils.getLogWriter()
.info(event.getRegion().getName() + " afterUpdate " + event.getKey());
synchronized (this.CONTROL_LOCK) {
if (event.getCallbackArgument() != null) {
this.callbackArguments
.add(new CallbackWrapper(event.getCallbackArgument(), CALLBACK_UPDATE));
this.callbackTypes.add(CALLBACK_UPDATE_INTEGER);
this.CONTROL_LOCK.notifyAll();
}
}
processEvent(event);
}
@Override
public void afterInvalidate(EntryEvent event) {
synchronized (this.CONTROL_LOCK) {
if (event.getCallbackArgument() != null) {
this.callbackArguments
.add(new CallbackWrapper(event.getCallbackArgument(), CALLBACK_INVALIDATE));
this.callbackTypes.add(CALLBACK_INVALIDATE_INTEGER);
this.CONTROL_LOCK.notifyAll();
}
}
}
@Override
public void afterDestroy(EntryEvent event) {
synchronized (this.CONTROL_LOCK) {
if (event.getCallbackArgument() != null) {
this.callbackArguments
.add(new CallbackWrapper(event.getCallbackArgument(), CALLBACK_DESTROY));
this.callbackTypes.add(CALLBACK_DESTROY_INTEGER);
this.CONTROL_LOCK.notifyAll();
}
}
}
@Override
public void afterRegionInvalidate(RegionEvent event) {
synchronized (this.CONTROL_LOCK) {
if (event.getCallbackArgument() != null) {
this.callbackArguments
.add(new CallbackWrapper(event.getCallbackArgument(), CALLBACK_REGION_INVALIDATE));
this.callbackTypes.add(CALLBACK_REGION_INVALIDATE_INTEGER);
this.CONTROL_LOCK.notifyAll();
}
}
}
private void processEvent(EntryEvent event) {
if (event.getKey().equals(KEY_SLEEP)) {
processSleep(event);
} else if (event.getKey().equals(KEY_WAIT)) {
processWait(event);
} else if (event.getKey().equals(KEY_DISCONNECT)) {
processDisconnect(event);
}
}
private void processSleep(EntryEvent event) {
int sleepMs = ((Integer) event.getNewValue()).intValue();
LogWriterUtils.getLogWriter().info("[processSleep] sleeping for " + sleepMs);
try {
Thread.sleep(sleepMs);
} catch (InterruptedException ignore) {
fail("interrupted");
}
}
private void processWait(EntryEvent event) {
int sleepMs = ((Integer) event.getNewValue()).intValue();
LogWriterUtils.getLogWriter().info("[processWait] waiting for " + sleepMs);
synchronized (this.CONTROL_LOCK) {
try {
this.CONTROL_LOCK.wait(sleepMs);
} catch (InterruptedException ignore) {
return;
}
}
}
private void processDisconnect(EntryEvent event) {
LogWriterUtils.getLogWriter().info("[processDisconnect] disconnecting");
disconnectFromDS();
}
};
/**
* Make sure a multiple no ack regions conflate properly. [bruce] disabled when use of this dunit
* test class was reenabled in the splitbrainNov07 branch. The class had been disabled since June
* 2006 r13222 in the trunk. This test is failing because conflation isn't kicking in for some
* reason.
*/
@Test
public void testMultipleRegionConflation() throws Exception {
try {
doTestMultipleRegionConflation();
} finally {
// make sure other vm was notified even if test failed
getOtherVm().invoke(new SerializableRunnable("Wake up other vm") {
@Override
public void run() {
synchronized (doTestMultipleRegionConflation_R1_Listener.CONTROL_LOCK) {
doTestMultipleRegionConflation_R1_Listener.CONTROL_LOCK.notifyAll();
}
}
});
}
}
protected static ControlListener doTestMultipleRegionConflation_R1_Listener;
protected static ControlListener doTestMultipleRegionConflation_R2_Listener;
private void doTestMultipleRegionConflation() throws Exception {
final AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_NO_ACK);
factory.setEnableAsyncConflation(true);
final Region r1 = createRootRegion("slowrec1", factory.create());
final Region r2 = createRootRegion("slowrec2", factory.create());
assertTrue(getSystem().isConnected());
assertNotNull(r1);
assertFalse(r1.isDestroyed());
assertNotNull(getCache());
assertNotNull(getCache().getRegion("slowrec1"));
assertNotNull(r2);
assertFalse(r2.isDestroyed());
assertNotNull(getCache());
assertNotNull(getCache().getRegion("slowrec2"));