Java Code Examples for org.apache.pulsar.common.util.FutureUtil#waitForAll()

The following examples show how to use org.apache.pulsar.common.util.FutureUtil#waitForAll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PulsarKafkaConsumer.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<Void> doCommitOffsets(Map<TopicPartition, OffsetAndMetadata> offsets) {
    List<CompletableFuture<Void>> futures = new ArrayList<>();

    offsets.forEach((topicPartition, offsetAndMetadata) -> {
        org.apache.pulsar.client.api.Consumer<byte[]> consumer = consumers.get(topicPartition);
        lastCommittedOffset.put(topicPartition, offsetAndMetadata);
        futures.add(consumer.acknowledgeCumulativeAsync(MessageIdUtils.getMessageId(offsetAndMetadata.offset())));
    });

    return FutureUtil.waitForAll(futures);
}
 
Example 2
Source File: PulsarKafkaConsumer.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<Void> doCommitOffsets(Map<TopicPartition, OffsetAndMetadata> offsets) {
    List<CompletableFuture<Void>> futures = new ArrayList<>();

    applyConsumerInterceptorsOnCommit(interceptors, offsets);
    offsets.forEach((topicPartition, offsetAndMetadata) -> {
        org.apache.pulsar.client.api.Consumer<byte[]> consumer = consumers.get(topicPartition);
        lastCommittedOffset.put(topicPartition, offsetAndMetadata);
        futures.add(consumer.acknowledgeCumulativeAsync(MessageIdUtils.getMessageId(offsetAndMetadata.offset())));
    });

    return FutureUtil.waitForAll(futures);
}
 
Example 3
Source File: PersistentTransactionBuffer.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<Void> deleteTxns(Set<TxnID> txnIDS) {
    if (log.isDebugEnabled()) {
        log.debug("Start delete txns {} under ledger", txnIDS);
    }
    List<CompletableFuture<Void>> futures = txnIDS.stream().map(txnID -> deleteTxn(txnID))
                                                  .collect(Collectors.toList());
    return FutureUtil.waitForAll(futures);
}
 
Example 4
Source File: PersistentTransactionBuffer.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<Void> deleteEntries(SortedMap<Long, Position> entriesMap, TxnID txnID) {
    if (log.isDebugEnabled()) {
        log.debug("Delete entries {}", entriesMap);
    }
    List<CompletableFuture<Void>> deleteFutures = entriesMap.values().stream()
        .map(position -> asyncDeletePosition(position, txnID))
        .collect(Collectors.toList());

    return FutureUtil.waitForAll(deleteFutures);
}
 
Example 5
Source File: AdminResource.java    From pulsar with Apache License 2.0 5 votes vote down vote up
protected CompletableFuture<Void> tryCreatePartitionsAsync(int numPartitions) {
    if (!topicName.isPersistent()) {
        return CompletableFuture.completedFuture(null);
    }
    List<CompletableFuture<Void>> futures = new ArrayList<>(numPartitions);
    for (int i = 0; i < numPartitions; i++) {
        futures.add(tryCreatePartitionAsync(i, null));
    }
    return FutureUtil.waitForAll(futures);
}
 
Example 6
Source File: PersistentTopicsBase.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<Void> updatePartitionInOtherCluster(int numPartitions, Set<String> clusters) {
    List<CompletableFuture<Void>> results = new ArrayList<>(clusters.size() -1);
    clusters.forEach(cluster -> {
        if (cluster.equals(pulsar().getConfig().getClusterName())) {
            return;
        }
        results.add(pulsar().getBrokerService().getClusterPulsarAdmin(cluster).topics()
                .updatePartitionedTopicAsync(topicName.toString(), numPartitions, true));
    });
    return FutureUtil.waitForAll(results);
}
 
Example 7
Source File: SystemTopicClientBase.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> closeAsync() {
    List<CompletableFuture<Void>> futures = new ArrayList<>();
    writers.forEach(writer -> futures.add(writer.closeAsync()));
    readers.forEach(reader -> futures.add(reader.closeAsync()));
    writers.clear();
    readers.clear();
    return FutureUtil.waitForAll(futures);
}
 
Example 8
Source File: OwnershipCache.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Method to remove ownership of all owned bundles
 *
 * @param bundles
 *            <code>NamespaceBundles</code> to remove from ownership cache
 */
