-
Notifications
You must be signed in to change notification settings - Fork 215
/
AbstractSubscriber.java
145 lines (128 loc) · 6.15 KB
/
AbstractSubscriber.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
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.pubsub.actors;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.acks.AcknowledgementLabel;
import org.eclipse.ditto.base.model.signals.SignalWithEntityId;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgements;
import org.eclipse.ditto.internal.utils.akka.logging.DittoDiagnosticLoggingAdapter;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.metrics.DittoMetrics;
import org.eclipse.ditto.internal.utils.metrics.instruments.counter.Counter;
import org.eclipse.ditto.internal.utils.pubsub.DistributedAcks;
import org.eclipse.ditto.internal.utils.pubsub.api.LocalAcksChanged;
import org.eclipse.ditto.internal.utils.pubsub.api.PublishSignal;
import org.eclipse.ditto.internal.utils.pubsub.ddata.SubscriptionsReader;
import org.eclipse.ditto.internal.utils.pubsub.ddata.ack.GroupedSnapshot;
import org.eclipse.ditto.internal.utils.pubsub.extractors.AckExtractor;
import org.eclipse.ditto.internal.utils.pubsub.extractors.PubSubTopicExtractor;
import akka.actor.AbstractActorWithTimers;
import akka.actor.ActorRef;
import akka.japi.Pair;
import akka.japi.pf.ReceiveBuilder;
/**
* Super class of actors that distributes messages to local subscribers
*
* @param <T> type of messages.
*/
abstract class AbstractSubscriber<T extends SignalWithEntityId<?>> extends AbstractActorWithTimers {
final DittoDiagnosticLoggingAdapter logger = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
final Class<T> messageClass;
final PubSubTopicExtractor<T> topicExtractor;
final AckExtractor<T> ackExtractor;
final DistributedAcks distributedAcks;
@Nullable ActorRef ackUpdater = null;
@Nullable ActorRef subUpdater = null;
private final Counter truePositiveCounter = DittoMetrics.counter("pubsub-true-positive");
private final Counter falsePositiveCounter = DittoMetrics.counter("pubsub-false-positive");
private final Counter receivedMessagesCounter = DittoMetrics.counter("pubsub-received-messages");
private PublisherIndex<String> publisherIndex = PublisherIndex.empty();
private GroupedSnapshot<ActorRef, String> declaredAcks = GroupedSnapshot.empty();
AbstractSubscriber(final Class<T> messageClass,
final PubSubTopicExtractor<T> topicExtractor,
final AckExtractor<T> ackExtractor,
final DistributedAcks distributedAcks) {
this.messageClass = messageClass;
this.topicExtractor = topicExtractor;
this.ackExtractor = ackExtractor;
this.distributedAcks = distributedAcks;
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(PublishSignal.class, this::broadcastToLocalSubscribers)
.match(SubscriptionsReader.class, this::updateLocalSubscriptions)
.match(LocalAcksChanged.class, this::updateLocalAcks)
.build();
}
void broadcastToLocalSubscribers(final PublishSignal command) {
receivedMessagesCounter.increment();
final T message = messageClass.cast(command.getSignal());
final Collection<String> topics = topicExtractor.getTopics(message);
final Set<ActorRef> localSubscribers =
publisherIndex.assignGroupsToSubscribers(message, topics, command.getGroups())
.stream()
.map(Pair::first)
.collect(Collectors.toSet());
if (localSubscribers.isEmpty()) {
falsePositiveCounter.increment();
} else {
truePositiveCounter.increment();
for (final ActorRef localSubscriber : localSubscribers) {
localSubscriber.tell(message, getSender());
}
}
replyWeakAck(message, command, localSubscribers, getSender());
}
private void replyWeakAck(final T message, final PublishSignal command, final Set<ActorRef> localSubscribers,
final ActorRef sender) {
final Set<String> responsibleAcks = declaredAcks.getValues(command.getGroups().keySet());
final Collection<AcknowledgementLabel> declaredCustomAcks =
ackExtractor.getDeclaredCustomAcksRequestedBy(message, responsibleAcks::contains);
final Collection<AcknowledgementLabel> declaredCustomAcksWithoutSubscribers = declaredCustomAcks.stream()
.filter(label -> disjoint(localSubscribers, declaredAcks.getKeys(label.toString())))
.toList();
if (!declaredCustomAcksWithoutSubscribers.isEmpty()) {
final Acknowledgements acknowledgements =
ackExtractor.toWeakAcknowledgements(message, declaredCustomAcksWithoutSubscribers);
sender.tell(acknowledgements, ActorRef.noSender());
}
}
void updateLocalSubscriptions(final SubscriptionsReader subscriptionsReader) {
this.publisherIndex = PublisherIndex.fromSubscriptionsReader(subscriptionsReader);
// no need to watch the subUpdater -- the supervisor takes care of restarting on termination.
subUpdater = getSender();
}
void updateLocalAcks(final LocalAcksChanged localAcksChanged) {
declaredAcks = localAcksChanged.getSnapshot();
ackUpdater = getSender();
}
private static <T> boolean disjoint(final Set<T> set1, final Set<T> set2) {
final Set<T> smaller;
final Set<T> bigger;
if (set1.size() < set2.size()) {
smaller = set1;
bigger = set2;
} else {
smaller = set2;
bigger = set1;
}
return smaller.stream().noneMatch(bigger::contains);
}
private enum Control {
RECEIVE_LOCAL_DECLARED_ACKS
}
}