Search in sources :

Example 81 with Future

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project zeebe by camunda-cloud.

the class NettyMessagingService method bind.

/**
 * Recursively binds the given bootstrap to the given interfaces.
 *
 * @param bootstrap the bootstrap to bind
 * @param addressIterator an iterator of Addresses to which to bind
 * @param future the future to completed once the bootstrap has been bound to all provided
 *     interfaces
 */
private void bind(final ServerBootstrap bootstrap, final Iterator<Address> addressIterator, final CompletableFuture<Void> future) {
    if (addressIterator.hasNext()) {
        final Address address = addressIterator.next();
        bootstrap.bind(address.host(), address.port()).addListener((ChannelFutureListener) f -> {
            if (f.isSuccess()) {
                log.info("TCP server listening for connections on {}", address);
                serverChannel = f.channel();
                bind(bootstrap, addressIterator, future);
            } else {
                log.warn("Failed to bind TCP server to port {} due to {}", address, f.cause());
                future.completeExceptionally(f.cause());
            }
        });
    } else {
        future.complete(null);
    }
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) Address(io.atomix.utils.net.Address) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) MessagingConfig(io.atomix.cluster.messaging.MessagingConfig) InetAddress(java.net.InetAddress) Duration(java.time.Duration) Map(java.util.Map) SocketChannel(io.netty.channel.socket.SocketChannel) ChannelInitializer(io.netty.channel.ChannelInitializer) Collection(java.util.Collection) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Collectors(java.util.stream.Collectors) ServerChannel(io.netty.channel.ServerChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Executors(java.util.concurrent.Executors) List(java.util.List) Optional(java.util.Optional) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) ChannelOption(io.netty.channel.ChannelOption) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedMessagingService(io.atomix.cluster.messaging.ManagedMessagingService) OptionalInt(java.util.OptionalInt) Function(java.util.function.Function) ArrayList(java.util.ArrayList) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Threads.namedThreads(io.atomix.utils.concurrent.Threads.namedThreads) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ByteBuf(io.netty.buffer.ByteBuf) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) ChannelFutureListener(io.netty.channel.ChannelFutureListener) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BiConsumer(java.util.function.BiConsumer) StringUtil(io.camunda.zeebe.util.StringUtil) ConnectException(java.net.ConnectException) ZlibCodecFactory(io.netty.handler.codec.compression.ZlibCodecFactory) EventLoopGroup(io.netty.channel.EventLoopGroup) Logger(org.slf4j.Logger) SslContext(io.netty.handler.ssl.SslContext) Iterator(java.util.Iterator) Executor(java.util.concurrent.Executor) MessagingService(io.atomix.cluster.messaging.MessagingService) Throwables(com.google.common.base.Throwables) SnappyFrameEncoder(io.netty.handler.codec.compression.SnappyFrameEncoder) ZlibWrapper(io.netty.handler.codec.compression.ZlibWrapper) SnappyFrameDecoder(io.netty.handler.codec.compression.SnappyFrameDecoder) Maps(com.google.common.collect.Maps) Epoll(io.netty.channel.epoll.Epoll) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) AtomicLong(java.util.concurrent.atomic.AtomicLong) SslProvider(io.netty.handler.ssl.SslProvider) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) OrderedFuture(io.atomix.utils.concurrent.OrderedFuture) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) MessagingException(io.atomix.cluster.messaging.MessagingException) Future(io.netty.util.concurrent.Future) Address(io.atomix.utils.net.Address) InetAddress(java.net.InetAddress)

Example 82 with Future

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project zeebe by camunda-cloud.

the class NettyMessagingService method bootstrapClient.

/**
 * Bootstraps a new channel to the given address.
 *
 * @param address the address to which to connect
 * @return a future to be completed with the connected channel
 */
