Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

public class TemporalInterceptorMiddleware implements OperationMiddleware {
private final WorkerInterceptor[] interceptors;
RootNexusOperationInboundCallsInterceptor rootInboundCallsInterceptor;

public TemporalInterceptorMiddleware(WorkerInterceptor[] interceptors) {
this.interceptors = interceptors;
Expand All @@ -19,7 +18,8 @@ public TemporalInterceptorMiddleware(WorkerInterceptor[] interceptors) {
@Override
public OperationHandler<Object, Object> intercept(
OperationContext context, OperationHandler<Object, Object> operationHandler) {
rootInboundCallsInterceptor = new RootNexusOperationInboundCallsInterceptor(operationHandler);
RootNexusOperationInboundCallsInterceptor rootInboundCallsInterceptor =
new RootNexusOperationInboundCallsInterceptor(operationHandler);
NexusOperationInboundCallsInterceptor inboundCallsInterceptor = rootInboundCallsInterceptor;
for (WorkerInterceptor interceptor : interceptors) {
inboundCallsInterceptor =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Future<Integer>> misrouted = new ArrayList<>();
for (int t = 0; t < 4; t++) {
String operation = "operation" + t;
OperationHandler<Object, Object> 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<Integer> f : misrouted) {
Assert.assertEquals(0, (int) f.get());
}
} finally {
executor.shutdownNow();
}
}

private static CancellationToken<CancellationException> interceptAndGetCancellationToken(
OperationMethodCanceller canceller) {
OperationContext operationContext =
Expand Down