Search in sources :

Example 31 with GenericFutureListener

use of io.netty.util.concurrent.GenericFutureListener in project cdap by cdapio.

the class ServiceSocksServerConnectHandler method createForwardingChannelHandler.

@Override
protected Future<RelayChannelHandler> createForwardingChannelHandler(Channel inboundChannel, String destAddress, int destPort) {
    Promise<RelayChannelHandler> promise = new DefaultPromise<>(inboundChannel.eventLoop());
    // Creates a bootstrap for connecting to the target service
    ChannelGroup channels = new DefaultChannelGroup(inboundChannel.eventLoop());
    Bootstrap bootstrap = new Bootstrap().group(inboundChannel.eventLoop()).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            channels.add(ctx.channel());
            // When the outbound connection is active, adds the relay channel handler for the current pipeline,
            // which is for relaying traffic coming back from outbound connection.
            // Also complete the relay channel handler future, which is for relaying traffic from inbound to outbound.
            ctx.pipeline().addLast(new SimpleRelayChannelHandler(inboundChannel));
            promise.setSuccess(new SimpleRelayChannelHandler(ctx.channel()));
        }
    });
    // Discover the target address
    Promise<Discoverable> discoverablePromise = new DefaultPromise<>(inboundChannel.eventLoop());
    Cancellable cancellable = discoveryServiceClient.discover(destAddress).watchChanges(serviceDiscovered -> {
        // If it is discovered, make a connection and complete the channel handler future
        Discoverable discoverable = new RandomEndpointStrategy(() -> serviceDiscovered).pick();
        if (discoverable != null) {
            discoverablePromise.setSuccess(discoverable);
        }
    }, inboundChannel.eventLoop());
    // When discovery completed successfully, connect to the destination
    discoverablePromise.addListener((GenericFutureListener<Future<Discoverable>>) discoverableFuture -> {
        cancellable.cancel();
        if (discoverableFuture.isSuccess()) {
            Discoverable discoverable = discoverableFuture.get();
            bootstrap.connect(discoverable.getSocketAddress()).addListener((ChannelFutureListener) channelFuture -> {
                if (!channelFuture.isSuccess()) {
                    promise.setFailure(channelFuture.cause());
                }
            });
        } else {
            promise.setFailure(discoverableFuture.cause());
        }
    });
    // On inbound channel close, close all outbound channels.
    // Also cancel the watch since it is no longer needed.
    // This is to handle case where discovery never return an endpoint before client connection timeout
    inboundChannel.closeFuture().addListener((ChannelFutureListener) future -> {
        cancellable.cancel();
        channels.close();
    });
    return promise;
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ChannelGroup(io.netty.channel.group.ChannelGroup) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) ChannelOption(io.netty.channel.ChannelOption) Promise(io.netty.util.concurrent.Promise) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DefaultPromise(io.netty.util.concurrent.DefaultPromise) Discoverable(org.apache.twill.discovery.Discoverable) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Cancellable(org.apache.twill.common.Cancellable) Future(io.netty.util.concurrent.Future) Discoverable(org.apache.twill.discovery.Discoverable) Cancellable(org.apache.twill.common.Cancellable) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelFutureListener(io.netty.channel.ChannelFutureListener) DefaultPromise(io.netty.util.concurrent.DefaultPromise) Bootstrap(io.netty.bootstrap.Bootstrap) Future(io.netty.util.concurrent.Future) ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy)

Example 32 with GenericFutureListener

use of io.netty.util.concurrent.GenericFutureListener in project riposte by Nike-Inc.

the class DTraceEndHandlerTest method endDtrace_completes_the_trace_using_ChannelFutureListener_if_state_is_not_null_and_isResponseSendingLastChunkSent_returns_true.

