Search in sources :

Example 1 with MicroserviceInstances

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstances in project java-chassis by ServiceComb.

the class InstanceCacheChecker method check.

protected InstanceCacheResult check(MicroserviceVersions microserviceVersions) {
    InstanceCacheResult instanceCacheResult = new InstanceCacheResult();
    instanceCacheResult.setAppId(microserviceVersions.getAppId());
    instanceCacheResult.setMicroserviceName(microserviceVersions.getMicroserviceName());
    instanceCacheResult.setPulledInstances(microserviceVersions.getPulledInstances());
    MicroserviceInstances microserviceInstances = RegistryUtils.findServiceInstances(microserviceVersions.getAppId(), microserviceVersions.getMicroserviceName(), DefinitionConst.VERSION_RULE_ALL);
    if (microserviceInstances == null) {
        instanceCacheResult.setStatus(Status.UNKNOWN);
        instanceCacheResult.setDetail("failed to find instances from service center");
        return instanceCacheResult;
    }
    if (microserviceInstances.isMicroserviceNotExist()) {
        // no problem, will be deleted from MicroserviceManager in next pull
        instanceCacheResult.setStatus(Status.UNKNOWN);
        instanceCacheResult.setDetail("microservice is not exist anymore, will be deleted from memory in next pull");
        return instanceCacheResult;
    }
    if (!Objects.equals(microserviceInstances.getRevision(), microserviceVersions.getRevision())) {
        // maybe not pull, wait for next pull we get the same revision
        instanceCacheResult.setStatus(Status.UNKNOWN);
        instanceCacheResult.setDetail(String.format("revision is different, will be synchronized in next pull. local revision=%s, remote revision=%s", microserviceVersions.getRevision(), microserviceInstances.getRevision()));
        // better to change revision and more likely to find the correct instances in next pull.
        microserviceVersions.setRevision(null);
        return instanceCacheResult;
    }
    // compare all instances
    List<MicroserviceInstance> remoteInstances = microserviceInstances.getInstancesResponse().getInstances();
    remoteInstances.sort(Comparator.comparing(MicroserviceInstance::getInstanceId));
    String local = Json.encode(microserviceVersions.getPulledInstances());
    String remote = Json.encode(remoteInstances);
    if (local.equals(remote)) {
        LOGGER.info("instance cache match. appId={}, microservice={}.\n" + "current cache: {}\n", microserviceVersions.getAppId(), microserviceVersions.getMicroserviceName(), remoteInstances.toString());
        instanceCacheResult.setStatus(Status.NORMAL);
        return instanceCacheResult;
    }
    LOGGER.error("instance cache not match. appId={}, microservice={}.\n" + "local cache: {}\n" + "remote cache: {}", microserviceVersions.getAppId(), microserviceVersions.getMicroserviceName(), microserviceVersions.getPulledInstances().toString(), remoteInstances.toString());
    instanceCacheResult.setStatus(Status.ABNORMAL);
    instanceCacheResult.setDetail("instance cache not match");
    // auto fix, will do a full pull request when invoke MicroserviceVersions.pullInstances
    microserviceVersions.setRevision(null);
    return instanceCacheResult;
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance)

Example 2 with MicroserviceInstances

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstances in project java-chassis by ServiceComb.

the class ServiceRegistryClientImpl method findServiceInstances.

