forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReplicatorSubscriptionTest.java
1121 lines (981 loc) · 52.3 KB
/
ReplicatorSubscriptionTest.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.pulsar.broker.service;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import com.google.common.collect.Sets;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import lombok.Cleanup;
import org.apache.bookkeeper.mledger.Position;
import org.apache.pulsar.broker.BrokerTestUtil;
import org.apache.pulsar.broker.service.persistent.PersistentTopic;
import org.apache.pulsar.broker.service.persistent.ReplicatedSubscriptionsController;
import org.apache.pulsar.client.admin.LongRunningProcessStatus;
import org.apache.pulsar.client.admin.PulsarAdminException;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.MessageRoutingMode;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.Reader;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.client.api.SubscriptionInitialPosition;
import org.apache.pulsar.client.api.SubscriptionType;
import org.apache.pulsar.common.policies.data.PartitionedTopicStats;
import org.apache.pulsar.common.policies.data.PersistentTopicInternalStats;
import org.apache.pulsar.common.policies.data.TenantInfoImpl;
import org.apache.pulsar.common.policies.data.TopicStats;
import org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap;
import org.awaitility.Awaitility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* Tests replicated subscriptions (PIP-33)
*/
@Test(groups = "broker")
public class ReplicatorSubscriptionTest extends ReplicatorTestBase {
private static final Logger log = LoggerFactory.getLogger(ReplicatorSubscriptionTest.class);
@Override
@BeforeClass(timeOut = 300000)
public void setup() throws Exception {
super.setup();
}
@Override
@AfterClass(alwaysRun = true, timeOut = 300000)
public void cleanup() throws Exception {
super.cleanup();
}
/**
* Tests replicated subscriptions across two regions
*/
@Test
public void testReplicatedSubscriptionAcrossTwoRegions() throws Exception {
String namespace = BrokerTestUtil.newUniqueName("pulsar/replicatedsubscription");
String topicName = "persistent://" + namespace + "/mytopic";
String subscriptionName = "cluster-subscription";
// Subscription replication produces duplicates, https://github.com/apache/pulsar/issues/10054
// TODO: duplications shouldn't be allowed, change to "false" when fixing the issue
boolean allowDuplicates = true;
// this setting can be used to manually run the test with subscription replication disabled
// it shows that subscription replication has no impact in behavior for this test case
boolean replicateSubscriptionState = true;
admin1.namespaces().createNamespace(namespace);
admin1.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("r1", "r2"));
@Cleanup
PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString())
.statsInterval(0, TimeUnit.SECONDS)
.build();
// create subscription in r1
createReplicatedSubscription(client1, topicName, subscriptionName, replicateSubscriptionState);
@Cleanup
PulsarClient client2 = PulsarClient.builder().serviceUrl(url2.toString())
.statsInterval(0, TimeUnit.SECONDS)
.build();
// create subscription in r2
createReplicatedSubscription(client2, topicName, subscriptionName, replicateSubscriptionState);
Set<String> sentMessages = new LinkedHashSet<>();
// send messages in r1
{
@Cleanup
Producer<byte[]> producer = client1.newProducer().topic(topicName)
.enableBatching(false)
.messageRoutingMode(MessageRoutingMode.SinglePartition)
.create();
int numMessages = 6;
for (int i = 0; i < numMessages; i++) {
String body = "message" + i;
producer.send(body.getBytes(StandardCharsets.UTF_8));
sentMessages.add(body);
}
producer.close();
}
Set<String> receivedMessages = new LinkedHashSet<>();
// consume 3 messages in r1
try (Consumer<byte[]> consumer1 = client1.newConsumer()
.topic(topicName)
.subscriptionName(subscriptionName)
.replicateSubscriptionState(replicateSubscriptionState)
.subscribe()) {
readMessages(consumer1, receivedMessages, 3, allowDuplicates);
}
// wait for subscription to be replicated
Thread.sleep(2 * config1.getReplicatedSubscriptionsSnapshotFrequencyMillis());
// consume remaining messages in r2
try (Consumer<byte[]> consumer2 = client2.newConsumer()
.topic(topicName)
.subscriptionName(subscriptionName)
.replicateSubscriptionState(replicateSubscriptionState)
.subscribe()) {
readMessages(consumer2, receivedMessages, -1, allowDuplicates);
}
// assert that all messages have been received
assertEquals(new ArrayList<>(sentMessages), new ArrayList<>(receivedMessages), "Sent and received " +
"messages don't match.");
}
/**
* Tests replicated subscriptions across two regions and can read successful.
*/
@Test
public void testReplicatedSubscriptionAcrossTwoRegionsGetLastMessage() throws Exception {
String namespace = BrokerTestUtil.newUniqueName("pulsar/replicatedsubscriptionlastmessage");
String topicName = "persistent://" + namespace + "/mytopic";
String subscriptionName = "cluster-subscription";
// this setting can be used to manually run the test with subscription replication disabled
// it shows that subscription replication has no impact in behavior for this test case
boolean replicateSubscriptionState = true;
admin1.namespaces().createNamespace(namespace);
admin1.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("r1", "r2"));
@Cleanup
PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString())
.statsInterval(0, TimeUnit.SECONDS)
.build();
// create subscription in r1
createReplicatedSubscription(client1, topicName, subscriptionName, replicateSubscriptionState);
@Cleanup
PulsarClient client2 = PulsarClient.builder().serviceUrl(url2.toString())
.statsInterval(0, TimeUnit.SECONDS)
.build();
// create subscription in r2
createReplicatedSubscription(client2, topicName, subscriptionName, replicateSubscriptionState);
Set<String> sentMessages = new LinkedHashSet<>();
// send messages in r1
@Cleanup
Producer<byte[]> producer = client1.newProducer().topic(topicName)
.enableBatching(false)
.messageRoutingMode(MessageRoutingMode.SinglePartition)
.create();
int numMessages = 6;
for (int i = 0; i < numMessages; i++) {
String body = "message" + i;
producer.send(body.getBytes(StandardCharsets.UTF_8));
sentMessages.add(body);
}
producer.close();
// consume 3 messages in r1
Set<String> receivedMessages = new LinkedHashSet<>();
try (Consumer<byte[]> consumer1 = client1.newConsumer()
.topic(topicName)
.subscriptionName(subscriptionName)
.replicateSubscriptionState(replicateSubscriptionState)
.subscribe()) {
readMessages(consumer1, receivedMessages, 3, false);
}
// wait for subscription to be replicated
Thread.sleep(2 * config1.getReplicatedSubscriptionsSnapshotFrequencyMillis());
// create a reader in r2
Reader<byte[]> reader = client2.newReader().topic(topicName)
.subscriptionName("new-sub")
.startMessageId(MessageId.earliest)
.create();
int readNum = 0;
while (reader.hasMessageAvailable()) {
Message<byte[]> message = reader.readNext(10, TimeUnit.SECONDS);
assertNotNull(message);
log.info("Receive message: " + new String(message.getValue()) + " msgId: " + message.getMessageId());
readNum++;
}
assertEquals(readNum, numMessages);
}
@Test
public void testReplicatedSubscribeAndSwitchToStandbyCluster() throws Exception {
final String namespace = BrokerTestUtil.newUniqueName("pulsar/ns_");
final String topicName = BrokerTestUtil.newUniqueName("persistent://" + namespace + "/tp_");
final String subscriptionName = "s1";
final boolean isReplicatedSubscription = true;
final int messagesCount = 20;
final LinkedHashSet<String> sentMessages = new LinkedHashSet<>();
final Set<String> receivedMessages = Collections.synchronizedSet(new LinkedHashSet<>());
admin1.namespaces().createNamespace(namespace);
admin1.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("r1", "r2"));
admin1.topics().createNonPartitionedTopic(topicName);
admin1.topics().createSubscription(topicName, subscriptionName, MessageId.earliest, isReplicatedSubscription);
final PersistentTopic topic1 =
(PersistentTopic) pulsar1.getBrokerService().getTopic(topicName, false).join().get();
// Send messages
// Wait for the topic created on the cluster2.
// Wait for the snapshot created.
final PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString()).build();
Producer<String> producer1 = client1.newProducer(Schema.STRING).topic(topicName).enableBatching(false).create();
Consumer<String> consumer1 = client1.newConsumer(Schema.STRING).topic(topicName)
.subscriptionName(subscriptionName).replicateSubscriptionState(isReplicatedSubscription).subscribe();
for (int i = 0; i < messagesCount / 2; i++) {
String msg = i + "";
producer1.send(msg);
sentMessages.add(msg);
}
Awaitility.await().untilAsserted(() -> {
ConcurrentOpenHashMap<String, ? extends Replicator> replicators = topic1.getReplicators();
assertTrue(replicators != null && replicators.size() == 1, "Replicator should started");
assertTrue(replicators.values().iterator().next().isConnected(), "Replicator should be connected");
assertTrue(topic1.getReplicatedSubscriptionController().get().getLastCompletedSnapshotId().isPresent(),
"One snapshot should be finished");
});
final PersistentTopic topic2 =
(PersistentTopic) pulsar2.getBrokerService().getTopic(topicName, false).join().get();
Awaitility.await().untilAsserted(() -> {
assertTrue(topic2.getReplicatedSubscriptionController().isPresent(),
"Replicated subscription controller should created");
});
for (int i = messagesCount / 2; i < messagesCount; i++) {
String msg = i + "";
producer1.send(msg);
sentMessages.add(msg);
}
// Consume half messages and wait the subscription created on the cluster2.
for (int i = 0; i < messagesCount / 2; i++){
Message<String> message = consumer1.receive(2, TimeUnit.SECONDS);
if (message == null) {
fail("Should not receive null.");
}
receivedMessages.add(message.getValue());
consumer1.acknowledge(message);
}
Awaitility.await().untilAsserted(() -> {
assertNotNull(topic2.getSubscriptions().get(subscriptionName), "Subscription should created");
});
// Switch client to cluster2.
// Since the cluster1 was not crash, all messages will be replicated to the cluster2.
consumer1.close();
final PulsarClient client2 = PulsarClient.builder().serviceUrl(url2.toString()).build();
final Consumer consumer2 = client2.newConsumer(Schema.AUTO_CONSUME()).topic(topicName)
.subscriptionName(subscriptionName).replicateSubscriptionState(isReplicatedSubscription).subscribe();
// Verify all messages will be consumed.
Awaitility.await().untilAsserted(() -> {
while (true) {
Message message = consumer2.receive(2, TimeUnit.SECONDS);
if (message != null) {
receivedMessages.add(message.getValue().toString());
consumer2.acknowledge(message);
} else {
break;
}
}
assertEquals(receivedMessages.size(), sentMessages.size());
});
consumer2.close();
producer1.close();
client1.close();
client2.close();
}
/**
* If there's no traffic, the snapshot creation should stop and then resume when traffic comes back
*/
@Test
public void testReplicationSnapshotStopWhenNoTraffic() throws Exception {
String namespace = BrokerTestUtil.newUniqueName("pulsar/replicatedsubscription");
String topicName = "persistent://" + namespace + "/mytopic";
String subscriptionName = "cluster-subscription";
admin1.namespaces().createNamespace(namespace);
admin1.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("r1", "r2"));
@Cleanup
PulsarClient client1 = PulsarClient.builder()
.serviceUrl(url1.toString())
.statsInterval(0, TimeUnit.SECONDS)
.build();
// create subscription in r1
createReplicatedSubscription(client1, topicName, subscriptionName, true);
// Validate that no snapshots are created before messages are published
Thread.sleep(2 * config1.getReplicatedSubscriptionsSnapshotFrequencyMillis());
PersistentTopic t1 = (PersistentTopic) pulsar1.getBrokerService()
.getTopic(topicName, false).get().get();
ReplicatedSubscriptionsController rsc1 = t1.getReplicatedSubscriptionController().get();
// no snapshot should have been created before any messages are published
assertTrue(rsc1.getLastCompletedSnapshotId().isEmpty());
@Cleanup
PulsarClient client2 = PulsarClient.builder()
.serviceUrl(url2.toString())
.statsInterval(0, TimeUnit.SECONDS)
.build();
Set<String> sentMessages = new LinkedHashSet<>();
// send messages in r1
{
@Cleanup
Producer<String> producer = client1.newProducer(Schema.STRING)
.topic(topicName)
.create();
for (int i = 0; i < 10; i++) {
producer.send("hello-" + i);
}
}
// Wait for last snapshots to be created
Thread.sleep(2 * config1.getReplicatedSubscriptionsSnapshotFrequencyMillis());
// In R1
Position p1 = t1.getLastPosition();
String snapshot1 = rsc1.getLastCompletedSnapshotId().get();
// In R2
PersistentTopic t2 = (PersistentTopic) pulsar1.getBrokerService()
.getTopic(topicName, false).get().get();
ReplicatedSubscriptionsController rsc2 = t2.getReplicatedSubscriptionController().get();
Position p2 = t2.getLastPosition();
String snapshot2 = rsc2.getLastCompletedSnapshotId().get();
// There shouldn't be anymore snapshots
Thread.sleep(2 * config1.getReplicatedSubscriptionsSnapshotFrequencyMillis());
assertEquals(t1.getLastPosition(), p1);
assertEquals(rsc1.getLastCompletedSnapshotId().get(), snapshot1);
assertEquals(t2.getLastPosition(), p2);
assertEquals(rsc2.getLastCompletedSnapshotId().get(), snapshot2);
@Cleanup
Producer<String> producer2 = client2.newProducer(Schema.STRING)
.topic(topicName)
.create();
for (int i = 0; i < 10; i++) {
producer2.send("hello-" + i);
}
Thread.sleep(2 * config1.getReplicatedSubscriptionsSnapshotFrequencyMillis());
// Now we should have one or more snapshots
assertNotEquals(t1.getLastPosition(), p1);
assertNotEquals(rsc1.getLastCompletedSnapshotId().get(), snapshot1);
assertNotEquals(t2.getLastPosition(), p2);
assertNotEquals(rsc2.getLastCompletedSnapshotId().get(), snapshot2);
}
@Test(timeOut = 30000)
public void testReplicatedSubscriptionRestApi1() throws Exception {
final String namespace = BrokerTestUtil.newUniqueName("pulsar/replicatedsubscription");
final String topicName = "persistent://" + namespace + "/topic-rest-api1";
final String subName = "sub";
// Subscription replication produces duplicates, https://github.com/apache/pulsar/issues/10054
// TODO: duplications shouldn't be allowed, change to "false" when fixing the issue
final boolean allowDuplicates = true;
admin1.namespaces().createNamespace(namespace);
admin1.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("r1", "r2"));
@Cleanup
final PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString())
.statsInterval(0, TimeUnit.SECONDS).build();
// Create subscription in r1
createReplicatedSubscription(client1, topicName, subName, true);
@Cleanup
final PulsarClient client2 = PulsarClient.builder().serviceUrl(url2.toString())
.statsInterval(0, TimeUnit.SECONDS).build();
// Create subscription in r2
createReplicatedSubscription(client2, topicName, subName, true);
TopicStats stats = admin1.topics().getStats(topicName);
assertTrue(stats.getSubscriptions().get(subName).isReplicated());
// Disable replicated subscription in r1
admin1.topics().setReplicatedSubscriptionStatus(topicName, subName, false);
stats = admin1.topics().getStats(topicName);
assertFalse(stats.getSubscriptions().get(subName).isReplicated());
stats = admin2.topics().getStats(topicName);
assertTrue(stats.getSubscriptions().get(subName).isReplicated());
// Disable replicated subscription in r2
admin2.topics().setReplicatedSubscriptionStatus(topicName, subName, false);
stats = admin2.topics().getStats(topicName);
assertFalse(stats.getSubscriptions().get(subName).isReplicated());
// Unload topic in r1
admin1.topics().unload(topicName);
Awaitility.await().untilAsserted(() -> {
TopicStats stats2 = admin1.topics().getStats(topicName);
assertFalse(stats2.getSubscriptions().get(subName).isReplicated());
});
// Make sure the replicated subscription is actually disabled
final int numMessages = 20;
final Set<String> sentMessages = new LinkedHashSet<>();
final Set<String> receivedMessages = new LinkedHashSet<>();
Producer<byte[]> producer = client1.newProducer().topic(topicName).enableBatching(false).create();
sentMessages.clear();
publishMessages(producer, 0, numMessages, sentMessages);
producer.close();
Consumer<byte[]> consumer1 = client1.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
receivedMessages.clear();
readMessages(consumer1, receivedMessages, numMessages, false);
assertEquals(receivedMessages, sentMessages);
consumer1.close();
Consumer<byte[]> consumer2 = client2.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
receivedMessages.clear();
readMessages(consumer2, receivedMessages, numMessages, false);
assertEquals(receivedMessages, sentMessages);
consumer2.close();
// Enable replicated subscription in r1
admin1.topics().setReplicatedSubscriptionStatus(topicName, subName, true);
stats = admin1.topics().getStats(topicName);
assertTrue(stats.getSubscriptions().get(subName).isReplicated());
stats = admin2.topics().getStats(topicName);
assertFalse(stats.getSubscriptions().get(subName).isReplicated());
// Enable replicated subscription in r2
admin2.topics().setReplicatedSubscriptionStatus(topicName, subName, true);
stats = admin2.topics().getStats(topicName);
assertTrue(stats.getSubscriptions().get(subName).isReplicated());
// Make sure the replicated subscription is actually enabled
sentMessages.clear();
receivedMessages.clear();
producer = client1.newProducer().topic(topicName).enableBatching(false).create();
publishMessages(producer, 0, numMessages / 2, sentMessages);
producer.close();
Thread.sleep(2 * config1.getReplicatedSubscriptionsSnapshotFrequencyMillis());
consumer1 = client1.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
final int numReceivedMessages1 = readMessages(consumer1, receivedMessages, numMessages / 2, allowDuplicates);
consumer1.close();
producer = client1.newProducer().topic(topicName).enableBatching(false).create();
publishMessages(producer, numMessages / 2, numMessages / 2, sentMessages);
producer.close();
Thread.sleep(2 * config1.getReplicatedSubscriptionsSnapshotFrequencyMillis());
consumer2 = client2.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
final int numReceivedMessages2 = readMessages(consumer2, receivedMessages, -1, allowDuplicates);
consumer2.close();
assertEquals(receivedMessages, sentMessages);
assertTrue(numReceivedMessages1 < numMessages,
String.format("numReceivedMessages1 (%d) should be less than %d", numReceivedMessages1, numMessages));
assertTrue(numReceivedMessages2 < numMessages,
String.format("numReceivedMessages2 (%d) should be less than %d", numReceivedMessages2, numMessages));
}
@Test
public void testGetReplicatedSubscriptionStatus() throws Exception {
final String namespace = BrokerTestUtil.newUniqueName("pulsar/replicatedsubscription");
final String topicName1 = "persistent://" + namespace + "/tp-no-part";
final String topicName2 = "persistent://" + namespace + "/tp-with-part";
final String subName1 = "sub1";
final String subName2 = "sub2";
admin1.namespaces().createNamespace(namespace);
admin1.topics().createNonPartitionedTopic(topicName1);
admin1.topics().createPartitionedTopic(topicName2, 3);
@Cleanup final PulsarClient client = PulsarClient.builder().serviceUrl(url1.toString())
.statsInterval(0, TimeUnit.SECONDS).build();
// Create subscription on non-partitioned topic
createReplicatedSubscription(client, topicName1, subName1, true);
Awaitility.await().untilAsserted(() -> {
Map<String, Boolean> status = admin1.topics().getReplicatedSubscriptionStatus(topicName1, subName1);
assertTrue(status.get(topicName1));
});
// Disable replicated subscription on non-partitioned topic
admin1.topics().setReplicatedSubscriptionStatus(topicName1, subName1, false);
Awaitility.await().untilAsserted(() -> {
Map<String, Boolean> status = admin1.topics().getReplicatedSubscriptionStatus(topicName1, subName1);
assertFalse(status.get(topicName1));
});
// Create subscription on partitioned topic
createReplicatedSubscription(client, topicName2, subName2, true);
Awaitility.await().untilAsserted(() -> {
Map<String, Boolean> status = admin1.topics().getReplicatedSubscriptionStatus(topicName2, subName2);
assertEquals(status.size(), 3);
for (int i = 0; i < 3; i++) {
assertTrue(status.get(topicName2 + "-partition-" + i));
}
});
// Disable replicated subscription on partitioned topic
admin1.topics().setReplicatedSubscriptionStatus(topicName2, subName2, false);
Awaitility.await().untilAsserted(() -> {
Map<String, Boolean> status = admin1.topics().getReplicatedSubscriptionStatus(topicName2, subName2);
assertEquals(status.size(), 3);
for (int i = 0; i < 3; i++) {
assertFalse(status.get(topicName2 + "-partition-" + i));
}
});
// Enable replicated subscription on partition-2
admin1.topics().setReplicatedSubscriptionStatus(topicName2 + "-partition-2", subName2, true);
Awaitility.await().untilAsserted(() -> {
Map<String, Boolean> status = admin1.topics().getReplicatedSubscriptionStatus(topicName2, subName2);
assertEquals(status.size(), 3);
for (int i = 0; i < 3; i++) {
if (i == 2) {
assertTrue(status.get(topicName2 + "-partition-" + i));
} else {
assertFalse(status.get(topicName2 + "-partition-" + i));
}
}
});
}
@Test(timeOut = 30000)
public void testReplicatedSubscriptionRestApi2() throws Exception {
final String namespace = BrokerTestUtil.newUniqueName("pulsar/replicatedsubscription");
final String topicName = "persistent://" + namespace + "/topic-rest-api2";
final String subName = "sub";
// Subscription replication produces duplicates, https://github.com/apache/pulsar/issues/10054
// TODO: duplications shouldn't be allowed, change to "false" when fixing the issue
final boolean allowDuplicates = true;
admin1.namespaces().createNamespace(namespace);
admin1.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("r1", "r2"));
admin1.topics().createPartitionedTopic(topicName, 2);
@Cleanup
final PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString())
.statsInterval(0, TimeUnit.SECONDS).build();
// Create subscription in r1
createReplicatedSubscription(client1, topicName, subName, true);
@Cleanup
final PulsarClient client2 = PulsarClient.builder().serviceUrl(url2.toString())
.statsInterval(0, TimeUnit.SECONDS).build();
// Create subscription in r2
createReplicatedSubscription(client2, topicName, subName, true);
PartitionedTopicStats partitionedStats = admin1.topics().getPartitionedStats(topicName, true);
for (TopicStats stats : partitionedStats.getPartitions().values()) {
assertTrue(stats.getSubscriptions().get(subName).isReplicated());
}
// Disable replicated subscription in r1
admin1.topics().setReplicatedSubscriptionStatus(topicName, subName, false);
partitionedStats = admin1.topics().getPartitionedStats(topicName, true);
for (TopicStats stats : partitionedStats.getPartitions().values()) {
assertFalse(stats.getSubscriptions().get(subName).isReplicated());
}
// Disable replicated subscription in r2
admin2.topics().setReplicatedSubscriptionStatus(topicName, subName, false);
partitionedStats = admin2.topics().getPartitionedStats(topicName, true);
for (TopicStats stats : partitionedStats.getPartitions().values()) {
assertFalse(stats.getSubscriptions().get(subName).isReplicated());
}
// Make sure the replicated subscription is actually disabled
final int numMessages = 20;
final Set<String> sentMessages = new LinkedHashSet<>();
final Set<String> receivedMessages = new LinkedHashSet<>();
Producer<byte[]> producer = client1.newProducer().topic(topicName).enableBatching(false)
.messageRoutingMode(MessageRoutingMode.SinglePartition).create();
sentMessages.clear();
publishMessages(producer, 0, numMessages, sentMessages);
producer.close();
Consumer<byte[]> consumer1 = client1.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
receivedMessages.clear();
readMessages(consumer1, receivedMessages, numMessages, false);
assertEquals(receivedMessages, sentMessages);
consumer1.close();
Consumer<byte[]> consumer2 = client2.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
receivedMessages.clear();
readMessages(consumer2, receivedMessages, numMessages, false);
assertEquals(receivedMessages, sentMessages);
consumer2.close();
// Enable replicated subscription in r1
admin1.topics().setReplicatedSubscriptionStatus(topicName, subName, true);
partitionedStats = admin1.topics().getPartitionedStats(topicName, true);
for (TopicStats stats : partitionedStats.getPartitions().values()) {
assertTrue(stats.getSubscriptions().get(subName).isReplicated());
}
// Enable replicated subscription in r2
admin2.topics().setReplicatedSubscriptionStatus(topicName, subName, true);
partitionedStats = admin2.topics().getPartitionedStats(topicName, true);
for (TopicStats stats : partitionedStats.getPartitions().values()) {
assertTrue(stats.getSubscriptions().get(subName).isReplicated());
}
// Make sure the replicated subscription is actually enabled
sentMessages.clear();
receivedMessages.clear();
producer = client1.newProducer().topic(topicName).enableBatching(false)
.messageRoutingMode(MessageRoutingMode.SinglePartition).create();
publishMessages(producer, 0, numMessages / 2, sentMessages);
producer.close();
Thread.sleep(2 * config1.getReplicatedSubscriptionsSnapshotFrequencyMillis());
consumer1 = client1.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
final int numReceivedMessages1 = readMessages(consumer1, receivedMessages, numMessages / 2, allowDuplicates);
consumer1.close();
producer = client1.newProducer().topic(topicName).enableBatching(false)
.messageRoutingMode(MessageRoutingMode.SinglePartition).create();
publishMessages(producer, numMessages / 2, numMessages / 2, sentMessages);
producer.close();
Thread.sleep(2 * config1.getReplicatedSubscriptionsSnapshotFrequencyMillis());
consumer2 = client2.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
final int numReceivedMessages2 = readMessages(consumer2, receivedMessages, -1, allowDuplicates);
consumer2.close();
assertEquals(receivedMessages, sentMessages);
assertTrue(numReceivedMessages1 < numMessages,
String.format("numReceivedMessages1 (%d) should be less than %d", numReceivedMessages1, numMessages));
assertTrue(numReceivedMessages2 < numMessages,
String.format("numReceivedMessages2 (%d) should be less than %d", numReceivedMessages2, numMessages));
}
@Test(timeOut = 30000)
public void testReplicatedSubscriptionRestApi3() throws Exception {
final String namespace = BrokerTestUtil.newUniqueName("geo/replicatedsubscription");
final String topicName = "persistent://" + namespace + "/topic-rest-api3";
final String subName = "sub";
admin4.tenants().createTenant("geo",
new TenantInfoImpl(Sets.newHashSet("appid1", "appid4"), Sets.newHashSet(cluster1, cluster4)));
admin4.namespaces().createNamespace(namespace);
admin4.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet(cluster1, cluster4));
admin4.topics().createPartitionedTopic(topicName, 2);
@Cleanup
final PulsarClient client4 = PulsarClient.builder().serviceUrl(url4.toString())
.statsInterval(0, TimeUnit.SECONDS).build();
Consumer<byte[]> consumer4 = client4.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
Assert.expectThrows(PulsarAdminException.class, () ->
admin4.topics().setReplicatedSubscriptionStatus(topicName, subName, true));
consumer4.close();
}
/**
* Tests replicated subscriptions when replicator producer is closed
*/
@Test
public void testReplicatedSubscriptionWhenReplicatorProducerIsClosed() throws Exception {
String namespace = BrokerTestUtil.newUniqueName("pulsar/replicatedsubscription");
String topicName = "persistent://" + namespace + "/when-replicator-producer-is-closed";
String subscriptionName = "sub";
admin1.namespaces().createNamespace(namespace);
admin1.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("r1", "r2"));
@Cleanup
PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString())
.statsInterval(0, TimeUnit.SECONDS)
.build();
{
// create consumer in r1
@Cleanup
Consumer<byte[]> consumer = client1.newConsumer()
.topic(topicName)
.subscriptionName(subscriptionName)
.replicateSubscriptionState(true)
.subscribe();
// send one message to trigger replication
@Cleanup
Producer<byte[]> producer = client1.newProducer().topic(topicName)
.enableBatching(false)
.messageRoutingMode(MessageRoutingMode.SinglePartition)
.create();
producer.send("message".getBytes(StandardCharsets.UTF_8));
assertEquals(readMessages(consumer, new HashSet<>(), 1, false), 1);
// waiting to replicate topic/subscription to r1->r2
Awaitility.await().until(() -> pulsar2.getBrokerService().getTopics().containsKey(topicName));
final PersistentTopic topic2 = (PersistentTopic) pulsar2.getBrokerService().getTopic(topicName, false).join().get();
Awaitility.await().untilAsserted(() -> assertTrue(topic2.getReplicators().get("r1").isConnected()));
Awaitility.await().untilAsserted(() -> assertNotNull(topic2.getSubscription(subscriptionName)));
}
// unsubscribe replicated subscription in r2
admin2.topics().deleteSubscription(topicName, subscriptionName);
final PersistentTopic topic2 = (PersistentTopic) pulsar2.getBrokerService().getTopic(topicName, false).join().get();
assertNull(topic2.getSubscription(subscriptionName));
// close replicator producer in r2
final Method closeReplProducersIfNoBacklog = PersistentTopic.class.getDeclaredMethod("closeReplProducersIfNoBacklog", null);
closeReplProducersIfNoBacklog.setAccessible(true);
((CompletableFuture<Void>) closeReplProducersIfNoBacklog.invoke(topic2, null)).join();
assertFalse(topic2.getReplicators().get("r1").isConnected());
// send messages in r1
int numMessages = 6;
{
@Cleanup
Producer<byte[]> producer = client1.newProducer().topic(topicName)
.enableBatching(false)
.messageRoutingMode(MessageRoutingMode.SinglePartition)
.create();
for (int i = 0; i < numMessages; i++) {
String body = "message" + i;
producer.send(body.getBytes(StandardCharsets.UTF_8));
}
}
// consume 6 messages in r1
Set<String> receivedMessages = new LinkedHashSet<>();
@Cleanup
Consumer<byte[]> consumer1 = client1.newConsumer()
.topic(topicName)
.subscriptionName(subscriptionName)
.replicateSubscriptionState(true)
.subscribe();
assertEquals(readMessages(consumer1, receivedMessages, numMessages, false), numMessages);
// wait for subscription to be replicated
Awaitility.await().untilAsserted(() -> assertTrue(topic2.getReplicators().get("r1").isConnected()));
Awaitility.await().untilAsserted(() -> assertNotNull(topic2.getSubscription(subscriptionName)));
}
@DataProvider(name = "isTopicPolicyEnabled")
private Object[][] isTopicPolicyEnabled() {
// Todo: fix replication can not be enabled at topic level.
return new Object[][] { { Boolean.FALSE } };
}
/**
* Test the replication subscription can work normal in the following cases:
* <p>
* 1. Do not write data into the original topic when the topic does not configure a remote cluster. {topic1}
* 1. Publish message to the topic and then wait a moment,
* the backlog will not increase after publishing completely.
* 2. Acknowledge the messages, the last confirm entry does not change.
* 2. Snapshot and mark will be written after topic configure a remote cluster. {topic2}
* 1. publish message to topic. After publishing completely, the backlog of the topic keep increase.
* 2. Wait the snapshot complete, the backlog stop changing.
* 3. Publish messages to wait another snapshot complete.
* 4. Ack messages to move the mark delete position after the position record in the first snapshot.
* 5. Check new entry (a mark) appending to the original topic.
* 3. Stopping writing snapshot and mark after remove the remote cluster of the topic. {topic2}
* similar to step 1.
* </p>
*/
@Test(dataProvider = "isTopicPolicyEnabled")
public void testWriteMarkerTaskOfReplicateSubscriptions(boolean isTopicPolicyEnabled) throws Exception {
// 1. Prepare resource and use proper configuration.
String namespace = BrokerTestUtil.newUniqueName("pulsar/testReplicateSubBackLog");
String topic1 = "persistent://" + namespace + "/replication-enable";
String topic2 = "persistent://" + namespace + "/replication-disable";
String subName = "sub";
admin1.namespaces().createNamespace(namespace);
pulsar1.getConfiguration().setTopicLevelPoliciesEnabled(isTopicPolicyEnabled);
pulsar1.getConfiguration().setReplicationPolicyCheckDurationSeconds(1);
pulsar1.getConfiguration().setReplicatedSubscriptionsSnapshotFrequencyMillis(1000);
// 2. Build Producer and Consumer.
@Cleanup
PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString())
.statsInterval(0, TimeUnit.SECONDS)
.build();
@Cleanup
Consumer<byte[]> consumer1 = client1.newConsumer()
.topic(topic1)
.subscriptionName(subName)
.ackTimeout(5, TimeUnit.SECONDS)
.subscriptionType(SubscriptionType.Shared)
.replicateSubscriptionState(true)
.subscribe();
@Cleanup
Producer<byte[]> producer1 = client1.newProducer()
.topic(topic1)
.create();
// 3. Test replication subscription work as expected.
// Test case 1: disable replication, backlog will not increase.
testReplicatedSubscriptionWhenDisableReplication(producer1, consumer1, topic1);
// Test case 2: enable replication, mark and snapshot work as expected.
if (isTopicPolicyEnabled) {
admin1.topics().createNonPartitionedTopic(topic2);
admin1.topics().setReplicationClusters(topic2, List.of("r1", "r2"));
} else {
admin1.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("r1", "r2"));
}
@Cleanup
Consumer<byte[]> consumer2 = client1.newConsumer()
.topic(topic2)
.subscriptionName(subName)
.ackTimeout(5, TimeUnit.SECONDS)
.subscriptionType(SubscriptionType.Shared)
.replicateSubscriptionState(true)
.subscribe();
@Cleanup
Producer<byte[]> producer2 = client1.newProducer()
.topic(topic2)
.create();
testReplicatedSubscriptionWhenEnableReplication(producer2, consumer2, topic2);
// Test case 3: enable replication, mark and snapshot work as expected.
if (isTopicPolicyEnabled) {
admin1.topics().setReplicationClusters(topic2, List.of("r1"));
} else {
admin1.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("r1"));
}
testReplicatedSubscriptionWhenDisableReplication(producer2, consumer2, topic2);
// 4. Clear resource.
pulsar1.getConfiguration().setForceDeleteNamespaceAllowed(true);
admin1.namespaces().deleteNamespace(namespace, true);
pulsar1.getConfiguration().setForceDeleteNamespaceAllowed(false);
}
@Test
public void testReplicatedSubscriptionWithCompaction() throws Exception {
final String namespace = BrokerTestUtil.newUniqueName("pulsar/replicatedsubscription");
final String topicName = "persistent://" + namespace + "/testReplicatedSubscriptionWithCompaction";
final String subName = "sub";
admin1.namespaces().createNamespace(namespace);
admin1.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("r1", "r2"));
admin1.topics().createNonPartitionedTopic(topicName);
admin1.topicPolicies().setCompactionThreshold(topicName, 100 * 1024 * 1024L);
@Cleanup final PulsarClient client = PulsarClient.builder().serviceUrl(url1.toString())
.statsInterval(0, TimeUnit.SECONDS).build();
Producer<String> producer = client.newProducer(Schema.STRING).topic(topicName).create();
producer.newMessage().key("K1").value("V1").send();
producer.newMessage().key("K1").value("V2").send();
producer.close();
createReplicatedSubscription(client, topicName, subName, true);
Awaitility.await().untilAsserted(() -> {
Map<String, Boolean> status = admin1.topics().getReplicatedSubscriptionStatus(topicName, subName);
assertTrue(status.get(topicName));
});
Awaitility.await().untilAsserted(() -> {
PersistentTopic t1 = (PersistentTopic) pulsar1.getBrokerService()
.getTopic(topicName, false).get().get();
ReplicatedSubscriptionsController rsc1 = t1.getReplicatedSubscriptionController().get();
Assert.assertTrue(rsc1.getLastCompletedSnapshotId().isPresent());
assertEquals(t1.getPendingWriteOps().get(), 0L);
});
admin1.topics().triggerCompaction(topicName);
Awaitility.await().untilAsserted(() -> {
assertEquals(admin1.topics().compactionStatus(topicName).status,
LongRunningProcessStatus.Status.SUCCESS);
});
@Cleanup
Consumer<String> consumer = client.newConsumer(Schema.STRING)
.topic(topicName)
.subscriptionName("sub2")
.subscriptionType(SubscriptionType.Exclusive)
.subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
.readCompacted(true)
.subscribe();
List<String> result = new ArrayList<>();
while (true) {
Message<String> receive = consumer.receive(2, TimeUnit.SECONDS);
if (receive == null) {
break;
}
result.add(receive.getValue());
}
Assert.assertEquals(result, List.of("V2"));
}
/**
* Disable replication subscription.
* Test scheduled task case.
* 1. Send three messages |1:0|1:1|1:2|.
* 2. Get topic backlog, as backlog1.
* 3. Wait a moment.
* 4. Get the topic backlog again, the backlog will not increase.
* Test acknowledge messages case.
* 1. Get the last confirm entry, as LAC1.
* 2. Acknowledge these messages |1:0|1:1|.
* 3. wait a moment.
* 4. Get the last confirm entry, as LAC2. LAC1 is equal to LAC2.
* Clear environment.
* 1. Ack all the retained messages. |1:2|
* 2. Wait for the backlog to return to zero.
*/
private void testReplicatedSubscriptionWhenDisableReplication(Producer<byte[]> producer, Consumer<byte[]> consumer,
String topic) throws Exception {
final int messageSum = 3;
// Test scheduled task case.
for (int i = 0; i < messageSum; i++) {
producer.newMessage().send();
}
long backlog1 = admin1.topics().getStats(topic, false).getBacklogSize();
Thread.sleep(3000);
long backlog2 = admin1.topics().getStats(topic, false).getBacklogSize();
assertEquals(backlog1, backlog2);
// Test acknowledge messages case.
String lastConfirmEntry1 = admin1.topics().getInternalStats(topic).lastConfirmedEntry;
for (int i = 0; i < messageSum - 1; i++) {
consumer.acknowledge(consumer.receive(5, TimeUnit.SECONDS));
}
Awaitility.await().untilAsserted(() -> {
String lastConfirmEntry2 = admin1.topics().getInternalStats(topic).lastConfirmedEntry;
assertEquals(lastConfirmEntry1, lastConfirmEntry2);
});