private DataTree convertThisSnapShot() throws IOException { // create a datatree DataTree dataTree = new DataTree(); DataNodeV1 oldDataNode = oldDataTree.getNode(""); if (oldDataNode == null) { // should never happen LOG.error("Upgrading from an empty snapshot."); } recurseThroughDataTree(dataTree, ""); dataTree.lastProcessedZxid = oldDataTree.lastProcessedZxid; return dataTree; }
/** * convert a given old datanode to new datanode * * @param dt the new datatree * @param parent the parent of the datanode to be constructed * @param oldDataNode the old datanode * @return the new datanode */ private DataNode convertDataNode(DataTree dt, DataNode parent, DataNodeV1 oldDataNode) { StatPersisted stat = convertStat(oldDataNode.stat); DataNode dataNode = new DataNode(parent, oldDataNode.data, dt.convertAcls(oldDataNode.acl), stat); dataNode.setChildren(oldDataNode.children); return dataNode; }
/** * Return the data and the stat of the node of the given path. * * <p>If the watch is non-null and the call is successful (no exception is thrown), a watch will * be left on the node with the given path. The watch will be triggered by a successful operation * that sets data on the node, or deletes the node. * * <p>A KeeperException with error code KeeperException.NoNode will be thrown if no node with the * given path exists. * * @param path the given path * @param watcher explicit watcher * @param stat the stat of the node * @return the data of the node * @throws KeeperException If the server signals an error with a non-zero error code * @throws InterruptedException If the server transaction is interrupted. * @throws IllegalArgumentException if an invalid path is specified */ public byte[] getData(final String path, Watcher watcher, Stat stat) throws KeeperException, InterruptedException { final String clientPath = path; PathUtils.validatePath(clientPath); // the watch contains the un-chroot path WatchRegistration wcb = null; if (watcher != null) { wcb = new DataWatchRegistration(watcher, clientPath); } final String serverPath = prependChroot(clientPath); RequestHeader h = new RequestHeader(); h.setType(ZooDefs.OpCode.getData); GetDataRequest request = new GetDataRequest(); request.setPath(serverPath); request.setWatch(watcher != null); GetDataResponse response = new GetDataResponse(); ReplyHeader r = cnxn.submitRequest(h, request, response, wcb); if (r.getErr() != 0) { throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath); } if (stat != null) { DataTree.copyStat(response.getStat(), stat); } return response.getData(); }
/** * set watches on the datatree * * @param relativeZxid the relative zxid that client has seen * @param dataWatches the data watches the client wants to reset * @param existWatches the exists watches the client wants to reset * @param childWatches the child watches the client wants to reset * @param watcher the watcher function */ public void setWatches( long relativeZxid, List<String> dataWatches, List<String> existWatches, List<String> childWatches, Watcher watcher) { dataTree.setWatches(relativeZxid, dataWatches, existWatches, childWatches, watcher); }
/** * sync update * * @return: updatedData on success, or null on fail */ public T update( String path, DataUpdater<T> updater, List<String> createPaths, Stat stat, int options) { CreateMode mode = AccessOption.getMode(options); if (mode == null) { LOG.error("Invalid update mode. options: " + options); return null; } boolean retry; T updatedData = null; do { retry = false; try { Stat readStat = new Stat(); T oldData = (T) _zkClient.readData(path, readStat); T newData = updater.update(oldData); Stat setStat = _zkClient.writeDataGetStat(path, newData, readStat.getVersion()); if (stat != null) { DataTree.copyStat(setStat, stat); } updatedData = newData; } catch (ZkBadVersionException e) { retry = true; } catch (ZkNoNodeException e) { // node not exist, try create try { T newData = updater.update(null); RetCode rc = create(path, newData, createPaths, options); switch (rc) { case OK: updatedData = newData; break; case NODE_EXISTS: retry = true; break; default: LOG.error("Fail to update path by creating: " + path); return null; } } catch (Exception e1) { LOG.error("Exception while updating path by creating: " + path, e1); return null; } } catch (Exception e) { LOG.error("Exception while updating path: " + path, e); return null; } } while (retry); return updatedData; }
/** * recurse through the old datatree and construct the new data tree * * @param dataTree the new datatree to be constructed * @param path the path to start with */ private void recurseThroughDataTree(DataTree dataTree, String path) { if (path == null) return; DataNodeV1 oldDataNode = oldDataTree.getNode(path); HashSet<String> children = oldDataNode.children; DataNode parent = null; if ("".equals(path)) { parent = null; } else { int lastSlash = path.lastIndexOf('/'); String parentPath = path.substring(0, lastSlash); parent = dataTree.getNode(parentPath); } DataNode thisDatNode = convertDataNode(dataTree, parent, oldDataNode); dataTree.addDataNode(path, thisDatNode); if (children == null || children.size() == 0) { return; } else { for (String child : children) { recurseThroughDataTree(dataTree, path + "/" + child); } } }
/** * sync set * * @param setstat : if node is created instead of set, stat will NOT be set */ public boolean set( String path, T record, List<String> pathsCreated, Stat setstat, int expectVersion, int options) { CreateMode mode = AccessOption.getMode(options); if (mode == null) { LOG.error("Invalid set mode. options: " + options); return false; } boolean retry; do { retry = false; try { // _zkClient.writeData(path, record); Stat setStat = _zkClient.writeDataGetStat(path, record, expectVersion); if (setstat != null) DataTree.copyStat(setStat, setstat); } catch (ZkNoNodeException e) { // node not exists, try create. in this case, stat will not be set try { RetCode rc = create(path, record, pathsCreated, options); // if (rc == RetCode.OK || rc == RetCode.NODE_EXISTS) // retry = true; switch (rc) { case OK: // not set stat if node is created (instead of set) break; case NODE_EXISTS: retry = true; break; default: LOG.error("Fail to set path by creating: " + path); return false; } } catch (Exception e1) { LOG.error("Exception while setting path by creating: " + path, e); return false; } } catch (ZkBadVersionException e) { throw e; } catch (Exception e) { LOG.error("Exception while setting path: " + path, e); return false; } } while (retry); return true; }
/** * Return the ACL and stat of the node of the given path. * * <p>A KeeperException with error code KeeperException.NoNode will be thrown if no node with the * given path exists. * * @param path the given path for the node * @param stat the stat of the node will be copied to this parameter. * @return the ACL array of the given node. * @throws InterruptedException If the server transaction is interrupted. * @throws KeeperException If the server signals an error with a non-zero error code. * @throws IllegalArgumentException if an invalid path is specified */ public List<ACL> getACL(final String path, Stat stat) throws KeeperException, InterruptedException { final String clientPath = path; PathUtils.validatePath(clientPath); final String serverPath = prependChroot(clientPath); RequestHeader h = new RequestHeader(); h.setType(ZooDefs.OpCode.getACL); GetACLRequest request = new GetACLRequest(); request.setPath(serverPath); GetACLResponse response = new GetACLResponse(); ReplyHeader r = cnxn.submitRequest(h, request, response, null); if (r.getErr() != 0) { throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath); } DataTree.copyStat(response.getStat(), stat); return response.getAcl(); }
/** * kill a given session in the datatree * * @param sessionId the session id to be killed * @param zxid the zxid of kill session transaction */ public void killSession(long sessionId, long zxid) { dataTree.killSession(sessionId, zxid); }
/** * get data and stat for a path * * @param path the path being queried * @param stat the stat for this path * @param watcher the watcher function * @return * @throws KeeperException.NoNodeException */ public byte[] getData(String path, Stat stat, Watcher watcher) throws KeeperException.NoNodeException { return dataTree.getData(path, stat, watcher); }
/** * get the datanode for this path * * @param path the path to lookup * @return the datanode for getting the path */ public DataNode getNode(String path) { return dataTree.getNode(path); }
/** * stat the path * * @param path the path for which stat is to be done * @param serverCnxn the servercnxn attached to this request * @return the stat of this node * @throws KeeperException.NoNodeException */ public Stat statNode(String path, ServerCnxn serverCnxn) throws KeeperException.NoNodeException { return dataTree.statNode(path, serverCnxn); }
/** * the process txn on the data * * @param hdr the txnheader for the txn * @param txn the transaction that needs to be processed * @return the result of processing the transaction on this datatree/zkdatabase */ public ProcessTxnResult processTxn(TxnHeader hdr, Record txn) { return dataTree.processTxn(hdr, txn); }
/** * the last processed zxid in the datatree * * @param zxid the last processed zxid in the datatree */ public void setlastProcessedZxid(long zxid) { dataTree.lastProcessedZxid = zxid; }
/** * the paths for ephemeral session id * * @param sessionId the session id for which paths match to * @return the paths for a session id */ public Set<String> getEphemerals(long sessionId) { return dataTree.getEphemerals(sessionId); }
/** * Check whether the given watcher exists in datatree * * @param path node to check watcher existence * @param type type of watcher * @param watcher watcher function */ public boolean containsWatcher(String path, WatcherType type, Watcher watcher) { return dataTree.containsWatcher(path, type, watcher); }
/** * check if the path is special or not * * @param path the input path * @return true if path is special and false if not */ public boolean isSpecialPath(String path) { return dataTree.isSpecialPath(path); }
/** * get acl for a path * * @param path the path to query for acl * @param stat the stat for the node * @return the acl list for this path * @throws NoNodeException */ public List<ACL> getACL(String path, Stat stat) throws NoNodeException { return dataTree.getACL(path, stat); }
/** * remove a cnxn from the datatree * * @param cnxn the cnxn to remove from the datatree */ public void removeCnxn(ServerCnxn cnxn) { dataTree.removeCnxn(cnxn); }
public List<ACL> aclForNode(DataNode n) { return dataTree.getACL(n); }
/** * return the sessions in the datatree * * @return the data tree sessions */ public Collection<Long> getSessions() { return dataTree.getSessions(); }
/** * get children list for this path * * @param path the path of the node * @param stat the stat of the node * @param watcher the watcher function for this path * @return the list of children for this path * @throws KeeperException.NoNodeException */ public List<String> getChildren(String path, Stat stat, Watcher watcher) throws KeeperException.NoNodeException { return dataTree.getChildren(path, stat, watcher); }
/** * write a text dump of all the ephemerals in the datatree * * @param pwriter the output to write to */ public void dumpEphemerals(PrintWriter pwriter) { dataTree.dumpEphemerals(pwriter); }
/** * get the acl size of the datatree * * @return the acl size of the datatree */ public int getAclSize() { return dataTree.aclCacheSize(); }
public Map<Long, Set<String>> getEphemerals() { return dataTree.getEphemerals(); }
/** * Remove watch from the datatree * * @param path node to remove watches from * @param type type of watcher to remove * @param watcher watcher function to remove */ public boolean removeWatch(String path, WatcherType type, Watcher watcher) { return dataTree.removeWatch(path, type, watcher); }
/** * the node count of the datatree * * @return the node count of datatree */ public int getNodeCount() { return dataTree.getNodeCount(); }