Package org.apache.bookkeeper.zookeeper
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 Summary
ConstructorsConstructorDescriptionCustomZooKeeperHostProvider(String connectString) CustomZooKeeperHostProvider(Collection<InetSocketAddress> serverAddresses) Constructs a SimpleHostSet.CustomZooKeeperHostProvider(Collection<InetSocketAddress> serverAddresses, long randomnessSeed) Constructs a SimpleHostSet.CustomZooKeeperHostProvider(Collection<InetSocketAddress> serverAddresses, org.apache.zookeeper.client.StaticHostProvider.Resolver resolver) Constructs a SimpleHostSet. -
Method Summary
Modifier and TypeMethodDescriptiongetServerAtIndex(int i) (package private) static booleanisMyServerInNewConfig(org.apache.zookeeper.client.StaticHostProvider.Resolver resolver, List<InetSocketAddress> connectAddresses, InetSocketAddress myServer) next(long spinDelay) voidintsize()booleanupdateServerList(Collection<InetSocketAddress> serverAddresses, InetSocketAddress currentHost) Update the list of servers.
-
Constructor Details
-
CustomZooKeeperHostProvider
-
CustomZooKeeperHostProvider
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 addressesresolver- 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 addressesrandomnessSeed- 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:
updateServerListin interfaceorg.apache.zookeeper.client.HostProvider- Parameters:
serverAddresses- new host listcurrentHost- 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
-
getServerAtCurrentIndex
-
size
public int size()- Specified by:
sizein interfaceorg.apache.zookeeper.client.HostProvider
-
next
- Specified by:
nextin interfaceorg.apache.zookeeper.client.HostProvider
-
onConnected
public void onConnected()- Specified by:
onConnectedin interfaceorg.apache.zookeeper.client.HostProvider
-