-
Notifications
You must be signed in to change notification settings - Fork 214
/
ResponseReceiverCache.java
359 lines (305 loc) · 14.9 KB
/
ResponseReceiverCache.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Copyright (c) 2022 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.things.service.persistence.actors;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.time.Duration;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.common.ConditionChecker;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.internal.utils.cache.Cache;
import org.eclipse.ditto.internal.utils.cache.CaffeineCache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Expiry;
import org.apache.pekko.actor.AbstractExtensionId;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.ExtendedActorSystem;
import org.apache.pekko.actor.Extension;
/**
* A cache of response receivers and their associated correlation ID.
* <p>
* Each cache entry gets evicted after becoming expired.
* <p>
* To put a response receiver to this cache a {@link Command} has to be provided as key.
* The command is necessary because of its headers.
* The command headers are required to contain the mandatory correlation ID.
* Optionally they provide the timeout of the command.
* If the command headers provide a timeout, it is used as expiry for the cache entry, otherwise a fall-back expiry
* is used.
*/
@NotThreadSafe
public final class ResponseReceiverCache implements Extension {
private static final ExtensionId EXTENSION_ID = new ExtensionId();
private static final Duration DEFAULT_ENTRY_EXPIRY = Duration.ofMinutes(2L);
private final ActorSystem actorSystem;
private final Duration fallBackEntryExpiry;
private final Cache<CorrelationIdKey, ResponseReceiverCacheEntry> cache;
private ResponseReceiverCache(final ActorSystem actorSystem,
final Duration fallBackEntryExpiry,
final Cache<CorrelationIdKey, ResponseReceiverCacheEntry> cache) {
this.actorSystem = actorSystem;
this.fallBackEntryExpiry = fallBackEntryExpiry;
this.cache = cache;
}
/**
* Returns a default instance of {@code ResponseReceiverCache} with a hard-coded fall-back entry expiry
* for an actor system.
*
* @param actorSystem the actor system.
* @return the instance.
*/
public static ResponseReceiverCache lookup(final ActorSystem actorSystem) {
return EXTENSION_ID.get(actorSystem);
}
/**
* Returns a new instance of {@code ResponseReceiverCache} with a hard-coded fall-back entry expiry.
*
* @param actorSystem the ActorSystem.
* @return the instance.
*/
static ResponseReceiverCache newInstance(final ActorSystem actorSystem) {
return newInstance(actorSystem, DEFAULT_ENTRY_EXPIRY);
}
/**
* Returns a new instance of {@code ResponseReceiverCache} with the specified fall-back entry expiry.
*
* @param actorSystem the ActorSystem.
* @param fallBackEntryExpiry the expiry to be used for cache entries of commands without a timeout.
* @return the instance.
* @throws NullPointerException if {@code fallBackEntryExpiry} is {@code null}.
* @throws IllegalArgumentException if {@code fallBackEntryExpiry} is not positive.
*/
static ResponseReceiverCache newInstance(final ActorSystem actorSystem, final Duration fallBackEntryExpiry) {
ConditionChecker.checkArgument(checkNotNull(fallBackEntryExpiry, "fallBackEntryExpiry"),
Predicate.not(Duration::isZero).and(Predicate.not(Duration::isNegative)),
() -> "The fallBackEntryExpiry must be positive.");
return new ResponseReceiverCache(actorSystem, fallBackEntryExpiry, createCache(fallBackEntryExpiry));
}
private static Cache<CorrelationIdKey, ResponseReceiverCacheEntry> createCache(final Duration fallBackEntryExpiry) {
return CaffeineCache.of(Caffeine.newBuilder().expireAfter(new CorrelationIdKeyExpiry(fallBackEntryExpiry)));
}
/**
* Caches the specified response receiver for the correlation ID of the signal's correlation ID.
*
* @param signal the signal to extract the correlation ID from used for the cache key.
* @param responseReceiver the ActorRef of the response receiver to cache for the correlation ID.
* @throws NullPointerException if any argument is {@code null}.
* @throws IllegalArgumentException if the headers of {@code signal} do not contain a correlation ID.
*/
public void cacheSignalResponseReceiver(final Signal<?> signal, final ActorRef responseReceiver) {
cache.put(getCorrelationIdKeyForInsertion(checkNotNull(signal, "signal")),
new ResponseReceiverCacheEntry(
checkNotNull(responseReceiver, "responseReceiver"),
signal.getDittoHeaders().getAuthorizationContext()
));
}
private CorrelationIdKey getCorrelationIdKeyForInsertion(final WithDittoHeaders command) {
final var commandDittoHeaders = command.getDittoHeaders();
return CorrelationIdKey.forCacheInsertion(getCorrelationIdOrThrow(commandDittoHeaders),
getExpiry(commandDittoHeaders));
}
private static String getCorrelationIdOrThrow(final DittoHeaders commandDittoHeaders) {
return commandDittoHeaders.getCorrelationId()
.orElseThrow(() -> new IllegalArgumentException("Command headers have no correlation ID."));
}
private Duration getExpiry(final DittoHeaders commandDittoHeaders) {
return commandDittoHeaders.getTimeout().orElse(fallBackEntryExpiry);
}
/**
* Returns the cached response receiver for the specified correlation ID argument.
*
* @param correlationId the correlation ID to get the cached response receiver for.
* @return when successful, the cached response receiver for {@code correlationId} or an empty {@code Optional}.
* @throws NullPointerException if {@code correlationId} is {@code null}.
* @throws IllegalArgumentException if {@code correlationId} is empty or blank.
*/
public CompletableFuture<Optional<ResponseReceiverCacheEntry>> get(final CharSequence correlationId) {
final var correlationIdString = checkNotNull(correlationId, "correlationId").toString();
ConditionChecker.checkArgument(correlationIdString,
Predicate.not(String::isBlank),
() -> "The correlationId must not be blank.");
return cache.get(CorrelationIdKey.forCacheRetrieval(correlationIdString));
}
/**
* Invalidates the cached response receiver for the specified correlation ID argument.
*
* @param correlationId the correlation ID to invalidate the cached response receiver for.
* @throws NullPointerException if {@code correlationId} is {@code null}.
* @throws IllegalArgumentException if {@code correlationId} is empty or blank.
*/
private void invalidate(final CharSequence correlationId) {
final var correlationIdString = checkNotNull(correlationId, "correlationId").toString();
ConditionChecker.checkArgument(correlationIdString,
Predicate.not(String::isBlank),
() -> "The correlationId must not be blank.");
cache.invalidate(CorrelationIdKey.forCacheRetrieval(correlationIdString));
}
/**
* Insert a response receiver for a live or message command.
*
* @param signal the live or message command.
* @param receiverCreator creator of the receiver actor.
* @param responseHandler handler of the response.
* @param <T> type of results of the response handler.
* @return the result of the response handler.
*/
public <S extends Signal<?>, T> CompletionStage<T> insertResponseReceiverConflictFree(final S signal,
final Function<S, ActorRef> receiverCreator,
final BiFunction<S, ActorRef, T> responseHandler) {
signal.getDittoHeaders().getCorrelationId().ifPresent(correlationId ->
actorSystem.getScheduler().scheduleOnce(
signal.getDittoHeaders().getTimeout().orElse(fallBackEntryExpiry),
() -> invalidate(correlationId),
actorSystem.dispatcher()
)
);
return insertResponseReceiverConflictFreeWithFuture(signal,
receiverCreator,
responseHandler.andThen(CompletableFuture::completedStage));
}
/**
* Insert a response receiver for a live or message command.
*
* @param signal the live or message command.
* @param receiverCreator creator of the receiver actor.
* @param responseHandler handler of the response.
* @param <T> type of results of the response handler.
* @return the result of the response handler.
*/
public <S extends Signal<?>, T> CompletionStage<T> insertResponseReceiverConflictFreeWithFuture(final S signal,
final Function<S, ActorRef> receiverCreator,
final BiFunction<S, ActorRef, CompletionStage<T>> responseHandler) {
return setUniqueCorrelationIdForGlobalDispatching(signal, false)
.thenCompose(commandWithUniqueCorrelationId -> {
final ActorRef receiver = receiverCreator.apply(commandWithUniqueCorrelationId);
cacheSignalResponseReceiver(commandWithUniqueCorrelationId, receiver);
return responseHandler.apply(commandWithUniqueCorrelationId, receiver);
});
}
@SuppressWarnings("unchecked")
private <S extends Signal<?>> CompletionStage<S> setUniqueCorrelationIdForGlobalDispatching(final S signal,
final boolean refreshCorrelationId) {
final String correlationId;
final var previousCorrelationId = WithDittoHeaders.getCorrelationId(signal);
final boolean correlationIdRefreshed = refreshCorrelationId || previousCorrelationId.isEmpty();
if (correlationIdRefreshed) {
correlationId = previousCorrelationId.map(existingId -> existingId + "_" + UUID.randomUUID())
.orElseGet(() -> UUID.randomUUID().toString());
} else {
correlationId = previousCorrelationId.get();
}
return get(correlationId)
.thenCompose(entry -> {
final CompletionStage<S> result;
if (entry.isPresent()) {
result = setUniqueCorrelationIdForGlobalDispatching(signal, true);
} else if (correlationIdRefreshed) {
result = CompletableFuture.completedFuture(
(S) signal.setDittoHeaders(DittoHeaders.newBuilder(signal.getDittoHeaders())
.correlationId(correlationId)
.build())
);
} else {
result = CompletableFuture.completedFuture(signal);
}
return result;
});
}
@Immutable
private static final class CorrelationIdKey {
private final String correlationId;
@Nullable private final Duration expiry;
private CorrelationIdKey(final String correlationId, @Nullable final Duration expiry) {
this.correlationId = correlationId;
this.expiry = expiry;
}
static CorrelationIdKey forCacheInsertion(final String correlationId, final Duration expiry) {
return new CorrelationIdKey(correlationId, expiry);
}
static CorrelationIdKey forCacheRetrieval(final String correlationId) {
return new CorrelationIdKey(correlationId, null);
}
// Only use correlation ID here because this is the only value that
// is used for cache entry retrieval.
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final var that = (CorrelationIdKey) o;
return Objects.equals(correlationId, that.correlationId);
}
@Override
public int hashCode() {
return Objects.hash(correlationId);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"correlationId=" + correlationId +
", expiry=" + expiry +
"]";
}
}
static final class ExtensionId extends AbstractExtensionId<ResponseReceiverCache> {
@Override
public ResponseReceiverCache createExtension(final ExtendedActorSystem system) {
return newInstance(system);
}
}
@Immutable
private static final class CorrelationIdKeyExpiry implements Expiry<CorrelationIdKey, ResponseReceiverCacheEntry> {
private final Duration fallBackEntryExpiry;
private CorrelationIdKeyExpiry(final Duration fallBackEntryExpiry) {
this.fallBackEntryExpiry = fallBackEntryExpiry;
}
@Override
public long expireAfterCreate(final CorrelationIdKey key, final ResponseReceiverCacheEntry value, final long currentTime) {
final var entryExpiry = Objects.requireNonNullElse(key.expiry, fallBackEntryExpiry);
return entryExpiry.toNanos(); // it is crucial to return nanoseconds here
}
@Override
public long expireAfterUpdate(final CorrelationIdKey key,
final ResponseReceiverCacheEntry value,
final long currentTime,
final long currentDuration) {
return currentDuration;
}
@Override
public long expireAfterRead(final CorrelationIdKey key,
final ResponseReceiverCacheEntry value,
final long currentTime,
final long currentDuration) {
return currentDuration;
}
}
record ResponseReceiverCacheEntry(ActorRef sender, AuthorizationContext authorizationContext) {
}
}