Example usage for io.netty.channel ChannelFuture cause

List of usage examples for io.netty.channel ChannelFuture cause

Introduction

In this page you can find the example usage for io.netty.channel ChannelFuture cause.

Prototype

Throwable cause();

Source Link

Document

Returns the cause of the failed I/O operation if the I/O operation has failed.

Usage

From source file:org.apache.camel.component.netty4.NettyProducer.java

License:Apache License

protected Channel openChannel(ChannelFuture channelFuture) throws Exception {
    // blocking for channel to be done
    if (LOG.isTraceEnabled()) {
        LOG.trace("Waiting for operation to complete {} for {} millis", channelFuture,
                configuration.getConnectTimeout());
    }// ww  w . j ava 2s.  c o m
    // here we need to wait it in other thread
    final CountDownLatch channelLatch = new CountDownLatch(1);
    channelFuture.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture cf) throws Exception {
            channelLatch.countDown();
        }
    });

    try {
        channelLatch.await(configuration.getConnectTimeout(), TimeUnit.MILLISECONDS);
    } catch (InterruptedException ex) {
        throw new CamelException(
                "Interrupted while waiting for " + "connection to " + configuration.getAddress());
    }

    if (!channelFuture.isDone() || !channelFuture.isSuccess()) {
        ConnectException cause = new ConnectException("Cannot connect to " + configuration.getAddress());
        if (channelFuture.cause() != null) {
            cause.initCause(channelFuture.cause());
        }
        throw cause;
    }
    Channel answer = channelFuture.channel();
    // to keep track of all channels in use
    allChannels.add(answer);

    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating connector to address: {}", configuration.getAddress());
    }
    return answer;
}

From source file:org.apache.cassandra.transport.SimpleClient.java

License:Apache License

protected void establishConnection() throws IOException {
    // Configure the client.
    bootstrap = new Bootstrap().group(new NioEventLoopGroup())
            .channel(io.netty.channel.socket.nio.NioSocketChannel.class)
            .option(ChannelOption.TCP_NODELAY, true);

    // Configure the pipeline factory.
    if (encryptionOptions.enabled) {
        bootstrap.handler(new SecureInitializer());
    } else {//w w w .j  av a2 s  .  c  o m
        bootstrap.handler(new Initializer());
    }
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));

    // Wait until the connection attempt succeeds or fails.
    channel = future.awaitUninterruptibly().channel();
    if (!future.isSuccess()) {
        bootstrap.group().shutdownGracefully();
        throw new IOException("Connection Error", future.cause());
    }
}

From source file:org.apache.dubbo.remoting.transport.netty4.NettyChannel.java

License:Apache License

/**
 * Send message by netty and whether to wait the completion of the send.
 *
 * @param message message that need send.
 * @param sent whether to ack async-sent
 * @throws RemotingException throw RemotingException if wait until timeout or any exception thrown by method body that surrounded by try-catch.
 *//*from  www . ja v a  2s .  com*/
@Override
public void send(Object message, boolean sent) throws RemotingException {
    // whether the channel is closed
    super.send(message, sent);

    boolean success = true;
    int timeout = 0;
    try {
        ChannelFuture future = channel.writeAndFlush(message);
        if (sent) {
            // wait timeout ms
            timeout = getUrl().getPositiveParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);
            success = future.await(timeout);
        }
        Throwable cause = future.cause();
        if (cause != null) {
            throw cause;
        }
    } catch (Throwable e) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + ", cause: " + e.getMessage(), e);
    }
    if (!success) {
        throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
                + "in timeout(" + timeout + "ms) limit");
    }
}

From source file:org.apache.dubbo.remoting.transport.netty4.NettyClient.java

License:Apache License

@Override
protected void doConnect() throws Throwable {
    long start = System.currentTimeMillis();
    ChannelFuture future = bootstrap.connect(getConnectAddress());
    try {//from   w ww .  ja  v  a 2s.  co m
        boolean ret = future.awaitUninterruptibly(getConnectTimeout(), MILLISECONDS);

        if (ret && future.isSuccess()) {
            Channel newChannel = future.channel();
            try {
                // Close old channel
                // copy reference
                Channel oldChannel = NettyClient.this.channel;
                if (oldChannel != null) {
                    try {
                        if (logger.isInfoEnabled()) {
                            logger.info("Close old netty channel " + oldChannel
                                    + " on create new netty channel " + newChannel);
                        }
                        oldChannel.close();
                    } finally {
                        NettyChannel.removeChannelIfDisconnected(oldChannel);
                    }
                }
            } finally {
                if (NettyClient.this.isClosed()) {
                    try {
                        if (logger.isInfoEnabled()) {
                            logger.info(
                                    "Close new netty channel " + newChannel + ", because the client closed.");
                        }
                        newChannel.close();
                    } finally {
                        NettyClient.this.channel = null;
                        NettyChannel.removeChannelIfDisconnected(newChannel);
                    }
                } else {
                    NettyClient.this.channel = newChannel;
                }
            }
        } else if (future.cause() != null) {
            throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server "
                    + getRemoteAddress() + ", error message is:" + future.cause().getMessage(), future.cause());
        } else {
            throw new RemotingException(this,
                    "client(url: " + getUrl() + ") failed to connect to server " + getRemoteAddress()
                            + " client-side timeout " + getConnectTimeout() + "ms (elapsed: "
                            + (System.currentTimeMillis() - start) + "ms) from netty client "
                            + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion());
        }
    } finally {
        // just add new valid channel to NettyChannel's cache
        if (!isConnected()) {
            //future.cancel(true);
        }
    }
}

