Repository navigation
AsyncLoadingCache: ThreadLocal propagation and Request Coalescing #2001
|
Hey! I've prepared reproducible example for that, where two threads are trying to get value for the same key. Expected behaviour: Current behaviour: Here's the code, I'm using package com.nalepa.demo.example08;
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
import com.github.benmanes.caffeine.cache.Caffeine;
import java.time.Duration;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
class ThreadLocalBleed {
private static final String USER_A_VALUE = "userAValue";
private static final String USER_B_VALUE = "userBValue";
public static void main(String[] args) throws Exception {
threadLocalBleed();
}
static void threadLocalBleed() throws Exception {
ExecutorService customCaffeineExecutor =
new ContextPropagationExecutorService(
Executors.newFixedThreadPool(1, new SimpleThreadFactory("test-caffeine-executor"))
);
CountDownLatch loadStarted = new CountDownLatch(1);
CountDownLatch doneLatch = new CountDownLatch(2);
AsyncLoadingCache<String, String> cache = Caffeine.newBuilder()
.executor(customCaffeineExecutor)
.buildAsync(
(key, executor) -> {
return CompletableFuture.supplyAsync(
() -> {
loadStarted.countDown();
nonThrowingSleep(5);
return key;
}, executor);
}
);
AtomicReference<String> valueObservedOnUserAThread = new AtomicReference<>("");
AtomicReference<String> valueObservedOnUserBThread = new AtomicReference<>("");
// user A — triggers the loader
Thread.ofPlatform().name("userA").start(() -> {
ThreadLocalWrapper.setValue(USER_A_VALUE);
cache.get("shared-key")
.thenRun(() -> {
observeThreadLocalValue("User A", USER_A_VALUE, valueObservedOnUserAThread, doneLatch);
});
});
loadStarted.await(10, TimeUnit.SECONDS);
// user B — coalesced onto the future from user A
Thread.ofPlatform().name("userB").start(() -> {
ThreadLocalWrapper.setValue(USER_B_VALUE);
cache.get("shared-key")
.thenRun(() -> {
observeThreadLocalValue("User B", USER_B_VALUE, valueObservedOnUserBThread, doneLatch);
});
});
doneLatch.await(10, TimeUnit.SECONDS);
customCaffeineExecutor.shutdownNow();
if (USER_A_VALUE.equals(valueObservedOnUserAThread.get())) {
System.out.println("OK. User A sees " + USER_A_VALUE);
} else {
System.out.println("NOT OK! User A expected " + USER_A_VALUE + ", but saw: " + valueObservedOnUserAThread.get());
}
if (USER_B_VALUE.equals(valueObservedOnUserBThread.get())) {
System.out.println("OK. User B sees " + USER_B_VALUE);
} else {
System.out.println("NOT OK! User B expected " + USER_B_VALUE + ", but saw: " + valueObservedOnUserBThread.get());
}
}
static void observeThreadLocalValue(String user, String expectedUserValue, AtomicReference<String> observedValueHolder, CountDownLatch doneLatch) {
log(user + " value should be : " + expectedUserValue);
String currentThreadLocalValue = ThreadLocalWrapper.getValue();
log(user + " observed: " + currentThreadLocalValue);
observedValueHolder.set(currentThreadLocalValue);
doneLatch.countDown();
}
static void nonThrowingSleep(int seconds) {
try {
Thread.sleep(Duration.ofSeconds(seconds));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
static void log(String message) {
System.out.println("### " + Thread.currentThread().getName() + " : " + message);
}
}
class ThreadLocalWrapper {
private static final ThreadLocal<String> threadLocal = ThreadLocal.withInitial(() -> "INITIAL");
static String getValue() {
return threadLocal.get();
}
static void setValue(String newValue) {
threadLocal.set(newValue);
}
static void removeValue() {
threadLocal.remove();
}
}
class ContextPropagationExecutorService implements ExecutorService {
private final ExecutorService delegate;
ContextPropagationExecutorService(ExecutorService delegate) {
this.delegate = delegate;
}
@Override
public void execute(Runnable command) {
delegate.execute(new ContextPropagationExecutorService.ContextPropagationRunnable(command));
}
@Override
public <T> Future<T> submit(Callable<T> task) {
return delegate.submit(new ContextPropagationExecutorService.ContextPropagationCallable<>(task));
}
@Override
public <T> Future<T> submit(Runnable task, T result) {
return delegate.submit(new ContextPropagationExecutorService.ContextPropagationRunnable(task), result);
}
@Override
public Future<?> submit(Runnable task) {
return delegate.submit(new ContextPropagationExecutorService.ContextPropagationRunnable(task));
}
@Override
public <T> java.util.List<Future<T>> invokeAll(java.util.Collection<? extends Callable<T>> tasks) throws InterruptedException {
return delegate.invokeAll(tasks.stream().map(ContextPropagationExecutorService.ContextPropagationCallable::new).toList());
}
@Override
public <T> java.util.List<Future<T>> invokeAll(java.util.Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
return delegate.invokeAll(tasks.stream().map(ContextPropagationExecutorService.ContextPropagationCallable::new).toList(), timeout, unit);
}
@Override
public <T> T invokeAny(java.util.Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
return delegate.invokeAny(tasks.stream().map(ContextPropagationExecutorService.ContextPropagationCallable::new).toList());
}
@Override
public <T> T invokeAny(java.util.Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return delegate.invokeAny(tasks.stream().map(ContextPropagationExecutorService.ContextPropagationCallable::new).toList(), timeout, unit);
}
@Override
public void shutdown() {
delegate.shutdown();
}
@Override
public java.util.List<Runnable> shutdownNow() {
return delegate.shutdownNow();
}
@Override
public boolean isShutdown() {
return delegate.isShutdown();
}
@Override
public boolean isTerminated() {
return delegate.isTerminated();
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
return delegate.awaitTermination(timeout, unit);
}
static class ContextPropagationRunnable implements Runnable {
private static final AtomicInteger counter = new AtomicInteger(0);
private final Runnable wrapped;
private final String capturedValue;
ContextPropagationRunnable(Runnable wrapped) {
this.wrapped = wrapped;
this.capturedValue = ThreadLocalWrapper.getValue();
}
@Override
public void run() {
try {
ThreadLocalWrapper.setValue(capturedValue);
wrapped.run();
} finally {
ThreadLocalWrapper.removeValue();
}
}
}
static class ContextPropagationCallable<T> implements Callable<T> {
private final Callable<T> wrapped;
private final String capturedValue;
ContextPropagationCallable(Callable<T> wrapped) {
this.wrapped = wrapped;
this.capturedValue = ThreadLocalWrapper.getValue();
}
@Override
public T call() throws Exception {
try {
ThreadLocalWrapper.setValue(capturedValue);
return wrapped.call();
} finally {
ThreadLocalWrapper.removeValue();
}
}
}
}
class SimpleThreadFactory implements ThreadFactory {
private final String namePrefix;
private final AtomicInteger threadNumber = new AtomicInteger(1);
SimpleThreadFactory(String namePrefix) {
this.namePrefix = namePrefix;
}
@Override
public Thread newThread(Runnable runnable) {
return new Thread(runnable, namePrefix + "-" + threadNumber.getAndIncrement());
}
} |
Replies: 3 comments 1 reply
|
Working as designed. And coalescing isn't really the bug, it just made an existing one deterministic.
CompletableFuture<V> future = cache().computeIfAbsent(key, k -> {
var castedResult = (CompletableFuture<V>) mappingFunction.apply(key, cache().executor());Only the winner of Now remove the coalescing. Let A's load finish before B arrives and B gets a plain cache hit, still The cached value has to be a pure function of the key. Anything the loader pulls from a ThreadLocal that changes the value is a hidden key component, and those alias. If the value varies per user, put the user in the key: record CacheKey(String resource, String userId) {}If it doesn't vary but the loader needs context to fetch it (auth token, trace id), pass it explicitly and keep it out of the result. Propagating spans is fine, it's only unsafe once the propagated value reaches the returned object. Not Caffeine specific either, plain |
|
It looks like the // user A — triggers the loader
Thread.ofPlatform().name("userA").start(() -> {
ThreadLocalWrapper.setValue(USER_A_VALUE);
cache.get("shared-key")
.thenRun(() -> {
observeThreadLocalValue("User A", USER_A_VALUE, valueObservedOnUserAThread, doneLatch);
});
});
loadStarted.await(10, TimeUnit.SECONDS);
// user B — coalesced onto the future from user A
Thread.ofPlatform().name("userB").start(() -> {
ThreadLocalWrapper.setValue(USER_B_VALUE);
cache.get("shared-key")
.thenRun(() -> {
observeThreadLocalValue("User B", USER_B_VALUE, valueObservedOnUserBThread, doneLatch);
});
});The The context propagation stuff just gets really messy with async flows. JEE seems to try to centralize how futures are created so they can have a customizable one, but that is error prone (ala quarkusio/quarkus#50513 for smallrye-context-propagation). I'm not sure how robust one can really make this idiom, so switching to more explicit context passing via by parameters and capturing lambdas is probably the safest going forward. ScopedValues make it clearer and work, but are not meant to interact with futures so its likely not appropriate for AsyncCache. |
|
Thanks @0xshubhs @ben-manes for answers! How do you think @ben-manes is it worth to mention current behaviour of thread locals propagation in Wiki, for example in faq? |
It looks like the
cachecomputes a shared value and the dependent actions need to be contextual. This is where the bug lies.