-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ContextPropagation.java
548 lines (465 loc) · 17.5 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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
* Copyright (c) 2022-2024 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.Supplier;
import io.micrometer.context.ContextAccessor;
import io.micrometer.context.ContextRegistry;
import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import io.micrometer.context.ThreadLocalAccessor;
import reactor.core.Fuseable;
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 Function<Context, Context> NO_OP = c -> c;
static final Function<Context, Context> WITH_GLOBAL_REGISTRY_NO_PREDICATE;
static ContextSnapshotFactory globalContextSnapshotFactory = null;
static {
WITH_GLOBAL_REGISTRY_NO_PREDICATE = ContextPropagationSupport.isContextPropagationAvailable() ?
new ContextCaptureNoPredicate() : NO_OP;
if (ContextPropagationSupport.isContextPropagation103Available()) {
globalContextSnapshotFactory = ContextSnapshotFactory.builder()
.clearMissing(false)
.build();
}
}
static <T> Flux<T> fluxRestoreThreadLocals(Flux<? extends T> flux, boolean fuseable) {
return fuseable ?
new FluxContextWriteRestoringThreadLocalsFuseable<>(flux, Function.identity())
: new FluxContextWriteRestoringThreadLocals<>(flux, Function.identity());
}
static <T> Mono<T> monoRestoreThreadLocals(Mono<? extends T> mono) {
return new MonoContextWriteRestoringThreadLocals<>(mono, Function.identity());
}
static void configureContextSnapshotFactory(boolean clearMissing) {
if (ContextPropagationSupport.isContextPropagation103OnClasspath) {
globalContextSnapshotFactory = ContextSnapshotFactory.builder()
.clearMissing(clearMissing)
.build();
}
}
@SuppressWarnings("unchecked")
static <C> ContextSnapshot.Scope setThreadLocals(Object context) {
if (ContextPropagationSupport.isContextPropagation103OnClasspath) {
return globalContextSnapshotFactory.setThreadLocalsFrom(context);
}
else {
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);
}
if (ContextPropagationSupport.isContextPropagation101Available()) {
return ReactorScopeImpl.from(previousValues, registry);
}
return ReactorScopeImpl100.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;
}
static ContextSnapshot captureThreadLocals() {
if (ContextPropagationSupport.isContextPropagation103OnClasspath) {
return globalContextSnapshotFactory.captureAll();
}
else {
return ContextSnapshot.captureAll();
}
}
public static Function<Runnable, Runnable> scopePassingOnScheduleHook() {
return delegate -> {
ContextSnapshot contextSnapshot = captureThreadLocals();
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());
}
/**
* 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;
}
if (ContextPropagationSupport.isContextPropagation103OnClasspath) {
return (v, sink) -> {
try (ContextSnapshot.Scope ignored = globalContextSnapshotFactory.setThreadLocalsFrom(ctx)) {
handler.accept(v, sink);
}
};
}
else {
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;
}
if (ContextPropagationSupport.isContextPropagation103OnClasspath) {
return new ContextRestore103SignalListener<>(original, ctx, globalContextSnapshotFactory);
}
else {
return new ContextRestoreSignalListener<>(original, ctx);
}
}
//the SignalListener implementation can be tested independently with a test-specific ContextRegistry
static class ContextRestoreSignalListener<T> implements SignalListener<T> {
final SignalListener<T> original;
final ContextView context;
public ContextRestoreSignalListener(SignalListener<T> original,
ContextView context) {
this.original = original;
this.context = context;
}
ContextSnapshot.Scope restoreThreadLocals() {
return ContextSnapshot.setAllThreadLocalsFrom(this.context);
}
@Override
public final void doFirst() throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doFirst();
}
}
@Override
public final void doFinally(SignalType terminationType) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doFinally(terminationType);
}
}
@Override
public final void doOnSubscription() throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnSubscription();
}
}
@Override
public final void doOnFusion(int negotiatedFusion) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnFusion(negotiatedFusion);
}
}
@Override
public final void doOnRequest(long requested) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnRequest(requested);
}
}
@Override
public final void doOnCancel() throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnCancel();
}
}
@Override
public final void doOnNext(T value) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnNext(value);
}
}
@Override
public final void doOnComplete() throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnComplete();
}
}
@Override
public final void doOnError(Throwable error) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnError(error);
}
}
@Override
public final void doAfterComplete() throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doAfterComplete();
}
}
@Override
public final void doAfterError(Throwable error) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doAfterError(error);
}
}
@Override
public final void doOnMalformedOnNext(T value) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnMalformedOnNext(value);
}
}
@Override
public final void doOnMalformedOnError(Throwable error) throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnMalformedOnError(error);
}
}
@Override
public final void doOnMalformedOnComplete() throws Throwable {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.doOnMalformedOnComplete();
}
}
@Override
public final void handleListenerError(Throwable listenerError) {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
original.handleListenerError(listenerError);
}
}
@Override
public final Context addToContext(Context originalContext) {
try (ContextSnapshot.Scope ignored = restoreThreadLocals()) {
return original.addToContext(originalContext);
}
}
}
//the SignalListener implementation can be tested independently with a test-specific ContextRegistry
static final class ContextRestore103SignalListener<T> extends ContextRestoreSignalListener<T> {
final ContextSnapshotFactory contextSnapshotFactory;
public ContextRestore103SignalListener(SignalListener<T> original,
ContextView context, ContextSnapshotFactory contextSnapshotFactory) {
super(original, context);
this.contextSnapshotFactory = contextSnapshotFactory;
}
ContextSnapshot.Scope restoreThreadLocals() {
return contextSnapshotFactory.setThreadLocalsFrom(this.context);
}
}
static final class ContextCaptureNoPredicate implements Function<Context, Context> {
@Override
public Context apply(Context context) {
return captureThreadLocals().updateContext(context);
}
}
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 = captureThreadLocals();
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 = captureThreadLocals();
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;
}
}
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) : () -> {
});
}
}
private static class ReactorScopeImpl100 implements ContextSnapshot.Scope {
private final Map<Object, Object> previousValues;
private final ContextRegistry contextRegistry;
private ReactorScopeImpl100(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).setValue(previousValue);
}
else {
accessor.reset();
}
}
public static ContextSnapshot.Scope from(@Nullable Map<Object, Object> previousValues, ContextRegistry registry) {
return (previousValues != null ? new ReactorScopeImpl100(previousValues, registry) : () -> {
});
}
}
}