Search in sources :

Example 16 with RemotingTimeoutException

use of org.apache.rocketmq.remoting.exception.RemotingTimeoutException in project rocketmq by apache.

the class MQClientAPIImplTest method testSendMessageOneWay_WithException.

@Test
public void testSendMessageOneWay_WithException() throws RemotingException, InterruptedException, MQBrokerException {
    doThrow(new RemotingTimeoutException("Remoting Exception in Test")).when(remotingClient).invokeOneway(anyString(), any(RemotingCommand.class), anyLong());
    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ONEWAY, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(RemotingException.class);
    } catch (RemotingException e) {
        assertThat(e).hasMessage("Remoting Exception in Test");
    }
    doThrow(new InterruptedException("Interrupted Exception in Test")).when(remotingClient).invokeOneway(anyString(), any(RemotingCommand.class), anyLong());
    try {
        mqClientAPI.sendMessage(brokerAddr, brokerName, msg, new SendMessageRequestHeader(), 3 * 1000, CommunicationMode.ONEWAY, new SendMessageContext(), defaultMQProducerImpl);
        failBecauseExceptionWasNotThrown(InterruptedException.class);
    } catch (InterruptedException e) {
        assertThat(e).hasMessage("Interrupted Exception in Test");
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SendMessageRequestHeader(org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) SendMessageContext(org.apache.rocketmq.client.hook.SendMessageContext) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) Test(org.junit.Test)

Example 17 with RemotingTimeoutException

use of org.apache.rocketmq.remoting.exception.RemotingTimeoutException in project rocketmq by apache.

the class AdminBrokerProcessor method callConsumer.

private RemotingCommand callConsumer(final int requestCode, final RemotingCommand request, final String consumerGroup, final String clientId) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    ClientChannelInfo clientChannelInfo = this.brokerController.getConsumerManager().findChannel(consumerGroup, clientId);
    if (null == clientChannelInfo) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("The Consumer <%s> <%s> not online", consumerGroup, clientId));
        return response;
    }
    if (clientChannelInfo.getVersion() < MQVersion.Version.V3_1_8_SNAPSHOT.ordinal()) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("The Consumer <%s> Version <%s> too low to finish, please upgrade it to V3_1_8_SNAPSHOT", clientId, MQVersion.getVersionDesc(clientChannelInfo.getVersion())));
        return response;
    }
    try {
        RemotingCommand newRequest = RemotingCommand.createRequestCommand(requestCode, null);
        newRequest.setExtFields(request.getExtFields());
        newRequest.setBody(request.getBody());
        return this.brokerController.getBroker2Client().callClient(clientChannelInfo.getChannel(), newRequest);
    } catch (RemotingTimeoutException e) {
        response.setCode(ResponseCode.CONSUME_MSG_TIMEOUT);
        response.setRemark(String.format("consumer <%s> <%s> Timeout: %s", consumerGroup, clientId, RemotingHelper.exceptionSimpleDesc(e)));
        return response;
    } catch (Exception e) {
        response.setCode(ResponseCode.SYSTEM_ERROR);
        response.setRemark(String.format("invoke consumer <%s> <%s> Exception: %s", consumerGroup, clientId, RemotingHelper.exceptionSimpleDesc(e)));
        return response;
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ClientChannelInfo(org.apache.rocketmq.broker.client.ClientChannelInfo) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException)

Example 18 with RemotingTimeoutException

use of org.apache.rocketmq.remoting.exception.RemotingTimeoutException in project rocketmq by apache.

the class NettyRemotingAbstract method invokeSyncImpl.