public CompletableFuture<Void> removeOwnership(NamespaceBundles bundles) {
    List<CompletableFuture<Void>> allFutures = Lists.newArrayList();
    for (NamespaceBundle bundle : bundles.getBundles()) {
        if (getOwnedBundle(bundle) == null) {
            // continue
            continue;
        }
        allFutures.add(this.removeOwnership(bundle));
    }
    return FutureUtil.waitForAll(allFutures);
}
 
Example 9
Source File: PersistentTopic.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Clears backlog for all cursors in the topic
 *
 * @return
 */
public CompletableFuture<Void> clearBacklog() {
    log.info("[{}] Clearing backlog on all cursors in the topic.", topic);
    List<CompletableFuture<Void>> futures = Lists.newArrayList();
    List<String> cursors = getSubscriptions().keys();
    cursors.addAll(getReplicators().keys());
    for (String cursor : cursors) {
        futures.add(clearBacklog(cursor));
    }
    return FutureUtil.waitForAll(futures);
}
 
Example 10
Source File: MultiTopicsConsumerImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<Void> seekAsync(long timestamp) {
    List<CompletableFuture<Void>> futures = new ArrayList<>(consumers.size());
    consumers.values().forEach(consumer -> futures.add(consumer.seekAsync(timestamp)));
    return FutureUtil.waitForAll(futures);
}
 
Example 11
Source File: PersistentTopic.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<Void> checkReplication() {
    TopicName name = TopicName.get(topic);
    if (!name.isGlobal()) {
        return CompletableFuture.completedFuture(null);
    }

    if (log.isDebugEnabled()) {
        log.debug("[{}] Checking replication status", name);
    }

    Policies policies = null;
    try {
        policies = brokerService.pulsar().getConfigurationCache().policiesCache()
                .get(AdminResource.path(POLICIES, name.getNamespace()))
                .orElseThrow(() -> new KeeperException.NoNodeException());
    } catch (Exception e) {
        CompletableFuture<Void> future = new CompletableFuture<>();
        future.completeExceptionally(new ServerMetadataException(e));
        return future;
    }

    final int newMessageTTLinSeconds = policies.message_ttl_in_seconds;

    Set<String> configuredClusters;
    if (policies.replication_clusters != null) {
        configuredClusters = Sets.newTreeSet(policies.replication_clusters);
    } else {
        configuredClusters = Collections.emptySet();
    }

    String localCluster = brokerService.pulsar().getConfiguration().getClusterName();

    // if local cluster is removed from global namespace cluster-list : then delete topic forcefully because pulsar
    // doesn't serve global topic without local repl-cluster configured.
    if (TopicName.get(topic).isGlobal() && !configuredClusters.contains(localCluster)) {
        log.info("Deleting topic [{}] because local cluster is not part of global namespace repl list {}",
                topic, configuredClusters);
        return deleteForcefully();
    }

    List<CompletableFuture<Void>> futures = Lists.newArrayList();

    // Check for missing replicators
    for (String cluster : configuredClusters) {
        if (cluster.equals(localCluster)) {
            continue;
        }

        if (!replicators.containsKey(cluster)) {
            futures.add(startReplicator(cluster));
        }
    }

    // Check for replicators to be stopped
    replicators.forEach((cluster, replicator) -> {
        // Update message TTL
        ((PersistentReplicator) replicator).updateMessageTTL(newMessageTTLinSeconds);

        if (!cluster.equals(localCluster)) {
            if (!configuredClusters.contains(cluster)) {
                futures.add(removeReplicator(cluster));
            }
        }

    });

    return FutureUtil.waitForAll(futures);
}
 
Example 12
Source File: PersistentTopic.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private synchronized CompletableFuture<Void> closeReplProducersIfNoBacklog() {
    List<CompletableFuture<Void>> closeFutures = Lists.newArrayList();
    replicators.forEach((region, replicator) -> closeFutures.add(replicator.disconnect(true)));
    return FutureUtil.waitForAll(closeFutures);
}
 
Example 13
Source File: PersistentTopic.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public CompletableFuture<Void> stopReplProducers() {
    List<CompletableFuture<Void>> closeFutures = Lists.newArrayList();
    replicators.forEach((region, replicator) -> closeFutures.add(replicator.disconnect()));
    return FutureUtil.waitForAll(closeFutures);
}
 
