Example usage for io.netty.util.concurrent GenericFutureListener GenericFutureListener

List of usage examples for io.netty.util.concurrent GenericFutureListener GenericFutureListener

Introduction

In this page you can find the example usage for io.netty.util.concurrent GenericFutureListener GenericFutureListener.

Prototype

GenericFutureListener

Source Link

Usage

From source file:org.apache.bookkeeper.proto.PerChannelBookieClient.java

License:Apache License

void initTLSHandshake() {
    // create TLS handler
    PerChannelBookieClient parentObj = PerChannelBookieClient.this;
    SslHandler handler = parentObj.shFactory.newTLSHandler();
    channel.pipeline().addFirst(parentObj.shFactory.getHandlerName(), handler);
    handler.handshakeFuture().addListener(new GenericFutureListener<Future<Channel>>() {
        @Override//from   w  w  w .j av  a2s.  c om
        public void operationComplete(Future<Channel> future) throws Exception {
            int rc;
            Queue<GenericCallback<PerChannelBookieClient>> oldPendingOps;

            synchronized (PerChannelBookieClient.this) {
                if (future.isSuccess() && state == ConnectionState.CONNECTING) {
                    LOG.error("Connection state changed before TLS handshake completed {}/{}", addr, state);
                    rc = BKException.Code.BookieHandleNotAvailableException;
                    closeChannel(channel);
                    channel = null;
                    if (state != ConnectionState.CLOSED) {
                        state = ConnectionState.DISCONNECTED;
                    }
                } else if (future.isSuccess() && state == ConnectionState.START_TLS) {
                    rc = BKException.Code.OK;
                    LOG.info("Successfully connected to bookie using TLS: " + addr);

                    state = ConnectionState.CONNECTED;
                    AuthHandler.ClientSideHandler authHandler = future.get().pipeline()
                            .get(AuthHandler.ClientSideHandler.class);
                    authHandler.authProvider.onProtocolUpgrade();
                    activeTlsChannelCounter.inc();
                } else if (future.isSuccess()
                        && (state == ConnectionState.CLOSED || state == ConnectionState.DISCONNECTED)) {
                    LOG.warn("Closed before TLS handshake completed, clean up: {}, current state {}", channel,
                            state);
                    closeChannel(channel);
                    rc = BKException.Code.BookieHandleNotAvailableException;
                    channel = null;
                } else if (future.isSuccess() && state == ConnectionState.CONNECTED) {
                    LOG.debug("Already connected with another channel({}), so close the new channel({})",
                            channel, channel);
                    closeChannel(channel);
                    return; // pendingOps should have been completed when other channel connected
                } else {
                    LOG.error("TLS handshake failed with bookie: {}/{}, current state {} : ", channel, addr,
                            state, future.cause());
                    rc = BKException.Code.SecurityException;
                    closeChannel(channel);
                    channel = null;
                    if (state != ConnectionState.CLOSED) {
                        state = ConnectionState.DISCONNECTED;
                    }
                    failedTlsHandshakeCounter.inc();
                }

                // trick to not do operations under the lock, take the list
                // of pending ops and assign it to a new variable, while
                // emptying the pending ops by just assigning it to a new
                // list
                oldPendingOps = pendingOps;
                pendingOps = new ArrayDeque<>();
            }

            makeWritable();

            for (GenericCallback<PerChannelBookieClient> pendingOp : oldPendingOps) {
                pendingOp.operationComplete(rc, PerChannelBookieClient.this);
            }
        }
    });
}

From source file:org.apache.flink.runtime.webmonitor.handlers.TaskManagerLogHandler.java

License:Apache License

/**
 * Response when running with leading JobManager.
 *//* w ww .j av a  2  s  .co  m*/
