Search in sources :

Example 1 with MicroserviceInstance

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance 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 MicroserviceInstance

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance 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 MicroserviceInstance

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance 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 MicroserviceInstance

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance 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 MicroserviceInstance

use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance 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

MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)232 Test (org.junit.Test)136 ArrayList (java.util.ArrayList)60 HashMap (java.util.HashMap)42 Invocation (org.apache.servicecomb.core.Invocation)40 DiscoveryTreeNode (org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)40 Microservice (org.apache.servicecomb.registry.api.registry.Microservice)38 Expectations (mockit.Expectations)36 MicroserviceInstances (org.apache.servicecomb.registry.api.registry.MicroserviceInstances)32 CacheEndpoint (org.apache.servicecomb.registry.cache.CacheEndpoint)30 List (java.util.List)22 Transport (org.apache.servicecomb.core.Transport)22 Before (org.junit.Before)22 Map (java.util.Map)18 TransportManager (org.apache.servicecomb.core.transport.TransportManager)18 InstanceCacheManager (org.apache.servicecomb.registry.cache.InstanceCacheManager)18 MockUp (mockit.MockUp)16 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)14 ServiceCombServer (org.apache.servicecomb.loadbalance.ServiceCombServer)14 Server (com.netflix.loadbalancer.Server)12