Class StaticHostProvider

java.lang.Object
org.apache.zookeeper.client.StaticHostProvider
All Implemented Interfaces:
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.
  • 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, 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 sourceOfRandomnes
      Throws:
      IllegalArgumentException - if serverAddresses is empty or resolves to an empty list
  • Method Details

    • updateServerList

      public boolean updateServerList(Collection<InetSocketAddress> serverAddresses, InetSocketAddress currentHost)
      Update the list of servers. This returns true if changing connections is necessary for load-balancing, false otherwise. Changing connections is necessary if one of the following holds: a) the host to which this client is currently connected is not in serverAddresses. Otherwise (if currentHost is in the new list serverAddresses): b) the number of servers in the cluster is increasing - in this case the load on currentHost should decrease, which means that SOME of the clients connected to it will migrate to the new servers. The decision whether this client migrates or not (i.e., whether true or false is returned) is probabilistic so that the expected number of clients connected to each server is the same. If true is returned, the function sets pOld and pNew that correspond to the probability to migrate to ones of the new servers in serverAddresses or one of the old servers (migrating to one of the old servers is done only if our client's currentHost is not in serverAddresses). See nextHostInReconfigMode for the selection logic. See ZOOKEEPER-1355 for the protocol and its evaluation, and StaticHostProviderTest for the tests that illustrate how load balancing works with this policy.
      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
    • getServerAtIndex

      public InetSocketAddress getServerAtIndex(int i)
    • getServerAtCurrentIndex

      public InetSocketAddress getServerAtCurrentIndex()
    • 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