/
ConnectionIdsRetrievalActor.java
244 lines (220 loc) · 12.5 KB
/
ConnectionIdsRetrievalActor.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
/*
* 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;
import static org.eclipse.ditto.connectivity.api.ConnectivityMessagingConstants.CONNECTION_ID_RETRIEVAL_ACTOR_NAME;
import static org.eclipse.ditto.internal.utils.persistence.mongo.streaming.MongoReadJournal.LIFECYCLE;
import java.time.Duration;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import java.util.function.Supplier;
import org.apache.pekko.NotUsed;
import org.apache.pekko.actor.AbstractActor;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.Props;
import org.apache.pekko.cluster.pubsub.DistributedPubSub;
import org.apache.pekko.japi.pf.ReceiveBuilder;
import org.apache.pekko.pattern.Patterns;
import org.apache.pekko.stream.Materializer;
import org.apache.pekko.stream.javadsl.Flow;
import org.apache.pekko.stream.javadsl.Sink;
import org.apache.pekko.stream.javadsl.Source;
import org.bson.Document;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.connectivity.api.commands.sudo.SudoRetrieveConnectionIdsByTag;
import org.eclipse.ditto.connectivity.api.commands.sudo.SudoRetrieveConnectionIdsByTagResponse;
import org.eclipse.ditto.connectivity.model.ConnectionId;
import org.eclipse.ditto.connectivity.model.ConnectivityInternalErrorException;
import org.eclipse.ditto.connectivity.model.signals.commands.ConnectivityErrorResponse;
import org.eclipse.ditto.connectivity.model.signals.commands.query.RetrieveAllConnectionIds;
import org.eclipse.ditto.connectivity.model.signals.commands.query.RetrieveAllConnectionIdsResponse;
import org.eclipse.ditto.connectivity.model.signals.events.ConnectionDeleted;
import org.eclipse.ditto.connectivity.service.config.ConnectionIdsRetrievalConfig;
import org.eclipse.ditto.connectivity.service.messaging.persistence.ConnectionPersistenceActor;
import org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoDiagnosticLoggingAdapter;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.persistence.mongo.streaming.MongoReadJournal;
/**
* Actor handling messages related to connections e.g. retrieving all connections ids.
*/
public final class ConnectionIdsRetrievalActor extends AbstractActor {
/**
* The name of this Actor.
*/
public static final String ACTOR_NAME = CONNECTION_ID_RETRIEVAL_ACTOR_NAME;
private static final String PERSISTENCE_ID_FIELD = "_id";
private final DittoDiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
private final Supplier<Source<Document, NotUsed>> persistenceIdsFromJournalSourceSupplier;
private final Supplier<Source<Document, NotUsed>> persistenceIdsFromSnapshotSourceSupplier;
private final Materializer materializer;
private final Function<String, Source<String, NotUsed>> taggedPidSourceFunction;
@SuppressWarnings("unused")
private ConnectionIdsRetrievalActor(final MongoReadJournal readJournal,
final ConnectionIdsRetrievalConfig connectionIdsRetrievalConfig) {
materializer = Materializer.createMaterializer(this::getContext);
persistenceIdsFromJournalSourceSupplier =
() -> readJournal.getLatestJournalEntries(connectionIdsRetrievalConfig.getReadJournalBatchSize(),
Duration.ofSeconds(1), materializer);
persistenceIdsFromSnapshotSourceSupplier =
() -> readJournal.getNewestSnapshotsAbove("", connectionIdsRetrievalConfig.getReadSnapshotBatchSize(),
materializer);
taggedPidSourceFunction =
tag -> readJournal.getJournalPidsWithTag(tag, connectionIdsRetrievalConfig.getReadJournalBatchSize(),
Duration.ofSeconds(1), materializer, true);
}
@Override
public void preStart() {
final var actorSystem = getContext().getSystem();
final var pubSubMediator = DistributedPubSub.get(actorSystem).mediator();
final var self = getSelf();
pubSubMediator.tell(DistPubSubAccess.put(self), self);
}
/**
* Creates Pekko configuration object Props for this Actor.
*
* @param readJournal readJournal to extract current PIDs from.
* @param connectionIdsRetrievalConfig the config to build the pid suppliers from.
* @return the Pekko configuration Props object.
*/
public static Props props(final MongoReadJournal readJournal,
final ConnectionIdsRetrievalConfig connectionIdsRetrievalConfig) {
return Props.create(ConnectionIdsRetrievalActor.class, readJournal, connectionIdsRetrievalConfig);
}
private static boolean isDeleted(final Document document) {
return Optional.ofNullable(document.getString(MongoReadJournal.J_EVENT_MANIFEST))
.map(ConnectionDeleted.TYPE::equals)
.orElse(true);
}
private static boolean isNotDeleted(final Document document) {
return Optional.ofNullable(document.getString(MongoReadJournal.J_EVENT_MANIFEST))
.map(manifest -> !ConnectionDeleted.TYPE.equals(manifest))
.orElse(false);
}
private static boolean snapshotIsNotDeleted(final Document document) {
return Optional.ofNullable(document.getString(LIFECYCLE))
.map(lifecycle -> !"DELETED".equals(lifecycle))
.orElse(false);
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(RetrieveAllConnectionIds.class, this::getAllConnectionIDs)
.match(SudoRetrieveConnectionIdsByTag.class, this::getConnectionIDsByTag)
.matchAny(m -> {
log.warning("Unknown message: {}", m);
unhandled(m);
}).build();
}
private void getConnectionIDsByTag(final SudoRetrieveConnectionIdsByTag sudoRetrieveConnectionIdsByTag) {
final DittoHeaders dittoHeaders = sudoRetrieveConnectionIdsByTag.getDittoHeaders();
final DittoDiagnosticLoggingAdapter l = log.withCorrelationId(dittoHeaders);
final String tag = sudoRetrieveConnectionIdsByTag.getTag();
l.info("Retrieving connection IDs by tag <{}>: {}", tag, sudoRetrieveConnectionIdsByTag);
try {
final ActorRef sender = sender();
final CompletionStage<SudoRetrieveConnectionIdsByTagResponse>
retrieveConnectionIdsByTagResponseCompletionStage =
taggedPidSourceFunction.apply(tag)
.filter(pid -> pid.startsWith(ConnectionPersistenceActor.PERSISTENCE_ID_PREFIX))
.map(pid -> pid.substring(ConnectionPersistenceActor.PERSISTENCE_ID_PREFIX.length()))
.map(ConnectionId::of)
.runWith(Sink.seq(), materializer)
.thenApply(Set::copyOf)
.thenApply(connectionIds -> {
l.info("Found the following connection IDs for tag <{}>: <{}>", tag, connectionIds);
return connectionIds;
})
.thenApply(connectionIds -> SudoRetrieveConnectionIdsByTagResponse.of(connectionIds,
dittoHeaders));
Patterns.pipe(retrieveConnectionIdsByTagResponseCompletionStage, getContext().dispatcher()).to(sender);
} catch (final Exception e) {
l.error(e, "Failed to load persistence ids from journal/snapshots for connections with tag <{}>.", tag);
getSender().tell(buildErrorResponse(e, dittoHeaders), getSelf());
}
}
private void getAllConnectionIDs(final WithDittoHeaders cmd) {
final DittoDiagnosticLoggingAdapter logger = log.withCorrelationId(cmd);
logger.info("Retrieving all connection IDs ...");
try {
final Source<String, NotUsed> idsFromSnapshots = persistenceIdsFromSnapshotSourceSupplier.get()
.via(Flow.fromFunction(result -> {
logger.debug("getIdsFromSnapshotsSource element: <{}>", result);
return result;
}))
.filter(ConnectionIdsRetrievalActor::snapshotIsNotDeleted)
.map(this::extractPersistenceIdFromDocument)
.filter(Optional::isPresent)
.map(Optional::get)
.via(Flow.fromFunction(result -> {
logger.debug("idsFromSnapshots element: <{}>", result);
return result;
}));
final Source<String, NotUsed> idsFromJournal = persistenceIdsFromJournalSourceSupplier.get()
.via(Flow.fromFunction(result -> {
logger.debug("idsFromJournalSource element: <{}>", result);
return result;
}))
.filter(ConnectionIdsRetrievalActor::isNotDeleted)
.map(document -> document.getString(MongoReadJournal.J_EVENT_PID))
.via(Flow.fromFunction(result -> {
logger.debug("idsFromJournal element: <{}>", result);
return result;
}));
final CompletionStage<List<String>> deletedPidsStage = persistenceIdsFromJournalSourceSupplier.get()
.filter(ConnectionIdsRetrievalActor::isDeleted)
.map(document -> document.getString(MongoReadJournal.J_EVENT_PID))
.runWith(Sink.seq(), materializer);
final CompletionStage<CommandResponse> retrieveAllConnectionIdsResponse = deletedPidsStage
.thenApply(deletedIdsFromJournal -> {
logger.debug("deletedIdsFromJournal element: <{}>", deletedIdsFromJournal);
return deletedIdsFromJournal;
})
.thenCompose(deletedIdsFromJournal -> idsFromSnapshots.concat(idsFromJournal)
.filter(pid -> !deletedIdsFromJournal.contains(pid))
.filter(pid -> pid.startsWith(ConnectionPersistenceActor.PERSISTENCE_ID_PREFIX))
.map(pid -> pid.substring(
ConnectionPersistenceActor.PERSISTENCE_ID_PREFIX.length()))
.runWith(Sink.seq(), materializer)
)
.thenApply(idList -> idList.stream().sorted().toList())
.thenApply(LinkedHashSet::new)
.thenApply(ids -> buildResponse(cmd, ids))
.exceptionally(throwable -> buildErrorResponse(throwable, cmd.getDittoHeaders()));
Patterns.pipe(retrieveAllConnectionIdsResponse, getContext().dispatcher()).to(getSender());
} catch (final Exception e) {
log.info("Failed to load persistence ids from journal/snapshots.", e);
getSender().tell(buildErrorResponse(e, cmd.getDittoHeaders()), getSelf());
}
}
@SuppressWarnings({"rawtypes", "java:S3740"})
private CommandResponse buildResponse(final WithDittoHeaders cmd, final Set<String> ids) {
return RetrieveAllConnectionIdsResponse.of(ids, cmd.getDittoHeaders());
}
private ConnectivityErrorResponse buildErrorResponse(final Throwable throwable, final DittoHeaders dittoHeaders) {
final ConnectivityInternalErrorException dittoRuntimeException =
ConnectivityInternalErrorException.newBuilder()
.message(() -> "Failed to load connections ids from persistence: " + throwable.getMessage())
.dittoHeaders(dittoHeaders)
.build();
return ConnectivityErrorResponse.of(dittoRuntimeException);
}
private Optional<String> extractPersistenceIdFromDocument(final Document document) {
return Optional.ofNullable(document.getString(PERSISTENCE_ID_FIELD));
}
}