███████████: write recount requests robustly [#METR-11718]

This commit is contained in:
Pavel Kartavyy 2014-06-27 21:52:50 +04:00
parent 0eedb5ee84
commit 9feab97b19
2 changed files with 28 additions and 0 deletions

View File

@ -9,6 +9,7 @@ namespace zkutil
{
const UInt32 DEFAULT_SESSION_TIMEOUT = 30000;
const UInt32 DEFAULT_RETRY_NUM = 3;
struct WatchWithPromise;
@ -86,6 +87,12 @@ public:
int32_t tryCreate(const std::string & path, const std::string & data, int32_t mode, std::string & pathCreated);
int32_t tryCreate(const std::string & path, const std::string & data, int32_t mode);
/** создает Persistent ноду.
* Игнорирует, если нода уже создана.
* Пытается сделать retry при ConnectionLoss или OperationTimeout
*/
void createIfNotExists(const std::string & path, const std::string & data);
/** Удалить ноду, если ее версия равна version (если -1, подойдет любая версия).
*/
void remove(const std::string & path, int32_t version = -1);

View File

@ -222,6 +222,27 @@ int32_t ZooKeeper::tryCreate(const std::string & path, const std::string & data,
return tryCreate(path, data, mode, pathCreated);
}
void ZooKeeper::createIfNotExists(const std::string & path, const std::string & data)
{
int32_t code = tryCreate(path, "", zkutil::CreateMode::Persistent);
if (code == ZOK || code == ZNODEEXISTS)
return;
if (!(code == ZOPERATIONTIMEOUT || code == ZCONNECTIONLOSS))
throw KeeperException(code, path);
for (size_t attempt = 0; attempt < retry_num && (code == ZOPERATIONTIMEOUT || code == ZCONNECTIONLOSS); ++attempt)
{
code = tryCreate(path, "", zkutil::CreateMode::Persistent);
};
if (code == ZOK || code == ZNODEEXISTS)
return;
else
throw KeeperException(code, path);
}
int32_t ZooKeeper::removeImpl(const std::string & path, int32_t version)
{
int32_t code = zoo_delete(impl, path.c_str(), version);