Class CustomZooKeeperHostProvider

java.lang.Object
org.apache.bookkeeper.zookeeper.CustomZooKeeperHostProvider
All Implemented Interfaces:
org.apache.zookeeper.client.HostProvider

public final class CustomZooKeeperHostProvider extends Object implements org.apache.zookeeper.client.HostProvider
ZooKeeper HostProvider with a workaround for https://issues.apache.org/jira/browse/ZOOKEEPER-3825. Based on ZooKeeper's StaticHostProvider
  • Constructor Details

    • CustomZooKeeperHostProvider

      public CustomZooKeeperHostProvider(String connectString)
    • CustomZooKeeperHostProvider

      public CustomZooKeeperHostProvider(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
    • CustomZooKeeperHostProvider

      public CustomZooKeeperHostProvider(Collection<InetSocketAddress> serverAddresses, org.apache.zookeeper.client.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
    • CustomZooKeeperHostProvider

      public CustomZooKeeperHostProvider(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 org.apache.zookeeper.client.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
    • isMyServerInNewConfig

      static boolean isMyServerInNewConfig(org.apache.zookeeper.client.StaticHostProvider.Resolver resolver, List<InetSocketAddress> connectAddresses, InetSocketAddress myServer)
    • getServerAtIndex

      public InetSocketAddress getServerAtIndex(int i)
    • getServerAtCurrentIndex

      public InetSocketAddress getServerAtCurrentIndex()
    • size

      public int size()
      Specified by:
      size in interface org.apache.zookeeper.client.HostProvider
    • next

      public InetSocketAddress next(long spinDelay)
      Specified by:
      next in interface org.apache.zookeeper.client.HostProvider
    • onConnected

      public void onConnected()
      Specified by:
      onConnected in interface org.apache.zookeeper.client.HostProvider