@Test
public void endDtrace_completes_the_trace_using_ChannelFutureListener_if_state_is_not_null_and_isResponseSendingLastChunkSent_returns_true() throws Exception {
    // given
    assertThat(state.isTraceCompletedOrScheduled(), is(false));
    assertThat(state.isResponseSendingLastChunkSent(), is(true));
    assertThat(state.getDistributedTraceStack(), nullValue());
    Pair<Deque<Span>, Map<String, String>> expectedDtraceInfo = setupStateWithNewSpan("blahTrace");
    assertThat(state.getDistributedTraceStack(), notNullValue());
    assertThat(state.getDistributedTraceStack(), is(expectedDtraceInfo.getLeft()));
    assertThat(state.getDistributedTraceStack().size(), is(1));
    assertThat(state.isTracingResponseTaggingAndFinalSpanNameCompleted(), is(false));
    Span expectedSpan = expectedDtraceInfo.getLeft().peek();
    // when
    handlerSpy.endDtrace(ctxMock);
    // then
    // completeCurrentSpan() not immediately called, but scheduled
    verify(handlerSpy, never()).completeCurrentSpan();
    assertThat(state.isTraceCompletedOrScheduled(), is(true));
    // Response tagging was done.
    assertThat(state.isTracingResponseTaggingAndFinalSpanNameCompleted(), is(true));
    // Extract the listener that was attached to the last chunk future.
    GenericFutureListener lastChunkListener = extractChannelFutureListenerAddedToLastChunkFuture();
    assertThat(lastChunkListener, notNullValue());
    assertThat(lastChunkListener, instanceOf(ChannelFutureListenerWithTracingAndMdc.class));
    assertThat(Whitebox.getInternalState(lastChunkListener, "distributedTraceStackForExecution"), is(expectedDtraceInfo.getLeft()));
    assertThat(Whitebox.getInternalState(lastChunkListener, "mdcContextMapForExecution"), is(expectedDtraceInfo.getRight()));
    Consumer<ChannelFuture> embeddedListenerConsumer = (Consumer<ChannelFuture>) Whitebox.getInternalState(lastChunkListener, "postCompleteOperation");
    // Execute the embedded listener so we can validate what it does. Note that we can't verify using mockito spy verify(),
    // because the method call goes through the internal handler, not the spy impl. But we can still verify by
    // setting up the Tracer state to what we expect, execute the embedded listener, and verify subsequent Tracer state.
    AsyncNettyHelper.linkTracingAndMdcToCurrentThread(expectedDtraceInfo);
    assertThat(Tracer.getInstance().getCurrentSpan(), is(expectedSpan));
    embeddedListenerConsumer.accept(null);
    assertThat(Tracer.getInstance().getCurrentSpan(), nullValue());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Consumer(java.util.function.Consumer) ChannelFutureListenerWithTracingAndMdc(com.nike.riposte.util.asynchelperwrapper.ChannelFutureListenerWithTracingAndMdc) Deque(java.util.Deque) Map(java.util.Map) Span(com.nike.wingtips.Span) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) Test(org.junit.Test)

Example 33 with GenericFutureListener

use of io.netty.util.concurrent.GenericFutureListener in project ambry by linkedin.

the class MultiplexedChannelRecord method acquireClaimedStream.

