Search in sources :

Example 26 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project redisson by redisson.

the class RedisCommonBatchExecutor method sendCommand.

private void sendCommand(RedisConnection connection, CompletableFuture<Void> attemptPromise, List<CommandData<?, ?>> list) {
    boolean isAtomic = options.getExecutionMode() != ExecutionMode.IN_MEMORY;
    boolean isQueued = options.getExecutionMode() == ExecutionMode.REDIS_READ_ATOMIC || options.getExecutionMode() == ExecutionMode.REDIS_WRITE_ATOMIC;
    CommandData<?, ?> lastCommand = connection.getLastCommand();
    if (lastCommand != null && options.isSkipResult()) {
        writeFuture = connection.getChannel().newPromise();
        lastCommand.getPromise().whenComplete((r, e) -> {
            CommandData<?, ?> currentLastCommand = connection.getLastCommand();
            if (lastCommand != currentLastCommand && currentLastCommand != null) {
                sendCommand(connection, attemptPromise, list);
                return;
            }
            ChannelFuture wf = connection.send(new CommandsData(attemptPromise, list, options.isSkipResult(), isAtomic, isQueued, options.getSyncSlaves() > 0));
            wf.addListener((ChannelFutureListener) future -> {
                if (future.isSuccess()) {
                    ((ChannelPromise) writeFuture).trySuccess(future.getNow());
                } else {
                    ((ChannelPromise) writeFuture).tryFailure(future.cause());
                }
            });
        });
        return;
    }
    writeFuture = connection.send(new CommandsData(attemptPromise, list, options.isSkipResult(), isAtomic, isQueued, options.getSyncSlaves() > 0));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RedisConnection(org.redisson.client.RedisConnection) Logger(org.slf4j.Logger) StringCodec(org.redisson.client.codec.StringCodec) ConnectionManager(org.redisson.connection.ConnectionManager) LoggerFactory(org.slf4j.LoggerFactory) CommandData(org.redisson.client.protocol.CommandData) CompletableFuture(java.util.concurrent.CompletableFuture) BatchOptions(org.redisson.api.BatchOptions) ExecutionMode(org.redisson.api.BatchOptions.ExecutionMode) Redirect(org.redisson.connection.NodeSource.Redirect) ChannelFuture(io.netty.channel.ChannelFuture) RedisCommands(org.redisson.client.protocol.RedisCommands) ArrayList(java.util.ArrayList) NodeSource(org.redisson.connection.NodeSource) List(java.util.List) ChannelPromise(io.netty.channel.ChannelPromise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Entry(org.redisson.command.CommandBatchService.Entry) RedissonObjectBuilder(org.redisson.liveobject.core.RedissonObjectBuilder) CommandsData(org.redisson.client.protocol.CommandsData) CommandsData(org.redisson.client.protocol.CommandsData)

Example 27 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project cassandra by apache.

the class InitialConnectionHandler method decode.

protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> list) throws Exception {
    Envelope inbound = decoder.decode(buffer);
    if (inbound == null)
        return;
    try {
        Envelope outbound;
        switch(inbound.header.type) {
            case OPTIONS:
                logger.trace("OPTIONS received {}", inbound.header.version);
                List<String> cqlVersions = new ArrayList<>();
                cqlVersions.add(QueryProcessor.CQL_VERSION.toString());
                List<String> compressions = new ArrayList<>();
                if (Compressor.SnappyCompressor.instance != null)
                    compressions.add("snappy");
                // LZ4 is always available since worst case scenario it default to a pure JAVA implem.
                compressions.add("lz4");
                Map<String, List<String>> supportedOptions = new HashMap<>();
                supportedOptions.put(StartupMessage.CQL_VERSION, cqlVersions);
                supportedOptions.put(StartupMessage.COMPRESSION, compressions);
                supportedOptions.put(StartupMessage.PROTOCOL_VERSIONS, ProtocolVersion.supportedVersions());
                SupportedMessage supported = new SupportedMessage(supportedOptions);
                supported.setStreamId(inbound.header.streamId);
                outbound = supported.encode(inbound.header.version);
                ctx.writeAndFlush(outbound);
                break;
            case STARTUP:
                Attribute<Connection> attrConn = ctx.channel().attr(Connection.attributeKey);
                Connection connection = attrConn.get();
                if (connection == null) {
                    connection = factory.newConnection(ctx.channel(), inbound.header.version);
                    attrConn.set(connection);
                }
                assert connection instanceof ServerConnection;
                StartupMessage startup = (StartupMessage) Message.Decoder.decodeMessage(ctx.channel(), inbound);
                InetAddress remoteAddress = ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress();
                final ClientResourceLimits.Allocator allocator = ClientResourceLimits.getAllocatorForEndpoint(remoteAddress);
                ChannelPromise promise;
                if (inbound.header.version.isGreaterOrEqualTo(ProtocolVersion.V5)) {
                    // in this case we need to defer configuring the pipeline until after the response
                    // has been sent, as the frame encoding specified in v5 should not be applied to
                    // the STARTUP response.
                    allocator.allocate(inbound.header.bodySizeInBytes);
                    promise = AsyncChannelPromise.withListener(ctx, future -> {
                        if (future.isSuccess()) {
                            logger.trace("Response to STARTUP sent, configuring pipeline for {}", inbound.header.version);
                            configurator.configureModernPipeline(ctx, allocator, inbound.header.version, startup.options);
                            allocator.release(inbound.header.bodySizeInBytes);
                        } else {
                            Throwable cause = future.cause();
                            if (null == cause)
                                cause = new ServerError("Unexpected error establishing connection");
                            logger.warn("Writing response to STARTUP failed, unable to configure pipeline", cause);
                            ErrorMessage error = ErrorMessage.fromException(cause);
                            Envelope response = error.encode(inbound.header.version);
                            ChannelPromise closeChannel = AsyncChannelPromise.withListener(ctx, f -> ctx.close());
                            ctx.writeAndFlush(response, closeChannel);
                            if (ctx.channel().isOpen())
                                ctx.channel().close();
                        }
                    });
                } else {
                    // no need to configure the pipeline asynchronously in this case
                    // the capacity obtained from allocator for the STARTUP message
                    // is released when flushed by the legacy dispatcher/flusher so
                    // there's no need to explicitly release that here either.
                    configurator.configureLegacyPipeline(ctx, allocator);
                    promise = new VoidChannelPromise(ctx.channel(), false);
                }
                final Message.Response response = Dispatcher.processRequest(ctx.channel(), startup, Overload.NONE);
                outbound = response.encode(inbound.header.version);
                ctx.writeAndFlush(outbound, promise);
                logger.trace("Configured pipeline: {}", ctx.pipeline());
                break;
            default:
                ErrorMessage error = ErrorMessage.fromException(new ProtocolException(String.format("Unexpected message %s, expecting STARTUP or OPTIONS", inbound.header.type)));
                outbound = error.encode(inbound.header.version);
                ctx.writeAndFlush(outbound);
        }
    } finally {
        inbound.release();
    }
}
Also used : Attribute(io.netty.util.Attribute) Logger(org.slf4j.Logger) AsyncChannelPromise(org.apache.cassandra.net.AsyncChannelPromise) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Overload(org.apache.cassandra.transport.ClientResourceLimits.Overload) QueryProcessor(org.apache.cassandra.cql3.QueryProcessor) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) VoidChannelPromise(io.netty.channel.VoidChannelPromise) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) ChannelPromise(io.netty.channel.ChannelPromise) Map(java.util.Map) StartupMessage(org.apache.cassandra.transport.messages.StartupMessage) SupportedMessage(org.apache.cassandra.transport.messages.SupportedMessage) ErrorMessage(org.apache.cassandra.transport.messages.ErrorMessage) ByteToMessageDecoder(io.netty.handler.codec.ByteToMessageDecoder) StartupMessage(org.apache.cassandra.transport.messages.StartupMessage) SupportedMessage(org.apache.cassandra.transport.messages.SupportedMessage) ErrorMessage(org.apache.cassandra.transport.messages.ErrorMessage) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) StartupMessage(org.apache.cassandra.transport.messages.StartupMessage) VoidChannelPromise(io.netty.channel.VoidChannelPromise) AsyncChannelPromise(org.apache.cassandra.net.AsyncChannelPromise) VoidChannelPromise(io.netty.channel.VoidChannelPromise) ChannelPromise(io.netty.channel.ChannelPromise) ArrayList(java.util.ArrayList) List(java.util.List) SupportedMessage(org.apache.cassandra.transport.messages.SupportedMessage) ErrorMessage(org.apache.cassandra.transport.messages.ErrorMessage) InetAddress(java.net.InetAddress)

