Search in sources :

Example 1 with ChannelException

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelException in project pulsar by yahoo.

the class ConnectionPool method createConnection.

private CompletableFuture<ClientCnx> createConnection(InetSocketAddress address, int connectionKey) {
    if (log.isDebugEnabled()) {
        log.debug("Connection for {} not found in cache", address);
    }
    final CompletableFuture<ClientCnx> cnxFuture = new CompletableFuture<ClientCnx>();
    // Trigger async connect to broker
    bootstrap.connect(address).addListener((ChannelFuture future) -> {
        if (!future.isSuccess()) {
            cnxFuture.completeExceptionally(new PulsarClientException(future.cause()));
            cleanupConnection(address, connectionKey, cnxFuture);
            return;
        }
        log.info("[{}] Connected to server", future.channel());
        future.channel().closeFuture().addListener(v -> {
            if (log.isDebugEnabled()) {
                log.debug("Removing closed connection from pool: {}", v);
            }
            cleanupConnection(address, connectionKey, cnxFuture);
        });
        // We are connected to broker, but need to wait until the connect/connected handshake is
        // complete
        final ClientCnx cnx = (ClientCnx) future.channel().pipeline().get("handler");
        if (!future.channel().isActive() || cnx == null) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Connection was already closed by the time we got notified", future.channel());
            }
            cnxFuture.completeExceptionally(new ChannelException("Connection already closed"));
            return;
        }
        cnx.connectionFuture().thenRun(() -> {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Connection handshake completed", cnx.channel());
            }
            cnxFuture.complete(cnx);
        }).exceptionally(exception -> {
            log.warn("[{}] Connection handshake failed: {}", cnx.channel(), exception.getMessage());
            cnxFuture.completeExceptionally(exception);
            cleanupConnection(address, connectionKey, cnxFuture);
            cnx.ctx().close();
            return null;
        });
    });
    return cnxFuture;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) CompletableFuture(java.util.concurrent.CompletableFuture) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) ChannelException(io.netty.channel.ChannelException)

Example 2 with ChannelException

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelException in project incubator-pulsar by apache.

the class ConnectionPool method createConnection.

private CompletableFuture<ClientCnx> createConnection(InetSocketAddress logicalAddress, InetSocketAddress physicalAddress, int connectionKey) {
    if (log.isDebugEnabled()) {
        log.debug("Connection for {} not found in cache", logicalAddress);
    }
    final CompletableFuture<ClientCnx> cnxFuture = new CompletableFuture<ClientCnx>();
    // Trigger async connect to broker
    createConnection(physicalAddress).thenAccept(channel -> {
        log.info("[{}] Connected to server", channel);
        channel.closeFuture().addListener(v -> {
            // Remove connection from pool when it gets closed
            if (log.isDebugEnabled()) {
                log.debug("Removing closed connection from pool: {}", v);
            }
            cleanupConnection(logicalAddress, connectionKey, cnxFuture);
        });
        // We are connected to broker, but need to wait until the connect/connected handshake is
        // complete
        final ClientCnx cnx = (ClientCnx) channel.pipeline().get("handler");
        if (!channel.isActive() || cnx == null) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Connection was already closed by the time we got notified", channel);
            }
            cnxFuture.completeExceptionally(new ChannelException("Connection already closed"));
            return;
        }
        if (!logicalAddress.equals(physicalAddress)) {
            // We are connecting through a proxy. We need to set the target broker in the ClientCnx object so that
            // it can be specified when sending the CommandConnect.
            // That phase will happen in the ClientCnx.connectionActive() which will be invoked immediately after
            // this method.
            cnx.setTargetBroker(logicalAddress);
        }
        cnx.setRemoteHostName(physicalAddress.getHostName());
        cnx.connectionFuture().thenRun(() -> {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Connection handshake completed", cnx.channel());
            }
            cnxFuture.complete(cnx);
        }).exceptionally(exception -> {
            log.warn("[{}] Connection handshake failed: {}", cnx.channel(), exception.getMessage());
            cnxFuture.completeExceptionally(exception);
            cleanupConnection(logicalAddress, connectionKey, cnxFuture);
            cnx.ctx().close();
            return null;
        });
    }).exceptionally(exception -> {
        eventLoopGroup.execute(() -> {
            log.warn("Failed to open connection to {} : {}", physicalAddress, exception.getMessage());
            cleanupConnection(logicalAddress, connectionKey, cnxFuture);
            cnxFuture.completeExceptionally(new PulsarClientException(exception));
        });
        return null;
    });
    return cnxFuture;
}
Also used : X509Certificate(java.security.cert.X509Certificate) DnsNameResolverBuilder(io.netty.resolver.dns.DnsNameResolverBuilder) ChannelOption(io.netty.channel.ChannelOption) ByteBufPair(org.apache.pulsar.common.api.ByteBufPair) SecurityUtility(org.apache.pulsar.common.util.SecurityUtility) LoggerFactory(org.slf4j.LoggerFactory) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) ConcurrentMap(java.util.concurrent.ConcurrentMap) InetAddress(java.net.InetAddress) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) SocketChannel(io.netty.channel.socket.SocketChannel) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) EventLoopUtil(org.apache.pulsar.common.util.netty.EventLoopUtil) Logger(org.slf4j.Logger) EventLoopGroup(io.netty.channel.EventLoopGroup) DnsNameResolver(io.netty.resolver.dns.DnsNameResolver) Iterator(java.util.Iterator) ChannelInitializer(io.netty.channel.ChannelInitializer) SslContext(io.netty.handler.ssl.SslContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) InetSocketAddress(java.net.InetSocketAddress) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) List(java.util.List) ChannelException(io.netty.channel.ChannelException) ClientConfigurationData(org.apache.pulsar.client.impl.conf.ClientConfigurationData) Closeable(java.io.Closeable) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AuthenticationDataProvider(org.apache.pulsar.client.api.AuthenticationDataProvider) Future(io.netty.util.concurrent.Future) CompletableFuture(java.util.concurrent.CompletableFuture) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ChannelException(io.netty.channel.ChannelException)

