mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-22 07:31:57 +00:00
zkutil: add Increment [#METR-15299]
███████████: write clicks to cloud [#METR-15299]
This commit is contained in:
parent
ed1ffd0fbf
commit
23ef0b9787
47
libs/libzkutil/include/zkutil/Increment.h
Normal file
47
libs/libzkutil/include/zkutil/Increment.h
Normal file
@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <zkutil/ZooKeeper.h>
|
||||
|
||||
namespace zkutil
|
||||
{
|
||||
|
||||
class Increment
|
||||
{
|
||||
public:
|
||||
Increment(ZooKeeperPtr zk_, const std::string & path_)
|
||||
: zk(zk_), path(path_)
|
||||
{
|
||||
}
|
||||
|
||||
size_t get()
|
||||
{
|
||||
LOG_TRACE(log, "Get increment");
|
||||
|
||||
size_t result = 0;
|
||||
std::string result_str;
|
||||
zkutil::Stat stat;
|
||||
|
||||
bool success = false;
|
||||
do
|
||||
{
|
||||
if (zk->tryGet(path, result_str, &stat))
|
||||
{
|
||||
result = std::stol(result_str) + 1;
|
||||
success = zk->trySet(path, std::to_string(result), stat.version) == ZOK;
|
||||
}
|
||||
else
|
||||
{
|
||||
success = zk->tryCreate(path, std::to_string(result), zkutil::CreateMode::Persistent) == ZOK;
|
||||
}
|
||||
}
|
||||
while(!success);
|
||||
|
||||
return result;
|
||||
}
|
||||
private:
|
||||
zkutil::ZooKeeperPtr zk;
|
||||
std::string path;
|
||||
Logger * log = &Logger::get("zkutil::Increment");
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user