Search in sources :

Example 41 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class AbstractClusterInvokerTest method testSelect_multiInvokers.

public void testSelect_multiInvokers(String lbname) throws Exception {
    int min = 1000, max = 5000;
    Double d = (Math.random() * (max - min + 1) + min);
    int runs = d.intValue();
    Assert.assertTrue(runs > min);
    LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(lbname);
    initlistsize5();
    for (int i = 0; i < runs; i++) {
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertEquals(true, sinvoker.isAvailable());
    }
    for (int i = 0; i < runs; i++) {
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertEquals(true, sinvoker.isAvailable());
    }
    for (int i = 0; i < runs; i++) {
        selectedInvokers.clear();
        selectedInvokers.add(invoker2);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertEquals(true, sinvoker.isAvailable());
    }
    for (int i = 0; i < runs; i++) {
        selectedInvokers.clear();
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker4);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertEquals(true, sinvoker.isAvailable());
    }
    for (int i = 0; i < runs; i++) {
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        selectedInvokers.add(invoker3);
        selectedInvokers.add(invoker5);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertEquals(true, sinvoker.isAvailable());
    }
    for (int i = 0; i < runs; i++) {
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker3);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertEquals(true, sinvoker.isAvailable());
    }
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) RoundRobinLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) LoadBalance(com.alibaba.dubbo.rpc.cluster.LoadBalance)

Example 42 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class AbstractClusterInvokerTest method testMockedInvokerSelect.

/**
 * Test mock invoker selector works as expected
 */
@Test
public void testMockedInvokerSelect() {
    initlistsize5();
    invokers.add(mockedInvoker1);
    RpcInvocation mockedInvocation = new RpcInvocation();
    mockedInvocation.setMethodName("sayHello");
    mockedInvocation.setAttachment(Constants.INVOCATION_NEED_MOCK, "true");
    List<Invoker<IHelloService>> mockedInvokers = dic.list(mockedInvocation);
    Assert.assertEquals(1, mockedInvokers.size());
    List<Invoker<IHelloService>> invokers = dic.list(invocation);
    Assert.assertEquals(5, invokers.size());
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invoker(com.alibaba.dubbo.rpc.Invoker) Test(org.junit.Test)

Example 43 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class AbstractClusterInvokerTest method testSelectAgainAndCheckAvailable.

@Test
public void testSelectAgainAndCheckAvailable() {
    LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(RoundRobinLoadBalance.NAME);
    initlistsize5();
    {
        // Boundary condition test .
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker3);
        selectedInvokers.add(invoker5);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertTrue(sinvoker == invoker4);
    }
    {
        // Boundary condition test .
        selectedInvokers.clear();
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker3);
        selectedInvokers.add(invoker4);
        selectedInvokers.add(invoker5);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4);
    }
    {
        // Boundary condition test .
        for (int i = 0; i < 100; i++) {
            selectedInvokers.clear();
            Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
            Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4);
        }
    }
    {
        // Boundary condition test .
        for (int i = 0; i < 100; i++) {
            selectedInvokers.clear();
            selectedInvokers.add(invoker1);
            selectedInvokers.add(invoker3);
            selectedInvokers.add(invoker5);
            Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
            Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4);
        }
    }
    {
        // Boundary condition test .
        for (int i = 0; i < 100; i++) {
            selectedInvokers.clear();
            selectedInvokers.add(invoker1);
            selectedInvokers.add(invoker3);
            selectedInvokers.add(invoker2);
            selectedInvokers.add(invoker4);
            selectedInvokers.add(invoker5);
            Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
            Assert.assertTrue(sinvoker == invoker2 || sinvoker == invoker4);
        }
    }
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) RoundRobinLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) LoadBalance(com.alibaba.dubbo.rpc.cluster.LoadBalance) Test(org.junit.Test)

Example 44 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class AbstractClusterInvokerTest method testSelectBalance.

/**
 * Test balance.
 */