Example 14
Source File: NonPersistentTopic.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public CompletableFuture<Void> checkReplication() {
    TopicName name = TopicName.get(topic);
    if (!name.isGlobal()) {
        return CompletableFuture.completedFuture(null);
    }

    if (log.isDebugEnabled()) {
        log.debug("[{}] Checking replication status", name);
    }

    Policies policies = null;
    try {
        policies = brokerService.pulsar().getConfigurationCache().policiesCache()
                .get(AdminResource.path(POLICIES, name.getNamespace()))
                .orElseThrow(() -> new KeeperException.NoNodeException());
    } catch (Exception e) {
        CompletableFuture<Void> future = new CompletableFuture<>();
        future.completeExceptionally(new ServerMetadataException(e));
        return future;
    }

    Set<String> configuredClusters;
    if (policies.replication_clusters != null) {
        configuredClusters = policies.replication_clusters;
    } else {
        configuredClusters = Collections.emptySet();
    }

    String localCluster = brokerService.pulsar().getConfiguration().getClusterName();

    List<CompletableFuture<Void>> futures = Lists.newArrayList();

    // Check for missing replicators
    for (String cluster : configuredClusters) {
        if (cluster.equals(localCluster)) {
            continue;
        }

        if (!replicators.containsKey(cluster)) {
            if (!startReplicator(cluster)) {
                // it happens when global topic is a partitioned topic and replicator can't start on original
                // non partitioned-topic (topic without partition prefix)
                return FutureUtil
                        .failedFuture(new NamingException(topic + " failed to start replicator for " + cluster));
            }
        }
    }

    // Check for replicators to be stopped
    replicators.forEach((cluster, replicator) -> {
        if (!cluster.equals(localCluster)) {
            if (!configuredClusters.contains(cluster)) {
                futures.add(removeReplicator(cluster));
            }
        }
    });
    return FutureUtil.waitForAll(futures);
}
 
Example 15
Source File: NonPersistentTopic.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public CompletableFuture<Void> stopReplProducers() {
    List<CompletableFuture<Void>> closeFutures = Lists.newArrayList();
    replicators.forEach((region, replicator) -> closeFutures.add(replicator.disconnect()));
    return FutureUtil.waitForAll(closeFutures);
}
 
Example 16
Source File: NonPersistentTopic.java    From pulsar with Apache License 2.0 4 votes vote down vote up
/**
 * Close this topic - close all producers and subscriptions associated with this topic
 * 
 * @param closeWithoutWaitingClientDisconnect
 *            don't wait for client disconnect and forcefully close managed-ledger
 * @return Completable future indicating completion of close operation
 */
@Override
public CompletableFuture<Void> close(boolean closeWithoutWaitingClientDisconnect) {
    CompletableFuture<Void> closeFuture = new CompletableFuture<>();

    lock.writeLock().lock();
    try {
        if (!isFenced || closeWithoutWaitingClientDisconnect) {
            isFenced = true;
        } else {
            log.warn("[{}] Topic is already being closed or deleted", topic);
            closeFuture.completeExceptionally(new TopicFencedException("Topic is already fenced"));
            return closeFuture;
        }
    } finally {
        lock.writeLock().unlock();
    }

    List<CompletableFuture<Void>> futures = Lists.newArrayList();

    replicators.forEach((cluster, replicator) -> futures.add(replicator.disconnect()));
    producers.values().forEach(producer -> futures.add(producer.disconnect()));
    subscriptions.forEach((s, sub) -> futures.add(sub.disconnect()));

    CompletableFuture<Void> clientCloseFuture = closeWithoutWaitingClientDisconnect ? CompletableFuture.completedFuture(null)
            : FutureUtil.waitForAll(futures);

    clientCloseFuture.thenRun(() -> {
        log.info("[{}] Topic closed", topic);
        // unload topic iterates over topics map and removing from the map with the same thread creates deadlock.
        // so, execute it in different thread
        brokerService.executor().execute(() -> {
            brokerService.removeTopicFromCache(topic);
            closeFuture.complete(null);
        });
    }).exceptionally(exception -> {
        log.error("[{}] Error closing topic", topic, exception);
        isFenced = false;
        closeFuture.completeExceptionally(exception);
        return null;
    });

    return closeFuture;
}
 
Example 17
Source File: PersistentTransactionBuffer.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private CompletableFuture<Void> removeCommittedLedgerFromIndex(List<Long> dataLedgers) {
    List<CompletableFuture<Void>> removeFutures = dataLedgers.stream().map(
        dataLedger -> txnCursor.removeTxnsCommittedAtLedger(dataLedger)).collect(Collectors.toList());
    return FutureUtil.waitForAll(removeFutures);
}