Search in sources :

Example 86 with Invoker

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

the class RoundRobinLoadBalanceTest method testRoundRobinLoadBalanceSelect.

@Test
public void testRoundRobinLoadBalanceSelect() {
    int runs = 10000;
    Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, RoundRobinLoadBalance.NAME);
    for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
        Long count = entry.getValue().get();
        Assertions.assertTrue(Math.abs(count - runs / (0f + invokers.size())) < 1f, "abs diff should < 1");
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Invoker(org.apache.dubbo.rpc.Invoker) AtomicLong(java.util.concurrent.atomic.AtomicLong) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 87 with Invoker

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

the class RoundRobinLoadBalanceTest method testSelectByWeight.

@Test
public void testSelectByWeight() {
    final Map<Invoker, InvokeResult> totalMap = new HashMap<Invoker, InvokeResult>();
    final AtomicBoolean shouldBegin = new AtomicBoolean(false);
    final int runs = 10000;
    List<Thread> threads = new ArrayList<Thread>();
    int threadNum = 10;
    for (int i = 0; i < threadNum; i++) {
        threads.add(new Thread(() -> {
            while (!shouldBegin.get()) {
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                }
            }
            Map<Invoker, InvokeResult> resultMap = getWeightedInvokeResult(runs, RoundRobinLoadBalance.NAME);
            synchronized (totalMap) {
                for (Entry<Invoker, InvokeResult> entry : resultMap.entrySet()) {
                    if (!totalMap.containsKey(entry.getKey())) {
                        totalMap.put(entry.getKey(), entry.getValue());
                    } else {
                        totalMap.get(entry.getKey()).getCount().addAndGet(entry.getValue().getCount().get());
                    }
                }
            }
        }));
    }
    for (Thread thread : threads) {
        thread.start();
    }
    // let's rock it!
    shouldBegin.set(true);
    for (Thread thread : threads) {
        try {
            thread.join();
        } catch (InterruptedException e) {
        }
    }
    assertStrictWRRResult(runs * threadNum, totalMap);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Entry(java.util.Map.Entry) Invoker(org.apache.dubbo.rpc.Invoker) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 88 with Invoker

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

the class MockClusterInvokerTest method getClusterInvokerMock.

private Invoker<IHelloService> getClusterInvokerMock(URL url, Invoker<IHelloService> mockInvoker) {
    // As `javassist` have a strict restriction of argument types, request will fail if Invocation do not contains complete parameter type information
    final URL durl = url.addParameter("proxy", "jdk");
    invokers.clear();
    ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getExtension("jdk");
    Invoker<IHelloService> invoker1 = proxy.getInvoker(new HelloService(), IHelloService.class, durl);
    invokers.add(invoker1);
    if (mockInvoker != null) {
        invokers.add(mockInvoker);
    }
    StaticDirectory<IHelloService> dic = new StaticDirectory<IHelloService>(durl, invokers, null);
    dic.buildRouterChain();
    AbstractClusterInvoker<IHelloService> cluster = new AbstractClusterInvoker(dic) {

        @Override
        protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
            if (durl.getParameter("invoke_return_error", false)) {
                throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "test rpc exception");
            } else {
                return ((Invoker<?>) invokers.get(0)).invoke(invocation);
            }
        }
    };
    return new MockClusterInvoker<IHelloService>(dic, cluster);
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) AbstractClusterInvoker(org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker) URL(org.apache.dubbo.common.URL) StaticDirectory(org.apache.dubbo.rpc.cluster.directory.StaticDirectory) Invoker(org.apache.dubbo.rpc.Invoker) AbstractClusterInvoker(org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RpcException(org.apache.dubbo.rpc.RpcException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 89 with Invoker

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

the class MockProviderRpcExceptionTest method getClusterInvokerMock.

private Invoker<IHelloRpcService> getClusterInvokerMock(URL url, Invoker<IHelloRpcService> mockInvoker) {
    // As `javassist` have a strict restriction of argument types, request will fail if Invocation do not contains complete parameter type information
    final URL durl = url.addParameter("proxy", "jdk");
    invokers.clear();
    ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getExtension("jdk");
    Invoker<IHelloRpcService> invoker1 = proxy.getInvoker(new HelloRpcService(), IHelloRpcService.class, durl);
    invokers.add(invoker1);
    if (mockInvoker != null) {
        invokers.add(mockInvoker);
    }
    StaticDirectory<IHelloRpcService> dic = new StaticDirectory<IHelloRpcService>(durl, invokers, null);
    dic.buildRouterChain();
    AbstractClusterInvoker<IHelloRpcService> cluster = new AbstractClusterInvoker(dic) {

        @Override
        protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
            if (durl.getParameter("invoke_return_error", false)) {
                throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "test rpc exception ");
            } else {
                return ((Invoker<?>) invokers.get(0)).invoke(invocation);
            }
        }
    };
    return new MockClusterInvoker<IHelloRpcService>(dic, cluster);
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) ProxyFactory(org.apache.dubbo.rpc.ProxyFactory) AbstractClusterInvoker(org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker) URL(org.apache.dubbo.common.URL) StaticDirectory(org.apache.dubbo.rpc.cluster.directory.StaticDirectory) Invoker(org.apache.dubbo.rpc.Invoker) AbstractClusterInvoker(org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RpcException(org.apache.dubbo.rpc.RpcException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 90 with Invoker

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

the class DubboMonitorTest method testAvailable.

@Test
public void testAvailable() {
    Invoker invoker = mock(Invoker.class);
    MonitorService monitorService = mock(MonitorService.class);
    given(invoker.isAvailable()).willReturn(true);
    given(invoker.getUrl()).willReturn(URL.valueOf("dubbo://127.0.0.1:7070?interval=20"));
    DubboMonitor dubboMonitor = new DubboMonitor(invoker, monitorService);
    assertThat(dubboMonitor.isAvailable(), is(true));
    verify(invoker).isAvailable();
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) MonitorService(org.apache.dubbo.monitor.MonitorService) 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