From 1f03839830c1ec92b912bab6cdcfba6908780ccf Mon Sep 17 00:00:00 2001 From: Val Date: Sun, 24 May 2020 20:12:59 +0300 Subject: [PATCH] add zookeeper tryRemoveChildren method --- src/Common/ZooKeeper/ZooKeeper.cpp | 17 +++++++++++++++++ src/Common/ZooKeeper/ZooKeeper.h | 5 +++++ 2 files changed, 22 insertions(+) diff --git a/src/Common/ZooKeeper/ZooKeeper.cpp b/src/Common/ZooKeeper/ZooKeeper.cpp index 476e88d7e72..541625149dd 100644 --- a/src/Common/ZooKeeper/ZooKeeper.cpp +++ b/src/Common/ZooKeeper/ZooKeeper.cpp @@ -579,6 +579,23 @@ void ZooKeeper::removeChildren(const std::string & path) } +void ZooKeeper::tryRemoveChildren(const std::string & path) +{ + Strings children; + if (tryGetChildren(path, children) != Coordination::ZOK) + return; + while (!children.empty()) + { + Coordination::Requests ops; + for (size_t i = 0; i < MULTI_BATCH_SIZE && !children.empty(); ++i) + { + ops.emplace_back(makeRemoveRequest(path + "/" + children.back(), -1)); + children.pop_back(); + } + multi(ops); + } +} + void ZooKeeper::removeChildrenRecursive(const std::string & path) { Strings children = getChildren(path); diff --git a/src/Common/ZooKeeper/ZooKeeper.h b/src/Common/ZooKeeper/ZooKeeper.h index 416e40c2da4..cb28f442392 100644 --- a/src/Common/ZooKeeper/ZooKeeper.h +++ b/src/Common/ZooKeeper/ZooKeeper.h @@ -187,7 +187,12 @@ public: /// Remove all children nodes (non recursive). void removeChildren(const std::string & path); + /// Remove all children nodes (non recursive). + /// If there're no children, this method doesn't throw an exception + void tryRemoveChildren(const std::string & path); + using WaitCondition = std::function; + /// Wait for the node to disappear or return immediately if it doesn't exist. /// If condition is speficied, it is used to return early (when condition returns false) /// The function returns true if waited and false if waiting was interrupted by condition.