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 2019/12/24 08:40:47 UTC

[servicecomb-java-chassis] branch master updated: [SCB-1682][HIGWWAY-WEAK]Refactor getVendorExtensions method and add ProtoMapper to ScopedProtobufSchemaManager (#1487)

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 89ab5e6  [SCB-1682][HIGWWAY-WEAK]Refactor getVendorExtensions method and add ProtoMapper to ScopedProtobufSchemaManager (#1487)
89ab5e6 is described below

commit 89ab5e60c10af1f932447f337510a87533475680
Author: bao liu <bi...@qq.com>
AuthorDate: Tue Dec 24 16:40:37 2019 +0800

    [SCB-1682][HIGWWAY-WEAK]Refactor getVendorExtensions method and add ProtoMapper to ScopedProtobufSchemaManager (#1487)
---
 .../protobuf/definition/OperationProtobuf.java     |  2 ++
 .../codec/protobuf/definition/ProtobufManager.java | 10 ++++++-
 .../utils/ScopedProtobufSchemaManager.java         | 33 ++++++++++++++++++++--
 .../common/rest/definition/RestMetaUtils.java      |  2 +-
 .../common/rest/locator/ServicePathManager.java    |  6 ++--
 .../servicecomb/core/definition/CoreMetaUtils.java |  6 ++--
 .../core/definition/MicroserviceMeta.java          |  8 ++++--
 .../servicecomb/core/definition/OperationMeta.java |  4 ---
 .../servicecomb/core/definition/SchemaMeta.java    |  8 ++++--
 .../core/definition/ServiceRegistryListener.java   |  2 +-
 .../provider/producer/ProducerProviderManager.java |  4 +--
 11 files changed, 64 insertions(+), 21 deletions(-)

diff --git a/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/definition/OperationProtobuf.java b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/definition/OperationProtobuf.java
index e3ee342..17a0c71 100644
--- a/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/definition/OperationProtobuf.java
+++ b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/definition/OperationProtobuf.java
@@ -48,10 +48,12 @@ public class OperationProtobuf {
   }
 
   public WrapSchema getRequestSchema() {
+    // TODO : work with request
     return requestSchema;
   }
 
   public WrapSchema getResponseSchema() {
+    // TODO : work with response
     return responseSchema;
   }
 
diff --git a/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/definition/ProtobufManager.java b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/definition/ProtobufManager.java
index a1fb224..d67f8d5 100644
--- a/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/definition/ProtobufManager.java
+++ b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/definition/ProtobufManager.java
@@ -18,6 +18,7 @@
 package org.apache.servicecomb.codec.protobuf.definition;
 
 import org.apache.servicecomb.codec.protobuf.utils.ScopedProtobufSchemaManager;
+import org.apache.servicecomb.core.definition.MicroserviceMeta;
 import org.apache.servicecomb.core.definition.OperationMeta;
 
 public final class ProtobufManager {
@@ -29,9 +30,16 @@ public final class ProtobufManager {
     OperationProtobuf operationProtobuf = operationMeta.getExtData(EXT_ID);
     if (operationProtobuf == null) {
       synchronized (LOCK) {
+        MicroserviceMeta microserviceMeta = operationMeta.getMicroserviceMeta();
+        ScopedProtobufSchemaManager scopedProtobufSchemaManager = microserviceMeta.getExtData(EXT_ID);
+        if (scopedProtobufSchemaManager == null) {
+          scopedProtobufSchemaManager = new ScopedProtobufSchemaManager();
+          microserviceMeta.putExtData(EXT_ID, scopedProtobufSchemaManager);
+        }
+
         operationProtobuf = operationMeta.getExtData(EXT_ID);
         if (operationProtobuf == null) {
-          operationProtobuf = new OperationProtobuf(ScopedProtobufSchemaManager.INSTANCE, operationMeta);
+          operationProtobuf = new OperationProtobuf(scopedProtobufSchemaManager, operationMeta);
           operationMeta.putExtData(EXT_ID, operationProtobuf);
         }
       }
diff --git a/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/utils/ScopedProtobufSchemaManager.java b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/utils/ScopedProtobufSchemaManager.java
index d6c942a..9c157cf 100644
--- a/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/utils/ScopedProtobufSchemaManager.java
+++ b/common/common-protobuf/src/main/java/org/apache/servicecomb/codec/protobuf/utils/ScopedProtobufSchemaManager.java
@@ -20,12 +20,27 @@ package org.apache.servicecomb.codec.protobuf.utils;
 import java.lang.reflect.Type;
 import java.util.Map;
 
+import org.apache.servicecomb.codec.protobuf.internal.converter.SwaggerToProtoGenerator;
 import org.apache.servicecomb.core.definition.OperationMeta;
+import org.apache.servicecomb.core.definition.SchemaMeta;
 import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
+import org.apache.servicecomb.foundation.protobuf.ProtoMapper;
+import org.apache.servicecomb.foundation.protobuf.ProtoMapperFactory;
 
+import io.protostuff.compiler.model.Proto;
+import io.swagger.models.Swagger;
+
+/**
+ * Manage swagger -> protoBuffer mappings.
+ *
+ * This class have the same lifecycle as MicroserviceMeta, so we need to create an instance
+ * for each MicroserviceMeta.
+ */
 public class ScopedProtobufSchemaManager {
-  public  static final ScopedProtobufSchemaManager INSTANCE = new ScopedProtobufSchemaManager();
-  private ScopedProtobufSchemaManager() {
+  // Because this class belongs to each SchemaMeta, the key is the schema id.
+  private Map<String, ProtoMapper> mapperCache = new ConcurrentHashMapEx<>();
+
+  public ScopedProtobufSchemaManager() {
 
   }
 
@@ -40,4 +55,18 @@ public class ScopedProtobufSchemaManager {
     // TODO: add implementation using new API
     return null;
   }
+
+  /**
+   * get the ProtoMapper from Swagger
+   */
+  public ProtoMapper getOrCreateProtoMapper(SchemaMeta schemaMeta) {
+    return mapperCache.computeIfAbsent(schemaMeta.getSchemaId(), key -> {
+      Swagger swagger = schemaMeta.getSwagger();
+      SwaggerToProtoGenerator generator = new SwaggerToProtoGenerator(schemaMeta.getMicroserviceQualifiedName(),
+          swagger);
+      Proto proto = generator.convert();
+      ProtoMapperFactory protoMapperFactory = new ProtoMapperFactory();
+      return protoMapperFactory.create(proto);
+    });
+  }
 }
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestMetaUtils.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestMetaUtils.java
index d8f75d2..3626d1e 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestMetaUtils.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/RestMetaUtils.java
@@ -24,6 +24,6 @@ public final class RestMetaUtils {
   }
 
   public static RestOperationMeta getRestOperationMeta(OperationMeta operationMeta) {
-    return operationMeta.getVendorExtensions().get(RestConst.SWAGGER_REST_OPERATION);
+    return operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
   }
 }
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/locator/ServicePathManager.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/locator/ServicePathManager.java
index 15542b0..318824b 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/locator/ServicePathManager.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/locator/ServicePathManager.java
@@ -48,7 +48,7 @@ public class ServicePathManager {
   protected MicroservicePaths producerPaths;
 
   public static ServicePathManager getServicePathManager(MicroserviceMeta microserviceMeta) {
-    return microserviceMeta.getVendorExtensions().get(REST_PATH_MANAGER);
+    return microserviceMeta.getExtData(REST_PATH_MANAGER);
   }
 
   public ServicePathManager(MicroserviceMeta microserviceMeta) {
@@ -59,14 +59,14 @@ public class ServicePathManager {
     }
     sortPath();
 
-    microserviceMeta.getVendorExtensions().put(REST_PATH_MANAGER, this);
+    microserviceMeta.putExtData(REST_PATH_MANAGER, this);
   }
 
   private void addSchema(SchemaMeta schemaMeta) {
     for (OperationMeta operationMeta : schemaMeta.getOperations().values()) {
       RestOperationMeta restOperationMeta = new RestOperationMeta();
       restOperationMeta.init(operationMeta);
-      operationMeta.getVendorExtensions().put(RestConst.SWAGGER_REST_OPERATION, restOperationMeta);
+      operationMeta.putExtData(RestConst.SWAGGER_REST_OPERATION, restOperationMeta);
       addResource(restOperationMeta);
     }
 
diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/CoreMetaUtils.java b/core/src/main/java/org/apache/servicecomb/core/definition/CoreMetaUtils.java
index e66b820..d89ccd3 100644
--- a/core/src/main/java/org/apache/servicecomb/core/definition/CoreMetaUtils.java
+++ b/core/src/main/java/org/apache/servicecomb/core/definition/CoreMetaUtils.java
@@ -52,7 +52,7 @@ public final class CoreMetaUtils {
 
   // only for consumer flow
   public static MicroserviceVersion getMicroserviceVersion(MicroserviceMeta microserviceMeta) {
-    return microserviceMeta.getVendorExtensions().get(CORE_MICROSERVICE_VERSION);
+    return microserviceMeta.getExtData(CORE_MICROSERVICE_VERSION);
   }
 
   public static MicroserviceMeta getMicroserviceMeta(MicroserviceVersion microserviceVersion) {
@@ -60,10 +60,10 @@ public final class CoreMetaUtils {
   }
 
   public static SwaggerProducerOperation getSwaggerProducerOperation(OperationMeta operationMeta) {
-    return operationMeta.getVendorExtensions().get(Const.PRODUCER_OPERATION);
+    return operationMeta.getExtData(Const.PRODUCER_OPERATION);
   }
 
   public static SwaggerProducer getSwaggerProducer(SchemaMeta schemaMeta) {
-    return schemaMeta.getVendorExtensions().get(SWAGGER_PRODUCER);
+    return schemaMeta.getExtData(SWAGGER_PRODUCER);
   }
 }
diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/MicroserviceMeta.java b/core/src/main/java/org/apache/servicecomb/core/definition/MicroserviceMeta.java
index 18f594c..7b341a9 100644
--- a/core/src/main/java/org/apache/servicecomb/core/definition/MicroserviceMeta.java
+++ b/core/src/main/java/org/apache/servicecomb/core/definition/MicroserviceMeta.java
@@ -195,8 +195,12 @@ public class MicroserviceMeta {
     return schemaMetas;
   }
 
-  public VendorExtensions getVendorExtensions() {
-    return vendorExtensions;
+  public void putExtData(String key, Object data) {
+    vendorExtensions.put(key, data);
+  }
+
+  public <T> T getExtData(String key) {
+    return vendorExtensions.get(key);
   }
 
   public List<Handler> getConsumerHandlerChain() {
diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/OperationMeta.java b/core/src/main/java/org/apache/servicecomb/core/definition/OperationMeta.java
index 0a3bf28..755ac5e 100644
--- a/core/src/main/java/org/apache/servicecomb/core/definition/OperationMeta.java
+++ b/core/src/main/java/org/apache/servicecomb/core/definition/OperationMeta.java
@@ -141,10 +141,6 @@ public class OperationMeta {
     return vendorExtensions.get(key);
   }
 
-  public VendorExtensions getVendorExtensions() {
-    return vendorExtensions;
-  }
-
   public Executor getExecutor() {
     return executor;
   }
diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/SchemaMeta.java b/core/src/main/java/org/apache/servicecomb/core/definition/SchemaMeta.java
index ab886c8..be6ad7b 100644
--- a/core/src/main/java/org/apache/servicecomb/core/definition/SchemaMeta.java
+++ b/core/src/main/java/org/apache/servicecomb/core/definition/SchemaMeta.java
@@ -93,8 +93,12 @@ public class SchemaMeta {
     return operations;
   }
 
-  public VendorExtensions getVendorExtensions() {
-    return vendorExtensions;
+  public void putExtData(String key, Object data) {
+    vendorExtensions.put(key, data);
+  }
+
+  public <T> T getExtData(String key) {
+    return vendorExtensions.get(key);
   }
 
   public OperationMeta findOperation(String operationId) {
diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/ServiceRegistryListener.java b/core/src/main/java/org/apache/servicecomb/core/definition/ServiceRegistryListener.java
index c63039b..8d9f79a 100644
--- a/core/src/main/java/org/apache/servicecomb/core/definition/ServiceRegistryListener.java
+++ b/core/src/main/java/org/apache/servicecomb/core/definition/ServiceRegistryListener.java
@@ -100,7 +100,7 @@ public class ServiceRegistryListener {
       }
     }
 
-    microserviceMeta.getVendorExtensions().put(CORE_MICROSERVICE_VERSION, microserviceVersion);
+    microserviceMeta.putExtData(CORE_MICROSERVICE_VERSION, microserviceVersion);
     microserviceVersion.getVendorExtensions().put(CORE_MICROSERVICE_META, microserviceMeta);
   }
 
diff --git a/core/src/main/java/org/apache/servicecomb/core/provider/producer/ProducerProviderManager.java b/core/src/main/java/org/apache/servicecomb/core/provider/producer/ProducerProviderManager.java
index 47ca847..6a645fd 100644
--- a/core/src/main/java/org/apache/servicecomb/core/provider/producer/ProducerProviderManager.java
+++ b/core/src/main/java/org/apache/servicecomb/core/provider/producer/ProducerProviderManager.java
@@ -95,11 +95,11 @@ public class ProducerProviderManager {
     swagger = swaggerProducer.getSwagger();
 
     SchemaMeta schemaMeta = producerMicroserviceMeta.registerSchemaMeta(schemaId, swagger);
-    schemaMeta.getVendorExtensions().put(CoreMetaUtils.SWAGGER_PRODUCER, swaggerProducer);
+    schemaMeta.putExtData(CoreMetaUtils.SWAGGER_PRODUCER, swaggerProducer);
     Executor reactiveExecutor = scbEngine.getExecutorManager().findExecutorById(ExecutorManager.EXECUTOR_REACTIVE);
     for (SwaggerProducerOperation producerOperation : swaggerProducer.getAllOperations()) {
       OperationMeta operationMeta = schemaMeta.ensureFindOperation(producerOperation.getOperationId());
-      operationMeta.getVendorExtensions().put(Const.PRODUCER_OPERATION, producerOperation);
+      operationMeta.putExtData(Const.PRODUCER_OPERATION, producerOperation);
 
       if (CompletableFuture.class.equals(producerOperation.getProducerMethod().getReturnType())) {
         operationMeta.setExecutor(scbEngine.getExecutorManager().findExecutor(operationMeta, reactiveExecutor));