From source file:org.apache.flink.runtime.io.network.netty.PartitionRequestClient.java

License:Apache License

/**
 * Requests a remote intermediate result partition queue.
 * <p>/*from   w  ww .  ja  v  a 2  s .c  o  m*/
 * 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];
    }
}

From source file:org.apache.flink.runtime.io.network.netty.PartitionRequestClient.java

License:Apache License

/**
 * Sends a task event backwards to an intermediate result partition producer.
 * <p>//ww  w .  j  a v  a2  s .  c  o  m
 * 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()));
                    }
                }
            });
}

From source file:org.apache.giraph.comm.netty.NettyClient.java

License:Apache License

/**
 * Connect to a collection of tasks servers
 *
 * @param tasks Tasks to connect to (if haven't already connected)
 *///  w  w  w  .  ja  v a 2  s  .c  o  m
public void connectAllAddresses(Collection<? extends TaskInfo> tasks) {
    List<ChannelFutureAddress> waitingConnectionList = Lists
            .newArrayListWithCapacity(tasks.size() * channelsPerServer);
    for (TaskInfo taskInfo : tasks) {
        context.progress();
        InetSocketAddress address = taskIdAddressMap.get(taskInfo.getTaskId());
        if (address == null || !address.getHostName().equals(taskInfo.getHostname())
                || address.getPort() != taskInfo.getPort()) {
            address = resolveAddress(maxResolveAddressAttempts, taskInfo.getInetSocketAddress());
            taskIdAddressMap.put(taskInfo.getTaskId(), address);
        }
        if (address == null || address.getHostName() == null || address.getHostName().isEmpty()) {
            throw new IllegalStateException("connectAllAddresses: Null address " + "in addresses " + tasks);
        }
        if (address.isUnresolved()) {
            throw new IllegalStateException("connectAllAddresses: Unresolved " + "address " + address);
        }

        if (addressChannelMap.containsKey(address)) {
            continue;
        }

        // Start connecting to the remote server up to n time
        for (int i = 0; i < channelsPerServer; ++i) {
            ChannelFuture connectionFuture = bootstrap.connect(address);

            waitingConnectionList
                    .add(new ChannelFutureAddress(connectionFuture, address, taskInfo.getTaskId()));
        }
    }

    // Wait for all the connections to succeed up to n tries
    int failures = 0;
    int connected = 0;
    while (failures < maxConnectionFailures) {
        List<ChannelFutureAddress> nextCheckFutures = Lists.newArrayList();
        for (ChannelFutureAddress waitingConnection : waitingConnectionList) {
            context.progress();
            ChannelFuture future = waitingConnection.future;
            ProgressableUtils.awaitChannelFuture(future, context);
            if (!future.isSuccess()) {
                LOG.warn("connectAllAddresses: Future failed " + "to connect with " + waitingConnection.address
                        + " with " + failures + " failures because of " + future.cause());

                ChannelFuture connectionFuture = bootstrap.connect(waitingConnection.address);
                nextCheckFutures.add(new ChannelFutureAddress(connectionFuture, waitingConnection.address,
                        waitingConnection.taskId));
                ++failures;
            } else {
                Channel channel = future.channel();
                if (LOG.isDebugEnabled()) {
                    LOG.debug("connectAllAddresses: Connected to " + channel.remoteAddress() + ", open = "
                            + channel.isOpen());
                }

                if (channel.remoteAddress() == null) {
                    throw new IllegalStateException("connectAllAddresses: Null remote address!");
                }

                ChannelRotater rotater = addressChannelMap.get(waitingConnection.address);
                if (rotater == null) {
                    ChannelRotater newRotater = new ChannelRotater(waitingConnection.taskId);
                    rotater = addressChannelMap.putIfAbsent(waitingConnection.address, newRotater);
                    if (rotater == null) {
                        rotater = newRotater;
                    }
                }
                rotater.addChannel(future.channel());
                ++connected;
            }
        }
        LOG.info("connectAllAddresses: Successfully added "
                + (waitingConnectionList.size() - nextCheckFutures.size()) + " connections, (" + connected
                + " total connected) " + nextCheckFutures.size() + " failed, " + failures + " failures total.");
        if (nextCheckFutures.isEmpty()) {
            break;
        }
        waitingConnectionList = nextCheckFutures;
    }
    if (failures >= maxConnectionFailures) {
        throw new IllegalStateException("connectAllAddresses: Too many failures (" + failures + ").");
    }
}

