All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.streamnative.pulsar.handlers.kop.lookup.ServiceLookupDataCache Maven / Gradle / Ivy

There is a newer version: 4.0.0.4
Show newest version
/**
 * Copyright (c) 2019 - 2024 StreamNative, Inc.. All Rights Reserved.
 */
package io.streamnative.pulsar.handlers.kop.lookup;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import org.apache.pulsar.broker.PulsarService;
import org.apache.pulsar.broker.loadbalance.LoadManager;
import org.apache.pulsar.common.util.FutureUtil;
import org.apache.pulsar.metadata.api.coordination.LockManager;

public class ServiceLookupDataCache {

    private final LockManager lookupDataLockManager;

    public ServiceLookupDataCache(PulsarService pulsar) {
        this.lookupDataLockManager = pulsar.getCoordinationService().getLockManager(ServiceLookupData.class);
    }

    public CompletableFuture> getBrokers() {
        return lookupDataLockManager.listLocks(LoadManager.LOADBALANCE_BROKERS_ROOT).thenCompose(brokers -> {
            final var brokerLookupDataFutures = brokers.stream().map(broker -> {
                final var path = LoadManager.LOADBALANCE_BROKERS_ROOT + "/" + broker;
                return lookupDataLockManager.readLock(path);
            }).toList();
            return FutureUtil.waitForAll(brokerLookupDataFutures).thenApply(ignored -> brokerLookupDataFutures.stream()
                .map(CompletableFuture::join).filter(Optional::isPresent).map(Optional::get).toList());
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy