Search in sources :

Example 1 with LoadBalance

use of org.apache.dubbo.rpc.cluster.LoadBalance 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);
        Assertions.assertSame(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);
        Assertions.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);
            Assertions.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);
            Assertions.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);
            Assertions.assertTrue(sinvoker == invoker2 || sinvoker == invoker4);
        }
    }
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RoundRobinLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) Test(org.junit.jupiter.api.Test)

Example 2 with LoadBalance

use of org.apache.dubbo.rpc.cluster.LoadBalance in project dubbo by alibaba.

the class AbstractClusterInvokerTest method testSelect_Invokersize1.

@Test
public void testSelect_Invokersize1() throws Exception {
    invokers.clear();
    invokers.add(invoker1);
    LoadBalance l = cluster.initLoadBalance(invokers, invocation);
    Assertions.assertNotNull(l, "cluster.initLoadBalance returns null!");
    Invoker invoker = cluster.select(l, null, invokers, null);
    Assertions.assertEquals(invoker1, invoker);
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RoundRobinLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) Test(org.junit.jupiter.api.Test)

Example 3 with LoadBalance

use of org.apache.dubbo.rpc.cluster.LoadBalance 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();
    Assertions.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);
        Assertions.assertTrue(sinvoker.isAvailable());
        Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
    }
    for (int i = 0; i < runs; i++) {
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assertions.assertTrue(sinvoker.isAvailable());
        Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
    }
    for (int i = 0; i < runs; i++) {
        selectedInvokers.clear();
        selectedInvokers.add(invoker2);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assertions.assertTrue(sinvoker.isAvailable());
        Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
    }
    for (int i = 0; i < runs; i++) {
        selectedInvokers.clear();
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker4);
        Invoker sinvoker = cluster.select(lb, invocation, invokers, selectedInvokers);
        Assertions.assertTrue(sinvoker.isAvailable());
        Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
    }
    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);
        Assertions.assertTrue(sinvoker.isAvailable());
        Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
    }
    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);
        Assertions.assertTrue(sinvoker.isAvailable());
        Mockito.clearInvocations(invoker1, invoker2, invoker3, invoker4, invoker5);
    }
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RoundRobinLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance)

Example 4 with LoadBalance

use of org.apache.dubbo.rpc.cluster.LoadBalance in project dubbo by alibaba.

the class LoadBalanceBaseTest method getInvokeCounter.

public Map<Invoker, AtomicLong> getInvokeCounter(int runs, String loadbalanceName) {
    Map<Invoker, AtomicLong> counter = new ConcurrentHashMap<Invoker, AtomicLong>();
    LoadBalance lb = getLoadBalance(loadbalanceName);
    for (Invoker invoker : invokers) {
        counter.put(invoker, new AtomicLong(0));
    }
    URL url = invokers.get(0).getUrl();
    for (int i = 0; i < runs; i++) {
        Invoker sinvoker = lb.select(invokers, url, invocation);
        counter.get(sinvoker).incrementAndGet();
    }
    return counter;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Invoker(org.apache.dubbo.rpc.Invoker) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(org.apache.dubbo.common.URL)

Example 5 with LoadBalance

use of org.apache.dubbo.rpc.cluster.LoadBalance in project dubbo by alibaba.

the class AbstractClusterInvokerTest method setUp.

@SuppressWarnings({ "unchecked" })
@BeforeEach
public void setUp() throws Exception {
    invocation.setMethodName("sayHello");
    invoker1 = mock(Invoker.class);
    invoker2 = mock(Invoker.class);
    invoker3 = mock(Invoker.class);
    invoker4 = mock(Invoker.class);
    invoker5 = mock(Invoker.class);
    mockedInvoker1 = mock(Invoker.class);
    URL turl = URL.valueOf("test://test:11/test");
    given(invoker1.isAvailable()).willReturn(false);
    given(invoker1.getInterface()).willReturn(IHelloService.class);
    given(invoker1.getUrl()).willReturn(turl.setPort(1).addParameter("name", "invoker1"));
    given(invoker2.isAvailable()).willReturn(true);
    given(invoker2.getInterface()).willReturn(IHelloService.class);
    given(invoker2.getUrl()).willReturn(turl.setPort(2).addParameter("name", "invoker2"));
    given(invoker3.isAvailable()).willReturn(false);
    given(invoker3.getInterface()).willReturn(IHelloService.class);
    given(invoker3.getUrl()).willReturn(turl.setPort(3).addParameter("name", "invoker3"));
    given(invoker4.isAvailable()).willReturn(true);
    given(invoker4.getInterface()).willReturn(IHelloService.class);
    given(invoker4.getUrl()).willReturn(turl.setPort(4).addParameter("name", "invoker4"));
    given(invoker5.isAvailable()).willReturn(false);
    given(invoker5.getInterface()).willReturn(IHelloService.class);
    given(invoker5.getUrl()).willReturn(turl.setPort(5).addParameter("name", "invoker5"));
    given(mockedInvoker1.isAvailable()).willReturn(false);
    given(mockedInvoker1.getInterface()).willReturn(IHelloService.class);
    given(mockedInvoker1.getUrl()).willReturn(turl.setPort(999).setProtocol("mock"));
    invokers.add(invoker1);
    dic = new StaticDirectory<>(url, invokers, null);
    dic.setConsumerUrl(consumerUrl);
    cluster = new AbstractClusterInvoker(dic) {

        @Override
        protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
            return null;
        }
    };
    cluster_nocheck = new AbstractClusterInvoker(dic, url.addParameterIfAbsent(CLUSTER_AVAILABLE_CHECK_KEY, Boolean.FALSE.toString())) {

        @Override
        protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
            return null;
        }
    };
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RoundRobinLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) RpcException(org.apache.dubbo.rpc.RpcException) ArrayList(java.util.ArrayList) List(java.util.List) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

Invoker (org.apache.dubbo.rpc.Invoker)13 LoadBalance (org.apache.dubbo.rpc.cluster.LoadBalance)13 LeastActiveLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance)9 RandomLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance)9 RoundRobinLoadBalance (org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance)9 Test (org.junit.jupiter.api.Test)7 URL (org.apache.dubbo.common.URL)4 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Invocation (org.apache.dubbo.rpc.Invocation)3 RpcException (org.apache.dubbo.rpc.RpcException)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ProxyFactory (org.apache.dubbo.rpc.ProxyFactory)2 StaticDirectory (org.apache.dubbo.rpc.cluster.directory.StaticDirectory)2 AbstractClusterInvoker (org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker)2 Map (java.util.Map)1 Result (org.apache.dubbo.rpc.Result)1 ClusterInvoker (org.apache.dubbo.rpc.cluster.ClusterInvoker)1