Search in sources :

Example 1 with DiscoveryTree

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

the class TestLoadBalanceHandler2 method createMockedDiscoveryTree.

/**
 * Create a mocked DiscoveryTree that always returns {@code servers} as the versionedCache result.
 */
private DiscoveryTree createMockedDiscoveryTree(List<ServiceCombServer> servers) {
    DiscoveryTree discoveryTree = Mockito.mock(DiscoveryTree.class);
    DiscoveryTreeNode versionedCache = new DiscoveryTreeNode().name("testVersionedCacheName").data(servers);
    Mockito.when(discoveryTree.discovery(Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(versionedCache);
    return discoveryTree;
}
Also used : DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) DiscoveryTree(org.apache.servicecomb.registry.discovery.DiscoveryTree)

Example 2 with DiscoveryTree

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

the class TestLoadBalanceHandler2 method testStatusFilterUsingMockedInvocationWorks.

@Test
public void testStatusFilterUsingMockedInvocationWorks() throws Exception {
    ArchaiusUtils.setProperty("servicecomb.loadbalance.filter.status.enabled", "false");
    Invocation invocation = new NonSwaggerInvocation("testApp", "testMicroserviceName", "0.0.0+", (inv, aysnc) -> {
        aysnc.success("OK");
    });
    InstanceCacheManager instanceCacheManager = Mockito.mock(InstanceCacheManager.class);
    TransportManager transportManager = Mockito.mock(TransportManager.class);
    Transport transport = Mockito.mock(Transport.class);
    ArchaiusUtils.setProperty("servicecomb.loadbalance.filter.operation.enabled", "false");
    // set up data
    MicroserviceInstance myself = new MicroserviceInstance();
    DataCenterInfo info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region");
    info.setAvailableZone("test-zone");
    myself.setDataCenterInfo(info);
    MicroserviceInstance allmatchInstance = new MicroserviceInstance();
    info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region");
    info.setAvailableZone("test-zone");
    List<String> allMatchEndpoint = new ArrayList<>();
    allMatchEndpoint.add("rest://localhost:7090");
    allmatchInstance.setEndpoints(allMatchEndpoint);
    allmatchInstance.setDataCenterInfo(info);
    allmatchInstance.setInstanceId("allmatchInstance");
    allmatchInstance.setStatus(MicroserviceInstanceStatus.TESTING);
    MicroserviceInstance regionMatchInstance = new MicroserviceInstance();
    info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region");
    info.setAvailableZone("test-zone2");
    List<String> regionMatchEndpoint = new ArrayList<>();
    regionMatchEndpoint.add("rest://localhost:7091");
    regionMatchInstance.setEndpoints(regionMatchEndpoint);
    regionMatchInstance.setDataCenterInfo(info);
    regionMatchInstance.setInstanceId("regionMatchInstance");
    MicroserviceInstance noneMatchInstance = new MicroserviceInstance();
    info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region2");
    info.setAvailableZone("test-zone2");
    List<String> noMatchEndpoint = new ArrayList<>();
    noMatchEndpoint.add("rest://localhost:7092");
    noneMatchInstance.setEndpoints(noMatchEndpoint);
    noneMatchInstance.setDataCenterInfo(info);
    noneMatchInstance.setInstanceId("noneMatchInstance");
    Map<String, MicroserviceInstance> data = new HashMap<>();
    DiscoveryTreeNode parent = new DiscoveryTreeNode().name("parent").data(data);
    scbEngine.setTransportManager(transportManager);
    LocalRegistryStore.INSTANCE.initSelfWithMocked(null, myself);
    mockUpInstanceCacheManager(instanceCacheManager);
    when(instanceCacheManager.getOrCreateVersionedCache("testApp", "testMicroserviceName", "0.0.0+")).thenReturn(parent);
    when(transportManager.findTransport("rest")).thenReturn(transport);
    LoadbalanceHandler handler = null;
    LoadBalancer loadBalancer = null;
    ServiceCombServer server = null;
    DiscoveryTree discoveryTree = new DiscoveryTree();
    discoveryTree.addFilter(new IsolationDiscoveryFilter());
    discoveryTree.addFilter(new ZoneAwareDiscoveryFilter());
    discoveryTree.addFilter(new ServerDiscoveryFilter());
    discoveryTree.sort();
    handler = new LoadbalanceHandler(discoveryTree);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals(null, server);
    data.put("noneMatchInstance", noneMatchInstance);
    parent.cacheVersion(1);
    handler = new LoadbalanceHandler();
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7092", server.getEndpoint().getEndpoint());
    handler.handle(invocation, (response) -> {
        Assert.assertEquals("OK", response.getResult());
    });
    data.put("regionMatchInstance", regionMatchInstance);
    parent.cacheVersion(parent.cacheVersion() + 1);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
    handler.handle(invocation, (response) -> {
        Assert.assertEquals("OK", response.getResult());
    });
    data.put("allmatchInstance", allmatchInstance);
    parent.cacheVersion(parent.cacheVersion() + 1);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7090", server.getEndpoint().getEndpoint());
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
    ServiceCombLoadBalancerStats.INSTANCE.markFailure(server);
    // if errorThresholdPercentage is 0,that means errorThresholdPercentage is not active.
    ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "0");
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7090", server.getEndpoint().getEndpoint());
    // if errorThresholdPercentage greater than 0, it will activate.
    ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "20");
    ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.minIsolationTime", "10");
    ServiceCombServer server2 = server;
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server2);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
    mockDelayMillis(20);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7090", server.getEndpoint().getEndpoint());
    ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
    ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
    handler.handle(invocation, (response) -> {
        Assert.assertEquals("OK", response.getResult());
    });
}
Also used : ZoneAwareDiscoveryFilter(org.apache.servicecomb.loadbalance.filter.ZoneAwareDiscoveryFilter) NonSwaggerInvocation(org.apache.servicecomb.core.NonSwaggerInvocation) Invocation(org.apache.servicecomb.core.Invocation) HashMap(java.util.HashMap) ServerDiscoveryFilter(org.apache.servicecomb.loadbalance.filter.ServerDiscoveryFilter) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) DataCenterInfo(org.apache.servicecomb.registry.api.registry.DataCenterInfo) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) InstanceCacheManager(org.apache.servicecomb.registry.cache.InstanceCacheManager) NonSwaggerInvocation(org.apache.servicecomb.core.NonSwaggerInvocation) TransportManager(org.apache.servicecomb.core.transport.TransportManager) Transport(org.apache.servicecomb.core.Transport) DiscoveryTree(org.apache.servicecomb.registry.discovery.DiscoveryTree) IsolationDiscoveryFilter(org.apache.servicecomb.loadbalance.filter.IsolationDiscoveryFilter) Test(org.junit.Test)

