Search in sources :

Example 1 with LoadBalance

use of com.alibaba.dubbo.rpc.cluster.LoadBalance in project dubbo by alibaba.

the class MockClusterInvokerTest method getClusterInvoker.

@SuppressWarnings({ "unchecked", "rawtypes" })
private Invoker<IHelloService> getClusterInvoker(URL url) {
    // 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);
    Directory<IHelloService> dic = new StaticDirectory<IHelloService>(durl, invokers, null);
    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 : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invocation(com.alibaba.dubbo.rpc.Invocation) ProxyFactory(com.alibaba.dubbo.rpc.ProxyFactory) AbstractClusterInvoker(com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker) URL(com.alibaba.dubbo.common.URL) StaticDirectory(com.alibaba.dubbo.rpc.cluster.directory.StaticDirectory) Invoker(com.alibaba.dubbo.rpc.Invoker) AbstractClusterInvoker(com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker) LoadBalance(com.alibaba.dubbo.rpc.cluster.LoadBalance) RpcException(com.alibaba.dubbo.rpc.RpcException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with LoadBalance

use of com.alibaba.dubbo.rpc.cluster.LoadBalance in project dubbo by alibaba.

the class AbstractClusterInvoker method invoke.

public Result invoke(final Invocation invocation) throws RpcException {
    checkWhetherDestroyed();
    LoadBalance loadbalance = null;
    List<Invoker<T>> invokers = list(invocation);
    if (invokers != null && !invokers.isEmpty()) {
        loadbalance = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(invokers.get(0).getUrl().getMethodParameter(invocation.getMethodName(), Constants.LOADBALANCE_KEY, Constants.DEFAULT_LOADBALANCE));
    }
    RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
    return doInvoke(invocation, invokers, loadbalance);
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) LoadBalance(com.alibaba.dubbo.rpc.cluster.LoadBalance)

Example 3 with LoadBalance

use of com.alibaba.dubbo.rpc.cluster.LoadBalance in project dubbo by alibaba.

the class LoadBalanceTest method getInvokeCounter.

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

Example 4 with LoadBalance

use of com.alibaba.dubbo.rpc.cluster.LoadBalance in project dubbo by alibaba.

the class AbstractClusterInvokerTest method setUp.

@SuppressWarnings({ "unchecked" })
@Before
public void setUp() throws Exception {
    invocation.setMethodName("sayHello");
    invoker1 = EasyMock.createMock(Invoker.class);
    invoker2 = EasyMock.createMock(Invoker.class);
    invoker3 = EasyMock.createMock(Invoker.class);
    invoker4 = EasyMock.createMock(Invoker.class);
    invoker5 = EasyMock.createMock(Invoker.class);
    mockedInvoker1 = EasyMock.createMock(Invoker.class);
    URL turl = URL.valueOf("test://test:11/test");
    EasyMock.expect(invoker1.isAvailable()).andReturn(false).anyTimes();
    EasyMock.expect(invoker1.getInterface()).andReturn(IHelloService.class).anyTimes();
    EasyMock.expect(invoker1.getUrl()).andReturn(turl.addParameter("name", "invoker1")).anyTimes();
    EasyMock.expect(invoker2.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker2.getInterface()).andReturn(IHelloService.class).anyTimes();
    EasyMock.expect(invoker2.getUrl()).andReturn(turl.addParameter("name", "invoker2")).anyTimes();
    EasyMock.expect(invoker3.isAvailable()).andReturn(false).anyTimes();
    EasyMock.expect(invoker3.getInterface()).andReturn(IHelloService.class).anyTimes();
    EasyMock.expect(invoker3.getUrl()).andReturn(turl.addParameter("name", "invoker3")).anyTimes();
    EasyMock.expect(invoker4.isAvailable()).andReturn(true).anyTimes();
    EasyMock.expect(invoker4.getInterface()).andReturn(IHelloService.class).anyTimes();
    EasyMock.expect(invoker4.getUrl()).andReturn(turl.addParameter("name", "invoker4")).anyTimes();
    EasyMock.expect(invoker5.isAvailable()).andReturn(false).anyTimes();
    EasyMock.expect(invoker5.getInterface()).andReturn(IHelloService.class).anyTimes();
    EasyMock.expect(invoker5.getUrl()).andReturn(turl.addParameter("name", "invoker5")).anyTimes();
    EasyMock.expect(mockedInvoker1.isAvailable()).andReturn(false).anyTimes();
    EasyMock.expect(mockedInvoker1.getInterface()).andReturn(IHelloService.class).anyTimes();
    EasyMock.expect(mockedInvoker1.getUrl()).andReturn(turl.setProtocol("mock")).anyTimes();
    EasyMock.replay(invoker1, invoker2, invoker3, invoker4, invoker5, mockedInvoker1);
    invokers.add(invoker1);
    dic = new StaticDirectory<IHelloService>(url, invokers, null);
    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(Constants.CLUSTER_AVAILABLE_CHECK_KEY, Boolean.FALSE.toString())) {

        @Override
        protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
            return null;
        }
    };
}
Also used : Invoker(com.alibaba.dubbo.rpc.Invoker) Invocation(com.alibaba.dubbo.rpc.Invocation) RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) 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) RpcException(com.alibaba.dubbo.rpc.RpcException) ArrayList(java.util.ArrayList) List(java.util.List) URL(com.alibaba.dubbo.common.URL) Result(com.alibaba.dubbo.rpc.Result) Before(org.junit.Before)