public RemotingCommand invokeSyncImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis) throws InterruptedException, RemotingSendRequestException, RemotingTimeoutException {
    final int opaque = request.getOpaque();
    try {
        final ResponseFuture responseFuture = new ResponseFuture(opaque, timeoutMillis, null, null);
        this.responseTable.put(opaque, responseFuture);
        final SocketAddress addr = channel.remoteAddress();
        channel.writeAndFlush(request).addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture f) throws Exception {
                if (f.isSuccess()) {
                    responseFuture.setSendRequestOK(true);
                    return;
                } else {
                    responseFuture.setSendRequestOK(false);
                }
                responseTable.remove(opaque);
                responseFuture.setCause(f.cause());
                responseFuture.putResponse(null);
                log.warn("send a request command to channel <" + addr + "> failed.");
            }
        });
        RemotingCommand responseCommand = responseFuture.waitResponse(timeoutMillis);
        if (null == responseCommand) {
            if (responseFuture.isSendRequestOK()) {
                throw new RemotingTimeoutException(RemotingHelper.parseSocketAddressAddr(addr), timeoutMillis, responseFuture.getCause());
            } else {
                throw new RemotingSendRequestException(RemotingHelper.parseSocketAddressAddr(addr), responseFuture.getCause());
            }
        }
        return responseCommand;
    } finally {
        this.responseTable.remove(opaque);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) SocketAddress(java.net.SocketAddress) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RemotingTooMuchRequestException(org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException)

Example 19 with RemotingTimeoutException

use of org.apache.rocketmq.remoting.exception.RemotingTimeoutException in project rocketmq by apache.

the class NettyRemotingAbstract method invokeAsyncImpl.

