@Override public boolean isInSafeMode(boolean useCache, String cluster) { if (useCache) { Long safeModeTimestamp = _safeModeMap.get(cluster); if (safeModeTimestamp == null) { return true; } return safeModeTimestamp < System.currentTimeMillis() ? false : true; } LOG.debug("trace isInSafeMode"); try { String blurSafemodePath = ZookeeperPathConstants.getSafemodePath(cluster); Stat stat = _zk.exists(blurSafemodePath, false); if (stat == null) { return false; } byte[] data = _zk.getData(blurSafemodePath, false, stat); if (data == null) { return false; } long timestamp = Long.parseLong(new String(data)); long waitTime = timestamp - System.currentTimeMillis(); if (waitTime > 0) { return true; } return false; } catch (KeeperException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } }
@Override public void action(List<String> clusters) { for (String cluster : clusters) { if (!tableWatchers.containsKey(cluster)) { WatchChildren clusterWatcher = new WatchChildren(_zk, ZookeeperPathConstants.getTablesPath(cluster)) .watch(new Tables(cluster)); tableWatchers.put(cluster, clusterWatcher); WatchNodeExistance watchNodeExistance = new WatchNodeExistance(_zk, ZookeeperPathConstants.getSafemodePath(cluster)) .watch(new SafeExistance(cluster)); safeModeWatchers.put(cluster, watchNodeExistance); } } List<String> clustersToCloseAndRemove = new ArrayList<String>(clusters); clustersToCloseAndRemove.removeAll(tableWatchers.keySet()); for (String cluster : clustersToCloseAndRemove) { WatchChildren watcher = tableWatchers.remove(cluster); if (watcher == null) { LOG.error("Error watcher is null [" + cluster + "] "); } else { watcher.close(); } } }
@Override public void action(Stat stat) { if (stat != null) { safeModeDataWatchers.put( cluster, new WatchNodeData(_zk, ZookeeperPathConstants.getSafemodePath(cluster)) .watch( new WatchNodeData.OnChange() { @Override public void action(byte[] data) { if (data == null) { _safeModeMap.put(cluster, null); } else { _safeModeMap.put(cluster, Long.parseLong(new String(data))); } } })); } }