-
Notifications
You must be signed in to change notification settings - Fork 215
/
SubscriptionActor.java
267 lines (236 loc) · 11.2 KB
/
SubscriptionActor.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
/*
* Copyright (c) 2020 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.search;
import java.time.Duration;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.signals.commands.exceptions.DittoInternalErrorException;
import org.eclipse.ditto.internal.utils.akka.actors.AbstractActorWithStashWithTimers;
import org.eclipse.ditto.internal.utils.akka.logging.DittoDiagnosticLoggingAdapter;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.thingsearch.model.signals.commands.ThingSearchCommand;
import org.eclipse.ditto.thingsearch.model.signals.commands.exceptions.SubscriptionProtocolErrorException;
import org.eclipse.ditto.thingsearch.model.signals.commands.exceptions.SubscriptionTimeoutException;
import org.eclipse.ditto.thingsearch.model.signals.commands.subscription.CancelSubscription;
import org.eclipse.ditto.thingsearch.model.signals.commands.subscription.RequestFromSubscription;
import org.eclipse.ditto.thingsearch.model.signals.events.SubscriptionComplete;
import org.eclipse.ditto.thingsearch.model.signals.events.SubscriptionCreated;
import org.eclipse.ditto.thingsearch.model.signals.events.SubscriptionFailed;
import org.eclipse.ditto.thingsearch.model.signals.events.SubscriptionHasNextPage;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import akka.actor.ActorRef;
import akka.actor.PoisonPill;
import akka.actor.Props;
import akka.actor.ReceiveTimeout;
import akka.japi.pf.ReceiveBuilder;
/**
* Actor that translates subscription commands into stream operations and stream signals into subscription events.
*
* @since 1.1.0
*/
public final class SubscriptionActor extends AbstractActorWithStashWithTimers {
/**
* Live on as zombie for a while to prevent timeout at client side
*/
private static final Duration ZOMBIE_LIFETIME = Duration.ofSeconds(10L);
private final DittoDiagnosticLoggingAdapter log;
private Subscription subscription;
private ActorRef sender;
private DittoHeaders dittoHeaders;
SubscriptionActor(final Duration idleTimeout, final ActorRef sender, final DittoHeaders dittoHeaders) {
log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
this.sender = sender;
this.dittoHeaders = dittoHeaders;
getContext().setReceiveTimeout(idleTimeout);
}
/**
* Create Props object for the SubscriptionActor.
*
* @param idleTimeout maximum lifetime while idling
* @param sender sender of the command that created this actor.
* @param dittoHeaders headers of the command that created this actor.
* @return Props for this actor.
*/
public static Props props(final Duration idleTimeout, final ActorRef sender, final DittoHeaders dittoHeaders) {
return Props.create(SubscriptionActor.class, idleTimeout, sender, dittoHeaders);
}
/**
* Wrap a subscription actor as a reactive stream subscriber.
*
* @param subscriptionActor reference to the subscription actor.
* @return the actor presented as a reactive stream subscriber.
*/
public static Subscriber<JsonArray> asSubscriber(final ActorRef subscriptionActor) {
return new SubscriberOps(subscriptionActor);
}
@Override
public void postStop() {
if (subscription != null) {
subscription.cancel();
}
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(RequestFromSubscription.class, this::requestSubscription)
.match(CancelSubscription.class, this::cancelSubscription)
.match(SubscriptionHasNextPage.class, this::subscriptionHasNext)
.match(SubscriptionComplete.class, this::subscriptionComplete)
.match(SubscriptionFailed.class, this::subscriptionFailed)
.match(Subscription.class, this::onSubscribe)
.matchEquals(ReceiveTimeout.getInstance(), this::idleTimeout)
.build();
}
private Receive createZombieBehavior() {
return ReceiveBuilder.create()
.match(RequestFromSubscription.class, requestSubscription -> {
log.withCorrelationId(requestSubscription)
.info("Rejecting RequestSubscription[demand={}] as zombie",
requestSubscription.getDemand());
final String errorMessage =
"This subscription is considered cancelled. No more messages are processed.";
final SubscriptionFailed subscriptionFailed = SubscriptionFailed.of(
getSubscriptionId(),
SubscriptionProtocolErrorException.newBuilder()
.message(errorMessage)
.build(),
requestSubscription.getDittoHeaders()
);
getSender().tell(subscriptionFailed, ActorRef.noSender());
})
.matchAny(message -> log.debug("Ignoring as zombie: <{}>", message))
.build();
}
private void idleTimeout(final ReceiveTimeout receiveTimeout) {
// usually a user error
log.info("Stopping due to idle timeout");
final String subscriptionId = getSubscriptionId();
final SubscriptionTimeoutException error = SubscriptionTimeoutException.of(subscriptionId, dittoHeaders);
final SubscriptionFailed subscriptionFailed = SubscriptionFailed.of(subscriptionId, error, dittoHeaders);
if (subscription == null) {
sender.tell(getSubscriptionCreated(), ActorRef.noSender());
}
sender.tell(subscriptionFailed, ActorRef.noSender());
becomeZombie();
}
private void onSubscribe(final Subscription subscription) {
if (this.subscription != null) {
subscription.cancel();
} else {
this.subscription = subscription;
sender.tell(getSubscriptionCreated(), ActorRef.noSender());
unstashAll();
}
}
private SubscriptionCreated getSubscriptionCreated() {
return SubscriptionCreated.of(getSubscriptionId(), dittoHeaders);
}
private void setSenderAndDittoHeaders(final ThingSearchCommand<?> command) {
sender = getSender();
dittoHeaders = command.getDittoHeaders();
}
private void requestSubscription(final RequestFromSubscription requestFromSubscription) {
if (subscription == null) {
log.withCorrelationId(requestFromSubscription).debug("Stashing <{}>", requestFromSubscription);
stash();
} else {
log.withCorrelationId(requestFromSubscription).debug("Processing <{}>", requestFromSubscription);
setSenderAndDittoHeaders(requestFromSubscription);
subscription.request(requestFromSubscription.getDemand());
}
}
private void cancelSubscription(final CancelSubscription cancelSubscription) {
if (subscription == null) {
log.withCorrelationId(cancelSubscription).info("Stashing <{}>", cancelSubscription);
stash();
} else {
log.withCorrelationId(cancelSubscription).info("Processing <{}>", cancelSubscription);
setSenderAndDittoHeaders(cancelSubscription);
subscription.cancel();
becomeZombie();
}
}
private void subscriptionHasNext(final SubscriptionHasNextPage event) {
log.debug("Forwarding {}", event);
sender.tell(event.setDittoHeaders(dittoHeaders), ActorRef.noSender());
}
private void subscriptionComplete(final SubscriptionComplete event) {
// just in case: if error overtakes subscription, then there *will* be a subscription.
if (subscription == null) {
log.withCorrelationId(event).debug("Stashing <{}>", event);
stash();
} else {
log.info("{}", event);
sender.tell(event.setDittoHeaders(dittoHeaders), ActorRef.noSender());
becomeZombie();
}
}
private void subscriptionFailed(final SubscriptionFailed event) {
// just in case: if error overtakes subscription, then there *will* be a subscription.
if (subscription == null) {
log.withCorrelationId(event).debug("Stashing <{}>", event);
stash();
} else {
// log at INFO level because user errors may cause subscription failure.
log.withCorrelationId(event).info("{}", event);
sender.tell(event.setDittoHeaders(dittoHeaders), ActorRef.noSender());
becomeZombie();
}
}
private void becomeZombie() {
getTimers().startSingleTimer(PoisonPill.getInstance(), PoisonPill.getInstance(), ZOMBIE_LIFETIME);
getContext().become(createZombieBehavior());
}
private String getSubscriptionId() {
return getSelf().path().name();
}
private static final class SubscriberOps implements Subscriber<JsonArray> {
private final ActorRef subscriptionActor;
private final String subscriptionId;
private SubscriberOps(final ActorRef subscriptionActor) {
this.subscriptionActor = subscriptionActor;
subscriptionId = subscriptionActor.path().name();
}
@Override
public void onSubscribe(final Subscription subscription) {
subscriptionActor.tell(subscription, ActorRef.noSender());
}
@Override
public void onNext(final JsonArray items) {
final SubscriptionHasNextPage event = SubscriptionHasNextPage.of(subscriptionId, items, DittoHeaders.empty());
subscriptionActor.tell(event, ActorRef.noSender());
}
@Override
public void onError(final Throwable t) {
final SubscriptionFailed event =
SubscriptionFailed.of(subscriptionId,
DittoRuntimeException.asDittoRuntimeException(t, e -> {
if (e instanceof IllegalArgumentException) {
// incorrect protocol from the client side
return SubscriptionProtocolErrorException.of(e, DittoHeaders.empty());
} else {
return DittoInternalErrorException.newBuilder().cause(e).build();
}
}),
DittoHeaders.empty());
subscriptionActor.tell(event, ActorRef.noSender());
}
@Override
public void onComplete() {
final SubscriptionComplete event = SubscriptionComplete.of(subscriptionId, DittoHeaders.empty());
subscriptionActor.tell(event, ActorRef.noSender());
}
}
}