Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.temporal.internal.nexus.system;

import io.temporal.common.converter.DataConverter;
import java.util.Objects;
import java.util.function.Supplier;

/** Scopes user-value conversion independently of generated bindings and system envelopes. */
public final class SystemNexusConverterContext {
private static final ThreadLocal<DataConverter> USER_CONVERTER = new ThreadLocal<>();

private SystemNexusConverterContext() {}

/** Returns the user converter active in the current synchronous transfer conversion scope. */
public static DataConverter currentDataConverter() {
DataConverter converter = USER_CONVERTER.get();
if (converter == null) {
throw new IllegalStateException("System Nexus user converter context is not active");
}
return converter;
}

/** Runs a synchronous conversion with the user converter and restores any enclosing scope. */
public static <T> T withDataConverter(DataConverter converter, Supplier<T> action) {
Objects.requireNonNull(converter, "converter");
DataConverter previous = USER_CONVERTER.get();
USER_CONVERTER.set(converter);
try {
return action.get();
} finally {
if (previous == null) {
USER_CONVERTER.remove();
} else {
USER_CONVERTER.set(previous);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,76 @@
import io.temporal.common.interceptors.WorkflowOutboundCallsInterceptor;
import io.temporal.common.metadata.POJOWorkflowInterfaceMetadata;
import io.temporal.common.metadata.POJOWorkflowMethodMetadata;
import io.temporal.common.metadata.WorkflowMethodType;
import io.temporal.internal.common.InternalUtils;
import io.temporal.workflow.ExternalWorkflowStub;
import io.temporal.workflow.Functions;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Objects;

/** Dynamic implementation of a strongly typed child workflow interface. */
class ExternalWorkflowInvocationHandler implements InvocationHandler {
/** Dynamic implementation of a strongly typed external workflow interface. */
public class ExternalWorkflowInvocationHandler implements InvocationHandler {

private static final ThreadLocal<ProbeContext> probeContext = new ThreadLocal<>();
private static final ThreadLocal<ProbeResult> probeResult = new ThreadLocal<>();

/** Probes one invocation without starting a workflow or sending a signal. */
public static ProbeResult probe(Functions.Proc invocation, WorkflowMethodType expected) {
Objects.requireNonNull(invocation, "invocation");
if (probeContext.get() != null) {
throw new IllegalStateException("Already probing an external workflow invocation");
}
ProbeContext context = new ProbeContext(expected);
probeContext.set(context);
try {
invocation.apply();
ProbeResult result = probeResult.get();
if (context.invocations != 1 || result == null) {
throw new IllegalArgumentException(
"Expected exactly one external workflow " + expected + " invocation");
}
return result;
} finally {
probeResult.remove();
probeContext.remove();
}
}

private static final class ProbeContext {
private final WorkflowMethodType expected;
private int invocations;

private ProbeContext(WorkflowMethodType expected) {
this.expected = Objects.requireNonNull(expected, "expected");
}
}

/** Metadata captured from a bound external workflow method. */
public static final class ProbeResult {
private final ExternalWorkflowStub stub;
private final Object[] arguments;
private final POJOWorkflowMethodMetadata metadata;

private ProbeResult(
ExternalWorkflowStub stub, POJOWorkflowMethodMetadata metadata, Object[] args) {
this.stub = stub;
this.arguments = args == null ? new Object[0] : args.clone();
this.metadata = metadata;
}

public ExternalWorkflowStub getStub() {
return stub;
}

public Object[] getArguments() {
return arguments.clone();
}

public POJOWorkflowMethodMetadata getMetadata() {
return metadata;
}
}

private final ExternalWorkflowStub stub;
private final POJOWorkflowInterfaceMetadata workflowMetadata;
Expand Down Expand Up @@ -40,6 +103,15 @@ public Object invoke(Object proxy, Method method, Object[] args) {
return stub;
}
POJOWorkflowMethodMetadata methodMetadata = workflowMetadata.getMethodMetadata(method);
ProbeContext context = probeContext.get();
if (context != null) {
if (++context.invocations != 1 || methodMetadata.getType() != context.expected) {
throw new IllegalArgumentException(
"Expected exactly one external workflow " + context.expected + " invocation");
}
probeResult.set(new ProbeResult(stub, methodMetadata, args));
return InternalUtils.getValueOrDefault(null, method.getReturnType());
}
switch (methodMetadata.getType()) {
case QUERY:
throw new UnsupportedOperationException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.temporal.internal.nexus.system;

import static org.junit.Assert.*;

import io.temporal.common.converter.CodecDataConverter;
import io.temporal.common.converter.DataConverter;
import io.temporal.common.converter.DefaultDataConverter;
import java.util.Collections;
import java.util.concurrent.*;
import org.junit.Test;

public class SystemNexusConverterContextTest {
@Test
public void restoresNestedScopesAfterSuccessAndFailure() {
DataConverter outer = DefaultDataConverter.STANDARD_INSTANCE;
DataConverter inner = new CodecDataConverter(outer, Collections.emptyList());
SystemNexusConverterContext.withDataConverter(
outer,
() -> {
assertSame(outer, SystemNexusConverterContext.currentDataConverter());
assertSame(
inner,
SystemNexusConverterContext.withDataConverter(
inner, SystemNexusConverterContext::currentDataConverter));
assertSame(outer, SystemNexusConverterContext.currentDataConverter());
assertThrows(
IllegalArgumentException.class,
() ->
SystemNexusConverterContext.withDataConverter(
inner,
() -> {
throw new IllegalArgumentException("failure");
}));
assertSame(outer, SystemNexusConverterContext.currentDataConverter());
return null;
});
assertThrows(IllegalStateException.class, SystemNexusConverterContext::currentDataConverter);
assertThrows(
NullPointerException.class,
() -> SystemNexusConverterContext.withDataConverter(null, () -> null));
assertThrows(IllegalStateException.class, SystemNexusConverterContext::currentDataConverter);
}

@Test
public void isolatesConcurrentConversionScopes() throws Exception {
DataConverter first = DefaultDataConverter.STANDARD_INSTANCE;
DataConverter second = new CodecDataConverter(first, Collections.emptyList());
CyclicBarrier barrier = new CyclicBarrier(2);
ExecutorService executor = Executors.newFixedThreadPool(2);
try {
Future<DataConverter> a = executor.submit(() -> scopedConverter(first, barrier));
Future<DataConverter> b = executor.submit(() -> scopedConverter(second, barrier));
assertSame(first, a.get(10, TimeUnit.SECONDS));
assertSame(second, b.get(10, TimeUnit.SECONDS));
} finally {
executor.shutdownNow();
}
assertThrows(IllegalStateException.class, SystemNexusConverterContext::currentDataConverter);
}

private DataConverter scopedConverter(DataConverter converter, CyclicBarrier barrier) {
DataConverter result =
SystemNexusConverterContext.withDataConverter(
converter,
() -> {
try {
barrier.await(5, TimeUnit.SECONDS);
} catch (Exception e) {
throw new RuntimeException(e);
}
return SystemNexusConverterContext.currentDataConverter();
});
assertThrows(IllegalStateException.class, SystemNexusConverterContext::currentDataConverter);
return result;
}
}
Loading
Loading