-
Notifications
You must be signed in to change notification settings - Fork 214
/
PolicyModificationForwarder.java
247 lines (213 loc) · 9.68 KB
/
PolicyModificationForwarder.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
245
246
247
/*
* Copyright (c) 2019 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.thingsearch.service.updater.actors;
import static org.apache.pekko.cluster.pubsub.DistributedPubSubMediator.SubscribeAck;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess;
import org.eclipse.ditto.internal.utils.config.DefaultScopedConfig;
import org.eclipse.ditto.internal.utils.namespaces.BlockNamespaceBehavior;
import org.eclipse.ditto.internal.utils.namespaces.BlockedNamespaces;
import org.eclipse.ditto.policies.api.PolicyTag;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.thingsearch.api.PolicyReferenceTag;
import org.eclipse.ditto.thingsearch.service.common.config.DittoSearchConfig;
import org.eclipse.ditto.thingsearch.service.persistence.write.ThingsSearchUpdaterPersistence;
import org.apache.pekko.Done;
import org.apache.pekko.NotUsed;
import org.apache.pekko.actor.AbstractActor;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.CoordinatedShutdown;
import org.apache.pekko.actor.Props;
import org.apache.pekko.actor.Status;
import org.apache.pekko.event.DiagnosticLoggingAdapter;
import org.apache.pekko.japi.pf.ReceiveBuilder;
import org.apache.pekko.pattern.Patterns;
import org.apache.pekko.stream.KillSwitch;
import org.apache.pekko.stream.KillSwitches;
import org.apache.pekko.stream.javadsl.Keep;
import org.apache.pekko.stream.javadsl.Sink;
import org.apache.pekko.stream.javadsl.Source;
/**
* Actor that forwards policy tags (emitted by PolicyPersistence after each policy modification) to updater
* shard region with buffering.
*/
final class PolicyModificationForwarder extends AbstractActor {
private static final Duration ASK_SELF_TIMEOUT = Duration.ofSeconds(10L);
static final String ACTOR_NAME = "policyModificationForwarder";
private final DiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
private final ActorRef pubSubMediator;
private final ActorRef thingsUpdater;
private final ThingsSearchUpdaterPersistence persistence;
private final BlockNamespaceBehavior blockNamespaceBehavior;
private final Duration interval;
private Map<PolicyId, Long> policyRevisions = new HashMap<>();
@Nullable private KillSwitch killSwitch;
@SuppressWarnings("unused")
private PolicyModificationForwarder(final ActorRef pubSubMediator,
final ActorRef thingsUpdater,
final BlockedNamespaces blockedNamespaces,
final ThingsSearchUpdaterPersistence persistence) {
this.pubSubMediator = pubSubMediator;
this.thingsUpdater = thingsUpdater;
this.persistence = persistence;
blockNamespaceBehavior = BlockNamespaceBehavior.of(blockedNamespaces);
interval = DittoSearchConfig.of(DefaultScopedConfig.dittoScoped(getContext().getSystem().settings().config()))
.getUpdaterConfig().getStreamConfig().getWriteInterval();
pubSubMediator.tell(DistPubSubAccess.subscribeViaGroup(PolicyTag.PUB_SUB_TOPIC_MODIFIED, ACTOR_NAME, getSelf()),
getSelf());
restartPolicyReferenceTagStream();
}
/**
* Create Props for this actor.
*
* @param pubSubMediator Pekko pub-sub-mediator
* @param thingsUpdater thingsUpdater
* @param blockedNamespaces blocked namespaces.
* @return the Props object.
*/
public static Props props(final ActorRef pubSubMediator,
final ActorRef thingsUpdater,
final BlockedNamespaces blockedNamespaces,
final ThingsSearchUpdaterPersistence persistence) {
return Props.create(PolicyModificationForwarder.class, pubSubMediator, thingsUpdater, blockedNamespaces,
persistence);
}
@Override
public void preStart() {
CoordinatedShutdown.get(getContext().getSystem())
.addTask(CoordinatedShutdown.PhaseServiceUnbind(), "service-unbind-" + ACTOR_NAME, () -> {
final var unsub =
DistPubSubAccess.unsubscribeViaGroup(PolicyTag.PUB_SUB_TOPIC_MODIFIED, ACTOR_NAME, getSelf());
final var shutdownAskTimeout = Duration.ofMinutes(1); // does not matter as phase will timeout
return Patterns.ask(pubSubMediator, unsub, shutdownAskTimeout).thenApply(reply -> Done.done());
});
}
@Override
public void postStop() {
terminateStream();
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(PolicyTag.class, this::policyTag)
.match(LocalWrappedPolicyTag.class, this::updatePolicyRevision)
.match(PolicyReferenceTag.class, this::forwardToThingsUpdater)
.matchEquals(Control.DUMP_POLICY_REVISIONS, this::dumpPolicyRevisions)
.matchEquals(Control.STREAM_COMPLETED, this::streamTerminated)
.match(Status.Failure.class, this::streamTerminated)
.match(SubscribeAck.class, this::subscribeAck)
.build();
}
/**
* Apply PolicyTag after checking for blocking purged namespace.
*
* @param policyTag incoming policy tag
*/
private void policyTag(final PolicyTag policyTag) {
final ActorRef self = getSelf();
blockNamespaceBehavior.isBlocked(policyTag.getEntityId())
.whenComplete((isBlocked, error) -> {
if (null == error && !isBlocked) {
self.tell(new LocalWrappedPolicyTag(policyTag), self);
}
});
}
private void updatePolicyRevision(final LocalWrappedPolicyTag wrappedPolicyTag) {
final PolicyTag policyTag = wrappedPolicyTag.delegate();
final PolicyId policyId = policyTag.getEntityId();
final long revision = policyTag.getRevision();
policyRevisions.merge(policyId, revision, Long::max);
}
/**
* Forward PolicyReferenceTag to ThingsUpdater, who then forward it to shard region after namespace blocking.
* There should be no sender because this actor expects no acknowledgement.
*
* @param policyReferenceTag the policy reference tag.
*/
private void forwardToThingsUpdater(final PolicyReferenceTag policyReferenceTag) {
log.info("Forwarding <{}> at <{}> to <{}>", policyReferenceTag.getPolicyTag().getEntityId(),
policyReferenceTag.getPolicyTag().getRevision(),
policyReferenceTag.getThingId());
thingsUpdater.tell(policyReferenceTag, ActorRef.noSender());
}
@SuppressWarnings("unused")
private void dumpPolicyRevisions(final Control trigger) {
final Map<PolicyId, Long> dump = policyRevisions;
policyRevisions = new HashMap<>();
getSender().tell(dump, getSelf());
}
private void streamTerminated(final Object streamTerminated) {
if (streamTerminated instanceof Status.Failure failure) {
final String errorMessage =
"PolicyModificationForwarder stream terminated (should NEVER happen!), restarting";
log.error(failure.cause(), errorMessage);
} else {
log.info("PolicyModificationForwarder stream completed; restarting");
}
restartPolicyReferenceTagStream();
}
private void subscribeAck(final SubscribeAck subscribeAck) {
log.info("SubscribeAck: <{}>", subscribeAck);
}
private void restartPolicyReferenceTagStream() {
terminateStream();
final ActorRef self = getSelf();
final Source<Control, NotUsed> repeat;
if (!interval.isNegative() && !interval.isZero()) {
repeat = Source.repeat(Control.DUMP_POLICY_REVISIONS)
.throttle(1, interval);
} else {
repeat = Source.repeat(Control.DUMP_POLICY_REVISIONS);
}
killSwitch = repeat.viaMat(KillSwitches.single(), Keep.right())
.mapAsync(1, message ->
Patterns.ask(self, message, ASK_SELF_TIMEOUT).exceptionally(Function.identity()))
.flatMapConcat(this::mapDumpResult)
.to(Sink.actorRef(self, Control.STREAM_COMPLETED))
.run(getContext().getSystem());
}
private void terminateStream() {
if (killSwitch != null) {
killSwitch.shutdown();
killSwitch = null;
}
}
@SuppressWarnings("unchecked")
private Source<PolicyReferenceTag, NotUsed> mapDumpResult(final Object dumpResult) {
if (dumpResult instanceof Map) {
final Map<PolicyId, Long> map = (Map<PolicyId, Long>) dumpResult;
if (map.isEmpty()) {
return Source.empty();
} else {
return persistence.getPolicyReferenceTags(map);
}
} else {
if (dumpResult instanceof Throwable throwable) {
log.error(throwable, "dump failed");
} else {
log.warning("Unexpected dump result: <{}>", dumpResult);
}
return Source.empty();
}
}
private enum Control {
DUMP_POLICY_REVISIONS,
STREAM_COMPLETED
}
private record LocalWrappedPolicyTag(PolicyTag delegate) {}
}