Skip to content

Commit 58e34ff

Browse files
authored
Merge pull request ClickHouse#79879 from ClickHouse/always-log-important-errors
Always log critical exceptions
2 parents 1201bb1 + 7673a9d commit 58e34ff

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/Common/Exception.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <Common/logger_useful.h>
1818

1919
#include <algorithm>
20+
#include <atomic>
2021
#include <cstdlib>
2122
#include <cstring>
2223
#include <filesystem>
@@ -37,6 +38,7 @@ namespace ErrorCodes
3738
extern const int LOGICAL_ERROR;
3839
extern const int CANNOT_ALLOCATE_MEMORY;
3940
extern const int CANNOT_MREMAP;
41+
extern const int POTENTIALLY_BROKEN_DATA_PART;
4042
}
4143

4244
void abortOnFailedAssertion(const String & description, void * const * trace, size_t trace_offset, size_t trace_size)
@@ -263,6 +265,25 @@ void Exception::clearThreadFramePointers()
263265
thread_frame_pointers.frame_pointers.clear();
264266
}
265267

268+
bool Exception::isErrorCodeImportant() const
269+
{
270+
const int error_code = code();
271+
return error_code == ErrorCodes::LOGICAL_ERROR
272+
|| error_code == ErrorCodes::POTENTIALLY_BROKEN_DATA_PART;
273+
}
274+
275+
Exception::~Exception()
276+
try
277+
{
278+
if (logged != nullptr && !logged->load(std::memory_order_relaxed) && isErrorCodeImportant() && isLoggingEnabled())
279+
{
280+
LOG_ERROR(getLogger("ForcedCriticalErrorsLogger"), "{}", getExceptionMessage(*this, /*with_stacktrace=*/ true));
281+
}
282+
}
283+
catch (...) // NOLINT(bugprone-empty-catch)
284+
{
285+
}
286+
266287
static void tryLogCurrentExceptionImpl(Poco::Logger * logger, const std::string & start_of_message, LogsLevel level)
267288
{
268289
if (!isLoggingEnabled())
@@ -290,6 +311,19 @@ static void tryLogCurrentExceptionImpl(Poco::Logger * logger, const std::string
290311
catch (...) // NOLINT(bugprone-empty-catch)
291312
{
292313
}
314+
315+
/// Mark the exception as logged.
316+
try
317+
{
318+
throw;
319+
}
320+
catch (Exception & e)
321+
{
322+
e.markAsLogged();
323+
}
324+
catch (...) // NOLINT(bugprone-empty-catch)
325+
{
326+
}
293327
}
294328

295329
void tryLogCurrentException(const char * log_name, const std::string & start_of_message, LogsLevel level)

src/Common/Exception.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <Common/StackTrace.h>
77
#include <Core/LogsLevel.h>
88

9+
#include <atomic>
910
#include <cerrno>
1011
#include <exception>
1112
#include <vector>
@@ -62,6 +63,12 @@ class Exception : public Poco::Exception
6263
message_format_string_args = msg.format_string_args;
6364
}
6465

66+
~Exception() override;
67+
Exception(const Exception &) = default;
68+
Exception & operator=(const Exception &) = default;
69+
Exception(Exception &&) = default;
70+
Exception & operator=(Exception &&) = default;
71+
6572
/// Collect call stacks of all previous jobs' schedulings leading to this thread job's execution
6673
static thread_local bool enable_job_stack_trace;
6774
using ThreadFramePointersBase = std::vector<StackTrace::FramePointers>;
@@ -166,11 +173,23 @@ class Exception : public Poco::Exception
166173

167174
std::vector<std::string> getMessageFormatStringArgs() const { return message_format_string_args; }
168175

176+
void markAsLogged()
177+
{
178+
if (logged)
179+
{
180+
logged->store(true, std::memory_order_relaxed);
181+
}
182+
}
183+
184+
/// Indicates if the error code triggers alerts in ClickHouse Cloud
185+
bool isErrorCodeImportant() const;
186+
169187
private:
170188
#ifndef STD_EXCEPTION_HAS_STACK_TRACE
171189
StackTrace trace;
172190
#endif
173191
bool remote = false;
192+
std::shared_ptr<std::atomic<bool>> logged = std::make_shared<std::atomic<bool>>(false);
174193

175194
/// Number of this error among other errors with the same code and the same `remote` flag since the program startup.
176195
size_t error_index = static_cast<size_t>(-1);

0 commit comments

Comments
 (0)