Search in sources :

Example 1 with CommandTimeoutException

use of com.ctrip.xpipe.command.CommandTimeoutException in project x-pipe by ctripcorp.

the class AbstractNettyRequestResponseCommand method doSendRequest.

@Override
protected void doSendRequest(final NettyClient nettyClient, ByteBuf byteBuf) {
    if (logRequest()) {
        logger.info("[doSendRequest]{}, {}", nettyClient, ByteBufUtils.readToString(byteBuf.slice()));
    }
    if (hasResponse()) {
        nettyClient.sendRequest(byteBuf, this);
    } else {
        nettyClient.sendRequest(byteBuf);
        // TODO sendfuture, make sure send success
        future().setSuccess(null);
        return;
    }
    if (getCommandTimeoutMilli() > 0 && scheduled != null) {
        logger.debug("[doSendRequest][schedule timeout]{}, {}", this, getCommandTimeoutMilli());
        final ScheduledFuture<?> timeoutFuture = scheduled.schedule(new AbstractExceptionLogTask() {

            @Override
            public void doRun() {
                logger.info("[run][timeout]{}", nettyClient);
                future().setFailure(new CommandTimeoutException("timeout " + +getCommandTimeoutMilli()));
            }
        }, getCommandTimeoutMilli(), TimeUnit.MILLISECONDS);
        future().addListener(new CommandFutureListener<V>() {

            @Override
            public void operationComplete(CommandFuture<V> commandFuture) {
                boolean cancel = true;
                try {
                    commandFuture.get();
                } catch (InterruptedException e) {
                } catch (ExecutionException e) {
                    if (e.getCause() instanceof CommandTimeoutException) {
                        cancel = false;
                    }
                }
                if (cancel) {
                    logger.debug("[operationComplete][cancel timeout future]");
                    timeoutFuture.cancel(false);
                }
            }
        });
    }
}
Also used : CommandTimeoutException(com.ctrip.xpipe.command.CommandTimeoutException) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with CommandTimeoutException

use of com.ctrip.xpipe.command.CommandTimeoutException in project x-pipe by ctripcorp.

the class RedisCommandTest method testTimeoutNext.

@Test
public void testTimeoutNext() throws Exception {
    Server server = startEchoPrefixServer(String.valueOf((char) RedisClientProtocol.PLUS_BYTE));
    SimpleObjectPool<NettyClient> keyPool = getXpipeNettyClientKeyedObjectPool().getKeyPool(new InetSocketAddress("127.0.0.1", server.getPort()));
    int sleepTime = timeoutMilli + 50;
    String str1 = String.format("sleep %d %s\r\n", sleepTime, randomString(10));
    String str2 = randomString(10) + "\r\n";
    try {
        new TestCommand(str1, timeoutMilli, keyPool, scheduled).execute().get();
        Assert.fail();
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof CommandTimeoutException);
    }
    sleep(sleepTime * 2);
    new TestCommand(str2, sleepTime, keyPool, scheduled).execute().get();
}
Also used : NettyClient(com.ctrip.xpipe.netty.commands.NettyClient) CommandTimeoutException(com.ctrip.xpipe.command.CommandTimeoutException) Server(com.ctrip.xpipe.simpleserver.Server) InetSocketAddress(java.net.InetSocketAddress) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) AbstractRedisTest(com.ctrip.xpipe.redis.core.AbstractRedisTest)

Example 3 with CommandTimeoutException

use of com.ctrip.xpipe.command.CommandTimeoutException in project x-pipe by ctripcorp.

the class RequestResponseCommandTest method testTimeout.

@Test
public void testTimeout() throws CommandExecutionException, InterruptedException {
    TestCommand testCommand = new TestCommand("sleep 5000\r\n", 1000, clientPool, scheduled, null);
    CommandFuture<String> future = testCommand.execute();
    final AtomicReference<CommandFuture<String>> listenerFuture = new AtomicReference<CommandFuture<String>>(null);
    final CountDownLatch latch = new CountDownLatch(1);
    future.addListener(new CommandFutureListener<String>() {

        @Override
        public void operationComplete(CommandFuture<String> commandFuture) throws Exception {
            try {
                listenerFuture.set(commandFuture);
            } finally {
                latch.countDown();
            }
        }
    });
    try {
        future.get();
        Assert.fail();
    } catch (InterruptedException e) {
        Assert.fail();
    } catch (ExecutionException e) {
        if (!(e.getCause() instanceof CommandTimeoutException)) {
            Assert.fail();
        }
    }
    latch.await();
    Assert.assertTrue(listenerFuture.get() != null);
}
Also used : CommandFuture(com.ctrip.xpipe.api.command.CommandFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) CommandExecutionException(com.ctrip.xpipe.command.CommandExecutionException) ExecutionException(java.util.concurrent.ExecutionException) BorrowObjectException(com.ctrip.xpipe.pool.BorrowObjectException) CommandTimeoutException(com.ctrip.xpipe.command.CommandTimeoutException) CommandTimeoutException(com.ctrip.xpipe.command.CommandTimeoutException) CommandExecutionException(com.ctrip.xpipe.command.CommandExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Aggregations

CommandTimeoutException (com.ctrip.xpipe.command.CommandTimeoutException)3 ExecutionException (java.util.concurrent.ExecutionException)3 Test (org.junit.Test)2 AbstractTest (com.ctrip.xpipe.AbstractTest)1 CommandFuture (com.ctrip.xpipe.api.command.CommandFuture)1 CommandExecutionException (com.ctrip.xpipe.command.CommandExecutionException)1 AbstractExceptionLogTask (com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)1 NettyClient (com.ctrip.xpipe.netty.commands.NettyClient)1 BorrowObjectException (com.ctrip.xpipe.pool.BorrowObjectException)1 AbstractRedisTest (com.ctrip.xpipe.redis.core.AbstractRedisTest)1 Server (com.ctrip.xpipe.simpleserver.Server)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1