From cd8bb17117d9d1e838dabb27950b8fc5dc56c772 Mon Sep 17 00:00:00 2001 From: evenliu Date: Fri, 10 May 2024 15:16:06 +0800 Subject: [PATCH] fix baggage do not pick up to RpcInvokeContext problem (#1402) Co-authored-by: liujianjun.ljj --- .../alipay/sofa/rpc/codec/bolt/SofaRpcSerialization.java | 3 +++ .../sofa/rpc/codec/bolt/SofaRpcSerializationTest.java | 9 +++++++++ .../src/test/resources/sofa-rpc/rpc-config.json | 3 ++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/remoting/remoting-bolt/src/main/java/com/alipay/sofa/rpc/codec/bolt/SofaRpcSerialization.java b/remoting/remoting-bolt/src/main/java/com/alipay/sofa/rpc/codec/bolt/SofaRpcSerialization.java index 655c8fd87..297243824 100644 --- a/remoting/remoting-bolt/src/main/java/com/alipay/sofa/rpc/codec/bolt/SofaRpcSerialization.java +++ b/remoting/remoting-bolt/src/main/java/com/alipay/sofa/rpc/codec/bolt/SofaRpcSerialization.java @@ -312,6 +312,9 @@ protected void parseRequestHeader(Map headerMap, Object sofaRequ if (sofaRequest instanceof SofaRequest) { // 处理 tracer parseRequestHeader(RemotingConstants.RPC_TRACE_NAME, headerMap, (SofaRequest) sofaRequest); + if (RpcInvokeContext.isBaggageEnable()) { + parseRequestHeader(RemotingConstants.RPC_REQUEST_BAGGAGE, headerMap, (SofaRequest) sofaRequest); + } Map requestProps = ((SofaRequest) sofaRequest).getRequestProps(); if (requestProps == null) { for (Map.Entry entry : headerMap.entrySet()) { diff --git a/remoting/remoting-bolt/src/test/java/com/alipay/sofa/rpc/codec/bolt/SofaRpcSerializationTest.java b/remoting/remoting-bolt/src/test/java/com/alipay/sofa/rpc/codec/bolt/SofaRpcSerializationTest.java index 646f12200..2283ec307 100644 --- a/remoting/remoting-bolt/src/test/java/com/alipay/sofa/rpc/codec/bolt/SofaRpcSerializationTest.java +++ b/remoting/remoting-bolt/src/test/java/com/alipay/sofa/rpc/codec/bolt/SofaRpcSerializationTest.java @@ -25,6 +25,7 @@ import com.alipay.sofa.rpc.core.request.SofaRequest; import org.junit.Assert; import org.junit.Test; + import java.util.HashMap; import java.util.Map; @@ -92,16 +93,24 @@ public void testParseRequestHeader(){ headerMap.put("testKey1","testValue1"); headerMap.put("rpc_trace_context.sofaTraceId", "traceId"); headerMap.put("rpc_trace_context.sofaRpcId", "rpcId"); + headerMap.put("rpc_req_baggage.testBaggageKey1", "testBaggageValue1"); + headerMap.put("rpc_req_baggage.testBaggageKey2", "testBaggageValue2"); SofaRpcSerialization sofaRpcSerialization = new SofaRpcSerialization(); SofaRequest sofaRequest = new SofaRequest(); sofaRequest.addRequestProp("testKey1", "testValue11"); sofaRequest.addRequestProp("testKey2", "testValue2"); sofaRpcSerialization.parseRequestHeader(headerMap, sofaRequest); + sofaRpcSerialization.parseRequestHeader(headerMap, sofaRequest); Assert.assertEquals("testValue1", sofaRequest.getRequestProp("testKey1")); Assert.assertEquals("testValue2", sofaRequest.getRequestProp("testKey2")); Object traceMap = sofaRequest.getRequestProp(RemotingConstants.RPC_TRACE_NAME); Assert.assertTrue(traceMap instanceof Map); Assert.assertEquals("traceId",((Map)traceMap).get("sofaTraceId")); Assert.assertEquals("rpcId",((Map)traceMap).get("sofaRpcId")); + Object baggageMap = sofaRequest.getRequestProp(RemotingConstants.RPC_REQUEST_BAGGAGE); + Assert.assertTrue(baggageMap instanceof Map); + Assert.assertEquals("testBaggageValue1", ((Map) baggageMap).get("testBaggageKey1")); + Assert.assertEquals("testBaggageValue2", ((Map) baggageMap).get("testBaggageKey2")); + } } \ No newline at end of file diff --git a/remoting/remoting-bolt/src/test/resources/sofa-rpc/rpc-config.json b/remoting/remoting-bolt/src/test/resources/sofa-rpc/rpc-config.json index 5b573549a..edd0e9cb4 100644 --- a/remoting/remoting-bolt/src/test/resources/sofa-rpc/rpc-config.json +++ b/remoting/remoting-bolt/src/test/resources/sofa-rpc/rpc-config.json @@ -1,4 +1,5 @@ { "rpc.config.order": 999, - "logger.impl": "com.alipay.sofa.rpc.log.SLF4JLoggerImpl" + "logger.impl": "com.alipay.sofa.rpc.log.SLF4JLoggerImpl", + "invoke.baggage.enable": true } \ No newline at end of file