public static async Task moveZnode(SolrZkClient zkClient, string src, string dst, CancellationToken token) { String destName = normalizeDest(src, dst, true, true); // Special handling if the source has no children, i.e. copying just a single file. if (!(await zkClient.getChildren(src, null, true)).Any()) { await zkClient.makePath(destName, false, true); await zkClient.setData(destName, await zkClient.getData(src, null, null, true), true); } else { await traverseZkTree(zkClient, src, VISIT_ORDER.VISIT_PRE, async path => { var finalDestination = dst; if (path.Equals(src) == false) { finalDestination += "/" + path.Substring(src.Length + 1); } await zkClient.makePath(finalDestination, false, true); await zkClient.setData(finalDestination, await zkClient.getData(path, null, null, true), true); }); } // Insure all source znodes are present in dest before deleting the source. // throws error if not all there so the source is left intact. Throws error if source and dest don't match. await checkAllZnodesThere(zkClient, src, destName); await clean(zkClient, src, token); }
//Real place of this func is on ZkController.class in java version of code public async Task linkConfSet(string collection, string confSetName) { //This const palced in ZkStateReader.class in java version of code var path = CollectionsZknode + "/" + collection; byte[] data; KeyedList <string, object> props; try { data = await zkClient.getData(path, null, null, true); } catch (KeeperException.NoNodeException e) { // if there is no node, we will try and create it // first try to make in case we are pre configuring props = new KeyedList <string, object> { { ConfignameProp, confSetName } }; try { await zkClient.makePath(path, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(props)), CreateMode.PERSISTENT, null, true); } catch (KeeperException ex) { // it's okay if the node already exists if (ex.getCode() != KeeperException.Code.NODEEXISTS) { throw e; } // if we fail creating, setdata await zkClient.setData(path, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(props)), true); } return; } // we found existing data, let's update it if (data != null) { props = JsonConvert.DeserializeObject <KeyedList <string, object> >(Encoding.UTF8.GetString(data)); if (props.ContainsKey(ConfignameProp)) { props[ConfignameProp] = confSetName; } else { props.Add(ConfignameProp, confSetName); } } else { props = new KeyedList <string, object> { { ConfignameProp, confSetName } }; } await zkClient.setData(path, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(props)), true); }
private static async Task uploadFileToZk(SolrZkClient zkClient, string zkNode, string file, Regex filenameExclusions) { var fielName = Path.GetFileName(file); if (filenameExclusions != null && filenameExclusions.Match(fielName ?? throw new InvalidOperationException("File name is empty")).Success) { //TODO: Log here //log.info("uploadToZK skipping '{}' due to filenameExclusions '{}'", filename, filenameExclusions); return; } try { // if the path exists (and presumably we're uploading data to it) just set its data if (Path.GetFileName(file).Equals(ZKNODE_DATA_FILE) && (await zkClient.exists(zkNode, true))) { await zkClient.setData(zkNode, file, true); } else { //Can't work async because it will try to create same path zkClient.makePath(zkNode, file, false, true).Wait(); } } catch (KeeperException ex) { throw new Exception("Error uploading file " + file + " to zookeeper path " + zkNode, SolrZkClient.checkInterrupted(ex)); } }
public async void EnsureExists(string path, byte[] data, CreateMode createMode, SolrZkClient zkClient, int skipPathParts) { if (await zkClient.exists(path, true)) { return; } try { await zkClient.makePath(path, data, createMode, null, true, true, skipPathParts); } catch (KeeperException.NodeExistsException ex) { // it's okay if another beats us creating the node } }
/// <summary> /// Copy between local file system and Zookeeper, or from one Zookeeper node to another, optionally copying recursively. /// </summary> /// <param name="zkClient"></param> /// <param name="src">Source to copy from. Both src and dst may be Znodes. However, both may NOT be local</param> /// <param name="srcIsZk"></param> /// <param name="dst">The place to copy the files too. Both src and dst may be Znodes. However both may NOT be local</param> /// <param name="dstIsZk"></param> /// <param name="recurse">If the source is a directory, reccursively copy the contents iff this is true.</param> /// <exception cref="ArgumentException">Explanatory exception due to bad params, failed operation, etc.</exception> public static async Task zkTransfer(SolrZkClient zkClient, string src, bool srcIsZk, string dst, bool dstIsZk, bool recurse, CancellationToken token) { if (srcIsZk == false && dstIsZk == false) { throw new Exception("One or both of source or destination must specify ZK nodes."); } // Make sure -recurse is specified if the source has children. if (recurse == false) { if (srcIsZk) { if ((await zkClient.getChildren(src, null, true)).Any()) { throw new ArgumentException("Zookeeper node " + src + " has children and recurse is false"); } } else if (IsDirectory(src)) { throw new ArgumentException("Local path " + src + " is a directory and recurse is false"); } } if (dstIsZk && dst.Length == 0) { dst = "/"; // for consistency, one can copy from zk: and send to zk:/ } dst = normalizeDest(src, dst, srcIsZk, dstIsZk); // ZK -> ZK copy. if (srcIsZk && dstIsZk) { await traverseZkTree(zkClient, src, VISIT_ORDER.VISIT_PRE, async path => { var finalDestination = dst; if (path.Equals(src) == false) { finalDestination += "/" + path.Substring(src.Length + 1); } await zkClient.makePath(finalDestination, false, true); await zkClient.setData(finalDestination, await zkClient.getData(path, null, null, true), true); }); return; } //local -> ZK copy if (dstIsZk) { await uploadToZK(zkClient, src, dst, null, token); return; } // Copying individual files from ZK requires special handling since downloadFromZK assumes the node has children. // This is kind of a weak test for the notion of "directory" on Zookeeper. // ZK -> local copy where ZK is a parent node if ((await zkClient.getChildren(src, null, true)).Any()) { await downloadFromZK(zkClient, src, dst, token); return; } // Single file ZK -> local copy where ZK is a leaf node if (IsDirectory(dst)) { if (dst.EndsWith(Path.DirectorySeparatorChar.ToString()) == false) { dst += Path.DirectorySeparatorChar; } dst = normalizeDest(src, dst, srcIsZk, dstIsZk); } byte[] data = await zkClient.getData(src, null, null, true); Directory.CreateDirectory(Path.GetDirectoryName(dst)); //TODO: Log here //log.info("Writing file {}", filename); File.WriteAllBytes(dst, data); }