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 2022/09/20 04:05:07 UTC

[servicecomb-java-chassis] branch master updated: [SCB-2686]fix date time from edge not parsed by highway problem (#3348)

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


The following commit(s) were added to refs/heads/master by this push:
     new 6030685f4 [SCB-2686]fix date time from edge not parsed by highway problem (#3348)
6030685f4 is described below

commit 6030685f47cb5e06e939031f6e0aa331c629eeea
Author: liubao68 <bi...@qq.com>
AuthorDate: Tue Sep 20 12:05:01 2022 +0800

    [SCB-2686]fix date time from edge not parsed by highway problem (#3348)
---
 .../servicecomb/core/governance/MatchType.java     | 14 +++--
 .../demo/zeroconfig/client/ClientModel.java        | 31 +++++++++++
 .../zeroconfig/client/ClientServerEndpoint.java    |  5 ++
 .../src/main/resources/application.yml             |  2 +
 .../demo/zeroconfig/tests/ClientModel.java         | 31 +++++++++++
 .../demo/zeroconfig/tests/ServerTest.java          | 25 +++++++++
 .../ClientServerEndpoint.yaml                      | 63 +++++++++++++++++++++-
 .../serializer/scalar/Int64WriteSchemas.java       | 34 +++++++-----
 .../protobuf/TestISODateTimeParsing.java           | 43 +++++++++++++++
 .../foundation/vertx/tcp/TcpConnection.java        |  6 ++-
 10 files changed, 234 insertions(+), 20 deletions(-)

diff --git a/core/src/main/java/org/apache/servicecomb/core/governance/MatchType.java b/core/src/main/java/org/apache/servicecomb/core/governance/MatchType.java
index d680b5578..a02a61bae 100644
--- a/core/src/main/java/org/apache/servicecomb/core/governance/MatchType.java
+++ b/core/src/main/java/org/apache/servicecomb/core/governance/MatchType.java
@@ -40,10 +40,16 @@ public final class MatchType {
         request.setHeaders(getHeaderMap(invocation, true));
         return request;
       }
-      request.setUri(invocation.getRequestEx().getRequestURI());
-      request.setMethod(invocation.getRequestEx().getMethod());
-      request.setHeaders(getHeaderMap(invocation, false));
-      return request;
+
+      // not highway
+      if (invocation.getRequestEx() != null) {
+        request.setUri(invocation.getRequestEx().getRequestURI());
+        request.setMethod(invocation.getRequestEx().getMethod());
+        request.setHeaders(getHeaderMap(invocation, false));
+        return request;
+      }
+
+      // maybe highway
     }
 
     if (invocation.isConsumer()) {
diff --git a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/ClientModel.java b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/ClientModel.java
new file mode 100644
index 000000000..ed8458a57
--- /dev/null
+++ b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/ClientModel.java
@@ -0,0 +1,31 @@
+/*
+ * 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.demo.zeroconfig.client;
+
+import java.util.Date;
+
+public class ClientModel {
+  private Date updateDate;
+
+  public Date getUpdateDate() {
+    return updateDate;
+  }
+
+  public void setUpdateDate(Date updateDate) {
+    this.updateDate = updateDate;
+  }
+}
diff --git a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/ClientServerEndpoint.java b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/ClientServerEndpoint.java
index 33d0e22ba..b68f18649 100644
--- a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/ClientServerEndpoint.java
+++ b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/java/org/apache/servicecomb/demo/zeroconfig/client/ClientServerEndpoint.java
@@ -83,4 +83,9 @@ public class ClientServerEndpoint {
   public String getString(@RawJsonRequestBody String jsonString) {
     return jsonString;
   }
+
+  @PostMapping(path = "/postModel")
+  public ClientModel postModel(@RequestBody ClientModel clientModel) {
+    return clientModel;
+  }
 }
diff --git a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/resources/application.yml b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/resources/application.yml
index c663b708b..b46710609 100644
--- a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/resources/application.yml
+++ b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-client/src/main/resources/application.yml
@@ -27,6 +27,8 @@ service_description:
 servicecomb:
   rest:
     address: 0.0.0.0:8082
+  highway:
+    address: 0.0.0.0:8084
   config:
     client:
       # for testing dynamic configuration
diff --git a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/java/org/apache/servicecomb/demo/zeroconfig/tests/ClientModel.java b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/java/org/apache/servicecomb/demo/zeroconfig/tests/ClientModel.java
new file mode 100644
index 000000000..b664eef31
--- /dev/null
+++ b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/java/org/apache/servicecomb/demo/zeroconfig/tests/ClientModel.java
@@ -0,0 +1,31 @@
+/*
+ * 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.demo.zeroconfig.tests;
+
+import java.util.Date;
+
+public class ClientModel {
+  private Date updateDate;
+
+  public Date getUpdateDate() {
+    return updateDate;
+  }
+
+  public void setUpdateDate(Date updateDate) {
+    this.updateDate = updateDate;
+  }
+}
diff --git a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/java/org/apache/servicecomb/demo/zeroconfig/tests/ServerTest.java b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/java/org/apache/servicecomb/demo/zeroconfig/tests/ServerTest.java
index 21585d8f2..f7333bab9 100644
--- a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/java/org/apache/servicecomb/demo/zeroconfig/tests/ServerTest.java
+++ b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/java/org/apache/servicecomb/demo/zeroconfig/tests/ServerTest.java
@@ -17,7 +17,9 @@
 
 package org.apache.servicecomb.demo.zeroconfig.tests;
 
+import java.util.Date;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.CountDownLatch;
 
 import org.apache.servicecomb.demo.CategorizedTestCase;
@@ -39,6 +41,29 @@ public class ServerTest implements CategorizedTestCase {
     testGetAllMicroservice();
     testJsonObject();
     testString();
+    testDateForEdge();
+  }
+
+  @SuppressWarnings("unchecked")
+  private void testDateForEdge() {
+    for (int i = 0; i < 3; i++) {
+      ClientModel clientModelReq = new ClientModel();
+      Date date = new Date(1663590135202L);
+      clientModelReq.setUpdateDate(date);
+      Map<String, Object> response = template
+          .postForObject(
+              "cse://demo-zeroconfig-schemadiscovery-registry-edge"
+                  + "/register/url/prefix/postModel", clientModelReq,
+              Map.class);
+      Object result = response.get("updateDate");
+      // TODO: highway and rest Date field will give different result
+      // we can not change this now, because it is incompatible
+      if (result instanceof Long) {
+        TestMgr.check(response.get("updateDate"), 1663590135202L);
+      } else {
+        TestMgr.check(response.get("updateDate"), "2022-09-19T12:22:15.202+00:00");
+      }
+    }
   }
 
   private void testServerGetName() throws Exception {
diff --git a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/resources/microservices/demo-zeroconfig-schemadiscovery-registry-edge/ClientServerEndpoint.yaml b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/resources/microservices/demo-zeroconfig-schemadiscovery-registry-edge/ClientServerEndpoint.yaml
index 3243bbd8d..fcfe266d8 100644
--- a/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/resources/microservices/demo-zeroconfig-schemadiscovery-registry-edge/ClientServerEndpoint.yaml
+++ b/demo/demo-zeroconfig-schemadiscovery-registry/demo-zeroconfig-schemadiscovery-registry-tests/src/main/resources/microservices/demo-zeroconfig-schemadiscovery-registry-edge/ClientServerEndpoint.yaml
@@ -52,4 +52,65 @@ paths:
             type: "array"
             items:
               type: "string"
-            uniqueItems: true
\ No newline at end of file
+            uniqueItems: true
+  /getString:
+    post:
+      operationId: "getString"
+      parameters:
+        - in: "body"
+          name: "jsonString"
+          required: true
+          schema:
+            type: "string"
+          x-raw-json: true
+      responses:
+        "200":
+          description: "response of 200"
+          schema:
+            type: "string"
+  /jsonObject:
+    post:
+      operationId: "jsonObject"
+      parameters:
+        - in: "body"
+          name: "jsonObject"
+          required: true
+          schema:
+            $ref: "#/definitions/JsonObject"
+      responses:
+        "200":
+          description: "response of 200"
+          schema:
+            $ref: "#/definitions/JsonObject"
+  /postModel:
+    post:
+      operationId: "postModel"
+      parameters:
+        - in: "body"
+          name: "clientModel"
+          required: true
+          schema:
+            $ref: "#/definitions/ClientModel"
+      responses:
+        "200":
+          description: "response of 200"
+          schema:
+            $ref: "#/definitions/ClientModel"
+definitions:
+  JsonObject:
+    type: "object"
+    properties:
+      map:
+        type: "object"
+        additionalProperties:
+          type: "object"
+      empty:
+        type: "boolean"
+    x-java-class: "io.vertx.core.json.JsonObject"
+  ClientModel:
+    type: "object"
+    properties:
+      updateDate:
+        type: "string"
+        format: "date-time"
+    x-java-class: "org.apache.servicecomb.demo.zeroconfig.client.ClientModel"
diff --git a/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/schema/serializer/scalar/Int64WriteSchemas.java b/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/schema/serializer/scalar/Int64WriteSchemas.java
index aca800351..d85810524 100644
--- a/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/schema/serializer/scalar/Int64WriteSchemas.java
+++ b/foundations/foundation-protobuf/src/main/java/org/apache/servicecomb/foundation/protobuf/internal/schema/serializer/scalar/Int64WriteSchemas.java
@@ -19,6 +19,7 @@ package org.apache.servicecomb.foundation.protobuf.internal.schema.serializer.sc
 import java.io.IOException;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
 import java.time.ZoneOffset;
 import java.time.temporal.ChronoField;
 import java.util.Date;
@@ -53,35 +54,42 @@ public class Int64WriteSchemas {
         return;
       }
 
-      if (value instanceof String[]) {
-        if (((String[]) value).length == 0) {
-          return;
-        }
-        long parsedValue = Long.parseLong(((String[]) value)[0], 10);
+      if (value instanceof Date) {
+        long parsedValue = ((Date) value).getTime();
         output.writeScalarInt64(tag, tagSize, parsedValue);
         return;
       }
 
-      if (value instanceof String) {
-        long parsedValue = Long.parseLong((String) value, 10);
+      if (value instanceof LocalDate) {
+        long parsedValue = ((LocalDate) value).getLong(ChronoField.EPOCH_DAY);
         output.writeScalarInt64(tag, tagSize, parsedValue);
         return;
       }
 
-      if (value instanceof Date) {
-        long parsedValue = ((Date) value).getTime();
+      if (value instanceof LocalDateTime) {
+        long parsedValue = ((LocalDateTime) value).toInstant(ZoneOffset.UTC).toEpochMilli();
         output.writeScalarInt64(tag, tagSize, parsedValue);
         return;
       }
 
-      if (value instanceof LocalDate) {
-        long parsedValue = ((LocalDate) value).getLong(ChronoField.EPOCH_DAY);
+      if (value instanceof String) {
+        long parsedValue;
+        if (((String) value).contains(":")) {
+          // from edge, ISO8601 date time, e.g. 2022-05-31T09:16:38.941Z
+          OffsetDateTime offsetDateTime = OffsetDateTime.parse((String) value);
+          parsedValue = offsetDateTime.toInstant().toEpochMilli();
+        } else {
+          parsedValue = Long.parseLong((String) value, 10);
+        }
         output.writeScalarInt64(tag, tagSize, parsedValue);
         return;
       }
 
-      if (value instanceof LocalDateTime) {
-        long parsedValue = ((LocalDateTime) value).toInstant(ZoneOffset.UTC).toEpochMilli();
+      if (value instanceof String[]) {
+        if (((String[]) value).length == 0) {
+          return;
+        }
+        long parsedValue = Long.parseLong(((String[]) value)[0], 10);
         output.writeScalarInt64(tag, tagSize, parsedValue);
         return;
       }
diff --git a/foundations/foundation-protobuf/src/test/java/org/apache/servicecomb/foundation/protobuf/TestISODateTimeParsing.java b/foundations/foundation-protobuf/src/test/java/org/apache/servicecomb/foundation/protobuf/TestISODateTimeParsing.java
new file mode 100644
index 000000000..8fa797724
--- /dev/null
+++ b/foundations/foundation-protobuf/src/test/java/org/apache/servicecomb/foundation/protobuf/TestISODateTimeParsing.java
@@ -0,0 +1,43 @@
+/*
+ * 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.foundation.protobuf;
+
+import java.text.SimpleDateFormat;
+import java.time.OffsetDateTime;
+import java.util.Date;
+import java.util.TimeZone;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestISODateTimeParsing {
+  @Test
+  public void testParseStringToDate() {
+    SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD'T'hh:mm:ssZ");
+    sdf.setTimeZone(TimeZone.getTimeZone("EST"));
+
+    OffsetDateTime offsetDateTime = OffsetDateTime.parse("2022-05-31T09:16:38.941Z");
+    Date d = Date.from(offsetDateTime.toInstant());
+    String date = sdf.format(d);
+    Assertions.assertEquals("2022-05-151T04:16:38-0500", date);
+
+    offsetDateTime = OffsetDateTime.parse("2022-05-31T09:16:38.941+00:00");
+    d = Date.from(offsetDateTime.toInstant());
+    date = sdf.format(d);
+    Assertions.assertEquals("2022-05-151T04:16:38-0500", date);
+  }
+}
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/tcp/TcpConnection.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/tcp/TcpConnection.java
index f498697c6..95f460013 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/tcp/TcpConnection.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/tcp/TcpConnection.java
@@ -108,12 +108,14 @@ public class TcpConnection {
       cbb.addComponent(true, buf);
 
       if (cbb.numComponents() == cbb.maxNumComponents()) {
-        netSocket.write(Buffer.buffer(cbb));
+        CompositeByteBuf last = cbb;
+        netSocket.write(Buffer.buffer(last)).onComplete(r -> last.release());
         cbb = ByteBufAllocator.DEFAULT.compositeBuffer();
       }
     }
     if (cbb.isReadable()) {
-      netSocket.write(Buffer.buffer(cbb));
+      CompositeByteBuf last = cbb;
+      netSocket.write(Buffer.buffer(last)).onComplete(r -> last.release());
     }
   }
 }