-
Notifications
You must be signed in to change notification settings - Fork 214
/
OneForOneEscalateStrategy.java
95 lines (82 loc) · 3.82 KB
/
OneForOneEscalateStrategy.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
/*
* Copyright (c) 2021 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.connectivity.service.messaging.persistence;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkArgument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ChildRestartStats;
import org.apache.pekko.actor.SupervisorStrategy;
import scala.PartialFunction;
import scala.collection.Iterable;
/**
* Implementation of {@link SupervisorStrategy}, which restarts supervised actors {@code maxRetries} times and
* afterwards escalates failures. This stands in contrast to the original {@link org.apache.pekko.actor.OneForOneStrategy}
* which stops instead of escalating.
*/
final class OneForOneEscalateStrategy extends SupervisorStrategy {
private static final Logger LOGGER = LoggerFactory.getLogger(OneForOneEscalateStrategy.class);
private final int maxRetries;
private int currentRetries = 0;
private OneForOneEscalateStrategy(final int maxRetries) {
this.maxRetries = maxRetries;
}
/**
* Create a supervisor strategy which tries to restart failing children {@code maxRetries} and afterwards
* will escalate errors.
*
* @param maxRetries how often a failing child should be restarted.
* @throws IllegalArgumentException if the passed in {@code maxRetries} is negative.
* @return the strategy.
*/
static OneForOneEscalateStrategy withRetries(final int maxRetries) {
checkArgument(maxRetries, max -> max >= 0, () -> "Maximum retries must not be negative.");
return new OneForOneEscalateStrategy(maxRetries);
}
/**
* Create a supervisor strategy which immediately escalates when a child fails.
*
* @return the strategy.
*/
static OneForOneEscalateStrategy escalateStrategy() {
return withRetries(0);
}
@Override
public void handleChildTerminated(final org.apache.pekko.actor.ActorContext context, final ActorRef child,
final Iterable<ActorRef> children) {
LOGGER.debug("Child <{}> terminated. Ignoring. Remaining children: {}", child, children);
}
@Override
public PartialFunction<Throwable, Directive> decider() {
return PartialFunction.fromFunction(throwable -> {
if (currentRetries >= maxRetries) {
LOGGER.info("Child failed <{}> times, which exceeds the maximum allowed number of <{}> failures." +
" Will escalate the failure.", currentRetries, maxRetries, throwable);
return (Directive) SupervisorStrategy.escalate();
}
LOGGER.info("Child failed <{}> times, which is less than the maximum allowed number of <{}> failures." +
" Will restart the child.", currentRetries, maxRetries, throwable);
return (Directive) SupervisorStrategy.restart();
});
}
@Override
public void processFailure(final org.apache.pekko.actor.ActorContext context, final boolean restart,
final ActorRef child,
final Throwable cause, final ChildRestartStats stats, final Iterable<ChildRestartStats> children) {
// ignoring the arguments, because #decider will either escalate and this code is never reached, or it
// will use the restart directive, which will call this method with restart = true.
++currentRetries;
LOGGER.debug("Restarting child <{}>", child);
restartChild(child, cause, false);
}
}