2020-10-29 07:07:42 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <utility>
|
2021-03-08 20:08:04 +00:00
|
|
|
#include <mutex>
|
2020-10-29 07:07:42 +00:00
|
|
|
#include <common/types.h>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
/** Allows to count number of simultaneously happening error codes.
|
|
|
|
* See also Exception.cpp for incrementing part.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
/// ErrorCode identifier (index in array).
|
2021-03-08 19:05:51 +00:00
|
|
|
using ErrorCode = int;
|
|
|
|
using Value = size_t;
|
2020-10-29 07:07:42 +00:00
|
|
|
|
|
|
|
/// Get name of error_code by identifier.
|
|
|
|
/// Returns statically allocated string.
|
2020-10-29 18:08:09 +00:00
|
|
|
std::string_view getName(ErrorCode error_code);
|
2020-10-29 07:07:42 +00:00
|
|
|
|
2021-03-16 18:31:14 +00:00
|
|
|
struct Error
|
2021-03-08 19:05:51 +00:00
|
|
|
{
|
2021-03-16 18:31:14 +00:00
|
|
|
/// Number of times Exception with this ErrorCode had been throw.
|
|
|
|
Value count;
|
|
|
|
/// Time of the last error.
|
2021-03-08 20:43:58 +00:00
|
|
|
UInt64 error_time_ms = 0;
|
2021-03-16 18:31:14 +00:00
|
|
|
/// Message for the last error.
|
2021-03-08 20:31:51 +00:00
|
|
|
std::string message;
|
2021-03-16 18:31:14 +00:00
|
|
|
/// Stacktrace for the last error.
|
2021-03-08 20:39:19 +00:00
|
|
|
std::string stacktrace;
|
2021-03-16 18:31:14 +00:00
|
|
|
};
|
|
|
|
struct ErrorPair
|
|
|
|
{
|
|
|
|
Error local;
|
|
|
|
Error remote;
|
2021-03-08 20:08:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Thread-safe
|
2021-03-16 18:31:14 +00:00
|
|
|
struct ErrorPairHolder
|
2021-03-08 20:08:04 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-03-16 18:31:14 +00:00
|
|
|
ErrorPair get();
|
|
|
|
void increment(bool remote, const std::string & message, const std::string & stacktrace);
|
2021-03-08 20:08:04 +00:00
|
|
|
|
|
|
|
private:
|
2021-03-16 18:31:14 +00:00
|
|
|
ErrorPair value;
|
2021-03-08 20:08:04 +00:00
|
|
|
std::mutex mutex;
|
2021-03-08 19:05:51 +00:00
|
|
|
};
|
2021-03-08 20:08:04 +00:00
|
|
|
|
|
|
|
/// ErrorCode identifier -> current value of error_code.
|
2021-03-16 18:31:14 +00:00
|
|
|
extern ErrorPairHolder values[];
|
2020-10-29 07:07:42 +00:00
|
|
|
|
|
|
|
/// Get index just after last error_code identifier.
|
|
|
|
ErrorCode end();
|
|
|
|
|
|
|
|
/// Add value for specified error_code.
|
2021-03-08 20:39:19 +00:00
|
|
|
void increment(ErrorCode error_code, bool remote, const std::string & message, const std::string & stacktrace);
|
2020-10-29 07:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|