Skip to content

Commit 32df8d4

Browse files
Remove Nexus sync client handler (#2403)
1 parent fd65ea9 commit 32df8d4

29 files changed

+158
-138
lines changed

temporal-opentracing/src/test/java/io/temporal/opentracing/NexusOperationTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import io.temporal.client.WorkflowClient;
3535
import io.temporal.client.WorkflowClientOptions;
3636
import io.temporal.client.WorkflowOptions;
37+
import io.temporal.nexus.Nexus;
3738
import io.temporal.nexus.WorkflowClientOperationHandlers;
3839
import io.temporal.testing.internal.SDKTestWorkflowRule;
3940
import io.temporal.worker.WorkerFactoryOptions;
@@ -83,10 +84,14 @@ public class TestNexusServiceImpl {
8384
@OperationImpl
8485
public OperationHandler<String, String> operation() {
8586
return WorkflowClientOperationHandlers.fromWorkflowMethod(
86-
(context, details, client, input) ->
87-
client.newWorkflowStub(
88-
TestOtherWorkflow.class,
89-
WorkflowOptions.newBuilder().setWorkflowId(details.getRequestId()).build())
87+
(context, details, input) ->
88+
Nexus.getOperationContext()
89+
.getWorkflowClient()
90+
.newWorkflowStub(
91+
TestOtherWorkflow.class,
92+
WorkflowOptions.newBuilder()
93+
.setWorkflowId(details.getRequestId())
94+
.build())
9095
::workflow);
9196
}
9297
}

temporal-sdk/src/main/java/io/temporal/common/interceptors/NexusOperationInboundCallsInterceptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
* Intercepts inbound calls to a Nexus operation on the worker side.
2929
*
3030
* <p>An instance should be created in {@link
31-
* WorkerInterceptor#interceptNexusOperation(NexusOperationInboundCallsInterceptor)}.
31+
* WorkerInterceptor#interceptNexusOperation(OperationContext,
32+
* NexusOperationInboundCallsInterceptor)}.
3233
*
3334
* <p>Prefer extending {@link NexusOperationInboundCallsInterceptorBase} and overriding only the
3435
* methods you need instead of implementing this interface directly. {@link

temporal-sdk/src/main/java/io/temporal/common/interceptors/NexusOperationOutboundCallsInterceptor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package io.temporal.common.interceptors;
2222

2323
import com.uber.m3.tally.Scope;
24+
import io.temporal.client.WorkflowClient;
2425
import io.temporal.common.Experimental;
2526

2627
/**
@@ -41,4 +42,7 @@
4142
public interface NexusOperationOutboundCallsInterceptor {
4243
/** Intercepts call to get the metric scope in a Nexus operation. */
4344
Scope getMetricsScope();
45+
46+
/** Intercepts call to get the workflow client in a Nexus operation. */
47+
WorkflowClient getWorkflowClient();
4448
}

temporal-sdk/src/main/java/io/temporal/common/interceptors/NexusOperationOutboundCallsInterceptorBase.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package io.temporal.common.interceptors;
2222

2323
import com.uber.m3.tally.Scope;
24+
import io.temporal.client.WorkflowClient;
2425
import io.temporal.common.Experimental;
2526

2627
/** Convenience base class for {@link NexusOperationOutboundCallsInterceptor} implementations. */
@@ -37,4 +38,9 @@ public NexusOperationOutboundCallsInterceptorBase(NexusOperationOutboundCallsInt
3738
public Scope getMetricsScope() {
3839
return next.getMetricsScope();
3940
}
41+
42+
@Override
43+
public WorkflowClient getWorkflowClient() {
44+
return next.getWorkflowClient();
45+
}
4046
}

