Search in sources :

Example 71 with Future

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project flink by splunk.

the class RestServerEndpoint method shutDownInternal.

/**
 * Stops this REST server endpoint.
 *
 * @return Future which is completed once the shut down has been finished.
 */
protected CompletableFuture<Void> shutDownInternal() {
    synchronized (lock) {
        CompletableFuture<?> channelFuture = new CompletableFuture<>();
        if (serverChannel != null) {
            serverChannel.close().addListener(finished -> {
                if (finished.isSuccess()) {
                    channelFuture.complete(null);
                } else {
                    channelFuture.completeExceptionally(finished.cause());
                }
            });
            serverChannel = null;
        }
        final CompletableFuture<Void> channelTerminationFuture = new CompletableFuture<>();
        channelFuture.thenRun(() -> {
            CompletableFuture<?> groupFuture = new CompletableFuture<>();
            CompletableFuture<?> childGroupFuture = new CompletableFuture<>();
            final Time gracePeriod = Time.seconds(10L);
            if (bootstrap != null) {
                final ServerBootstrapConfig config = bootstrap.config();
                final EventLoopGroup group = config.group();
                if (group != null) {
                    group.shutdownGracefully(0L, gracePeriod.toMilliseconds(), TimeUnit.MILLISECONDS).addListener(finished -> {
                        if (finished.isSuccess()) {
                            groupFuture.complete(null);
                        } else {
                            groupFuture.completeExceptionally(finished.cause());
                        }
                    });
                } else {
                    groupFuture.complete(null);
                }
                final EventLoopGroup childGroup = config.childGroup();
                if (childGroup != null) {
                    childGroup.shutdownGracefully(0L, gracePeriod.toMilliseconds(), TimeUnit.MILLISECONDS).addListener(finished -> {
                        if (finished.isSuccess()) {
                            childGroupFuture.complete(null);
                        } else {
                            childGroupFuture.completeExceptionally(finished.cause());
                        }
                    });
                } else {
                    childGroupFuture.complete(null);
                }
                bootstrap = null;
            } else {
                // complete the group futures since there is nothing to stop
                groupFuture.complete(null);
                childGroupFuture.complete(null);
            }
            CompletableFuture<Void> combinedFuture = FutureUtils.completeAll(Arrays.asList(groupFuture, childGroupFuture));
            combinedFuture.whenComplete((Void ignored, Throwable throwable) -> {
                if (throwable != null) {
                    channelTerminationFuture.completeExceptionally(throwable);
                } else {
                    channelTerminationFuture.complete(null);
                }
            });
        });
        return channelTerminationFuture;
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) NioEventLoopGroup(org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup) Time(org.apache.flink.api.common.time.Time) ServerBootstrapConfig(org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrapConfig)

Example 72 with Future

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project flink by splunk.

the class RestClient method submitRequest.

private <P extends ResponseBody> CompletableFuture<P> submitRequest(String targetAddress, int targetPort, Request httpRequest, JavaType responseType) {
    final ChannelFuture connectFuture = bootstrap.connect(targetAddress, targetPort);
    final CompletableFuture<Channel> channelFuture = new CompletableFuture<>();
    connectFuture.addListener((ChannelFuture future) -> {
        if (future.isSuccess()) {
            channelFuture.complete(future.channel());
        } else {
            channelFuture.completeExceptionally(future.cause());
        }
    });
    return channelFuture.thenComposeAsync(channel -> {
        ClientHandler handler = channel.pipeline().get(ClientHandler.class);
        CompletableFuture<JsonResponse> future;
        boolean success = false;
        try {
            if (handler == null) {
                throw new IOException("Netty pipeline was not properly initialized.");
            } else {
                httpRequest.writeTo(channel);
                future = handler.getJsonFuture();
                success = true;
            }
        } catch (IOException e) {
            future = FutureUtils.completedExceptionally(new ConnectionException("Could not write request.", e));
        } finally {
            if (!success) {
                channel.close();
            }
        }
        return future;
    }, executor).thenComposeAsync((JsonResponse rawResponse) -> parseResponse(rawResponse, responseType), executor);
}
Also used : ChannelFuture(org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture) SimpleChannelInboundHandler(org.apache.flink.shaded.netty4.io.netty.channel.SimpleChannelInboundHandler) MessageParameters(org.apache.flink.runtime.rest.messages.MessageParameters) FullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse) ChunkedWriteHandler(org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler) OutboundChannelHandlerFactory(org.apache.flink.runtime.io.network.netty.OutboundChannelHandlerFactory) MemoryAttribute(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.MemoryAttribute) LoggerFactory(org.slf4j.LoggerFactory) ExceptionUtils(org.apache.flink.util.ExceptionUtils) JsonParser(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) NetUtils(org.apache.flink.util.NetUtils) EmptyMessageParameters(org.apache.flink.runtime.rest.messages.EmptyMessageParameters) EmptyRequestBody(org.apache.flink.runtime.rest.messages.EmptyRequestBody) HttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse) RequestBody(org.apache.flink.runtime.rest.messages.RequestBody) HttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest) RestClientException(org.apache.flink.runtime.rest.util.RestClientException) Path(java.nio.file.Path) ChannelHandlerContext(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext) DefaultFullHttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest) Attribute(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.Attribute) ExecutorThreadFactory(org.apache.flink.util.concurrent.ExecutorThreadFactory) RestConstants(org.apache.flink.runtime.rest.util.RestConstants) Collection(java.util.Collection) NioEventLoopGroup(org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup) HttpVersion(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpVersion) HttpObjectAggregator(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpObjectAggregator) IdleStateHandler(org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler) ServiceLoader(java.util.ServiceLoader) JavaType(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType) Preconditions(org.apache.flink.util.Preconditions) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) Collectors(java.util.stream.Collectors) List(java.util.List) SocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel) HttpClientCodec(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpClientCodec) DefaultHttpDataFactory(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) Optional(java.util.Optional) IdleStateEvent(org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateEvent) Time(org.apache.flink.api.common.time.Time) RestMapperUtils(org.apache.flink.runtime.rest.util.RestMapperUtils) Bootstrap(org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap) ChannelInitializer(org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer) ConfigurationException(org.apache.flink.util.ConfigurationException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SSLHandlerFactory(org.apache.flink.runtime.io.network.netty.SSLHandlerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) ChannelHandler(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler) MessageHeaders(org.apache.flink.runtime.rest.messages.MessageHeaders) HttpResponseStatus(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus) ArrayList(java.util.ArrayList) FutureUtils(org.apache.flink.util.concurrent.FutureUtils) ByteBufInputStream(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream) HttpHeaders(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders) REQUEST_ENTITY_TOO_LARGE(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE) ObjectMapper(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper) ConfigConstants(org.apache.flink.configuration.ConfigConstants) RestOptions(org.apache.flink.configuration.RestOptions) RestAPIVersion(org.apache.flink.runtime.rest.versioning.RestAPIVersion) ChannelFuture(org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture) ErrorResponseBody(org.apache.flink.runtime.rest.messages.ErrorResponseBody) ChannelOption(org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) AutoCloseableAsync(org.apache.flink.util.AutoCloseableAsync) Files(java.nio.file.Files) Executor(java.util.concurrent.Executor) StringWriter(java.io.StringWriter) Configuration(org.apache.flink.configuration.Configuration) Unpooled(org.apache.flink.shaded.netty4.io.netty.buffer.Unpooled) IOException(java.io.IOException) JsonProcessingException(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonProcessingException) File(java.io.File) VisibleForTesting(org.apache.flink.annotation.VisibleForTesting) TimeUnit(java.util.concurrent.TimeUnit) NioSocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel) ResponseBody(org.apache.flink.runtime.rest.messages.ResponseBody) TooLongFrameException(org.apache.flink.shaded.netty4.io.netty.handler.codec.TooLongFrameException) HttpPostRequestEncoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) HttpMethod(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod) Comparator(java.util.Comparator) Collections(java.util.Collections) Channel(org.apache.flink.shaded.netty4.io.netty.channel.Channel) InputStream(java.io.InputStream) CompletableFuture(java.util.concurrent.CompletableFuture) SocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel) NioSocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel) Channel(org.apache.flink.shaded.netty4.io.netty.channel.Channel) IOException(java.io.IOException)

