Search in sources :

Example 1 with DiscoveryTreeNode

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

the class IsolationDiscoveryFilterTest method before.

@Before
public void before() {
    discoveryContext = new DiscoveryContext();
    discoveryContext.setInputParameters(invocation);
    discoveryTreeNode = new DiscoveryTreeNode();
    Mockito.doAnswer(a -> a.getArguments()[0]).when(transport).parseAddress(Mockito.anyString());
    data = new HashMap<>();
    for (int i = 0; i < 3; ++i) {
        MicroserviceInstance instance = new MicroserviceInstance();
        instance.setInstanceId("i" + i);
        String endpoint = "rest://127.0.0.1:" + i;
        instance.setEndpoints(Collections.singletonList(endpoint));
        data.put(instance.getInstanceId(), instance);
        ServiceCombServer serviceCombServer = new ServiceCombServer(invocation.getMicroserviceName(), transport, new CacheEndpoint(endpoint, instance));
        ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer);
    }
    discoveryTreeNode.data(data);
    filter = new IsolationDiscoveryFilter();
    TestServiceCombServerStats.releaseTryingChance();
}
Also used : ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) Before(org.junit.Before)

Example 2 with DiscoveryTreeNode

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

the class IsolationDiscoveryFilterTest method discovery_not_try_isolated_instance_concurrently.

@Test
public void discovery_not_try_isolated_instance_concurrently() {
    ServiceCombServer server0 = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServer(data.get("i0"));
    ServiceCombServerStats serviceCombServerStats = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server0);
    for (int i = 0; i < 5; ++i) {
        serviceCombServerStats.markFailure();
    }
    ServiceCombLoadBalancerStats.INSTANCE.markIsolated(server0, true);
    letIsolatedInstancePassSingleTestTime(serviceCombServerStats);
    Assert.assertTrue(ServiceCombServerStats.isolatedServerCanTry());
    // The first invocation can occupy the trying chance
    DiscoveryTreeNode childNode = filter.discovery(discoveryContext, discoveryTreeNode);
    Map<String, MicroserviceInstance> childNodeData = childNode.data();
    Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i0", "i1", "i2"));
    Assert.assertEquals(data.get("i0"), childNodeData.get("i0"));
    Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
    Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
    Assert.assertFalse(ServiceCombServerStats.isolatedServerCanTry());
    // Other invocation cannot get trying chance concurrently
    childNode = filter.discovery(discoveryContext, discoveryTreeNode);
    childNodeData = childNode.data();
    Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i1", "i2"));
    Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
    Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
    ServiceCombServerStats.checkAndReleaseTryingChance(// after the first invocation releases the trying chance
    invocation);
    // Other invocation can get the trying chance
    childNode = filter.discovery(discoveryContext, discoveryTreeNode);
    childNodeData = childNode.data();
    Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i0", "i1", "i2"));
    Assert.assertEquals(data.get("i0"), childNodeData.get("i0"));
    Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
    Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
    Assert.assertFalse(ServiceCombServerStats.isolatedServerCanTry());
}
Also used : ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) ServiceCombServerStats(org.apache.servicecomb.loadbalance.ServiceCombServerStats) TestServiceCombServerStats(org.apache.servicecomb.loadbalance.TestServiceCombServerStats) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) Test(org.junit.Test)

Example 3 with DiscoveryTreeNode

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

the class IsolationDiscoveryFilterTest method discovery_try_isolated_instance_after_singleTestTime.

@Test
public void discovery_try_isolated_instance_after_singleTestTime() {
    ServiceCombServer server0 = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServer(data.get("i0"));
    ServiceCombServerStats serviceCombServerStats = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server0);
    for (int i = 0; i < 5; ++i) {
        serviceCombServerStats.markFailure();
    }
    letIsolatedInstancePassSingleTestTime(serviceCombServerStats);
    ServiceCombLoadBalancerStats.INSTANCE.markIsolated(server0, true);
    Assert.assertTrue(ServiceCombServerStats.isolatedServerCanTry());
    Assert.assertNull(TestServiceCombServerStats.getTryingIsolatedServerInvocation());
    DiscoveryTreeNode childNode = filter.discovery(discoveryContext, discoveryTreeNode);
    Map<String, MicroserviceInstance> childNodeData = childNode.data();
    Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i0", "i1", "i2"));
    Assert.assertEquals(data.get("i0"), childNodeData.get("i0"));
    Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
    Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
    Assert.assertTrue(serviceCombServerStats.isIsolated());
    Assert.assertFalse(ServiceCombServerStats.isolatedServerCanTry());
    Assert.assertSame(invocation, TestServiceCombServerStats.getTryingIsolatedServerInvocation());
}
Also used : ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) ServiceCombServerStats(org.apache.servicecomb.loadbalance.ServiceCombServerStats) TestServiceCombServerStats(org.apache.servicecomb.loadbalance.TestServiceCombServerStats) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) Test(org.junit.Test)

