Search in sources :

Example 1 with ResponseFuture

use of com.alibaba.dubbo.remoting.exchange.ResponseFuture in project dubbo by alibaba.

the class ClientToServerTest method testFuture.

@Test
public void testFuture() throws Exception {
    ResponseFuture future = client.request(new World("world"));
    Hello result = (Hello) future.get();
    Assert.assertEquals("hello,world", result.getName());
}
Also used : ResponseFuture(com.alibaba.dubbo.remoting.exchange.ResponseFuture) Test(org.junit.Test)

Example 2 with ResponseFuture

use of com.alibaba.dubbo.remoting.exchange.ResponseFuture in project brave by openzipkin.

the class TracingFilter method invoke.

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (!isInit)
        return invoker.invoke(invocation);
    TraceContext invocationContext = currentTraceContext.get();
    RpcContext rpcContext = RpcContext.getContext();
    Kind kind = rpcContext.isProviderSide() ? Kind.SERVER : Kind.CLIENT;
    Span span;
    DubboRequest request;
    if (kind.equals(Kind.CLIENT)) {
        // When A service invoke B service, then B service then invoke C service, the parentId of the
        // C service span is A when read from invocation.getAttachments(). This is because
        // AbstractInvoker adds attachments via RpcContext.getContext(), not the invocation.
        // See com.alibaba.dubbo.rpc.protocol.AbstractInvoker(line 138) from v2.6.7
        Map<String, String> attachments = RpcContext.getContext().getAttachments();
        DubboClientRequest clientRequest = new DubboClientRequest(invoker, invocation, attachments);
        request = clientRequest;
        span = clientHandler.handleSendWithParent(clientRequest, invocationContext);
    } else {
        DubboServerRequest serverRequest = new DubboServerRequest(invoker, invocation);
        request = serverRequest;
        span = serverHandler.handleReceive(serverRequest);
    }
    boolean isSynchronous = true;
    Scope scope = currentTraceContext.newScope(span.context());
    Result result = null;
    Throwable error = null;
    try {
        result = invoker.invoke(invocation);
        error = result.getException();
        // the case on async client invocation
        Future<Object> future = rpcContext.getFuture();
        if (future != null) {
            if (!(future instanceof FutureAdapter)) {
                assert false : "we can't defer the span finish unless we can access the ResponseFuture";
                return result;
            }
            isSynchronous = false;
            ResponseFuture original = ((FutureAdapter<Object>) future).getFuture();
            // See instrumentation/RATIONALE.md for why the below response callbacks are invocation context
            TraceContext callbackContext = kind == Kind.CLIENT ? invocationContext : span.context();
            ResponseFuture wrapped = new FinishSpanResponseFuture(original, this, request, result, span, callbackContext);
            RpcContext.getContext().setFuture(new FutureAdapter<>(wrapped));
        }
        return result;
    } catch (Throwable e) {
        propagateIfFatal(e);
        error = e;
        throw e;
    } finally {
        if (isSynchronous)
            FinishSpan.finish(this, request, result, error, span);
        scope.close();
    }
}
Also used : FutureAdapter(com.alibaba.dubbo.rpc.protocol.dubbo.FutureAdapter) ResponseFuture(com.alibaba.dubbo.remoting.exchange.ResponseFuture) Span(brave.Span) Result(com.alibaba.dubbo.rpc.Result) RpcContext(com.alibaba.dubbo.rpc.RpcContext) Scope(brave.propagation.CurrentTraceContext.Scope) Kind(brave.Span.Kind) CurrentTraceContext(brave.propagation.CurrentTraceContext) TraceContext(brave.propagation.TraceContext)

Example 3 with ResponseFuture

use of com.alibaba.dubbo.remoting.exchange.ResponseFuture in project dubbo by alibaba.

the class DubboInvoker method doInvoke.

