Search in sources :

Example 1 with ServiceLookupData

use of org.apache.pulsar.policies.data.loadbalancer.ServiceLookupData in project incubator-pulsar by apache.

the class LookupProxyHandler method handleLookup.

public void handleLookup(CommandLookupTopic lookup) {
    if (log.isDebugEnabled()) {
        log.debug("Received Lookup from {}", clientAddress);
    }
    long clientRequestId = lookup.getRequestId();
    if (this.service.getLookupRequestSemaphore().tryAcquire()) {
        lookupRequests.inc();
        String topic = lookup.getTopic();
        String serviceUrl;
        if (isBlank(brokerServiceURL)) {
            ServiceLookupData availableBroker = null;
            try {
                availableBroker = service.getDiscoveryProvider().nextBroker();
            } catch (Exception e) {
                log.warn("[{}] Failed to get next active broker {}", clientAddress, e.getMessage(), e);
                proxyConnection.ctx().writeAndFlush(Commands.newLookupErrorResponse(ServerError.ServiceNotReady, e.getMessage(), clientRequestId));
                return;
            }
            serviceUrl = this.connectWithTLS ? availableBroker.getPulsarServiceUrlTls() : availableBroker.getPulsarServiceUrl();
        } else {
            serviceUrl = this.connectWithTLS ? service.getConfiguration().getBrokerServiceURLTLS() : service.getConfiguration().getBrokerServiceURL();
        }
        performLookup(clientRequestId, topic, serviceUrl, false, 10);
        this.service.getLookupRequestSemaphore().release();
    } else {
        rejectedLookupRequests.inc();
        if (log.isDebugEnabled()) {
            log.debug("Lookup Request ID {} from {} rejected - {}.", clientRequestId, clientAddress, throttlingErrorMessage);
        }
        proxyConnection.ctx().writeAndFlush(Commands.newLookupErrorResponse(ServerError.ServiceNotReady, throttlingErrorMessage, clientRequestId));
    }
}
Also used : ServiceLookupData(org.apache.pulsar.policies.data.loadbalancer.ServiceLookupData) URISyntaxException(java.net.URISyntaxException)

Example 2 with ServiceLookupData

use of org.apache.pulsar.policies.data.loadbalancer.ServiceLookupData in project incubator-pulsar by apache.

the class NamespaceService method createLookupResult.

protected CompletableFuture<LookupResult> createLookupResult(String candidateBroker) throws Exception {
    CompletableFuture<LookupResult> lookupFuture = new CompletableFuture<>();
    try {
        checkArgument(StringUtils.isNotBlank(candidateBroker), "Lookup broker can't be null " + candidateBroker);
        URI uri = new URI(candidateBroker);
        String path = String.format("%s/%s:%s", LoadManager.LOADBALANCE_BROKERS_ROOT, uri.getHost(), uri.getPort());
        pulsar.getLocalZkCache().getDataAsync(path, pulsar.getLoadManager().get().getLoadReportDeserializer()).thenAccept(reportData -> {
            if (reportData.isPresent()) {
                ServiceLookupData lookupData = reportData.get();
                lookupFuture.complete(new LookupResult(lookupData.getWebServiceUrl(), lookupData.getWebServiceUrlTls(), lookupData.getPulsarServiceUrl(), lookupData.getPulsarServiceUrlTls()));
            } else {
                lookupFuture.completeExceptionally(new KeeperException.NoNodeException(path));
            }
        }).exceptionally(ex -> {
            lookupFuture.completeExceptionally(ex);
            return null;
        });
    } catch (Exception e) {
        lookupFuture.completeExceptionally(e);
    }
    return lookupFuture;
}
Also used : ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) URL(java.net.URL) AdminResource(org.apache.pulsar.broker.admin.AdminResource) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) LoggerFactory(org.slf4j.LoggerFactory) LoadManager(org.apache.pulsar.broker.loadbalance.LoadManager) StringUtils(org.apache.commons.lang3.StringUtils) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Matcher(java.util.regex.Matcher) Pair(org.apache.commons.lang3.tuple.Pair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NamespaceOwnershipStatus(org.apache.pulsar.common.policies.data.NamespaceOwnershipStatus) Map(java.util.Map) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) URI(java.net.URI) NamespaceIsolationPolicy(org.apache.pulsar.common.policies.NamespaceIsolationPolicy) Set(java.util.Set) LocalPolicies(org.apache.pulsar.common.policies.data.LocalPolicies) PulsarWebResource.joinPath(org.apache.pulsar.broker.web.PulsarWebResource.joinPath) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) NamespaceBundleFactory(org.apache.pulsar.common.naming.NamespaceBundleFactory) List(java.util.List) ServiceUnitId(org.apache.pulsar.common.naming.ServiceUnitId) NamespaceBundleFactory.getBundlesData(org.apache.pulsar.common.naming.NamespaceBundleFactory.getBundlesData) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Code(org.apache.zookeeper.KeeperException.Code) TopicName(org.apache.pulsar.common.naming.TopicName) LOCAL_POLICIES_ROOT(org.apache.pulsar.broker.cache.LocalZooKeeperCacheService.LOCAL_POLICIES_ROOT) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ResourceUnit(org.apache.pulsar.broker.loadbalance.ResourceUnit) Hashing(com.google.common.hash.Hashing) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServerMetadataException(org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException) Lists(com.google.common.collect.Lists) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) ZooKeeperCache.cacheTimeOutInSec(org.apache.pulsar.zookeeper.ZooKeeperCache.cacheTimeOutInSec) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) LookupData(org.apache.pulsar.common.lookup.data.LookupData) PulsarService(org.apache.pulsar.broker.PulsarService) ServiceLookupData(org.apache.pulsar.policies.data.loadbalancer.ServiceLookupData) TimeUnit(java.util.concurrent.TimeUnit) LookupResult(org.apache.pulsar.broker.lookup.LookupResult) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) Codec(org.apache.pulsar.common.util.Codec) SECONDS(java.util.concurrent.TimeUnit.SECONDS) BrokerAssignment(org.apache.pulsar.common.policies.data.BrokerAssignment) CompletableFuture(java.util.concurrent.CompletableFuture) LookupResult(org.apache.pulsar.broker.lookup.LookupResult) ServiceLookupData(org.apache.pulsar.policies.data.loadbalancer.ServiceLookupData) URI(java.net.URI) ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) ServerMetadataException(org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException) KeeperException(org.apache.zookeeper.KeeperException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Aggregations

ServiceLookupData (org.apache.pulsar.policies.data.loadbalancer.ServiceLookupData)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Lists (com.google.common.collect.Lists)1 Hashing (com.google.common.hash.Hashing)1 String.format (java.lang.String.format)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1 SECONDS (java.util.concurrent.TimeUnit.SECONDS)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Matcher (java.util.regex.Matcher)1