You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/12/05 08:07:56 UTC

[servicecomb-java-chassis] branch master updated (dc0f8ae -> 28be813)

This is an automated email from the ASF dual-hosted git repository.

liubao pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git.


    from dc0f8ae  [SCB-1066] when start error, destroy method may throw an exception lead to origin exception losed
     new 2e21d07  [SCB-1065] when request not contain traceId,should use provider's invocation's traceId
     new 28be813  [SCB-1065] highway also modify,use mergeContext to handler the new and old context

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../common/rest/AbstractRestInvocation.java        |   2 +-
 .../common/rest/TestAbstractRestInvocation.java    |  28 ++++++
 .../invocation/context/InvocationContext.java      |  19 ++++
 .../invocation/context/TestInvocationContext.java  | 106 +++++++++++++++++++++
 .../transport/highway/HighwayCodec.java            |   2 +-
 .../transport/highway/TestHighwayCodec.java        |  21 ++++
 6 files changed, 176 insertions(+), 2 deletions(-)
 create mode 100644 swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/context/TestInvocationContext.java


[servicecomb-java-chassis] 01/02: [SCB-1065] when request not contain traceId, should use provider's invocation's traceId

Posted by li...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git

commit 2e21d07fd76de3b25cfafef7cbba3c8dd3c9aa70
Author: weichao666 <we...@huawei.com>
AuthorDate: Tue Dec 4 16:34:07 2018 +0800

    [SCB-1065] when request not contain traceId,should use provider's invocation's traceId
---
 .../common/rest/AbstractRestInvocation.java        |  2 +-
 .../common/rest/TestAbstractRestInvocation.java    | 28 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

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 d2b08aa..42a9972 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
@@ -105,7 +105,7 @@ public abstract class AbstractRestInvocation {
     @SuppressWarnings("unchecked")
     Map<String, String> cseContext =
         JsonUtils.readValue(strCseContext.getBytes(StandardCharsets.UTF_8), Map.class);
-    invocation.setContext(cseContext);
+    invocation.addContext(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 5a8c859..958fbef 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
@@ -254,6 +254,34 @@ public class TestAbstractRestInvocation {
   }
 
   @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");
     assertEquals("test", restInvocation.getContext("key"));


[servicecomb-java-chassis] 02/02: [SCB-1065] highway also modify, use mergeContext to handler the new and old context

Posted by li...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git

commit 28be813434bba2c7245e219366eb05e934253378
Author: weichao666 <we...@huawei.com>
AuthorDate: Tue Dec 4 18:51:38 2018 +0800

    [SCB-1065] highway also modify,use mergeContext to handler the new and old context
---
 .../common/rest/AbstractRestInvocation.java        |   2 +-
 .../invocation/context/InvocationContext.java      |  19 ++++
 .../invocation/context/TestInvocationContext.java  | 106 +++++++++++++++++++++
 .../transport/highway/HighwayCodec.java            |   2 +-
 .../transport/highway/TestHighwayCodec.java        |  21 ++++
 5 files changed, 148 insertions(+), 2 deletions(-)

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 42a9972..3908bb5 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
@@ -105,7 +105,7 @@ public abstract class AbstractRestInvocation {
     @SuppressWarnings("unchecked")
     Map<String, String> cseContext =
         JsonUtils.readValue(strCseContext.getBytes(StandardCharsets.UTF_8), Map.class);
-    invocation.addContext(cseContext);
+    invocation.mergeContext(cseContext);
   }
 
   public String getContext(String key) {
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 2edeca7..0807beb 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 @@ package org.apache.servicecomb.swagger.invocation.context;
 
 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 class InvocationContext {
     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 0000000..189b1af
--- /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 a51210a..5d7395d 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 final class HighwayCodec {
     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 97282c5..a9932e2 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.RegistryUtils;
 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;
@@ -166,6 +167,26 @@ public class TestHighwayCodec {
   }
 
   @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;
     WrapSchema bodySchema = Mockito.mock(WrapSchema.class);