Example 73 with Future

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project aws-sdk-java-v2 by aws.

the class HealthCheckedChannelPoolTest method stubAcquireTwiceFirstTimeNotKeepAlive.

private void stubAcquireTwiceFirstTimeNotKeepAlive() {
    OngoingStubbing<Future<Channel>> stubbing = Mockito.when(downstreamChannelPool.acquire(any()));
    stubbing = stubbing.thenAnswer(invocation -> {
        Promise<Channel> promise = invocation.getArgument(0, Promise.class);
        Channel channel = Mockito.mock(Channel.class);
        stubKeepAliveAttribute(channel, false);
        Mockito.when(channel.isActive()).thenReturn(true);
        channels.add(channel);
        promise.setSuccess(channel);
        return promise;
    });
    stubbing.thenAnswer(invocation -> {
        Promise<Channel> promise = invocation.getArgument(0, Promise.class);
        Channel channel = Mockito.mock(Channel.class);
        Mockito.when(channel.isActive()).thenReturn(true);
        channels.add(channel);
        promise.setSuccess(channel);
        stubKeepAliveAttribute(channel, true);
        return promise;
    });
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) CONNECTION_ACQUIRE_TIMEOUT(software.amazon.awssdk.http.SdkHttpConfigurationOption.CONNECTION_ACQUIRE_TIMEOUT) TimeoutException(java.util.concurrent.TimeoutException) AttributeMap(software.amazon.awssdk.utils.AttributeMap) ArrayList(java.util.ArrayList) GlobalEventExecutor(io.netty.util.concurrent.GlobalEventExecutor) Answer(org.mockito.stubbing.Answer) ArgumentCaptor(org.mockito.ArgumentCaptor) KEEP_ALIVE(software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.KEEP_ALIVE) Duration(java.time.Duration) Attribute(io.netty.util.Attribute) EventLoopGroup(io.netty.channel.EventLoopGroup) Promise(io.netty.util.concurrent.Promise) OngoingStubbing(org.mockito.stubbing.OngoingStubbing) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) EventLoop(io.netty.channel.EventLoop) Channel(io.netty.channel.Channel) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) DefaultPromise(io.netty.util.concurrent.DefaultPromise) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) List(java.util.List) VerificationModeFactory.times(org.mockito.internal.verification.VerificationModeFactory.times) Future(io.netty.util.concurrent.Future) Mockito.mock(org.mockito.Mockito.mock) Promise(io.netty.util.concurrent.Promise) DefaultPromise(io.netty.util.concurrent.DefaultPromise) Channel(io.netty.channel.Channel) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) Future(io.netty.util.concurrent.Future)

