-
Notifications
You must be signed in to change notification settings - Fork 214
/
Subscriber.java
181 lines (160 loc) · 7.5 KB
/
Subscriber.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
/*
* Copyright (c) 2019 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.time.Duration;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.internal.utils.pubsub.DistributedAcks;
import org.eclipse.ditto.internal.utils.pubsub.PubSubFactory;
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.config.PubSubConfig;
import org.eclipse.ditto.internal.utils.pubsub.ddata.SubscriptionsReader;
import org.eclipse.ditto.internal.utils.pubsub.extractors.AckExtractor;
import org.eclipse.ditto.internal.utils.pubsub.extractors.PubSubTopicExtractor;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSelection;
import org.apache.pekko.actor.OneForOneStrategy;
import org.apache.pekko.actor.Props;
import org.apache.pekko.actor.SupervisorStrategy;
import org.apache.pekko.actor.Terminated;
import org.apache.pekko.cluster.Cluster;
import org.apache.pekko.japi.pf.DeciderBuilder;
import org.apache.pekko.japi.pf.ReceiveBuilder;
/**
* Actor that distributes messages to local subscribers, triggers synchronization of declared acknowledgements,
* and manages the subscriber pool.
*
* @param <T> type of messages.
*/
public final class Subscriber<T extends Signal<?>> extends AbstractSubscriber<T> {
/**
* Prefix of this actor's name.
*/
public static final String ACTOR_NAME_PREFIX = "subscriber";
private final List<ActorRef> subSubscribers;
@SuppressWarnings("unused")
private Subscriber(final Class<T> messageClass,
final PubSubTopicExtractor<T> topicExtractor,
final AckExtractor<T> ackExtractor,
final DistributedAcks distributedAcks) {
super(messageClass, topicExtractor, ackExtractor, distributedAcks);
subSubscribers = new ArrayList<>(distributedAcks.getConfig().getNumberOfShards());
distributedAcks.receiveLocalDeclaredAcks(getSelf());
}
/**
* Create Props object for this actor.
*
* @param <T> type of messages.
* @param messageClass class of message distributed by the pub-sub.
* @param topicExtractor extractor of topics from messages.
* @param ackExtractor extractor of acknowledgement-related information from a message.
* @param distributedAcks access to the distributed data of declared acknowledgement labels.
* @return the Props object.
*/
public static <T> Props props(final Class<T> messageClass, final PubSubTopicExtractor<T> topicExtractor,
final AckExtractor<T> ackExtractor, final DistributedAcks distributedAcks) {
return Props.create(Subscriber.class, messageClass, topicExtractor, ackExtractor, distributedAcks);
}
/**
* Choose a subscriber from the subscriber pool based on the hash code of the entity ID of the published signal.
*
* @param parentSubscriber The parent subscriber from the distributed data.
* @param signal The signal to be published.
* @param poolSize The size of the subscriber pool.
* @return An actor selection containing the subscriber in the pool responsible for the signal.
*/
static ActorSelection chooseSubscriber(final ActorRef parentSubscriber, final PublishSignal signal,
final int poolSize) {
if (poolSize > 1) {
final int index = PubSubFactory.hashForPubSub(signal.getGroupIndexKey()) % poolSize;
if (index > 0) {
return ActorSelection.apply(parentSubscriber, String.valueOf(index));
}
}
return ActorSelection.apply(parentSubscriber, "");
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(PublishSignal.class, this::broadcastToLocalSubscribers)
.match(SubscriptionsReader.class, this::updateLocalSubscriptions)
.match(LocalAcksChanged.class, this::updateLocalAcks)
.match(Terminated.class, this::terminated)
.matchEquals(ActorEvent.ACK_UPDATER_NOT_AVAILABLE, this::scheduleReceiveLocalDeclaredAcks)
.matchEquals(Control.RECEIVE_LOCAL_DECLARED_ACKS, this::receiveLocalDeclaredAcks)
.build();
}
@Override
public SupervisorStrategy supervisorStrategy() {
return new OneForOneStrategy(DeciderBuilder.matchAny(e -> {
logger.error("Unknown error:'{}'! Escalating!", e);
return (SupervisorStrategy.Directive) SupervisorStrategy.escalate();
}).build());
}
@Override
public void preStart() throws Exception {
super.preStart();
final int subscriberPoolSize = distributedAcks.getConfig().getSubscriberPoolSize();
if (subscriberPoolSize > 1) {
final Props props = SubSubscriber.props(messageClass, topicExtractor, ackExtractor, distributedAcks);
for (int i = 1; i < subscriberPoolSize; ++i) {
subSubscribers.add(getContext().actorOf(props, String.valueOf(i)));
}
}
}
@Override
void updateLocalSubscriptions(final SubscriptionsReader subscriptionsReader) {
super.updateLocalSubscriptions(subscriptionsReader);
for (final ActorRef subSubscriber : subSubscribers) {
subSubscriber.forward(subscriptionsReader, getContext());
}
}
@Override
void updateLocalAcks(final LocalAcksChanged localAcksChanged) {
super.updateLocalAcks(localAcksChanged);
getContext().watch(ackUpdater);
for (final ActorRef subSubscriber : subSubscribers) {
subSubscriber.forward(localAcksChanged, getContext());
}
}
private void scheduleReceiveLocalDeclaredAcks(final ActorEvent ackUpdaterNotAvailable) {
if (!timers().isTimerActive(Control.RECEIVE_LOCAL_DECLARED_ACKS)) {
timers().startSingleTimer(Control.RECEIVE_LOCAL_DECLARED_ACKS, Control.RECEIVE_LOCAL_DECLARED_ACKS,
getRestartDelayWithBuffer());
}
}
private void receiveLocalDeclaredAcks(final Control receiveLocalDeclaredAcks) {
distributedAcks.receiveLocalDeclaredAcks(getSelf());
}
private void terminated(final Terminated terminated) {
if (Cluster.get(getContext().getSystem()).isTerminated()) {
logger.info("This cluster instance was terminated - no action required ..");
} else if (terminated.getActor().equals(ackUpdater)) {
logger.error("Notifying SubUpdater <{}> of AckUpdater termination: <{}>", subUpdater, terminated);
if (subUpdater != null) {
subUpdater.tell(ActorEvent.PUBSUB_TERMINATED, getSelf());
}
scheduleReceiveLocalDeclaredAcks(ActorEvent.ACK_UPDATER_NOT_AVAILABLE);
}
}
private Duration getRestartDelayWithBuffer() {
final long bufferFactor = 4;
final Duration configuredRestartDelay = PubSubConfig.of(getContext().getSystem()).getRestartDelay();
return configuredRestartDelay.plus(configuredRestartDelay.dividedBy(bufferFactor));
}
private enum Control {
RECEIVE_LOCAL_DECLARED_ACKS
}
}