void acquireClaimedStream(Promise<Channel> promise) {
    NettyUtils.doInEventLoop(parentChannel.eventLoop(), () -> {
        if (state != RecordState.OPEN) {
            String message;
            // GOAWAY
            if (state == RecordState.CLOSED_TO_NEW) {
                message = String.format("Connection %s received GOAWAY with Last Stream ID %d. Unable to open new " + "streams on this connection.", parentChannel, lastStreamId);
            } else {
                message = String.format("Connection %s was closed while acquiring new stream.", parentChannel);
            }
            log.warn(message);
            promise.setFailure(new IOException(message));
            return;
        }
        Future<Http2StreamChannel> streamFuture = new Http2StreamChannelBootstrap(parentChannel).handler(streamChannelInitializer).open();
        streamFuture.addListener((GenericFutureListener<Future<Http2StreamChannel>>) future -> {
            NettyUtils.warnIfNotInEventLoop(parentChannel.eventLoop());
            if (!future.isSuccess()) {
                promise.setFailure(future.cause());
                return;
            }
            Http2StreamChannel channel = future.getNow();
            streamChannels.put(channel.id(), channel);
            promise.setSuccess(channel);
            if (closeIfIdleTask == null && allowedIdleTimeInMs != null && allowedIdleTimeInMs > 0) {
                enableCloseIfIdleTask();
            }
        });
    }, promise);
}
Also used : Logger(org.slf4j.Logger) ChannelInitializer(io.netty.channel.ChannelInitializer) Promise(io.netty.util.concurrent.Promise) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) ChannelId(io.netty.channel.ChannelId) LoggerFactory(org.slf4j.LoggerFactory) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) IOException(java.io.IOException) HashMap(java.util.HashMap) Http2GoAwayFrame(io.netty.handler.codec.http2.Http2GoAwayFrame) ArrayList(java.util.ArrayList) NettyUtils(com.github.ambry.commons.NettyUtils) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) Http2StreamChannelBootstrap(io.netty.handler.codec.http2.Http2StreamChannelBootstrap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ChannelOutboundInvoker(io.netty.channel.ChannelOutboundInvoker) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel) Future(io.netty.util.concurrent.Future) Http2StreamChannelBootstrap(io.netty.handler.codec.http2.Http2StreamChannelBootstrap) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) Future(io.netty.util.concurrent.Future) IOException(java.io.IOException) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel)

Example 34 with GenericFutureListener

use of io.netty.util.concurrent.GenericFutureListener in project flink by apache.

the class TaskManagerLogHandler method respondAsLeader.

/**
	 * Response when running with leading JobManager.
	 */
