-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ContextPropagation.java
470 lines (405 loc) · 15.4 KB
/
ContextPropagation.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
* Copyright (c) 2022-2023 VMware Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.core.publisher;
import java.util.AbstractQueue;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Queue;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import io.micrometer.context.ContextAccessor;
import io.micrometer.context.ContextRegistry;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ThreadLocalAccessor;
import reactor.core.observability.SignalListener;
import reactor.util.annotation.Nullable;
import reactor.util.context.Context;
import reactor.util.context.ContextView;
/**
* Utility private class to detect if the <a href="https://github.com/micrometer-metrics/context-propagation">context-propagation library</a> is on the classpath and to offer
* ContextSnapshot support to {@link Flux} and {@link Mono}.
*
* @author Simon Baslé
*/
final class ContextPropagation {
static final Predicate<Object> PREDICATE_TRUE = v -> true;
static final Function<Context, Context> NO_OP = c -> c;
static final Function<Context, Context> WITH_GLOBAL_REGISTRY_NO_PREDICATE;
static {
WITH_GLOBAL_REGISTRY_NO_PREDICATE = ContextPropagationSupport.isContextPropagationAvailable() ?
new ContextCaptureNoPredicate(ContextRegistry.getInstance()) : NO_OP;
}
public static Function<Runnable, Runnable> scopePassingOnScheduleHook() {
return delegate -> {
ContextSnapshot contextSnapshot = ContextSnapshot.captureAll();
return contextSnapshot.wrap(delegate);
};
}
/**
* Create a support function that takes a snapshot of thread locals and merges them with the
* provided {@link Context}, resulting in a new {@link Context} which includes entries
* captured from threadLocals by the Context-Propagation API.
* <p>
* This variant uses the implicit global {@code ContextRegistry} and captures from all
* available {@code ThreadLocalAccessors}. It is the same variant backing {@link Flux#contextCapture()}
* and {@link Mono#contextCapture()}.
*
* @return the {@link Context} augmented with captured entries
*/
static Function<Context, Context> contextCapture() {
return WITH_GLOBAL_REGISTRY_NO_PREDICATE;
}
static Context contextCaptureToEmpty() {
return contextCapture().apply(Context.empty());
}
/**
* Create a support function that takes a snapshot of thread locals and merges them with the
* provided {@link Context}, resulting in a new {@link Context} which includes entries
* captured from threadLocals by the Context-Propagation API.
* <p>
* The provided {@link Predicate} is used on keys associated to said thread locals
* by the Context-Propagation API to filter which entries should be captured in the
* first place.
* <p>
* This variant uses the implicit global {@code ContextRegistry} and captures only from
* available {@code ThreadLocalAccessors} that match the {@link Predicate}.
*
* @param captureKeyPredicate a {@link Predicate} used on keys to determine if each entry
* should be injected into the new {@link Context}
* @return a {@link Function} augmenting {@link Context} with captured entries
*/
static Function<Context, Context> contextCapture(Predicate<Object> captureKeyPredicate) {
if (!ContextPropagationSupport.isContextPropagationOnClasspath) {
return NO_OP;
}
return target -> ContextSnapshot.captureAllUsing(captureKeyPredicate, ContextRegistry.getInstance())
.updateContext(target);
}
/**
* When <a href="https://github.com/micrometer-metrics/context-propagation">context-propagation library</a>
* is available on the classpath, the provided {@link BiConsumer handler} will be
* called with {@link ThreadLocal} values restored from the provided {@link Context}.
* @param handler user provided handler
* @param contextSupplier supplies the potentially modified {@link Context} to
* restore {@link ThreadLocal} values from
* @return potentially wrapped {@link BiConsumer} or the original
* @param <T> type of handled values
* @param <R> the transformed type
*/
static <T, R> BiConsumer<T, SynchronousSink<R>> contextRestoreForHandle(BiConsumer<T, SynchronousSink<R>> handler, Supplier<Context> contextSupplier) {
if (ContextPropagationSupport.shouldRestoreThreadLocalsInSomeOperators()) {
final Context ctx = contextSupplier.get();
if (ctx.isEmpty()) {
return handler;
}
return (v, sink) -> {
try (ContextSnapshot.Scope ignored = ContextSnapshot.setAllThreadLocalsFrom(ctx)) {
handler.accept(v, sink);
}
};
}
else {
return handler;
}
}
/**
* When <a href="https://github.com/micrometer-metrics/context-propagation">context-propagation library</a>
* is available on the classpath, the provided {@link SignalListener} will be wrapped
* with another one that restores {@link ThreadLocal} values from the provided
* {@link Context}.
* <p><strong>Note, this is only applied to {@link FluxTap}, {@link FluxTapFuseable},
* {@link MonoTap}, and {@link MonoTapFuseable}.</strong> The automatic propagation
* variants: {@link FluxTapRestoringThreadLocals} and
* {@link MonoTapRestoringThreadLocals} do not use this method.
* @param original the original {@link SignalListener} from the user
* @param contextSupplier supplies the potentially modified {@link Context} to
* restore {@link ThreadLocal} values from
* @return potentially wrapped {@link SignalListener} or the original
* @param <T> type of handled values
*/
static <T> SignalListener<T> contextRestoreForTap(final SignalListener<T> original, Supplier<Context> contextSupplier) {
if (!ContextPropagationSupport.isContextPropagationAvailable()) {
return original;
}
final Context ctx = contextSupplier.get();
if (ctx.isEmpty()) {
return original;
}
return new ContextRestoreSignalListener<T>(original, ctx, null);
}
//the SignalListener implementation can be tested independently with a test-specific ContextRegistry
static final class ContextRestoreSignalListener<T> implements SignalListener<T> {
final SignalListener<T> original;
final ContextView context;
final ContextRegistry registry;
public ContextRestoreSignalListener(SignalListener<T> original, ContextView context, @Nullable ContextRegistry registry) {
this.original = original;
this.context = context;
this.registry = registry == null ? ContextRegistry.getInstance() : registry;
}
ContextSnapshot.Scope restoreThreadLocals() {
return ContextSnapshot.setAllThreadLocalsFrom(this.context, this.registry);
}
@Override
public void doFirst() throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doFirst();
}
}
@Override
public void doFinally(SignalType terminationType) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doFinally(terminationType);
}
}
@Override
public void doOnSubscription() throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnSubscription();
}
}
@Override
public void doOnFusion(int negotiatedFusion) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnFusion(negotiatedFusion);
}
}
@Override
public void doOnRequest(long requested) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnRequest(requested);
}
}
@Override
public void doOnCancel() throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnCancel();
}
}
@Override
public void doOnNext(T value) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnNext(value);
}
}
@Override
public void doOnComplete() throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnComplete();
}
}
@Override
public void doOnError(Throwable error) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnError(error);
}
}
@Override
public void doAfterComplete() throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doAfterComplete();
}
}
@Override
public void doAfterError(Throwable error) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doAfterError(error);
}
}
@Override
public void doOnMalformedOnNext(T value) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnMalformedOnNext(value);
}
}
@Override
public void doOnMalformedOnError(Throwable error) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnMalformedOnError(error);
}
}
@Override
public void doOnMalformedOnComplete() throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnMalformedOnComplete();
}
}
@Override
public void handleListenerError(Throwable listenerError) {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.handleListenerError(listenerError);
}
}
@Override
public Context addToContext(Context originalContext) {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
return original.addToContext(originalContext);
}
}
}
static final class ContextQueue<T> extends AbstractQueue<T> {
static final String NOT_SUPPORTED_MESSAGE = "ContextQueue wrapper is intended " +
"for use with instances returned by Queues class. Iterator based " +
"methods are usually unsupported.";
final Queue<Envelope<T>> envelopeQueue;
boolean cleanOnNull;
boolean hasPrevious = false;
Thread lastReader;
ContextSnapshot.Scope scope;
@SuppressWarnings({"unchecked", "rawtypes"})
ContextQueue(Queue<?> queue) {
this.envelopeQueue = (Queue) queue;
}
@Override
public int size() {
return envelopeQueue.size();
}
@Override
public boolean offer(T o) {
ContextSnapshot contextSnapshot = ContextSnapshot.captureAll();
return envelopeQueue.offer(new Envelope<>(o, contextSnapshot));
}
@Override
public T poll() {
Envelope<T> envelope = envelopeQueue.poll();
if (envelope == null) {
if (cleanOnNull && scope != null) {
// clear thread-locals if they were just restored
scope.close();
}
cleanOnNull = true;
lastReader = Thread.currentThread();
hasPrevious = false;
return null;
}
restoreTheContext(envelope);
hasPrevious = true;
return envelope.body;
}
private void restoreTheContext(Envelope<T> envelope) {
ContextSnapshot contextSnapshot = envelope.contextSnapshot;
// tries to read existing Thread for existing ThreadLocals
ContextSnapshot currentContextSnapshot = ContextSnapshot.captureAll();
if (!contextSnapshot.equals(currentContextSnapshot)) {
if (!hasPrevious || !Thread.currentThread().equals(this.lastReader)) {
// means context was restored form the envelope,
// thus it has to be cleared
cleanOnNull = true;
lastReader = Thread.currentThread();
}
scope = contextSnapshot.setThreadLocals();
}
else if (!hasPrevious || !Thread.currentThread().equals(this.lastReader)) {
// means same context was already available, no need to clean anything
cleanOnNull = false;
lastReader = Thread.currentThread();
}
}
@Override
@Nullable
public T peek() {
Envelope<T> envelope = envelopeQueue.peek();
return envelope == null ? null : envelope.body;
}
@Override
public Iterator<T> iterator() {
throw new UnsupportedOperationException(NOT_SUPPORTED_MESSAGE);
}
}
static class Envelope<T> {
final T body;
final ContextSnapshot contextSnapshot;
Envelope(T body, ContextSnapshot contextSnapshot) {
this.body = body;
this.contextSnapshot = contextSnapshot;
}
}
static final class ContextCaptureNoPredicate implements Function<Context, Context> {
final ContextRegistry globalRegistry;
ContextCaptureNoPredicate(ContextRegistry globalRegistry) {
this.globalRegistry = globalRegistry;
}
@Override
public Context apply(Context context) {
return ContextSnapshot.captureAllUsing(PREDICATE_TRUE, globalRegistry)
.updateContext(context);
}
}
/*
* Temporary methods not present in context-propagation library that allow
* clearing ThreadLocals not present in Reactor Context. Once context-propagation
* library adds the ability to do this, they can be removed from reactor-core.
*/
@SuppressWarnings("unchecked")
static <C> ContextSnapshot.Scope setThreadLocals(Object context) {
ContextRegistry registry = ContextRegistry.getInstance();
ContextAccessor<?, ?> contextAccessor = registry.getContextAccessorForRead(context);
Map<Object, Object> previousValues = null;
for (ThreadLocalAccessor<?> threadLocalAccessor : registry.getThreadLocalAccessors()) {
Object key = threadLocalAccessor.key();
Object value = ((ContextAccessor<C, ?>) contextAccessor).readValue((C) context, key);
previousValues = setThreadLocal(key, value, threadLocalAccessor, previousValues);
}
return ReactorScopeImpl.from(previousValues, registry);
}
@SuppressWarnings("unchecked")
private static <V> Map<Object, Object> setThreadLocal(Object key, @Nullable V value,
ThreadLocalAccessor<?> accessor, @Nullable Map<Object, Object> previousValues) {
previousValues = (previousValues != null ? previousValues : new HashMap<>());
previousValues.put(key, accessor.getValue());
if (value != null) {
((ThreadLocalAccessor<V>) accessor).setValue(value);
}
else {
accessor.reset();
}
return previousValues;
}
private static class ReactorScopeImpl implements ContextSnapshot.Scope {
private final Map<Object, Object> previousValues;
private final ContextRegistry contextRegistry;
private ReactorScopeImpl(Map<Object, Object> previousValues,
ContextRegistry contextRegistry) {
this.previousValues = previousValues;
this.contextRegistry = contextRegistry;
}
@Override
public void close() {
for (ThreadLocalAccessor<?> accessor : this.contextRegistry.getThreadLocalAccessors()) {
if (this.previousValues.containsKey(accessor.key())) {
Object previousValue = this.previousValues.get(accessor.key());
resetThreadLocalValue(accessor, previousValue);
}
}
}
@SuppressWarnings("unchecked")
private <V> void resetThreadLocalValue(ThreadLocalAccessor<?> accessor, @Nullable V previousValue) {
if (previousValue != null) {
((ThreadLocalAccessor<V>) accessor).restore(previousValue);
}
else {
accessor.reset();
}
}
public static ContextSnapshot.Scope from(@Nullable Map<Object, Object> previousValues, ContextRegistry registry) {
return (previousValues != null ? new ReactorScopeImpl(previousValues, registry) : () -> {
});
}
}
}