temporal-sdk/src/main/java/io/temporal/internal/nexus/CurrentNexusOperationContext.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
* used directly.
2626
*/
2727
public final class CurrentNexusOperationContext {
28-
private static final ThreadLocal<NexusOperationContextImpl> CURRENT = new ThreadLocal<>();
28+
private static final ThreadLocal<InternalNexusOperationContext> CURRENT = new ThreadLocal<>();
2929

30-
public static NexusOperationContextImpl get() {
31-
NexusOperationContextImpl result = CURRENT.get();
30+
public static InternalNexusOperationContext get() {
31+
InternalNexusOperationContext result = CURRENT.get();
3232
if (result == null) {
3333
throw new IllegalStateException(
3434
"NexusOperationContext can be used only inside of nexus operation handler "
@@ -37,7 +37,7 @@ public static NexusOperationContextImpl get() {
3737
return CURRENT.get();
3838
}
3939

40-
public static void set(NexusOperationContextImpl context) {
40+
public static void set(InternalNexusOperationContext context) {
4141
if (context == null) {
4242
throw new IllegalArgumentException("null context");
4343
}

temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusOperationContextImpl.java renamed to temporal-sdk/src/main/java/io/temporal/internal/nexus/InternalNexusOperationContext.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,23 @@
2525
import io.temporal.common.interceptors.NexusOperationOutboundCallsInterceptor;
2626
import io.temporal.nexus.NexusOperationContext;
2727

28-
public class NexusOperationContextImpl implements NexusOperationContext {
28+
public class InternalNexusOperationContext {
2929
private final String namespace;
3030
private final String taskQueue;
31+
private final Scope metricScope;
3132
private final WorkflowClient client;
3233
NexusOperationOutboundCallsInterceptor outboundCalls;
3334

34-
public NexusOperationContextImpl(
35-
String namespace,
36-
String taskQueue,
37-
WorkflowClient client,
38-
NexusOperationOutboundCallsInterceptor outboundCalls) {
35+
public InternalNexusOperationContext(
36+
String namespace, String taskQueue, Scope metricScope, WorkflowClient client) {
3937
this.namespace = namespace;
4038
this.taskQueue = taskQueue;
39+
this.metricScope = metricScope;
4140
this.client = client;
42-
this.outboundCalls = outboundCalls;
4341
}
4442

45-
@Override
4643
public Scope getMetricsScope() {
47-
return outboundCalls.getMetricsScope();
44+
return metricScope;
4845
}
4946

5047
public WorkflowClient getWorkflowClient() {
@@ -62,4 +59,23 @@ public String getNamespace() {
6259
public void setOutboundInterceptor(NexusOperationOutboundCallsInterceptor outboundCalls) {
6360
this.outboundCalls = outboundCalls;
6461
}
62+
63+
public NexusOperationContext getUserFacingContext() {
64+
if (outboundCalls == null) {
65+
throw new IllegalStateException("Outbound interceptor is not set");
66+
}
67+
return new NexusOperationContextImpl();
68+
}
69+
70+
private class NexusOperationContextImpl implements NexusOperationContext {
71+
@Override
72+
public Scope getMetricsScope() {
73+
return outboundCalls.getMetricsScope();
74+
}
75+
76+
@Override
77+
public WorkflowClient getWorkflowClient() {
78+
return outboundCalls.getWorkflowClient();
79+
}
80+
}
6581
}

temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusInternal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public final class NexusInternal {
2626
private NexusInternal() {}
2727

2828
public static NexusOperationContext getOperationContext() {
29-
return CurrentNexusOperationContext.get();
29+
return CurrentNexusOperationContext.get().getUserFacingContext();
3030
}
3131
}

temporal-sdk/src/main/java/io/temporal/internal/nexus/NexusTaskHandlerImpl.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,7 @@ public Result handle(NexusTask task, Scope metricsScope) throws TimeoutException
132132
}
133133

134134
CurrentNexusOperationContext.set(
135-
new NexusOperationContextImpl(
136-
namespace,
137-
taskQueue,
138-
client,
139-
new RootNexusOperationOutboundCallsInterceptor(metricsScope)));
135+
new InternalNexusOperationContext(namespace, taskQueue, metricsScope, client));
140136

141137
switch (request.getVariantCase()) {
142138
case START_OPERATION:

temporal-sdk/src/main/java/io/temporal/internal/nexus/RootNexusOperationOutboundCallsInterceptor.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,26 @@
2121
package io.temporal.internal.nexus;
2222

2323
import com.uber.m3.tally.Scope;
24+
import io.temporal.client.WorkflowClient;
2425
import io.temporal.common.interceptors.NexusOperationOutboundCallsInterceptor;
2526

2627
public class RootNexusOperationOutboundCallsInterceptor
2728
implements NexusOperationOutboundCallsInterceptor {
2829
private final Scope scope;
30+
private final WorkflowClient client;
2931

30-
RootNexusOperationOutboundCallsInterceptor(Scope scope) {
32+
RootNexusOperationOutboundCallsInterceptor(Scope scope, WorkflowClient client) {
3133
this.scope = scope;
34+
this.client = client;
3235
}
3336

3437
@Override
3538
public Scope getMetricsScope() {
3639
return scope;
3740
}
41+
42+
@Override
43+
public WorkflowClient getWorkflowClient() {
44+
return client;
45+
}
3846
}

temporal-sdk/src/main/java/io/temporal/internal/nexus/TemporalInterceptorMiddleware.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ public OperationHandler<Object, Object> intercept(
4444
interceptor.interceptNexusOperation(context, inboundCallsInterceptor);
4545
}
4646

47+
InternalNexusOperationContext temporalNexusContext = CurrentNexusOperationContext.get();
4748
inboundCallsInterceptor.init(
4849
new RootNexusOperationOutboundCallsInterceptor(
49-
CurrentNexusOperationContext.get().getMetricsScope()));
50+
temporalNexusContext.getMetricsScope(), temporalNexusContext.getWorkflowClient()));
5051
return new OperationInterceptorConverter(inboundCallsInterceptor);
5152
}
5253

0 commit comments

Comments
 (0)