public void invokeAsyncImpl(final Channel channel, final RemotingCommand request, final long timeoutMillis, final InvokeCallback invokeCallback) throws InterruptedException, RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException {
    final int opaque = request.getOpaque();
    boolean acquired = this.semaphoreAsync.tryAcquire(timeoutMillis, TimeUnit.MILLISECONDS);
    if (acquired) {
        final SemaphoreReleaseOnlyOnce once = new SemaphoreReleaseOnlyOnce(this.semaphoreAsync);
        final ResponseFuture responseFuture = new ResponseFuture(opaque, timeoutMillis, invokeCallback, once);
        this.responseTable.put(opaque, responseFuture);
        try {
            channel.writeAndFlush(request).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture f) throws Exception {
                    if (f.isSuccess()) {
                        responseFuture.setSendRequestOK(true);
                        return;
                    } else {
                        responseFuture.setSendRequestOK(false);
                    }
                    responseFuture.putResponse(null);
                    responseTable.remove(opaque);
                    try {
                        executeInvokeCallback(responseFuture);
                    } catch (Throwable e) {
                        log.warn("excute callback in writeAndFlush addListener, and callback throw", e);
                    } finally {
                        responseFuture.release();
                    }
                    log.warn("send a request command to channel <{}> failed.", RemotingHelper.parseChannelRemoteAddr(channel));
                }
            });
        } catch (Exception e) {
            responseFuture.release();
            log.warn("send a request command to channel <" + RemotingHelper.parseChannelRemoteAddr(channel) + "> Exception", e);
            throw new RemotingSendRequestException(RemotingHelper.parseChannelRemoteAddr(channel), e);
        }
    } else {
        if (timeoutMillis <= 0) {
            throw new RemotingTooMuchRequestException("invokeAsyncImpl invoke too fast");
        } else {
            String info = String.format("invokeAsyncImpl tryAcquire semaphore timeout, %dms, waiting thread nums: %d semaphoreAsyncValue: %d", timeoutMillis, this.semaphoreAsync.getQueueLength(), this.semaphoreAsync.availablePermits());
            log.warn(info);
            throw new RemotingTimeoutException(info);
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RemotingTooMuchRequestException(org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) SemaphoreReleaseOnlyOnce(org.apache.rocketmq.remoting.common.SemaphoreReleaseOnlyOnce) RemotingTooMuchRequestException(org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException)

Example 20 with RemotingTimeoutException

use of org.apache.rocketmq.remoting.exception.RemotingTimeoutException in project rocketmq by apache.

the class RemotingHelper method invokeSync.

public static RemotingCommand invokeSync(final String addr, final RemotingCommand request, final long timeoutMillis) throws InterruptedException, RemotingConnectException, RemotingSendRequestException, RemotingTimeoutException {
    long beginTime = System.currentTimeMillis();
    SocketAddress socketAddress = RemotingUtil.string2SocketAddress(addr);
    SocketChannel socketChannel = RemotingUtil.connect(socketAddress);
    if (socketChannel != null) {
        boolean sendRequestOK = false;
        try {
            socketChannel.configureBlocking(true);
            // bugfix  http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4614802
            socketChannel.socket().setSoTimeout((int) timeoutMillis);
            ByteBuffer byteBufferRequest = request.encode();
            while (byteBufferRequest.hasRemaining()) {
                int length = socketChannel.write(byteBufferRequest);
                if (length > 0) {
                    if (byteBufferRequest.hasRemaining()) {
                        if ((System.currentTimeMillis() - beginTime) > timeoutMillis) {
                            throw new RemotingSendRequestException(addr);
                        }
                    }
                } else {
                    throw new RemotingSendRequestException(addr);
                }
                Thread.sleep(1);
            }
            sendRequestOK = true;
            ByteBuffer byteBufferSize = ByteBuffer.allocate(4);
            while (byteBufferSize.hasRemaining()) {
                int length = socketChannel.read(byteBufferSize);
                if (length > 0) {
                    if (byteBufferSize.hasRemaining()) {
                        if ((System.currentTimeMillis() - beginTime) > timeoutMillis) {
                            throw new RemotingTimeoutException(addr, timeoutMillis);
                        }
                    }
                } else {
                    throw new RemotingTimeoutException(addr, timeoutMillis);
                }
                Thread.sleep(1);
            }
            int size = byteBufferSize.getInt(0);
            ByteBuffer byteBufferBody = ByteBuffer.allocate(size);
            while (byteBufferBody.hasRemaining()) {
                int length = socketChannel.read(byteBufferBody);
                if (length > 0) {
                    if (byteBufferBody.hasRemaining()) {
                        if ((System.currentTimeMillis() - beginTime) > timeoutMillis) {
                            throw new RemotingTimeoutException(addr, timeoutMillis);
                        }
                    }
                } else {
                    throw new RemotingTimeoutException(addr, timeoutMillis);
                }
                Thread.sleep(1);
            }
            byteBufferBody.flip();
            return RemotingCommand.decode(byteBufferBody);
        } catch (IOException e) {
            log.error("invokeSync failure", e);
            if (sendRequestOK) {
                throw new RemotingTimeoutException(addr, timeoutMillis);
            } else {
                throw new RemotingSendRequestException(addr);
            }
        } finally {
            try {
                socketChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else {
        throw new RemotingConnectException(addr);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ByteBuffer(java.nio.ByteBuffer)

Aggregations

RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)20 RemotingSendRequestException (org.apache.rocketmq.remoting.exception.RemotingSendRequestException)12 RemotingConnectException (org.apache.rocketmq.remoting.exception.RemotingConnectException)8 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)8 ChannelFuture (io.netty.channel.ChannelFuture)6 ChannelFutureListener (io.netty.channel.ChannelFutureListener)6 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)6 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)6 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)6 RemotingTooMuchRequestException (org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException)6 IOException (java.io.IOException)4 SocketAddress (java.net.SocketAddress)4 UnknownHostException (java.net.UnknownHostException)4 Map (java.util.Map)4 Set (java.util.Set)4 TreeSet (java.util.TreeSet)4 SendMessageContext (org.apache.rocketmq.client.hook.SendMessageContext)4 ClusterInfo (org.apache.rocketmq.common.protocol.body.ClusterInfo)4 KVTable (org.apache.rocketmq.common.protocol.body.KVTable)4 SendMessageRequestHeader (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)4