private CompletableFuture<Channel> bootstrapClient(final Address address) {
    final CompletableFuture<Channel> future = new OrderedFuture<>();
    final InetAddress resolvedAddress = address.address(true);
    if (resolvedAddress == null) {
        future.completeExceptionally(new IllegalStateException("Failed to bootstrap client (address " + address.toString() + " cannot be resolved)"));
        return future;
    }
    final Bootstrap bootstrap = new Bootstrap();
    bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    bootstrap.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(10 * 32 * 1024, 10 * 64 * 1024));
    bootstrap.option(ChannelOption.SO_RCVBUF, 1024 * 1024);
    bootstrap.option(ChannelOption.SO_SNDBUF, 1024 * 1024);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000);
    bootstrap.group(clientGroup);
    // TODO: Make this faster:
    // http://normanmaurer.me/presentations/2014-facebook-eng-netty/slides.html#37.0
    bootstrap.channel(clientChannelClass);
    bootstrap.remoteAddress(resolvedAddress, address.port());
    bootstrap.handler(new BasicClientChannelInitializer(future));
    final Channel channel = bootstrap.connect().addListener(onConnect -> {
        if (!onConnect.isSuccess()) {
            future.completeExceptionally(new ConnectException(String.format("Failed to connect channel for address %s", address)));
        }
    }).channel();
    // immediately ensure we're notified of the channel being closed. the common case is that the
    // channel is closed after we've handled the request (and response in the case of a
    // sendAndReceive operation), so the future is already completed by then. If it isn't, then the
    // channel was closed too early, which should be handled as a failure from the consumer point
    // of view.
    channel.closeFuture().addListener(onClose -> future.completeExceptionally(new ConnectException(String.format("Channel %s for address %s was closed unexpectedly before the request was handled", channel, address))));
    return future;
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) Address(io.atomix.utils.net.Address) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) MessagingConfig(io.atomix.cluster.messaging.MessagingConfig) InetAddress(java.net.InetAddress) Duration(java.time.Duration) Map(java.util.Map) SocketChannel(io.netty.channel.socket.SocketChannel) ChannelInitializer(io.netty.channel.ChannelInitializer) Collection(java.util.Collection) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Collectors(java.util.stream.Collectors) ServerChannel(io.netty.channel.ServerChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Executors(java.util.concurrent.Executors) List(java.util.List) Optional(java.util.Optional) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) ChannelOption(io.netty.channel.ChannelOption) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedMessagingService(io.atomix.cluster.messaging.ManagedMessagingService) OptionalInt(java.util.OptionalInt) Function(java.util.function.Function) ArrayList(java.util.ArrayList) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Threads.namedThreads(io.atomix.utils.concurrent.Threads.namedThreads) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ByteBuf(io.netty.buffer.ByteBuf) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) ChannelFutureListener(io.netty.channel.ChannelFutureListener) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BiConsumer(java.util.function.BiConsumer) StringUtil(io.camunda.zeebe.util.StringUtil) ConnectException(java.net.ConnectException) ZlibCodecFactory(io.netty.handler.codec.compression.ZlibCodecFactory) EventLoopGroup(io.netty.channel.EventLoopGroup) Logger(org.slf4j.Logger) SslContext(io.netty.handler.ssl.SslContext) Iterator(java.util.Iterator) Executor(java.util.concurrent.Executor) MessagingService(io.atomix.cluster.messaging.MessagingService) Throwables(com.google.common.base.Throwables) SnappyFrameEncoder(io.netty.handler.codec.compression.SnappyFrameEncoder) ZlibWrapper(io.netty.handler.codec.compression.ZlibWrapper) SnappyFrameDecoder(io.netty.handler.codec.compression.SnappyFrameDecoder) Maps(com.google.common.collect.Maps) Epoll(io.netty.channel.epoll.Epoll) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) AtomicLong(java.util.concurrent.atomic.AtomicLong) SslProvider(io.netty.handler.ssl.SslProvider) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) OrderedFuture(io.atomix.utils.concurrent.OrderedFuture) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) MessagingException(io.atomix.cluster.messaging.MessagingException) Future(io.netty.util.concurrent.Future) OrderedFuture(io.atomix.utils.concurrent.OrderedFuture) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerChannel(io.netty.channel.ServerChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) InetAddress(java.net.InetAddress) ConnectException(java.net.ConnectException)

Example 83 with Future

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project pegasus-java-client by XiaoMi.

the class ClusterManager method close.