@Override
protected void respondAsLeader(final ChannelHandlerContext ctx, final Routed routed, final ActorGateway jobManager) {
    if (cache == null) {
        scala.concurrent.Future<Object> portFuture = jobManager.ask(JobManagerMessages.getRequestBlobManagerPort(), timeout);
        scala.concurrent.Future<BlobCache> cacheFuture = portFuture.map(new Mapper<Object, BlobCache>() {

            @Override
            public BlobCache checkedApply(Object result) throws IOException {
                Option<String> hostOption = jobManager.actor().path().address().host();
                String host = hostOption.isDefined() ? hostOption.get() : "localhost";
                int port = (int) result;
                return new BlobCache(new InetSocketAddress(host, port), config);
            }
        }, executor);
        cache = new FlinkFuture<>(cacheFuture);
    }
    final String taskManagerID = routed.pathParams().get(TaskManagersHandler.TASK_MANAGER_ID_KEY);
    final HttpRequest request = routed.request();
    //fetch TaskManager logs if no other process is currently doing it
    if (lastRequestPending.putIfAbsent(taskManagerID, true) == null) {
        try {
            InstanceID instanceID = new InstanceID(StringUtils.hexStringToByte(taskManagerID));
            scala.concurrent.Future<JobManagerMessages.TaskManagerInstance> scalaTaskManagerFuture = jobManager.ask(new JobManagerMessages.RequestTaskManagerInstance(instanceID), timeout).mapTo(ClassTag$.MODULE$.<JobManagerMessages.TaskManagerInstance>apply(JobManagerMessages.TaskManagerInstance.class));
            Future<JobManagerMessages.TaskManagerInstance> taskManagerFuture = new FlinkFuture<>(scalaTaskManagerFuture);
            Future<BlobKey> blobKeyFuture = taskManagerFuture.thenCompose(new ApplyFunction<JobManagerMessages.TaskManagerInstance, Future<BlobKey>>() {

                @Override
                public Future<BlobKey> apply(JobManagerMessages.TaskManagerInstance value) {
                    Instance taskManager = value.instance().get();
                    if (serveLogFile) {
                        return taskManager.getTaskManagerGateway().requestTaskManagerLog(timeTimeout);
                    } else {
                        return taskManager.getTaskManagerGateway().requestTaskManagerStdout(timeTimeout);
                    }
                }
            });
            Future<String> logPathFuture = blobKeyFuture.thenCombine(cache, new BiFunction<BlobKey, BlobCache, Tuple2<BlobKey, BlobCache>>() {

                @Override
                public Tuple2<BlobKey, BlobCache> apply(BlobKey blobKey, BlobCache blobCache) {
                    return Tuple2.of(blobKey, blobCache);
                }
            }).thenComposeAsync(new ApplyFunction<Tuple2<BlobKey, BlobCache>, Future<String>>() {

                @Override
                public Future<String> apply(Tuple2<BlobKey, BlobCache> value) {
                    final BlobKey blobKey = value.f0;
                    final BlobCache blobCache = value.f1;
                    //delete previous log file, if it is different than the current one
                    HashMap<String, BlobKey> lastSubmittedFile = serveLogFile ? lastSubmittedLog : lastSubmittedStdout;
                    if (lastSubmittedFile.containsKey(taskManagerID)) {
                        if (!blobKey.equals(lastSubmittedFile.get(taskManagerID))) {
                            try {
                                blobCache.deleteGlobal(lastSubmittedFile.get(taskManagerID));
                            } catch (IOException e) {
                                return FlinkCompletableFuture.completedExceptionally(new Exception("Could not delete file for " + taskManagerID + '.', e));
                            }
                            lastSubmittedFile.put(taskManagerID, blobKey);
                        }
                    } else {
                        lastSubmittedFile.put(taskManagerID, blobKey);
                    }
                    try {
                        return FlinkCompletableFuture.completed(blobCache.getURL(blobKey).getFile());
                    } catch (IOException e) {
                        return FlinkCompletableFuture.completedExceptionally(new Exception("Could not retrieve blob for " + blobKey + '.', e));
                    }
                }
            }, executor);
            logPathFuture.exceptionally(new ApplyFunction<Throwable, Void>() {

                @Override
                public Void apply(Throwable failure) {
                    display(ctx, request, "Fetching TaskManager log failed.");
                    LOG.error("Fetching TaskManager log failed.", failure);
                    lastRequestPending.remove(taskManagerID);
                    return null;
                }
            });
            logPathFuture.thenAccept(new AcceptFunction<String>() {

                @Override
                public void accept(String filePath) {
                    File file = new File(filePath);
                    final RandomAccessFile raf;
                    try {
                        raf = new RandomAccessFile(file, "r");
                    } catch (FileNotFoundException e) {
                        display(ctx, request, "Displaying TaskManager log failed.");
                        LOG.error("Displaying TaskManager log failed.", e);
                        return;
                    }
                    long fileLength;
                    try {
                        fileLength = raf.length();
                    } catch (IOException ioe) {
                        display(ctx, request, "Displaying TaskManager log failed.");
                        LOG.error("Displaying TaskManager log failed.", ioe);
                        try {
                            raf.close();
                        } catch (IOException e) {
                            LOG.error("Could not close random access file.", e);
                        }
                        return;
                    }
                    final FileChannel fc = raf.getChannel();
                    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
                    response.headers().set(CONTENT_TYPE, "text/plain");
                    if (HttpHeaders.isKeepAlive(request)) {
                        response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
                    }
                    HttpHeaders.setContentLength(response, fileLength);
                    // write the initial line and the header.
                    ctx.write(response);
                    // write the content.
                    ChannelFuture lastContentFuture;
                    final GenericFutureListener<io.netty.util.concurrent.Future<? super Void>> completionListener = new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {

                        @Override
                        public void operationComplete(io.netty.util.concurrent.Future<? super Void> future) throws Exception {
                            lastRequestPending.remove(taskManagerID);
                            fc.close();
                            raf.close();
                        }
                    };
                    if (ctx.pipeline().get(SslHandler.class) == null) {
                        ctx.write(new DefaultFileRegion(fc, 0, fileLength), ctx.newProgressivePromise()).addListener(completionListener);
                        lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
                    } else {
                        try {
                            lastContentFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)), ctx.newProgressivePromise()).addListener(completionListener);
                        } catch (IOException e) {
                            display(ctx, request, "Displaying TaskManager log failed.");
                            LOG.warn("Could not write http data.", e);
                            return;
                        }
                    // HttpChunkedInput will write the end marker (LastHttpContent) for us.
                    }
                    // close the connection, if no keep-alive is needed
                    if (!HttpHeaders.isKeepAlive(request)) {
                        lastContentFuture.addListener(ChannelFutureListener.CLOSE);
                    }
                }
            });
        } catch (Exception e) {
            display(ctx, request, "Error: " + e.getMessage());
            LOG.error("Fetching TaskManager log failed.", e);
            lastRequestPending.remove(taskManagerID);
        }
    } else {
        display(ctx, request, "loading...");
    }
}
Also used : InstanceID(org.apache.flink.runtime.instance.InstanceID) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) FileNotFoundException(java.io.FileNotFoundException) FlinkFuture(org.apache.flink.runtime.concurrent.impl.FlinkFuture) BlobKey(org.apache.flink.runtime.blob.BlobKey) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) ChannelFuture(io.netty.channel.ChannelFuture) ChunkedFile(io.netty.handler.stream.ChunkedFile) JobManagerMessages(org.apache.flink.runtime.messages.JobManagerMessages) DefaultFileRegion(io.netty.channel.DefaultFileRegion) RandomAccessFile(java.io.RandomAccessFile) Option(scala.Option) RandomAccessFile(java.io.RandomAccessFile) ChunkedFile(io.netty.handler.stream.ChunkedFile) File(java.io.File) Instance(org.apache.flink.runtime.instance.Instance) BlobCache(org.apache.flink.runtime.blob.BlobCache) HttpChunkedInput(io.netty.handler.codec.http.HttpChunkedInput) HttpRequest(io.netty.handler.codec.http.HttpRequest) FileChannel(java.nio.channels.FileChannel) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) BiFunction(org.apache.flink.runtime.concurrent.BiFunction) Tuple2(org.apache.flink.api.java.tuple.Tuple2) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) FlinkFuture(org.apache.flink.runtime.concurrent.impl.FlinkFuture) Future(org.apache.flink.runtime.concurrent.Future) FlinkCompletableFuture(org.apache.flink.runtime.concurrent.impl.FlinkCompletableFuture) ChannelFuture(io.netty.channel.ChannelFuture)