From source file:org.apache.giraph.comm.netty.NettyClient.java

License:Apache License

/**
 * Get the next available channel, reconnecting if necessary
 *
 * @param remoteServer Remote server to get a channel for
 * @return Available channel for this remote server
 *//*from   w  w w . j  a  va  2  s.c  o m*/
private Channel getNextChannel(InetSocketAddress remoteServer) {
    Channel channel = addressChannelMap.get(remoteServer).nextChannel();
    if (channel == null) {
        throw new IllegalStateException("getNextChannel: No channel exists for " + remoteServer);
    }

    // Return this channel if it is connected
    if (channel.isActive()) {
        return channel;
    }

    // Get rid of the failed channel
    if (addressChannelMap.get(remoteServer).removeChannel(channel)) {
        LOG.warn("getNextChannel: Unlikely event that the channel " + channel + " was already removed!");
    }
    if (LOG.isInfoEnabled()) {
        LOG.info("getNextChannel: Fixing disconnected channel to " + remoteServer + ", open = "
                + channel.isOpen() + ", " + "bound = " + channel.isRegistered());
    }
    int reconnectFailures = 0;
    while (reconnectFailures < maxConnectionFailures) {
        ChannelFuture connectionFuture = bootstrap.connect(remoteServer);
        ProgressableUtils.awaitChannelFuture(connectionFuture, context);
        if (connectionFuture.isSuccess()) {
            if (LOG.isInfoEnabled()) {
                LOG.info("getNextChannel: Connected to " + remoteServer + "!");
            }
            addressChannelMap.get(remoteServer).addChannel(connectionFuture.channel());
            return connectionFuture.channel();
        }
        ++reconnectFailures;
        LOG.warn(
                "getNextChannel: Failed to reconnect to " + remoteServer + " on attempt " + reconnectFailures
                        + " out of " + maxConnectionFailures + " max attempts, sleeping for 5 secs",
                connectionFuture.cause());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            LOG.warn("getNextChannel: Unexpected interrupted exception", e);
        }
    }
    throw new IllegalStateException("getNextChannel: Failed to connect " + "to " + remoteServer + " in "
            + reconnectFailures + " connect attempts");
}

From source file:org.apache.giraph.comm.netty.NettyClient.java

License:Apache License

/**
 * Check if there are some open requests which have been sent a long time
 * ago, and if so resend them.//from   w  w w  . j av a  2  s.  com
 */
private void checkRequestsForProblems() {
    long lastTimeChecked = lastTimeCheckedRequestsForProblems.get();
    // If not enough time passed from the previous check, return
    if (System.currentTimeMillis() < lastTimeChecked + waitingRequestMsecs) {
        return;
    }
    // If another thread did the check already, return
    if (!lastTimeCheckedRequestsForProblems.compareAndSet(lastTimeChecked, System.currentTimeMillis())) {
        return;
    }
    List<ClientRequestId> addedRequestIds = Lists.newArrayList();
    List<RequestInfo> addedRequestInfos = Lists.newArrayList();
    // Check all the requests for problems
    for (Map.Entry<ClientRequestId, RequestInfo> entry : clientRequestIdRequestInfoMap.entrySet()) {
        RequestInfo requestInfo = entry.getValue();
        ChannelFuture writeFuture = requestInfo.getWriteFuture();
        // Request wasn't sent yet
        if (writeFuture == null) {
            continue;
        }
        // If not connected anymore, request failed, or the request is taking
        // too long, re-establish and resend
        if (!writeFuture.channel().isActive() || (writeFuture.isDone() && !writeFuture.isSuccess())
                || (requestInfo.getElapsedMsecs() > maxRequestMilliseconds)) {
            LOG.warn("checkRequestsForProblems: Problem with request id " + entry.getKey() + " connected = "
                    + writeFuture.channel().isActive() + ", future done = " + writeFuture.isDone() + ", "
                    + "success = " + writeFuture.isSuccess() + ", " + "cause = " + writeFuture.cause() + ", "
                    + "elapsed time = " + requestInfo.getElapsedMsecs() + ", " + "destination = "
                    + writeFuture.channel().remoteAddress() + " " + requestInfo);
            addedRequestIds.add(entry.getKey());
            addedRequestInfos
                    .add(new RequestInfo(requestInfo.getDestinationAddress(), requestInfo.getRequest()));
        }
    }

    // Add any new requests to the system, connect if necessary, and re-send
    for (int i = 0; i < addedRequestIds.size(); ++i) {
        ClientRequestId requestId = addedRequestIds.get(i);
        RequestInfo requestInfo = addedRequestInfos.get(i);

        if (clientRequestIdRequestInfoMap.put(requestId, requestInfo) == null) {
            LOG.warn("checkRequestsForProblems: Request " + requestId
                    + " completed prior to sending the next request");
            clientRequestIdRequestInfoMap.remove(requestId);
        }
        InetSocketAddress remoteServer = requestInfo.getDestinationAddress();
        Channel channel = getNextChannel(remoteServer);
        if (LOG.isInfoEnabled()) {
            LOG.info("checkRequestsForProblems: Re-issuing request " + requestInfo);
        }
        ChannelFuture writeFuture = channel.write(requestInfo.getRequest());
        requestInfo.setWriteFuture(writeFuture);
        writeFuture.addListener(logErrorListener);
    }
    addedRequestIds.clear();
    addedRequestInfos.clear();
}

