use of org.apache.zookeeper.client.ConnectStringParser in project otter by alibaba.
the class ZooKeeperx method configMutliCluster.
// ===============================
public void configMutliCluster(ZooKeeper zk) {
if (_servers.size() == 1) {
return;
}
String cluster1 = _servers.get(0);
try {
if (_servers.size() > 1) {
// 强制的声明accessible
ReflectionUtils.makeAccessible(clientCnxnField);
ReflectionUtils.makeAccessible(hostProviderField);
ReflectionUtils.makeAccessible(serverAddressesField);
// 添加第二组集群列表
for (int i = 1; i < _servers.size(); i++) {
String cluster = _servers.get(i);
// 强制获取zk中的地址信息
ClientCnxn cnxn = (ClientCnxn) ReflectionUtils.getField(clientCnxnField, zk);
HostProvider hostProvider = (HostProvider) ReflectionUtils.getField(hostProviderField, cnxn);
List<InetSocketAddress> serverAddrs = (List<InetSocketAddress>) ReflectionUtils.getField(serverAddressesField, hostProvider);
// 添加第二组集群列表
serverAddrs.addAll(new ConnectStringParser(cluster).getServerAddresses());
}
}
} catch (Exception e) {
try {
if (zk != null) {
zk.close();
}
} catch (InterruptedException ie) {
// ignore interrupt
}
throw new ZkException("zookeeper_create_error, serveraddrs=" + cluster1, e);
}
}
use of org.apache.zookeeper.client.ConnectStringParser in project samza by apache.
the class ZkCoordinationServiceFactory method createZkPath.
/**
* if ZkConnectString contains some path at the end, it needs to be created when connecting for the first time.
* @param zkConnect - connect string
* @param zkClient - zkClient object to talk to the ZK
*/
public static void createZkPath(String zkConnect, ZkClient zkClient) {
ConnectStringParser parser = new ConnectStringParser(zkConnect);
String path = parser.getChrootPath();
LOG.info("path =" + path);
if (!Strings.isNullOrEmpty(path)) {
// create this path in zk
LOG.info("first connect. creating path =" + path + " in ZK " + parser.getServerAddresses());
if (!zkClient.exists(path)) {
// will create parents if needed and will not throw exception if exists
zkClient.createPersistent(path, true);
}
}
}
use of org.apache.zookeeper.client.ConnectStringParser in project zookeeper by apache.
the class ZooKeeper method updateServerList.
/**
* This function allows a client to update the connection string by providing
* a new comma separated list of host:port pairs, each corresponding to a
* ZooKeeper server.
* <p>
* The function invokes a <a href="https://issues.apache.org/jira/browse/ZOOKEEPER-1355">
* probabilistic load-balancing algorithm</a> which may cause the client to disconnect from
* its current host with the goal to achieve expected uniform number of connections per server
* in the new list. In case the current host to which the client is connected is not in the new
* list this call will always cause the connection to be dropped. Otherwise, the decision
* is based on whether the number of servers has increased or decreased and by how much.
* For example, if the previous connection string contained 3 hosts and now the list contains
* these 3 hosts and 2 more hosts, 40% of clients connected to each of the 3 hosts will
* move to one of the new hosts in order to balance the load. The algorithm will disconnect
* from the current host with probability 0.4 and in this case cause the client to connect
* to one of the 2 new hosts, chosen at random.
* <p>
* If the connection is dropped, the client moves to a special mode "reconfigMode" where he chooses
* a new server to connect to using the probabilistic algorithm. After finding a server,
* or exhausting all servers in the new list after trying all of them and failing to connect,
* the client moves back to the normal mode of operation where it will pick an arbitrary server
* from the connectString and attempt to connect to it. If establishment of
* the connection fails, another server in the connect string will be tried
* (the order is non-deterministic, as we random shuffle the list), until a
* connection is established. The client will continue attempts until the
* session is explicitly closed (or the session is expired by the server).
*
* @param connectString
* comma separated host:port pairs, each corresponding to a zk
* server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"
* If the optional chroot suffix is used the example would look
* like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a"
* where the client would be rooted at "/app/a" and all paths
* would be relative to this root - ie getting/setting/etc...
* "/foo/bar" would result in operations being run on
* "/app/a/foo/bar" (from the server perspective).
*
* @throws IOException in cases of network failure
*/
public void updateServerList(String connectString) throws IOException {
ConnectStringParser connectStringParser = new ConnectStringParser(connectString);
Collection<InetSocketAddress> serverAddresses = connectStringParser.getServerAddresses();
ClientCnxnSocket clientCnxnSocket = cnxn.sendThread.getClientCnxnSocket();
InetSocketAddress currentHost = (InetSocketAddress) clientCnxnSocket.getRemoteSocketAddress();
boolean reconfigMode = hostProvider.updateServerList(serverAddresses, currentHost);
// which will in turn call nextReconfigMode
if (reconfigMode)
clientCnxnSocket.testableCloseSocket();
}
Aggregations