Example 5 with LoadBalance

use of com.alibaba.dubbo.rpc.cluster.LoadBalance in project dubbo by alibaba.

the class AbstractClusterInvokerTest method testDonotSelectAgainAndNoCheckAvailable.

@Test
public void testDonotSelectAgainAndNoCheckAvailable() {
    LoadBalance lb = ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(RoundRobinLoadBalance.NAME);
    initlistsize5();
    {
        // Boundary condition test .
        selectedInvokers.clear();
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker3);
        selectedInvokers.add(invoker4);
        selectedInvokers.add(invoker5);
        Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertSame(invoker1, sinvoker);
    }
    {
        // Boundary condition test .
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        selectedInvokers.add(invoker3);
        selectedInvokers.add(invoker4);
        selectedInvokers.add(invoker5);
        Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertSame(invoker2, sinvoker);
    }
    {
        // Boundary condition test .
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker4);
        selectedInvokers.add(invoker5);
        Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertSame(invoker3, sinvoker);
    }
    {
        // Boundary condition test .
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker3);
        selectedInvokers.add(invoker4);
        Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertSame(invoker5, sinvoker);
    }
    {
        // Boundary condition test .
        selectedInvokers.clear();
        selectedInvokers.add(invoker1);
        selectedInvokers.add(invoker2);
        selectedInvokers.add(invoker3);
        selectedInvokers.add(invoker4);
        selectedInvokers.add(invoker5);
        Invoker sinvoker = cluster_nocheck.select(lb, invocation, invokers, selectedInvokers);
        Assert.assertTrue(invokers.contains(sinvoker));
    }
}
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)

Aggregations

Invoker (com.alibaba.dubbo.rpc.Invoker)9 LoadBalance (com.alibaba.dubbo.rpc.cluster.LoadBalance)9 LeastActiveLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance)6 RandomLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance)6 RoundRobinLoadBalance (com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance)6 Test (org.junit.Test)4 URL (com.alibaba.dubbo.common.URL)2 Invocation (com.alibaba.dubbo.rpc.Invocation)2 RpcException (com.alibaba.dubbo.rpc.RpcException)2 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ProxyFactory (com.alibaba.dubbo.rpc.ProxyFactory)1 Result (com.alibaba.dubbo.rpc.Result)1 StaticDirectory (com.alibaba.dubbo.rpc.cluster.directory.StaticDirectory)1 AbstractClusterInvoker (com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker)1 Before (org.junit.Before)1