@Override public void setCurrentCommandChangeListener(WatchedNodeListener<HostCommand> listener) { currentCommand.addListener(listener); }
public void close() { state.cancelWatch(); currentCommand.cancelWatch(); commandQueueWatcher.cancel(); }
@Override public HostState getState() throws IOException { HostState stateValue = state.get(); if (stateValue == null) { return HostState.OFFLINE; } else { return stateValue; } }
zk.create(ZkPath.append(path, COMMAND_QUEUE_PATH), null); this.state = new WatchedEnum<HostState>(HostState.class, zk, ZkPath.append(path, STATE_PATH), false); this.state.addListener(new HostStateDataLocationChangeNotifier()); this.statistics = new WatchedThriftNode<StatisticsMetadata>(zk, ZkPath.append(path, STATISTICS_PATH), false, null, null, new StatisticsMetadata()); commandQueueWatcher = new CommandQueueWatcher(); currentCommand = new WatchedEnum<HostCommand>(HostCommand.class, zk, ZkPath.append(path, CURRENT_COMMAND_PATH), true); if (create) {
@Override public void removeRingGroupConductorModeListener(WatchedNodeListener<RingGroupConductorMode> listener) { ringGroupConductorMode.removeListener(listener); }
@Override public void setRingGroupConductorMode(RingGroupConductorMode mode) throws IOException { try { ringGroupConductorMode.set(mode); } catch (KeeperException e) { throw new IOException(e); } catch (InterruptedException e) { throw new IOException(e); } }
@Override public void onWatchedNodeChange(String value) { cachedValue = getEnumValue(value); synchronized (listeners) { for (WatchedNodeListener<E> listener : listeners) { listener.onWatchedNodeChange(cachedValue); } } }
public WatchedEnum(final Class<E> clazz, final ZooKeeperPlus zk, final String nodePath, boolean waitForCreation) throws KeeperException, InterruptedException { this.clazz = clazz; this.watchedString = new WatchedString(zk, nodePath, waitForCreation); this.watchedString.addListener(this); onWatchedNodeChange(watchedString.get()); }
public ZkRingGroup(ZooKeeperPlus zk, String ringGroupPath, DomainGroup domainGroup, final Coordinator coordinator) throws InterruptedException, KeeperException { super(ZkPath.getFilename(ringGroupPath)); this.zk = zk; this.ringGroupPath = ringGroupPath; this.domainGroup = domainGroup; this.coordinator = coordinator; if (coordinator == null) { throw new IllegalArgumentException("Cannot initialize a ZkRingGroup with a null Coordinator."); } rings = new WatchedMap<ZkRing>(zk, ringGroupPath, new ElementLoader<ZkRing>() { @Override public ZkRing load(ZooKeeperPlus zk, String basePath, String relPath) throws KeeperException, InterruptedException { if (relPath.matches("ring-\\d+")) { return new ZkRing(zk, ZkPath.append(basePath, relPath), ZkRingGroup.this, coordinator, dataLocationChangeListener); } return null; } }); rings.addListener(new ZkRingGroup.RingsWatchedMapListener()); // Note: clients is lazy loaded in getClients() ringGroupConductorOnlinePath = ZkPath.append(ringGroupPath, RING_GROUP_CONDUCTOR_ONLINE_PATH); ringGroupConductorMode = new WatchedEnum<RingGroupConductorMode>(RingGroupConductorMode.class, zk, ringGroupConductorOnlinePath, false); }
@Override public void cancelStateChangeListener(final WatchedNodeListener<HostState> listener) { state.removeListener(listener); }
@Override public HostCommand nextCommand() throws IOException { try { // get the queue and sort so we have correct ordering List<String> children = zk.getChildren(ZkPath.append(path, COMMAND_QUEUE_PATH), false); Collections.sort(children); // if there are no children, the queue is empty. if (children.size() == 0) { currentCommand.set(null); return null; } // parse out the actual command String headOfQueuePath = ZkPath.append(path, COMMAND_QUEUE_PATH, children.get(0)); HostCommand nextCommand = HostCommand.valueOf(zk.getString(headOfQueuePath)); // set the current command first (modifying the queue will call the queue listeners) currentCommand.set(nextCommand); // delete the head of the queue zk.delete(headOfQueuePath, -1); return nextCommand; } catch (Exception e) { throw new IOException(e); } }
@Override public Long getUpSince() throws IOException { if (getState() == HostState.OFFLINE) { return null; } try { Stat stat = zk.exists(state.getPath(), false); if (stat == null) { return null; } else { return stat.getCtime(); } } catch (Exception e) { throw new IOException(e); } }
@Override public void addRingGroupConductorModeListener(WatchedNodeListener<RingGroupConductorMode> listener) { ringGroupConductorMode.addListener(listener); }
@Override public RingGroupConductorMode getRingGroupConductorMode() throws IOException { return ringGroupConductorMode.get(); }
@Override public void setStateChangeListener(final WatchedNodeListener<HostState> listener) throws IOException { state.addListener(listener); }
@Override public HostCommand getCurrentCommand() throws IOException { return currentCommand.get(); }