io.streamnative.pulsar.handlers.kop.lookup.PulsarClientLookupService 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
/**
* Copyright (c) 2019 - 2024 StreamNative, Inc.. All Rights Reserved.
*/
package io.streamnative.pulsar.handlers.kop.lookup;
import io.streamnative.pulsar.handlers.kop.AdvertisedListener;
import io.streamnative.pulsar.handlers.kop.EndPoint;
import io.streamnative.pulsar.handlers.kop.KafkaProtocolHandler;
import io.streamnative.pulsar.handlers.kop.KafkaServiceConfiguration;
import java.net.InetSocketAddress;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Nullable;
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.broker.PulsarService;
import org.apache.pulsar.common.naming.TopicName;
/**
* The lookup service that's based on a PulsarClient.
*/
@Slf4j
public class PulsarClientLookupService implements LookupService {
private final LookupClient lookupClient;
private final ServiceLookupDataCache serviceLookupDataCache;
public PulsarClientLookupService(PulsarService pulsar, KafkaServiceConfiguration config) {
this.lookupClient = new LookupClient(pulsar, config);
this.serviceLookupDataCache = new ServiceLookupDataCache(pulsar);
}
@Override
public CompletableFuture lookup(TopicName topicName, @Nullable EndPoint advertisedEndPoint) {
return lookupClient.getBrokerAddress(topicName).thenCombine(serviceLookupDataCache.getBrokers(),
(inetSocketAddress, serviceLookupDataList) -> {
final var serviceLookupData = serviceLookupDataList.stream()
.filter(lookupData -> lookupData.hasListened(inetSocketAddress))
.findAny()
.orElseThrow(() -> new IllegalStateException("No brokers in " + serviceLookupDataList + " have url "
+ inetSocketAddress));
final var kafkaAdvertisedListeners = Optional.ofNullable(serviceLookupData.protocols())
.map(protocols -> protocols.get(KafkaProtocolHandler.PROTOCOL_NAME))
.orElseThrow(() -> new IllegalStateException("No protocol handler is configured for "
+ serviceLookupData));
if (advertisedEndPoint == null) {
return EndPoint.findFirstListener(kafkaAdvertisedListeners);
} else {
return EndPoint.findListener(kafkaAdvertisedListeners, advertisedEndPoint.getListenerName());
}
})
.thenApply(listener -> {
final var advertisedListener = AdvertisedListener.create(listener);
return new InetSocketAddress(advertisedListener.getHostname(), advertisedListener.getPort());
});
}
@Override
public CompletableFuture closeAsync() {
return lookupClient.closeAsync();
}
}