From source file:org.apache.hadoop.hbase.io.asyncfs.FanOutOneBlockAsyncDFSOutputHelper.java

License:Apache License

private static List<Future<Channel>> connectToDataNodes(final Configuration conf, final DFSClient client,
        String clientName, final LocatedBlock locatedBlock, long maxBytesRcvd, long latestGS,
        BlockConstructionStage stage, DataChecksum summer, EventLoop eventLoop) {
    Enum<?>[] storageTypes = locatedBlock.getStorageTypes();
    DatanodeInfo[] datanodeInfos = locatedBlock.getLocations();
    boolean connectToDnViaHostname = conf.getBoolean(DFS_CLIENT_USE_DN_HOSTNAME,
            DFS_CLIENT_USE_DN_HOSTNAME_DEFAULT);
    final int timeoutMs = conf.getInt(DFS_CLIENT_SOCKET_TIMEOUT_KEY, READ_TIMEOUT);
    ExtendedBlock blockCopy = new ExtendedBlock(locatedBlock.getBlock());
    blockCopy.setNumBytes(locatedBlock.getBlockSize());
    ClientOperationHeaderProto header = ClientOperationHeaderProto.newBuilder()
            .setBaseHeader(BaseHeaderProto.newBuilder().setBlock(PB_HELPER.convert(blockCopy))
                    .setToken(PB_HELPER.convert(locatedBlock.getBlockToken())))
            .setClientName(clientName).build();
    ChecksumProto checksumProto = DataTransferProtoUtil.toProto(summer);
    final OpWriteBlockProto.Builder writeBlockProtoBuilder = OpWriteBlockProto.newBuilder().setHeader(header)
            .setStage(OpWriteBlockProto.BlockConstructionStage.valueOf(stage.name())).setPipelineSize(1)
            .setMinBytesRcvd(locatedBlock.getBlock().getNumBytes()).setMaxBytesRcvd(maxBytesRcvd)
            .setLatestGenerationStamp(latestGS).setRequestedChecksum(checksumProto)
            .setCachingStrategy(CachingStrategyProto.newBuilder().setDropBehind(true).build());
    List<Future<Channel>> futureList = new ArrayList<>(datanodeInfos.length);
    for (int i = 0; i < datanodeInfos.length; i++) {
        final DatanodeInfo dnInfo = datanodeInfos[i];
        // Use Enum here because StoregType is moved to another package in hadoop 2.6. Use StorageType
        // will cause compilation error for hadoop 2.5 or before.
        final Enum<?> storageType = storageTypes[i];
        final Promise<Channel> promise = eventLoop.newPromise();
        futureList.add(promise);/*from  www . j  a  va  2  s.c o  m*/
        String dnAddr = dnInfo.getXferAddr(connectToDnViaHostname);
        new Bootstrap().group(eventLoop).channel(NioSocketChannel.class)
                .option(CONNECT_TIMEOUT_MILLIS, timeoutMs).handler(new ChannelInitializer<Channel>() {

                    @Override
                    protected void initChannel(Channel ch) throws Exception {
                        // we need to get the remote address of the channel so we can only move on after
                        // channel connected. Leave an empty implementation here because netty does not allow
                        // a null handler.
                    }
                }).connect(NetUtils.createSocketAddr(dnAddr)).addListener(new ChannelFutureListener() {

                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        if (future.isSuccess()) {
                            initialize(conf, future.channel(), dnInfo, storageType, writeBlockProtoBuilder,
                                    timeoutMs, client, locatedBlock.getBlockToken(), promise);
                        } else {
                            promise.tryFailure(future.cause());
                        }
                    }
                });
    }
    return futureList;
}