Example 3 with DiscoveryTree

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

the class TestLoadBalanceHandler2 method trying_chance_should_be_released.

@Test
public void trying_chance_should_be_released() {
    List<ServiceCombServer> servers = new ArrayList<>();
    ServiceCombServer serviceCombServer = createMockedServer("instanceId", "rest://127.0.0.1:8080");
    servers.add(serviceCombServer);
    DiscoveryTree discoveryTree = createMockedDiscoveryTree(servers);
    LoadbalanceHandler handler = new LoadbalanceHandler(discoveryTree);
    // mock the process of the isolated server selected and changed to TRYING status
    ServiceCombServerStats serviceCombServerStats = mockServiceCombServerStats(serviceCombServer, 5, true);
    Invocation invocation = new NonSwaggerInvocation("testApp", "testMicroserviceName", "0.0.0+", (inv, aysnc) -> {
        Assert.assertEquals("rest://127.0.0.1:8080", inv.getEndpoint().getEndpoint());
        Assert.assertTrue(serviceCombServerStats.isIsolated());
        Assert.assertEquals(5, serviceCombServerStats.getContinuousFailureCount());
        Assert.assertFalse(ServiceCombServerStats.isolatedServerCanTry());
        aysnc.success("OK");
    });
    Assert.assertTrue(ServiceCombServerStats.applyForTryingChance(invocation));
    invocation.addLocalContext(IsolationDiscoveryFilter.TRYING_INSTANCES_EXISTING, true);
    try {
        handler.handle(invocation, (response) -> Assert.assertEquals("OK", response.getResult()));
    } catch (Exception e) {
        Assert.fail("unexpected exception " + e.getMessage());
    }
    Assert.assertEquals("rest://127.0.0.1:8080", invocation.getEndpoint().getEndpoint());
    Assert.assertTrue(serviceCombServerStats.isIsolated());
    Assert.assertEquals(0, serviceCombServerStats.getContinuousFailureCount());
    Assert.assertTrue(ServiceCombServerStats.isolatedServerCanTry());
}
Also used : NonSwaggerInvocation(org.apache.servicecomb.core.NonSwaggerInvocation) Invocation(org.apache.servicecomb.core.Invocation) NonSwaggerInvocation(org.apache.servicecomb.core.NonSwaggerInvocation) ArrayList(java.util.ArrayList) DiscoveryTree(org.apache.servicecomb.registry.discovery.DiscoveryTree) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) Test(org.junit.Test)

