Search in sources :

Example 41 with ChannelFuture

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

the class PartitionRequestClient method requestSubpartition.

/**
	 * Requests a remote intermediate result partition queue.
	 * <p>
	 * The request goes to the remote producer, for which this partition
	 * request client instance has been created.
	 */
public ChannelFuture requestSubpartition(final ResultPartitionID partitionId, final int subpartitionIndex, final RemoteInputChannel inputChannel, int delayMs) throws IOException {
    checkNotClosed();
    LOG.debug("Requesting subpartition {} of partition {} with {} ms delay.", subpartitionIndex, partitionId, delayMs);
    partitionRequestHandler.addInputChannel(inputChannel);
    final PartitionRequest request = new PartitionRequest(partitionId, subpartitionIndex, inputChannel.getInputChannelId());
    final ChannelFutureListener listener = new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
                partitionRequestHandler.removeInputChannel(inputChannel);
                inputChannel.onError(new LocalTransportException("Sending the partition request failed.", future.channel().localAddress(), future.cause()));
            }
        }
    };
    if (delayMs == 0) {
        ChannelFuture f = tcpChannel.writeAndFlush(request);
        f.addListener(listener);
        return f;
    } else {
        final ChannelFuture[] f = new ChannelFuture[1];
        tcpChannel.eventLoop().schedule(new Runnable() {

            @Override
            public void run() {
                f[0] = tcpChannel.writeAndFlush(request);
                f[0].addListener(listener);
            }
        }, delayMs, TimeUnit.MILLISECONDS);
        return f[0];
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) PartitionRequest(org.apache.flink.runtime.io.network.netty.NettyMessage.PartitionRequest) LocalTransportException(org.apache.flink.runtime.io.network.netty.exception.LocalTransportException) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 42 with ChannelFuture

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

the class PartitionRequestClient method sendTaskEvent.

/**
	 * Sends a task event backwards to an intermediate result partition producer.
	 * <p>
	 * Backwards task events flow between readers and writers and therefore
	 * will only work when both are running at the same time, which is only
	 * guaranteed to be the case when both the respective producer and
	 * consumer task run pipelined.
	 */
public void sendTaskEvent(ResultPartitionID partitionId, TaskEvent event, final RemoteInputChannel inputChannel) throws IOException {
    checkNotClosed();
    tcpChannel.writeAndFlush(new TaskEventRequest(event, partitionId, inputChannel.getInputChannelId())).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
                inputChannel.onError(new LocalTransportException("Sending the task event failed.", future.channel().localAddress(), future.cause()));
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) TaskEventRequest(org.apache.flink.runtime.io.network.netty.NettyMessage.TaskEventRequest) LocalTransportException(org.apache.flink.runtime.io.network.netty.exception.LocalTransportException) ChannelFutureListener(io.netty.channel.ChannelFutureListener) IOException(java.io.IOException) LocalTransportException(org.apache.flink.runtime.io.network.netty.exception.LocalTransportException)

Example 43 with ChannelFuture

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture in project canal by alibaba.

the class SocketChannelPool method open.

public static SocketChannel open(SocketAddress address) throws Exception {
    final SocketChannel socket = new SocketChannel();
    boot.connect(address).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture arg0) throws Exception {
            if (arg0.isSuccess()) {
                socket.setChannel(arg0.channel(), false);
            }
            synchronized (socket) {
                socket.notify();
            }
        }
    });
    synchronized (socket) {
        socket.wait();
    }
    if (null == socket.getChannel()) {
        throw new IOException("can't create socket!");
    }
    chManager.put(socket.getChannel(), socket);
    return socket;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) IOException(java.io.IOException) ChannelFutureListener(io.netty.channel.ChannelFutureListener) IOException(java.io.IOException)

Example 44 with ChannelFuture

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

the class WebSocketServerExtensionHandler method write.

