Skip to content

Commit 35386da

Browse files
Clear MDC context after each task (#2545)
1 parent dc1e26d commit 35386da

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

temporal-sdk/src/main/java/io/temporal/internal/worker/PollTaskExecutor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ public void process(@Nonnull T task) {
8181
.uncaughtException(Thread.currentThread(), handler.wrapFailure(task, e));
8282
}
8383
} finally {
84-
MDC.remove(LoggerTag.NAMESPACE);
85-
MDC.remove(LoggerTag.TASK_QUEUE);
84+
MDC.clear();
8685
}
8786
});
8887
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.temporal.worker;
2+
3+
import static org.junit.Assert.assertNull;
4+
5+
import io.temporal.activity.ActivityInterface;
6+
import io.temporal.activity.ActivityMethod;
7+
import io.temporal.testing.internal.SDKTestOptions;
8+
import io.temporal.testing.internal.SDKTestWorkflowRule;
9+
import io.temporal.workflow.Workflow;
10+
import io.temporal.workflow.shared.TestWorkflows;
11+
import org.junit.Rule;
12+
import org.junit.Test;
13+
import org.slf4j.MDC;
14+
15+
public class MdcClearedBetweenTasksTest {
16+
@Rule
17+
public SDKTestWorkflowRule testWorkflowRule =
18+
SDKTestWorkflowRule.newBuilder()
19+
.setWorkflowTypes(TestWorkflowImpl.class)
20+
.setActivityImplementations(new SetMdcActivityImpl(), new GetMdcActivityImpl())
21+
.build();
22+
23+
@Test
24+
public void testMdcClearedBetweenActivities() {
25+
TestWorkflows.TestWorkflowReturnString workflow =
26+
testWorkflowRule.newWorkflowStub(TestWorkflows.TestWorkflowReturnString.class);
27+
String result = workflow.execute();
28+
assertNull(result);
29+
}
30+
31+
public static class TestWorkflowImpl implements TestWorkflows.TestWorkflowReturnString {
32+
private final SetMdcActivity setMdcActivity =
33+
Workflow.newActivityStub(
34+
SetMdcActivity.class, SDKTestOptions.newActivityOptions20sScheduleToClose());
35+
private final GetMdcActivity getMdcActivity =
36+
Workflow.newActivityStub(
37+
GetMdcActivity.class, SDKTestOptions.newActivityOptions20sScheduleToClose());
38+
39+
@Override
40+
public String execute() {
41+
setMdcActivity.execute();
42+
return getMdcActivity.execute();
43+
}
44+
}
45+
46+
@ActivityInterface
47+
public interface SetMdcActivity {
48+
@ActivityMethod(name = "setMdc")
49+
void execute();
50+
}
51+
52+
public static class SetMdcActivityImpl implements SetMdcActivity {
53+
@Override
54+
public void execute() {
55+
MDC.put("mdcTest", "value");
56+
}
57+
}
58+
59+
@ActivityInterface
60+
public interface GetMdcActivity {
61+
@ActivityMethod(name = "getMdc")
62+
String execute();
63+
}
64+
65+
public static class GetMdcActivityImpl implements GetMdcActivity {
66+
@Override
67+
public String execute() {
68+
return MDC.get("mdcTest");
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)