/** * Get the monitor lock in ZooKeeper * * @throws KeeperException * @throws InterruptedException */ 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; // Ensure that everything is kosher with ZK as this has changed. ZooReaderWriter zoo = ZooReaderWriter.getInstance(); if (zoo.exists(monitorPath)) { byte[] data = zoo.getData(monitorPath, null); // If the node isn't empty, it's from a previous install (has hostname:port for HTTP server) if (0 != data.length) { // Recursively delete from that parent node zoo.recursiveDelete(monitorPath, NodeMissingPolicy.SKIP); // And then make the nodes that we expect for the incoming ephemeral nodes zoo.putPersistentData(monitorPath, new byte[0], NodeExistsPolicy.FAIL); zoo.putPersistentData(monitorLockPath, new byte[0], NodeExistsPolicy.FAIL); } else if (!zoo.exists(monitorLockPath)) { // monitor node in ZK exists and is empty as we expect // but the monitor/lock node does not zoo.putPersistentData(monitorLockPath, new byte[0], NodeExistsPolicy.FAIL); } } else { // 1.5.0 and earlier zoo.putPersistentData(zRoot + Constants.ZMONITOR, new byte[0], NodeExistsPolicy.FAIL); if (!zoo.exists(monitorLockPath)) { // Somehow the monitor node exists but not monitor/lock zoo.putPersistentData(monitorLockPath, new byte[0], NodeExistsPolicy.FAIL); } } // Get a ZooLock for the monitor 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(); UtilWaitThread.sleep( getSystemConfiguration().getTimeInMillis(Property.MONITOR_LOCK_CHECK_INTERVAL)); } log.info("Got Monitor lock."); }