Search in sources :

Example 1 with VersionedCache

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);
}
Also used : VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) InstanceCache(org.apache.servicecomb.serviceregistry.cache.InstanceCache)

Example 2 with VersionedCache

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);
}
Also used : DiscoveryContext(org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) Endpoint(org.apache.servicecomb.core.Endpoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Endpoint(org.apache.servicecomb.core.Endpoint)

Example 3 with VersionedCache

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());
}
Also used : VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) Test(org.junit.Test)

Example 4 with VersionedCache

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());
}
Also used : VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) Test(org.junit.Test)

Example 5 with VersionedCache

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());
}
Also used : VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) Test(org.junit.Test)

Aggregations

VersionedCache (org.apache.servicecomb.foundation.common.cache.VersionedCache)20 Test (org.junit.Test)9 DiscoveryContext (org.apache.servicecomb.registry.discovery.DiscoveryContext)5 DiscoveryContext (org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext)4 MicroserviceInstance (org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance)3 LocalDate (java.time.LocalDate)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Endpoint (org.apache.servicecomb.core.Endpoint)2 URIEndpointObject (org.apache.servicecomb.foundation.common.net.URIEndpointObject)2 DiscoveryTreeNode (org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)2 InvocationContext (org.apache.servicecomb.swagger.invocation.context.InvocationContext)2 Server (com.netflix.loadbalancer.Server)1 HashMap (java.util.HashMap)1 Expectations (mockit.Expectations)1 CseHttpEntity (org.apache.servicecomb.provider.springmvc.reference.CseHttpEntity)1 InstanceCache (org.apache.servicecomb.registry.cache.InstanceCache)1 InstanceCache (org.apache.servicecomb.serviceregistry.cache.InstanceCache)1 DiscoveryTree (org.apache.servicecomb.serviceregistry.discovery.DiscoveryTree)1