Example 74 with Future

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project aws-sdk-java-v2 by aws.

the class Http2MultiplexedChannelPool method acquireStreamOnNewConnection.

private void acquireStreamOnNewConnection(Promise<Channel> promise) {
    Future<Channel> newConnectionAcquire = connectionPool.acquire();
    newConnectionAcquire.addListener(f -> {
        if (!newConnectionAcquire.isSuccess()) {
            promise.setFailure(newConnectionAcquire.cause());
            return;
        }
        Channel parentChannel = newConnectionAcquire.getNow();
        try {
            parentChannel.attr(HTTP2_MULTIPLEXED_CHANNEL_POOL).set(this);
            // When the protocol future is completed on the new connection, we're ready for new streams to be added to it.
            parentChannel.attr(PROTOCOL_FUTURE).get().thenAccept(protocol -> acquireStreamOnFreshConnection(promise, parentChannel, protocol)).exceptionally(throwable -> failAndCloseParent(promise, parentChannel, throwable));
        } catch (Throwable e) {
            failAndCloseParent(promise, parentChannel, e);
        }
    });
}
Also used : AttributeKey(io.netty.util.AttributeKey) HTTP2_MULTIPLEXED_CHANNEL_POOL(software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.HTTP2_MULTIPLEXED_CHANNEL_POOL) Protocol(software.amazon.awssdk.http.Protocol) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) BetterFixedChannelPool(software.amazon.awssdk.http.nio.netty.internal.utils.BetterFixedChannelPool) NettyUtils.doInEventLoop(software.amazon.awssdk.http.nio.netty.internal.utils.NettyUtils.doInEventLoop) MAX_CONCURRENT_STREAMS(software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.MAX_CONCURRENT_STREAMS) Sharable(io.netty.channel.ChannelHandler.Sharable) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HTTP2_INITIAL_WINDOW_SIZE(software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.HTTP2_INITIAL_WINDOW_SIZE) Http2StreamChannelBootstrap(io.netty.handler.codec.http2.Http2StreamChannelBootstrap) Http2Exception(io.netty.handler.codec.http2.Http2Exception) Duration(java.time.Duration) Http2Stream(io.netty.handler.codec.http2.Http2Stream) SdkTestInternalApi(software.amazon.awssdk.annotations.SdkTestInternalApi) SdkChannelPool(software.amazon.awssdk.http.nio.netty.internal.SdkChannelPool) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) Validate(software.amazon.awssdk.utils.Validate) EventLoopGroup(io.netty.channel.EventLoopGroup) Promise(io.netty.util.concurrent.Promise) MetricCollector(software.amazon.awssdk.metrics.MetricCollector) ClosedChannelException(java.nio.channels.ClosedChannelException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HttpMetric(software.amazon.awssdk.http.HttpMetric) NettyUtils(software.amazon.awssdk.http.nio.netty.internal.utils.NettyUtils) Set(java.util.Set) IOException(java.io.IOException) PromiseCombiner(io.netty.util.concurrent.PromiseCombiner) EventLoop(io.netty.channel.EventLoop) HTTP2_CONNECTION(software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.HTTP2_CONNECTION) NettyClientLogger(software.amazon.awssdk.http.nio.netty.internal.utils.NettyClientLogger) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) PROTOCOL_FUTURE(software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.PROTOCOL_FUTURE) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Http2Connection(io.netty.handler.codec.http2.Http2Connection) ChannelPool(io.netty.channel.pool.ChannelPool) Http2LocalFlowController(io.netty.handler.codec.http2.Http2LocalFlowController) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) Future(io.netty.util.concurrent.Future) Channel(io.netty.channel.Channel)

