-
Notifications
You must be signed in to change notification settings - Fork 214
/
AbstractPubSubSupervisor.java
139 lines (119 loc) · 4.33 KB
/
AbstractPubSubSupervisor.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
/*
* 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.Collection;
import java.util.List;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.pubsub.config.PubSubConfig;
import org.eclipse.ditto.internal.utils.pubsub.ddata.Hashes;
import org.apache.pekko.actor.AbstractActorWithTimers;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.AllForOneStrategy;
import org.apache.pekko.actor.Props;
import org.apache.pekko.actor.SupervisorStrategy;
import org.apache.pekko.event.DiagnosticLoggingAdapter;
import org.apache.pekko.japi.pf.DeciderBuilder;
import org.apache.pekko.japi.pf.ReceiveBuilder;
/**
* Supervisor of actors dealing with pub-sub featuring an all-for-one supervision strategy with delayed restart
* and child actor name disambiguation.
*/
abstract class AbstractPubSubSupervisor extends AbstractActorWithTimers implements Hashes {
/**
* Logger of this actor.
*/
protected final DiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
/**
* The pub-sub config.
*/
protected final PubSubConfig config;
private final List<Integer> seeds;
private int childCounter = 0;
/**
* Create a supervisor actor.
*/
protected AbstractPubSubSupervisor() {
this.config = PubSubConfig.of(getContext().getSystem());
seeds = Hashes.digestStringsToIntegers(config.getSeed(), Hashes.HASH_FAMILY_SIZE);
}
/**
* Behavior for publishing or subscribing.
*
* @return pub- or sub-behavior.
*/
protected abstract Receive createPubSubBehavior();
/**
* Callback for when a child failed.
*
* @param failingChild the child who failed.
*/
protected abstract void onChildFailure(final ActorRef failingChild);
/**
* Start children without regard for previous children.
*/
protected abstract void startChildren();
@Override
public void preStart() {
startChildren();
}
@Override
public Collection<Integer> getSeeds() {
return seeds;
}
@Override
public SupervisorStrategy supervisorStrategy() {
return new AllForOneStrategy(
DeciderBuilder.matchAny(error -> {
final Duration restartDelay = config.getRestartDelay();
log.error(error, "Child <{}> crashed. Restarting all children after <{}>",
getSender(), restartDelay);
scheduleRestartChildren();
onChildFailure(getSender());
return (SupervisorStrategy.Directive) SupervisorStrategy.stop();
}).build());
}
@Override
public Receive createReceive() {
return createPubSubBehavior().orElse(ReceiveBuilder.create()
.matchEquals(Control.RESTART, this::restartChildren)
.build());
}
/**
* Schedule restart for all children.
*/
protected void scheduleRestartChildren() {
if (!getTimers().isTimerActive(Control.RESTART)) {
getTimers().startSingleTimer(Control.RESTART, Control.RESTART, config.getRestartDelay());
}
}
/**
* Start child actor without fear of actor name collision.
* Not thread-safe. Call in actor's thread only.
*
* @param props Props of the child actor.
* @param namePrefix What the child actor's name should start with.
* @return actor reference of the child actor.
*/
protected ActorRef startChild(final Props props, final String namePrefix) {
final String actorName = namePrefix + ++childCounter;
return getContext().watch(getContext().actorOf(props, actorName));
}
private void restartChildren(final Control restart) {
getContext().getChildren().forEach(getContext()::stop);
startChildren();
}
private enum Control {
RESTART
}
}