Class DnsSrvHostProvider

java.lang.Object
org.apache.zookeeper.client.DnsSrvHostProvider
All Implemented Interfaces:
AutoCloseable, HostProvider

@Public public final class DnsSrvHostProvider extends Object implements HostProvider
DNS SRV-based HostProvider that dynamically resolves host port names from DNS SRV records.

This implementation periodically refreshes the server list by querying DNS SRV records and uses HostConnectionManager for all connection management and reconfiguration logic.

Two-Phase Update Strategy:

  • Phase 1 (Background): Timer thread detects DNS changes without caching results
  • Phase 2 (Connection-time): Fresh DNS lookup during connect attempt if changes detected
  • Constructor Details

    • DnsSrvHostProvider

      public DnsSrvHostProvider(String dnsSrvName)
      Constructs a DnsSrvHostProvider with the given DNS name
      Parameters:
      dnsSrvName - the DNS name to query for SRV records
      Throws:
      IllegalArgumentException - if dnsSrvName is null or empty or invalid or if no SRV records are found for the DNS name or if DNS lookup fails
    • DnsSrvHostProvider

      public DnsSrvHostProvider(String dnsSrvName, ZKClientConfig clientConfig)
      Constructs a DnsSrvHostProvider with the given DNS name and ZKClientConfig
      Parameters:
      dnsSrvName - the DNS name to query for SRV records
      clientConfig - ZooKeeper client configuration
      Throws:
      IllegalArgumentException - if dnsSrvName is null or empty or invalid or if no SRV records are found for the DNS name or if DNS lookup fails
    • DnsSrvHostProvider

      public DnsSrvHostProvider(String dnsSrvName, long randomnessSeed, ZKClientConfig clientConfig)
      Constructs a DnsSrvHostProvider with the given DNS name, randomness seed and ZKClientConfig
      Parameters:
      dnsSrvName - the DNS name to query for SRV records
      randomnessSeed - seed for randomization
      clientConfig - ZooKeeper client configuration
      Throws:
      IllegalArgumentException - if dnsSrvName is null or empty or invalid or if no SRV records are found for the DNS name or if DNS lookup fails
    • DnsSrvHostProvider

      public DnsSrvHostProvider(String dnsSrvName, long randomnessSeed, DnsSrvHostProvider.DnsSrvResolver dnsResolver, ZKClientConfig clientConfig)
      Constructs a DnsSrvHostProvider with the given DNS name, randomization seed, DNS resolver and ZKClientConfig
      Parameters:
      dnsSrvName - the DNS name to query for SRV records
      randomnessSeed - seed for randomization
      dnsResolver - custom DNS resolver
      clientConfig - ZooKeeper client configuration
      Throws:
      IllegalArgumentException - if dnsSrvName is null or empty or invalid or if no SRV records are found for the DNS name or if DNS lookup fails
  • Method Details

    • size

      public int size()
      Specified by:
      size in interface HostProvider
    • next

      public InetSocketAddress next(long spinDelay)
      Description copied from interface: HostProvider
      The next host to try to connect to. For a spinDelay of 0 there should be no wait.
      Specified by:
      next in interface HostProvider
      Parameters:
      spinDelay - Milliseconds to wait if all hosts have been tried once.
    • onConnected

      public void onConnected()
      Description copied from interface: HostProvider
      Notify the HostProvider of a successful connection. The HostProvider may use this notification to reset it's inner state.
      Specified by:
      onConnected in interface HostProvider
    • updateServerList

      public boolean updateServerList(Collection<InetSocketAddress> serverAddresses, InetSocketAddress currentHost)
      Description copied from interface: HostProvider
      Update the list of servers. This returns true if changing connections is necessary for load-balancing, false otherwise.
      Specified by:
      updateServerList in interface HostProvider
      Parameters:
      serverAddresses - new host list
      currentHost - the host to which this client is currently connected
      Returns:
      true if changing connections is necessary for load-balancing, false otherwise
    • close

      public void close()
      Description copied from interface: HostProvider
      Close the HostProvider and release any resources. Default implementation does nothing for backward compatibility
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface HostProvider