Search in sources :

Example 31 with CompletableFuture

use of java.util.concurrent.CompletableFuture 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 32 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project pulsar by yahoo.

the class PartitionedConsumerImpl method internalReceiveAsync.

@Override
protected CompletableFuture<Message> internalReceiveAsync() {
    CompletableFuture<Message> result = new CompletableFuture<Message>();
    Message message;
    try {
        lock.writeLock().lock();
        message = incomingMessages.poll(0, TimeUnit.SECONDS);
        if (message == null) {
            pendingReceives.add(result);
        } else {
            resumeReceivingFromPausedConsumersIfNeeded();
            result.complete(message);
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        result.completeExceptionally(new PulsarClientException(e));
    } finally {
        lock.writeLock().unlock();
    }
    return result;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Message(com.yahoo.pulsar.client.api.Message) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException)

Example 33 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project pulsar by yahoo.

the class PartitionedConsumerImpl method unsubscribeAsync.

@Override
public CompletableFuture<Void> unsubscribeAsync() {
    if (getState() == State.Closing || getState() == State.Closed) {
        return FutureUtil.failedFuture(new PulsarClientException.AlreadyClosedException("Partitioned Consumer was already closed"));
    }
    setState(State.Closing);
    AtomicReference<Throwable> unsubscribeFail = new AtomicReference<Throwable>();
    AtomicInteger completed = new AtomicInteger(numPartitions);
    CompletableFuture<Void> unsubscribeFuture = new CompletableFuture<>();
    for (Consumer consumer : consumers) {
        if (consumer != null) {
            consumer.unsubscribeAsync().handle((unsubscribed, ex) -> {
                if (ex != null) {
                    unsubscribeFail.compareAndSet(null, ex);
                }
                if (completed.decrementAndGet() == 0) {
                    if (unsubscribeFail.get() == null) {
                        setState(State.Closed);
                        unsubscribeFuture.complete(null);
                        log.info("[{}] [{}] Unsubscribed Partitioned Consumer", topic, subscription);
                    } else {
                        setState(State.Failed);
                        unsubscribeFuture.completeExceptionally(unsubscribeFail.get());
                        log.error("[{}] [{}] Could not unsubscribe Partitioned Consumer", topic, subscription, unsubscribeFail.get().getCause());
                    }
                }
                return null;
            });
        }
    }
    return unsubscribeFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Consumer(com.yahoo.pulsar.client.api.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 34 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project pulsar by yahoo.

the class PartitionedProducerImpl method closeAsync.

@Override
public CompletableFuture<Void> closeAsync() {
    if (getState() == State.Closing || getState() == State.Closed) {
        return CompletableFuture.completedFuture(null);
    }
    setState(State.Closing);
    AtomicReference<Throwable> closeFail = new AtomicReference<Throwable>();
    AtomicInteger completed = new AtomicInteger(numPartitions);
    CompletableFuture<Void> closeFuture = new CompletableFuture<>();
    for (Producer producer : producers) {
        if (producer != null) {
            producer.closeAsync().handle((closed, ex) -> {
                if (ex != null) {
                    closeFail.compareAndSet(null, ex);
                }
                if (completed.decrementAndGet() == 0) {
                    if (closeFail.get() == null) {
                        setState(State.Closed);
                        closeFuture.complete(null);
                        log.info("[{}] Closed Partitioned Producer", topic);
                        client.cleanupProducer(this);
                    } else {
                        setState(State.Failed);
                        closeFuture.completeExceptionally(closeFail.get());
                        log.error("[{}] Could not close Partitioned Producer", topic, closeFail.get().getCause());
                    }
                }
                return null;
            });
        }
    }
    return closeFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Producer(com.yahoo.pulsar.client.api.Producer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 35 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project cassandra by apache.

the class ValidatorTest method registerOutgoingMessageSink.

private CompletableFuture<MessageOut> registerOutgoingMessageSink() {
    final CompletableFuture<MessageOut> future = new CompletableFuture<>();
    MessagingService.instance().addMessageSink(new IMessageSink() {

        public boolean allowOutgoingMessage(MessageOut message, int id, InetAddress to) {
            future.complete(message);
            return false;
        }

        public boolean allowIncomingMessage(MessageIn message, int id) {
            return false;
        }
    });
    return future;
}
Also used : MessageIn(org.apache.cassandra.net.MessageIn) CompletableFuture(java.util.concurrent.CompletableFuture) IMessageSink(org.apache.cassandra.net.IMessageSink) MessageOut(org.apache.cassandra.net.MessageOut) InetAddress(java.net.InetAddress)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)490 Test (org.junit.Test)152 ArrayList (java.util.ArrayList)88 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)77 List (java.util.List)75 UUID (java.util.UUID)62 Futures (io.pravega.common.concurrent.Futures)60 Map (java.util.Map)59 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)57 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)53 HashMap (java.util.HashMap)52 TimeUnit (java.util.concurrent.TimeUnit)52 Cleanup (lombok.Cleanup)49 Exceptions (io.pravega.common.Exceptions)48 Collectors (java.util.stream.Collectors)48 lombok.val (lombok.val)47 IOException (java.io.IOException)46 Duration (java.time.Duration)46 Slf4j (lombok.extern.slf4j.Slf4j)46 AtomicReference (java.util.concurrent.atomic.AtomicReference)45