Class NamespaceService

java.lang.Object
org.apache.pulsar.broker.namespace.NamespaceService
All Implemented Interfaces:
AutoCloseable

public class NamespaceService extends Object implements AutoCloseable
The NamespaceService provides resource ownership lookup as well as resource ownership claiming services for the PulsarService.

The PulsarService relies on this service for resource ownership operations.

The focus of this phase is to bring up the system and be able to iterate and improve the services effectively.

See Also:
  • Field Details

    • BUNDLE_SPLIT_RETRY_LIMIT

      public static final int BUNDLE_SPLIT_RETRY_LIMIT
      See Also:
    • SLA_NAMESPACE_PROPERTY

      public static final String SLA_NAMESPACE_PROPERTY
      See Also:
    • HEARTBEAT_NAMESPACE_PATTERN

      public static final Pattern HEARTBEAT_NAMESPACE_PATTERN
    • HEARTBEAT_NAMESPACE_PATTERN_V2

      public static final Pattern HEARTBEAT_NAMESPACE_PATTERN_V2
    • SLA_NAMESPACE_PATTERN

      public static final Pattern SLA_NAMESPACE_PATTERN
    • HEARTBEAT_NAMESPACE_FMT

      public static final String HEARTBEAT_NAMESPACE_FMT
      See Also:
    • HEARTBEAT_NAMESPACE_FMT_V2

      public static final String HEARTBEAT_NAMESPACE_FMT_V2
      See Also:
    • SLA_NAMESPACE_FMT

      public static final String SLA_NAMESPACE_FMT
      See Also:
    • LOOKUP_REQUEST_DURATION_METRIC_NAME

      public static final String LOOKUP_REQUEST_DURATION_METRIC_NAME
      See Also:
    • PULSAR_LOOKUP_RESPONSE_BROKER_ATTRIBUTES

      public static final io.opentelemetry.api.common.Attributes PULSAR_LOOKUP_RESPONSE_BROKER_ATTRIBUTES
    • PULSAR_LOOKUP_RESPONSE_REDIRECT_ATTRIBUTES

      public static final io.opentelemetry.api.common.Attributes PULSAR_LOOKUP_RESPONSE_REDIRECT_ATTRIBUTES
    • PULSAR_LOOKUP_RESPONSE_FAILURE_ATTRIBUTES

      public static final io.opentelemetry.api.common.Attributes PULSAR_LOOKUP_RESPONSE_FAILURE_ATTRIBUTES
  • Constructor Details

    • NamespaceService

      public NamespaceService(PulsarService pulsar)
      Default constructor.
  • Method Details

    • initialize

      public void initialize()
    • getBrokerServiceUrlAsync

      public CompletableFuture<Optional<LookupResult>> getBrokerServiceUrlAsync(org.apache.pulsar.common.naming.TopicName topic, LookupOptions options)
    • getBundleAsync

      public CompletableFuture<NamespaceBundle> getBundleAsync(org.apache.pulsar.common.naming.TopicName topic)
    • getBundleIfPresent

      public Optional<NamespaceBundle> getBundleIfPresent(org.apache.pulsar.common.naming.TopicName topicName)
    • getBundle

      public NamespaceBundle getBundle(org.apache.pulsar.common.naming.TopicName topicName)
    • getBundleCount

      public int getBundleCount(org.apache.pulsar.common.naming.NamespaceName namespace) throws Exception
      Throws:
      Exception
    • getWebServiceUrlAsync

      public CompletableFuture<Optional<URL>> getWebServiceUrlAsync(org.apache.pulsar.common.naming.ServiceUnitId suName, LookupOptions options)
      Return the URL of the broker who's owning a particular service unit in asynchronous way.

      If the service unit is not owned, return a CompletableFuture with empty optional.

    • getWebServiceUrl

      public Optional<URL> getWebServiceUrl(org.apache.pulsar.common.naming.ServiceUnitId suName, LookupOptions options) throws Exception
      Return the URL of the broker who's owning a particular service unit.

      If the service unit is not owned, return an empty optional

      Throws:
      Exception
    • registerBootstrapNamespaces

      public void registerBootstrapNamespaces() throws PulsarServerException
      Register all the bootstrap name spaces including the heartbeat namespace.
      Throws:
      PulsarServerException - if an unexpected error occurs
    • registerNamespace

      public boolean registerNamespace(org.apache.pulsar.common.naming.NamespaceName nsname, boolean ensureOwned) throws PulsarServerException
      Tries to register a namespace to this instance.
      Parameters:
      nsname - namespace name
      ensureOwned - sets the behavior when the namespace is already owned by another broker. If this flag is set to true, then the method will throw an exception. If this flag is set to false, then the method will return false.
      Returns:
      true if the namespace was successfully registered, false otherwise
      Throws:
      PulsarServerException - if an error occurs when registering the namespace
    • getHeartbeatOrSLAMonitorBrokerId

      public CompletableFuture<String> getHeartbeatOrSLAMonitorBrokerId(org.apache.pulsar.common.naming.ServiceUnitId serviceUnit, Function<String,CompletableFuture<Boolean>> isBrokerActive)
      Check if this is Heartbeat or SLAMonitor namespace and return the broker id.
      Parameters:
      serviceUnit - the service unit
      isBrokerActive - the function to check if the broker is active
      Returns:
      the broker id
    • createLookupResult

      public CompletableFuture<LookupResult> createLookupResult(String candidateBroker, boolean authoritativeRedirect, String advertisedListenerName)
    • isBrokerActive

      public boolean isBrokerActive(String candidateBroker)
    • unloadNamespaceBundle

      public CompletableFuture<Void> unloadNamespaceBundle(NamespaceBundle bundle)
    • unloadNamespaceBundle

      public CompletableFuture<Void> unloadNamespaceBundle(NamespaceBundle bundle, Optional<String> destinationBroker)
    • unloadNamespaceBundle

      public CompletableFuture<Void> unloadNamespaceBundle(NamespaceBundle bundle, Optional<String> destinationBroker, long timeout, TimeUnit timeoutUnit)
    • unloadNamespaceBundle

      public CompletableFuture<Void> unloadNamespaceBundle(NamespaceBundle bundle, long timeout, TimeUnit timeoutUnit)
    • unloadNamespaceBundle

      public CompletableFuture<Void> unloadNamespaceBundle(NamespaceBundle bundle, long timeout, TimeUnit timeoutUnit, boolean closeWithoutWaitingClientDisconnect)
    • unloadNamespaceBundle

      public CompletableFuture<Void> unloadNamespaceBundle(NamespaceBundle bundle, Optional<String> destinationBroker, long timeout, TimeUnit timeoutUnit, boolean closeWithoutWaitingClientDisconnect)
    • isNamespaceBundleOwned

      public CompletableFuture<Boolean> isNamespaceBundleOwned(NamespaceBundle bundle)
    • getOwnedNameSpacesStatusAsync

      public CompletableFuture<Map<String,org.apache.pulsar.common.policies.data.NamespaceOwnershipStatus>> getOwnedNameSpacesStatusAsync()
    • isNamespaceBundleDisabled

      public boolean isNamespaceBundleDisabled(NamespaceBundle bundle) throws Exception
      Throws:
      Exception
    • splitAndOwnBundle

      public CompletableFuture<Void> splitAndOwnBundle(NamespaceBundle bundle, boolean unload, NamespaceBundleSplitAlgorithm splitAlgorithm, List<Long> boundaries)
      1. split the given bundle into two bundles 2. assign ownership of both the bundles to current broker 3. update policies with newly created bundles into LocalZK 4. disable original bundle and refresh the cache.

      It will call splitAndOwnBundleOnceAndRetry to do the real retry work, which will retry "retryTimes".

      Parameters:
      bundle - the bundle to split
      unload - whether to unload the new split bundles
      splitAlgorithm - the algorithm to split the bundle
      boundaries - the boundaries to split the bundle
      Returns:
      a future that will complete when the bundle is split and owned
    • getSplitBoundary

      public CompletableFuture<org.apache.commons.lang3.tuple.Pair<NamespaceBundles,List<NamespaceBundle>>> getSplitBoundary(NamespaceBundle bundle, NamespaceBundleSplitAlgorithm nsBundleSplitAlgorithm, List<Long> boundaries)
      Get the split boundary's.
      Parameters:
      bundle - The bundle to split.
      boundaries - The specified positions, use for SpecifiedPositionsBundleSplitAlgorithm.
      Returns:
      A pair, left is target namespace bundle, right is split bundles.
    • getSplitBoundary

      public CompletableFuture<List<Long>> getSplitBoundary(NamespaceBundle bundle, List<Long> boundaries, NamespaceBundleSplitAlgorithm nsBundleSplitAlgorithm)
    • getNamespaceBundleSplitAlgorithmByName

      public NamespaceBundleSplitAlgorithm getNamespaceBundleSplitAlgorithmByName(String algorithmName)
    • updateNamespaceBundlesForPolicies

      public CompletableFuture<Void> updateNamespaceBundlesForPolicies(org.apache.pulsar.common.naming.NamespaceName nsname, NamespaceBundles nsBundles)
      Update new bundle-range to admin/policies/namespace. Update may fail because of concurrent write to Zookeeper.
      Parameters:
      nsname - the namespace name
      nsBundles - the new namespace bundles
    • updateNamespaceBundles

      public CompletableFuture<Void> updateNamespaceBundles(org.apache.pulsar.common.naming.NamespaceName nsname, NamespaceBundles nsBundles)
      Update new bundle-range to LocalZk (create a new node if not present). Update may fail because of concurrent write to Zookeeper.
      Parameters:
      nsname - the namespace name
      nsBundles - the new namespace bundles
    • getOwnershipCache

      public OwnershipCache getOwnershipCache()
    • getOwnedServiceUnits

      public Set<NamespaceBundle> getOwnedServiceUnits()
    • isServiceUnitOwned

      public boolean isServiceUnitOwned(org.apache.pulsar.common.naming.ServiceUnitId suName) throws Exception
      Throws:
      Exception
    • isServiceUnitOwnedAsync

      public CompletableFuture<Boolean> isServiceUnitOwnedAsync(org.apache.pulsar.common.naming.ServiceUnitId suName)
    • isServiceUnitActive

      @Deprecated public boolean isServiceUnitActive(org.apache.pulsar.common.naming.TopicName topicName)
      Deprecated.
      This method is only used in test now.
    • isServiceUnitActiveAsync

      public CompletableFuture<Boolean> isServiceUnitActiveAsync(org.apache.pulsar.common.naming.TopicName topicName)
    • checkTopicOwnership

      public CompletableFuture<Boolean> checkTopicOwnership(org.apache.pulsar.common.naming.TopicName topicName)
    • removeOwnedServiceUnitAsync

      public CompletableFuture<Void> removeOwnedServiceUnitAsync(NamespaceBundle nsBundle)
    • onNamespaceBundleOwned

      public void onNamespaceBundleOwned(NamespaceBundle bundle)
    • onNamespaceBundleUnload

      public void onNamespaceBundleUnload(NamespaceBundle bundle)
    • onNamespaceBundleSplit

      public void onNamespaceBundleSplit(NamespaceBundle bundle)
    • addNamespaceBundleOwnershipListener

      public void addNamespaceBundleOwnershipListener(NamespaceBundleOwnershipListener... listeners)
    • addNamespaceBundleSplitListener

      public void addNamespaceBundleSplitListener(NamespaceBundleSplitListener... listeners)
    • getNamespaceBundleFactory

      public NamespaceBundleFactory getNamespaceBundleFactory()
    • getServiceUnitId

      public org.apache.pulsar.common.naming.ServiceUnitId getServiceUnitId(org.apache.pulsar.common.naming.TopicName topicName) throws Exception
      Throws:
      Exception
    • getFullListOfTopics

      public CompletableFuture<List<String>> getFullListOfTopics(org.apache.pulsar.common.naming.NamespaceName namespaceName)
    • getFullListOfPartitionedTopic

      public CompletableFuture<List<String>> getFullListOfPartitionedTopic(org.apache.pulsar.common.naming.NamespaceName namespaceName)
    • getOwnedTopicListForNamespaceBundle

      public CompletableFuture<List<String>> getOwnedTopicListForNamespaceBundle(NamespaceBundle bundle)
    • checkTopicExists

      public CompletableFuture<TopicExistsInfo> checkTopicExists(org.apache.pulsar.common.naming.TopicName topic)
      Check topic exists( partitioned or non-partitioned ).
    • checkNonPartitionedTopicExists

      public CompletableFuture<Boolean> checkNonPartitionedTopicExists(org.apache.pulsar.common.naming.TopicName topic)
      Check non-partitioned topic exists.
    • checkNonPersistentNonPartitionedTopicExists

      public CompletableFuture<Boolean> checkNonPersistentNonPartitionedTopicExists(String topic)
      Regarding non-persistent topic, we do not know whether it exists or not. Redirect the request to the ownership broker of this topic. HTTP API has implemented the mechanism that redirect to ownership broker, so just call HTTP API here.
    • getListOfTopics

      public CompletableFuture<List<String>> getListOfTopics(org.apache.pulsar.common.naming.NamespaceName namespaceName, org.apache.pulsar.common.api.proto.CommandGetTopicsOfNamespace.Mode mode)
    • getListOfUserTopics

      public CompletableFuture<List<String>> getListOfUserTopics(org.apache.pulsar.common.naming.NamespaceName namespaceName, org.apache.pulsar.common.api.proto.CommandGetTopicsOfNamespace.Mode mode)
    • getAllPartitions

      public CompletableFuture<List<String>> getAllPartitions(org.apache.pulsar.common.naming.NamespaceName namespaceName)
    • getPartitions

      public CompletableFuture<List<String>> getPartitions(org.apache.pulsar.common.naming.NamespaceName namespaceName, org.apache.pulsar.common.naming.TopicDomain topicDomain)
    • getListOfPersistentTopics

      public CompletableFuture<List<String>> getListOfPersistentTopics(org.apache.pulsar.common.naming.NamespaceName namespaceName)
      List persistent topics names under a namespace, the topic name contains the partition suffix.
    • getListOfNonPersistentTopics

      public CompletableFuture<List<String>> getListOfNonPersistentTopics(org.apache.pulsar.common.naming.NamespaceName namespaceName)
    • getNamespaceClient

      public PulsarClientImpl getNamespaceClient(org.apache.pulsar.common.policies.data.ClusterDataImpl cluster)
    • getOwnerAsync

    • checkOwnershipPresent

      public boolean checkOwnershipPresent(NamespaceBundle bundle) throws Exception
      Throws:
      Exception
    • checkOwnershipPresentAsync

      public CompletableFuture<Boolean> checkOwnershipPresentAsync(NamespaceBundle bundle)
    • unloadSLANamespace

      public void unloadSLANamespace() throws Exception
      Throws:
      Exception
    • getHeartbeatNamespace

      public static org.apache.pulsar.common.naming.NamespaceName getHeartbeatNamespace(String lookupBroker, ServiceConfiguration config)
    • getHeartbeatNamespaceV2

      public static org.apache.pulsar.common.naming.NamespaceName getHeartbeatNamespaceV2(String lookupBroker, ServiceConfiguration config)
    • getSLAMonitorNamespace

      public static org.apache.pulsar.common.naming.NamespaceName getSLAMonitorNamespace(String lookupBroker, ServiceConfiguration config)
    • checkHeartbeatNamespace

      public static String checkHeartbeatNamespace(org.apache.pulsar.common.naming.ServiceUnitId ns)
    • checkHeartbeatNamespaceV2

      public static String checkHeartbeatNamespaceV2(org.apache.pulsar.common.naming.ServiceUnitId ns)
    • getSLAMonitorBrokerName

      public static String getSLAMonitorBrokerName(org.apache.pulsar.common.naming.ServiceUnitId ns)
    • isSystemServiceNamespace

      public static boolean isSystemServiceNamespace(String namespace)
    • isSLAOrHeartbeatNamespace

      public static boolean isSLAOrHeartbeatNamespace(String namespace)
      used for filtering bundles in special namespace.
      Parameters:
      namespace - the namespace name
      Returns:
      True if namespace is HEARTBEAT_NAMESPACE or SLA_NAMESPACE
    • isHeartbeatNamespace

      public static boolean isHeartbeatNamespace(org.apache.pulsar.common.naming.ServiceUnitId ns)
    • registerSLANamespace

      public boolean registerSLANamespace() throws PulsarServerException
      Throws:
      PulsarServerException
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable