io.streamnative.pulsar.handlers.kop.lookup.ServiceLookupDataCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pulsar-protocol-handler-kafka Show documentation
Show all versions of pulsar-protocol-handler-kafka Show documentation
Kafka on Pulsar implemented using Pulsar Protocol Handler
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());
});
}
}