@Override
protected void respondAsLeader(final ChannelHandlerContext ctx, final Routed routed,
        final ActorGateway jobManager) {
    if (cache == null) {
        Future<Object> portFuture = jobManager.ask(JobManagerMessages.getRequestBlobManagerPort(), timeout);
        cache = portFuture.map(new Mapper<Object, BlobCache>() {
            @Override
            public BlobCache apply(Object result) {
                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);
    }

    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));
            Future<Object> taskManagerFuture = jobManager
                    .ask(new JobManagerMessages.RequestTaskManagerInstance(instanceID), timeout);

            Future<Object> blobKeyFuture = taskManagerFuture.flatMap(new Mapper<Object, Future<Object>>() {
                @Override
                public Future<Object> apply(Object instance) {
                    Instance taskManager = ((JobManagerMessages.TaskManagerInstance) instance).instance().get();
                    return taskManager.getActorGateway()
                            .ask(serveLogFile ? TaskManagerMessages.getRequestTaskManagerLog()
                                    : TaskManagerMessages.getRequestTaskManagerStdout(), timeout);
                }
            }, executor);

            Future<Object> logPathFuture = cache.zip(blobKeyFuture)
                    .map(new Mapper<Tuple2<BlobCache, Object>, Object>() {
                        @Override
                        public Object checkedApply(Tuple2<BlobCache, Object> instance) throws Exception {
                            BlobCache cache = instance._1();
                            if (instance._2() instanceof Exception) {
                                throw (Exception) instance._2();
                            }
                            BlobKey blobKey = (BlobKey) instance._2();

                            //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))) {
                                    cache.deleteGlobal(lastSubmittedFile.get(taskManagerID));
                                    lastSubmittedFile.put(taskManagerID, blobKey);
                                }
                            } else {
                                lastSubmittedFile.put(taskManagerID, blobKey);
                            }
                            return cache.getURL(blobKey).getFile();
                        }
                    }, executor);

            logPathFuture.onFailure(new OnFailure() {
                @Override
                public void onFailure(Throwable failure) throws Throwable {
                    display(ctx, request, "Fetching TaskManager log failed.");
                    LOG.error("Fetching TaskManager log failed.", failure);
                    lastRequestPending.remove(taskManagerID);
                }
            }, executor);

            logPathFuture.onSuccess(new OnSuccess<Object>() {
                @Override
                public void onSuccess(Object filePathOption) throws Throwable {
                    String filePath = (String) filePathOption;

                    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 = raf.length();
                    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.
                    ctx.write(new DefaultFileRegion(fc, 0, fileLength), ctx.newProgressivePromise())
                            .addListener(
                                    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();
                                        }
                                    });
                    ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);

                    // close the connection, if no keep-alive is needed
                    if (!HttpHeaders.isKeepAlive(request)) {
                        lastContentFuture.addListener(ChannelFutureListener.CLOSE);
                    }
                }
            }, executor);
        } catch (Exception e) {
            display(ctx, request, "Error: " + e.getMessage());
            LOG.error("Fetching TaskManager log failed.", e);
            lastRequestPending.remove(taskManagerID);
        }
    } else {
        display(ctx, request, "loading...");
    }
}

From source file:org.apache.hadoop.hbase.ipc.AsyncRpcChannel.java

License:Apache License

/**
 * Connect to channel//ww w  .java  2s  .c  o m
 *
 * @param bootstrap to connect to
 * @return future of connection
 */
