Search in sources :

Example 46 with MicroserviceInstances

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

the class MicroserviceStore method findServiceInstances.

public MicroserviceInstances findServiceInstances(String revision) {
    if (instancesRevision.equals(revision)) {
        return new MicroserviceInstances().setRevision(instancesRevision).setNeedRefresh(false);
    }
    List<MicroserviceInstance> instances = instancesById.values().stream().map(InstanceStore::getInstance).collect(Collectors.toList());
    FindInstancesResponse response = new FindInstancesResponse().setInstances(instances);
    return new MicroserviceInstances().setRevision(instancesRevision).setInstancesResponse(response);
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) FindInstancesResponse(org.apache.servicecomb.registry.api.registry.FindInstancesResponse)

Example 47 with MicroserviceInstances

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

the class SchemaDiscovery method findServiceInstances.

@Override
public MicroserviceInstances findServiceInstances(String appId, String serviceName, String versionRule, String revision) {
    MicroserviceInstances microserviceInstances = new MicroserviceInstances();
    microserviceInstances.setMicroserviceNotExist(true);
    return microserviceInstances;
}
Also used : MicroserviceInstances(org.apache.servicecomb.registry.api.registry.MicroserviceInstances)

Example 48 with MicroserviceInstances

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

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 49 with MicroserviceInstances

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

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)

Example 50 with MicroserviceInstances

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

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)

Aggregations

MicroserviceInstances (org.apache.servicecomb.registry.api.registry.MicroserviceInstances)52 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)34 Test (org.junit.Test)22 FindInstancesResponse (org.apache.servicecomb.registry.api.registry.FindInstancesResponse)18 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)12 ArrayList (java.util.ArrayList)8 MockUp (mockit.MockUp)8 Before (org.junit.Before)6 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Handler (io.vertx.core.Handler)2 Buffer (io.vertx.core.buffer.Buffer)2 HttpClientResponse (io.vertx.core.http.HttpClientResponse)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 Enumeration (java.util.Enumeration)2 List (java.util.List)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2