Search in sources :

Example 21 with Invocation

use of com.alibaba.dubbo.rpc.Invocation 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 22 with Invocation

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

the class FailbackClusterInvoker method retryFailed.

void retryFailed() {
    if (failed.size() == 0) {
        return;
    }
    for (Map.Entry<Invocation, AbstractClusterInvoker<?>> entry : new HashMap<Invocation, AbstractClusterInvoker<?>>(failed).entrySet()) {
        Invocation invocation = entry.getKey();
        Invoker<?> invoker = entry.getValue();
        try {
            invoker.invoke(invocation);
            failed.remove(invocation);
        } catch (Throwable e) {
            logger.error("Failed retry to invoke method " + invocation.getMethodName() + ", waiting again.", e);
        }
    }
}
Also used : Invocation(com.alibaba.dubbo.rpc.Invocation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 23 with Invocation

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

the class ConditionRouterTest method testRoute_methodRoute.

@Test
public void testRoute_methodRoute() {
    Invocation invocation = new RpcInvocation("getFoo", new Class<?>[0], new Object[0]);
    // More than one methods, mismatch
    Router router = new ConditionRouterFactory().getRouter(getRouteUrl("methods=getFoo => host = 1.2.3.4"));
    boolean matchWhen = ((ConditionRouter) router).matchWhen(URL.valueOf("consumer://1.1.1.1/com.foo.BarService?methods=setFoo,getFoo,findFoo"), invocation);
    Assert.assertEquals(true, matchWhen);
    // Exactly one method, match
    matchWhen = ((ConditionRouter) router).matchWhen(URL.valueOf("consumer://1.1.1.1/com.foo.BarService?methods=getFoo"), invocation);
    Assert.assertEquals(true, matchWhen);
    // Method routing and Other condition routing can work together
    Router router2 = new ConditionRouterFactory().getRouter(getRouteUrl("methods=getFoo & host!=1.1.1.1 => host = 1.2.3.4"));
    matchWhen = ((ConditionRouter) router2).matchWhen(URL.valueOf("consumer://1.1.1.1/com.foo.BarService?methods=getFoo"), invocation);
    Assert.assertEquals(false, matchWhen);
    Router router3 = new ConditionRouterFactory().getRouter(getRouteUrl("methods=getFoo & host=1.1.1.1 => host = 1.2.3.4"));
    matchWhen = ((ConditionRouter) router3).matchWhen(URL.valueOf("consumer://1.1.1.1/com.foo.BarService?methods=getFoo"), invocation);
    Assert.assertEquals(true, matchWhen);
    // Test filter condition
    List<Invoker<String>> invokers = new ArrayList<Invoker<String>>();
    Invoker<String> invoker1 = new MockInvoker<String>(URL.valueOf("dubbo://10.20.3.3:20880/com.foo.BarService"));
    Invoker<String> invoker2 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
    Invoker<String> invoker3 = new MockInvoker<String>(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/com.foo.BarService"));
    invokers.add(invoker1);
    invokers.add(invoker2);
    invokers.add(invoker3);
    Router router4 = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " & methods = getFoo => " + " host = 10.20.3.3").addParameter(Constants.FORCE_KEY, String.valueOf(true)));
    List<Invoker<String>> fileredInvokers1 = router4.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), invocation);
    Assert.assertEquals(1, fileredInvokers1.size());
    Router router5 = new ConditionRouterFactory().getRouter(getRouteUrl("host = " + NetUtils.getLocalHost() + " & methods = unvalidmethod => " + " host = 10.20.3.3").addParameter(Constants.FORCE_KEY, String.valueOf(true)));
    List<Invoker<String>> fileredInvokers2 = router5.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), invocation);
    Assert.assertEquals(3, fileredInvokers2.size());
// Request a non-exists method
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invocation(com.alibaba.dubbo.rpc.Invocation) Invoker(com.alibaba.dubbo.rpc.Invoker) MockInvoker(com.alibaba.dubbo.rpc.cluster.router.MockInvoker) MockInvoker(com.alibaba.dubbo.rpc.cluster.router.MockInvoker) ArrayList(java.util.ArrayList) Router(com.alibaba.dubbo.rpc.cluster.Router) Test(org.junit.Test)

