mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-18 04:12:19 +00:00
98e3a99a88
Since this hides real problems, since destructor does final flush and if it fails, then data will be lost. One of such examples if MEMORY_LIMIT_EXCEEDED exception, so lock exceptions from destructors, by using MemoryTracker::LockExceptionInThread to block these exception, and allow others (so std::terminate will be called, since this is c++11 with noexcept for destructors by default). Here is an example, that leads to empty block in the distributed batch: 2021.01.21 12:43:18.619739 [ 46468 ] {7bd60d75-ebcb-45d2-874d-260df9a4ddac} <Error> virtual DB::CompressedWriteBuffer::~CompressedWriteBuffer(): Code: 241, e.displayText() = DB::Exception: Memory limit (for user) exceeded: would use 332.07 GiB (attempt to allocate chunk of 4355342 bytes), maximum: 256.00 GiB, Stack trace (when copying this message, always include the lines below): 0. DB::Exception::Exception<>() @ 0x86f7b88 in /usr/bin/clickhouse ... 4. void DB::PODArrayBase<>::resize<>(unsigned long) @ 0xe9e878d in /usr/bin/clickhouse 5. DB::CompressedWriteBuffer::nextImpl() @ 0xe9f0296 in /usr/bin/clickhouse 6. DB::CompressedWriteBuffer::~CompressedWriteBuffer() @ 0xe9f0415 in /usr/bin/clickhouse 7. DB::DistributedBlockOutputStream::writeToShard() @ 0xf6bed4a in /usr/bin/clickhouse
146 lines
3.7 KiB
C++
146 lines
3.7 KiB
C++
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include <cassert>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
|
|
#include <Common/Exception.h>
|
|
#include <Common/ProfileEvents.h>
|
|
#include <Common/CurrentMetrics.h>
|
|
#include <Common/Stopwatch.h>
|
|
#include <Common/MemoryTracker.h>
|
|
|
|
#include <IO/WriteBufferFromFileDescriptor.h>
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
|
namespace ProfileEvents
|
|
{
|
|
extern const Event WriteBufferFromFileDescriptorWrite;
|
|
extern const Event WriteBufferFromFileDescriptorWriteFailed;
|
|
extern const Event WriteBufferFromFileDescriptorWriteBytes;
|
|
extern const Event DiskWriteElapsedMicroseconds;
|
|
}
|
|
|
|
namespace CurrentMetrics
|
|
{
|
|
extern const Metric Write;
|
|
}
|
|
|
|
namespace DB
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
{
|
|
extern const int CANNOT_WRITE_TO_FILE_DESCRIPTOR;
|
|
extern const int CANNOT_FSYNC;
|
|
extern const int CANNOT_SEEK_THROUGH_FILE;
|
|
extern const int CANNOT_TRUNCATE_FILE;
|
|
extern const int CANNOT_FSTAT;
|
|
}
|
|
|
|
|
|
void WriteBufferFromFileDescriptor::nextImpl()
|
|
{
|
|
if (!offset())
|
|
return;
|
|
|
|
Stopwatch watch;
|
|
|
|
size_t bytes_written = 0;
|
|
while (bytes_written != offset())
|
|
{
|
|
ProfileEvents::increment(ProfileEvents::WriteBufferFromFileDescriptorWrite);
|
|
|
|
ssize_t res = 0;
|
|
{
|
|
CurrentMetrics::Increment metric_increment{CurrentMetrics::Write};
|
|
res = ::write(fd, working_buffer.begin() + bytes_written, offset() - bytes_written);
|
|
}
|
|
|
|
if ((-1 == res || 0 == res) && errno != EINTR)
|
|
{
|
|
ProfileEvents::increment(ProfileEvents::WriteBufferFromFileDescriptorWriteFailed);
|
|
throwFromErrnoWithPath("Cannot write to file " + getFileName(), getFileName(),
|
|
ErrorCodes::CANNOT_WRITE_TO_FILE_DESCRIPTOR);
|
|
}
|
|
|
|
if (res > 0)
|
|
bytes_written += res;
|
|
}
|
|
|
|
ProfileEvents::increment(ProfileEvents::DiskWriteElapsedMicroseconds, watch.elapsedMicroseconds());
|
|
ProfileEvents::increment(ProfileEvents::WriteBufferFromFileDescriptorWriteBytes, bytes_written);
|
|
}
|
|
|
|
|
|
/// Name or some description of file.
|
|
std::string WriteBufferFromFileDescriptor::getFileName() const
|
|
{
|
|
return "(fd = " + toString(fd) + ")";
|
|
}
|
|
|
|
|
|
WriteBufferFromFileDescriptor::WriteBufferFromFileDescriptor(
|
|
int fd_,
|
|
size_t buf_size,
|
|
char * existing_memory,
|
|
size_t alignment)
|
|
: WriteBufferFromFileBase(buf_size, existing_memory, alignment), fd(fd_) {}
|
|
|
|
|
|
WriteBufferFromFileDescriptor::~WriteBufferFromFileDescriptor()
|
|
{
|
|
if (fd < 0)
|
|
{
|
|
assert(!offset() && "attempt to write after close");
|
|
return;
|
|
}
|
|
|
|
/// FIXME move final flush into the caller
|
|
MemoryTracker::LockExceptionInThread lock;
|
|
next();
|
|
}
|
|
|
|
|
|
void WriteBufferFromFileDescriptor::sync()
|
|
{
|
|
/// If buffer has pending data - write it.
|
|
next();
|
|
|
|
/// Request OS to sync data with storage medium.
|
|
int res = fsync(fd);
|
|
if (-1 == res)
|
|
throwFromErrnoWithPath("Cannot fsync " + getFileName(), getFileName(), ErrorCodes::CANNOT_FSYNC);
|
|
}
|
|
|
|
|
|
off_t WriteBufferFromFileDescriptor::seek(off_t offset, int whence)
|
|
{
|
|
off_t res = lseek(fd, offset, whence);
|
|
if (-1 == res)
|
|
throwFromErrnoWithPath("Cannot seek through file " + getFileName(), getFileName(),
|
|
ErrorCodes::CANNOT_SEEK_THROUGH_FILE);
|
|
return res;
|
|
}
|
|
|
|
|
|
void WriteBufferFromFileDescriptor::truncate(off_t length)
|
|
{
|
|
int res = ftruncate(fd, length);
|
|
if (-1 == res)
|
|
throwFromErrnoWithPath("Cannot truncate file " + getFileName(), getFileName(), ErrorCodes::CANNOT_TRUNCATE_FILE);
|
|
}
|
|
|
|
|
|
off_t WriteBufferFromFileDescriptor::size()
|
|
{
|
|
struct stat buf;
|
|
int res = fstat(fd, &buf);
|
|
if (-1 == res)
|
|
throwFromErrnoWithPath("Cannot execute fstat " + getFileName(), getFileName(), ErrorCodes::CANNOT_FSTAT);
|
|
return buf.st_size;
|
|
}
|
|
|
|
}
|