private ChannelFuture connect(final Bootstrap bootstrap) {
    return bootstrap.remoteAddress(address).connect().addListener(new GenericFutureListener<ChannelFuture>() {
        @Override
        public void operationComplete(final ChannelFuture f) throws Exception {
            if (!f.isSuccess()) {
                if (f.cause() instanceof SocketException) {
                    retryOrClose(bootstrap, connectFailureCounter++, f.cause());
                } else {
                    retryOrClose(bootstrap, ioFailureCounter++, f.cause());
                }
                return;
            }
            channel = f.channel();

            setupAuthorization();

            ByteBuf b = channel.alloc().directBuffer(6);
            createPreamble(b, authMethod);
            channel.writeAndFlush(b).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
            if (useSasl) {
                UserGroupInformation ticket = AsyncRpcChannel.this.ticket.getUGI();
                if (authMethod == AuthMethod.KERBEROS) {
                    if (ticket != null && ticket.getRealUser() != null) {
                        ticket = ticket.getRealUser();
                    }
                }
                SaslClientHandler saslHandler;
                if (ticket == null) {
                    throw new FatalConnectionException("ticket/user is null");
                }
                final UserGroupInformation realTicket = ticket;
                saslHandler = ticket.doAs(new PrivilegedExceptionAction<SaslClientHandler>() {
                    @Override
                    public SaslClientHandler run() throws IOException {
                        return getSaslHandler(realTicket, bootstrap);
                    }
                });
                if (saslHandler != null) {
                    // Sasl connect is successful. Let's set up Sasl channel handler
                    channel.pipeline().addFirst(saslHandler);
                } else {
                    // fall back to simple auth because server told us so.
                    authMethod = AuthMethod.SIMPLE;
                    useSasl = false;
                }
            } else {
                startHBaseConnection(f.channel());
            }
        }
    });
}

From source file:org.apache.hadoop.hbase.ipc.AsyncRpcChannel.java

License:Apache License

/**
 * Start HBase connection//  w  w w  .j  av a 2s  .  c  om
 *
 * @param ch channel to start connection on
 */
private void startHBaseConnection(Channel ch) {
    ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
    ch.pipeline().addLast(new AsyncServerResponseHandler(this));
    try {
        writeChannelHeader(ch).addListener(new GenericFutureListener<ChannelFuture>() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (!future.isSuccess()) {
                    close(future.cause());
                    return;
                }
                List<AsyncCall> callsToWrite;
                synchronized (pendingCalls) {
                    connected = true;
                    callsToWrite = new ArrayList<AsyncCall>(pendingCalls.values());
                }
                for (AsyncCall call : callsToWrite) {
                    writeRequest(call);
                }
            }
        });
    } catch (IOException e) {
        close(e);
    }
}

From source file:org.apache.hadoop.hbase.ipc.AsyncRpcChannelImpl.java

License:Apache License

/**
 * Connect to channel/*from  w  w w .j ava 2s. c o m*/
 * @param bootstrap to connect to
 * @return future of connection
 */
private ChannelFuture connect(final Bootstrap bootstrap) {
    return bootstrap.remoteAddress(address).connect().addListener(new GenericFutureListener<ChannelFuture>() {
        @Override
        public void operationComplete(final ChannelFuture f) throws Exception {
            if (!f.isSuccess()) {
                retryOrClose(bootstrap, failureCounter++, client.failureSleep, f.cause());
                return;
            }
            channel = f.channel();

            setupAuthorization();

            ByteBuf b = channel.alloc().directBuffer(6);
            createPreamble(b, authMethod);
            channel.writeAndFlush(b).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
            if (useSasl) {
                UserGroupInformation ticket = AsyncRpcChannelImpl.this.ticket.getUGI();
                if (authMethod == AuthMethod.KERBEROS) {
                    if (ticket != null && ticket.getRealUser() != null) {
                        ticket = ticket.getRealUser();
                    }
                }
                SaslClientHandler saslHandler;
                if (ticket == null) {
                    throw new FatalConnectionException("ticket/user is null");
                }
                final UserGroupInformation realTicket = ticket;
                saslHandler = ticket.doAs(new PrivilegedExceptionAction<SaslClientHandler>() {
                    @Override
                    public SaslClientHandler run() throws IOException {
                        return getSaslHandler(realTicket, bootstrap);
                    }
                });
                if (saslHandler != null) {
                    // Sasl connect is successful. Let's set up Sasl channel handler
                    channel.pipeline().addFirst(saslHandler);
                } else {
                    // fall back to simple auth because server told us so.
                    authMethod = AuthMethod.SIMPLE;
                    useSasl = false;
                }
            } else {
                startHBaseConnection(f.channel());
            }
        }
    });
}

