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

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

There is a newer version: 3.3.1.5
Show newest version
/**
 * 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();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy