-
Notifications
You must be signed in to change notification settings - Fork 215
/
AbstractPersistenceSupervisor.java
538 lines (484 loc) · 23.7 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
/*
* 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.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ThreadLocalRandom;
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.commands.Command;
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.config.DefaultScopedConfig;
import org.eclipse.ditto.internal.utils.namespaces.BlockedNamespaces;
import org.eclipse.ditto.policies.enforcement.CreationRestrictionEnforcer;
import org.eclipse.ditto.policies.enforcement.DefaultCreationRestrictionEnforcer;
import org.eclipse.ditto.policies.enforcement.PreEnforcer;
import org.eclipse.ditto.policies.enforcement.config.DefaultEntityCreationConfig;
import akka.actor.ActorRef;
import akka.actor.PoisonPill;
import akka.actor.Props;
import akka.actor.ReceiveTimeout;
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
*/
public abstract class AbstractPersistenceSupervisor<E extends EntityId> 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 final DittoDiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
@Nullable protected final BlockedNamespaces blockedNamespaces;
protected final PreEnforcer preEnforcer;
protected final CreationRestrictionEnforcer creationRestrictionEnforcer;
@Nullable protected E entityId;
@Nullable protected ActorRef persistenceActorChild;
@Nullable protected ActorRef enforcerChild;
private final ExponentialBackOffConfig exponentialBackOffConfig;
private ExponentialBackOff backOff;
private boolean waitingForStopBeforeRestart = false;
protected AbstractPersistenceSupervisor(@Nullable final BlockedNamespaces blockedNamespaces,
final PreEnforcer preEnforcer) {
this(null, null, blockedNamespaces, preEnforcer);
}
protected AbstractPersistenceSupervisor(@Nullable final ActorRef persistenceActorChild,
@Nullable final ActorRef enforcerChild,
@Nullable final BlockedNamespaces blockedNamespaces,
final PreEnforcer preEnforcer) {
this.persistenceActorChild = persistenceActorChild;
this.enforcerChild = enforcerChild;
this.blockedNamespaces = blockedNamespaces;
this.preEnforcer = preEnforcer;
exponentialBackOffConfig = getExponentialBackOffConfig();
backOff = ExponentialBackOff.initial(exponentialBackOffConfig);
creationRestrictionEnforcer = DefaultCreationRestrictionEnforcer.of(
DefaultEntityCreationConfig.of(
DefaultScopedConfig.dittoScoped(getContext().getSystem().settings().config())
)
);
}
/**
* @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_CHILDS, this::startChilds)
.matchEquals(Control.PASSIVATE, this::passivate)
.match(SudoCommand.class, this::forwardSudoCommandToChildIfAvailable)
.match(WithDittoHeaders.class, w -> w.getDittoHeaders().isSudo(),
this::forwardDittoSudoToChildIfAvailable)
.matchAny(this::enforceAndForwardToPersistenceActor)
.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> modifyEnforcerActorEnforcedCommandResponse(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 enforcedCommand the already enforced command 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> modifyPersistenceActorCommandResponse(final Command<?> enforcedCommand,
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();
startChilds(Control.START_CHILDS);
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 -> passivate(Control.PASSIVATE))
.matchAny(this::replyUnavailableException)
.build());
}
private void becomeActive(final ShutdownBehaviour shutdownBehaviour) {
getContext().become(shutdownBehaviour.createReceive().build()
.orElse(activeBehaviour()));
}
private void passivate(final Control passivationTrigger) {
getContext().getParent().tell(new ShardRegion.Passivate(PoisonPill.getInstance()), getSelf());
}
private void startChilds(final Control startChild) {
if (null == persistenceActorChild) {
log.debug("Starting persistence actor for entity with ID <{}>.", entityId);
assert entityId != null;
final ActorRef paRef = getContext().actorOf(getPersistenceActorProps(entityId), "pa");
persistenceActorChild = getContext().watch(paRef);
} else {
log.debug("Not starting persistence child actor because it is started already.");
}
startEnforcerActor();
}
private void startEnforcerActor() {
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);
} 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)) {
if (waitingForStopBeforeRestart) {
log.info("Persistence actor for entity with ID <{}> was stopped and will now be started again.",
entityId);
persistenceActorChild = null;
self().tell(Control.START_CHILDS, 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);
}
persistenceActorChild = null;
backOff = backOff.calculateNextBackOff();
final Duration restartDelay = backOff.getRestartDelay();
getTimers().startSingleTimer(Control.START_CHILDS, Control.START_CHILDS, restartDelay);
}
} else if (message.getActor().equals(enforcerChild)) {
// simply restart the enforcer actor
startEnforcerActor();
}
}
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);
}
}
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);
}
}
/**
* 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 enforceAndForwardToPersistenceActor(final Object message) {
final ActorRef sender = getSender();
if (message instanceof Command<?> command) {
if (sender.equals(persistenceActorChild)) {
log.withCorrelationId(command)
.warning("Received unhandled message from persistenceActorChild '{}': {}",
entityId, message);
unhandled(message);
} else if (sender.equals(enforcerChild)) {
log.withCorrelationId(command)
.warning("Received unhandled message from enforcerChild '{}': {}",
entityId, message);
unhandled(message);
} else {
// all commands must be enforced by the enforcer child, so ask all commands to it:
enforceCommandAndForwardToPersistenceActorIfAuthorized(sender, command);
}
} 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);
}
}
private CompletionStage<Object> enforceCommandAndForwardToPersistenceActorIfAuthorized(final ActorRef sender,
final Command<?> command) {
if (null != enforcerChild) {
return preEnforcer.apply(command).thenCompose(preEnforcedCommand ->
Patterns.ask(enforcerChild, preEnforcedCommand, DEFAULT_LOCAL_ASK_TIMEOUT)
.thenCompose(this::modifyEnforcerActorEnforcedCommandResponse)
.handle((enforcerResponse, enforcerThrowable) ->
handleEnforcerResponse(sender, enforcerResponse, enforcerThrowable,
preEnforcedCommand.getDittoHeaders())
)
);
} else {
log.withCorrelationId(command)
.error("Could not enforce command because enforcerChild was not present");
return CompletableFuture.completedStage(null);
}
}
private CompletionStage<Object> handleEnforcerResponse(final ActorRef sender,
@Nullable final Object enforcerResponse,
@Nullable final Throwable enforcerThrowable,
final DittoHeaders dittoHeaders) {
if (null != enforcerThrowable) {
log.withCorrelationId(dittoHeaders)
.info("Encountered Throwable when interacting with enforcerChild, telling sender: {}",
enforcerThrowable);
final DittoRuntimeException dre =
DittoRuntimeException.asDittoRuntimeException(enforcerThrowable, throwable ->
DittoInternalErrorException.newBuilder()
.dittoHeaders(dittoHeaders)
.cause(throwable)
.build());
log.withCorrelationId(dre)
.debug("Received DittoRuntimeException from enforcerChild, telling sender: {}", dre);
sender.tell(dre, null != persistenceActorChild ? persistenceActorChild : getSelf());
} else if (null == persistenceActorChild) {
final DittoRuntimeException unavailableException = getUnavailableExceptionBuilder(entityId)
.dittoHeaders(dittoHeaders)
.build();
sender.tell(unavailableException, getSelf());
} else if (enforcerResponse instanceof Command<?> enforcedCommand) {
log.withCorrelationId(enforcedCommand)
.debug("Received enforcedCommand from enforcerChild, forwarding to persistenceActorChild: {}",
enforcedCommand);
return Patterns.ask(persistenceActorChild, enforcedCommand, DEFAULT_LOCAL_ASK_TIMEOUT)
.thenCompose(response -> modifyPersistenceActorCommandResponse(enforcedCommand, response))
.whenComplete((persistenceActorResponse, paThrowable) ->
handlePersistenceActorResponse(sender,
enforcedCommand,
persistenceActorResponse,
paThrowable
)
);
} else if (enforcerResponse instanceof DittoRuntimeException dre) {
log.withCorrelationId(dre)
.debug("Received DittoRuntimeException from enforcerChild, telling sender: {}", dre);
sender.tell(dre, persistenceActorChild);
} else {
log.withCorrelationId(enforcerResponse instanceof WithDittoHeaders wdh ? wdh : null)
.warning("Unexpected response from enforcerChild: {}", enforcerResponse);
}
return CompletableFuture.completedStage(null);
}
private void handlePersistenceActorResponse(final ActorRef sender,
@Nullable final Command<?> enforcedCommand,
@Nullable final Object persistenceActorResponse,
@Nullable final Throwable persistenceActorThrowable) {
assert enforcerChild != null;
if (persistenceActorResponse instanceof CommandResponse<?> commandResponse) {
log.withCorrelationId(commandResponse)
.debug("Received CommandResponse from persistenceActorChild, " +
"telling enforcerChild to apply response filtering: {}", commandResponse);
enforcerChild.tell(commandResponse, sender);
} else if (persistenceActorResponse instanceof DittoRuntimeException dre) {
log.withCorrelationId(enforcedCommand)
.debug("Received DittoRuntimeException as response from persistenceActorChild, " +
"telling sender: {}", dre);
sender.tell(dre, persistenceActorChild);
} else if (null != persistenceActorThrowable) {
log.withCorrelationId(enforcedCommand)
.info("Encountered Throwable when interacting with persistenceActorChild, " +
"telling sender: {}", persistenceActorThrowable);
sender.tell(persistenceActorThrowable, persistenceActorChild);
} else {
log.withCorrelationId(enforcedCommand)
.warning("Unexpected response from persistenceActorChild: {}", persistenceActorResponse);
}
}
private void replyUnavailableException(final Object message) {
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());
}
getSender().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 actor.
*/
START_CHILDS,
/**
* Signals initialization is done, child actors can be started.
*/
INIT_DONE
}
}