Skip to content

Commit

Permalink
Fix remove tag issue for DubboProviderInterceptor in consumer side, i…
Browse files Browse the repository at this point in the history
…t will cause previously traffic tag removed and the follow up invocation can not get traffic tag from thread local

Signed-off-by: rztao <[email protected]>
  • Loading branch information
rztao authored and chengyouling committed Nov 15, 2024
1 parent 87035c5 commit ec2d183
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ protected ExecuteContext doBefore(ExecuteContext context) {

@Override
protected ExecuteContext doAfter(ExecuteContext context) {
if (isConsumer(context)) {
return context;
}

TrafficUtils.removeTrafficTag();
return context;
}
Expand Down Expand Up @@ -123,6 +127,9 @@ protected Map<String, List<String>> extractTrafficTagFromCarrier(RpcInvocation i

@Override
public ExecuteContext onThrow(ExecuteContext context) {
if (isConsumer(context)) {
return context;
}
TrafficUtils.removeTrafficTag();
return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ public void testAlibabaDubboProvider() {
interceptor.after(returnContext);
}

@Test
public void testConsumerSideRemoveTrafficTag() {
// If interceptor is invoked in consumer side, it should not remove traffic tag in after method.
Map<String, List<String>> expectTag = buildExpectTrafficTag("id", "name");
TrafficUtils.setTrafficTag(new TrafficTag(expectTag));

ExecuteContext context = buildContext(new RpcInvocation(), new HashMap<>(), "consumer");
interceptor.before(context);
Assert.assertEquals(TrafficUtils.getTrafficTag().getTag(), expectTag);
interceptor.after(context);
Assert.assertEquals(TrafficUtils.getTrafficTag().getTag(), expectTag);
}

@Test
public void testProviderSideRemoveTrafficTag() {
// If interceptor is invoked in provider side, it should remove traffic tag in after method.
Map<String, List<String>> expectTag = buildExpectTrafficTag("id", "name");
TrafficUtils.setTrafficTag(new TrafficTag(expectTag));

ExecuteContext context = buildContext(new RpcInvocation(), new HashMap<>(), "provider");
interceptor.before(context);
Assert.assertEquals(TrafficUtils.getTrafficTag().getTag(), expectTag);
interceptor.after(context);
Assert.assertNull(TrafficUtils.getTrafficTag());
}

private ExecuteContext buildContext(RpcInvocation rpcInvocation, Map<String, String> headers, String side) {
URL url = new URL("http", "127.0.0.1", 8080);
url = url.addParameter("side", side);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,18 @@ protected ExecuteContext doBefore(ExecuteContext context) {

@Override
protected ExecuteContext doAfter(ExecuteContext context) {
if (isConsumer(context)) {
return context;
}
TrafficUtils.removeTrafficTag();
return context;
}

@Override
public ExecuteContext onThrow(ExecuteContext context) {
if (isConsumer(context)) {
return context;
}
TrafficUtils.removeTrafficTag();
return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,32 @@ public void testApacheDubboProvider() {
interceptor.after(returnContext);
}

@Test
public void testConsumerSideRemoveTrafficTag() {
// If interceptor is invoked in consumer side, it should not remove traffic tag in after method.
Map<String, List<String>> expectTag = buildExpectTrafficTag("id", "name");
TrafficUtils.setTrafficTag(new TrafficTag(expectTag));

ExecuteContext context = buildContext(new RpcInvocation(), new HashMap<>(), "consumer");
interceptor.before(context);
Assert.assertEquals(TrafficUtils.getTrafficTag().getTag(), expectTag);
interceptor.after(context);
Assert.assertEquals(TrafficUtils.getTrafficTag().getTag(), expectTag);
}

@Test
public void testProviderSideRemoveTrafficTag() {
// If interceptor is invoked in provider side, it should remove traffic tag in after method.
Map<String, List<String>> expectTag = buildExpectTrafficTag("id", "name");
TrafficUtils.setTrafficTag(new TrafficTag(expectTag));

ExecuteContext context = buildContext(new RpcInvocation(), new HashMap<>(), "provider");
interceptor.before(context);
Assert.assertEquals(TrafficUtils.getTrafficTag().getTag(), expectTag);
interceptor.after(context);
Assert.assertNull(TrafficUtils.getTrafficTag());
}

private ExecuteContext buildContext(RpcInvocation rpcInvocation, Map<String, String> headers, String side) {
URL url = new URL("http", "127.0.0.1", 8080);
url = url.addParameter("side", side);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,18 @@ protected ExecuteContext doBefore(ExecuteContext context) {

@Override
protected ExecuteContext doAfter(ExecuteContext context) {
if (isConsumer(context)) {
return context;
}
TrafficUtils.removeTrafficTag();
return context;
}

@Override
public ExecuteContext onThrow(ExecuteContext context) {
if (isConsumer(context)) {
return context;
}
TrafficUtils.removeTrafficTag();
return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,32 @@ public void testApacheDubboProvider() {
interceptor.after(returnContext);
}

@Test
public void testConsumerSideRemoveTrafficTag() {
// If interceptor is invoked in consumer side, it should not remove traffic tag in after method.
Map<String, List<String>> expectTag = buildExpectTrafficTag("id", "name");
TrafficUtils.setTrafficTag(new TrafficTag(expectTag));

ExecuteContext context = buildContext(new RpcInvocation(), new HashMap<>(), "consumer");
interceptor.before(context);
Assert.assertEquals(TrafficUtils.getTrafficTag().getTag(), expectTag);
interceptor.after(context);
Assert.assertEquals(TrafficUtils.getTrafficTag().getTag(), expectTag);
}

@Test
public void testProviderSideRemoveTrafficTag() {
// If interceptor is invoked in provider side, it should remove traffic tag in after method.
Map<String, List<String>> expectTag = buildExpectTrafficTag("id", "name");
TrafficUtils.setTrafficTag(new TrafficTag(expectTag));

ExecuteContext context = buildContext(new RpcInvocation(), new HashMap<>(), "provider");
interceptor.before(context);
Assert.assertEquals(TrafficUtils.getTrafficTag().getTag(), expectTag);
interceptor.after(context);
Assert.assertNull(TrafficUtils.getTrafficTag());
}

private ExecuteContext buildContext(RpcInvocation rpcInvocation, Map<String, String> headers, String side) {
URL url = new URL("http", "127.0.0.1", 8080);
url = url.addParameter("side", side);
Expand Down

0 comments on commit ec2d183

Please sign in to comment.