-
Notifications
You must be signed in to change notification settings - Fork 215
/
EnforcementFlow.java
315 lines (283 loc) · 15.3 KB
/
EnforcementFlow.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* 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.persistence.write.streaming;
import java.time.Duration;
import java.time.Instant;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;
import org.eclipse.ditto.internal.models.signalenrichment.CachingSignalEnrichmentFacade;
import org.eclipse.ditto.internal.models.signalenrichment.CachingSignalEnrichmentFacadeProvider;
import org.eclipse.ditto.internal.utils.cache.Cache;
import org.eclipse.ditto.internal.utils.cache.CacheFactory;
import org.eclipse.ditto.internal.utils.cache.config.CacheConfig;
import org.eclipse.ditto.internal.utils.cache.entry.Entry;
import org.eclipse.ditto.internal.utils.cacheloaders.EnforcementCacheKey;
import org.eclipse.ditto.internal.utils.cacheloaders.PolicyCacheLoader;
import org.eclipse.ditto.internal.utils.cacheloaders.config.AskWithRetryConfig;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonRuntimeException;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.PolicyIdInvalidException;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.events.ThingDeleted;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.thingsearch.service.common.config.StreamCacheConfig;
import org.eclipse.ditto.thingsearch.service.common.config.StreamConfig;
import org.eclipse.ditto.thingsearch.service.persistence.write.mapping.EnforcedThingMapper;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.AbstractWriteModel;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.Metadata;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.ThingDeleteModel;
import org.eclipse.ditto.thingsearch.service.updater.actors.MongoWriteModel;
import org.eclipse.ditto.thingsearch.service.updater.actors.SearchUpdateObserver;
import org.eclipse.ditto.thingsearch.service.updater.actors.ThingUpdater;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.benmanes.caffeine.cache.AsyncCacheLoader;
import akka.NotUsed;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Scheduler;
import akka.japi.Pair;
import akka.japi.pf.PFBuilder;
import akka.stream.javadsl.Flow;
import akka.stream.javadsl.Keep;
import akka.stream.javadsl.Source;
/**
* Converts Thing changes into write models by retrieving data and applying enforcement via an enforcer cache.
*/
final class EnforcementFlow {
private static final Source<Entry<Policy>, NotUsed> POLICY_NONEXISTENT = Source.single(Entry.nonexistent());
private final Logger log = LoggerFactory.getLogger(getClass());
private final CachingSignalEnrichmentFacade thingsFacade;
private final Cache<EnforcementCacheKey, Entry<Policy>> policyEnforcerCache;
private final Duration cacheRetryDelay;
private final int maxArraySize;
private final SearchUpdateObserver searchUpdateObserver;
private EnforcementFlow(final ActorSystem actorSystem,
final ActorRef thingsShardRegion,
final Cache<EnforcementCacheKey, Entry<Policy>> policyEnforcerCache,
final AskWithRetryConfig askWithRetryConfig,
final StreamCacheConfig thingCacheConfig,
final int maxArraySize,
final Executor thingCacheDispatcher) {
thingsFacade = createThingsFacade(actorSystem, thingsShardRegion, askWithRetryConfig.getAskTimeout(),
thingCacheConfig, thingCacheDispatcher);
this.policyEnforcerCache = policyEnforcerCache;
searchUpdateObserver = SearchUpdateObserver.get(actorSystem);
cacheRetryDelay = thingCacheConfig.getRetryDelay();
this.maxArraySize = maxArraySize;
}
/**
* Create an EnforcementFlow object.
*
* @param actorSystem the actor system for loading the {@link CachingSignalEnrichmentFacadeProvider}
* @param updaterStreamConfig configuration of the updater stream.
* @param thingsShardRegion the shard region to retrieve things from.
* @param policiesShardRegion the shard region to retrieve policies from.
* @param scheduler the scheduler to use for retrying timed out asks for the policy enforcer cache loader.
* @return an EnforcementFlow object.
*/
public static EnforcementFlow of(final ActorSystem actorSystem,
final StreamConfig updaterStreamConfig,
final ActorRef thingsShardRegion,
final ActorRef policiesShardRegion,
final Scheduler scheduler) {
final var askWithRetryConfig = updaterStreamConfig.getAskWithRetryConfig();
final var policyCacheConfig = updaterStreamConfig.getPolicyCacheConfig();
final var policyCacheDispatcher = actorSystem.dispatchers()
.lookup(policyCacheConfig.getDispatcherName());
final AsyncCacheLoader<EnforcementCacheKey, Entry<Policy>> policyCacheLoader =
new PolicyCacheLoader(askWithRetryConfig, scheduler, policiesShardRegion);
final Cache<EnforcementCacheKey, Entry<Policy>> policyEnforcerCache =
CacheFactory.createCache(policyCacheLoader, policyCacheConfig,
"things-search_enforcementflow_enforcer_cache_policy", policyCacheDispatcher);
final var thingCacheConfig = updaterStreamConfig.getThingCacheConfig();
final var thingCacheDispatcher = actorSystem.dispatchers()
.lookup(thingCacheConfig.getDispatcherName());
return new EnforcementFlow(actorSystem, thingsShardRegion, policyEnforcerCache, askWithRetryConfig,
thingCacheConfig, updaterStreamConfig.getMaxArraySize(), thingCacheDispatcher);
}
private static EnforcementCacheKey getPolicyCacheKey(final PolicyId policyId) {
return EnforcementCacheKey.of(policyId);
}
/**
* Decide whether to reload an enforcer entry.
* An entry should be reloaded if it is out-of-date, nonexistent, or corresponds to a nonexistent entity.
*
* @param entry the enforcer cache entry
* @param metadata the metadata
* @param iteration how many times cache read was attempted
* @return whether to reload the cache
*/
private static boolean shouldReloadCache(@Nullable final Entry<?> entry, final Metadata metadata,
final int iteration) {
if (iteration <= 0) {
return metadata.shouldInvalidatePolicy() || entry == null || !entry.exists() ||
entry.getRevision() < metadata.getPolicyRevision().orElse(Long.MAX_VALUE);
} else {
// never attempt to reload cache more than once
return false;
}
}
/**
* Create a flow from Thing changes to write models by retrieving data from Things shard region and enforcer cache.
*
* @param source the source of change maps.
* @param parallelismPerBulkShard how many thing retrieves to perform in parallel to the caching facade per bulk
* shard.
* @return the flow.
*/
public <T> Source<List<AbstractWriteModel>, T> create(
final Source<Collection<Metadata>, T> source,
final int parallelismPerBulkShard,
final int maxBulkSize) {
return source.flatMapConcat(changes -> Source.fromIterator(changes::iterator)
.flatMapMerge(parallelismPerBulkShard, changedMetadata ->
retrieveThingFromCachingFacade(changedMetadata.getThingId(), changedMetadata)
.flatMapConcat(pair -> {
final JsonObject thing = pair.second();
searchUpdateObserver.process(changedMetadata, thing);
return computeWriteModel(changedMetadata, thing);
})
)
.grouped(maxBulkSize))
.filterNot(List::isEmpty);
}
// TODO
public Flow<ThingUpdater.Data, MongoWriteModel, NotUsed> create(final SearchUpdateMapper mapper) {
return Flow.<ThingUpdater.Data>create()
.flatMapConcat(data -> retrieveThingFromCachingFacade(data.metadata().getThingId(), data.metadata())
.flatMapConcat(pair -> {
final JsonObject thing = pair.second();
searchUpdateObserver.process(data.metadata(), thing);
return computeWriteModel(data.metadata(), thing);
})
.flatMapConcat(writeModel -> mapper.processWriteModel(writeModel, data.lastWriteModel())
.orElse(Source.lazily(() -> {
data.metadata().sendWeakAck(null);
return Source.empty();
})))
);
}
private Source<Pair<ThingId, JsonObject>, NotUsed> retrieveThingFromCachingFacade(final ThingId thingId,
final Metadata metadata) {
ConsistencyLag.startS3RetrieveThing(metadata);
final CompletionStage<JsonObject> thingFuture;
if (metadata.shouldInvalidateThing()) {
thingFuture = thingsFacade.retrieveThing(thingId, List.of(), -1);
} else {
thingFuture = thingsFacade.retrieveThing(thingId, metadata.getEvents(), metadata.getThingRevision());
}
return Source.completionStage(thingFuture)
.map(thing -> Pair.create(thingId, thing))
.recoverWithRetries(1, new PFBuilder<Throwable, Source<Pair<ThingId, JsonObject>, NotUsed>>()
.match(Throwable.class, error -> {
log.error("Unexpected response for SudoRetrieveThing via cache: <{}>", thingId, error);
return Source.empty();
})
.build());
}
private Source<AbstractWriteModel, NotUsed> computeWriteModel(final Metadata metadata,
@Nullable final JsonObject thing) {
ConsistencyLag.startS4GetEnforcer(metadata);
final ThingEvent<?> latestEvent = metadata.getEvents()
.stream().max(Comparator.comparing(e -> e.getTimestamp().orElseGet(() -> {
log.warn("Event <{}> did not contain a timestamp.", e);
return Instant.EPOCH;
})))
.orElse(null);
if (latestEvent instanceof ThingDeleted || thing == null || thing.isEmpty()) {
log.info("Computed single ThingDeleteModel for metadata <{}> and thing <{}>", metadata, thing);
return Source.single(ThingDeleteModel.of(metadata));
} else {
return getPolicy(metadata, thing)
.map(entry -> {
if (entry.exists()) {
try {
return EnforcedThingMapper.toWriteModel(thing, entry.getValueOrThrow(),
entry.getRevision(),
maxArraySize,
metadata);
} catch (final JsonRuntimeException e) {
log.error(e.getMessage(), e);
log.info(
"Computed - due to <{}: {}> - ThingDeleteModel for metadata <{}> and thing <{}>",
e.getClass().getSimpleName(), e.getMessage(), metadata, thing);
return ThingDeleteModel.of(metadata);
}
} else {
// no enforcer; delete thing from search index
log.info("Computed - due to missing enforcer - ThingDeleteModel for metadata <{}> " +
"and thing <{}>", metadata, thing);
return ThingDeleteModel.of(metadata);
}
});
}
}
/**
* Get the enforcer of a thing or an empty source if it does not exist.
*
* @param metadata metadata of the thing.
* @param thing the thing
* @return source of an enforcer or an empty source.
*/
private Source<Entry<Policy>, NotUsed> getPolicy(final Metadata metadata, final JsonObject thing) {
try {
return thing.getValue(Thing.JsonFields.POLICY_ID)
.map(PolicyId::of)
.map(policyId -> readCachedEnforcer(metadata, getPolicyCacheKey(policyId), 0))
.orElse(POLICY_NONEXISTENT);
} catch (final PolicyIdInvalidException e) {
return POLICY_NONEXISTENT;
}
}
private Source<Entry<Policy>, NotUsed> readCachedEnforcer(final Metadata metadata,
final EnforcementCacheKey policyId, final int iteration) {
final Source<Entry<Policy>, ?> lazySource = Source.lazySource(() -> {
final CompletionStage<Source<Entry<Policy>, NotUsed>> enforcerFuture = policyEnforcerCache.get(policyId)
.thenApply(optionalEnforcerEntry -> {
if (shouldReloadCache(optionalEnforcerEntry.orElse(null), metadata, iteration)) {
// invalid entry; invalidate and retry after delay
policyEnforcerCache.invalidate(policyId);
return readCachedEnforcer(metadata, policyId, iteration + 1)
.initialDelay(cacheRetryDelay);
} else {
return optionalEnforcerEntry.map(Source::single)
.orElse(POLICY_NONEXISTENT);
}
})
.exceptionally(error -> {
log.error("Failed to read policyEnforcerCache", error);
return POLICY_NONEXISTENT;
});
return Source.completionStageSource(enforcerFuture);
});
return lazySource.viaMat(Flow.create(), Keep.none());
}
private static CachingSignalEnrichmentFacade createThingsFacade(final ActorSystem actorSystem,
final ActorRef thingsShardRegion,
final Duration timeout,
final CacheConfig thingCacheConfig,
final Executor thingCacheDispatcher) {
final var sudoRetrieveThingFacade = SudoSignalEnrichmentFacade.of(thingsShardRegion, timeout);
final var cachingSignalEnrichmentFacadeProvider = CachingSignalEnrichmentFacadeProvider.get(actorSystem);
return cachingSignalEnrichmentFacadeProvider.getSignalEnrichmentFacade(actorSystem, sudoRetrieveThingFacade,
thingCacheConfig, thingCacheDispatcher, "things-search_enforcementflow_enforcer_cache_things");
}
}