Example 3 with ChannelException

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelException in project JaPS by JackWhite20.

the class ClusterPublisherChannelInitializer method initChannel.

@Override
protected void initChannel(Channel channel) throws Exception {
    try {
        channel.config().setOption(ChannelOption.IP_TOS, 0x18);
    } catch (ChannelException e) {
    // Not supported
    }
    channel.config().setAllocator(PooledByteBufAllocator.DEFAULT);
    channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
    channel.pipeline().addLast(new JSONObjectDecoder());
    channel.pipeline().addLast(new LengthFieldPrepender(4));
    channel.pipeline().addLast(new JSONObjectEncoder());
    channel.pipeline().addLast(clusterPublisher);
}
Also used : JSONObjectEncoder(de.jackwhite20.japs.shared.pipeline.handler.JSONObjectEncoder) JSONObjectDecoder(de.jackwhite20.japs.shared.pipeline.handler.JSONObjectDecoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ChannelException(io.netty.channel.ChannelException)

Example 4 with ChannelException

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

the class NettyClient method connect.

// ------------------------------------------------------------------------
// Client connections
// ------------------------------------------------------------------------
ChannelFuture connect(final InetSocketAddress serverSocketAddress) {
    checkState(bootstrap != null, "Client has not been initialized yet.");
    // --------------------------------------------------------------------
    // Child channel pipeline for accepted connections
    // --------------------------------------------------------------------
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel channel) throws Exception {
            // SSL handler should be added first in the pipeline
            if (clientSSLFactory != null) {
                SslHandler sslHandler = clientSSLFactory.createNettySSLHandler(channel.alloc(), serverSocketAddress.getAddress().getCanonicalHostName(), serverSocketAddress.getPort());
                channel.pipeline().addLast("ssl", sslHandler);
            }
            channel.pipeline().addLast(protocol.getClientChannelHandlers());
        }
    });
    try {
        return bootstrap.connect(serverSocketAddress);
    } catch (ChannelException e) {
        if ((e.getCause() instanceof java.net.SocketException && e.getCause().getMessage().equals("Too many open files")) || (e.getCause() instanceof ChannelException && e.getCause().getCause() instanceof java.net.SocketException && e.getCause().getCause().getMessage().equals("Too many open files"))) {
            throw new ChannelException("The operating system does not offer enough file handles to open the network connection. " + "Please increase the number of available file handles.", e.getCause());
        } else {
            throw e;
        }
    }
}
Also used : EpollSocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.epoll.EpollSocketChannel) NioSocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel) IOException(java.io.IOException) ChannelException(org.apache.flink.shaded.netty4.io.netty.channel.ChannelException) SslHandler(org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler) ChannelException(org.apache.flink.shaded.netty4.io.netty.channel.ChannelException)

Example 5 with ChannelException

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

the class LocalChannelRegistry method register.

static LocalAddress register(Channel channel, LocalAddress oldLocalAddress, SocketAddress localAddress) {
    if (oldLocalAddress != null) {
        throw new ChannelException("already bound");
    }
    if (!(localAddress instanceof LocalAddress)) {
        throw new ChannelException("unsupported address type: " + StringUtil.simpleClassName(localAddress));
    }
    LocalAddress addr = (LocalAddress) localAddress;
    if (LocalAddress.ANY.equals(addr)) {
        addr = new LocalAddress(channel);
    }
    Channel boundChannel = boundChannels.putIfAbsent(addr, channel);
    if (boundChannel != null) {
        throw new ChannelException("address already in use by: " + boundChannel);
    }
    return addr;
}
Also used : Channel(io.netty.channel.Channel) ChannelException(io.netty.channel.ChannelException)

Aggregations

ChannelException (io.netty.channel.ChannelException)23 IOException (java.io.IOException)10 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)4 ClosedChannelException (java.nio.channels.ClosedChannelException)4 JSONObjectDecoder (de.jackwhite20.japs.shared.pipeline.handler.JSONObjectDecoder)3 JSONObjectEncoder (de.jackwhite20.japs.shared.pipeline.handler.JSONObjectEncoder)3 Channel (io.netty.channel.Channel)3 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)3 RepeatedIfExceptionsTest (io.github.artsok.RepeatedIfExceptionsTest)2 ByteBuf (io.netty.buffer.ByteBuf)2 ChannelFuture (io.netty.channel.ChannelFuture)2 BindException (java.net.BindException)2 InetSocketAddress (java.net.InetSocketAddress)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Send (com.github.ambry.network.Send)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 SessionResumptionSslHandler (com.linkedin.r2.netty.handler.common.SessionResumptionSslHandler)1 Config (com.typesafe.config.Config)1 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)1 Connection (de.jackwhite20.japs.server.network.Connection)1