Source:
private void getMonitorLock() throws KeeperException, InterruptedException { final String zRoot = ZooUtil.getRoot(instance); final String monitorPath = zRoot + Constants.ZMONITOR; final String monitorLockPath = zRoot + Constants.ZMONITOR_LOCK; ZooReaderWriter zoo = ZooReaderWriter.getInstance(); if (zoo.exists(monitorPath)) { byte[] data = zoo.getData(monitorPath, null); if (0 != data.length) { zoo.recursiveDelete(monitorPath, NodeMissingPolicy.SKIP); zoo.putPersistentData(monitorPath, new byte[0], NodeExistsPolicy.FAIL); zoo.putPersistentData(monitorLockPath, new byte[0], NodeExistsPolicy.FAIL); } else if (!zoo.exists(monitorLockPath)) { zoo.putPersistentData(monitorLockPath, new byte[0], NodeExistsPolicy.FAIL); } } else { zoo.putPersistentData(zRoot + Constants.ZMONITOR, new byte[0], NodeExistsPolicy.FAIL); if (!zoo.exists(monitorLockPath)) { zoo.putPersistentData(monitorLockPath, new byte[0], NodeExistsPolicy.FAIL); } } log.info("Acquiring Monitor lock"); while (true) { MoniterLockWatcher monitorLockWatcher = new MoniterLockWatcher(); monitorLock = new ZooLock(monitorLockPath); monitorLock.lockAsync(monitorLockWatcher, new byte[0]); monitorLockWatcher.waitForChange(); if (monitorLockWatcher.acquiredLock) { break; } if (!monitorLockWatcher.failedToAcquireLock) { log.warn("Failed to acquire Monitor lock. Monitor lock is in unknown state."); throw new IllegalStateException("monitor lock in unknown state"); } monitorLock.tryToCancelAsyncLockOrUnlock(); sleepUninterruptibly(getContext().getConfiguration().getTimeInMillis(Property.MONITOR_LOCK_CHECK_INTERVAL), TimeUnit.MILLISECONDS); } log.info("Got Monitor lock."); }

Target:
private void getMonitorLock() throws KeeperException, InterruptedException { final String zRoot = ZooUtil.getRoot(instance); final String monitorPath = zRoot + Constants.ZMONITOR; final String monitorLockPath = zRoot + Constants.ZMONITOR_LOCK; ZooReaderWriter zoo = ZooReaderWriter.getInstance(); if (zoo.exists(monitorPath)) { byte[] data = zoo.getData(monitorPath, null); if (0 != data.length) { zoo.recursiveDelete(monitorPath, NodeMissingPolicy.SKIP); zoo.putPersistentData(monitorPath, new byte[0], NodeExistsPolicy.FAIL); zoo.putPersistentData(monitorLockPath, new byte[0], NodeExistsPolicy.FAIL); } else if (!zoo.exists(monitorLockPath)) { zoo.putPersistentData(monitorLockPath, new byte[0], NodeExistsPolicy.FAIL); } } else { zoo.putPersistentData(zRoot + Constants.ZMONITOR, new byte[0], NodeExistsPolicy.FAIL); if (!zoo.exists(monitorLockPath)) { zoo.putPersistentData(monitorLockPath, new byte[0], NodeExistsPolicy.FAIL); } } log.info("Attempting to acquire Monitor Lock"); while (true) { MoniterLockWatcher monitorLockWatcher = new MoniterLockWatcher(); monitorLock = new ZooLock(monitorLockPath); monitorLock.lockAsync(monitorLockWatcher, new byte[0]); monitorLockWatcher.waitForChange(); if (monitorLockWatcher.acquiredLock) { break; } if (!monitorLockWatcher.failedToAcquireLock) { throw new IllegalStateException("monitor lock in unknown state"); } monitorLock.tryToCancelAsyncLockOrUnlock(); sleepUninterruptibly(getContext().getConfiguration().getTimeInMillis(Property.MONITOR_LOCK_CHECK_INTERVAL), TimeUnit.MILLISECONDS); } log.info("Acquired Monitor Lock " + monitorLock.getLockPath()); }