Example 4 with DiscoveryTree

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

the class TestLoadBalanceHandler2 method testZoneAwareAndIsolationFilterUsingMockedInvocationWorks.

@Test
public void testZoneAwareAndIsolationFilterUsingMockedInvocationWorks() throws Exception {
    Invocation invocation = new NonSwaggerInvocation("testApp", "testMicroserviceName", "0.0.0+", (inv, aysnc) -> {
        aysnc.success("OK");
    });
    InstanceCacheManager instanceCacheManager = Mockito.mock(InstanceCacheManager.class);
    TransportManager transportManager = Mockito.mock(TransportManager.class);
    Transport transport = Mockito.mock(Transport.class);
    ArchaiusUtils.setProperty("servicecomb.loadbalance.filter.operation.enabled", "false");
    // set up data
    MicroserviceInstance myself = new MicroserviceInstance();
    DataCenterInfo info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region");
    info.setAvailableZone("test-zone");
    myself.setDataCenterInfo(info);
    MicroserviceInstance allmatchInstance = new MicroserviceInstance();
    info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region");
    info.setAvailableZone("test-zone");
    List<String> allMatchEndpoint = new ArrayList<>();
    allMatchEndpoint.add("rest://localhost:7090");
    allmatchInstance.setEndpoints(allMatchEndpoint);
    allmatchInstance.setDataCenterInfo(info);
    allmatchInstance.setInstanceId("allmatchInstance");
    MicroserviceInstance regionMatchInstance = new MicroserviceInstance();
    info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region");
    info.setAvailableZone("test-zone2");
    List<String> regionMatchEndpoint = new ArrayList<>();
    regionMatchEndpoint.add("rest://localhost:7091");
    regionMatchInstance.setEndpoints(regionMatchEndpoint);
    regionMatchInstance.setDataCenterInfo(info);
    regionMatchInstance.setInstanceId("regionMatchInstance");
    MicroserviceInstance noneMatchInstance = new MicroserviceInstance();
    info = new DataCenterInfo();
    info.setName("test");
    info.setRegion("test-Region2");
    info.setAvailableZone("test-zone2");
    List<String> noMatchEndpoint = new ArrayList<>();
    noMatchEndpoint.add("rest://localhost:7092");
    noneMatchInstance.setEndpoints(noMatchEndpoint);
    noneMatchInstance.setDataCenterInfo(info);
    noneMatchInstance.setInstanceId("noneMatchInstance");
    Map<String, MicroserviceInstance> data = new HashMap<>();
    DiscoveryTreeNode parent = new DiscoveryTreeNode().name("parent").data(data);
    scbEngine.setTransportManager(transportManager);
    LocalRegistryStore.INSTANCE.initSelfWithMocked(null, myself);
    mockUpInstanceCacheManager(instanceCacheManager);
    when(instanceCacheManager.getOrCreateVersionedCache("testApp", "testMicroserviceName", "0.0.0+")).thenReturn(parent);
    when(transportManager.findTransport("rest")).thenReturn(transport);
    LoadbalanceHandler handler = null;
    LoadBalancer loadBalancer = null;
    ServiceCombServer server = null;
    DiscoveryTree discoveryTree = new DiscoveryTree();
    discoveryTree.addFilter(new IsolationDiscoveryFilter());
    discoveryTree.addFilter(new ZoneAwareDiscoveryFilter());
    discoveryTree.addFilter(new ServerDiscoveryFilter());
    discoveryTree.sort();
    handler = new LoadbalanceHandler(discoveryTree);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals(null, server);
    data.put("noneMatchInstance", noneMatchInstance);
    parent.cacheVersion(1);
    handler = new LoadbalanceHandler();
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7092", server.getEndpoint().getEndpoint());
    handler.handle(invocation, (response) -> {
        Assert.assertEquals("OK", response.getResult());
    });
    data.put("regionMatchInstance", regionMatchInstance);
    parent.cacheVersion(parent.cacheVersion() + 1);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
    handler.handle(invocation, (response) -> {
        Assert.assertEquals("OK", response.getResult());
    });
    data.put("allmatchInstance", allmatchInstance);
    parent.cacheVersion(parent.cacheVersion() + 1);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7090", server.getEndpoint().getEndpoint());
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
    ServiceCombLoadBalancerStats.INSTANCE.markFailure(server);
    // if errorThresholdPercentage is 0,that means errorThresholdPercentage is not active.
    ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "0");
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7090", server.getEndpoint().getEndpoint());
    // if errorThresholdPercentage greater than 0, it will activate.
    ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "20");
    ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.minIsolationTime", "30");
    ServiceCombServer server2 = server;
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server2);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
    mockDelayMillis(31);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7090", server.getEndpoint().getEndpoint());
    ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
    ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
    server = loadBalancer.chooseServer(invocation);
    Assert.assertEquals("rest://localhost:7091", server.getEndpoint().getEndpoint());
    handler.handle(invocation, (response) -> {
        Assert.assertEquals("OK", response.getResult());
    });
}
Also used : ZoneAwareDiscoveryFilter(org.apache.servicecomb.loadbalance.filter.ZoneAwareDiscoveryFilter) NonSwaggerInvocation(org.apache.servicecomb.core.NonSwaggerInvocation) Invocation(org.apache.servicecomb.core.Invocation) HashMap(java.util.HashMap) ServerDiscoveryFilter(org.apache.servicecomb.loadbalance.filter.ServerDiscoveryFilter) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) DataCenterInfo(org.apache.servicecomb.registry.api.registry.DataCenterInfo) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) InstanceCacheManager(org.apache.servicecomb.registry.cache.InstanceCacheManager) NonSwaggerInvocation(org.apache.servicecomb.core.NonSwaggerInvocation) TransportManager(org.apache.servicecomb.core.transport.TransportManager) Transport(org.apache.servicecomb.core.Transport) DiscoveryTree(org.apache.servicecomb.registry.discovery.DiscoveryTree) IsolationDiscoveryFilter(org.apache.servicecomb.loadbalance.filter.IsolationDiscoveryFilter) Test(org.junit.Test)