@Override
protected Result doInvoke(final Invocation invocation) throws Throwable {
    RpcInvocation inv = (RpcInvocation) invocation;
    final String methodName = RpcUtils.getMethodName(invocation);
    inv.setAttachment(Constants.PATH_KEY, getUrl().getPath());
    inv.setAttachment(Constants.VERSION_KEY, version);
    ExchangeClient currentClient;
    if (clients.length == 1) {
        currentClient = clients[0];
    } else {
        currentClient = clients[index.getAndIncrement() % clients.length];
    }
    try {
        boolean isAsync = RpcUtils.isAsync(getUrl(), invocation);
        boolean isOneway = RpcUtils.isOneway(getUrl(), invocation);
        int timeout = getUrl().getMethodParameter(methodName, Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
        if (isOneway) {
            boolean isSent = getUrl().getMethodParameter(methodName, Constants.SENT_KEY, false);
            currentClient.send(inv, isSent);
            RpcContext.getContext().setFuture(null);
            return new RpcResult();
        } else if (isAsync) {
            ResponseFuture future = currentClient.request(inv, timeout);
            RpcContext.getContext().setFuture(new FutureAdapter<Object>(future));
            return new RpcResult();
        } else {
            RpcContext.getContext().setFuture(null);
            return (Result) currentClient.request(inv, timeout).get();
        }
    } catch (TimeoutException e) {
        throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "Invoke remote method timeout. method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
    } catch (RemotingException e) {
        throw new RpcException(RpcException.NETWORK_EXCEPTION, "Failed to invoke remote method: " + invocation.getMethodName() + ", provider: " + getUrl() + ", cause: " + e.getMessage(), e);
    }
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) ExchangeClient(com.alibaba.dubbo.remoting.exchange.ExchangeClient) RpcException(com.alibaba.dubbo.rpc.RpcException) RemotingException(com.alibaba.dubbo.remoting.RemotingException) RpcResult(com.alibaba.dubbo.rpc.RpcResult) ResponseFuture(com.alibaba.dubbo.remoting.exchange.ResponseFuture) TimeoutException(com.alibaba.dubbo.remoting.TimeoutException)

Example 4 with ResponseFuture

use of com.alibaba.dubbo.remoting.exchange.ResponseFuture in project dubbo by alibaba.

the class FutureFilter method asyncCallback.

private void asyncCallback(final Invoker<?> invoker, final Invocation invocation) {
    Future<?> f = RpcContext.getContext().getFuture();
    if (f instanceof FutureAdapter) {
        ResponseFuture future = ((FutureAdapter<?>) f).getFuture();
        future.setCallback(new ResponseCallback() {

            public void done(Object rpcResult) {
                if (rpcResult == null) {
                    logger.error(new IllegalStateException("invalid result value : null, expected " + Result.class.getName()));
                    return;
                }
                // /must be rpcResult
                if (!(rpcResult instanceof Result)) {
                    logger.error(new IllegalStateException("invalid result type :" + rpcResult.getClass() + ", expected " + Result.class.getName()));
                    return;
                }
                Result result = (Result) rpcResult;
                if (result.hasException()) {
                    fireThrowCallback(invoker, invocation, result.getException());
                } else {
                    fireReturnCallback(invoker, invocation, result.getValue());
                }
            }

            public void caught(Throwable exception) {
                fireThrowCallback(invoker, invocation, exception);
            }
        });
    }
}
Also used : FutureAdapter(com.alibaba.dubbo.rpc.protocol.dubbo.FutureAdapter) ResponseCallback(com.alibaba.dubbo.remoting.exchange.ResponseCallback) ResponseFuture(com.alibaba.dubbo.remoting.exchange.ResponseFuture) Result(com.alibaba.dubbo.rpc.Result)

Example 5 with ResponseFuture

use of com.alibaba.dubbo.remoting.exchange.ResponseFuture in project dubbo by alibaba.

the class ClientToServerTest method testFuture.

@Test
public void testFuture() throws Exception {
    ResponseFuture future = client.request(new World("world"));
    Hello result = (Hello) future.get();
    Assert.assertEquals("hello,world", result.getName());
}
Also used : ResponseFuture(com.alibaba.dubbo.remoting.exchange.ResponseFuture) Test(org.junit.Test)

Aggregations

ResponseFuture (com.alibaba.dubbo.remoting.exchange.ResponseFuture)6 Result (com.alibaba.dubbo.rpc.Result)2 FutureAdapter (com.alibaba.dubbo.rpc.protocol.dubbo.FutureAdapter)2 Test (org.junit.Test)2 Span (brave.Span)1 Kind (brave.Span.Kind)1 CurrentTraceContext (brave.propagation.CurrentTraceContext)1 Scope (brave.propagation.CurrentTraceContext.Scope)1 TraceContext (brave.propagation.TraceContext)1 RemotingException (com.alibaba.dubbo.remoting.RemotingException)1 TimeoutException (com.alibaba.dubbo.remoting.TimeoutException)1 ExchangeChannel (com.alibaba.dubbo.remoting.exchange.ExchangeChannel)1 ExchangeClient (com.alibaba.dubbo.remoting.exchange.ExchangeClient)1 ResponseCallback (com.alibaba.dubbo.remoting.exchange.ResponseCallback)1 RpcContext (com.alibaba.dubbo.rpc.RpcContext)1 RpcException (com.alibaba.dubbo.rpc.RpcException)1 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)1 RpcResult (com.alibaba.dubbo.rpc.RpcResult)1