From source file:org.apache.hadoop.hbase.ipc.AsyncRpcClient.java

License:Apache License

/**
 * Call method async/*  w w w .ja va 2s.  c  o m*/
 */
private void callMethod(Descriptors.MethodDescriptor md, final PayloadCarryingRpcController pcrc, Message param,
        Message returnType, User ticket, InetSocketAddress addr, final RpcCallback<Message> done) {
    final AsyncRpcChannel connection;
    try {
        connection = createRpcChannel(md.getService().getName(), addr, ticket);

        connection.callMethod(md, pcrc, param, returnType)
                .addListener(new GenericFutureListener<Future<Message>>() {
                    @Override
                    public void operationComplete(Future<Message> future) throws Exception {
                        if (!future.isSuccess()) {
                            Throwable cause = future.cause();
                            if (cause instanceof IOException) {
                                pcrc.setFailed((IOException) cause);
                            } else {
                                pcrc.setFailed(new IOException(cause));
                            }
                        } else {
                            try {
                                done.run(future.get());
                            } catch (ExecutionException e) {
                                Throwable cause = e.getCause();
                                if (cause instanceof IOException) {
                                    pcrc.setFailed((IOException) cause);
                                } else {
                                    pcrc.setFailed(new IOException(cause));
                                }
                            } catch (InterruptedException e) {
                                pcrc.setFailed(new IOException(e));
                            }
                        }
                    }
                });
    } catch (StoppedRpcClientException | FailedServerException e) {
        pcrc.setFailed(e);
    }
}

From source file:org.apache.hama.ipc.AsyncServer.java

License:Apache License

/** start server listener */
public void start() throws ExecutionException, InterruptedException {
    ExecutorService es = Executors.newSingleThreadExecutor();
    Future<ChannelFuture> future = es.submit(new NioServerListener());
    try {/* www. jav  a 2s  . c o m*/
        ChannelFuture closeFuture = future.get();
        closeFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Void>>() {
            @Override
            public void operationComplete(io.netty.util.concurrent.Future<Void> voidFuture) throws Exception {
                // Stop the server gracefully if it's not terminated.
                stop();
            }
        });
    } finally {
        es.shutdown();
    }
}

From source file:org.apache.hive.spark.client.rpc.Rpc.java

License:Apache License

/**
 * Creates an RPC client for a server running on the given remote host and port.
 *
 * @param config RPC configuration data.
 * @param eloop Event loop for managing the connection.
 * @param host Host name or IP address to connect to.
 * @param port Port where server is listening.
 * @param clientId The client ID that identifies the connection.
 * @param secret Secret for authenticating the client with the server.
 * @param dispatcher Dispatcher used to handle RPC calls.
 * @return A future that can be used to monitor the creation of the RPC object.
 */// www.jav a2  s  . com
public static Promise<Rpc> createClient(Map<String, String> config, final NioEventLoopGroup eloop, String host,
        int port, final String clientId, final String secret, final RpcDispatcher dispatcher) throws Exception {
    final RpcConfiguration rpcConf = new RpcConfiguration(config);
    int connectTimeoutMs = (int) rpcConf.getConnectTimeoutMs();

    final ChannelFuture cf = new Bootstrap().group(eloop).handler(new ChannelInboundHandlerAdapter() {
    }).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMs).connect(host, port);

    final Promise<Rpc> promise = eloop.next().newPromise();
    final AtomicReference<Rpc> rpc = new AtomicReference<Rpc>();

    // Set up a timeout to undo everything.
    final Runnable timeoutTask = new Runnable() {
        @Override
        public void run() {
            promise.setFailure(new TimeoutException("Timed out waiting for RPC server connection."));
        }
    };
    final ScheduledFuture<?> timeoutFuture = eloop.schedule(timeoutTask, rpcConf.getServerConnectTimeoutMs(),
            TimeUnit.MILLISECONDS);

    // The channel listener instantiates the Rpc instance when the connection is established,
    // and initiates the SASL handshake.
    cf.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture cf) throws Exception {
            if (cf.isSuccess()) {
                SaslClientHandler saslHandler = new SaslClientHandler(rpcConf, clientId, promise, timeoutFuture,
                        secret, dispatcher);
                Rpc rpc = createRpc(rpcConf, saslHandler, (SocketChannel) cf.channel(), eloop);
                saslHandler.rpc = rpc;
                saslHandler.sendHello(cf.channel());
            } else {
                promise.setFailure(cf.cause());
            }
        }
    });

    // Handle cancellation of the promise.
    promise.addListener(new GenericFutureListener<Promise<Rpc>>() {
        @Override
        public void operationComplete(Promise<Rpc> p) {
            if (p.isCancelled()) {
                cf.cancel(true);
            }
        }
    });

    return promise;
}