Example 5 with DiscoveryTree

use of org.apache.servicecomb.registry.discovery.DiscoveryTree in project incubator-servicecomb-java-chassis by apache.

the class TestLoadBalanceHandler2 method createMockedDiscoveryTree.

/**
 * Create a mocked DiscoveryTree that always returns {@code servers} as the versionedCache result.
 */
private DiscoveryTree createMockedDiscoveryTree(List<ServiceCombServer> servers) {
    DiscoveryTree discoveryTree = Mockito.mock(DiscoveryTree.class);
    DiscoveryTreeNode versionedCache = new DiscoveryTreeNode().name("testVersionedCacheName").data(servers);
    Mockito.when(discoveryTree.discovery(Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(versionedCache);
    return discoveryTree;
}
Also used : DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) DiscoveryTree(org.apache.servicecomb.registry.discovery.DiscoveryTree)

Aggregations

DiscoveryTree (org.apache.servicecomb.registry.discovery.DiscoveryTree)8 ArrayList (java.util.ArrayList)6 Invocation (org.apache.servicecomb.core.Invocation)6 NonSwaggerInvocation (org.apache.servicecomb.core.NonSwaggerInvocation)6 DiscoveryTreeNode (org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)4 Transport (org.apache.servicecomb.core.Transport)4 TransportManager (org.apache.servicecomb.core.transport.TransportManager)4 IsolationDiscoveryFilter (org.apache.servicecomb.loadbalance.filter.IsolationDiscoveryFilter)4 ServerDiscoveryFilter (org.apache.servicecomb.loadbalance.filter.ServerDiscoveryFilter)4 ZoneAwareDiscoveryFilter (org.apache.servicecomb.loadbalance.filter.ZoneAwareDiscoveryFilter)4 DataCenterInfo (org.apache.servicecomb.registry.api.registry.DataCenterInfo)4 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)4 InstanceCacheManager (org.apache.servicecomb.registry.cache.InstanceCacheManager)4 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)2