The only solution I found, is to copy the context into a final variable outside of the stream and apply it for every single iteration:
Map<String, String> contextMap = MDC.getCopyOfContextMap();
Stream.iterate(0, i -> i + 1).parallel()
.peek(i -> MDC.setContextMap(contextMap))
// ...logic...
// in case you're using a filter, you need to use a predicate and combine it with a clear step:
filter(yourPredicate.or(i -> {
MDC.clear();
return false;
}))
// clear right before terminal operation
.peek(i -> MDC.clear())
.findFirst();
// since the initial thread is also used within the stream and the context is cleared there,
// we need to set it again to its initial state
MDC.setContextMap(contextMap);
The cost for that solution is 1) a few microseconds per 100 iterations and 2) worse readability, but both are acceptable:
- This is a benchmark comparing an
IntStream.range(0, 100).parallel().sum()
(=baseline) with same stream that uses that MDC copy logic:
Benchmark Mode Cnt Score Error Units
MDC_CopyTest.baseline thrpt 5 0,038 ± 0,005 ops/us
MDC_CopyTest.withMdc thrpt 5 0,024 ± 0,001 ops/us
MDC_CopyTest.baseline avgt 5 28,239 ± 1,308 us/op
MDC_CopyTest.withMdc avgt 5 40,178 ± 0,761 us/op
- To improve readability, it can be wrapped into a small helper class:
public class MDCCopyHelper {
private Map<String, String> contextMap = MDC.getCopyOfContextMap();
public void set(Object... any) {
MDC.setContextMap(contextMap);
}
public void clear(Object... any) {
MDC.clear();
}
public boolean clearAndFail() {
MDC.clear();
return false;
}
}
The streaming code looks then a bit nicer:
MDCCopyHelper mdcHelper = new MDCCopyHelper();
try {
Optional<Integer> findFirst = Stream.iterate(0, i -> i + 1)
.parallel()
.peek(mdcHelper::set)
// ...logic...
// filters predicates should be combined with clear step
.filter(yourPredicate.or(mdcHelper::clearAndFail))
// before terminal call:
.peek(mdcHelper::clear)
.findFirst();
} finally {
// set the correct MDC at the main thread again
mdcHelper.set();
}