2014-03-07 13:50:58 +00:00
|
|
|
#pragma once
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
2018-01-15 19:07:47 +00:00
|
|
|
#include "Types.h"
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
2016-01-11 21:46:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int KEEPER_EXCEPTION;
|
|
|
|
}
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Event ZooKeeperExceptions;
|
2016-10-24 02:02:37 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 13:50:58 +00:00
|
|
|
|
|
|
|
namespace zkutil
|
|
|
|
{
|
|
|
|
|
2017-10-03 14:44:10 +00:00
|
|
|
|
|
|
|
/// You should reinitialize ZooKeeper session in case of these errors
|
2018-03-21 21:40:53 +00:00
|
|
|
inline bool isHardwareError(int32_t zk_return_code)
|
2017-10-03 14:44:10 +00:00
|
|
|
{
|
2018-03-22 12:15:06 +00:00
|
|
|
return zk_return_code == ZooKeeperImpl::ZooKeeper::ZINVALIDSTATE
|
|
|
|
|| zk_return_code == ZooKeeperImpl::ZooKeeper::ZSESSIONEXPIRED
|
|
|
|
|| zk_return_code == ZooKeeperImpl::ZooKeeper::ZSESSIONMOVED
|
|
|
|
|| zk_return_code == ZooKeeperImpl::ZooKeeper::ZCONNECTIONLOSS
|
|
|
|
|| zk_return_code == ZooKeeperImpl::ZooKeeper::ZOPERATIONTIMEOUT;
|
2017-10-03 14:44:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 22:37:50 +00:00
|
|
|
/// Valid errors sent from server
|
|
|
|
inline bool isUserError(int32_t zk_return_code)
|
|
|
|
{
|
2018-03-22 12:15:06 +00:00
|
|
|
return zk_return_code == ZooKeeperImpl::ZooKeeper::ZNONODE
|
|
|
|
|| zk_return_code == ZooKeeperImpl::ZooKeeper::ZBADVERSION
|
|
|
|
|| zk_return_code == ZooKeeperImpl::ZooKeeper::ZNOCHILDRENFOREPHEMERALS
|
|
|
|
|| zk_return_code == ZooKeeperImpl::ZooKeeper::ZNODEEXISTS
|
|
|
|
|| zk_return_code == ZooKeeperImpl::ZooKeeper::ZNOTEMPTY;
|
2018-01-19 22:37:50 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 14:44:10 +00:00
|
|
|
|
2014-04-02 10:38:27 +00:00
|
|
|
class KeeperException : public DB::Exception
|
2014-03-07 13:50:58 +00:00
|
|
|
{
|
2015-05-26 14:40:36 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
/// delegate constructor, used to minimize repetition; last parameter used for overload resolution
|
|
|
|
KeeperException(const std::string & msg, const int32_t code, int)
|
|
|
|
: DB::Exception(msg, DB::ErrorCodes::KEEPER_EXCEPTION), code(code) { incrementEventCounter(); }
|
2015-05-26 14:40:36 +00:00
|
|
|
|
2014-03-07 13:50:58 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
KeeperException(const std::string & msg, const int32_t code)
|
2018-03-22 12:15:06 +00:00
|
|
|
: KeeperException(msg + " (" + ZooKeeperImpl::ZooKeeper::errorMessage(code) + ")", code, 0) {}
|
|
|
|
explicit KeeperException(const int32_t code) : KeeperException(ZooKeeperImpl::ZooKeeper::errorMessage(code), code, 0) {}
|
2017-04-01 07:20:54 +00:00
|
|
|
KeeperException(const int32_t code, const std::string & path)
|
2018-03-22 12:15:06 +00:00
|
|
|
: KeeperException(std::string{ZooKeeperImpl::ZooKeeper::errorMessage(code)} + ", path: " + path, code, 0) {}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
KeeperException(const KeeperException & exc) : DB::Exception(exc), code(exc.code) { incrementEventCounter(); }
|
|
|
|
|
2017-10-03 14:44:10 +00:00
|
|
|
const char * name() const throw() override { return "zkutil::KeeperException"; }
|
|
|
|
const char * className() const throw() override { return "zkutil::KeeperException"; }
|
|
|
|
KeeperException * clone() const override { return new KeeperException(*this); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-10-03 14:44:10 +00:00
|
|
|
/// Any error related with network or master election
|
2018-03-21 21:40:53 +00:00
|
|
|
/// In case of these errors you should reinitialize ZooKeeper session.
|
2017-10-03 14:44:10 +00:00
|
|
|
bool isHardwareError() const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-03-21 21:40:53 +00:00
|
|
|
return zkutil::isHardwareError(code);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const int32_t code;
|
2014-09-11 20:34:41 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
static void incrementEventCounter()
|
|
|
|
{
|
|
|
|
ProfileEvents::increment(ProfileEvents::ZooKeeperExceptions);
|
|
|
|
}
|
2014-10-30 13:28:45 +00:00
|
|
|
|
2014-03-07 13:50:58 +00:00
|
|
|
};
|
|
|
|
|
2018-03-13 20:36:22 +00:00
|
|
|
|
|
|
|
class KeeperMultiException : public KeeperException
|
|
|
|
{
|
|
|
|
public:
|
2018-03-21 21:40:53 +00:00
|
|
|
Requests requests;
|
|
|
|
Responses responses;
|
2018-03-24 20:00:16 +00:00
|
|
|
size_t failed_op_index = 0;
|
|
|
|
|
2018-03-25 00:36:35 +00:00
|
|
|
size_t getFailedOpIndex() const;
|
2018-03-24 20:00:16 +00:00
|
|
|
std::string getPathForFirstFailedOp() const;
|
2018-03-13 20:36:22 +00:00
|
|
|
|
|
|
|
/// If it is user error throws KeeperMultiException else throws ordinary KeeperException
|
|
|
|
/// If it is ZOK does nothing
|
2018-03-21 21:40:53 +00:00
|
|
|
static void check(int32_t code, const Requests & requests, const Responses & responses);
|
2018-03-13 20:36:22 +00:00
|
|
|
|
2018-03-23 23:15:14 +00:00
|
|
|
KeeperMultiException(int32_t code, const Requests & requests, const Responses & responses);
|
2018-03-13 20:36:22 +00:00
|
|
|
};
|
|
|
|
|
2014-03-07 13:50:58 +00:00
|
|
|
};
|