-
Notifications
You must be signed in to change notification settings - Fork 912
/
AMQPReplicaTest.java
1374 lines (1068 loc) · 56 KB
/
AMQPReplicaTest.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.activemq.artemis.tests.integration.amqp.connect;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;
import java.lang.invoke.MethodHandles;
import java.net.URI;
import java.util.HashSet;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.activemq.artemis.api.core.QueueConfiguration;
import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.api.core.management.SimpleManagement;
import org.apache.activemq.artemis.core.config.amqpBrokerConnectivity.AMQPBrokerConnectConfiguration;
import org.apache.activemq.artemis.core.config.amqpBrokerConnectivity.AMQPBrokerConnectionAddressType;
import org.apache.activemq.artemis.core.config.amqpBrokerConnectivity.AMQPMirrorBrokerConnectionElement;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.MessageReference;
import org.apache.activemq.artemis.core.server.Queue;
import org.apache.activemq.artemis.core.server.impl.AckReason;
import org.apache.activemq.artemis.core.server.impl.AddressInfo;
import org.apache.activemq.artemis.logs.AssertionLoggerHandler;
import org.apache.activemq.artemis.protocol.amqp.connect.mirror.AMQPMirrorControllerSource;
import org.apache.activemq.artemis.protocol.amqp.connect.mirror.AMQPMirrorMessageFactory;
import org.apache.activemq.artemis.tests.integration.amqp.AmqpClientTestSupport;
import org.apache.activemq.artemis.tests.util.CFUtil;
import org.apache.activemq.artemis.tests.util.RandomUtil;
import org.apache.activemq.artemis.utils.UUIDGenerator;
import org.apache.activemq.artemis.utils.Wait;
import org.apache.activemq.artemis.utils.collections.LinkedListIterator;
import org.apache.activemq.transport.amqp.client.AmqpClient;
import org.apache.activemq.transport.amqp.client.AmqpConnection;
import org.apache.activemq.transport.amqp.client.AmqpMessage;
import org.apache.activemq.transport.amqp.client.AmqpReceiver;
import org.apache.activemq.transport.amqp.client.AmqpSender;
import org.apache.activemq.transport.amqp.client.AmqpSession;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AMQPReplicaTest extends AmqpClientTestSupport {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
protected static final int AMQP_PORT_2 = 5673;
protected static final int AMQP_PORT_3 = 5674;
public static final int TIME_BEFORE_RESTART = 1000;
ActiveMQServer server_2;
private AssertionLoggerHandler loggerHandler;
@Before
public void startLogging() {
loggerHandler = new AssertionLoggerHandler();
}
@After
public void stopLogging() throws Exception {
try {
Assert.assertFalse(loggerHandler.findText("AMQ222214"));
} finally {
loggerHandler.close();
}
}
@Override
protected String getConfiguredProtocols() {
return "AMQP,OPENWIRE,CORE";
}
@Override
protected ActiveMQServer createServer() throws Exception {
return createServer(AMQP_PORT, false);
}
@Test
public void testReplicaCatchupOnQueueCreates() throws Exception {
server.setIdentity("Server1");
server.stop();
server_2 = createServer(AMQP_PORT_2, false);
AMQPBrokerConnectConfiguration amqpConnection = new AMQPBrokerConnectConfiguration("test", "tcp://localhost:" + AMQP_PORT);
amqpConnection.addElement(new AMQPMirrorBrokerConnectionElement());
server_2.getConfiguration().addAMQPConnection(amqpConnection);
server_2.start();
server_2.addAddressInfo(new AddressInfo("sometest").setAutoCreated(false));
server_2.createQueue(new QueueConfiguration("sometest").setDurable(true));
Wait.assertTrue(() -> server_2.locateQueue("sometest") != null);
server_2.stop();
server.start();
Assert.assertTrue(server.locateQueue("sometest") == null);
Wait.assertTrue(server::isActive);
server_2.start();
// if this does not succeed the catch up did not arrive at the other server
Wait.assertTrue(() -> server.locateQueue("sometest") != null);
server_2.stop();
server.stop();
}
@Test
public void testNotFoundRetries() throws Exception {
server.setIdentity("Server1");
server.start();
server_2 = createServer(AMQP_PORT_2, false);
AMQPBrokerConnectConfiguration amqpConnection = new AMQPBrokerConnectConfiguration("test", "tcp://localhost:" + AMQP_PORT);
amqpConnection.addElement(new AMQPMirrorBrokerConnectionElement().setDurable(true));
server_2.getConfiguration().addAMQPConnection(amqpConnection);
server_2.start();
server_2.addAddressInfo(new AddressInfo("sometest").setAutoCreated(false));
server_2.createQueue(new QueueConfiguration("sometest").setDurable(true));
Wait.assertTrue(() -> server_2.locateQueue("sometest") != null);
Wait.waitFor(() -> server_2.locateQueue("$ACTIVEMQ_ARTEMIS_MIRROR_test") != null);
Queue mirrorQueue = server_2.locateQueue("$ACTIVEMQ_ARTEMIS_MIRROR_test");
Assert.assertNotNull(mirrorQueue);
try (AssertionLoggerHandler loggerHandler = new AssertionLoggerHandler()) {
// Adding some PostAck event that will never be found on the target for an expiry
org.apache.activemq.artemis.api.core.Message message = AMQPMirrorMessageFactory.createMessage(mirrorQueue.getAddress().toString(), SimpleString.toSimpleString("sometest"), SimpleString.toSimpleString("sometest"), AMQPMirrorControllerSource.POST_ACK, "0000", 3333L, AckReason.EXPIRED).setDurable(true);
message.setMessageID(server_2.getStorageManager().generateID());
server_2.getPostOffice().route(message, false);
// Adding some PostAck event that will never be found on the target for a regular ack
message = AMQPMirrorMessageFactory.createMessage(mirrorQueue.getAddress().toString(), SimpleString.toSimpleString("sometest"), SimpleString.toSimpleString("sometest"), AMQPMirrorControllerSource.POST_ACK, "0000", 3334L, AckReason.NORMAL).setDurable(true);
message.setMessageID(server_2.getStorageManager().generateID());
server_2.getPostOffice().route(message, false);
Wait.assertEquals(0L, mirrorQueue::getMessageCount, 2000, 100);
Assert.assertFalse(loggerHandler.findText("AMQ224041"));
}
server_2.stop();
server.stop();
}
@Test
public void testDeleteQueueWithRemoveFalse() throws Exception {
server.setIdentity("Server1");
server.start();
server_2 = createServer(AMQP_PORT_2, false);
AMQPBrokerConnectConfiguration amqpConnection = new AMQPBrokerConnectConfiguration("test", "tcp://localhost:" + AMQP_PORT);
amqpConnection.addElement(new AMQPMirrorBrokerConnectionElement().setQueueRemoval(false));
server_2.getConfiguration().addAMQPConnection(amqpConnection);
server_2.start();
SimpleString queueName = RandomUtil.randomSimpleString();
server_2.addAddressInfo(new AddressInfo(queueName).setAutoCreated(false));
server_2.createQueue(new QueueConfiguration(queueName).setDurable(true));
Wait.assertTrue(() -> server_2.locateQueue(queueName) != null);
Wait.assertTrue(() -> server.locateQueue(queueName) != null);
server_2.destroyQueue(queueName);
Wait.assertTrue(() -> server_2.locateQueue(queueName) == null);
Thread.sleep(100);
Assert.assertTrue("Queue was removed when it was configured to not remove it", server.locateQueue(queueName) != null);
server_2.stop();
server.stop();
}
@Test
public void testSendCreateQueue() throws Exception {
doSendCreateQueueTestImpl(true);
}
@Test
public void testDoNotSendCreateQueue() throws Exception {
doSendCreateQueueTestImpl(false);
}
private void doSendCreateQueueTestImpl(boolean sendCreate) throws Exception {
server.start();
final SimpleString ADDRESS_NAME = SimpleString.toSimpleString("address");
server_2 = createServer(AMQP_PORT_2, false);
AMQPBrokerConnectConfiguration amqpConnection = new AMQPBrokerConnectConfiguration("test", "tcp://localhost:" + AMQP_PORT);
AMQPMirrorBrokerConnectionElement mirror = new AMQPMirrorBrokerConnectionElement();
if (sendCreate) {
mirror.setQueueCreation(true);
mirror.setQueueRemoval(false);
} else {
mirror.setQueueCreation(false);
}
amqpConnection.addElement(mirror);
server_2.getConfiguration().addAMQPConnection(amqpConnection);
server_2.start();
Wait.assertTrue(server::isActive);
server_2.addAddressInfo(new AddressInfo(ADDRESS_NAME).addRoutingType(RoutingType.ANYCAST));
server_2.createQueue(new QueueConfiguration(ADDRESS_NAME).setDurable(true).setAddress(ADDRESS_NAME));
if (sendCreate) {
Wait.assertTrue(() -> server.locateQueue(ADDRESS_NAME) != null);
Wait.assertTrue(() -> server.getAddressInfo(ADDRESS_NAME) != null);
} else {
Thread.sleep(250); // things are asynchronous, I need to wait some time to make sure things are transferred over
Assert.assertTrue(server.locateQueue(ADDRESS_NAME) == null);
Assert.assertTrue(server.getAddressInfo(ADDRESS_NAME) == null);
}
server_2.stop();
server.stop();
}
@Test
public void testReplicaCatchupOnQueueCreatesAndDeletes() throws Exception {
server.start();
server.setIdentity("Server1");
server.addAddressInfo(new AddressInfo("sometest").setAutoCreated(false).addRoutingType(RoutingType.MULTICAST));
// This queue will disappear from the source, so it should go
server.createQueue(new QueueConfiguration("ToBeGone").setDurable(true).setRoutingType(RoutingType.MULTICAST));
server.stop();
server_2 = createServer(AMQP_PORT_2, false);
server_2.setIdentity("server_2");
AMQPBrokerConnectConfiguration amqpConnection = new AMQPBrokerConnectConfiguration("test", "tcp://localhost:" + AMQP_PORT);
amqpConnection.addElement(new AMQPMirrorBrokerConnectionElement());
server_2.getConfiguration().addAMQPConnection(amqpConnection);
server_2.start();
server_2.addAddressInfo(new AddressInfo("sometest").setAutoCreated(false).addRoutingType(RoutingType.MULTICAST));
server_2.createQueue(new QueueConfiguration("sometest").setDurable(true).setRoutingType(RoutingType.MULTICAST));
Wait.assertTrue(() -> server_2.locateQueue("sometest") != null);
server_2.stop();
server_2 = createServer(AMQP_PORT_2, false);
server_2.setIdentity("server_2");
amqpConnection = new AMQPBrokerConnectConfiguration("test", "tcp://localhost:" + AMQP_PORT);
amqpConnection.addElement(new AMQPMirrorBrokerConnectionElement());
server_2.getConfiguration().addAMQPConnection(amqpConnection);
server.start();
Assert.assertTrue(server.locateQueue("sometest") == null);
Assert.assertTrue(server.locateQueue("ToBeGone") != null);
Wait.assertTrue(server::isActive);
server_2.start();
// if this does not succeed the catch up did not arrive at the other server
Wait.assertTrue(() -> server.locateQueue("sometest") != null);
server_2.stop();
server.stop();
}
@Test
public void testReplicaWithDurable() throws Exception {
server.start();
server.setIdentity("Server1");
server.addAddressInfo(new AddressInfo("sometest").setAutoCreated(false).addRoutingType(RoutingType.MULTICAST));
server.stop();
server_2 = createServer(AMQP_PORT_2, false);
server_2.setIdentity("server_2");
AMQPBrokerConnectConfiguration amqpConnection = new AMQPBrokerConnectConfiguration("test", "tcp://localhost:" + AMQP_PORT);
amqpConnection.addElement(new AMQPMirrorBrokerConnectionElement());
server_2.getConfiguration().addAMQPConnection(amqpConnection);
server_2.start();
server_2.addAddressInfo(new AddressInfo("sometest").setAutoCreated(false).addRoutingType(RoutingType.MULTICAST));
server_2.createQueue(new QueueConfiguration("sometest").setDurable(true).setRoutingType(RoutingType.MULTICAST));
Wait.assertTrue(() -> server_2.locateQueue("sometest") != null);
server_2.stop();
server_2 = createServer(AMQP_PORT_2, false);
server_2.setIdentity("server_2");
amqpConnection = new AMQPBrokerConnectConfiguration("test", "tcp://localhost:" + AMQP_PORT);
amqpConnection.addElement(new AMQPMirrorBrokerConnectionElement());
server_2.getConfiguration().addAMQPConnection(amqpConnection);
server.start();
Assert.assertTrue(server.locateQueue("sometest") == null);
Wait.assertTrue(server::isActive);
server_2.start();
// if this does not succeed the catch up did not arrive at the other server
Wait.assertTrue(() -> server.locateQueue("sometest") != null);
server_2.stop();
server.stop();
}
@Test
public void testReplicaLargeMessages() throws Exception {
replicaTest(true, true, false, false, false, false, false);
}
@Test
public void testReplicaLargeMessagesPagingEverywhere() throws Exception {
replicaTest(true, true, true, true, false, false, false);
}
@Test
public void testReplica() throws Exception {
replicaTest(false, true, false, false, false, false, false);
}
@Test
public void testReplicaRestartBrokerConnection() throws Exception {
replicaTest(false, true, false, false, false, false, true);
}
@Test
public void testReplicaRestart() throws Exception {
replicaTest(false, true, false, false, false, true, false);
}
@Test
public void testReplicaDeferredStart() throws Exception {
replicaTest(false, true, false, false, true, false, false);
}
@Test
public void testReplicaCopyOnly() throws Exception {
replicaTest(false, false, false, false, false, false, false);
}
@Test
public void testReplicaPagedTarget() throws Exception {
replicaTest(false, true, true, false, false, false, false);
}
@Test
public void testReplicaPagingEverywhere() throws Exception {
replicaTest(false, true, true, true, false, false, false);
}
private String getText(boolean large, int i) {
if (!large) {
return "Text " + i;
} else {
StringBuffer buffer = new StringBuffer();
while (buffer.length() < 110 * 1024) {
buffer.append("Text " + i + " ");
}
return buffer.toString();
}
}
/**
* This test is validating that annotations sent to the original broker are not translated to the receiving side.
* Also annotations could eventually damage the body if the broker did not take that into consideration.
* So, this test is sending delivery annotations on messages.
* @throws Exception
*/
@Test
public void testLargeMessagesWithDeliveryAnnotations() throws Exception {
server.setIdentity("targetServer");
server.start();
server_2 = createServer(AMQP_PORT_2, false);
server_2.setIdentity("server_2");
AMQPBrokerConnectConfiguration amqpConnection = new AMQPBrokerConnectConfiguration("test", "tcp://localhost:" + AMQP_PORT).setReconnectAttempts(-1).setRetryInterval(100);
AMQPMirrorBrokerConnectionElement replica = new AMQPMirrorBrokerConnectionElement().setMessageAcknowledgements(true);
amqpConnection.addElement(replica);
server_2.getConfiguration().addAMQPConnection(amqpConnection);
int NUMBER_OF_MESSAGES = 20;
server_2.start();
Wait.assertTrue(server_2::isStarted);
// We create the address to avoid auto delete on the queue
server_2.addAddressInfo(new AddressInfo(getQueueName()).addRoutingType(RoutingType.ANYCAST).setAutoCreated(false));
server_2.createQueue(new QueueConfiguration(getQueueName()).setRoutingType(RoutingType.ANYCAST).setAddress(getQueueName()).setAutoCreated(false));
Assert.assertFalse(loggerHandler.findText("AMQ222214"));
// Get the Queue View early to avoid racing the delivery.
final Queue queueView = locateQueue(server_2, getQueueName());
final Queue queueViewReplica = locateQueue(server_2, getQueueName());
{ // sender
AmqpClient client = new AmqpClient(new URI("tcp://localhost:" + AMQP_PORT_2), null, null);
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
AmqpSender sender = session.createSender(getQueueName());
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
AmqpMessage message = new AmqpMessage();
message.setDeliveryAnnotation("gone", "test");
message.setText(getText(true, i));
sender.send(message);
}
sender.close();
connection.close();
}
Wait.assertEquals(NUMBER_OF_MESSAGES, queueView::getMessageCount);
Wait.assertEquals(NUMBER_OF_MESSAGES, queueViewReplica::getMessageCount);
{ // receiver on replica
AmqpClient client = new AmqpClient(new URI("tcp://localhost:" + AMQP_PORT), null, null);
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
// Now try and get the message
AmqpReceiver receiver = session.createReceiver(getQueueName());
receiver.flow(NUMBER_OF_MESSAGES + 1);
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
AmqpMessage received = receiver.receive(5, TimeUnit.SECONDS);
assertNotNull(received);
Assert.assertEquals(getText(true, i), received.getText());
Assert.assertNull(received.getDeliveryAnnotation("gone"));
}
Assert.assertNull(receiver.receiveNoWait());
connection.close();
}
}
/** This is setting delivery annotations and sending messages with no address.
* The broker should know how to deal with the annotations and no address on the message. */
@Test
public void testNoAddressWithAnnotations() throws Exception {
server.setIdentity("targetServer");
server.start();
server_2 = createServer(AMQP_PORT_2, false);
server_2.setIdentity("server_2");
AMQPBrokerConnectConfiguration amqpConnection = new AMQPBrokerConnectConfiguration("test", "tcp://localhost:" + AMQP_PORT).setReconnectAttempts(-1).setRetryInterval(100);
AMQPMirrorBrokerConnectionElement replica = new AMQPMirrorBrokerConnectionElement().setMessageAcknowledgements(true);
amqpConnection.addElement(replica);
server_2.getConfiguration().addAMQPConnection(amqpConnection);
int NUMBER_OF_MESSAGES = 20;
server_2.start();
Wait.assertTrue(server_2::isStarted);
// We create the address to avoid auto delete on the queue
server_2.addAddressInfo(new AddressInfo(getQueueName()).addRoutingType(RoutingType.ANYCAST).setAutoCreated(false));
server_2.createQueue(new QueueConfiguration(getQueueName()).setRoutingType(RoutingType.ANYCAST).setAddress(getQueueName()).setAutoCreated(false));
Assert.assertFalse(loggerHandler.findText("AMQ222214"));
{ // sender
AmqpClient client = new AmqpClient(new URI("tcp://localhost:" + AMQP_PORT_2), null, null);
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
AmqpSender sender = session.createSender(getQueueName());
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
AmqpMessage message = new AmqpMessage();
message.setDeliveryAnnotation("gone", "test");
message.setText(getText(false, i));
sender.send(message);
}
sender.close();
connection.close();
}
{ // receiver on replica
AmqpClient client = new AmqpClient(new URI("tcp://localhost:" + AMQP_PORT), null, null);
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
// Now try and get the message
AmqpReceiver receiver = session.createReceiver(getQueueName());
receiver.flow(NUMBER_OF_MESSAGES + 1);
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
AmqpMessage received = receiver.receive(5, TimeUnit.SECONDS);
assertNotNull(received);
Assert.assertEquals(getText(false, i), received.getText());
Assert.assertNull(received.getDeliveryAnnotation("gone"));
}
Assert.assertNull(receiver.receiveNoWait());
connection.close();
}
}
@Test
public void testAddressFilter() throws Exception {
final String REPLICATED = "replicated";
final String NON_REPLICATED = "nonReplicated";
final String ADDRESS_FILTER = REPLICATED + "," + "!" + NON_REPLICATED;
final String MSG = "msg";
server.start();
server_2 = createServer(AMQP_PORT_2, false);
server_2.setIdentity("server_2");
server_2.getConfiguration().setName("server_2");
AMQPBrokerConnectConfiguration amqpConnection = new AMQPBrokerConnectConfiguration("mirror-source", "tcp://localhost:" + AMQP_PORT).setReconnectAttempts(-1).setRetryInterval(100);
AMQPMirrorBrokerConnectionElement replica = new AMQPMirrorBrokerConnectionElement().setDurable(true).setAddressFilter(ADDRESS_FILTER);
amqpConnection.addElement(replica);
server_2.getConfiguration().addAMQPConnection(amqpConnection);
server_2.start();
try (Connection connection = CFUtil.createConnectionFactory("AMQP", "tcp://localhost:" + AMQP_PORT_2).createConnection()) {
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Send to non replicated address
try (MessageProducer producer = session.createProducer(session.createQueue(NON_REPLICATED))) {
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
for (int i = 0; i < 2; i++) {
producer.send(session.createTextMessage("never receive"));
}
}
// Check nothing was added to SnF queue
Assert.assertEquals(0, server_2.locateQueue(replica.getMirrorSNF()).getMessagesAdded());
// Send to replicated address
try (MessageProducer producer = session.createProducer(session.createQueue(REPLICATED))) {
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
for (int i = 0; i < 2; i++) {
producer.send(session.createTextMessage(MSG));
}
}
// Check some messages were sent to SnF queue
Assert.assertTrue(server_2.locateQueue(replica.getMirrorSNF()).getMessagesAdded() > 0);
}
SimpleManagement simpleManagement = new SimpleManagement("tcp://localhost:" + AMQP_PORT_2, null, null);
Wait.assertEquals(0, () -> simpleManagement.getMessageCountOnQueue("$ACTIVEMQ_ARTEMIS_MIRROR_mirror-source"), 5000);
try (Connection connection = CFUtil.createConnectionFactory("AMQP", "tcp://localhost:" + AMQP_PORT).createConnection()) {
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
try (MessageConsumer consumer = session.createConsumer(session.createQueue(REPLICATED))) {
Message message = consumer.receive(3000);
Assert.assertNotNull(message);
Assert.assertEquals(MSG, message.getBody(String.class));
}
try (MessageConsumer consumer = session.createConsumer(session.createQueue(NON_REPLICATED))) {
Assert.assertNull(consumer.receiveNoWait());
}
}
}
@Test
public void testRouteSurviving() throws Exception {
testRouteSurvivor(false);
}
@Test
public void testRouteSurvivingStop() throws Exception {
testRouteSurvivor(true);
}
private void testRouteSurvivor(boolean server1Stopped) throws Exception {
if (!server1Stopped) {
server.start();
}
server_2 = createServer(AMQP_PORT_2, false);
server_2.setIdentity("server_2");
server_2.getConfiguration().setName("thisone");
AMQPBrokerConnectConfiguration amqpConnection = new AMQPBrokerConnectConfiguration("OtherSide", "tcp://localhost:" + AMQP_PORT).setReconnectAttempts(-1).setRetryInterval(100);
AMQPMirrorBrokerConnectionElement replica = new AMQPMirrorBrokerConnectionElement().setDurable(true);
amqpConnection.addElement(replica);
server_2.getConfiguration().addAMQPConnection(amqpConnection);
server_2.start();
// We create the address to avoid auto delete on the queue
server_2.addAddressInfo(new AddressInfo(getQueueName()).addRoutingType(RoutingType.ANYCAST).setAutoCreated(false));
server_2.createQueue(new QueueConfiguration(getQueueName()).setRoutingType(RoutingType.ANYCAST).setAddress(getQueueName()).setAutoCreated(false));
int NUMBER_OF_MESSAGES = 200;
ConnectionFactory factory = CFUtil.createConnectionFactory("AMQP", "tcp://localhost:" + AMQP_PORT_2);
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(session.createQueue(getQueueName()));
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
producer.send(session.createTextMessage("i=" + i));
}
connection.close();
{
if (!server1Stopped) {
Wait.assertTrue(() -> server.locateQueue(getQueueName()) != null);
Queue queueServer1 = server.locateQueue(getQueueName());
Wait.assertEquals(NUMBER_OF_MESSAGES, queueServer1::getMessageCount);
}
Wait.assertTrue(() -> server_2.locateQueue(getQueueName()) != null);
Queue queueServer2 = server_2.locateQueue(getQueueName());
Wait.assertEquals(NUMBER_OF_MESSAGES, queueServer2::getMessageCount);
}
if (!server1Stopped) {
server.stop();
}
server_2.stop();
server.start();
server_2.start();
Wait.assertTrue(() -> server.locateQueue(getQueueName()) != null);
Wait.assertTrue(() -> server_2.locateQueue(getQueueName()) != null);
Queue queueServer1 = server.locateQueue(getQueueName());
Queue queueServer2 = server_2.locateQueue(getQueueName());
Wait.assertEquals(NUMBER_OF_MESSAGES, queueServer1::getMessageCount);
Wait.assertEquals(NUMBER_OF_MESSAGES, queueServer2::getMessageCount);
}
private void replicaTest(boolean largeMessage,
boolean acks,
boolean pagingTarget,
boolean pagingSource,
boolean deferredStart,
boolean restartAndDisconnect,
boolean restartBrokerConnection) throws Exception {
String brokerConnectionName = "brokerConnectionName:" + UUIDGenerator.getInstance().generateStringUUID();
server.setIdentity("targetServer");
if (deferredStart) {
server.stop();
} else {
server.start();
}
server_2 = createServer(AMQP_PORT_2, false);
server_2.setIdentity("server_2");
server_2.getConfiguration().setName("thisone");
AMQPBrokerConnectConfiguration amqpConnection = new AMQPBrokerConnectConfiguration(brokerConnectionName, "tcp://localhost:" + AMQP_PORT).setReconnectAttempts(-1).setRetryInterval(100);
AMQPMirrorBrokerConnectionElement replica = new AMQPMirrorBrokerConnectionElement().setMessageAcknowledgements(acks).setDurable(true);
replica.setName("theReplica");
amqpConnection.addElement(replica);
server_2.getConfiguration().addAMQPConnection(amqpConnection);
server_2.getConfiguration().setName("server_2");
int NUMBER_OF_MESSAGES = 200;
server_2.start();
Wait.assertTrue(server_2::isStarted);
// We create the address to avoid auto delete on the queue
server_2.addAddressInfo(new AddressInfo(getQueueName()).addRoutingType(RoutingType.ANYCAST).setAutoCreated(false));
server_2.createQueue(new QueueConfiguration(getQueueName()).setRoutingType(RoutingType.ANYCAST).setAddress(getQueueName()).setAutoCreated(false));
ConnectionFactory factory = CFUtil.createConnectionFactory("AMQP", "tcp://localhost:" + AMQP_PORT_2);
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(session.createQueue(getQueueName()));
if (!deferredStart) {
Queue queueOnServer1 = locateQueue(server, getQueueName());
if (pagingTarget) {
queueOnServer1.getPagingStore().startPaging();
}
}
if (pagingSource) {
Queue queueOnServer2 = server_2.locateQueue(getQueueName());
queueOnServer2.getPagingStore().startPaging();
}
Assert.assertFalse(loggerHandler.findText("AMQ222214"));
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
Message message = session.createTextMessage(getText(largeMessage, i));
message.setIntProperty("i", i);
producer.send(message);
}
Assert.assertFalse(loggerHandler.findText("AMQ222214"));
Queue queueOnServer1;
if (deferredStart) {
Thread.sleep(TIME_BEFORE_RESTART);
server.start();
Wait.assertTrue(server::isActive);
queueOnServer1 = locateQueue(server, getQueueName());
if (pagingTarget) {
queueOnServer1.getPagingStore().startPaging();
}
} else {
queueOnServer1 = locateQueue(server, getQueueName());
}
Queue snfreplica = server_2.locateQueue(replica.getMirrorSNF());
Assert.assertNotNull(snfreplica);
Wait.assertEquals(0, snfreplica::getMessageCount);
Wait.assertEquals(NUMBER_OF_MESSAGES, queueOnServer1::getMessageCount, 2000);
Queue queueOnServer2 = locateQueue(server_2, getQueueName());
Wait.assertEquals(NUMBER_OF_MESSAGES, queueOnServer1::getMessageCount);
Wait.assertEquals(NUMBER_OF_MESSAGES, queueOnServer2::getMessageCount);
if (restartBrokerConnection) {
// stop and start the broker connection, making sure we wouldn't duplicate the mirror
server_2.stopBrokerConnection(brokerConnectionName);
Thread.sleep(1000);
server_2.startBrokerConnection(brokerConnectionName);
}
Assert.assertSame(snfreplica, server_2.locateQueue(replica.getMirrorSNF()));
if (pagingTarget) {
assertTrue(queueOnServer1.getPagingStore().isPaging());
}
if (acks) {
consumeMessages(largeMessage, 0, NUMBER_OF_MESSAGES / 2 - 1, AMQP_PORT_2, false);
// Replica is async, so we need to wait acks to arrive before we finish consuming there
Wait.assertEquals(0, snfreplica::getMessageCount);
Wait.assertEquals(NUMBER_OF_MESSAGES / 2, queueOnServer1::getMessageCount);
// we consume on replica, as half the messages were acked
consumeMessages(largeMessage, NUMBER_OF_MESSAGES / 2, NUMBER_OF_MESSAGES - 1, AMQP_PORT, true); // We consume on both servers as this is currently replicated
Wait.assertEquals(0, snfreplica::getMessageCount);
consumeMessages(largeMessage, NUMBER_OF_MESSAGES / 2, NUMBER_OF_MESSAGES - 1, AMQP_PORT_2, false);
Wait.assertEquals(0, snfreplica::getMessageCount);
if (largeMessage) {
validateNoFilesOnLargeDir(server.getConfiguration().getLargeMessagesDirectory(), 0);
}
} else {
consumeMessages(largeMessage, 0, NUMBER_OF_MESSAGES - 1, AMQP_PORT_2, true);
Wait.assertEquals(0, snfreplica::getMessageCount);
consumeMessages(largeMessage, 0, NUMBER_OF_MESSAGES - 1, AMQP_PORT, true);
Wait.assertEquals(0, snfreplica::getMessageCount);
if (largeMessage) {
validateNoFilesOnLargeDir(server.getConfiguration().getLargeMessagesDirectory(), 0);
validateNoFilesOnLargeDir(server_2.getConfiguration().getLargeMessagesDirectory(), 0); // we kept half of the messages
}
}
if (restartAndDisconnect) {
server.stop();
Thread.sleep(TIME_BEFORE_RESTART);
server.start();
Wait.assertTrue(server::isActive);
consumeMessages(largeMessage, 0, -1, AMQP_PORT_2, true);
consumeMessages(largeMessage, 0, -1, AMQP_PORT, true);
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
Message message = session.createTextMessage(getText(largeMessage, i));
message.setIntProperty("i", i);
producer.send(message);
}
consumeMessages(largeMessage, 0, NUMBER_OF_MESSAGES - 1, AMQP_PORT, true);
consumeMessages(largeMessage, 0, NUMBER_OF_MESSAGES - 1, AMQP_PORT_2, true);
}
}
@Test
public void testDualStandardRestartBrokerConnection() throws Exception {
dualReplica(false, false, false, true);
}
@Test
public void testDualStandard() throws Exception {
dualReplica(false, false, false, false);
}
@Test
public void testDualRegularPagedTargets() throws Exception {
dualReplica(false, false, true, false);
}
@Test
public void testDualRegularPagedEverything() throws Exception {
dualReplica(false, true, true, false);
}
@Test
public void testDualRegularLarge() throws Exception {
dualReplica(true, false, false, false);
}
public Queue locateQueue(ActiveMQServer server, String queueName) throws Exception {
Assert.assertNotNull(queueName);
Assert.assertNotNull(server);
Wait.waitFor(() -> server.locateQueue(queueName) != null);
return server.locateQueue(queueName);
}
private void dualReplica(boolean largeMessage, boolean pagingSource, boolean pagingTarget, boolean restartBC) throws Exception {
server.setIdentity("server_1");
server.start();
ActiveMQServer server_3 = createServer(AMQP_PORT_3, false);
server_3.setIdentity("server_3");
server_3.start();
Wait.assertTrue(server_3::isStarted);
ConnectionFactory factory_3 = CFUtil.createConnectionFactory("amqp", "tcp://localhost:" + AMQP_PORT_3);
factory_3.createConnection().close();
server_2 = createServer(AMQP_PORT_2, false);
String brokerConnectionOne = "brokerConnection1:" + UUIDGenerator.getInstance().generateStringUUID();
String brokerConnectionTwo = "brokerConnection2:" + UUIDGenerator.getInstance().generateStringUUID();
AMQPBrokerConnectConfiguration amqpConnection1 = new AMQPBrokerConnectConfiguration(brokerConnectionOne, "tcp://localhost:" + AMQP_PORT);
AMQPMirrorBrokerConnectionElement replica1 = new AMQPMirrorBrokerConnectionElement().setType(AMQPBrokerConnectionAddressType.MIRROR);
amqpConnection1.addElement(replica1);
server_2.getConfiguration().addAMQPConnection(amqpConnection1);
AMQPBrokerConnectConfiguration amqpConnection3 = new AMQPBrokerConnectConfiguration(brokerConnectionTwo, "tcp://localhost:" + AMQP_PORT_3);
AMQPMirrorBrokerConnectionElement replica2 = new AMQPMirrorBrokerConnectionElement().setType(AMQPBrokerConnectionAddressType.MIRROR);
amqpConnection3.addElement(replica2);
server_2.getConfiguration().addAMQPConnection(amqpConnection3);
int NUMBER_OF_MESSAGES = 200;
server_2.start();
Wait.assertTrue(server_2::isStarted);
ConnectionFactory factory = CFUtil.createConnectionFactory("AMQP", "tcp://localhost:" + AMQP_PORT_2);
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(session.createQueue(getQueueName()));
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
Queue queue_server_2 = locateQueue(server_2, getQueueName());
Queue queue_server_1 = locateQueue(server, getQueueName());
Queue queue_server_3 = locateQueue(server_3, getQueueName());
if (pagingSource) {
queue_server_2.getPagingStore().startPaging();
}
if (pagingTarget) {
queue_server_1.getPagingStore().startPaging();
queue_server_3.getPagingStore().startPaging();
}
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
Message message = session.createTextMessage(getText(largeMessage, i));
message.setIntProperty("i", i);
producer.send(message);
if (i == NUMBER_OF_MESSAGES / 2) {
if (restartBC) {
// half we restart it
Wait.assertEquals(NUMBER_OF_MESSAGES / 2 + 1, queue_server_2::getMessageCount);
Wait.assertEquals(NUMBER_OF_MESSAGES / 2 + 1, queue_server_3::getMessageCount);
Wait.assertEquals(NUMBER_OF_MESSAGES / 2 + 1, queue_server_1::getMessageCount);
server_2.stopBrokerConnection(brokerConnectionOne);
server_2.stopBrokerConnection(brokerConnectionTwo);
Thread.sleep(1000);
server_2.startBrokerConnection(brokerConnectionOne);
server_2.startBrokerConnection(brokerConnectionTwo);
}
}
}
Wait.assertEquals(NUMBER_OF_MESSAGES, queue_server_2::getMessageCount);
Wait.assertEquals(NUMBER_OF_MESSAGES, queue_server_3::getMessageCount);
Wait.assertEquals(NUMBER_OF_MESSAGES, queue_server_1::getMessageCount);
Queue replica1Queue = server_2.locateQueue(replica1.getMirrorSNF());
Queue replica2Queue = server_2.locateQueue(replica2.getMirrorSNF());
Wait.assertEquals(0L, replica2Queue.getPagingStore()::getAddressSize, 1000, 100);
Wait.assertEquals(0L, replica1Queue.getPagingStore()::getAddressSize, 1000, 100);
if (pagingTarget) {
Assert.assertTrue(queue_server_1.getPagingStore().isPaging());
Assert.assertTrue(queue_server_3.getPagingStore().isPaging());
}
if (pagingSource) {
Assert.assertTrue(queue_server_2.getPagingStore().isPaging());
}
consumeMessages(largeMessage, 0, NUMBER_OF_MESSAGES / 2 - 1, AMQP_PORT_2, false);
Wait.assertEquals(NUMBER_OF_MESSAGES / 2, queue_server_1::getMessageCount);
Wait.assertEquals(NUMBER_OF_MESSAGES / 2, queue_server_2::getMessageCount);
Wait.assertEquals(NUMBER_OF_MESSAGES / 2, queue_server_3::getMessageCount);
// Replica is async, so we need to wait acks to arrive before we finish consuming there
Wait.assertEquals(NUMBER_OF_MESSAGES / 2, queue_server_1::getMessageCount);
consumeMessages(largeMessage, NUMBER_OF_MESSAGES / 2, NUMBER_OF_MESSAGES - 1, AMQP_PORT, true); // We consume on both servers as this is currently replicated
consumeMessages(largeMessage, NUMBER_OF_MESSAGES / 2, NUMBER_OF_MESSAGES - 1, AMQP_PORT_3, true); // We consume on both servers as this is currently replicated
consumeMessages(largeMessage, NUMBER_OF_MESSAGES / 2, NUMBER_OF_MESSAGES - 1, AMQP_PORT_2, true); // We consume on both servers as this is currently replicated
validateNoFilesOnLargeDir(server.getConfiguration().getLargeMessagesDirectory(), 0);
validateNoFilesOnLargeDir(server_3.getConfiguration().getLargeMessagesDirectory(), 0);
validateNoFilesOnLargeDir(server_2.getConfiguration().getLargeMessagesDirectory(), 0);
}
@Test
public void testWithTXLargeMessage() throws Exception {
testWithTX(true);
}
@Test
public void testWithTX() throws Exception {
testWithTX(false);
}
private void testWithTX(boolean largeMessage) throws Exception {
server.setIdentity("server_1");
server.start();
ActiveMQServer server_3 = createServer(AMQP_PORT_3, false);
server_3.setIdentity("server_3");
server_3.start();
Wait.assertTrue(server_3::isStarted);
ConnectionFactory factory_3 = CFUtil.createConnectionFactory("amqp", "tcp://localhost:" + AMQP_PORT_3);
factory_3.createConnection().close();
server_2 = createServer(AMQP_PORT_2, false);
String brokerConnectionOne = "brokerConnection1:" + UUIDGenerator.getInstance().generateStringUUID();
String brokerConnectionTwo = "brokerConnection2:" + UUIDGenerator.getInstance().generateStringUUID();
AMQPBrokerConnectConfiguration amqpConnection1 = new AMQPBrokerConnectConfiguration(brokerConnectionOne, "tcp://localhost:" + AMQP_PORT);