Search in sources :

Example 1 with TimeoutException

use of org.apache.dubbo.remoting.TimeoutException in project dubbo by alibaba.

the class DefaultFutureTest method timeoutNotSend.

/**
 * for example, it will print like this:
 * before a future is create , time is : 2018-06-21 15:06:17
 * after a future is timeout , time is : 2018-06-21 15:06:22
 * <p>
 * The exception info print like:
 * Sending request timeout in client-side by scan timer.
 * start time: 2018-06-21 15:13:02.215, end time: 2018-06-21 15:13:07.231...
 */
@Test
@Disabled
public void timeoutNotSend() throws Exception {
    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    System.out.println("before a future is create , time is : " + LocalDateTime.now().format(formatter));
    // timeout after 5 seconds.
    DefaultFuture f = defaultFuture(5000);
    while (!f.isDone()) {
        // spin
        Thread.sleep(100);
    }
    System.out.println("after a future is timeout , time is : " + LocalDateTime.now().format(formatter));
    // get operate will throw a timeout exception, because the future is timeout.
    try {
        f.get();
    } catch (Exception e) {
        Assertions.assertTrue(e.getCause() instanceof TimeoutException, "catch exception is not timeout exception!");
        System.out.println(e.getMessage());
    }
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter) TimeoutException(org.apache.dubbo.remoting.TimeoutException) TimeoutException(org.apache.dubbo.remoting.TimeoutException) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 2 with TimeoutException

use of org.apache.dubbo.remoting.TimeoutException in project dubbo by alibaba.

the class ChannelWrappedInvoker method doInvoke.

@Override
protected Result doInvoke(Invocation invocation) throws Throwable {
    RpcInvocation inv = (RpcInvocation) invocation;
    // use interface's name as service path to export if it's not found on client side
    inv.setAttachment(PATH_KEY, getInterface().getName());
    inv.setAttachment(CALLBACK_SERVICE_KEY, serviceKey);
    try {
        if (RpcUtils.isOneway(getUrl(), inv)) {
            // may have concurrency issue
            currentClient.send(inv, getUrl().getMethodParameter(invocation.getMethodName(), SENT_KEY, false));
            return AsyncRpcResult.newDefaultAsyncResult(invocation);
        } else {
            final String methodName = RpcUtils.getMethodName(invocation);
            final int timeout = (int) RpcUtils.getTimeout(getUrl(), methodName, RpcContext.getContext(), DEFAULT_TIMEOUT);
            CompletableFuture<AppResponse> appResponseFuture = currentClient.request(inv, timeout, null).thenApply(obj -> (AppResponse) obj);
            return new AsyncRpcResult(appResponseFuture, inv);
        }
    } catch (RpcException e) {
        throw e;
    } catch (TimeoutException e) {
        throw new RpcException(RpcException.TIMEOUT_EXCEPTION, e.getMessage(), e);
    } catch (RemotingException e) {
        throw new RpcException(RpcException.NETWORK_EXCEPTION, e.getMessage(), e);
    } catch (Throwable e) {
        // here is non-biz exception, wrap it.
        throw new RpcException(e.getMessage(), e);
    }
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) RemotingException(org.apache.dubbo.remoting.RemotingException) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) TimeoutException(org.apache.dubbo.remoting.TimeoutException)

Example 3 with TimeoutException

use of org.apache.dubbo.remoting.TimeoutException in project dubbo by alibaba.

the class ThriftInvoker method doInvoke.

@Override
protected Result doInvoke(Invocation invocation) throws Throwable {
    RpcInvocation inv = (RpcInvocation) invocation;
    final String methodName;
    methodName = invocation.getMethodName();
    inv.setAttachment(PATH_KEY, getUrl().getPath());
    // for thrift codec
    inv.setAttachment(ThriftCodec.PARAMETER_CLASS_NAME_GENERATOR, getUrl().getParameter(ThriftCodec.PARAMETER_CLASS_NAME_GENERATOR, DubboClassNameGenerator.NAME));
    ExchangeClient currentClient;
    if (clients.length == 1) {
        currentClient = clients[0];
    } else {
        currentClient = clients[index.getAndIncrement() % clients.length];
    }
    try {
        int timeout = getUrl().getMethodParameter(methodName, TIMEOUT_KEY, DEFAULT_TIMEOUT);
        ExecutorService executor = getCallbackExecutor(getUrl(), inv);
        CompletableFuture<AppResponse> appResponseFuture = currentClient.request(inv, timeout, executor).thenApply(obj -> (AppResponse) obj);
        // save for 2.6.x compatibility, for example, TraceFilter in Zipkin uses com.alibaba.xxx.FutureAdapter
        FutureContext.getContext().setCompatibleFuture(appResponseFuture);
        AsyncRpcResult result = new AsyncRpcResult(appResponseFuture, invocation);
        result.setExecutor(executor);
        return result;
    } catch (TimeoutException e) {
        throw new RpcException(RpcException.TIMEOUT_EXCEPTION, e.getMessage(), e);
    } catch (RemotingException e) {
        throw new RpcException(RpcException.NETWORK_EXCEPTION, e.getMessage(), e);
    }
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) ExchangeClient(org.apache.dubbo.remoting.exchange.ExchangeClient) AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) RemotingException(org.apache.dubbo.remoting.RemotingException) ExecutorService(java.util.concurrent.ExecutorService) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) TimeoutException(org.apache.dubbo.remoting.TimeoutException)