From source file:org.apache.hive.spark.client.rpc.RpcServer.java

License:Apache License

@VisibleForTesting
Future<Rpc> registerClient(final String clientId, String secret, RpcDispatcher serverDispatcher,
        long clientTimeoutMs) {
    final Promise<Rpc> promise = group.next().newPromise();

    Runnable timeout = new Runnable() {
        @Override/* w w  w .  j a v a2 s . c om*/
        public void run() {
            promise.setFailure(new TimeoutException("Timed out waiting for client connection."));
        }
    };
    ScheduledFuture<?> timeoutFuture = group.schedule(timeout, clientTimeoutMs, TimeUnit.MILLISECONDS);
    final ClientInfo client = new ClientInfo(clientId, promise, secret, serverDispatcher, timeoutFuture);
    if (pendingClients.putIfAbsent(clientId, client) != null) {
        throw new IllegalStateException(String.format("Client '%s' already registered.", clientId));
    }

    promise.addListener(new GenericFutureListener<Promise<Rpc>>() {
        @Override
        public void operationComplete(Promise<Rpc> p) {
            if (!p.isSuccess()) {
                pendingClients.remove(clientId);
            }
        }
    });

    return promise;
}

From source file:org.apache.pulsar.client.impl.ConsumerImpl.java

License:Apache License

private CompletableFuture<Void> sendAcknowledge(MessageId messageId, AckType ackType) {
    MessageIdImpl msgId = (MessageIdImpl) messageId;
    final ByteBuf cmd = Commands.newAck(consumerId, msgId.getLedgerId(), msgId.getEntryId(), ackType, null);

    // There's no actual response from ack messages
    final CompletableFuture<Void> ackFuture = new CompletableFuture<Void>();

    if (isConnected()) {
        cnx().ctx().writeAndFlush(cmd).addListener(new GenericFutureListener<Future<Void>>() {
            @Override/*from w ww .jav  a2s . com*/
            public void operationComplete(Future<Void> future) throws Exception {
                if (future.isSuccess()) {
                    if (ackType == AckType.Individual) {
                        unAckedMessageTracker.remove(msgId);
                        // increment counter by 1 for non-batch msg
                        if (!(messageId instanceof BatchMessageIdImpl)) {
                            stats.incrementNumAcksSent(1);
                        }
                    } else if (ackType == AckType.Cumulative) {
                        stats.incrementNumAcksSent(unAckedMessageTracker.removeMessagesTill(msgId));
                    }
                    if (log.isDebugEnabled()) {
                        log.debug("[{}] [{}] [{}] Successfully acknowledged message - {}, acktype {}",
                                subscription, topic, consumerName, messageId, ackType);
                    }
                    ackFuture.complete(null);
                } else {
                    stats.incrementNumAcksFailed();
                    ackFuture.completeExceptionally(new PulsarClientException(future.cause()));
                }
            }
        });
    } else {
        stats.incrementNumAcksFailed();
        ackFuture.completeExceptionally(
                new PulsarClientException("Not connected to broker. State: " + getState()));
    }

    return ackFuture;
}