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);
}
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;
}
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());
}
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;
}
};
}
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;
}
Aggregations