Search in sources :

Example 81 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class ScriptRouterTest method testRoute_throwException.

@Test
public void testRoute_throwException() {
    List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
    MockInvoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.1:20880/com.dubbo.HelloService"));
    MockInvoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.2:20880/com.dubbo.HelloService"));
    MockInvoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.3:20880/com.dubbo.HelloService"));
    invokers.add(invoker1);
    invokers.add(invoker2);
    invokers.add(invoker3);
    String script = "/";
    Router router = new ScriptRouterFactory().getRouter(getRouteUrl(script));
    List<Invoker<String>> routeResult = router.route(invokers, invokers.get(0).getUrl(), new RpcInvocation());
    Assertions.assertEquals(3, routeResult.size());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) ArrayList(java.util.ArrayList) Router(org.apache.dubbo.rpc.cluster.Router) Test(org.junit.jupiter.api.Test)

Example 82 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class ScriptRouterTest method testRouteHostFilter.

@Test
public void testRouteHostFilter() {
    List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
    MockInvoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.1:20880/com.dubbo.HelloService"));
    MockInvoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.2:20880/com.dubbo.HelloService"));
    MockInvoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://10.134.108.3:20880/com.dubbo.HelloService"));
    invokers.add(invoker1);
    invokers.add(invoker2);
    invokers.add(invoker3);
    String script = "function route(invokers, invocation, context){ " + "	var result = new java.util.ArrayList(invokers.size()); " + "	var targetHost = new java.util.ArrayList(); " + "	targetHost.add(\"10.134.108.2\"); " + "	for (var i = 0; i < invokers.length; i++) { " + "		if(targetHost.contains(invokers[i].getUrl().getHost())){ " + "			result.add(invokers[i]); " + "		} " + "	} " + "	return result; " + "} " + "route(invokers, invocation, context) ";
    Router router = new ScriptRouterFactory().getRouter(getRouteUrl(script));
    List<Invoker<String>> routeResult = router.route(invokers, invokers.get(0).getUrl(), new RpcInvocation());
    Assertions.assertEquals(1, routeResult.size());
    Assertions.assertEquals(invoker2, routeResult.get(0));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) ArrayList(java.util.ArrayList) Router(org.apache.dubbo.rpc.cluster.Router) Test(org.junit.jupiter.api.Test)

Example 83 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class ScriptRouterTest method testRoutePickInvokers.

@Test
public void testRoutePickInvokers() {
    String rule = "var result = new java.util.ArrayList(invokers.size());" + "for (i=0;i<invokers.size(); i++){ " + "if (invokers.get(i).isAvailable()) {" + "result.add(invokers.get(i)) ;" + "}" + "} ; " + "return result;";
    String script = "function route(invokers,invocation,context){" + rule + "} route(invokers,invocation,context)";
    Router router = new ScriptRouterFactory().getRouter(getRouteUrl(script));
    List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
    Invoker<String> invoker1 = new MockInvoker<String>(false);
    Invoker<String> invoker2 = new MockInvoker<String>(true);
    Invoker<String> invoker3 = new MockInvoker<String>(true);
    invokers.add(invoker1);
    invokers.add(invoker2);
    invokers.add(invoker3);
    List<Invoker<String>> filteredInvokers = router.route(invokers, invokers.get(0).getUrl(), new RpcInvocation());
    Assertions.assertEquals(2, filteredInvokers.size());
    Assertions.assertEquals(invoker2, filteredInvokers.get(0));
    Assertions.assertEquals(invoker3, filteredInvokers.get(1));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) MockInvoker(org.apache.dubbo.rpc.cluster.router.MockInvoker) ArrayList(java.util.ArrayList) Router(org.apache.dubbo.rpc.cluster.Router) Test(org.junit.jupiter.api.Test)

Example 84 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class ConsistentHashLoadBalanceTest method testConsistentHashLoadBalance.

/**
 * Test case for hot key
 * https://github.com/apache/dubbo/issues/4103
 * invoke method with same params for 10000 times:
 * 1. count of request accept by each invoker will not
 * be higher than (overloadRatioAllowed * average + 1)
 */
@Test
public void testConsistentHashLoadBalance() {
    int runs = 10000;
    Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, ConsistentHashLoadBalance.NAME);
    double overloadRatioAllowed = 1.5F;
    int serverCount = counter.size();
    double overloadThread = ((double) runs * overloadRatioAllowed) / ((double) serverCount);
    for (Invoker invoker : counter.keySet()) {
        Long count = counter.get(invoker).get();
        Assertions.assertTrue(count < (overloadThread + 1L), "count of request accept by each invoker will not be higher than (overloadRatioAllowed * average + 1)");
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Invoker(org.apache.dubbo.rpc.Invoker) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.jupiter.api.Test)

Example 85 with Invoker

use of org.apache.dubbo.rpc.Invoker in project dubbo by alibaba.

the class ConsistentHashLoadBalanceTest method testNormalWhenRouterEnabled.

// https://github.com/apache/dubbo/issues/5429
@Test
void testNormalWhenRouterEnabled() {
    ConsistentHashLoadBalance lb = (ConsistentHashLoadBalance) getLoadBalance(ConsistentHashLoadBalance.NAME);
    URL url = invokers.get(0).getUrl();
    RouterChain<LoadBalanceBaseTest> routerChain = RouterChain.buildChain(url);
    Invoker<LoadBalanceBaseTest> result = lb.select(invokers, url, invocation);
    int originalHashCode = lb.getCorrespondingHashCode(invokers);
    for (int i = 0; i < 100; i++) {
        routerChain.setInvokers(invokers);
        List<Invoker<LoadBalanceBaseTest>> routeInvokers = routerChain.route(url, invocation);
        Invoker<LoadBalanceBaseTest> finalInvoker = lb.select(routeInvokers, url, invocation);
        Assertions.assertEquals(originalHashCode, lb.getCorrespondingHashCode(routeInvokers));
    }
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Aggregations

Invoker (org.apache.dubbo.rpc.Invoker)128 Test (org.junit.jupiter.api.Test)104 URL (org.apache.dubbo.common.URL)74 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)66 ArrayList (java.util.ArrayList)48 Invocation (org.apache.dubbo.rpc.Invocation)42 Result (org.apache.dubbo.rpc.Result)26 AppResponse (org.apache.dubbo.rpc.AppResponse)21 MockClusterInvoker (org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker)21 RegistryDirectory (org.apache.dubbo.registry.integration.RegistryDirectory)20 Router (org.apache.dubbo.rpc.cluster.Router)18 MockInvoker (org.apache.dubbo.rpc.cluster.router.MockInvoker)18 HashMap (java.util.HashMap)13 LoadBalance (org.apache.dubbo.rpc.cluster.LoadBalance)13 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)12 RpcException (org.apache.dubbo.rpc.RpcException)12 LeastActiveLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance)8 RandomLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance)8 RoundRobinLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8