Skip to content

Commit

Permalink
Merge pull request ClickHouse#52195 from Algunenano/happy_grandma
Browse files Browse the repository at this point in the history
Create ZK ancestors optimistically
  • Loading branch information
nikitamikhaylov authored Jul 20, 2023
2 parents b142777 + b8f73db commit 10b6fc9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
34 changes: 28 additions & 6 deletions src/Common/ZooKeeper/ZooKeeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include "KeeperException.h"
#include "TestKeeper.h"

#include <functional>
#include <filesystem>
#include <functional>
#include <ranges>
#include <vector>

#include <Common/ZooKeeper/Types.h>
#include <Common/ZooKeeper/ZooKeeperCommon.h>
Expand Down Expand Up @@ -350,15 +352,35 @@ void ZooKeeper::createIfNotExists(const std::string & path, const std::string &

void ZooKeeper::createAncestors(const std::string & path)
{
size_t pos = 1;
std::string data;
std::string path_created; // Ignored
std::vector<std::string> pending_nodes;

size_t last_pos = path.rfind('/');
if (last_pos == std::string::npos || last_pos == 0)
return;
std::string current_node = path.substr(0, last_pos);

while (true)
{
pos = path.find('/', pos);
if (pos == std::string::npos)
Coordination::Error code = createImpl(current_node, data, CreateMode::Persistent, path_created);
if (code == Coordination::Error::ZNONODE)
{
/// The parent node doesn't exist. Save the current node and try with the parent
last_pos = current_node.rfind('/');
if (last_pos == std::string::npos || last_pos == 0)
throw KeeperException(code, path);
pending_nodes.emplace_back(std::move(current_node));
current_node = path.substr(0, last_pos);
}
else if (code == Coordination::Error::ZOK || code == Coordination::Error::ZNODEEXISTS)
break;
createIfNotExists(path.substr(0, pos), "");
++pos;
else
throw KeeperException(code, path);
}

for (const std::string & pending : pending_nodes | std::views::reverse)
createIfNotExists(pending, data);
}

void ZooKeeper::checkExistsAndGetCreateAncestorsOps(const std::string & path, Coordination::Requests & requests)
Expand Down
3 changes: 1 addition & 2 deletions src/Storages/StorageReplicatedMergeTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9178,8 +9178,7 @@ std::optional<ZeroCopyLock> StorageReplicatedMergeTree::tryCreateZeroCopyExclusi
String zc_zookeeper_path = *getZeroCopyPartPath(part_name, disk);

/// Just recursively create ancestors for lock
zookeeper->createAncestors(zc_zookeeper_path);
zookeeper->createIfNotExists(zc_zookeeper_path, "");
zookeeper->createAncestors(zc_zookeeper_path + "/");

/// Create actual lock
ZeroCopyLock lock(zookeeper, zc_zookeeper_path, replica_name);
Expand Down

0 comments on commit 10b6fc9

Please sign in to comment.