Search in sources :

Example 6 with RemotingSendRequestException

use of org.apache.rocketmq.remoting.exception.RemotingSendRequestException 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 7 with RemotingSendRequestException

use of org.apache.rocketmq.remoting.exception.RemotingSendRequestException 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 8 with RemotingSendRequestException

use of org.apache.rocketmq.remoting.exception.RemotingSendRequestException 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)

Example 9 with RemotingSendRequestException

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

the class TlsTest method noClientAuthFailure.

/**
 * Tests that a server configured to require client authentication actually does require client
 * authentication.
 */
@Test
public void noClientAuthFailure() throws Exception {
    try {
        RemotingCommand response = remotingClient.invokeSync("localhost:8888", createRequest(), 1000 * 3);
        failBecauseExceptionWasNotThrown(RemotingSendRequestException.class);
    } catch (RemotingSendRequestException ignore) {
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) Test(org.junit.Test)

Example 10 with RemotingSendRequestException

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

the class TlsTest method clientRejectsUntrustedServerCert.

/**
 * Tests that a client configured using GrpcSslContexts refuses to talk to a server that has an
 * an untrusted certificate.
 */
@Test
public void clientRejectsUntrustedServerCert() throws Exception {
    try {
        RemotingCommand response = remotingClient.invokeSync("localhost:8888", createRequest(), 1000 * 3);
        failBecauseExceptionWasNotThrown(RemotingSendRequestException.class);
    } catch (RemotingSendRequestException ignore) {
    }
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) Test(org.junit.Test)

Aggregations

RemotingSendRequestException (org.apache.rocketmq.remoting.exception.RemotingSendRequestException)16 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)12 ChannelFuture (io.netty.channel.ChannelFuture)6 ChannelFutureListener (io.netty.channel.ChannelFutureListener)6 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)6 RemotingConnectException (org.apache.rocketmq.remoting.exception.RemotingConnectException)6 RemotingTooMuchRequestException (org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException)6 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)6 SocketAddress (java.net.SocketAddress)4 Map (java.util.Map)4 Set (java.util.Set)4 TreeSet (java.util.TreeSet)4 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)4 ClusterInfo (org.apache.rocketmq.common.protocol.body.ClusterInfo)4 KVTable (org.apache.rocketmq.common.protocol.body.KVTable)4 BrokerData (org.apache.rocketmq.common.protocol.route.BrokerData)4 SemaphoreReleaseOnlyOnce (org.apache.rocketmq.remoting.common.SemaphoreReleaseOnlyOnce)4 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)4 Test (org.junit.Test)4 IOException (java.io.IOException)2