/** Process the {@link WatchedEvent} for a node which represents an {@link AuthenticationKey} */ void processChildNode(WatchedEvent event) throws KeeperException, InterruptedException { final String path = event.getPath(); switch (event.getType()) { case NodeDeleted: // Key expired if (null == path) { log.error("Got null path for NodeDeleted event"); return; } // Pull off the base ZK path and the '/' separator String childName = path.substring(baseNode.length() + 1); secretManager.removeKey(Integer.parseInt(childName)); break; case None: // Not connected, don't care. We'll update when we're reconnected break; case NodeCreated: // New key created if (null == path) { log.error("Got null path for NodeCreated event"); return; } // Get the data and reset the watcher AuthenticationKey key = deserializeKey(zk.getData(path, this, null)); log.debug("Adding AuthenticationKey with keyId {}", key.getKeyId()); secretManager.addKey(key); break; case NodeDataChanged: // Key changed, could happen on restart after not running Accumulo. if (null == path) { log.error("Got null path for NodeDataChanged event"); return; } // Get the data and reset the watcher AuthenticationKey newKey = deserializeKey(zk.getData(path, this, null)); // Will overwrite the old key if one exists secretManager.addKey(newKey); break; case NodeChildrenChanged: // no children for the children.. log.warn("Unexpected NodeChildrenChanged event for authentication key node {}", path); break; default: log.warn("Unsupported event type: {}", event.getType()); break; } }
@Override public void process(WatchedEvent event) { if (EventType.None == event.getType()) { switch (event.getState()) { case Disconnected: // Intentional fall through of case case Expired: // ZooReader is handling the Expiration of the original ZooKeeper object for // us log.debug("ZooKeeper connection disconnected, clearing secret manager"); secretManager.removeAllKeys(); break; case SyncConnected: log.debug("ZooKeeper reconnected, updating secret manager"); try { updateAuthKeys(); } catch (KeeperException | InterruptedException e) { log.error("Failed to update secret manager after ZooKeeper reconnect"); } break; default: log.warn("Unhandled: " + event); } // Nothing more to do for EventType.None return; } String path = event.getPath(); if (null == path) { return; } if (!path.startsWith(baseNode)) { log.info("Ignoring event for path: {}", path); return; } try { if (path.equals(baseNode)) { processBaseNode(event); } else { processChildNode(event); } } catch (KeeperException | InterruptedException e) { log.error("Failed to communicate with ZooKeeper", e); } }
private int updateAuthKeys(String path) throws KeeperException, InterruptedException { int keysAdded = 0; for (String child : zk.getChildren(path, this)) { String childPath = path + "/" + child; try { // Get the node data and reset the watcher AuthenticationKey key = deserializeKey(zk.getData(childPath, this, null)); secretManager.addKey(key); keysAdded++; } catch (NoNodeException e) { // The master expired(deleted) the key between when we saw it in getChildren() and when we // went to add it to our secret manager. log.trace("{} was deleted when we tried to access it", childPath); } } return keysAdded; }
/** * Process the {@link WatchedEvent} for the base znode that the {@link AuthenticationKey}s are * stored in. */ void processBaseNode(WatchedEvent event) throws KeeperException, InterruptedException { switch (event.getType()) { case NodeDeleted: // The parent node was deleted, no children are possible, remove all keys log.debug("Parent ZNode was deleted, removing all AuthenticationKeys"); secretManager.removeAllKeys(); break; case None: // Not connected, don't care break; case NodeCreated: // intentional fall-through to NodeChildrenChanged case NodeChildrenChanged: // Process each child, and reset the watcher on the parent node. We know that the node // exists updateAuthKeys(event.getPath()); break; case NodeDataChanged: // The data on the parent changed. We aren't storing anything there so it's a noop break; default: log.warn("Unsupported event type: {}", event.getType()); break; } }