Example 75 with Future

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project aws-sdk-java-v2 by aws.

the class CancellableAcquireChannelPool method acquire.

@Override
public Future<Channel> acquire(Promise<Channel> acquirePromise) {
    Future<Channel> channelFuture = delegatePool.acquire(executor.newPromise());
    channelFuture.addListener((Future<Channel> f) -> {
        if (f.isSuccess()) {
            Channel ch = f.getNow();
            if (!acquirePromise.trySuccess(ch)) {
                ch.close().addListener(closeFuture -> delegatePool.release(ch));
            }
        } else {
            acquirePromise.tryFailure(f.cause());
        }
    });
    return acquirePromise;
}
Also used : Channel(io.netty.channel.Channel) CompletableFuture(java.util.concurrent.CompletableFuture) Future(io.netty.util.concurrent.Future)

Aggregations

Future (io.netty.util.concurrent.Future)177 Channel (io.netty.channel.Channel)61 ChannelFuture (io.netty.channel.ChannelFuture)58 InetSocketAddress (java.net.InetSocketAddress)45 ArrayList (java.util.ArrayList)45 IOException (java.io.IOException)44 GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)42 CompletableFuture (java.util.concurrent.CompletableFuture)40 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)35 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)34 List (java.util.List)34 ChannelFutureListener (io.netty.channel.ChannelFutureListener)31 EventLoopGroup (io.netty.channel.EventLoopGroup)30 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)30 FutureListener (io.netty.util.concurrent.FutureListener)28 Logger (org.slf4j.Logger)28 LoggerFactory (org.slf4j.LoggerFactory)28 TimeUnit (java.util.concurrent.TimeUnit)27 Bootstrap (io.netty.bootstrap.Bootstrap)25 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)25