[ 
https://issues.apache.org/jira/browse/SCB-1065?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16709718#comment-16709718
 ] 

ASF GitHub Bot commented on SCB-1065:
-------------------------------------

liubao68 closed pull request #1022: [SCB-1065] when request not contain 
traceId,should use provider's invocation's traceId
URL: https://github.com/apache/servicecomb-java-chassis/pull/1022
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
index 932aef4c3..08dc3802a 100644
--- 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
+++ 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
@@ -103,7 +103,7 @@ protected void setContext() throws Exception {
     @SuppressWarnings("unchecked")
     Map<String, String> cseContext =
         JsonUtils.readValue(strCseContext.getBytes(StandardCharsets.UTF_8), 
Map.class);
-    invocation.setContext(cseContext);
+    invocation.mergeContext(cseContext);
   }
 
   public String getContext(String key) {
diff --git 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
index faf37a539..d57ebe06c 100644
--- 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
+++ 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
@@ -250,6 +250,34 @@ public void setContextNormal() throws Exception {
     Assert.assertThat(invocation.getContext(), Matchers.hasEntry("name", 
"value"));
   }
 
+  @Test
+  public void setContextTraceId() throws Exception {
+    Map<String, String> context = new HashMap<>();
+    new Expectations() {
+      {
+        requestEx.getHeader(Const.CSE_CONTEXT);
+        result = JsonUtils.writeValueAsString(context);
+      }
+    };
+    invocation.addContext("X-B3-traceId", "value1");
+    //if request has no traceId, use invocation's traceId
+    restInvocation.setContext();
+    Assert.assertThat(invocation.getContext().size(), Matchers.is(1));
+    Assert.assertThat(invocation.getContext(), 
Matchers.hasEntry("X-B3-traceId", "value1"));
+
+    context.put("X-B3-traceId", "value2");
+    new Expectations() {
+      {
+        requestEx.getHeader(Const.CSE_CONTEXT);
+        result = JsonUtils.writeValueAsString(context);
+      }
+    };
+    //if request has traceId, use request's traceId
+    restInvocation.setContext();
+    Assert.assertThat(invocation.getContext().size(), Matchers.is(1));
+    Assert.assertThat(invocation.getContext(), 
Matchers.hasEntry("X-B3-traceId", "value2"));
+  }
+
   @Test
   public void getContext() {
     invocation.addContext("key", "test");
diff --git 
a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContext.java
 
b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContext.java
index 2edeca77e..0807beb1c 100644
--- 
a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContext.java
+++ 
b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/context/InvocationContext.java
@@ -19,6 +19,7 @@
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.Response.StatusType;
@@ -68,6 +69,24 @@ public void addContext(Map<String, String> otherContext) {
     context.putAll(otherContext);
   }
 
+  public void mergeContext(InvocationContext otherContext) {
+    mergeContext(otherContext.getContext());
+  }
+
+  public void mergeContext(Map<String, String> otherContext) {
+    if (otherContext == null) {
+      return;
+    }
+    if (otherContext.size() > context.size()) {
+      for (Entry<String, String> entry : context.entrySet()) {
+        otherContext.putIfAbsent(entry.getKey(), entry.getValue());
+      }
+      this.context = otherContext;
+      return;
+    }
+    context.putAll(otherContext);
+  }
+
   public Map<String, Object> getLocalContext() {
     return localContext;
   }
diff --git 
a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/context/TestInvocationContext.java
 
b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/context/TestInvocationContext.java
new file mode 100644
index 000000000..189b1af4f
--- /dev/null
+++ 
b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/context/TestInvocationContext.java
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.swagger.invocation.context;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.core.Response.Status.Family;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestInvocationContext {
+  @Test
+  public void addContext() {
+    InvocationContext invocationContext = new InvocationContext();
+    invocationContext.addContext("key1", "value1");
+    Assert.assertEquals(1, invocationContext.getContext().size());
+    Assert.assertEquals("value1", invocationContext.getContext("key1"));
+
+    Map<String, String> otherContext = new HashMap<>();
+    otherContext.put("key2", "value2");
+    invocationContext.addContext(otherContext);
+    Assert.assertEquals(2, invocationContext.getContext().size());
+    Assert.assertEquals("value2", invocationContext.getContext("key2"));
+
+    InvocationContext invocationContext2 = new InvocationContext();
+    Map<String, String> otherContext2 = new HashMap<>();
+    otherContext2.put("key3", "value3");
+    invocationContext2.context = otherContext2;
+    invocationContext.addContext(invocationContext2);
+    Assert.assertEquals(3, invocationContext.getContext().size());
+  }
+
+  @Test
+  public void mergeMapContext() {
+    InvocationContext invocationContext = new InvocationContext();
+    Map<String, String> otherContext = new HashMap<>();
+    otherContext.put("key1", "value1");
+    //otherContext's size is large than old.
+    invocationContext.mergeContext(otherContext);
+    Assert.assertEquals(1, invocationContext.getContext().size());
+    Assert.assertEquals("value1", invocationContext.getContext("key1"));
+
+    otherContext.put("key1", "value2");
+    //otherContext's size is not large than old.
+    invocationContext.mergeContext(otherContext);
+    Assert.assertEquals(1, invocationContext.getContext().size());
+    Assert.assertEquals("value2", invocationContext.getContext("key1"));
+  }
+
+  @Test
+  public void mergeInvocationContext() {
+    InvocationContext invocationContext = new InvocationContext();
+    Map<String, String> otherContext = new HashMap<>();
+    otherContext.put("key1", "value1");
+    InvocationContext context2 = new InvocationContext();
+    context2.context = otherContext;
+    invocationContext.mergeContext(context2);
+    Assert.assertEquals(1, invocationContext.getContext().size());
+    Assert.assertEquals("value1", invocationContext.getContext("key1"));
+  }
+
+  @Test
+  public void addLocalContext() {
+    InvocationContext invocationContext = new InvocationContext();
+    invocationContext.addLocalContext("key1", "value1");
+    Assert.assertEquals(1, invocationContext.getLocalContext().size());
+    Assert.assertEquals("value1", invocationContext.getLocalContext("key1"));
+
+    Map<String, Object> otherContext = new HashMap<>();
+    otherContext.put("key2", "value2");
+    invocationContext.addLocalContext(otherContext);
+    Assert.assertEquals(2, invocationContext.getLocalContext().size());
+    Assert.assertEquals("value2", invocationContext.getLocalContext("key2"));
+  }
+
+  @Test
+  public void setStatus() {
+    InvocationContext invocationContext = new InvocationContext();
+    invocationContext.setStatus(200);
+    System.out.println(invocationContext.getStatus().getFamily());
+    Assert.assertEquals(200, invocationContext.getStatus().getStatusCode());
+    Assert.assertEquals("OK", invocationContext.getStatus().getReasonPhrase());
+    Assert.assertEquals(Family.SUCCESSFUL, 
invocationContext.getStatus().getFamily());
+
+    invocationContext.setStatus(200, "TEST");
+    Assert.assertEquals(200, invocationContext.getStatus().getStatusCode());
+    Assert.assertEquals("TEST", 
invocationContext.getStatus().getReasonPhrase());
+    Assert.assertEquals(Family.SUCCESSFUL, 
invocationContext.getStatus().getFamily());
+  }
+}
diff --git 
a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayCodec.java
 
b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayCodec.java
index a51210a60..5d7395d1d 100644
--- 
a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayCodec.java
+++ 
b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayCodec.java
@@ -54,7 +54,7 @@ public static void decodeRequest(Invocation invocation, 
RequestHeader header, Op
     Object[] args = schema.readObject(bodyBuffer);
 
     invocation.setSwaggerArguments(args);
-    invocation.setContext(header.getContext());
+    invocation.mergeContext(header.getContext());
   }
 
   public static RequestHeader readRequestHeader(Buffer headerBuffer) throws 
Exception {
diff --git 
a/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayCodec.java
 
b/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayCodec.java
index 97282c5b6..a9932e2a1 100644
--- 
a/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayCodec.java
+++ 
b/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayCodec.java
@@ -37,6 +37,7 @@
 import org.apache.servicecomb.serviceregistry.ServiceRegistry;
 import org.apache.servicecomb.serviceregistry.registry.ServiceRegistryFactory;
 import org.apache.servicecomb.swagger.invocation.Response;
+import org.apache.servicecomb.swagger.invocation.context.InvocationContext;
 import org.apache.servicecomb.transport.highway.message.RequestHeader;
 import org.apache.servicecomb.transport.highway.message.ResponseHeader;
 import org.junit.After;
@@ -165,6 +166,26 @@ public void testDecodeResponse() throws Exception {
     Assert.assertEquals(200, response.getStatusCode());
   }
 
+  @Test
+  public void testDecodeRequestTraceId(@Mocked Endpoint endpoint) throws 
Exception {
+    commonMock();
+
+    Invocation invocation = new Invocation(endpoint, operationMeta, null);
+
+    invocation.addContext("X-B3-traceId", "test1");
+    Assert.assertEquals("test1", invocation.getContext("X-B3-traceId"));
+
+    RequestHeader headers = new RequestHeader();
+    Map<String, String> context = new HashMap<>();
+    headers.setContext(context);
+    HighwayCodec.decodeRequest(invocation, headers, operationProtobuf, 
bodyBuffer);
+    Assert.assertEquals("test1", invocation.getContext("X-B3-traceId"));
+
+    context.put("X-B3-traceId", "test2");
+    HighwayCodec.decodeRequest(invocation, headers, operationProtobuf, 
bodyBuffer);
+    Assert.assertEquals("test2", invocation.getContext("X-B3-traceId"));
+  }
+
   @Test
   public void testEncodeResponse() {
     boolean status = true;


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> when request not contain traceId,should use provider's invocation's traceId
> ---------------------------------------------------------------------------
>
>                 Key: SCB-1065
>                 URL: https://issues.apache.org/jira/browse/SCB-1065
>             Project: Apache ServiceComb
>          Issue Type: Bug
>          Components: Java-Chassis
>            Reporter: WeiChao
>            Assignee: WeiChao
>            Priority: Major
>             Fix For: java-chassis-1.2.0
>
>
> when request not contain traceId,should use provider's invocation's traceId



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to