@Override
public void close() {
    if (enableCounter) {
        MetricsManager.finish();
    }
    metaSession.closeSession();
    for (Map.Entry<rpc_address, ReplicaSession> entry : replicaSessions.entrySet()) {
        entry.getValue().closeSession();
    }
    Future metaGroupFuture = metaGroup.shutdownGracefully();
    Future replicaGroupFuture = replicaGroup.shutdownGracefully();
    Future tableGroupFuture = tableGroup.shutdownGracefully();
    Future timeoutTaskGroupFuture = timeoutTaskGroup.shutdownGracefully();
    try {
        metaGroupFuture.sync();
        logger.info("meta group has closed");
    } catch (Exception ex) {
        logger.warn("close meta group failed: ", ex);
    }
    try {
        replicaGroupFuture.sync();
        logger.info("replica group has closed");
    } catch (Exception ex) {
        logger.warn("close replica group failed: ", ex);
    }
    try {
        tableGroupFuture.sync();
        logger.info("table group has closed");
    } catch (Exception ex) {
        logger.warn("close table group failed: ", ex);
    }
    try {
        timeoutTaskGroupFuture.sync();
        logger.info("timeout task group has closed");
    } catch (Exception ex) {
        logger.warn("close timeout task group failed: ", ex);
    }
    logger.info("cluster manager has closed");
}
Also used : Future(io.netty.util.concurrent.Future) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) com.xiaomi.infra.pegasus.base.rpc_address(com.xiaomi.infra.pegasus.base.rpc_address) ReplicationException(com.xiaomi.infra.pegasus.rpc.ReplicationException)

Example 84 with Future

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project bazel-buildfarm by bazelbuild.

the class HttpBlobStore method acquireDownloadChannel.

@SuppressWarnings("FutureReturnValueIgnored")
private Future<Channel> acquireDownloadChannel() {
    Promise<Channel> channelReady = eventLoop.next().newPromise();
    channelPool.acquire().addListener((Future<Channel> channelAcquired) -> {
        if (!channelAcquired.isSuccess()) {
            channelReady.setFailure(channelAcquired.cause());
            return;
        }
        try {
            Channel ch = channelAcquired.getNow();
            ChannelPipeline p = ch.pipeline();
            if (!isChannelPipelineEmpty(p)) {
                channelReady.setFailure(new IllegalStateException("Channel pipeline is not empty."));
                return;
            }
            ch.pipeline().addFirst("read-timeout-handler", new ReadTimeoutHandler(timeoutMillis));
            p.addLast(new HttpClientCodec());
            synchronized (credentialsLock) {
                p.addLast(new HttpDownloadHandler(creds));
            }
            channelReady.setSuccess(ch);
        } catch (Throwable t) {
            channelReady.setFailure(t);
        }
    });
    return channelReady;
}
Also used : EpollDomainSocketChannel(io.netty.channel.epoll.EpollDomainSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) KQueueDomainSocketChannel(io.netty.channel.kqueue.KQueueDomainSocketChannel) SettableFuture(com.google.common.util.concurrent.SettableFuture) Utils.getFromFuture(build.buildfarm.proxy.http.Utils.getFromFuture) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 85 with Future

use of org.apache.flink.shaded.netty4.io.netty.util.concurrent.Future in project bazel-buildfarm by bazelbuild.

the class HttpBlobStore method getAfterCredentialRefresh.

@SuppressWarnings("FutureReturnValueIgnored")
private void getAfterCredentialRefresh(DownloadCommand cmd, SettableFuture<Boolean> outerF) {
    acquireDownloadChannel().addListener((Future<Channel> chP) -> {
        if (!chP.isSuccess()) {
            outerF.setException(chP.cause());
            return;
        }
        Channel ch = chP.getNow();
        ch.writeAndFlush(cmd).addListener((f) -> {
            try {
                if (f.isSuccess()) {
                    outerF.set(true);
                } else {
                    Throwable cause = f.cause();
                    if (cause instanceof HttpException) {
                        HttpResponse response = ((HttpException) cause).response();
                        if (cacheMiss(response.status())) {
                            outerF.set(false);
                            return;
                        }
                    }
                    outerF.setException(cause);
                }
            } finally {
                releaseDownloadChannel(ch);
            }
        });
    });
}
Also used : EpollDomainSocketChannel(io.netty.channel.epoll.EpollDomainSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) KQueueDomainSocketChannel(io.netty.channel.kqueue.KQueueDomainSocketChannel) SettableFuture(com.google.common.util.concurrent.SettableFuture) Utils.getFromFuture(build.buildfarm.proxy.http.Utils.getFromFuture) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future) HttpResponse(io.netty.handler.codec.http.HttpResponse)

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