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
|
|
|
|
inline bool isUnrecoverableErrorCode(int32_t zk_return_code)
|
|
|
|
{
|
|
|
|
return zk_return_code == ZINVALIDSTATE || zk_return_code == ZSESSIONEXPIRED || zk_return_code == ZSESSIONMOVED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Errors related with temporary network problems
|
|
|
|
inline bool isTemporaryErrorCode(int32_t zk_return_code)
|
|
|
|
{
|
|
|
|
return zk_return_code == ZCONNECTIONLOSS || zk_return_code == ZOPERATIONTIMEOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Any error related with network or master election
|
|
|
|
/// In case of these errors you should retry the query or reinitialize ZooKeeper session (see isUnrecoverable())
|
|
|
|
inline bool isHardwareErrorCode(int32_t zk_return_code)
|
|
|
|
{
|
|
|
|
return isUnrecoverableErrorCode(zk_return_code) || isTemporaryErrorCode(zk_return_code);
|
|
|
|
}
|
|
|
|
|
2018-01-19 22:37:50 +00:00
|
|
|
/// Valid errors sent from server
|
|
|
|
inline bool isUserError(int32_t zk_return_code)
|
|
|
|
{
|
|
|
|
return zk_return_code == ZNONODE
|
|
|
|
|| zk_return_code == ZBADVERSION
|
|
|
|
|| zk_return_code == ZNOCHILDRENFOREPHEMERALS
|
|
|
|
|| zk_return_code == ZNODEEXISTS
|
|
|
|
|| zk_return_code == ZNOTEMPTY;
|
|
|
|
}
|
|
|
|
|
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-10-03 14:44:10 +00:00
|
|
|
explicit KeeperException(const std::string & msg) : KeeperException(msg, ZOK, 0) {}
|
2017-04-01 07:20:54 +00:00
|
|
|
KeeperException(const std::string & msg, const int32_t code)
|
|
|
|
: KeeperException(msg + " (" + zerror(code) + ")", code, 0) {}
|
2017-10-03 14:44:10 +00:00
|
|
|
explicit KeeperException(const int32_t code) : KeeperException(zerror(code), code, 0) {}
|
2017-04-01 07:20:54 +00:00
|
|
|
KeeperException(const int32_t code, const std::string & path)
|
|
|
|
: KeeperException(std::string{zerror(code)} + ", path: " + path, code, 0) {}
|
|
|
|
|
|
|
|
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
|
|
|
/// You should reinitialize ZooKeeper session in case of these errors
|
2017-04-01 07:20:54 +00:00
|
|
|
bool isUnrecoverable() const
|
|
|
|
{
|
2017-10-03 14:44:10 +00:00
|
|
|
return isUnrecoverableErrorCode(code);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 14:44:10 +00:00
|
|
|
/// Errors related with temporary network problems
|
|
|
|
bool isTemporaryError() const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-10-03 14:44:10 +00:00
|
|
|
return isTemporaryErrorCode(code);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 14:44:10 +00:00
|
|
|
/// Any error related with network or master election
|
|
|
|
/// In case of these errors you should retry the query or reinitialize ZooKeeper session (see isUnrecoverable())
|
|
|
|
bool isHardwareError() const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-10-03 14:44:10 +00:00
|
|
|
return isHardwareErrorCode(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:
|
|
|
|
MultiTransactionInfo info;
|
|
|
|
|
|
|
|
/// If it is user error throws KeeperMultiException else throws ordinary KeeperException
|
|
|
|
/// If it is ZOK does nothing
|
|
|
|
static void check(const MultiTransactionInfo & info);
|
|
|
|
static void check(int code, const Ops & ops, const OpResultsPtr & op_results);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
KeeperMultiException(const MultiTransactionInfo & info, size_t failed_op_index);
|
|
|
|
};
|
|
|
|
|
2014-03-07 13:50:58 +00:00
|
|
|
};
|