Class StaticHostProvider

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

@Public public final class StaticHostProvider extends Object implements HostProvider
Most simple HostProvider, resolves on every next() call. Please be aware that although this class doesn't do any DNS caching, there're multiple levels of caching already present across the stack like in JVM, OS level, hardware, etc. The best we could do here is to get the most recent address from the underlying system which is considered up-to-date. It uses HostConnectionManager for all connection management and reconfiguration logic.
  • Constructor Details

    • StaticHostProvider

      public StaticHostProvider(Collection<InetSocketAddress> serverAddresses)
      Constructs a SimpleHostSet.
      Parameters:
      serverAddresses - possibly unresolved ZooKeeper server addresses
      Throws:
      IllegalArgumentException - if serverAddresses is empty or resolves to an empty list
    • StaticHostProvider

      public StaticHostProvider(Collection<InetSocketAddress> serverAddresses, ZKClientConfig clientConfig)
      Constructs a SimpleHostSet with ZKClientConfig. Introduced this new overload in 3.10.0 in order to take advantage of some newly introduced feature flags. Like the shuffle (old) / not to shuffle (new) behavior of DNS resolution.
      Parameters:
      serverAddresses - possibly unresolved ZooKeeper server addresses
      clientConfig - ZooKeeper client configuration
    • StaticHostProvider

      public StaticHostProvider(Collection<InetSocketAddress> serverAddresses, StaticHostProvider.Resolver resolver)
      Constructs a SimpleHostSet. Introduced for testing purposes. getAllByName() is a static method of InetAddress, therefore cannot be easily mocked. By abstraction of Resolver interface we can easily inject a mocked implementation in tests.
      Parameters:
      serverAddresses - possibly unresolved ZooKeeper server addresses
      resolver - custom resolver implementation
    • StaticHostProvider

      public StaticHostProvider(Collection<InetSocketAddress> serverAddresses, long randomnessSeed)
      Constructs a SimpleHostSet. This constructor is used from StaticHostProviderTest to produce deterministic test results by initializing sourceOfRandomness with the same seed
      Parameters:
      serverAddresses - possibly unresolved ZooKeeper server addresses
      randomnessSeed - a seed used to initialize sourceOfRandomness
      Throws:
      IllegalArgumentException - if serverAddresses is empty or resolves to an empty list
  • Method Details

    • 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
    • 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
    • getServerAtIndex

      public InetSocketAddress getServerAtIndex(int i)
    • getServerAtCurrentIndex

      public InetSocketAddress getServerAtCurrentIndex()