Fixed improper logging of failures in destructors of certain metrics (#11264)

This commit is contained in:
Alexander Kazakov 2020-05-29 12:45:55 +03:00 committed by GitHub
parent 09369c259c
commit 3f53b5b335
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 4 deletions

View File

@ -9,6 +9,7 @@
#include <Common/Exception.h>
#include <IO/ReadBufferFromMemory.h>
#include <IO/ReadHelpers.h>
#include <common/logger_useful.h>
namespace DB
@ -19,6 +20,7 @@ namespace ErrorCodes
extern const int FILE_DOESNT_EXIST;
extern const int CANNOT_OPEN_FILE;
extern const int CANNOT_READ_FROM_FILE_DESCRIPTOR;
extern const int CANNOT_CLOSE_FILE;
}
static constexpr auto filename = "/proc/self/statm";
@ -35,7 +37,18 @@ MemoryStatisticsOS::MemoryStatisticsOS()
MemoryStatisticsOS::~MemoryStatisticsOS()
{
if (0 != ::close(fd))
tryLogCurrentException(__PRETTY_FUNCTION__);
{
try
{
throwFromErrno(
"File descriptor for \"" + std::string(filename) + "\" could not be closed. "
"Something seems to have gone wrong. Inspect errno.", ErrorCodes::CANNOT_CLOSE_FILE);
}
catch (const ErrnoException &)
{
DB::tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
}
MemoryStatisticsOS::Data MemoryStatisticsOS::get() const

View File

@ -7,6 +7,7 @@
#include <IO/ReadHelpers.h>
#include <common/find_symbols.h>
#include <common/logger_useful.h>
#include <cassert>
#include <sys/types.h>
@ -22,6 +23,7 @@ namespace ErrorCodes
{
extern const int FILE_DOESNT_EXIST;
extern const int CANNOT_OPEN_FILE;
extern const int CANNOT_CLOSE_FILE;
extern const int CANNOT_READ_FROM_FILE_DESCRIPTOR;
}
@ -39,6 +41,20 @@ namespace
errno == ENOENT ? ErrorCodes::FILE_DOESNT_EXIST : ErrorCodes::CANNOT_OPEN_FILE);
}
inline void emitErrorMsgWithFailedToCloseFile(const std::string & filename)
{
try
{
throwFromErrno(
"File descriptor for \"" + filename + "\" could not be closed. "
"Something seems to have gone wrong. Inspect errno.", ErrorCodes::CANNOT_CLOSE_FILE);
}
catch (const ErrnoException &)
{
DB::tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
ssize_t readFromFD(const int fd, const char * filename, char * buf, size_t buf_size)
{
ssize_t res = 0;
@ -100,11 +116,11 @@ ProcfsMetricsProvider::ProcfsMetricsProvider(const pid_t /*tid*/)
ProcfsMetricsProvider::~ProcfsMetricsProvider()
{
if (stats_version >= 3 && 0 != ::close(thread_io_fd))
tryLogCurrentException(__PRETTY_FUNCTION__);
emitErrorMsgWithFailedToCloseFile(thread_io);
if (0 != ::close(thread_stat_fd))
tryLogCurrentException(__PRETTY_FUNCTION__);
emitErrorMsgWithFailedToCloseFile(thread_stat);
if (0 != ::close(thread_schedstat_fd))
tryLogCurrentException(__PRETTY_FUNCTION__);
emitErrorMsgWithFailedToCloseFile(thread_schedstat);
}