final String path = ZooUtil.getRoot(instance) + Constants.ZTSERVERS;
for (String server : getZooCache().getChildren(path)) {
// See if we have an async lock in place?
TServerInfo info = current.get(server);
TServerLockWatcher watcher;
ZooLock lock;
final String lockPath = path + "/" + server;
if (info != null) {
// yep: get out the lock/watcher so we can check on it
watcher = info.watcher;
lock = info.lock;
} else {
// nope: create a new lock and watcher
lock = new ZooLock(lockPath);
watcher = new TServerLockWatcher();
lock.lockAsync(watcher, "master".getBytes());
}
TServerInstance instance = null;
// Did we win the lock yet?
if (!lock.isLocked() && !watcher.gotLock && watcher.failureException == null) {
// Nope... there's a server out there: is this is a new server?
if (info == null) {
// Yep: hold onto the information about this server
Stat stat = new Stat();
byte[] lockData = ZooLock.getLockData(lockPath, stat);
String lockString = new String(lockData == null ? new byte[] {} : lockData);
if (lockString.length() > 0 && !lockString.equals("master")) {
ServerServices services = new ServerServices(new String(lockData));
InetSocketAddress client = services.getAddress(ServerServices.Service.TSERV_CLIENT);
InetSocketAddress addr = AddressUtil.parseAddress(server, Property.TSERV_CLIENTPORT);
TServerConnection conn = new TServerConnection(addr);
instance = new TServerInstance(client, stat.getEphemeralOwner());
info = new TServerInfo(lock, instance, conn, watcher);
current.put(server, info);
updates.add(instance);
} else {
lock.tryToCancelAsyncLockOrUnlock();
}
}
} else {
// Yes... there is no server here any more
lock.tryToCancelAsyncLockOrUnlock();
if (info != null) {
doomed.add(info.instance);
current.remove(server);
info.cleanup();
}