-
Notifications
You must be signed in to change notification settings - Fork 214
/
AbstractPersistenceSupervisor.java
666 lines (607 loc) · 30.1 KB
/
AbstractPersistenceSupervisor.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
/*
* 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.persistentactors;
import java.text.MessageFormat;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.BiConsumer;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.api.commands.sudo.SudoCommand;
import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.base.model.exceptions.DittoInternalErrorException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeExceptionBuilder;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.base.service.actors.ShutdownBehaviour;
import org.eclipse.ditto.base.service.config.supervision.ExponentialBackOff;
import org.eclipse.ditto.base.service.config.supervision.ExponentialBackOffConfig;
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.internal.utils.namespaces.BlockedNamespaces;
import org.eclipse.ditto.policies.enforcement.pre.PreEnforcerProvider;
import akka.actor.ActorRef;
import akka.actor.PoisonPill;
import akka.actor.Props;
import akka.actor.ReceiveTimeout;
import akka.actor.Status;
import akka.actor.SupervisorStrategy;
import akka.actor.Terminated;
import akka.cluster.sharding.ShardRegion;
import akka.japi.pf.ReceiveBuilder;
import akka.pattern.Patterns;
/**
* Sharded Supervisor of persistent actors. It:
* <ol>
* <li>restarts failed child actor after exponential backoff,</li>
* <li>shuts down self on command, and</li>
* <li>handles initialization errors by becoming corrupted for a time.</li>
* </ol>
*
* @param <E> the type of the EntityId
* @param <S> the type of the Signal
*/
public abstract class AbstractPersistenceSupervisor<E extends EntityId, S extends Signal<?>>
extends AbstractActorWithStashWithTimers {
/**
* Timeout for local actor invocations - a small timeout should be more than sufficient as those are just method
* calls.
*/
protected static final Duration DEFAULT_LOCAL_ASK_TIMEOUT = Duration.ofSeconds(5);
protected static final String PERSISTENCE_ACTOR_NAME = "pa";
protected final DittoDiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
@Nullable protected final BlockedNamespaces blockedNamespaces;
@Nullable protected E entityId;
@Nullable protected ActorRef persistenceActorChild;
@Nullable protected ActorRef enforcerChild;
private final PreEnforcerProvider preEnforcer;
private final ExponentialBackOffConfig exponentialBackOffConfig;
private ExponentialBackOff backOff;
private boolean waitingForStopBeforeRestart = false;
protected AbstractPersistenceSupervisor(@Nullable final BlockedNamespaces blockedNamespaces) {
this(null, null, blockedNamespaces);
}
protected AbstractPersistenceSupervisor(@Nullable final ActorRef persistenceActorChild,
@Nullable final ActorRef enforcerChild,
@Nullable final BlockedNamespaces blockedNamespaces) {
this.persistenceActorChild = persistenceActorChild;
this.enforcerChild = enforcerChild;
this.blockedNamespaces = blockedNamespaces;
preEnforcer = PreEnforcerProvider.get(getContext().getSystem());
exponentialBackOffConfig = getExponentialBackOffConfig();
backOff = ExponentialBackOff.initial(exponentialBackOffConfig);
}
/**
* @return ID of the entity this actor supervises.
* @throws Exception if entity ID of this actor cannot be determined.
*/
protected abstract E getEntityId() throws Exception;
/**
* Get the props of the supervised persistence actor.
*
* @param entityId entity ID of this actor.
* @return props of the child actor.
*/
protected abstract Props getPersistenceActorProps(E entityId);
/**
* Get the props of the supervised persistence enforcer actor.
*
* @param entityId entity ID of this actor.
* @return props of the child actor.
*/
protected abstract Props getPersistenceEnforcerProps(E entityId);
/**
* Read background configuration from actor context.
* Called in constructor.
* DO NOT rely on instance fields as they will not be initialized.
*
* @return exponential backoff configuration read from the actor system's settings.
*/
protected abstract ExponentialBackOffConfig getExponentialBackOffConfig();
/**
* Get the shutdown behavior appropriate for this actor.
*
* @param entityId ID of the entity this actor supervises.
* @return the shutdown behavior.
*/
protected abstract ShutdownBehaviour getShutdownBehaviour(E entityId);
/**
* Whether to start child actor immediately in {@link #preStart()} method or wait for {@link Control#INIT_DONE}
* message to start supervised child actor.
*
* @return {@code true} if child actor is started in {@link #preStart()} method or {@code false} if the
* implementation signals finished initialization with {@link Control#INIT_DONE} message. Default is {@code true}.
*/
protected boolean isStartChildImmediately() {
return true;
}
protected Receive activeBehaviour() {
return ReceiveBuilder.create()
.match(Terminated.class, this::childTerminated)
.matchEquals(Control.START_CHILDREN, this::startChildren)
.matchEquals(Control.PASSIVATE, this::passivate)
.match(SudoCommand.class, this::forwardSudoCommandToChildIfAvailable)
.match(WithDittoHeaders.class, w -> w.getDittoHeaders().isSudo(),
this::forwardDittoSudoToChildIfAvailable)
.matchAny(this::enforceAndForwardToTargetActor)
.build();
}
/**
* Create a builder for an exception to report unavailability of the entity.
*
* @param entityId the entity ID, or null if the actor is corrupted.
* @return the exception builder.
*/
protected abstract DittoRuntimeExceptionBuilder<?> getUnavailableExceptionBuilder(@Nullable E entityId);
/**
* Hook for modifying an EnforcerActor enforced command before it gets sent to the PersistenceActor.
*
* @param enforcedCommand the already enforced command to potentially modify.
* @return the potentially modified command.
*/
protected CompletionStage<Object> modifyEnforcerActorEnforcedSignalResponse(final Object enforcedCommand) {
return CompletableFuture.completedStage(enforcedCommand);
}
/**
* Hook for modifying a PersistenceActor command response before it gets sent to the EnforcerActor again for
* filtering.
*
* @param enforcedSignal the already enforced signal which was sent to the PersistenceActor.
* @param persistenceCommandResponse the command response sent by the PersistenceActor to potentially modify.
* @return the potentially modified command response.
*/
protected CompletionStage<Object> modifyTargetActorCommandResponse(final Signal<?> enforcedSignal,
final Object persistenceCommandResponse) {
return CompletableFuture.completedStage(persistenceCommandResponse);
}
/**
* Return a preferably static supervisor strategy for this actor. By default, child actor is stopped when killed
* or failing, triggering restart after exponential back-off.
* Overriding method should return a static object if possible to conserve memory.
*
* @return The default supervisor strategy.
*/
@Override
public SupervisorStrategy supervisorStrategy() {
return SupervisorStrategy.stoppingStrategy();
}
@Override
public void preStart() throws Exception {
super.preStart();
try {
entityId = getEntityId();
if (isStartChildImmediately()) {
getSelf().tell(Control.INIT_DONE, getSelf());
} else {
log.debug("Not starting child actor, waiting for initialization to be finished.");
}
} catch (final Exception e) {
log.error(e, "Failed to determine entity ID; becoming corrupted.");
becomeCorrupted();
}
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.matchEquals(Control.INIT_DONE, initDone -> {
entityId = getEntityId();
startChildren(Control.START_CHILDREN);
unstashAll();
becomeActive(getShutdownBehaviour(entityId));
})
.matchAny(this::handleMessagesDuringStartup)
.build();
}
/**
* Become corrupted because this actor cannot start.
*/
protected void becomeCorrupted() {
getContext().setReceiveTimeout(getCorruptedReceiveTimeout());
getContext().become(ReceiveBuilder.create()
.match(ReceiveTimeout.class, timeout -> {
getContext().cancelReceiveTimeout();
passivate(Control.PASSIVATE);
})
.matchAny(message -> replyUnavailableException(message, getSender()))
.build());
}
/**
* Asks the {@link org.eclipse.ditto.policies.enforcement.AbstractEnforcerActor} child the passed {@code signal},
* responding with a CompletionStage which is completed with the response.
* As the enforcer child is always located on the same node, this is a local method call, so the applied ask timeout
* is very low.
*
* @param signal the signal to ask the enforcer.
* @return the completion stage with the response (the enforced signal) or a failed stage when e.g. enforcement
* failed due to lacking permissions.
*/
protected CompletionStage<Object> askEnforcerChild(final Signal<?> signal) {
return Patterns.ask(enforcerChild, signal, DEFAULT_LOCAL_ASK_TIMEOUT);
}
/**
* Asks the "target actor" (being either the {@link AbstractPersistenceActor} for "twin" commands or e.g. a
* pub/sub actor reference for "live" commands/messages) - which is determined by
* {@link #getTargetActorForSendingEnforcedMessageTo(Object, akka.actor.ActorRef)} - the passed {@code message}.
*
* @param message the message to ask the target actor.
* @param sender the sender which originally sent the message.
* @param <T> the type of the message.
* @return the completion stage with the response for the message or a failed stage.
*/
protected <T> CompletionStage<Object> askTargetActor(final T message, final ActorRef sender) {
return getTargetActorForSendingEnforcedMessageTo(message, sender)
.thenCompose(this::askOrForwardToTargetActor)
.thenApply(response -> {
if (null == response) {
throw getUnavailableExceptionBuilder(entityId)
.dittoHeaders(getDittoHeaders(message))
.build();
} else {
return response;
}
});
}
private CompletionStage<Object> askOrForwardToTargetActor(
@Nullable final TargetActorWithMessage targetActorWithMessage) {
if (null != targetActorWithMessage) {
if (!targetActorWithMessage.messageTimeout().isZero()) {
return Patterns.ask(
targetActorWithMessage.targetActor(),
targetActorWithMessage.message(),
targetActorWithMessage.messageTimeout()
)
.thenApply(targetActorWithMessage.responseOrErrorConverter())
.exceptionally(throwable -> targetActorWithMessage.responseOrErrorConverter().apply(throwable));
} else {
targetActorWithMessage.targetActor().tell(targetActorWithMessage.message(), getSelf());
return CompletableFuture.completedStage(new Status.Success(MessageFormat.format(
"message <{0}> sent via tell", targetActorWithMessage.message().getClass().getSimpleName()))
);
}
} else {
return CompletableFuture.completedStage(null);
}
}
/**
* Determines the {@link TargetActorWithMessage} for sending the passed enforced {@code message} to.
* May be overwritten by implementations to determine the target actor in a different way.
*
* @param message the message to determine the target actor for.
* @param sender the sender which originally sent the message.
* @param <T> the type of the message.
* @return the completion stage with the determined {@link TargetActorWithMessage} which includes the target actor
* and the message to send it to
*/
protected <T> CompletionStage<TargetActorWithMessage> getTargetActorForSendingEnforcedMessageTo(final T message,
final ActorRef sender) {
if (null != persistenceActorChild) {
return CompletableFuture.completedStage(
new TargetActorWithMessage(
persistenceActorChild,
message,
DEFAULT_LOCAL_ASK_TIMEOUT,
Function.identity()
));
} else {
return CompletableFuture.completedStage(null);
}
}
private static DittoHeaders getDittoHeaders(final Object message) {
return message instanceof WithDittoHeaders wdh ? wdh.getDittoHeaders() : DittoHeaders.empty();
}
private void becomeActive(final ShutdownBehaviour shutdownBehaviour) {
getContext().become(shutdownBehaviour.createReceive().build()
.orElse(activeBehaviour()));
}
private void passivate(final Control passivationTrigger) {
getContext().cancelReceiveTimeout();
getContext().getParent().tell(new ShardRegion.Passivate(PoisonPill.getInstance()), getSelf());
}
private void startChildren(final Control startChild) {
ensurePersistenceActorBeingStarted();
ensureEnforcerActorBeingStarted();
}
private void ensurePersistenceActorBeingStarted() {
if (null == persistenceActorChild) {
log.debug("Starting persistence actor for entity with ID <{}>.", entityId);
assert entityId != null;
final ActorRef paRef = getContext().actorOf(getPersistenceActorProps(entityId), PERSISTENCE_ACTOR_NAME);
persistenceActorChild = getContext().watch(paRef);
} else {
log.debug("Not starting persistence child actor because it is started already.");
}
}
private void ensureEnforcerActorBeingStarted() {
if (null == enforcerChild) {
log.debug("Starting enforcer actor for entity with ID <{}>.", entityId);
assert entityId != null;
final ActorRef enRef = getContext().actorOf(getPersistenceEnforcerProps(entityId), "en");
enforcerChild = getContext().watch(enRef);
log.debug("Now watching enforcer child: <{}>", enRef);
} else {
log.debug("Not starting persistence enforcer child actor because it is started already.");
}
}
protected void restartChild() {
if (persistenceActorChild != null) {
waitingForStopBeforeRestart = true;
getContext().stop(persistenceActorChild); // start happens when "Terminated" message is received.
}
}
private void childTerminated(final Terminated message) {
if (message.getActor().equals(persistenceActorChild)) {
persistenceActorChild = null;
if (waitingForStopBeforeRestart) {
log.info("Persistence actor for entity with ID <{}> was stopped and will now be started again.",
entityId);
self().tell(Control.START_CHILDREN, ActorRef.noSender());
} else {
if (message.getAddressTerminated()) {
log.error("Persistence actor for entity with ID <{}> terminated abnormally " +
"because it crashed or because of network failure!", entityId);
} else {
log.warning("Persistence actor for entity with ID <{}> terminated abnormally.", entityId);
}
backOff = backOff.calculateNextBackOff();
final Duration restartDelay = backOff.getRestartDelay();
getTimers().startSingleTimer(Control.START_CHILDREN, Control.START_CHILDREN, restartDelay);
}
} else if (message.getActor().equals(enforcerChild)) {
enforcerChild = null;
// simply restart the enforcer actor
ensureEnforcerActorBeingStarted();
}
}
private Duration getCorruptedReceiveTimeout() {
return randomize(exponentialBackOffConfig.getCorruptedReceiveTimeout(),
exponentialBackOffConfig.getRandomFactor());
}
/**
* Return a random duration between the base duration and {@code (1 + randomFactor)} times the base duration.
*
* @param base the base duration.
* @param randomFactor the random factor.
* @return the random duration.
*/
private static Duration randomize(final Duration base, final double randomFactor) {
final double multiplier = 1.0 + ThreadLocalRandom.current().nextDouble() * randomFactor;
return Duration.ofMillis((long) (base.toMillis() * multiplier));
}
/**
* Forward all SudoCommand directly (bypassing enforcer) to the child if it is active or by reply immediately with
* an exception if the child has terminated (fail fast).
*/
private void forwardSudoCommandToChildIfAvailable(final SudoCommand<?> sudoCommand) {
if (null != persistenceActorChild) {
if (persistenceActorChild.equals(getSender())) {
log.withCorrelationId(sudoCommand)
.warning("Received unhandled SudoCommand from persistenceActorChild '{}': {}", entityId,
sudoCommand);
unhandled(sudoCommand);
} else {
persistenceActorChild.forward(sudoCommand, getContext());
}
} else {
replyUnavailableException(sudoCommand, getSender());
}
}
private void forwardDittoSudoToChildIfAvailable(final WithDittoHeaders withDittoHeaders) {
if (null != persistenceActorChild) {
if (persistenceActorChild.equals(getSender())) {
log.withCorrelationId(withDittoHeaders)
.warning("Received unhandled WithDittoHeaders from persistenceActorChild '{}': {}", entityId,
withDittoHeaders);
unhandled(withDittoHeaders);
} else {
persistenceActorChild.forward(withDittoHeaders, getContext());
}
} else {
replyUnavailableException(withDittoHeaders, getSender());
}
}
/**
* Forward all messages to the persistenceActorChild (after applied enforcement) if it is active or by reply
* immediately with an exception if the child has terminated (fail fast).
*/
private void enforceAndForwardToTargetActor(final Object message) {
final ActorRef sender = getSender();
if (message instanceof Signal<?> signal) {
if (sender.equals(persistenceActorChild)) {
log.withCorrelationId(signal)
.warning("Received unhandled message from persistenceActorChild '{}': {}",
entityId, message);
unhandled(message);
} else if (sender.equals(enforcerChild)) {
log.withCorrelationId(signal)
.warning("Received unhandled message from enforcerChild '{}': {}",
entityId, message);
unhandled(message);
} else {
enforceSignalAndForwardToTargetActor((S) signal, sender)
.whenComplete(handleSignalEnforcementResponse(signal, sender));
}
} else if (null != persistenceActorChild) {
if (persistenceActorChild.equals(sender)) {
log.withCorrelationId(message instanceof WithDittoHeaders withDittoHeaders ? withDittoHeaders : null)
.warning("Received unhandled message from persistenceActorChild '{}': {}", entityId, message);
unhandled(message);
} else {
persistenceActorChild.forward(message, getContext());
}
} else {
replyUnavailableException(message, sender);
}
}
private BiConsumer<Object, Throwable> handleSignalEnforcementResponse(final WithDittoHeaders signal,
final ActorRef sender) {
return (response, throwable) -> {
if (null != throwable) {
final DittoRuntimeException dre =
DittoRuntimeException.asDittoRuntimeException(throwable, t -> {
log.withCorrelationId(signal)
.warning("Encountered Throwable when interacting with enforcer " +
"or target actor, telling sender: {}", throwable);
return DittoInternalErrorException.newBuilder()
.dittoHeaders(signal.getDittoHeaders())
.cause(t)
.build();
});
log.withCorrelationId(dre)
.info("Received DittoRuntimeException during enforcement or " +
"forwarding to target actor, telling sender: {}", dre);
sender.tell(dre, getSelf());
} else if (response instanceof Status.Success success) {
log.debug("Ignoring Status.Success message as expected 'to be ignored' outcome: <{}>", success);
} else if (null != response) {
sender.tell(response, getSelf());
} else {
log.withCorrelationId(signal)
.error("Received nothing when enforcing signal and forwarding to " +
"target actor - this should not happen.");
replyUnavailableException(signal, sender);
}
};
}
/**
* All signals are treated in the following way:
* <ul>
* <li>they are sent to the enforcer child which enforces/applies authorization of the signal</li>
* <li>after successful enforcement, they are optionally modified in {@link #modifyEnforcerActorEnforcedSignalResponse(Object)}</li>
* <li>afterwards, the enforced signal is sent to the persistence actor child in
* {@link #enforcerResponseToTargetActor(org.eclipse.ditto.base.model.headers.DittoHeaders, Object, akka.actor.ActorRef)}</li>
* <li>the persistence actor's response is handled in
* {@link #filterTargetActorResponseViaEnforcer(org.eclipse.ditto.internal.utils.persistentactors.AbstractPersistenceSupervisor.EnforcedSignalAndTargetActorResponse)}
* where the enforcer applies optionally filtering of the response</li>
* <li>the result is returned in the CompletionStage</li>
* </ul>
*
* @param signal the signal to enforce and forward to the target actor
* @param sender the original sender of the signal.
* @return a successful CompletionStage with the signal response or a failed stage with a DittoRuntimeException as
* cause
*/
protected CompletionStage<Object> enforceSignalAndForwardToTargetActor(final S signal, final ActorRef sender) {
if (null != enforcerChild) {
return preEnforcer.apply(signal).thenCompose(preEnforcedSignal ->
askEnforcerChild(preEnforcedSignal)
.thenCompose(this::modifyEnforcerActorEnforcedSignalResponse)
.thenCompose(enforcedCommand -> enforcerResponseToTargetActor(
preEnforcedSignal.getDittoHeaders(),
enforcedCommand,
sender
))
.thenCompose(this::filterTargetActorResponseViaEnforcer)
);
} else {
log.withCorrelationId(signal)
.error("Could not enforce signal because enforcerChild was not present");
return CompletableFuture.completedStage(null);
}
}
private CompletionStage<EnforcedSignalAndTargetActorResponse> enforcerResponseToTargetActor(
final DittoHeaders dittoHeaders,
@Nullable final Object enforcerResponse,
final ActorRef sender
) {
if (null == persistenceActorChild) {
throw getUnavailableExceptionBuilder(entityId)
.dittoHeaders(dittoHeaders)
.build();
} else if (enforcerResponse instanceof Signal<?> enforcedSignal) {
log.withCorrelationId(enforcedSignal)
.debug("Received enforcedSignal from enforcerChild, forwarding to target actor: {}",
enforcedSignal);
return askTargetActor(enforcedSignal, sender)
.thenCompose(response ->
modifyTargetActorCommandResponse(enforcedSignal, response))
.thenApply(response ->
new EnforcedSignalAndTargetActorResponse(enforcedSignal, response)
);
} else if (enforcerResponse instanceof DistributedPubWithMessage distributedPubWithMessage) {
return askTargetActor(distributedPubWithMessage, sender)
.thenCompose(response ->
modifyTargetActorCommandResponse(distributedPubWithMessage.signal(), response))
.thenApply(response ->
new EnforcedSignalAndTargetActorResponse(distributedPubWithMessage.signal(), response)
);
} else if (enforcerResponse instanceof DittoRuntimeException dre) {
log.withCorrelationId(dittoHeaders)
.debug("Received DittoRuntimeException as response from enforcerChild: {}", dre);
throw dre;
} else {
return CompletableFuture.completedStage(new EnforcedSignalAndTargetActorResponse(null, null));
}
}
protected CompletionStage<Object> filterTargetActorResponseViaEnforcer(
final EnforcedSignalAndTargetActorResponse targetActorResponse) {
if (targetActorResponse.response() instanceof CommandResponse<?> commandResponse) {
log.withCorrelationId(commandResponse)
.debug("Received CommandResponse from target actor, " +
"telling enforcerChild to apply response filtering: {}", commandResponse);
return askEnforcerChild(commandResponse);
} else if (targetActorResponse.response() instanceof DittoRuntimeException dre) {
log.withCorrelationId(targetActorResponse.enforcedSignal())
.debug("Received DittoRuntimeException as response from target actor: {}", dre);
throw dre;
} else if (targetActorResponse.response() instanceof Status.Success success) {
log.withCorrelationId(targetActorResponse.enforcedSignal())
.info("Got success message from target actor: {}", success);
return CompletableFuture.completedStage(success);
} else {
log.withCorrelationId(targetActorResponse.enforcedSignal())
.warning("Unexpected response from target actor: {}", targetActorResponse);
return CompletableFuture.completedStage(null);
}
}
private void replyUnavailableException(final Object message, final ActorRef sender) {
log.withCorrelationId(message instanceof WithDittoHeaders withDittoHeaders ? withDittoHeaders : null)
.warning("Received message during downtime of child actor for Entity with ID <{}>: <{}>", entityId,
message);
final DittoRuntimeExceptionBuilder<?> builder = getUnavailableExceptionBuilder(entityId);
if (message instanceof WithDittoHeaders withDittoHeaders) {
builder.dittoHeaders(withDittoHeaders.getDittoHeaders());
}
sender.tell(builder.build(), getSelf());
}
private void handleMessagesDuringStartup(final Object message) {
stash();
log.withCorrelationId(message instanceof WithDittoHeaders withDittoHeaders ? withDittoHeaders : null)
.debug("Stashed received message during startup of supervised PersistenceActor: <{}>",
message.getClass().getSimpleName());
}
/**
* Control message for the supervisor actor.
*/
public enum Control {
/**
* Request for graceful shutdown.
*/
PASSIVATE,
/**
* Request to start child actors.
*/
START_CHILDREN,
/**
* Signals initialization is done, child actors can be started.
*/
INIT_DONE
}
private record EnforcedSignalAndTargetActorResponse(@Nullable Signal<?> enforcedSignal,
@Nullable Object response) {}
}