Example 24 with Invocation

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

the class AbstractClusterInvokerTest method testTimeoutExceptionCode.

@Test()
public void testTimeoutExceptionCode() {
    List<Invoker<DemoService>> invokers = new ArrayList<Invoker<DemoService>>();
    invokers.add(new Invoker<DemoService>() {

        public Class<DemoService> getInterface() {
            return DemoService.class;
        }

        public URL getUrl() {
            return URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/" + DemoService.class.getName());
        }

        public boolean isAvailable() {
            return false;
        }

        public Result invoke(Invocation invocation) throws RpcException {
            throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "test timeout");
        }

        public void destroy() {
        }
    });
    Directory<DemoService> directory = new StaticDirectory<DemoService>(invokers);
    FailoverClusterInvoker<DemoService> failoverClusterInvoker = new FailoverClusterInvoker<DemoService>(directory);
    try {
        failoverClusterInvoker.invoke(new RpcInvocation("sayHello", new Class<?>[0], new Object[0]));
        Assert.fail();
    } catch (RpcException e) {
        Assert.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
    }
    ForkingClusterInvoker<DemoService> forkingClusterInvoker = new ForkingClusterInvoker<DemoService>(directory);
    try {
        forkingClusterInvoker.invoke(new RpcInvocation("sayHello", new Class<?>[0], new Object[0]));
        Assert.fail();
    } catch (RpcException e) {
        Assert.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
    }
    FailfastClusterInvoker<DemoService> failfastClusterInvoker = new FailfastClusterInvoker<DemoService>(directory);
    try {
        failfastClusterInvoker.invoke(new RpcInvocation("sayHello", new Class<?>[0], new Object[0]));
        Assert.fail();
    } catch (RpcException e) {
        Assert.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
    }
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invocation(com.alibaba.dubbo.rpc.Invocation) RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) ArrayList(java.util.ArrayList) DemoService(com.alibaba.dubbo.rpc.cluster.filter.DemoService) URL(com.alibaba.dubbo.common.URL) Result(com.alibaba.dubbo.rpc.Result) StaticDirectory(com.alibaba.dubbo.rpc.cluster.directory.StaticDirectory) Invoker(com.alibaba.dubbo.rpc.Invoker) RpcException(com.alibaba.dubbo.rpc.RpcException) BeforeClass(org.junit.BeforeClass) Test(org.junit.Test)

Example 25 with Invocation

use of com.alibaba.dubbo.rpc.Invocation 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)

Aggregations

Invocation (com.alibaba.dubbo.rpc.Invocation)40 Test (org.junit.Test)30 URL (com.alibaba.dubbo.common.URL)29 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)14 Result (com.alibaba.dubbo.rpc.Result)13 Invoker (com.alibaba.dubbo.rpc.Invoker)12 RpcResult (com.alibaba.dubbo.rpc.RpcResult)12 DemoService (com.alibaba.dubbo.rpc.support.DemoService)11 MockInvocation (com.alibaba.dubbo.rpc.support.MockInvocation)11 MyInvoker (com.alibaba.dubbo.rpc.support.MyInvoker)10 RpcException (com.alibaba.dubbo.rpc.RpcException)9 ArrayList (java.util.ArrayList)4 Router (com.alibaba.dubbo.rpc.cluster.Router)3 BlockMyInvoker (com.alibaba.dubbo.rpc.support.BlockMyInvoker)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 MonitorService (com.alibaba.dubbo.monitor.MonitorService)2 LoadBalance (com.alibaba.dubbo.rpc.cluster.LoadBalance)2 StaticDirectory (com.alibaba.dubbo.rpc.cluster.directory.StaticDirectory)2 AbstractInvoker (com.alibaba.dubbo.rpc.protocol.AbstractInvoker)2