ClickHouse/src/IO/WriteBufferFromFileDescriptor.cpp

160 lines
4.3 KiB
C++
Raw Normal View History

2020-02-17 14:27:09 +00:00
#include <unistd.h>
2016-10-25 06:49:24 +00:00
#include <errno.h>
#include <cassert>
2020-07-07 11:45:20 +00:00
#include <sys/types.h>
#include <sys/stat.h>
2016-10-25 06:49:24 +00:00
#include <Common/Exception.h>
#include <Common/ProfileEvents.h>
#include <Common/CurrentMetrics.h>
2019-02-10 17:40:52 +00:00
#include <Common/Stopwatch.h>
#include <Common/MemoryTracker.h>
2016-10-25 06:49:24 +00:00
#include <IO/WriteBufferFromFileDescriptor.h>
#include <IO/WriteHelpers.h>
2016-10-25 06:49:24 +00:00
namespace ProfileEvents
{
extern const Event WriteBufferFromFileDescriptorWrite;
extern const Event WriteBufferFromFileDescriptorWriteFailed;
extern const Event WriteBufferFromFileDescriptorWriteBytes;
extern const Event DiskWriteElapsedMicroseconds;
2016-10-25 06:49:24 +00:00
}
namespace CurrentMetrics
{
extern const Metric Write;
2016-10-25 06:49:24 +00:00
}
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;
2020-07-07 11:45:20 +00:00
extern const int CANNOT_FSTAT;
2016-10-25 06:49:24 +00:00
}
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);
/// Don't use getFileName() here because this method can be called from destructor
2021-09-06 10:35:37 +00:00
String error_file_name = file_name;
if (error_file_name.empty())
error_file_name = "(fd = " + toString(fd) + ")";
throwFromErrnoWithPath("Cannot write to file " + error_file_name, error_file_name,
2019-08-07 12:52:47 +00:00
ErrorCodes::CANNOT_WRITE_TO_FILE_DESCRIPTOR);
}
if (res > 0)
bytes_written += res;
}
ProfileEvents::increment(ProfileEvents::DiskWriteElapsedMicroseconds, watch.elapsedMicroseconds());
ProfileEvents::increment(ProfileEvents::WriteBufferFromFileDescriptorWriteBytes, bytes_written);
2016-10-25 06:49:24 +00:00
}
2021-09-06 10:35:37 +00:00
/// NOTE: This class can be used as a very low-level building block, for example
/// in trace collector. In such places allocations of memory can be dangerous,
2021-09-06 11:34:17 +00:00
/// so don't allocate anything in this constructor.
2016-10-25 06:49:24 +00:00
WriteBufferFromFileDescriptor::WriteBufferFromFileDescriptor(
int fd_,
size_t buf_size,
char * existing_memory,
size_t alignment,
2021-09-06 10:35:37 +00:00
std::string file_name_)
: WriteBufferFromFileBase(buf_size, existing_memory, alignment)
, fd(fd_)
2021-09-06 10:35:37 +00:00
, file_name(std::move(file_name_))
{
}
2016-10-25 06:49:24 +00:00
WriteBufferFromFileDescriptor::~WriteBufferFromFileDescriptor()
{
if (fd < 0)
{
assert(!offset() && "attempt to write after close");
return;
}
/// FIXME move final flush into the caller
MemoryTracker::LockExceptionInThread lock(VariableContext::Global);
next();
2016-10-25 06:49:24 +00:00
}
void WriteBufferFromFileDescriptor::sync()
{
/// If buffer has pending data - write it.
next();
2016-10-25 06:49:24 +00:00
/// Request OS to sync data with storage medium.
int res = fsync(fd);
if (-1 == res)
2019-08-07 12:52:47 +00:00
throwFromErrnoWithPath("Cannot fsync " + getFileName(), getFileName(), ErrorCodes::CANNOT_FSYNC);
2016-10-25 06:49:24 +00:00
}
off_t WriteBufferFromFileDescriptor::seek(off_t offset, int whence) // NOLINT
2016-10-25 06:49:24 +00:00
{
off_t res = lseek(fd, offset, whence);
if (-1 == res)
2019-08-07 12:52:47 +00:00
throwFromErrnoWithPath("Cannot seek through file " + getFileName(), getFileName(),
ErrorCodes::CANNOT_SEEK_THROUGH_FILE);
return res;
2016-10-25 06:49:24 +00:00
}
void WriteBufferFromFileDescriptor::truncate(off_t length) // NOLINT
2016-10-25 06:49:24 +00:00
{
int res = ftruncate(fd, length);
if (-1 == res)
2019-08-07 12:52:47 +00:00
throwFromErrnoWithPath("Cannot truncate file " + getFileName(), getFileName(), ErrorCodes::CANNOT_TRUNCATE_FILE);
2016-10-25 06:49:24 +00:00
}
2020-07-07 11:45:20 +00:00
off_t WriteBufferFromFileDescriptor::size() const
2020-07-07 11:45:20 +00:00
{
struct stat buf;
int res = fstat(fd, &buf);
if (-1 == res)
throwFromErrnoWithPath("Cannot execute fstat " + getFileName(), getFileName(), ErrorCodes::CANNOT_FSTAT);
return buf.st_size;
}
2021-09-06 10:35:37 +00:00
std::string WriteBufferFromFileDescriptor::getFileName() const
{
if (file_name.empty())
return "(fd = " + toString(fd) + ")";
return file_name;
}
2016-10-25 06:49:24 +00:00
}