Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed the traffic label is lost in the HikariDataSource scenario #1719

Merged
merged 1 commit into from
Jan 2, 2025
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
Expand Up @@ -45,6 +45,8 @@ public abstract class AbstractThreadWrapper<T> {

private final boolean cannotTransmit;

private final long threadId;

/**
* Constructor
*
Expand All @@ -66,6 +68,7 @@ public AbstractThreadWrapper(Runnable runnable, Callable<T> callable, RequestTag
this.requestData = requestData;
}
this.cannotTransmit = cannotTransmit;
this.threadId = Thread.currentThread().getId();
}

/**
Expand Down Expand Up @@ -96,6 +99,9 @@ public T call() throws Exception {
}

private void before(Object obj) {
if (threadId == Thread.currentThread().getId()) {
return;
}
if (cannotTransmit) {
// If you enable normal thread pass through but do not enable thread pool pass through,
// you need to delete the data that is pass passed by InheritableThreadLocal before executing the method
Expand All @@ -117,6 +123,9 @@ private void before(Object obj) {
}

private void after() {
lilai23 marked this conversation as resolved.
Show resolved Hide resolved
if (threadId == Thread.currentThread().getId()) {
return;
}
ThreadLocalUtils.removeRequestTag();
ThreadLocalUtils.removeRequestData();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,24 @@ public class CallableWrapperTest extends BaseTest {
public void testCanTransmit() throws Exception {
Object obj = new Object();
CallableWrapper<Object> wrapper = new CallableWrapper<>(() -> {
Assert.assertNotNull(ThreadLocalUtils.getRequestData());
Assert.assertNotNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
return obj;
}, new RequestTag(null), new RequestData(null, null, null), false);

Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());

Assert.assertEquals(obj, wrapper.call());

Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
}

@Test
Expand All @@ -61,8 +67,10 @@ public void testCannotTransmit() throws Exception {

Object obj = new Object();
CallableWrapper<Object> wrapper = new CallableWrapper<>(() -> {
Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
return obj;
}, new RequestTag(null), new RequestData(null, null, null), true);

Expand All @@ -71,7 +79,9 @@ public void testCannotTransmit() throws Exception {

Assert.assertEquals(obj, wrapper.call());

Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,23 @@ public class RunnableAndCallableWrapperTest extends BaseTest {
@Test
public void testRunCanTransmit() {
RunnableAndCallableWrapper<Object> wrapper = new RunnableAndCallableWrapper<>(() -> {
Assert.assertNotNull(ThreadLocalUtils.getRequestData());
Assert.assertNotNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
}, null, new RequestTag(null), new RequestData(null, null, null), false);

Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());

wrapper.run();

Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
}

@Test
Expand All @@ -58,35 +64,45 @@ public void testRunCannotTransmit() {
Assert.assertNotNull(ThreadLocalUtils.getRequestTag());

RunnableWrapper<Object> wrapper = new RunnableWrapper<>(() -> {
Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
}, new RequestTag(null), new RequestData(null, null, null), true);

Assert.assertNull(ReflectUtils.getFieldValue(wrapper, "requestData").orElse(null));
Assert.assertNull(ReflectUtils.getFieldValue(wrapper, "requestTag").orElse(null));

wrapper.run();

Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
}

@Test
public void testCallCanTransmit() throws Exception {
Object obj = new Object();
RunnableAndCallableWrapper<Object> wrapper = new RunnableAndCallableWrapper<>(null, () -> {
Assert.assertNotNull(ThreadLocalUtils.getRequestData());
Assert.assertNotNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
return obj;
}, new RequestTag(null), new RequestData(null, null, null), false);

Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());

Assert.assertEquals(obj, wrapper.call());

Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
}

@Test
Expand All @@ -100,8 +116,10 @@ public void testCallCannotTransmit() throws Exception {

Object obj = new Object();
CallableWrapper<Object> wrapper = new CallableWrapper<>(() -> {
Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
return obj;
}, new RequestTag(null), new RequestData(null, null, null), true);

Expand All @@ -110,7 +128,9 @@ public void testCallCannotTransmit() throws Exception {

Assert.assertEquals(obj, wrapper.call());

Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,23 @@ public class RunnableWrapperTest extends BaseTest {
@Test
public void testCanTransmit() {
RunnableWrapper<?> wrapper = new RunnableWrapper<>(() -> {
Assert.assertNotNull(ThreadLocalUtils.getRequestData());
Assert.assertNotNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
}, new RequestTag(null), new RequestData(null, null, null), false);

Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());

wrapper.run();

Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
}

@Test
Expand All @@ -58,16 +64,20 @@ public void testCannotTransmit() {
Assert.assertNotNull(ThreadLocalUtils.getRequestTag());

RunnableWrapper<Object> wrapper = new RunnableWrapper<>(() -> {
Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
}, new RequestTag(null), new RequestData(null, null, null), true);

Assert.assertNull(ReflectUtils.getFieldValue(wrapper, "requestData").orElse(null));
Assert.assertNull(ReflectUtils.getFieldValue(wrapper, "requestTag").orElse(null));

wrapper.run();

Assert.assertNull(ThreadLocalUtils.getRequestData());
Assert.assertNull(ThreadLocalUtils.getRequestTag());
Assert.assertTrue(ThreadLocalUtils.getRequestData() == null
|| ThreadLocalUtils.getRequestData().getPath() == null);
Assert.assertTrue(ThreadLocalUtils.getRequestTag() == null
|| ThreadLocalUtils.getRequestTag().getTag().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public abstract class AbstractThreadWrapper<T> {

private final boolean cannotTransmit;

private final long threadId;

/**
* constructor
*
Expand All @@ -73,6 +75,7 @@ public AbstractThreadWrapper(Runnable runnable, Callable<T> callable, TrafficMes
}
this.cannotTransmit = cannotTransmit;
this.executorName = executorName;
this.threadId = Thread.currentThread().getId();
}

/**
Expand Down Expand Up @@ -108,6 +111,9 @@ public T call() throws Exception {
* @param obj thread object
*/
protected void before(Object obj) {
if (threadId == Thread.currentThread().getId()) {
return;
}
if (cannotTransmit) {
// When ordinary thread transparent transmission is enabled and thread pool transparent transmission is not
// enabled, the data transparently transmitted by InheritableThreadLocal needs to be deleted before
Expand All @@ -133,6 +139,9 @@ protected void before(Object obj) {
* Post method executed by thread object
*/
protected void after() {
if (threadId == Thread.currentThread().getId()) {
return;
}
TrafficUtils.removeTrafficTag();
TrafficUtils.removeTrafficData();
}
Expand Down
Loading