-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathConsumersCoordinator.java
1282 lines (1186 loc) · 50 KB
/
ConsumersCoordinator.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
// Copyright (c) 2020-2023 VMware, Inc. or its affiliates. All rights reserved.
//
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL").
// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
package com.rabbitmq.stream.impl;
import static com.rabbitmq.stream.impl.Utils.convertCodeToException;
import static com.rabbitmq.stream.impl.Utils.formatConstant;
import static com.rabbitmq.stream.impl.Utils.isSac;
import static com.rabbitmq.stream.impl.Utils.jsonField;
import static com.rabbitmq.stream.impl.Utils.namedFunction;
import static com.rabbitmq.stream.impl.Utils.namedRunnable;
import static com.rabbitmq.stream.impl.Utils.quote;
import static java.lang.String.format;
import com.rabbitmq.stream.*;
import com.rabbitmq.stream.Consumer;
import com.rabbitmq.stream.MessageHandler.Context;
import com.rabbitmq.stream.SubscriptionListener.SubscriptionContext;
import com.rabbitmq.stream.impl.Client.*;
import com.rabbitmq.stream.impl.Client.ConsumerUpdateListener;
import com.rabbitmq.stream.impl.Utils.ClientConnectionType;
import com.rabbitmq.stream.impl.Utils.ClientFactory;
import com.rabbitmq.stream.impl.Utils.ClientFactoryContext;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableSet;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class ConsumersCoordinator {
static final int MAX_SUBSCRIPTIONS_PER_CLIENT = 256;
static final int MAX_ATTEMPT_BEFORE_FALLING_BACK_TO_LEADER = 5;
static final OffsetSpecification DEFAULT_OFFSET_SPECIFICATION = OffsetSpecification.next();
private static final Logger LOGGER = LoggerFactory.getLogger(ConsumersCoordinator.class);
private final Random random = new Random();
private final StreamEnvironment environment;
private final ClientFactory clientFactory;
private final int maxConsumersByConnection;
private final Function<ClientConnectionType, String> connectionNamingStrategy;
private final AtomicLong managerIdSequence = new AtomicLong(0);
private final NavigableSet<ClientSubscriptionsManager> managers = new ConcurrentSkipListSet<>();
private final AtomicLong trackerIdSequence = new AtomicLong(0);
private final boolean debug = false;
private final List<SubscriptionTracker> trackers = new CopyOnWriteArrayList<>();
private final ExecutorServiceFactory executorServiceFactory =
new DefaultExecutorServiceFactory(
Runtime.getRuntime().availableProcessors(), 10, "rabbitmq-stream-consumer-connection-");
private final boolean forceReplica;
ConsumersCoordinator(
StreamEnvironment environment,
int maxConsumersByConnection,
Function<ClientConnectionType, String> connectionNamingStrategy,
ClientFactory clientFactory,
boolean forceReplica) {
this.environment = environment;
this.clientFactory = clientFactory;
this.maxConsumersByConnection = maxConsumersByConnection;
this.connectionNamingStrategy = connectionNamingStrategy;
this.forceReplica = forceReplica;
}
private static String keyForClientSubscription(Client.Broker broker) {
return broker.getHost() + ":" + broker.getPort();
}
private BackOffDelayPolicy recoveryBackOffDelayPolicy() {
return this.environment.recoveryBackOffDelayPolicy();
}
private BackOffDelayPolicy metadataUpdateBackOffDelayPolicy() {
return environment.topologyUpdateBackOffDelayPolicy();
}
Runnable subscribe(
StreamConsumer consumer,
String stream,
OffsetSpecification offsetSpecification,
String trackingReference,
SubscriptionListener subscriptionListener,
Runnable trackingClosingCallback,
MessageHandler messageHandler,
Map<String, String> subscriptionProperties,
ConsumerFlowStrategy flowStrategy) {
List<Client.Broker> candidates = findBrokersForStream(stream, forceReplica);
Client.Broker newNode = pickBroker(candidates);
if (newNode == null) {
throw new IllegalStateException("No available node to subscribe to");
}
// create stream subscription to track final and changing state of this very subscription
// we keep this instance when we move the subscription from a client to another one
SubscriptionTracker subscriptionTracker =
new SubscriptionTracker(
this.trackerIdSequence.getAndIncrement(),
consumer,
stream,
offsetSpecification,
trackingReference,
subscriptionListener,
trackingClosingCallback,
messageHandler,
subscriptionProperties,
flowStrategy);
try {
addToManager(newNode, subscriptionTracker, offsetSpecification, true);
} catch (ConnectionStreamException e) {
// these exceptions are not public
throw new StreamException(e.getMessage());
}
if (debug) {
this.trackers.add(subscriptionTracker);
return () -> {
try {
this.trackers.remove(subscriptionTracker);
} catch (Exception e) {
LOGGER.debug("Error while removing subscription tracker from list");
}
subscriptionTracker.cancel();
};
} else {
return subscriptionTracker::cancel;
}
}
private void addToManager(
Broker node,
SubscriptionTracker tracker,
OffsetSpecification offsetSpecification,
boolean isInitialSubscription) {
ClientParameters clientParameters =
environment
.clientParametersCopy()
.executorServiceFactory(this.executorServiceFactory)
.host(node.getHost())
.port(node.getPort());
ClientSubscriptionsManager pickedManager = null;
while (pickedManager == null) {
Iterator<ClientSubscriptionsManager> iterator = this.managers.iterator();
while (iterator.hasNext()) {
pickedManager = iterator.next();
if (pickedManager.isClosed()) {
iterator.remove();
pickedManager = null;
} else {
if (node.equals(pickedManager.node) && !pickedManager.isFull()) {
// let's try this one
break;
} else {
pickedManager = null;
}
}
}
if (pickedManager == null) {
String name = keyForClientSubscription(node);
LOGGER.debug("Creating subscription manager on {}", name);
pickedManager = new ClientSubscriptionsManager(node, clientParameters);
LOGGER.debug("Created subscription manager on {}, id {}", name, pickedManager.id);
}
try {
pickedManager.add(tracker, offsetSpecification, isInitialSubscription);
LOGGER.debug(
"Assigned tracker {} (stream '{}') to manager {} (node {}), subscription ID {}",
tracker.id,
tracker.stream,
pickedManager.id,
pickedManager.name,
tracker.subscriptionIdInClient);
this.managers.add(pickedManager);
} catch (IllegalStateException e) {
pickedManager = null;
} catch (ConnectionStreamException | ClientClosedException | StreamNotAvailableException e) {
// manager connection is dead or stream not available
// scheduling manager closing if necessary in another thread to avoid blocking this one
if (pickedManager.isEmpty()) {
ConsumersCoordinator.this.environment.execute(
pickedManager::closeIfEmpty,
"Consumer manager closing after timeout, consumer %d on stream '%s'",
tracker.consumer.id(),
tracker.stream);
}
throw e;
} catch (RuntimeException e) {
if (pickedManager != null) {
pickedManager.closeIfEmpty();
}
throw e;
}
}
}
int managerCount() {
return this.managers.size();
}
// package protected for testing
List<Client.Broker> findBrokersForStream(String stream, boolean forceReplica) {
LOGGER.debug(
"Candidate lookup to consumer from '{}', forcing replica? {}", stream, forceReplica);
Map<String, Client.StreamMetadata> metadata =
this.environment.locatorOperation(
namedFunction(
c -> c.metadata(stream), "Candidate lookup to consume from '%s'", stream));
if (metadata.isEmpty() || metadata.get(stream) == null) {
// this is not supposed to happen
throw new StreamDoesNotExistException(stream);
}
Client.StreamMetadata streamMetadata = metadata.get(stream);
if (!streamMetadata.isResponseOk()) {
if (streamMetadata.getResponseCode() == Constants.RESPONSE_CODE_STREAM_DOES_NOT_EXIST) {
throw new StreamDoesNotExistException(stream);
} else {
throw new IllegalStateException(
"Could not get stream metadata, response code: "
+ formatConstant(streamMetadata.getResponseCode()));
}
}
List<Client.Broker> replicas = streamMetadata.getReplicas();
if ((replicas == null || replicas.isEmpty()) && streamMetadata.getLeader() == null) {
throw new IllegalStateException("No node available to consume from stream " + stream);
}
List<Client.Broker> brokers;
if (replicas == null || replicas.isEmpty()) {
if (forceReplica) {
throw new IllegalStateException(
format(
"Only the leader node is available for consuming from %s and "
+ "consuming from leader has been deactivated for this consumer",
stream));
} else {
brokers = Collections.singletonList(streamMetadata.getLeader());
LOGGER.debug(
"Only leader node {} for consuming from {}", streamMetadata.getLeader(), stream);
}
} else {
LOGGER.debug("Replicas for consuming from {}: {}", stream, replicas);
brokers = new ArrayList<>(replicas);
}
LOGGER.debug("Candidates to consume from {}: {}", stream, brokers);
return brokers;
}
private Callable<List<Broker>> findBrokersForStream(String stream) {
AtomicInteger attemptNumber = new AtomicInteger();
return () -> {
boolean mustUseReplica;
if (forceReplica) {
mustUseReplica =
attemptNumber.incrementAndGet() <= MAX_ATTEMPT_BEFORE_FALLING_BACK_TO_LEADER;
} else {
mustUseReplica = false;
}
LOGGER.debug(
"Looking for broker(s) for stream {}, forcing replica {}", stream, mustUseReplica);
return findBrokersForStream(stream, mustUseReplica);
};
}
private Client.Broker pickBroker(List<Client.Broker> brokers) {
if (brokers.isEmpty()) {
return null;
} else if (brokers.size() == 1) {
return brokers.get(0);
} else {
return brokers.get(random.nextInt(brokers.size()));
}
}
public void close() {
Iterator<ClientSubscriptionsManager> iterator = this.managers.iterator();
while (iterator.hasNext()) {
ClientSubscriptionsManager manager = iterator.next();
try {
iterator.remove();
manager.close();
} catch (Exception e) {
LOGGER.info(
"Error while closing manager {} connected to node {}: {}",
manager.id,
manager.name,
e.getMessage());
}
}
try {
this.executorServiceFactory.close();
} catch (Exception e) {
LOGGER.info("Error while closing executor service factory: {}", e.getMessage());
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder("{");
builder.append(jsonField("client_count", this.managers.size())).append(", ");
builder.append(quote("clients")).append(" : [");
builder.append(
this.managers.stream()
.map(
m -> {
StringBuilder managerBuilder = new StringBuilder("{");
managerBuilder
.append(jsonField("id", m.id))
.append(",")
.append(jsonField("node", m.name))
.append(",")
.append(jsonField("consumer_count", m.trackerCount))
.append(",");
managerBuilder.append("\"subscriptions\" : [");
List<SubscriptionTracker> trackers = m.subscriptionTrackers;
managerBuilder.append(
trackers.stream()
.filter(Objects::nonNull)
.map(
t -> {
StringBuilder trackerBuilder = new StringBuilder("{");
trackerBuilder.append(jsonField("stream", t.stream)).append(",");
trackerBuilder.append(
jsonField("subscription_id", t.subscriptionIdInClient));
return trackerBuilder.append("}").toString();
})
.collect(Collectors.joining(",")));
managerBuilder.append("]");
return managerBuilder.append("}").toString();
})
.collect(Collectors.joining(",")));
builder.append("]");
if (debug) {
builder.append(",");
builder.append("\"subscription_count\" : ").append(this.trackers.size()).append(",");
builder.append("\"subscriptions\" : [");
builder.append(
this.trackers.stream()
.map(
t -> {
StringBuilder b = new StringBuilder("{");
b.append(quote("stream")).append(":").append(quote(t.stream)).append(",");
b.append(quote("node")).append(":");
Client client = null;
ClientSubscriptionsManager manager = t.manager;
if (manager != null) {
client = manager.client;
}
if (client == null) {
b.append("null");
} else {
b.append(quote(client.getHost() + ":" + client.getPort()));
}
return b.append("}").toString();
})
.collect(Collectors.joining(",")));
builder.append("]");
}
builder.append("}");
return builder.toString();
}
/**
* Data structure that keeps track of a given {@link StreamConsumer} and its message callback.
*
* <p>An instance is "moved" between {@link ClientSubscriptionsManager} instances on stream
* failure or on disconnection.
*/
private static class SubscriptionTracker {
private final long id;
private final String stream;
private final OffsetSpecification initialOffsetSpecification;
private final String offsetTrackingReference;
private final MessageHandler messageHandler;
private final StreamConsumer consumer;
private final SubscriptionListener subscriptionListener;
private final Runnable trackingClosingCallback;
private final Map<String, String> subscriptionProperties;
private volatile long offset;
private volatile boolean hasReceivedSomething = false;
private volatile byte subscriptionIdInClient;
private volatile ClientSubscriptionsManager manager;
private volatile AtomicReference<SubscriptionState> state =
new AtomicReference<>(SubscriptionState.OPENING);
private final ConsumerFlowStrategy flowStrategy;
private SubscriptionTracker(
long id,
StreamConsumer consumer,
String stream,
OffsetSpecification initialOffsetSpecification,
String offsetTrackingReference,
SubscriptionListener subscriptionListener,
Runnable trackingClosingCallback,
MessageHandler messageHandler,
Map<String, String> subscriptionProperties,
ConsumerFlowStrategy flowStrategy) {
this.id = id;
this.consumer = consumer;
this.stream = stream;
this.initialOffsetSpecification = initialOffsetSpecification;
this.offsetTrackingReference = offsetTrackingReference;
this.subscriptionListener = subscriptionListener;
this.trackingClosingCallback = trackingClosingCallback;
this.messageHandler = messageHandler;
this.flowStrategy = flowStrategy;
if (this.offsetTrackingReference == null) {
this.subscriptionProperties = subscriptionProperties;
} else {
Map<String, String> properties = new ConcurrentHashMap<>(subscriptionProperties.size() + 1);
properties.putAll(subscriptionProperties);
// we propagate the subscription name, used for monitoring
properties.put("name", this.offsetTrackingReference);
this.subscriptionProperties = Collections.unmodifiableMap(properties);
}
}
synchronized void cancel() {
// the flow of messages in the user message handler should stop, we can call the tracking
// closing callback
// with automatic offset tracking, it will store the last dispatched offset
LOGGER.debug("Calling tracking consumer closing callback (may be no-op)");
this.trackingClosingCallback.run();
if (this.manager != null) {
LOGGER.debug("Removing consumer from manager " + this.consumer);
this.manager.remove(this);
} else {
LOGGER.debug("No manager to remove consumer from");
}
this.state(SubscriptionState.CLOSED);
}
synchronized void assign(byte subscriptionIdInClient, ClientSubscriptionsManager manager) {
this.subscriptionIdInClient = subscriptionIdInClient;
this.manager = manager;
if (this.manager == null) {
if (consumer != null) {
this.consumer.setSubscriptionClient(null);
}
} else {
this.consumer.setSubscriptionClient(this.manager.client);
}
}
synchronized void detachFromManager() {
this.manager = null;
this.consumer.setSubscriptionClient(null);
}
void state(SubscriptionState state) {
this.state.set(state);
}
boolean compareAndSet(SubscriptionState expected, SubscriptionState newValue) {
return this.state.compareAndSet(expected, newValue);
}
SubscriptionState state() {
return this.state.get();
}
}
private enum SubscriptionState {
OPENING,
ACTIVE,
RECOVERING,
CLOSED
}
private static final class MessageHandlerContext implements Context {
private final long offset;
private final long timestamp;
private final long committedOffset;
private final StreamConsumer consumer;
private final ConsumerFlowStrategy.MessageProcessedCallback processedCallback;
private MessageHandlerContext(
long offset,
long timestamp,
long committedOffset,
StreamConsumer consumer,
ConsumerFlowStrategy.MessageProcessedCallback processedCallback) {
this.offset = offset;
this.timestamp = timestamp;
this.committedOffset = committedOffset;
this.consumer = consumer;
this.processedCallback = processedCallback;
}
@Override
public long offset() {
return this.offset;
}
@Override
public void storeOffset() {
this.consumer.store(this.offset);
}
@Override
public long timestamp() {
return this.timestamp;
}
@Override
public long committedChunkId() {
return committedOffset;
}
public String stream() {
return this.consumer.stream();
}
@Override
public Consumer consumer() {
return this.consumer;
}
@Override
public void processed() {
this.processedCallback.processed(this);
}
}
/**
* Maintains a set of {@link SubscriptionTracker} instances on a {@link Client}.
*
* <p>It dispatches inbound messages to the appropriate {@link SubscriptionTracker} and
* re-allocates {@link SubscriptionTracker}s in case of stream unavailability or disconnection.
*/
private class ClientSubscriptionsManager implements Comparable<ClientSubscriptionsManager> {
private final long id;
private final Broker node;
private final Client client;
private final String name;
// the 2 data structures track the subscriptions, they must remain consistent
private final Map<String, Set<SubscriptionTracker>> streamToStreamSubscriptions =
new ConcurrentHashMap<>();
// trackers and tracker count must be kept in sync
private volatile List<SubscriptionTracker> subscriptionTrackers =
new ArrayList<>(maxConsumersByConnection);
private volatile int trackerCount = 0;
private final AtomicBoolean closed = new AtomicBoolean(false);
private ClientSubscriptionsManager(Broker node, Client.ClientParameters clientParameters) {
this.id = managerIdSequence.getAndIncrement();
this.node = node;
this.name = keyForClientSubscription(node);
LOGGER.debug("creating subscription manager on {}", name);
IntStream.range(0, maxConsumersByConnection).forEach(i -> subscriptionTrackers.add(null));
this.trackerCount = 0;
AtomicBoolean clientInitializedInManager = new AtomicBoolean(false);
ChunkListener chunkListener =
(client, subscriptionId, offset, messageCount, dataSize) -> {
SubscriptionTracker subscriptionTracker =
subscriptionTrackers.get(subscriptionId & 0xFF);
ConsumerFlowStrategy.MessageProcessedCallback processCallback;
if (subscriptionTracker != null && subscriptionTracker.consumer.isOpen()) {
processCallback =
subscriptionTracker.flowStrategy.start(
new DefaultConsumerFlowStrategyContext(subscriptionId, client, messageCount));
} else {
LOGGER.debug(
"Could not find stream subscription {} or subscription closing, not providing credits",
subscriptionId & 0xFF);
processCallback = null;
}
return processCallback;
};
CreditNotification creditNotification =
(subscriptionId, responseCode) -> {
SubscriptionTracker subscriptionTracker =
subscriptionTrackers.get(subscriptionId & 0xFF);
String stream = subscriptionTracker == null ? "?" : subscriptionTracker.stream;
LOGGER.debug(
"Received credit notification for subscription {} (stream '{}'): {}",
subscriptionId & 0xFF,
stream,
Utils.formatConstant(responseCode));
};
MessageListener messageListener =
(subscriptionId, offset, chunkTimestamp, committedChunkId, chunkContext, message) -> {
SubscriptionTracker subscriptionTracker =
subscriptionTrackers.get(subscriptionId & 0xFF);
if (subscriptionTracker != null) {
subscriptionTracker.offset = offset;
subscriptionTracker.hasReceivedSomething = true;
subscriptionTracker.messageHandler.handle(
new MessageHandlerContext(
offset,
chunkTimestamp,
committedChunkId,
subscriptionTracker.consumer,
(ConsumerFlowStrategy.MessageProcessedCallback) chunkContext),
message);
} else {
LOGGER.debug(
"Could not find stream subscription {} in manager {}, node {} for message listener",
subscriptionId,
this.id,
this.name);
}
};
MessageIgnoredListener messageIgnoredListener =
(subscriptionId, offset, chunkTimestamp, committedChunkId, chunkContext) -> {
SubscriptionTracker subscriptionTracker =
subscriptionTrackers.get(subscriptionId & 0xFF);
if (subscriptionTracker != null) {
// message at the beginning of the first chunk is ignored
// we "simulate" the processing
MessageHandlerContext messageHandlerContext =
new MessageHandlerContext(
offset,
chunkTimestamp,
committedChunkId,
subscriptionTracker.consumer,
(ConsumerFlowStrategy.MessageProcessedCallback) chunkContext);
((ConsumerFlowStrategy.MessageProcessedCallback) chunkContext)
.processed(messageHandlerContext);
} else {
LOGGER.debug(
"Could not find stream subscription {} in manager {}, node {} for message ignored listener",
subscriptionId,
this.id,
this.name);
}
};
ShutdownListener shutdownListener =
shutdownContext -> {
if (clientInitializedInManager.get()) {
this.closed.set(true);
managers.remove(this);
}
if (shutdownContext.isShutdownUnexpected()) {
LOGGER.debug(
"Unexpected shutdown notification on subscription connection {}, scheduling consumers re-assignment",
name);
LOGGER.debug(
"Subscription connection has {} consumer(s) over {} stream(s) to recover",
this.subscriptionTrackers.stream().filter(Objects::nonNull).count(),
this.streamToStreamSubscriptions.size());
environment
.scheduledExecutorService()
.execute(
namedRunnable(
() -> {
if (Thread.currentThread().isInterrupted()) {
return;
}
subscriptionTrackers.stream()
.filter(Objects::nonNull)
.filter(t -> t.state() == SubscriptionState.ACTIVE)
.forEach(SubscriptionTracker::detachFromManager);
for (Entry<String, Set<SubscriptionTracker>> entry :
streamToStreamSubscriptions.entrySet()) {
if (Thread.currentThread().isInterrupted()) {
LOGGER.debug("Interrupting consumer re-assignment task");
break;
}
String stream = entry.getKey();
Set<SubscriptionTracker> trackersToReAssign = entry.getValue();
if (trackersToReAssign == null || trackersToReAssign.isEmpty()) {
LOGGER.debug(
"No consumer to re-assign to stream {} after disconnection",
stream);
} else {
LOGGER.debug(
"Re-assigning {} consumer(s) to stream {} after disconnection",
trackersToReAssign.size(),
stream);
assignConsumersToStream(
trackersToReAssign,
stream,
recoveryBackOffDelayPolicy(),
false);
}
}
},
"Consumers re-assignment after disconnection from %s",
name));
}
};
MetadataListener metadataListener =
(stream, code) -> {
LOGGER.debug(
"Received metadata notification for '{}', stream is likely to have become unavailable",
stream);
Set<SubscriptionTracker> affectedSubscriptions;
synchronized (this) {
Set<SubscriptionTracker> subscriptions = streamToStreamSubscriptions.remove(stream);
if (subscriptions != null && !subscriptions.isEmpty()) {
List<SubscriptionTracker> newSubscriptions =
new ArrayList<>(maxConsumersByConnection);
for (int i = 0; i < maxConsumersByConnection; i++) {
newSubscriptions.add(subscriptionTrackers.get(i));
}
for (SubscriptionTracker subscription : subscriptions) {
LOGGER.debug(
"Subscription {} was at offset {} (received something? {})",
subscription.subscriptionIdInClient,
subscription.offset,
subscription.hasReceivedSomething);
newSubscriptions.set(subscription.subscriptionIdInClient & 0xFF, null);
subscription.consumer.setSubscriptionClient(null);
}
this.setSubscriptionTrackers(newSubscriptions);
}
affectedSubscriptions = subscriptions;
}
if (affectedSubscriptions != null && !affectedSubscriptions.isEmpty()) {
environment
.scheduledExecutorService()
.execute(
namedRunnable(
() -> {
if (Thread.currentThread().isInterrupted()) {
return;
}
LOGGER.debug(
"Trying to move {} subscription(s) (stream '{}')",
affectedSubscriptions.size(),
stream);
assignConsumersToStream(
affectedSubscriptions,
stream,
metadataUpdateBackOffDelayPolicy(),
true);
},
"Consumers re-assignment after metadata update on stream '%s'",
stream));
}
};
ConsumerUpdateListener consumerUpdateListener =
(client, subscriptionId, active) -> {
OffsetSpecification result = null;
SubscriptionTracker subscriptionTracker =
subscriptionTrackers.get(subscriptionId & 0xFF);
if (subscriptionTracker != null) {
if (isSac(subscriptionTracker.subscriptionProperties)) {
result = subscriptionTracker.consumer.consumerUpdate(active);
} else {
LOGGER.debug(
"Subscription {} is not a single active consumer, nothing to do.",
subscriptionId);
}
} else {
LOGGER.debug(
"Could not find stream subscription {} for consumer update", subscriptionId);
}
return result;
};
String connectionName = connectionNamingStrategy.apply(ClientConnectionType.CONSUMER);
ClientFactoryContext clientFactoryContext =
ClientFactoryContext.fromParameters(
clientParameters
.clientProperty("connection_name", connectionName)
.chunkListener(chunkListener)
.creditNotification(creditNotification)
.messageListener(messageListener)
.messageIgnoredListener(messageIgnoredListener)
.shutdownListener(shutdownListener)
.metadataListener(metadataListener)
.consumerUpdateListener(consumerUpdateListener))
.key(name);
this.client = clientFactory.client(clientFactoryContext);
LOGGER.debug("Created consumer connection '{}'", connectionName);
clientInitializedInManager.set(true);
}
private void assignConsumersToStream(
Collection<SubscriptionTracker> subscriptions,
String stream,
BackOffDelayPolicy delayPolicy,
boolean maybeCloseClient) {
Runnable consumersClosingCallback =
() -> {
LOGGER.debug(
"Running consumer closing callback after recovery failure, "
+ "closing {} subscription(s)",
subscriptions.size());
for (SubscriptionTracker affectedSubscription : subscriptions) {
try {
affectedSubscription.consumer.closeAfterStreamDeletion();
} catch (Exception e) {
LOGGER.debug("Error while closing consumer: {}", e.getMessage());
}
}
};
AsyncRetry.asyncRetry(findBrokersForStream(stream))
.description("Candidate lookup to consume from '%s'", stream)
.scheduler(environment.scheduledExecutorService())
.retry(ex -> !(ex instanceof StreamDoesNotExistException))
.delayPolicy(delayPolicy)
.build()
.thenAccept(
candidateNodes -> {
List<Broker> candidates = candidateNodes;
if (candidates == null) {
LOGGER.debug("No candidate nodes to consume from '{}'", stream);
consumersClosingCallback.run();
} else {
for (SubscriptionTracker affectedSubscription : subscriptions) {
maybeRecoverSubscription(candidates, affectedSubscription);
}
if (maybeCloseClient) {
this.closeIfEmpty();
}
}
})
.exceptionally(
ex -> {
LOGGER.debug(
"Error while trying to assign {} consumer(s) to {}",
subscriptions.size(),
stream,
ex);
consumersClosingCallback.run();
if (maybeCloseClient) {
this.closeIfEmpty();
}
return null;
});
}
private void maybeRecoverSubscription(List<Broker> candidates, SubscriptionTracker tracker) {
if (tracker.compareAndSet(SubscriptionState.ACTIVE, SubscriptionState.RECOVERING)) {
try {
recoverSubscription(candidates, tracker);
} catch (Exception e) {
LOGGER.warn(
"Error while recovering consumer {} from stream '{}'. Reason: {}",
tracker.consumer.id(),
tracker.stream,
Utils.exceptionMessage(e));
}
} else {
LOGGER.debug(
"Not recovering consumer {} from stream {}, state is {}, expected is {}",
tracker.consumer.id(),
tracker.stream,
tracker.state(),
SubscriptionState.ACTIVE);
}
}
private void recoverSubscription(List<Broker> candidates, SubscriptionTracker tracker) {
boolean reassignmentCompleted = false;
while (!reassignmentCompleted) {
try {
if (tracker.consumer.isOpen()) {
Broker broker = pickBroker(candidates);
LOGGER.debug("Using {} to resume consuming from {}", broker, tracker.stream);
synchronized (tracker.consumer) {
if (tracker.consumer.isOpen()) {
OffsetSpecification offsetSpecification;
if (tracker.hasReceivedSomething) {
offsetSpecification = OffsetSpecification.offset(tracker.offset);
} else {
offsetSpecification = tracker.initialOffsetSpecification;
}
addToManager(broker, tracker, offsetSpecification, false);
}
}
} else {
LOGGER.debug(
"Not re-assigning consumer {} (stream '{}') because it has been closed",
tracker.consumer.id(),
tracker.stream);
}
reassignmentCompleted = true;
} catch (ConnectionStreamException
| ClientClosedException
| StreamNotAvailableException e) {
LOGGER.debug(
"Consumer {} re-assignment on stream {} timed out or connection closed or stream not available, "
+ "refreshing candidates and retrying",
tracker.consumer.id(),
tracker.stream);
// maybe not a good candidate, let's refresh and retry for this one
candidates =
Utils.callAndMaybeRetry(
findBrokersForStream(tracker.stream),
ex -> !(ex instanceof StreamDoesNotExistException),
recoveryBackOffDelayPolicy(),
"Candidate lookup to consume from '%s' (subscription recovery)",
tracker.stream);
} catch (Exception e) {
LOGGER.warn("Error while re-assigning subscription from stream {}", tracker.stream, e);
reassignmentCompleted = true;
}
}
}
private void checkNotClosed() {
if (!this.client.isOpen()) {
throw new ClientClosedException();
}
}
synchronized void add(
SubscriptionTracker subscriptionTracker,
OffsetSpecification offsetSpecification,
boolean isInitialSubscription) {
if (this.isFull()) {
LOGGER.debug(
"Cannot add subscription tracker for stream '{}', manager is full",
subscriptionTracker.stream);
throw new IllegalStateException("Cannot add subscription tracker, the manager is full");
}
if (this.isClosed()) {
LOGGER.debug(
"Cannot add subscription tracker for stream '{}', manager is closed",
subscriptionTracker.stream);
throw new IllegalStateException("Cannot add subscription tracker, the manager is closed");
}
checkNotClosed();
byte subscriptionId = 0;
for (int i = 0; i < MAX_SUBSCRIPTIONS_PER_CLIENT; i++) {
if (subscriptionTrackers.get(i) == null) {
subscriptionId = (byte) i;
break;
}
}
List<SubscriptionTracker> previousSubscriptions = this.subscriptionTrackers;
LOGGER.debug(
"Subscribing to {}, requested offset specification is {}, offset tracking reference is {}, properties are {}",
subscriptionTracker.stream,
offsetSpecification == null ? DEFAULT_OFFSET_SPECIFICATION : offsetSpecification,
subscriptionTracker.offsetTrackingReference,
subscriptionTracker.subscriptionProperties);
try {
// updating data structures before subscribing
// (to make sure they are up-to-date in case message would arrive super fast)
subscriptionTracker.assign(subscriptionId, this);
streamToStreamSubscriptions
.computeIfAbsent(subscriptionTracker.stream, s -> ConcurrentHashMap.newKeySet())
.add(subscriptionTracker);
this.setSubscriptionTrackers(
update(previousSubscriptions, subscriptionId, subscriptionTracker));
String offsetTrackingReference = subscriptionTracker.offsetTrackingReference;
if (offsetTrackingReference != null) {
checkNotClosed();
QueryOffsetResponse queryOffsetResponse =
Utils.callAndMaybeRetry(
() -> client.queryOffset(offsetTrackingReference, subscriptionTracker.stream),
RETRY_ON_TIMEOUT,
"Offset query for consumer %s on stream '%s' (reference %s)",
subscriptionTracker.consumer.id(),
subscriptionTracker.stream,
offsetTrackingReference);
if (queryOffsetResponse.isOk() && queryOffsetResponse.getOffset() != 0) {
if (offsetSpecification != null && isInitialSubscription) {
// subscription call (not recovery), so telling the user their offset specification
// is