-
Notifications
You must be signed in to change notification settings - Fork 215
/
ConnectionIdsRetrievalActor.java
208 lines (184 loc) · 10.4 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
/*
* 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 java.time.Duration;
import java.util.HashSet;
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.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.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess;
import org.eclipse.ditto.internal.utils.persistence.mongo.streaming.MongoReadJournal;
import akka.NotUsed;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.cluster.pubsub.DistributedPubSub;
import akka.event.DiagnosticLoggingAdapter;
import akka.japi.pf.ReceiveBuilder;
import akka.pattern.Patterns;
import akka.stream.Materializer;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
/**
* 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 DiagnosticLoggingAdapter 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);
}
@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 Akka 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 Akka 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);
}
@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 String tag = sudoRetrieveConnectionIdsByTag.getTag();
final DittoHeaders dittoHeaders = sudoRetrieveConnectionIdsByTag.getDittoHeaders();
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(connectionIds -> Set.of(connectionIds.toArray(new ConnectionId[0])))
.thenApply(connectionIds -> SudoRetrieveConnectionIdsByTagResponse.of(connectionIds,
dittoHeaders));
Patterns.pipe(retrieveConnectionIdsByTagResponseCompletionStage, getContext().dispatcher()).to(sender);
} catch (final Exception e) {
log.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) {
try {
final Source<String, NotUsed> idsFromSnapshots = getIdsFromSnapshotsSource();
final Source<String, NotUsed> idsFromJournal = persistenceIdsFromJournalSourceSupplier.get()
.filter(ConnectionIdsRetrievalActor::isNotDeleted)
.map(document -> document.getString(MongoReadJournal.J_EVENT_PID));
final CompletionStage<CommandResponse> retrieveAllConnectionIdsResponse =
persistenceIdsFromJournalSourceSupplier.get()
.filter(ConnectionIdsRetrievalActor::isDeleted)
.map(document -> document.getString(MongoReadJournal.J_EVENT_PID))
.runWith(Sink.seq(), materializer)
.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(HashSet::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());
}
}
private Source<String, NotUsed> getIdsFromSnapshotsSource() {
return persistenceIdsFromSnapshotSourceSupplier.get()
.map(this::extractPersistenceIdFromDocument)
.filter(Optional::isPresent)
.map(Optional::get);
}
@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));
}
}