You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/05/10 08:57:09 UTC

[GitHub] jeho0815 closed pull request #697: [SCB-546] refactor the schemas registry logic

jeho0815 closed pull request #697: [SCB-546] refactor the schemas registry logic
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/697
 
 
   

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/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/Const.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/Const.java
index 8166bec46..57855c435 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/Const.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/Const.java
@@ -119,6 +119,15 @@ private Const() {
       }
     }
 
+    public static final String MICROSERVICE_ALL_SCHEMAs;
+    static {
+      if (VERSION_V3.equals(CURRENT_VERSION)) {
+        MICROSERVICE_ALL_SCHEMAs = "/registry/v3/microservices/%s/schemas";
+      } else {
+        MICROSERVICE_ALL_SCHEMAs = V4_PREFIX + "/microservices/%s/schemas";
+      }
+    }
+
     public static final String MICROSERVICE_SCHEMA;
     static {
       if (VERSION_V3.equals(CURRENT_VERSION)) {
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/FrameworkVersions.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/FrameworkVersions.java
index 5c878552d..f91ffd3a0 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/FrameworkVersions.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/registry/FrameworkVersions.java
@@ -35,8 +35,10 @@ public static String allVersions() {
     frameworkVersions.forEach(version -> versions.putAll(version.loadVersion()));
     for (Iterator<Entry<String, String>> iterator = versions.entrySet().iterator(); iterator.hasNext();) {
       entry = (Entry<String, String>) iterator.next();
-      sb.append(entry.getKey()).append(":").append(entry.getValue())
-        .append(iterator.hasNext() ? ";" : "");
+      sb.append(entry.getKey())
+          .append(":")
+          .append(entry.getValue())
+          .append(iterator.hasNext() ? ";" : "");
     }
     return sb.toString();
   }
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/request/CreateSchemaRequest.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/request/CreateSchemaRequest.java
index e7b52c684..8de28832a 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/request/CreateSchemaRequest.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/request/CreateSchemaRequest.java
@@ -20,6 +20,8 @@
 public class CreateSchemaRequest {
   private String schema;
 
+  private String summary;
+
   public String getSchema() {
     return schema;
   }
@@ -27,4 +29,12 @@ public String getSchema() {
   public void setSchema(String schema) {
     this.schema = schema;
   }
+
+  public String getSummary() {
+    return summary;
+  }
+
+  public void setSummary(String summary) {
+    this.summary = summary;
+  }
 }
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/response/GetSchemaResponse.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/response/GetSchemaResponse.java
index 28253858a..d54bc767d 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/response/GetSchemaResponse.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/response/GetSchemaResponse.java
@@ -20,6 +20,10 @@
 public class GetSchemaResponse {
   private String schema;
 
+  private String schemaId;
+
+  private String summary;
+
   public String getSchema() {
     return schema;
   }
@@ -27,4 +31,20 @@ public String getSchema() {
   public void setSchema(String schema) {
     this.schema = schema;
   }
+
+  public String getSchemaId() {
+    return schemaId;
+  }
+
+  public void setSchemaId(String schemaId) {
+    this.schemaId = schemaId;
+  }
+
+  public String getSummary() {
+    return summary;
+  }
+
+  public void setSummary(String summary) {
+    this.summary = summary;
+  }
 }
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/response/GetSchemasResponse.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/response/GetSchemasResponse.java
new file mode 100644
index 000000000..0b4494fe0
--- /dev/null
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/response/GetSchemasResponse.java
@@ -0,0 +1,45 @@
+/*
+ * 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.serviceregistry.api.response;
+
+import java.util.List;
+
+public class GetSchemasResponse {
+
+  //to compatible service center interface, cur version return schema, but next version will change to schemas
+  private List<GetSchemaResponse> schema;
+
+  private List<GetSchemaResponse> schemas;
+
+  public List<GetSchemaResponse> getSchema() {
+    return schema;
+  }
+
+  public void setSchema(List<GetSchemaResponse> schema) {
+    this.schema = schema;
+  }
+
+  public List<GetSchemaResponse> getSchemas() {
+    return schemas;
+  }
+
+  public void setSchemas(List<GetSchemaResponse> schemas) {
+    this.schemas = schemas;
+  }
+
+}
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java
index c42b99189..8f1d88417 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImpl.java
@@ -37,6 +37,7 @@
 import org.apache.servicecomb.serviceregistry.api.registry.ServiceCenterConfig;
 import org.apache.servicecomb.serviceregistry.api.registry.ServiceCenterInfo;
 import org.apache.servicecomb.serviceregistry.api.response.FindInstancesResponse;
+import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
 import org.apache.servicecomb.serviceregistry.api.response.HeartbeatResponse;
 import org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent;
 import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances;
@@ -49,6 +50,9 @@
 import org.springframework.util.StringUtils;
 import org.yaml.snakeyaml.Yaml;
 
+import com.google.common.base.Charsets;
+import com.google.common.hash.Hashing;
+
 public class LocalServiceRegistryClientImpl implements ServiceRegistryClient {
   private static final Logger LOGGER = LoggerFactory.getLogger(LocalServiceRegistryClientImpl.class);
 
@@ -332,6 +336,23 @@ public String getSchema(String microserviceId, String schemaId) {
     return microservice.getSchemaMap().get(schemaId);
   }
 
+  @Override
+  public List<GetSchemaResponse> getSchemas(String microserviceId) {
+    Microservice microservice = microserviceIdMap.get(microserviceId);
+    if (microservice == null) {
+      throw new IllegalArgumentException("Invalid serviceId, serviceId=" + microserviceId);
+    }
+    List<GetSchemaResponse> schemas = new ArrayList<>();
+    microservice.getSchemaMap().forEach((key, val) -> {
+      GetSchemaResponse schema = new GetSchemaResponse();
+      schema.setSchema(val);
+      schema.setSchemaId(key);
+      schema.setSummary(Hashing.sha256().newHasher().putString(val, Charsets.UTF_8).hash().toString());
+      schemas.add(schema);
+    });
+    return schemas;
+  }
+
   @Override
   public boolean updateMicroserviceProperties(String microserviceId, Map<String, String> serviceProperties) {
     Microservice microservice = microserviceIdMap.get(microserviceId);
@@ -383,5 +404,4 @@ public ServiceCenterInfo getServiceCenterInfo() {
     info.setConfig(new ServiceCenterConfig());
     return info;
   }
-
 }
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java
index 1ac9b67ed..1d700ebba 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/ServiceRegistryClient.java
@@ -24,6 +24,7 @@
 import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
 import org.apache.servicecomb.serviceregistry.api.registry.ServiceCenterInfo;
+import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
 import org.apache.servicecomb.serviceregistry.api.response.HeartbeatResponse;
 import org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent;
 import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances;
@@ -77,6 +78,12 @@
    */
   String getSchema(String microserviceId, String schemaId);
 
+  /**
+  *
+  * 批量获取schemas内容
+  */
+  List<GetSchemaResponse> getSchemas(String microserviceId);
+
   /**
    *
    * 注册微服务实例
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
index 3b4218492..f379d9318 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java
@@ -46,6 +46,7 @@
 import org.apache.servicecomb.serviceregistry.api.response.GetExistenceResponse;
 import org.apache.servicecomb.serviceregistry.api.response.GetInstancesResponse;
 import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
+import org.apache.servicecomb.serviceregistry.api.response.GetSchemasResponse;
 import org.apache.servicecomb.serviceregistry.api.response.GetServiceResponse;
 import org.apache.servicecomb.serviceregistry.api.response.HeartbeatResponse;
 import org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent;
@@ -59,6 +60,8 @@
 import org.slf4j.LoggerFactory;
 
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Charsets;
+import com.google.common.hash.Hashing;
 
 import io.netty.handler.codec.http.HttpStatusClass;
 import io.vertx.core.Handler;
@@ -288,6 +291,7 @@ public boolean registerSchema(String microserviceId, String schemaId, String sch
     try {
       CreateSchemaRequest request = new CreateSchemaRequest();
       request.setSchema(schemaContent);
+      request.setSummary(Hashing.sha256().newHasher().putString(schemaContent, Charsets.UTF_8).hash().toString());
       byte[] body = JsonUtils.writeValueAsBytes(request);
 
       CountDownLatch countDownLatch = new CountDownLatch(1);
@@ -350,6 +354,31 @@ public String getSchema(String microserviceId, String schemaId) {
     return null;
   }
 
+  @Override
+  public List<GetSchemaResponse> getSchemas(String microserviceId) {
+    Holder<GetSchemasResponse> holder = new Holder<>();
+    IpPort ipPort = ipPortManager.getAvailableAddress();
+
+    CountDownLatch countDownLatch = new CountDownLatch(1);
+    // default not return schema content, just return summary!
+    RestUtils.get(ipPort,
+        String.format(Const.REGISTRY_API.MICROSERVICE_ALL_SCHEMAs, microserviceId),
+        new RequestParam(),
+        syncHandler(countDownLatch, GetSchemasResponse.class, holder));
+    try {
+      countDownLatch.await();
+    } catch (Exception e) {
+      LOGGER.error("query all schemas {} failed",
+          microserviceId,
+          e);
+    }
+    if (holder.value != null) {
+      return holder.value.getSchema() != null ? holder.value.getSchema() : holder.value.getSchemas();
+    }
+
+    return null;
+  }
+
   @Override
   public String registerMicroservice(Microservice microservice) {
     Holder<CreateServiceResponse> holder = new Holder<>();
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
index b029d6277..a482f520e 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
@@ -17,17 +17,21 @@
 package org.apache.servicecomb.serviceregistry.task;
 
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map.Entry;
 import java.util.Set;
 
 import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
+import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
 import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.util.StringUtils;
 
+import com.google.common.base.Charsets;
 import com.google.common.eventbus.EventBus;
 import com.google.common.eventbus.Subscribe;
+import com.google.common.hash.Hashing;
 
 public class MicroserviceRegisterTask extends AbstractRegisterTask {
   private static final Logger LOGGER = LoggerFactory.getLogger(MicroserviceRegisterTask.class);
@@ -136,19 +140,51 @@ private boolean checkSchemaIdSet() {
   }
 
   private boolean registerSchemas() {
+    List<GetSchemaResponse> existSchemas = srClient.getSchemas(microservice.getServiceId());
     for (Entry<String, String> entry : microservice.getSchemaMap().entrySet()) {
       String schemaId = entry.getKey();
       String content = entry.getValue();
-
-      boolean exists = srClient.isSchemaExist(microservice.getServiceId(), schemaId);
-      LOGGER.info("schemaId {} exists {}", schemaId, exists);
+      GetSchemaResponse existSchema = extractSchema(schemaId, existSchemas);
+      boolean exists = existSchema != null;
+      LOGGER.info("schemaId [{}] exists {}", schemaId, exists);
       if (!exists) {
         if (!srClient.registerSchema(microservice.getServiceId(), schemaId, content)) {
           return false;
         }
+      } else {
+        String curSchemaSumary = existSchema.getSummary();
+        String schemaSummary = Hashing.sha256().newHasher().putString(content, Charsets.UTF_8).hash().toString();
+        if (!schemaSummary.equals(curSchemaSumary)) {
+          if (curSchemaSumary == null || microservice.getInstance().getEnvironment().equalsIgnoreCase("development")) {
+            LOGGER.info(
+                "schemaId [{}]'s content changes and the current enviroment is development, so re-register it!",
+                schemaId);
+            if (!srClient.registerSchema(microservice.getServiceId(), schemaId, content)) {
+              return false;
+            }
+          } else {
+            throw new IllegalStateException("schemaId [" + schemaId
+                + "] exists in service center, but the content does not match the local content that means there are interface change "
+                + "and you need to increment microservice version before deploying. "
+                + "Or you can configure instance_description.environment=development to work in development enviroment and ignore this error");
+          }
+        }
       }
     }
-
     return true;
   }
+
+  private GetSchemaResponse extractSchema(String schemaId, List<GetSchemaResponse> schemas) {
+    if (schemas == null || schemas.isEmpty()) {
+      return null;
+    }
+    GetSchemaResponse schema = null;
+    for (GetSchemaResponse tempSchema : schemas) {
+      if (tempSchema.getSchemaId().equals(schemaId)) {
+        schema = tempSchema;
+        break;
+      }
+    }
+    return schema;
+  }
 }
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/api/registry/TestServiceCenterInfo.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/api/registry/TestServiceCenterInfo.java
index 50c4bb048..97be9f8c3 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/api/registry/TestServiceCenterInfo.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/api/registry/TestServiceCenterInfo.java
@@ -22,6 +22,7 @@
 
 public class TestServiceCenterInfo {
   ServiceCenterInfo info = new ServiceCenterInfo();
+
   ServiceCenterConfig config = new ServiceCenterConfig();
 
   @Test
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java
index ea416d62f..24b566f1d 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/LocalServiceRegistryClientImplTest.java
@@ -100,7 +100,8 @@ public void getMicroserviceId_latest() {
     Microservice v2 = mockRegisterMicroservice(appId, microserviceName, "2.0.0");
     mockRegisterMicroservice(appId, microserviceName, "1.0.0");
 
-    String serviceId = registryClient.getMicroserviceId(appId, microserviceName, DefinitionConst.VERSION_RULE_LATEST, "");
+    String serviceId =
+        registryClient.getMicroserviceId(appId, microserviceName, DefinitionConst.VERSION_RULE_LATEST, "");
     Assert.assertEquals(v2.getServiceId(), serviceId);
   }
 
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
index f3c8f3f2c..dd370e7cb 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java
@@ -35,6 +35,8 @@
 import org.apache.servicecomb.serviceregistry.api.registry.ServiceCenterConfig;
 import org.apache.servicecomb.serviceregistry.api.registry.ServiceCenterInfo;
 import org.apache.servicecomb.serviceregistry.api.response.GetExistenceResponse;
+import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
+import org.apache.servicecomb.serviceregistry.api.response.GetSchemasResponse;
 import org.apache.servicecomb.serviceregistry.client.ClientException;
 import org.apache.servicecomb.serviceregistry.client.IpPortManager;
 import org.apache.servicecomb.serviceregistry.client.http.ServiceRegistryClientImpl.ResponseWrapper;
@@ -48,6 +50,7 @@
 import io.vertx.core.buffer.Buffer;
 import io.vertx.core.http.HttpClientOptions;
 import io.vertx.core.http.HttpClientResponse;
+import io.vertx.core.json.Json;
 import mockit.Deencapsulation;
 import mockit.Expectations;
 import mockit.Mock;
@@ -295,6 +298,46 @@ void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler
     Assert.assertFalse(oClient.isSchemaExist(microserviceId, schemaId));
   }
 
+  @Test
+  public void getSchemas() {
+    String microserviceId = "msId";
+
+    new MockUp<RestUtils>() {
+      @Mock
+      void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) {
+        Holder<GetSchemasResponse> holder = Deencapsulation.getField(responseHandler, "arg$4");
+        GetSchemasResponse schemasResp = Json.decodeValue(
+            "{\"schema\":[{\"schemaId\":\"metricsEndpoint\",\"summary\":\"c1188d709631a9038874f9efc6eb894f\"},{\"schemaId\":\"comment\",\"summary\":\"bfa81d625cfbd3a57f38745323e16824\"},"
+                + "{\"schemaId\":\"healthEndpoint\",\"summary\":\"96a0aaaaa454cfa0c716e70c0017fe27\"}]}",
+            GetSchemasResponse.class);
+        holder.value = schemasResp;
+      }
+    };
+    List<GetSchemaResponse> abc = oClient.getSchemas(microserviceId);
+    Assert.assertEquals(3, abc.size());
+    Assert.assertEquals("bfa81d625cfbd3a57f38745323e16824", abc.get(1).getSummary());
+  }
+
+  @Test
+  public void getSchemasForNew() {
+    String microserviceId = "msId";
+
+    new MockUp<RestUtils>() {
+      @Mock
+      void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) {
+        Holder<GetSchemasResponse> holder = Deencapsulation.getField(responseHandler, "arg$4");
+        GetSchemasResponse schemasResp = Json.decodeValue(
+            "{\"schemas\":[{\"schemaId\":\"metricsEndpoint\",\"summary\":\"c1188d709631a9038874f9efc6eb894f\"},{\"schemaId\":\"comment\",\"summary\":\"bfa81d625cfbd3a57f38745323e16824\"},"
+                + "{\"schemaId\":\"healthEndpoint\",\"summary\":\"96a0aaaaa454cfa0c716e70c0017fe27\"}]}",
+            GetSchemasResponse.class);
+        holder.value = schemasResp;
+      }
+    };
+    List<GetSchemaResponse> abc = oClient.getSchemas(microserviceId);
+    Assert.assertEquals(3, abc.size());
+    Assert.assertEquals("bfa81d625cfbd3a57f38745323e16824", abc.get(1).getSummary());
+  }
+
   @Test
   public void testFindServiceInstance() {
     Assert.assertNull(oClient.findServiceInstance("aaa", "bbb"));
diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
index a59acfd8f..7932d6893 100644
--- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
+++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
@@ -19,8 +19,10 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
 import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import org.apache.servicecomb.serviceregistry.api.response.GetSchemaResponse;
 import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
 import org.junit.Assert;
 import org.junit.Before;
@@ -112,10 +114,6 @@ public void testRegisterSchemaFailed(@Mocked ServiceRegistryClient srClient) {
         result = null;
         srClient.registerMicroservice((Microservice) any);
         result = "serviceId";
-        srClient.isSchemaExist("serviceId", "exist");
-        result = true;
-        srClient.isSchemaExist(anyString, anyString);
-        result = false;
         srClient.registerSchema(anyString, anyString, anyString);
         result = false;
       }
@@ -140,8 +138,6 @@ public void testRegisterSchemaSuccess(@Mocked ServiceRegistryClient srClient) {
         result = null;
         srClient.registerMicroservice((Microservice) any);
         result = "serviceId";
-        srClient.isSchemaExist(anyString, anyString);
-        result = false;
         srClient.registerSchema(anyString, anyString, anyString);
         result = true;
       }
@@ -229,4 +225,110 @@ public void testAlreadyRegisteredGetSchemaIdSetFailed(@Mocked ServiceRegistryCli
     Assert.assertEquals("serviceId", microservice.getServiceId());
     Assert.assertEquals(1, taskList.size());
   }
+
+  @Test
+  public void testReRegisteredSetForDev(@Mocked ServiceRegistryClient srClient) {
+    ArchaiusUtils.resetConfig();
+    ArchaiusUtils.setProperty("instance_description.environment", "development");
+    Microservice otherMicroservice = new Microservice();
+    otherMicroservice.setAppId(microservice.getAppId());
+    otherMicroservice.setServiceName("ms1");
+    otherMicroservice.addSchema("s1", "");
+
+    List<GetSchemaResponse> list = new ArrayList<>();
+    GetSchemaResponse resp = new GetSchemaResponse();
+    resp.setSchemaId("s1");
+    resp.setSummary("c1188d709631a9038874f9efc6eb894f");
+    list.add(resp);
+
+    new Expectations() {
+      {
+        srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
+        result = "serviceId";
+        srClient.getMicroservice(anyString);
+        result = otherMicroservice;
+        srClient.getSchemas(anyString);
+        result = list;
+        srClient.registerSchema(microservice.getServiceId(), anyString, anyString);
+        result = true;
+      }
+    };
+
+    microservice.addSchema("s1", "");
+    microservice.getInstance().setEnvironment("development");
+    MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
+    registerTask.run();
+
+    Assert.assertEquals(true, registerTask.isRegistered());
+    Assert.assertEquals(true, registerTask.isSchemaIdSetMatch());
+    Assert.assertEquals("serviceId", microservice.getServiceId());
+    Assert.assertEquals(1, taskList.size());
+  }
+
+  @Test
+  public void testFirstRegisterForProd(@Mocked ServiceRegistryClient srClient) {
+    Microservice otherMicroservice = new Microservice();
+    otherMicroservice.setAppId(microservice.getAppId());
+    otherMicroservice.setServiceName("ms1");
+    otherMicroservice.addSchema("s1", "");
+
+    List<GetSchemaResponse> list = new ArrayList<>();
+    GetSchemaResponse resp = new GetSchemaResponse();
+    resp.setSchemaId("s1");
+    resp.setSummary(null);
+    list.add(resp);
+
+    new Expectations() {
+      {
+        srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
+        result = "serviceId";
+        srClient.getMicroservice(anyString);
+        result = otherMicroservice;
+        srClient.getSchemas(anyString);
+        result = list;
+        srClient.registerSchema(microservice.getServiceId(), anyString, anyString);
+        result = true;
+      }
+    };
+
+    microservice.addSchema("s1", "");
+    microservice.getInstance().setEnvironment("production");
+    MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
+    registerTask.run();
+
+    Assert.assertEquals(true, registerTask.isRegistered());
+    Assert.assertEquals(true, registerTask.isSchemaIdSetMatch());
+    Assert.assertEquals("serviceId", microservice.getServiceId());
+    Assert.assertEquals(1, taskList.size());
+  }
+
+  @Test(expected = IllegalStateException.class)
+  public void testReRegisteredSetForProd(@Mocked ServiceRegistryClient srClient) {
+    Microservice otherMicroservice = new Microservice();
+    otherMicroservice.setAppId(microservice.getAppId());
+    otherMicroservice.setServiceName("ms1");
+    otherMicroservice.addSchema("s1", "");
+
+    List<GetSchemaResponse> list = new ArrayList<>();
+    GetSchemaResponse resp = new GetSchemaResponse();
+    resp.setSchemaId("s1");
+    resp.setSummary("c1188d709631a9038874f9efc6eb894f");
+    list.add(resp);
+
+    new Expectations() {
+      {
+        srClient.getMicroserviceId(anyString, anyString, anyString, anyString);
+        result = "serviceId";
+        srClient.getMicroservice(anyString);
+        result = otherMicroservice;
+        srClient.getSchemas(anyString);
+        result = list;
+      }
+    };
+
+    microservice.addSchema("s1", "");
+    microservice.getInstance().setEnvironment("prod");
+    MicroserviceRegisterTask registerTask = new MicroserviceRegisterTask(eventBus, srClient, microservice);
+    registerTask.run();
+  }
 }


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services