@Test
public void testSelectBalance() {
    LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(RoundRobinLoadBalance.NAME);
    initlistsize5();
    Map<Invoker, AtomicLong> counter = new ConcurrentHashMap<Invoker, AtomicLong>();
    for (Invoker invoker : invokers) {
        counter.put(invoker, new AtomicLong(0));
    }
    int runs = 1000;
    for (int i = 0; i < runs; i++) {
        selectedInvokers.clear();
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        counter.get(sinvoker).incrementAndGet();
    }
    for (Invoker minvoker : counter.keySet()) {
        Long count = counter.get(minvoker).get();
        // System.out.println(count);
        if (minvoker.isAvailable())
            Assert.assertTrue("count should > avg", count > runs / invokers.size());
    }
    Assert.assertEquals(runs, counter.get(invoker2).get() + counter.get(invoker4).get());
    ;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Invoker(com.alibaba.dubbo.rpc.Invoker) RoundRobinLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) LoadBalance(com.alibaba.dubbo.rpc.cluster.LoadBalance) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 45 with Invoker

use of com.alibaba.dubbo.rpc.Invoker in project dubbo by alibaba.

the class FailoverClusterInvokerTest method testInvokerDestoryAndReList.

/**
 * When invokers in directory changes after a failed request but just before a retry effort,
 * then we should reselect from the latest invokers before retry.
 */
@Test
public void testInvokerDestoryAndReList() {
    final URL url = URL.valueOf("test://localhost/" + Demo.class.getName() + "?loadbalance=roundrobin&retries=" + retries);
    RpcException exception = new RpcException(RpcException.TIMEOUT_EXCEPTION);
    MockInvoker<Demo> invoker1 = new MockInvoker<Demo>(Demo.class, url);
    invoker1.setException(exception);
    MockInvoker<Demo> invoker2 = new MockInvoker<Demo>(Demo.class, url);
    invoker2.setException(exception);
    final List<Invoker<Demo>> invokers = new ArrayList<Invoker<Demo>>();
    invokers.add(invoker1);
    invokers.add(invoker2);
    Callable<Object> callable = new Callable<Object>() {

        public Object call() throws Exception {
            // Simulation: all invokers are destroyed
            for (Invoker<Demo> invoker : invokers) {
                invoker.destroy();
            }
            invokers.clear();
            MockInvoker<Demo> invoker3 = new MockInvoker<Demo>(Demo.class, url);
            invokers.add(invoker3);
            return null;
        }
    };
    invoker1.setCallable(callable);
    invoker2.setCallable(callable);
    RpcInvocation inv = new RpcInvocation();
    inv.setMethodName("test");
    Directory<Demo> dic = new MockDirectory<Demo>(url, invokers);
    FailoverClusterInvoker<Demo> clusterinvoker = new FailoverClusterInvoker<Demo>(dic);
    clusterinvoker.invoke(inv);
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) ArrayList(java.util.ArrayList) URL(com.alibaba.dubbo.common.URL) Callable(java.util.concurrent.Callable) Invoker(com.alibaba.dubbo.rpc.Invoker) AbstractInvoker(com.alibaba.dubbo.rpc.protocol.AbstractInvoker) RpcException(com.alibaba.dubbo.rpc.RpcException) Test(org.junit.Test)

Aggregations

Invoker (com.alibaba.dubbo.rpc.Invoker)67 Test (org.junit.Test)50 ArrayList (java.util.ArrayList)39 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)38 URL (com.alibaba.dubbo.common.URL)34 RegistryDirectory (com.alibaba.dubbo.registry.integration.RegistryDirectory)19 Router (com.alibaba.dubbo.rpc.cluster.Router)14 MockInvoker (com.alibaba.dubbo.rpc.cluster.router.MockInvoker)13 Result (com.alibaba.dubbo.rpc.Result)11 Invocation (com.alibaba.dubbo.rpc.Invocation)10 RpcException (com.alibaba.dubbo.rpc.RpcException)9 RpcResult (com.alibaba.dubbo.rpc.RpcResult)9 LoadBalance (com.alibaba.dubbo.rpc.cluster.LoadBalance)8 DemoService (com.alibaba.dubbo.rpc.support.DemoService)7 LeastActiveLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance)5 RandomLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance)5 RoundRobinLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 List (java.util.List)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4