@Override
public MicroserviceInstances findServiceInstances(String consumerId, String appId, String serviceName, String versionRule, String revision) {
    MicroserviceInstances microserviceInstances = new MicroserviceInstances();
    IpPort ipPort = ipPortManager.getAvailableAddress();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    RequestParam requestParam = new RequestParam().addQueryParam("appId", appId).addQueryParam("serviceName", serviceName).addQueryParam("global", "true").addQueryParam("version", versionRule);
    if (RegistryUtils.getMicroservice().getEnvironment() != null) {
        requestParam.addQueryParam("env", RegistryUtils.getMicroservice().getEnvironment());
    }
    if (consumerId != null) {
        requestParam.addHeader("X-ConsumerId", consumerId);
    }
    if (revision != null) {
        requestParam.addQueryParam("rev", revision);
    }
    restClientUtil.get(ipPort, Const.REGISTRY_API.MICROSERVICE_INSTANCES, requestParam, syncHandlerForInstances(countDownLatch, microserviceInstances));
    try {
        countDownLatch.await();
        if (!microserviceInstances.isNeedRefresh()) {
            return microserviceInstances;
        }
        if (microserviceInstances.getInstancesResponse() == null) {
            // error
            return null;
        }
        List<MicroserviceInstance> list = microserviceInstances.getInstancesResponse().getInstances();
        if (list == null) {
            microserviceInstances.getInstancesResponse().setInstances(new ArrayList<>());
        }
        return microserviceInstances;
    } catch (Exception e) {
        LOGGER.error("find microservice instance {}/{}/{} failed", appId, serviceName, versionRule, e);
    }
    return null;
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with MicroserviceInstances

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstances in project java-chassis by ServiceComb.

the class RefreshableMicroserviceCache method pullInstance.

void pullInstance(String revisionId) {
    MicroserviceInstances serviceInstances = pullInstanceFromServiceCenter(revisionId);
    if (serviceInstances == null) {
        LOGGER.error("Can not find any instances from service center due to previous errors. service={}/{}/{}", key.getAppId(), key.getServiceName(), key.getVersionRule());
        setStatus(MicroserviceCacheStatus.CLIENT_ERROR);
        return;
    }
    if (serviceInstances.isMicroserviceNotExist()) {
        setStatus(MicroserviceCacheStatus.SERVICE_NOT_FOUND);
        return;
    }
    if (!serviceInstances.isNeedRefresh()) {
        LOGGER.debug("instances revision is not changed, service={}/{}/{}", key.getAppId(), key.getServiceName(), key.getVersionRule());
        setStatus(MicroserviceCacheStatus.NO_CHANGE);
        return;
    }
    List<MicroserviceInstance> instances = serviceInstances.getInstancesResponse().getInstances();
    LOGGER.info("find instances[{}] from service center success. service={}/{}/{}, old revision={}, new revision={}", instances.size(), key.getAppId(), key.getServiceName(), key.getVersionRule(), this.revisionId, serviceInstances.getRevision());
    for (MicroserviceInstance instance : instances) {
        LOGGER.info("service id={}, instance id={}, endpoints={}", instance.getServiceId(), instance.getInstanceId(), instance.getEndpoints());
    }
    safeSetInstances(instances, serviceInstances.getRevision());
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance)

Example 4 with MicroserviceInstances

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstances in project java-chassis by ServiceComb.

the class RegistryUtilsTest method convertCacheToMicroserviceInstances.

@Test
public void convertCacheToMicroserviceInstances() {
    MockedMicroserviceCache microserviceCache = new MockedMicroserviceCache();
    microserviceCache.setStatus(MicroserviceCacheStatus.CLIENT_ERROR);
    MicroserviceInstances microserviceInstances = RegistryUtils.convertCacheToMicroserviceInstances(microserviceCache);
    Assert.assertNull(microserviceInstances);
    microserviceCache = new MockedMicroserviceCache();
    microserviceCache.setStatus(MicroserviceCacheStatus.SETTING_CACHE_ERROR);
    microserviceInstances = RegistryUtils.convertCacheToMicroserviceInstances(microserviceCache);
    Assert.assertNull(microserviceInstances);
    microserviceCache = new MockedMicroserviceCache();
    microserviceCache.setStatus(MicroserviceCacheStatus.INIT);
    microserviceInstances = RegistryUtils.convertCacheToMicroserviceInstances(microserviceCache);
    Assert.assertNull(microserviceInstances);
    microserviceCache = new MockedMicroserviceCache();
    microserviceCache.setStatus(MicroserviceCacheStatus.SERVICE_NOT_FOUND);
    microserviceInstances = RegistryUtils.convertCacheToMicroserviceInstances(microserviceCache);
    Assert.assertTrue(microserviceInstances.isMicroserviceNotExist());
    Assert.assertFalse(microserviceInstances.isNeedRefresh());
    Assert.assertEquals("", microserviceInstances.getRevision());
    Assert.assertNull(microserviceInstances.getInstancesResponse());
    microserviceCache = new MockedMicroserviceCache();
    microserviceCache.setStatus(MicroserviceCacheStatus.REFRESHED);
    microserviceCache.setRevisionId("0166f3c18702617d5e55cf911e4e412cc8760dab");
    MicroserviceInstance microserviceInstance = new MicroserviceInstance();
    microserviceCache.setInstances(Collections.singletonList(microserviceInstance));
    microserviceInstances = RegistryUtils.convertCacheToMicroserviceInstances(microserviceCache);
    Assert.assertFalse(microserviceInstances.isMicroserviceNotExist());
    Assert.assertTrue(microserviceInstances.isNeedRefresh());
    Assert.assertEquals("0166f3c18702617d5e55cf911e4e412cc8760dab", microserviceInstances.getRevision());
    Assert.assertEquals(1, microserviceInstances.getInstancesResponse().getInstances().size());
    Assert.assertSame(microserviceInstance, microserviceInstances.getInstancesResponse().getInstances().get(0));
    microserviceCache = new MockedMicroserviceCache();
    microserviceCache.setStatus(MicroserviceCacheStatus.NO_CHANGE);
    microserviceCache.setRevisionId("0166f3c18702617d5e55cf911e4e412cc8760dab");
    microserviceInstances = RegistryUtils.convertCacheToMicroserviceInstances(microserviceCache);
    Assert.assertFalse(microserviceInstances.isMicroserviceNotExist());
    Assert.assertFalse(microserviceInstances.isNeedRefresh());
    Assert.assertEquals("0166f3c18702617d5e55cf911e4e412cc8760dab", microserviceInstances.getRevision());
    Assert.assertNull(microserviceInstances.getInstancesResponse());
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) MockedMicroserviceCache(org.apache.servicecomb.serviceregistry.registry.cache.MockedMicroserviceCache) Test(org.junit.Test)

Example 5 with MicroserviceInstances

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstances in project java-chassis by ServiceComb.

the class TestRegistryBase method mockNotExist.

protected void mockNotExist() {
    MicroserviceInstances microserviceInstances = new MicroserviceInstances();
    microserviceInstances.setMicroserviceNotExist(true);
    new MockUp<DiscoveryManager>() {

        @Mock
        MicroserviceInstances findServiceInstances(String appId, String serviceName, String versionRule, String revision) {
            return microserviceInstances;
        }
    };
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MockUp(mockit.MockUp)

Aggregations

MicroserviceInstances (org.apache.servicecomb.registry.api.registry.MicroserviceInstances)26 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)17 Test (org.junit.Test)11 FindInstancesResponse (org.apache.servicecomb.registry.api.registry.FindInstancesResponse)9 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)6 ArrayList (java.util.ArrayList)4 MockUp (mockit.MockUp)4 Before (org.junit.Before)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Handler (io.vertx.core.Handler)1 Buffer (io.vertx.core.buffer.Buffer)1 HttpClientResponse (io.vertx.core.http.HttpClientResponse)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 Enumeration (java.util.Enumeration)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1