Search in sources :

Example 11 with DiscoveryContext

use of org.apache.servicecomb.registry.discovery.DiscoveryContext in project java-chassis by ServiceComb.

the class TestDiscoveryTree method filterRerun.

@Test
public void filterRerun() {
    parent.name("1.0.0-2.0.0");
    discoveryTree.addFilter(new DiscoveryFilterForTest("g1") {

        @Override
        public DiscoveryTreeNode discovery(DiscoveryContext context, DiscoveryTreeNode parent) {
            if (context.getContextParameter("step") == null) {
                context.pushRerunFilter();
                context.putContextParameter("step", 1);
                return new DiscoveryTreeNode().name(groupName).data("first");
            }
            return new DiscoveryTreeNode().name(groupName).data("second");
        }
    });
    discoveryTree.addFilter(new DiscoveryFilterForTest(null) {

        @Override
        public DiscoveryTreeNode discovery(DiscoveryContext context, DiscoveryTreeNode parent) {
            if ("first".equals(parent.data())) {
                return new DiscoveryTreeNode();
            }
            return new DiscoveryTreeNode().data(parent.data());
        }
    });
    result = discoveryTree.discovery(context, parent);
    Assert.assertEquals("second", result.data());
}
Also used : DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) Test(org.junit.Test)

Example 12 with DiscoveryContext

use of org.apache.servicecomb.registry.discovery.DiscoveryContext in project java-chassis by ServiceComb.

the class TestDiscoveryTree method discovery_filterReturnNull.

@Test
public void discovery_filterReturnNull(@Mocked InstanceCacheManager instanceCacheManager) {
    new Expectations(DiscoveryManager.class) {

        {
            DiscoveryManager.INSTANCE.getInstanceCacheManager();
            result = instanceCacheManager;
            instanceCacheManager.getOrCreateVersionedCache(anyString, anyString, anyString);
            result = parent;
        }
    };
    DiscoveryFilter filter = new DiscoveryFilter() {

        @Override
        public int getOrder() {
            return 0;
        }

        @Override
        public DiscoveryTreeNode discovery(DiscoveryContext context, DiscoveryTreeNode parent) {
            return null;
        }
    };
    discoveryTree.addFilter(filter);
    expectedException.expect(ServiceCombException.class);
    expectedException.expectMessage(Matchers.is(filter.getClass().getName() + " discovery return null."));
    result = discoveryTree.discovery(context, null, null, null);
}
Also used : Expectations(mockit.Expectations) DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) DiscoveryFilter(org.apache.servicecomb.registry.discovery.DiscoveryFilter) Test(org.junit.Test)

Example 13 with DiscoveryContext

use of org.apache.servicecomb.registry.discovery.DiscoveryContext in project java-chassis by ServiceComb.

the class LoadbalanceHandler method getOrCreateLoadBalancer.

protected LoadBalancer getOrCreateLoadBalancer(Invocation invocation) {
    DiscoveryContext context = new DiscoveryContext();
    context.setInputParameters(invocation);
    VersionedCache serversVersionedCache = discoveryTree.discovery(context, invocation.getAppId(), invocation.getMicroserviceName(), invocation.getMicroserviceVersionRule());
    invocation.addLocalContext(CONTEXT_KEY_SERVER_LIST, serversVersionedCache.data());
    return loadBalancerMap.computeIfAbsent(serversVersionedCache.name(), name -> createLoadBalancer(invocation.getMicroserviceName()));
}
Also used : DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache)

Example 14 with DiscoveryContext

use of org.apache.servicecomb.registry.discovery.DiscoveryContext in project java-chassis by ServiceComb.

the class CommonHttpEdgeDispatcher method getOrCreateLoadBalancer.

protected LoadBalancer getOrCreateLoadBalancer(Invocation invocation, String microserviceName, String versionRule) {
    DiscoveryContext context = new DiscoveryContext();
    context.setInputParameters(invocation);
    VersionedCache serversVersionedCache = discoveryTree.discovery(context, RegistrationManager.INSTANCE.getMicroservice().getAppId(), microserviceName, versionRule);
    invocation.addLocalContext(LoadbalanceHandler.CONTEXT_KEY_SERVER_LIST, serversVersionedCache.data());
    return loadBalancerMap.computeIfAbsent(microserviceName, name -> createLoadBalancer(microserviceName));
}
Also used : DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache)

Example 15 with DiscoveryContext

use of org.apache.servicecomb.registry.discovery.DiscoveryContext in project java-chassis by ServiceComb.

the class PriorityInstancePropertyDiscoveryFilterTest method executeTest.

private void executeTest(String selfProperty, Set<String> expectedMatchedKeys) {
    Invocation invocation = new Invocation();
    DiscoveryContext discoveryContext = new DiscoveryContext();
    discoveryContext.setInputParameters(invocation);
    self.getProperties().put(PROPERTY_KEY, selfProperty);
    DiscoveryTreeNode parent = new DiscoveryTreeNode();
    parent.name("parent");
    parent.data(instances);
    DiscoveryTreeNode node = filter.discovery(discoveryContext, parent);
    Map<String, MicroserviceInstance> filterInstance = node.data();
    assertThat(filterInstance.keySet()).containsAnyElementsOf(expectedMatchedKeys);
}
Also used : DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) Invocation(org.apache.servicecomb.core.Invocation) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance)

Aggregations

DiscoveryContext (org.apache.servicecomb.registry.discovery.DiscoveryContext)20 VersionedCache (org.apache.servicecomb.foundation.common.cache.VersionedCache)10 DiscoveryTreeNode (org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)10 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)6 LocalDate (java.time.LocalDate)4 Date (java.util.Date)4 Invocation (org.apache.servicecomb.core.Invocation)4 InvocationContext (org.apache.servicecomb.swagger.invocation.context.InvocationContext)4 Test (org.junit.Test)4 DynamicPropertyFactory (com.netflix.config.DynamicPropertyFactory)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 NotNull (javax.validation.constraints.NotNull)2 Expectations (mockit.Expectations)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Endpoint (org.apache.servicecomb.core.Endpoint)2 ServiceCombServer (org.apache.servicecomb.loadbalance.ServiceCombServer)2