/
Contextual.java
269 lines (221 loc) · 9.34 KB
/
Contextual.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
/*
* Copyright (c) 2017 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.concierge.service.enforcement;
import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.base.model.entity.id.WithEntityId;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.internal.utils.akka.controlflow.WithSender;
import org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLoggingAdapter;
import org.eclipse.ditto.internal.utils.cache.Cache;
import org.eclipse.ditto.internal.utils.cache.CacheKey;
import org.eclipse.ditto.internal.utils.metrics.instruments.timer.StartedTimer;
import akka.actor.ActorRef;
/**
* A message together with contextual information about the actor processing it.
*/
public final class Contextual<T extends WithDittoHeaders> implements WithSender<T>, WithDittoHeaders {
@Nullable
private final T message;
private final ActorRef self;
private final ActorRef sender;
private final ActorRef pubSubMediator;
private final ActorRef conciergeForwarder;
private final Duration askTimeout;
private final ThreadSafeDittoLoggingAdapter log;
@Nullable
private final CacheKey cacheKey;
@Nullable
private final StartedTimer startedTimer;
@Nullable
private final ActorRef receiver;
@Nullable
private final Function<Object, Object> receiverWrapperFunction;
// for live signal enforcement
@Nullable
private final Cache<String, ActorRef> responseReceivers;
@Nullable
private final Supplier<CompletionStage<Object>> askFuture;
private Contextual(@Nullable final T message, final ActorRef self, @Nullable final ActorRef sender,
final ActorRef pubSubMediator, final ActorRef conciergeForwarder,
final Duration askTimeout, final ThreadSafeDittoLoggingAdapter log,
@Nullable final CacheKey cacheKey,
@Nullable final StartedTimer startedTimer,
@Nullable final ActorRef receiver,
@Nullable final Function<Object, Object> receiverWrapperFunction,
@Nullable final Cache<String, ActorRef> responseReceivers,
@Nullable final Supplier<CompletionStage<Object>> askFuture) {
this.message = message;
this.self = self;
this.sender = sender;
this.pubSubMediator = pubSubMediator;
this.conciergeForwarder = conciergeForwarder;
this.askTimeout = askTimeout;
this.log = log;
this.cacheKey = cacheKey;
this.startedTimer = startedTimer;
this.receiver = receiver;
this.receiverWrapperFunction = receiverWrapperFunction;
this.responseReceivers = responseReceivers;
this.askFuture = askFuture;
}
static <T extends WithDittoHeaders> Contextual<T> forActor(final ActorRef self,
final ActorRef deadLetters,
final ActorRef pubSubMediator,
final ActorRef conciergeForwarder,
final Duration askTimeout,
final ThreadSafeDittoLoggingAdapter log,
@Nullable final Cache<String, ActorRef> responseReceivers) {
return new Contextual<>(null, self, deadLetters, pubSubMediator, conciergeForwarder, askTimeout, log, null,
null,
null, null, responseReceivers, null);
}
/**
* Perform Ask-steps before forwarding a message.
* {@code Patterns.ask()} has the same command order guarantee as {@code ActorRef.tell()} when executed inside
* an actor's {@code Receive}, because it eventually calls {@code ActorRef.tell()} in the calling thread.
*
* @param askFuture future of a message to forward to the receiver.
* @return a copy of this with an ask-future.
*/
Contextual<T> withAskFuture(final Supplier<CompletionStage<Object>> askFuture) {
return new Contextual<>(message, self, sender, pubSubMediator, conciergeForwarder, askTimeout, log, cacheKey,
startedTimer, receiver, receiverWrapperFunction, responseReceivers, askFuture);
}
/**
* Retrieve the message but tolerate that it may be null.
*
* @return the optional message.
*/
public Optional<T> getMessageOptional() {
return Optional.ofNullable(message);
}
@Override
public T getMessage() {
if (message == null) {
throw new IllegalStateException("Contextual: message was null where it should not have been");
}
return message;
}
@Override
public ActorRef getSender() {
return sender;
}
@Override
public DittoHeaders getDittoHeaders() {
if (message != null) {
return message.getDittoHeaders();
} else {
return DittoHeaders.empty();
}
}
@Override
public Contextual<T> withMessage(@Nullable final T message) {
return withReceivedMessage(message, sender);
}
<S extends WithDittoHeaders> Contextual<S> setMessage(@Nullable final S message) {
return withReceivedMessage(message, sender);
}
Optional<Supplier<CompletionStage<Object>>> getAskFuture() {
return Optional.ofNullable(askFuture);
}
ActorRef getSelf() {
return self;
}
ActorRef getPubSubMediator() {
return pubSubMediator;
}
ActorRef getConciergeForwarder() {
return conciergeForwarder;
}
Duration getAskTimeout() {
return askTimeout;
}
ThreadSafeDittoLoggingAdapter getLog() {
return log;
}
CacheKey getCacheKey() {
if (cacheKey == null) {
throw new IllegalStateException("Contextual: cacheKey was null where it should not have been");
}
return cacheKey;
}
Optional<StartedTimer> getStartedTimer() {
return Optional.ofNullable(startedTimer);
}
Optional<ActorRef> getReceiver() {
return Optional.ofNullable(receiver);
}
Function<Object, Object> getReceiverWrapperFunction() {
return receiverWrapperFunction != null ? receiverWrapperFunction : Function.identity();
}
Optional<Cache<String, ActorRef>> getResponseReceivers() {
return Optional.ofNullable(responseReceivers);
}
<S extends WithDittoHeaders> Optional<Contextual<S>> tryToMapMessage(final Function<T, Optional<S>> f) {
return f.apply(getMessage()).map(result -> withReceivedMessage(result, sender));
}
<S extends WithDittoHeaders> Contextual<S> withReceivedMessage(@Nullable final S message,
@Nullable final ActorRef sender) {
return new Contextual<>(message, self, sender, pubSubMediator, conciergeForwarder, askTimeout,
log, cacheKeyFor(message), startedTimer, receiver, receiverWrapperFunction, responseReceivers,
askFuture);
}
Contextual<T> withTimer(final StartedTimer startedTimer) {
return new Contextual<>(message, self, sender, pubSubMediator, conciergeForwarder, askTimeout,
log, cacheKey, startedTimer, receiver, receiverWrapperFunction, responseReceivers,
askFuture);
}
Contextual<T> withReceiver(@Nullable final ActorRef receiver) {
return new Contextual<>(message, self, sender, pubSubMediator, conciergeForwarder, askTimeout,
log, cacheKey, startedTimer, receiver, receiverWrapperFunction, responseReceivers,
askFuture);
}
Contextual<T> withReceiverWrapperFunction(final Function<Object, Object> receiverWrapperFunction) {
return new Contextual<>(message, self, sender, pubSubMediator, conciergeForwarder, askTimeout,
log, cacheKey, startedTimer, receiver, receiverWrapperFunction, responseReceivers,
askFuture);
}
@Nullable
private static CacheKey cacheKeyFor(@Nullable final WithDittoHeaders signal) {
if (signal == null) {
return null;
} else if (signal instanceof DittoRuntimeException) {
return null;
} else
return WithEntityId.getEntityIdOfType(EntityId.class, signal)
.map(CacheKey::of)
.orElseThrow(() -> new IllegalArgumentException(
"Contextual: processed WithDittoHeaders message did not implement " +
"WithResource or WithEntityId: " + signal.getClass().getSimpleName()));
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"message=" + message +
", self=" + self +
", sender=" + sender +
", cacheKey=" + cacheKey +
", receiver=" + receiver +
", receiverWrapperFunction=" + receiverWrapperFunction +
", responseReceivers=" + responseReceivers +
"]";
}
}