2020-10-29 07:07:42 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-03-23 20:03:08 +00:00
|
|
|
#include <cstddef>
|
2020-10-29 07:07:42 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <utility>
|
2021-03-08 20:08:04 +00:00
|
|
|
#include <mutex>
|
2020-10-29 07:07:42 +00:00
|
|
|
#include <string_view>
|
2021-03-23 20:03:08 +00:00
|
|
|
#include <vector>
|
2022-06-14 22:35:55 +00:00
|
|
|
#include <base/defines.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2020-10-29 07:07:42 +00:00
|
|
|
|
|
|
|
/** 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;
|
2021-03-23 20:03:08 +00:00
|
|
|
using FramePointers = std::vector<void *>;
|
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);
|
2021-08-08 17:08:36 +00:00
|
|
|
/// Get error code value by name.
|
|
|
|
///
|
|
|
|
/// It has O(N) complexity, but this is not major, since it is used only
|
|
|
|
/// for test hints, and it does not worth to keep another structure for
|
|
|
|
/// this.
|
|
|
|
ErrorCode getErrorCodeByName(std::string_view error_name);
|
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.
|
2021-05-08 15:20:40 +00:00
|
|
|
Value count = 0;
|
2021-03-16 18:31:14 +00:00
|
|
|
/// 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-23 20:03:08 +00:00
|
|
|
FramePointers trace;
|
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();
|
2021-03-23 20:03:08 +00:00
|
|
|
void increment(bool remote, const std::string & message, const FramePointers & trace);
|
2021-03-08 20:08:04 +00:00
|
|
|
|
|
|
|
private:
|
2022-06-14 22:35:55 +00:00
|
|
|
ErrorPair value TSA_GUARDED_BY(mutex);
|
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-23 20:03:08 +00:00
|
|
|
void increment(ErrorCode error_code, bool remote, const std::string & message, const FramePointers & trace);
|
2020-10-29 07:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|