@Override
public void write(final ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (msg instanceof HttpResponse && WebSocketExtensionUtil.isWebsocketUpgrade(((HttpResponse) msg).headers()) && validExtensions != null) {
        HttpResponse response = (HttpResponse) msg;
        String headerValue = response.headers().getAsString(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS);
        for (WebSocketServerExtension extension : validExtensions) {
            WebSocketExtensionData extensionData = extension.newReponseData();
            headerValue = WebSocketExtensionUtil.appendExtension(headerValue, extensionData.name(), extensionData.parameters());
        }
        promise.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    for (WebSocketServerExtension extension : validExtensions) {
                        WebSocketExtensionDecoder decoder = extension.newExtensionDecoder();
                        WebSocketExtensionEncoder encoder = extension.newExtensionEncoder();
                        ctx.pipeline().addAfter(ctx.name(), decoder.getClass().getName(), decoder);
                        ctx.pipeline().addAfter(ctx.name(), encoder.getClass().getName(), encoder);
                    }
                }
                ctx.pipeline().remove(ctx.name());
            }
        });
        if (headerValue != null) {
            response.headers().set(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS, headerValue);
        }
    }
    super.write(ctx, msg, promise);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HttpResponse(io.netty.handler.codec.http.HttpResponse) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 45 with ChannelFuture

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

the class LocalChannelTest method testClosePeerInWritePromiseCompleteSameEventLoopPreservesOrder.

@Test
public void testClosePeerInWritePromiseCompleteSameEventLoopPreservesOrder() throws InterruptedException {
    Bootstrap cb = new Bootstrap();
    ServerBootstrap sb = new ServerBootstrap();
    final CountDownLatch messageLatch = new CountDownLatch(2);
    final CountDownLatch serverChannelLatch = new CountDownLatch(1);
    final ByteBuf data = Unpooled.wrappedBuffer(new byte[1024]);
    final AtomicReference<Channel> serverChannelRef = new AtomicReference<Channel>();
    try {
        cb.group(sharedGroup).channel(LocalChannel.class).handler(new TestHandler());
        sb.group(sharedGroup).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

            @Override
            public void initChannel(LocalChannel ch) throws Exception {
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                        if (msg.equals(data)) {
                            ReferenceCountUtil.safeRelease(msg);
                            messageLatch.countDown();
                        } else {
                            super.channelRead(ctx, msg);
                        }
                    }

                    @Override
                    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                        messageLatch.countDown();
                        super.channelInactive(ctx);
                    }
                });
                serverChannelRef.set(ch);
                serverChannelLatch.countDown();
            }
        });
        Channel sc = null;
        Channel cc = null;
        try {
            // Start server
            sc = sb.bind(TEST_ADDRESS).syncUninterruptibly().channel();
            // Connect to the server
            cc = cb.connect(sc.localAddress()).syncUninterruptibly().channel();
            assertTrue(serverChannelLatch.await(5, SECONDS));
            final Channel ccCpy = cc;
            // Make sure a write operation is executed in the eventloop
            cc.pipeline().lastContext().executor().execute(new Runnable() {

                @Override
                public void run() {
                    ChannelPromise promise = ccCpy.newPromise();
                    promise.addListener(new ChannelFutureListener() {

                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            serverChannelRef.get().close();
                        }
                    });
                    ccCpy.writeAndFlush(data.retainedDuplicate(), promise);
                }
            });
            assertTrue(messageLatch.await(5, SECONDS));
            assertFalse(cc.isOpen());
            assertFalse(serverChannelRef.get().isOpen());
        } finally {
            closeChannel(cc);
            closeChannel(sc);
        }
    } finally {
        data.release();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AbstractChannel(io.netty.channel.AbstractChannel) Channel(io.netty.channel.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ConnectException(java.net.ConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Aggregations

ChannelFuture (io.netty.channel.ChannelFuture)936 Channel (io.netty.channel.Channel)271 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)246 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)239 Bootstrap (io.netty.bootstrap.Bootstrap)219 InetSocketAddress (java.net.InetSocketAddress)214 ChannelFutureListener (io.netty.channel.ChannelFutureListener)206 Test (org.junit.Test)195 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)194 ByteBuf (io.netty.buffer.ByteBuf)192 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)178 EventLoopGroup (io.netty.channel.EventLoopGroup)167 IOException (java.io.IOException)155 ChannelPipeline (io.netty.channel.ChannelPipeline)151 SocketChannel (io.netty.channel.socket.SocketChannel)118 ArrayList (java.util.ArrayList)118 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)116 ChannelInitializer (io.netty.channel.ChannelInitializer)115 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)112 AtomicReference (java.util.concurrent.atomic.AtomicReference)111