Example 4 with TimeoutException

use of org.apache.dubbo.remoting.TimeoutException in project dubbo by alibaba.

the class DefaultFutureTest method timeoutSend.

/**
 * for example, it will print like this:
 * before a future is create , time is : 2018-06-21 15:11:31
 * after a future is timeout , time is : 2018-06-21 15:11:36
 * <p>
 * The exception info print like:
 * Waiting server-side response timeout by scan timer.
 * start time: 2018-06-21 15:12:38.337, end time: 2018-06-21 15:12:43.354...
 */
@Test
@Disabled
public void timeoutSend() throws Exception {
    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    System.out.println("before a future is create , time is : " + LocalDateTime.now().format(formatter));
    // timeout after 5 seconds.
    Channel channel = new MockedChannel();
    Request request = new Request(10);
    DefaultFuture f = DefaultFuture.newFuture(channel, request, 5000, null);
    // mark the future is sent
    DefaultFuture.sent(channel, request);
    while (!f.isDone()) {
        // spin
        Thread.sleep(100);
    }
    System.out.println("after a future is timeout , time is : " + LocalDateTime.now().format(formatter));
    // get operate will throw a timeout exception, because the future is timeout.
    try {
        f.get();
    } catch (Exception e) {
        Assertions.assertTrue(e.getCause() instanceof TimeoutException, "catch exception is not timeout exception!");
        System.out.println(e.getMessage());
    }
}
Also used : MockedChannel(org.apache.dubbo.remoting.handler.MockedChannel) MockedChannel(org.apache.dubbo.remoting.handler.MockedChannel) Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) DateTimeFormatter(java.time.format.DateTimeFormatter) TimeoutException(org.apache.dubbo.remoting.TimeoutException) TimeoutException(org.apache.dubbo.remoting.TimeoutException) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 5 with TimeoutException

use of org.apache.dubbo.remoting.TimeoutException 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(PATH_KEY, getUrl().getPath());
    inv.setAttachment(VERSION_KEY, version);
    ExchangeClient currentClient;
    if (clients.length == 1) {
        currentClient = clients[0];
    } else {
        currentClient = clients[index.getAndIncrement() % clients.length];
    }
    try {
        boolean isOneway = RpcUtils.isOneway(getUrl(), invocation);
        int timeout = calculateTimeout(invocation, methodName);
        invocation.put(TIMEOUT_KEY, timeout);
        if (isOneway) {
            boolean isSent = getUrl().getMethodParameter(methodName, Constants.SENT_KEY, false);
            currentClient.send(inv, isSent);
            return AsyncRpcResult.newDefaultAsyncResult(invocation);
        } else {
            ExecutorService executor = getCallbackExecutor(getUrl(), inv);
            CompletableFuture<AppResponse> appResponseFuture = currentClient.request(inv, timeout, executor).thenApply(obj -> (AppResponse) obj);
            // save for 2.6.x compatibility, for example, TraceFilter in Zipkin uses com.alibaba.xxx.FutureAdapter
            FutureContext.getContext().setCompatibleFuture(appResponseFuture);
            AsyncRpcResult result = new AsyncRpcResult(appResponseFuture, inv);
            result.setExecutor(executor);
            return result;
        }
    } 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(org.apache.dubbo.rpc.RpcInvocation) ExchangeClient(org.apache.dubbo.remoting.exchange.ExchangeClient) AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) RemotingException(org.apache.dubbo.remoting.RemotingException) ExecutorService(java.util.concurrent.ExecutorService) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) TimeoutException(org.apache.dubbo.remoting.TimeoutException)

Aggregations

TimeoutException (org.apache.dubbo.remoting.TimeoutException)5 RemotingException (org.apache.dubbo.remoting.RemotingException)3 AppResponse (org.apache.dubbo.rpc.AppResponse)3 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)3 RpcException (org.apache.dubbo.rpc.RpcException)3 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)3 DateTimeFormatter (java.time.format.DateTimeFormatter)2 ExecutorService (java.util.concurrent.ExecutorService)2 ExchangeClient (org.apache.dubbo.remoting.exchange.ExchangeClient)2 Disabled (org.junit.jupiter.api.Disabled)2 Test (org.junit.jupiter.api.Test)2 Channel (org.apache.dubbo.remoting.Channel)1 Request (org.apache.dubbo.remoting.exchange.Request)1 MockedChannel (org.apache.dubbo.remoting.handler.MockedChannel)1