Example 35 with GenericFutureListener

use of io.netty.util.concurrent.GenericFutureListener in project MinecraftForge by MinecraftForge.

the class NetworkDispatcher method kickWithMessage.

private void kickWithMessage(String message) {
    FMLLog.log(Level.ERROR, "Network Disconnect: %s", message);
    final TextComponentString TextComponentString = new TextComponentString(message);
    if (side == Side.CLIENT) {
        manager.closeChannel(TextComponentString);
    } else {
        manager.sendPacket(new SPacketDisconnect(TextComponentString), new GenericFutureListener<Future<? super Void>>() {

            @Override
            public void operationComplete(Future<? super Void> result) {
                manager.closeChannel(TextComponentString);
            }
        }, (GenericFutureListener<? extends Future<? super Void>>[]) null);
    }
    manager.channel().config().setAutoRead(false);
}
Also used : Future(io.netty.util.concurrent.Future) SPacketDisconnect(net.minecraft.network.play.server.SPacketDisconnect) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) TextComponentString(net.minecraft.util.text.TextComponentString)

Aggregations

GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)70 Future (io.netty.util.concurrent.Future)44 ChannelFuture (io.netty.channel.ChannelFuture)32 Channel (io.netty.channel.Channel)19 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)18 IOException (java.io.IOException)18 List (java.util.List)15 InetSocketAddress (java.net.InetSocketAddress)13 ArrayList (java.util.ArrayList)13 Map (java.util.Map)12 ChannelOption (io.netty.channel.ChannelOption)10 Future (io.vertx.core.Future)10 Handler (io.vertx.core.Handler)10 ContextInternal (io.vertx.core.impl.ContextInternal)10 VertxInternal (io.vertx.core.impl.VertxInternal)10 Logger (org.slf4j.Logger)10 LoggerFactory (org.slf4j.LoggerFactory)9 Bootstrap (io.netty.bootstrap.Bootstrap)8 LoggingHandler (io.netty.handler.logging.LoggingHandler)8 AsyncResult (io.vertx.core.AsyncResult)8