2018-03-23 16:05:14 +00:00
|
|
|
#include <port/unistd.h>
|
2016-10-25 06:49:24 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/ProfileEvents.h>
|
|
|
|
#include <Common/CurrentMetrics.h>
|
2016-10-25 06:49:24 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBufferFromFileDescriptor.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2018-05-28 19:53:03 +00:00
|
|
|
#include <Common/Stopwatch.h>
|
2016-10-25 06:49:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Event WriteBufferFromFileDescriptorWrite;
|
|
|
|
extern const Event WriteBufferFromFileDescriptorWriteFailed;
|
|
|
|
extern const Event WriteBufferFromFileDescriptorWriteBytes;
|
2018-05-28 19:53:03 +00:00
|
|
|
extern const Event DiskWriteElapsedMicroseconds;
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Metric Write;
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
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;
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WriteBufferFromFileDescriptor::nextImpl()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!offset())
|
|
|
|
return;
|
|
|
|
|
2018-05-31 15:54:08 +00:00
|
|
|
Stopwatch watch;
|
2018-05-28 19:53:03 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
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);
|
|
|
|
throwFromErrno("Cannot write to file " + getFileName(), ErrorCodes::CANNOT_WRITE_TO_FILE_DESCRIPTOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res > 0)
|
|
|
|
bytes_written += res;
|
|
|
|
}
|
|
|
|
|
2018-05-31 15:54:08 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::DiskWriteElapsedMicroseconds, watch.elapsedMicroseconds());
|
2017-04-01 07:20:54 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::WriteBufferFromFileDescriptorWriteBytes, bytes_written);
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Name or some description of file.
|
|
|
|
std::string WriteBufferFromFileDescriptor::getFileName() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return "(fd = " + toString(fd) + ")";
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WriteBufferFromFileDescriptor::WriteBufferFromFileDescriptor(
|
2017-04-01 07:20:54 +00:00
|
|
|
int fd_,
|
|
|
|
size_t buf_size,
|
|
|
|
char * existing_memory,
|
|
|
|
size_t alignment)
|
|
|
|
: WriteBufferFromFileBase(buf_size, existing_memory, alignment), fd(fd_) {}
|
2016-10-25 06:49:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
WriteBufferFromFileDescriptor::~WriteBufferFromFileDescriptor()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (fd >= 0)
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
off_t WriteBufferFromFileDescriptor::getPositionInFile()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return seek(0, SEEK_CUR);
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WriteBufferFromFileDescriptor::sync()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// If buffer has pending data - write it.
|
|
|
|
next();
|
2016-10-25 06:49:24 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Request OS to sync data with storage medium.
|
|
|
|
int res = fsync(fd);
|
|
|
|
if (-1 == res)
|
|
|
|
throwFromErrno("Cannot fsync " + getFileName(), ErrorCodes::CANNOT_FSYNC);
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
off_t WriteBufferFromFileDescriptor::doSeek(off_t offset, int whence)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
off_t res = lseek(fd, offset, whence);
|
|
|
|
if (-1 == res)
|
|
|
|
throwFromErrno("Cannot seek through file " + getFileName(), ErrorCodes::CANNOT_SEEK_THROUGH_FILE);
|
|
|
|
return res;
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WriteBufferFromFileDescriptor::doTruncate(off_t length)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
int res = ftruncate(fd, length);
|
|
|
|
if (-1 == res)
|
|
|
|
throwFromErrno("Cannot truncate file " + getFileName(), ErrorCodes::CANNOT_TRUNCATE_FILE);
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|