Example 28 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project cassandra by apache.

the class AsyncMessageOutputPlus method doFlush.

@Override
protected void doFlush(int count) throws IOException {
    if (!channel.isOpen())
        throw new ClosedChannelException();
    // flush the current backing write buffer only if there's any pending data
    FrameEncoder.Payload flush = payload;
    int byteCount = flush.length();
    if (byteCount == 0)
        return;
    if (byteCount + flushed() > (closing ? messageSize : messageSize - 1))
        throw new InvalidSerializedSizeException(messageSize, byteCount + flushed());
    flush.finish();
    ChannelPromise promise = beginFlush(byteCount, lowWaterMark, highWaterMark);
    channel.writeAndFlush(flush, promise);
    allocateBuffer();
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelPromise(io.netty.channel.ChannelPromise)

Example 29 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project cassandra by apache.

the class AsyncStreamingOutputPlus method writeFileToChannelZeroCopyThrottled.

private long writeFileToChannelZeroCopyThrottled(FileChannel file, RateLimiter limiter, int batchSize, int lowWaterMark, int highWaterMark) throws IOException {
    final long length = file.size();
    long bytesTransferred = 0;
    final SharedFileChannel sharedFile = SharedDefaultFileRegion.share(file);
    try {
        int toWrite;
        while (bytesTransferred < length) {
            toWrite = (int) min(batchSize, length - bytesTransferred);
            limiter.acquire(toWrite);
            ChannelPromise promise = beginFlush(toWrite, lowWaterMark, highWaterMark);
            SharedDefaultFileRegion fileRegion = new SharedDefaultFileRegion(sharedFile, bytesTransferred, toWrite);
            channel.writeAndFlush(fileRegion, promise);
            if (logger.isTraceEnabled())
                logger.trace("Writing {} bytes at position {} of {}", toWrite, bytesTransferred, length);
            bytesTransferred += toWrite;
        }
        return bytesTransferred;
    } finally {
        sharedFile.release();
    }
}
Also used : SharedFileChannel(org.apache.cassandra.net.SharedDefaultFileRegion.SharedFileChannel) ChannelPromise(io.netty.channel.ChannelPromise)

Example 30 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project cassandra by apache.

the class EntireSSTableStreamConcurrentComponentMutationTest method createMockNettyChannel.

private EmbeddedChannel createMockNettyChannel(ByteBuf serializedFile) {
    WritableByteChannel wbc = new WritableByteChannel() {

        private boolean isOpen = true;

        public int write(ByteBuffer src) {
            int size = src.limit();
            serializedFile.writeBytes(src);
            return size;
        }

        public boolean isOpen() {
            return isOpen;
        }

        public void close() {
            isOpen = false;
        }
    };
    return new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            if (msg instanceof BufferPoolAllocator.Wrapped) {
                ByteBuffer buf = ((BufferPoolAllocator.Wrapped) msg).adopt();
                wbc.write(buf);
            } else {
                ((SharedDefaultFileRegion) msg).transferTo(wbc, 0);
            }
            super.write(ctx, msg, promise);
        }
    });
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) BufferPoolAllocator(org.apache.cassandra.net.BufferPoolAllocator) ByteBuffer(java.nio.ByteBuffer) RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) IOException(java.io.IOException)

Aggregations

ChannelPromise (io.netty.channel.ChannelPromise)223 Test (org.junit.jupiter.api.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)63 ChannelFuture (io.netty.channel.ChannelFuture)62 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)58 ByteBuf (io.netty.buffer.ByteBuf)56 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)30 Test (org.junit.Test)25 Channel (io.netty.channel.Channel)23 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)22 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 ClosedChannelException (java.nio.channels.ClosedChannelException)20 ChannelFutureListener (io.netty.channel.ChannelFutureListener)19 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)18 InvocationOnMock (org.mockito.invocation.InvocationOnMock)18 AsciiString (io.netty.util.AsciiString)15 IOException (java.io.IOException)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 Bootstrap (io.netty.bootstrap.Bootstrap)12