/
ReconnectActor.java
188 lines (163 loc) · 7.96 KB
/
ReconnectActor.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
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.connectivity.messaging;
import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.connectivity.ConnectionStatus;
import org.eclipse.ditto.services.connectivity.util.ConfigKeys;
import org.eclipse.ditto.services.utils.akka.LogUtil;
import org.eclipse.ditto.signals.commands.connectivity.query.RetrieveConnectionStatus;
import org.eclipse.ditto.signals.commands.connectivity.query.RetrieveConnectionStatusResponse;
import com.typesafe.config.Config;
import akka.NotUsed;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Cancellable;
import akka.actor.Props;
import akka.event.DiagnosticLoggingAdapter;
import akka.japi.pf.ReceiveBuilder;
import akka.stream.ActorMaterializer;
import akka.stream.javadsl.Source;
import scala.concurrent.duration.FiniteDuration;
/**
* Actor which restarts a {@link ConnectionActor} with status
* {@link ConnectionStatus#OPEN} automatically on startup.
* <p>
* This Actor must be created as a cluster singleton as it uses a fixed persistence id.
* </p>
*/
public final class ReconnectActor extends AbstractActor {
/**
* The name of this Actor.
*/
public static final String ACTOR_NAME = "reconnect";
private static final String CORRELATION_ID_PREFIX = "reconnect-actor-triggered:";
private final DiagnosticLoggingAdapter log = LogUtil.obtain(this);
private final ActorMaterializer materializer;
private final ActorRef connectionShardRegion;
private final Supplier<Source<String, NotUsed>> currentPersistenceIdsSourceSupplier;
private final Duration reconnectInitialDelay;
private final Duration reconnectInterval;
private final Duration reconnectRateFrequency;
private final int reconnectRateEntities;
private Cancellable reconnectCheck;
private boolean reconnectInProgress = false;
private ReconnectActor(final ActorRef connectionShardRegion,
final Supplier<Source<String, NotUsed>> currentPersistenceIdsSourceSupplier) {
this.connectionShardRegion = connectionShardRegion;
this.currentPersistenceIdsSourceSupplier = currentPersistenceIdsSourceSupplier;
final Config config = getContext().system().settings().config();
materializer = ActorMaterializer.create(getContext().getSystem());
reconnectInitialDelay = config.getDuration(ConfigKeys.Reconnect.RECONNECT_INITIAL_DELAY);
reconnectInterval = config.getDuration(ConfigKeys.Reconnect.RECONNECT_INTERVAL);
reconnectRateFrequency = config.getDuration(ConfigKeys.Reconnect.RECONNECT_RATE_FREQUENCY);
reconnectRateEntities = config.getInt(ConfigKeys.Reconnect.RECONNECT_RATE_ENTITIES);
}
/**
* Creates Akka configuration object Props for this Actor.
*
* @param connectionShardRegion the shard region of connections.
* @param currentPersistenceIdsSourceSupplier supplier of persistence id sources
* @return the Akka configuration Props object.
*/
public static Props props(final ActorRef connectionShardRegion,
final Supplier<Source<String, NotUsed>> currentPersistenceIdsSourceSupplier) {
return Props.create(ReconnectActor.class, connectionShardRegion, currentPersistenceIdsSourceSupplier);
}
private Cancellable scheduleReconnect() {
final FiniteDuration initialDelay =
FiniteDuration.apply(reconnectInitialDelay.toMillis(), TimeUnit.MILLISECONDS);
final FiniteDuration interval = FiniteDuration.apply(reconnectInterval.toMillis(), TimeUnit.MILLISECONDS);
final ReconnectConnections message = ReconnectConnections.INSTANCE;
log.info("Scheduling reconnect for all connections with initial delay {} and interval {}.",
reconnectInitialDelay, reconnectInterval);
return getContext().getSystem()
.scheduler()
.schedule(initialDelay, interval, getSelf(), message, getContext().dispatcher(), ActorRef.noSender());
}
@Override
public void preStart() throws Exception {
super.preStart();
reconnectCheck = scheduleReconnect();
}
@Override
public void postStop() throws Exception {
if (null != reconnectCheck) {
reconnectCheck.cancel();
}
super.postStop();
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(RetrieveConnectionStatusResponse.class,
command -> log.debug("Retrieved connection status response for connection {} with status: {}",
command.getConnectionId(), command.getConnectionStatus()))
.matchEquals(ReconnectConnections.INSTANCE, rc -> this.handleReconnectConnections())
.matchAny(m -> {
log.warning("Unknown message: {}", m);
unhandled(m);
}).build();
}
private void handleReconnectConnections() {
if (reconnectInProgress) {
log.info("Another reconnect iteration is currently in progress. Next iteration is stated after {}.",
reconnectInterval);
} else {
log.info("Sending reconnects for Connections. " +
"Will be sent again after the configured Interval of {}.", reconnectInterval);
reconnectInProgress = true;
final Source<String, NotUsed> currentPersistenceIdsSource = currentPersistenceIdsSourceSupplier.get();
if (currentPersistenceIdsSource != null) {
currentPersistenceIdsSource
.throttle(reconnectRateEntities, reconnectRateFrequency)
.runForeach(this::reconnect, materializer)
.thenRun(() -> {
log.info("Sending reconnects completed.");
reconnectInProgress = false;
});
} else {
log.warning("Failed to create new persistence id source for connection recovery.");
}
}
}
private void reconnect(final String persistenceId) {
// yes, this is intentionally a RetrieveConnectionStatus instead of OpenConnection.
// ConnectionActor manages its own reconnection on recovery.
// OpenConnection would set desired state to OPEN even for deleted connections.
//
// Save connection ID as correlation ID so that nonexistent connections are removed from the store.
if (persistenceId.startsWith(ConnectionActor.PERSISTENCE_ID_PREFIX)) {
final String connectionId = persistenceId.substring(ConnectionActor.PERSISTENCE_ID_PREFIX.length());
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.correlationId(toCorrelationId(connectionId))
.build();
log.debug("Sending a reconnect for Connection {}", connectionId);
connectionShardRegion.tell(RetrieveConnectionStatus.of(connectionId, dittoHeaders), getSelf());
} else {
log.debug("Unknown persistence id '{}', ignoring.", persistenceId);
}
}
static String toCorrelationId(final String connectionId) {
return CORRELATION_ID_PREFIX + connectionId;
}
static Optional<String> toConnectionId(final String correlationId) {
return correlationId.startsWith(CORRELATION_ID_PREFIX)
? Optional.of(correlationId.replace(CORRELATION_ID_PREFIX, ""))
: Optional.empty();
}
enum ReconnectConnections {
INSTANCE
}
}