-
Notifications
You must be signed in to change notification settings - Fork 214
/
DittoSplitBrainResolver.java
106 lines (89 loc) · 4.06 KB
/
DittoSplitBrainResolver.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
/*
* Copyright (c) 2022 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.base.service.cluster;
import java.time.Duration;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.pekko.actor.AbstractActor;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.Cancellable;
import org.apache.pekko.actor.Props;
import org.apache.pekko.japi.pf.ReceiveBuilder;
/**
* This is just a wrapper for the {@link org.apache.pekko.cluster.sbr.SplitBrainResolver pekko sbr} with an addition, that this
* sbr can be turned off/on via {@link ModifySplitBrainResolver} sent as piggyback command to /system/cluster/core/daemon/downingProvider
*/
final class DittoSplitBrainResolver extends AbstractActor {
private static final String SBR_ENABLING_DELAY = "ditto.cluster.sbr.auto-enable-after";
private static final Duration AUTO_ENABLE_AFTER_FALLBACK = Duration.ofHours(1);
private static final ModifySplitBrainResolver ENABLE = ModifySplitBrainResolver.of(true);
private static final Logger LOGGER = LoggerFactory.getLogger(DittoSplitBrainResolver.class);
private final Props splitBrainResolverProps;
private final Cancellable autoEnabling;
@Nullable
private ActorRef splitBrainResolverActor;
@SuppressWarnings("unused")
private DittoSplitBrainResolver(final Props splitBrainResolverProps) {
this.splitBrainResolverProps = splitBrainResolverProps;
final Duration autoEnableAfter = getAutoEnableAfter();
// Enable automatically after some configurable time
autoEnabling = context().system()
.scheduler()
.scheduleOnce(autoEnableAfter, getSelf(), ENABLE, context().dispatcher(), getSelf());
LOGGER.info("SBR will be automatically enabled after <{}>", autoEnableAfter);
}
static Props props(@Nullable final Props splitBrainResolverProps) {
return Props.create(DittoSplitBrainResolver.class, splitBrainResolverProps);
}
private Duration getAutoEnableAfter() {
try {
return context().system().settings().config().getDuration(SBR_ENABLING_DELAY);
} catch (final Exception ex) {
return AUTO_ENABLE_AFTER_FALLBACK;
}
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(ModifySplitBrainResolver.class, this::updateEnabled)
.matchAny(() -> splitBrainResolverActor != null, this::forward)
.matchAny(this::logDropped)
.build();
}
private ActorRef startChildActor(final Props props) {
return getContext().actorOf(props);
}
private void updateEnabled(final ModifySplitBrainResolver modifySplitBrainResolver) {
if (!autoEnabling.isCancelled()) {
autoEnabling.cancel();
}
if (modifySplitBrainResolver.isEnabled() && splitBrainResolverActor == null) {
LOGGER.info("Enabling Akka split brain resolver");
splitBrainResolverActor = startChildActor(splitBrainResolverProps);
} else if (!modifySplitBrainResolver.isEnabled() && splitBrainResolverActor != null) {
LOGGER.info("Stopping Akka split brain resolver");
getContext().stop(splitBrainResolverActor);
splitBrainResolverActor = null;
}
sender().tell(ModifySplitBrainResolverResponse.of(modifySplitBrainResolver), getSelf());
}
private void forward(final Object obj) {
if (splitBrainResolverActor != null) {
splitBrainResolverActor.forward(obj, getContext());
}
}
private void logDropped(final Object obj) {
LOGGER.info("Dropped message <{}> because split brain resolver is disabled", obj);
}
}