Example 4 with DiscoveryTreeNode

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

the class IsolationDiscoveryFilterTest method discovery_keep_minIsolationTime.

@Test
public void discovery_keep_minIsolationTime() {
    ServiceCombServer server0 = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServer(data.get("i0"));
    ServiceCombLoadBalancerStats.INSTANCE.markIsolated(server0, true);
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server0);
    DiscoveryTreeNode childNode = filter.discovery(discoveryContext, discoveryTreeNode);
    Map<String, MicroserviceInstance> childNodeData = childNode.data();
    Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i1", "i2"));
    Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
    Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
    ServiceCombServerStats serviceCombServerStats = ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(server0);
    Deencapsulation.setField(serviceCombServerStats, "isolatedTime", System.currentTimeMillis() - Configuration.INSTANCE.getMinIsolationTime(invocation.getMicroserviceName()) - 1);
    childNode = filter.discovery(discoveryContext, discoveryTreeNode);
    childNodeData = childNode.data();
    Assert.assertThat(childNodeData.keySet(), Matchers.containsInAnyOrder("i0", "i1", "i2"));
    Assert.assertEquals(data.get("i0"), childNodeData.get("i0"));
    Assert.assertEquals(data.get("i1"), childNodeData.get("i1"));
    Assert.assertEquals(data.get("i2"), childNodeData.get("i2"));
}
Also used : ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) ServiceCombServerStats(org.apache.servicecomb.loadbalance.ServiceCombServerStats) TestServiceCombServerStats(org.apache.servicecomb.loadbalance.TestServiceCombServerStats) Test(org.junit.Test)

Example 5 with DiscoveryTreeNode

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

the class TestInstancePropertyDiscoveryFilter method testGetFilteredListOfServers.

@Test
@SuppressWarnings("unchecked")
public void testGetFilteredListOfServers(@Injectable DiscoveryContext context, @Injectable DiscoveryTreeNode parent, @Injectable Invocation invocation) {
    Map<String, MicroserviceInstance> instances = new HashMap<>();
    instances.put(instance.getInstanceId(), instance);
    new Expectations() {

        {
            context.getInputParameters();
            result = invocation;
            parent.data();
            result = instances;
            parent.name();
            result = "parent";
        }
    };
    DiscoveryTreeNode node = filter.discovery(context, parent);
    Assert.assertEquals(1, ((Map<String, MicroserviceInstance>) node.data()).keySet().size());
}
Also used : Expectations(mockit.Expectations) HashMap(java.util.HashMap) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

DiscoveryTreeNode (org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)33 Test (org.junit.Test)25 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)20 HashMap (java.util.HashMap)11 Invocation (org.apache.servicecomb.core.Invocation)11 ArrayList (java.util.ArrayList)6 NonSwaggerInvocation (org.apache.servicecomb.core.NonSwaggerInvocation)6 Transport (org.apache.servicecomb.core.Transport)6 TransportManager (org.apache.servicecomb.core.transport.TransportManager)6 ServiceCombServer (org.apache.servicecomb.loadbalance.ServiceCombServer)6 InstanceCacheManager (org.apache.servicecomb.registry.cache.InstanceCacheManager)6 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)5 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)5 DataCenterInfo (org.apache.servicecomb.registry.api.registry.DataCenterInfo)5 DiscoveryContext (org.apache.servicecomb.registry.discovery.DiscoveryContext)5 InvocationRuntimeType (org.apache.servicecomb.core.definition.InvocationRuntimeType)4 SchemaMeta (org.apache.servicecomb.core.definition.SchemaMeta)4 ReferenceConfig (org.apache.servicecomb.core.provider.consumer.ReferenceConfig)4 ServiceCombServerStats (org.apache.servicecomb.loadbalance.ServiceCombServerStats)4 TestServiceCombServerStats (org.apache.servicecomb.loadbalance.TestServiceCombServerStats)4