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

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

The 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.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
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 static final Map cacheMap = new ConcurrentHashMap<>();
    private final LockManager lookupDataLockManager;

    public static ServiceLookupDataCache get(PulsarService pulsar) {
        return cacheMap.computeIfAbsent(pulsar, __ -> new ServiceLookupDataCache(pulsar));
    }

    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