use of org.apache.servicecomb.foundation.common.cache.VersionedCache in project incubator-servicecomb-java-chassis by apache.
the class MicroserviceVersionRule method resetInstanceCache.
private void resetInstanceCache() {
instanceCache = new InstanceCache(appId, microserviceName, versionRule.getVersionRule(), instances);
versionedCache = new VersionedCache().name(versionRule.getVersionRule()).autoCacheVersion().data(instances);
}
use of org.apache.servicecomb.foundation.common.cache.VersionedCache in project incubator-servicecomb-java-chassis by apache.
the class SimpleLoadBalanceHandler method handle.
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(invocation);
VersionedCache endpointsVersionedCache = discoveryTree.discovery(context, invocation.getAppId(), invocation.getMicroserviceName(), invocation.getMicroserviceVersionRule());
if (endpointsVersionedCache.isEmpty()) {
asyncResp.consumerFail(ExceptionUtils.lbAddressNotFound(invocation.getMicroserviceName(), invocation.getMicroserviceVersionRule(), endpointsVersionedCache.name()));
return;
}
List<Endpoint> endpoints = endpointsVersionedCache.data();
AtomicInteger index = indexMap.computeIfAbsent(endpointsVersionedCache.name(), name -> {
LOGGER.info("Create loadBalancer for {}.", name);
return new AtomicInteger();
});
LOGGER.debug("invocation {} use discoveryGroup {}.", invocation.getMicroserviceQualifiedName(), endpointsVersionedCache.name());
int idx = Math.abs(index.getAndIncrement());
idx = idx % endpoints.size();
Endpoint endpoint = endpoints.get(idx);
invocation.setEndpoint(endpoint);
invocation.next(asyncResp);
}
use of org.apache.servicecomb.foundation.common.cache.VersionedCache in project incubator-servicecomb-java-chassis by apache.
the class TestDiscoveryTreeNode method fromCache.
@Test
public void fromCache() {
Object data = new Object();
VersionedCache other = new VersionedCache().cacheVersion(1).name("cache").data(data);
node.fromCache(other);
Assert.assertEquals(1, node.cacheVersion());
Assert.assertEquals("cache", node.name());
Assert.assertSame(data, node.data());
}
use of org.apache.servicecomb.foundation.common.cache.VersionedCache in project incubator-servicecomb-java-chassis by apache.
the class TestDiscoveryTree method avoidConcurrentProblem.
@Test
public void avoidConcurrentProblem() {
Deencapsulation.setField(discoveryTree, "root", parent.cacheVersion(1));
Assert.assertTrue(parent.children().isEmpty());
discoveryTree.discovery(context, new VersionedCache().cacheVersion(0).name("input"));
Assert.assertTrue(parent.children().isEmpty());
}
use of org.apache.servicecomb.foundation.common.cache.VersionedCache in project incubator-servicecomb-java-chassis by apache.
the class TestInstanceCache method getVersionedCache.
@Test
public void getVersionedCache() {
VersionedCache versionedCache = instanceCache.getVersionedCache();
Assert.assertEquals("1.0", versionedCache.name());
Assert.assertSame(instMap, versionedCache.data());
}
Aggregations