diff --git a/CHANGELOG.md b/CHANGELOG.md index e3353dbdaa..1a9f0688b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,9 @@ to docs, or any other relevant information. credentials set through `setHeaders` no longer appear in logs. - The JavaDoc of `WorkflowClient.startUpdate` and `WorkflowClient.startUpdateWithStart` no longer describes these calls as asynchronous. +- Concurrent Nexus operations on the same worker no longer risk being executed by another + operation's handler. The shared Nexus interceptor middleware kept each request's root + interceptor in an instance field, which a concurrent request could overwrite. - Client request metrics (`temporal_long_request_latency` etc.) now always carry the `update_name` tag (default `none`), fixing Prometheus tag-key conflicts when Workflow Updates are used (#3210). diff --git a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java index 055d713d32..eb0c1ac16c 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java @@ -10,7 +10,6 @@ public class TemporalInterceptorMiddleware implements OperationMiddleware { private final WorkerInterceptor[] interceptors; - RootNexusOperationInboundCallsInterceptor rootInboundCallsInterceptor; public TemporalInterceptorMiddleware(WorkerInterceptor[] interceptors) { this.interceptors = interceptors; @@ -19,7 +18,8 @@ public TemporalInterceptorMiddleware(WorkerInterceptor[] interceptors) { @Override public OperationHandler intercept( OperationContext context, OperationHandler operationHandler) { - rootInboundCallsInterceptor = new RootNexusOperationInboundCallsInterceptor(operationHandler); + RootNexusOperationInboundCallsInterceptor rootInboundCallsInterceptor = + new RootNexusOperationInboundCallsInterceptor(operationHandler); NexusOperationInboundCallsInterceptor inboundCallsInterceptor = rootInboundCallsInterceptor; for (WorkerInterceptor interceptor : interceptors) { inboundCallsInterceptor = diff --git a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java index 82032d46c8..3899af87f2 100644 --- a/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java +++ b/temporal-sdk/src/test/java/io/temporal/internal/nexus/TemporalInterceptorMiddlewareTest.java @@ -2,6 +2,7 @@ import static org.mockito.Mockito.mock; +import com.uber.m3.tally.NoopScope; import io.nexusrpc.OperationException; import io.nexusrpc.handler.OperationContext; import io.nexusrpc.handler.OperationHandler; @@ -15,7 +16,12 @@ import io.temporal.nexus.Nexus; import io.temporal.nexus.TemporalOperationHandler; import io.temporal.nexus.TemporalOperationResult; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.CancellationException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicReference; import org.junit.After; import org.junit.Assert; @@ -100,6 +106,51 @@ public void cancellationBeforeInterceptionReachesNexusOperationInfo() { Assert.assertEquals("early reason", exception.getMessage()); } + // NexusTaskHandlerImpl shares one middleware between all concurrent Nexus tasks of a worker, so + // each intercept call must wrap its own handler, not the handler of a concurrent call. + @Test + public void concurrentInterceptKeepsOwnHandler() throws Exception { + TemporalInterceptorMiddleware middleware = + new TemporalInterceptorMiddleware(new WorkerInterceptor[] {}); + OperationContext context = mock(OperationContext.class); + OperationStartDetails details = mock(OperationStartDetails.class); + WorkflowClient client = mock(WorkflowClient.class); + + ExecutorService executor = Executors.newFixedThreadPool(4); + List> misrouted = new ArrayList<>(); + for (int t = 0; t < 4; t++) { + String operation = "operation" + t; + OperationHandler handler = OperationHandler.sync((c, d, i) -> operation); + misrouted.add( + executor.submit( + () -> { + CurrentNexusOperationContext.set( + new InternalNexusOperationContext( + "namespace", "taskQueue", "endpoint", new NoopScope(), client)); + int count = 0; + for (int i = 0; i < 10_000; i++) { + Object result = + middleware + .intercept(context, handler) + .start(context, details, null) + .getSyncResult(); + if (!operation.equals(result)) { + count++; + } + } + CurrentNexusOperationContext.unset(); + return count; + })); + } + try { + for (Future f : misrouted) { + Assert.assertEquals(0, (int) f.get()); + } + } finally { + executor.shutdownNow(); + } + } + private static CancellationToken interceptAndGetCancellationToken( OperationMethodCanceller canceller) { OperationContext operationContext =