2015-03-04 10:47:53 +00:00
|
|
|
|
#include <DB/IO/WriteBufferAIO.h>
|
|
|
|
|
#include <DB/Common/ProfileEvents.h>
|
|
|
|
|
#include <DB/Core/ErrorCodes.h>
|
|
|
|
|
|
2015-03-11 17:27:34 +00:00
|
|
|
|
#include <limits>
|
2015-03-04 10:47:53 +00:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2015-04-07 10:49:48 +00:00
|
|
|
|
/// Примечание: выделяется дополнительная страница, которая содежрит те данные, которые
|
|
|
|
|
/// не влезают в основной буфер.
|
2015-03-04 10:47:53 +00:00
|
|
|
|
WriteBufferAIO::WriteBufferAIO(const std::string & filename_, size_t buffer_size_, int flags_, mode_t mode_,
|
|
|
|
|
char * existing_memory_)
|
2015-04-07 10:49:48 +00:00
|
|
|
|
: WriteBufferFromFileBase(buffer_size_ + DEFAULT_AIO_FILE_BLOCK_SIZE, existing_memory_, DEFAULT_AIO_FILE_BLOCK_SIZE),
|
2015-04-07 00:04:36 +00:00
|
|
|
|
flush_buffer(BufferWithOwnMemory<WriteBuffer>(this->memory.size(), nullptr, DEFAULT_AIO_FILE_BLOCK_SIZE)),
|
2015-03-16 10:49:27 +00:00
|
|
|
|
filename(filename_)
|
2015-03-04 10:47:53 +00:00
|
|
|
|
{
|
2015-04-07 11:43:23 +00:00
|
|
|
|
/// Исправить информацию о размере буферов, чтобы дополнительные страницы не касались базового класса BufferBase.
|
2015-04-07 10:49:48 +00:00
|
|
|
|
this->buffer().resize(this->buffer().size() - DEFAULT_AIO_FILE_BLOCK_SIZE);
|
|
|
|
|
this->internalBuffer().resize(this->internalBuffer().size() - DEFAULT_AIO_FILE_BLOCK_SIZE);
|
|
|
|
|
flush_buffer.buffer().resize(this->buffer().size() - DEFAULT_AIO_FILE_BLOCK_SIZE);
|
|
|
|
|
flush_buffer.internalBuffer().resize(this->internalBuffer().size() - DEFAULT_AIO_FILE_BLOCK_SIZE);
|
|
|
|
|
|
2015-03-04 10:47:53 +00:00
|
|
|
|
ProfileEvents::increment(ProfileEvents::FileOpen);
|
|
|
|
|
|
2015-03-24 11:03:26 +00:00
|
|
|
|
int open_flags = (flags_ == -1) ? (O_WRONLY | O_TRUNC | O_CREAT) : flags_;
|
2015-03-04 10:47:53 +00:00
|
|
|
|
open_flags |= O_DIRECT;
|
|
|
|
|
|
2015-03-06 10:29:58 +00:00
|
|
|
|
fd = ::open(filename.c_str(), open_flags, mode_);
|
2015-03-04 10:47:53 +00:00
|
|
|
|
if (fd == -1)
|
2015-03-05 11:57:54 +00:00
|
|
|
|
{
|
2015-03-11 11:31:09 +00:00
|
|
|
|
auto error_code = (errno == ENOENT) ? ErrorCodes::FILE_DOESNT_EXIST : ErrorCodes::CANNOT_OPEN_FILE;
|
|
|
|
|
throwFromErrno("Cannot open file " + filename, error_code);
|
2015-03-05 11:57:54 +00:00
|
|
|
|
}
|
2015-03-11 13:40:15 +00:00
|
|
|
|
|
2015-03-25 14:58:23 +00:00
|
|
|
|
ProfileEvents::increment(ProfileEvents::FileOpen);
|
|
|
|
|
|
2015-03-24 11:03:26 +00:00
|
|
|
|
fd2 = ::open(filename.c_str(), O_RDONLY, mode_);
|
|
|
|
|
if (fd2 == -1)
|
|
|
|
|
{
|
|
|
|
|
auto error_code = (errno == ENOENT) ? ErrorCodes::FILE_DOESNT_EXIST : ErrorCodes::CANNOT_OPEN_FILE;
|
|
|
|
|
throwFromErrno("Cannot open file " + filename, error_code);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 13:40:15 +00:00
|
|
|
|
::memset(&request, 0, sizeof(request));
|
2015-03-04 10:47:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WriteBufferAIO::~WriteBufferAIO()
|
|
|
|
|
{
|
2015-04-07 12:08:08 +00:00
|
|
|
|
if (!aio_failed)
|
2015-03-04 10:47:53 +00:00
|
|
|
|
{
|
2015-03-05 11:57:54 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2015-03-13 11:46:04 +00:00
|
|
|
|
flush();
|
2015-03-05 11:57:54 +00:00
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
|
}
|
2015-03-04 10:47:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 11:57:54 +00:00
|
|
|
|
if (fd != -1)
|
|
|
|
|
::close(fd);
|
2015-03-25 14:15:50 +00:00
|
|
|
|
if (fd2 != -1)
|
|
|
|
|
::close(fd2);
|
2015-03-04 10:47:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
|
off_t WriteBufferAIO::getPositionInFile()
|
|
|
|
|
{
|
|
|
|
|
return seek(0, SEEK_CUR);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 14:21:55 +00:00
|
|
|
|
void WriteBufferAIO::truncate(off_t length)
|
|
|
|
|
{
|
2015-03-13 11:46:04 +00:00
|
|
|
|
flush();
|
2015-03-05 14:21:55 +00:00
|
|
|
|
|
|
|
|
|
int res = ::ftruncate(fd, length);
|
|
|
|
|
if (res == -1)
|
2015-03-06 10:29:58 +00:00
|
|
|
|
throwFromErrno("Cannot truncate file " + filename, ErrorCodes::CANNOT_TRUNCATE_FILE);
|
2015-03-05 14:21:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-10 16:34:00 +00:00
|
|
|
|
void WriteBufferAIO::sync()
|
2015-03-04 10:47:53 +00:00
|
|
|
|
{
|
2015-03-13 11:46:04 +00:00
|
|
|
|
flush();
|
2015-03-10 17:14:07 +00:00
|
|
|
|
|
|
|
|
|
/// Попросим ОС сбросить данные на диск.
|
|
|
|
|
int res = ::fsync(fd);
|
|
|
|
|
if (res == -1)
|
|
|
|
|
throwFromErrno("Cannot fsync " + getFileName(), ErrorCodes::CANNOT_FSYNC);
|
2015-03-04 10:47:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WriteBufferAIO::nextImpl()
|
|
|
|
|
{
|
|
|
|
|
if (!offset())
|
|
|
|
|
return;
|
|
|
|
|
|
2015-04-06 23:21:20 +00:00
|
|
|
|
if (waitForAIOCompletion())
|
|
|
|
|
finalize();
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
2015-04-06 22:33:52 +00:00
|
|
|
|
/// Создать запрос на асинхронную запись.
|
2015-04-07 12:36:51 +00:00
|
|
|
|
prepare();
|
2015-04-06 22:33:52 +00:00
|
|
|
|
|
2015-04-07 11:43:23 +00:00
|
|
|
|
request.aio_lio_opcode = IOCB_CMD_PWRITE;
|
2015-04-06 22:33:52 +00:00
|
|
|
|
request.aio_fildes = fd;
|
2015-04-07 11:43:23 +00:00
|
|
|
|
request.aio_buf = reinterpret_cast<UInt64>(buffer_begin);
|
|
|
|
|
request.aio_nbytes = region_aligned_size;
|
2015-04-06 22:33:52 +00:00
|
|
|
|
request.aio_offset = region_aligned_begin;
|
|
|
|
|
|
|
|
|
|
/// Отправить запрос.
|
|
|
|
|
while (io_submit(aio_context.ctx, request_ptrs.size(), &request_ptrs[0]) < 0)
|
|
|
|
|
{
|
|
|
|
|
if (errno != EINTR)
|
|
|
|
|
{
|
2015-04-07 12:08:08 +00:00
|
|
|
|
aio_failed = true;
|
2015-04-06 22:33:52 +00:00
|
|
|
|
throw Exception("Cannot submit request for asynchronous IO on file " + filename, ErrorCodes::AIO_SUBMIT_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
is_pending_write = true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-07 00:01:36 +00:00
|
|
|
|
off_t WriteBufferAIO::doSeek(off_t off, int whence)
|
|
|
|
|
{
|
|
|
|
|
flush();
|
|
|
|
|
|
|
|
|
|
if (whence == SEEK_SET)
|
|
|
|
|
{
|
|
|
|
|
if (off < 0)
|
|
|
|
|
throw Exception("SEEK_SET underflow", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
|
pos_in_file = off;
|
|
|
|
|
}
|
|
|
|
|
else if (whence == SEEK_CUR)
|
|
|
|
|
{
|
|
|
|
|
if (off >= 0)
|
|
|
|
|
{
|
|
|
|
|
if (off > (std::numeric_limits<off_t>::max() - pos_in_file))
|
|
|
|
|
throw Exception("SEEK_CUR overflow", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
|
}
|
|
|
|
|
else if (off < -pos_in_file)
|
|
|
|
|
throw Exception("SEEK_CUR underflow", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
|
pos_in_file += off;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception("WriteBufferAIO::seek expects SEEK_SET or SEEK_CUR as whence", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
|
|
|
|
|
|
if (pos_in_file > max_pos_in_file)
|
|
|
|
|
max_pos_in_file = pos_in_file;
|
|
|
|
|
|
|
|
|
|
return pos_in_file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WriteBufferAIO::flush()
|
|
|
|
|
{
|
|
|
|
|
next();
|
|
|
|
|
if (waitForAIOCompletion())
|
|
|
|
|
finalize();
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-06 23:21:20 +00:00
|
|
|
|
bool WriteBufferAIO::waitForAIOCompletion()
|
2015-04-06 22:33:52 +00:00
|
|
|
|
{
|
|
|
|
|
if (!is_pending_write)
|
2015-04-06 23:21:20 +00:00
|
|
|
|
return false;
|
2015-04-06 22:33:52 +00:00
|
|
|
|
|
|
|
|
|
while (io_getevents(aio_context.ctx, events.size(), events.size(), &events[0], nullptr) < 0)
|
|
|
|
|
{
|
|
|
|
|
if (errno != EINTR)
|
|
|
|
|
{
|
2015-04-07 12:08:08 +00:00
|
|
|
|
aio_failed = true;
|
2015-04-06 22:33:52 +00:00
|
|
|
|
throw Exception("Failed to wait for asynchronous IO completion on file " + filename, ErrorCodes::AIO_COMPLETION_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
is_pending_write = false;
|
|
|
|
|
bytes_written = events[0].res;
|
|
|
|
|
|
2015-04-06 23:21:20 +00:00
|
|
|
|
return true;
|
2015-04-06 22:33:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-07 00:01:36 +00:00
|
|
|
|
void WriteBufferAIO::prepare()
|
2015-04-06 22:33:52 +00:00
|
|
|
|
{
|
2015-04-07 00:01:36 +00:00
|
|
|
|
/// Менять местами основной и дублирующий буферы.
|
2015-04-06 22:33:52 +00:00
|
|
|
|
buffer().swap(flush_buffer.buffer());
|
|
|
|
|
std::swap(position(), flush_buffer.position());
|
|
|
|
|
|
2015-03-25 16:44:30 +00:00
|
|
|
|
truncation_count = 0;
|
2015-03-20 16:58:57 +00:00
|
|
|
|
|
2015-03-30 11:42:10 +00:00
|
|
|
|
/*
|
|
|
|
|
Страница на диске или в памяти
|
|
|
|
|
|
|
|
|
|
начальный адрес (начальная позиция в случае диска) кратен DEFAULT_AIO_FILE_BLOCK_SIZE
|
|
|
|
|
:
|
|
|
|
|
:
|
|
|
|
|
+---------------+
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
|
+---------------+
|
|
|
|
|
<--------------->
|
|
|
|
|
:
|
|
|
|
|
:
|
|
|
|
|
DEFAULT_AIO_FILE_BLOCK_SIZE
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
2015-03-30 08:55:40 +00:00
|
|
|
|
/*
|
|
|
|
|
Представление данных на диске
|
|
|
|
|
|
|
|
|
|
XXX : данные, которые хотим записать
|
|
|
|
|
ZZZ : данные, которые уже на диске или нули, если отсутствуют данные
|
|
|
|
|
|
|
|
|
|
region_aligned_begin region_aligned_end
|
|
|
|
|
: region_begin region_end :
|
|
|
|
|
: : : :
|
|
|
|
|
: : : :
|
|
|
|
|
+---:-----------+---------------+---------------+---------------+--:------------+
|
|
|
|
|
| : | | | | : |
|
|
|
|
|
| +-----------+---------------+---------------+---------------+--+ |
|
|
|
|
|
|ZZZ|XXXXXXXXXXX|XXXXXXXXXXXXXXX|XXXXXXXXXXXXXXX|XXXXXXXXXXXXXXX|XX|ZZZZZZZZZZZZ|
|
|
|
|
|
|ZZZ|XXXXXXXXXXX|XXXXXXXXXXXXXXX|XXXXXXXXXXXXXXX|XXXXXXXXXXXXXXX|XX|ZZZZZZZZZZZZ|
|
|
|
|
|
| +-----------+---------------+---------------+---------------+--+ |
|
|
|
|
|
| | | | | |
|
|
|
|
|
+---------------+---------------+---------------+---------------+---------------+
|
|
|
|
|
|
|
|
|
|
<--><--------------------------------------------------------------><----------->
|
|
|
|
|
: : :
|
|
|
|
|
: : :
|
|
|
|
|
region_left_padding region_size region_right_padding
|
|
|
|
|
|
|
|
|
|
<------------------------------------------------------------------------------->
|
|
|
|
|
:
|
|
|
|
|
:
|
|
|
|
|
region_aligned_size
|
|
|
|
|
*/
|
2015-03-27 16:53:01 +00:00
|
|
|
|
|
2015-03-25 14:14:06 +00:00
|
|
|
|
/// Регион диска, в который хотим записать данные.
|
2015-03-25 15:30:48 +00:00
|
|
|
|
const off_t region_begin = pos_in_file;
|
2015-04-07 12:36:51 +00:00
|
|
|
|
|
|
|
|
|
if ((flush_buffer.offset() > std::numeric_limits<off_t>::max()) ||
|
|
|
|
|
(pos_in_file > (std::numeric_limits<off_t>::max() - static_cast<off_t>(flush_buffer.offset()))))
|
|
|
|
|
throw Exception("An overflow occurred during file operation", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
2015-03-25 15:30:48 +00:00
|
|
|
|
const off_t region_end = pos_in_file + flush_buffer.offset();
|
|
|
|
|
const size_t region_size = region_end - region_begin;
|
2015-03-19 10:06:11 +00:00
|
|
|
|
|
2015-03-30 09:59:16 +00:00
|
|
|
|
/// Выровненный регион диска, в который хотим записать данные.
|
2015-03-25 15:30:48 +00:00
|
|
|
|
const size_t region_left_padding = region_begin % DEFAULT_AIO_FILE_BLOCK_SIZE;
|
|
|
|
|
const size_t region_right_padding = (DEFAULT_AIO_FILE_BLOCK_SIZE - (region_end % DEFAULT_AIO_FILE_BLOCK_SIZE)) % DEFAULT_AIO_FILE_BLOCK_SIZE;
|
2015-03-19 10:06:11 +00:00
|
|
|
|
|
2015-04-06 22:33:52 +00:00
|
|
|
|
region_aligned_begin = region_begin - region_left_padding;
|
2015-04-07 12:36:51 +00:00
|
|
|
|
|
|
|
|
|
if (region_end > (std::numeric_limits<off_t>::max() - static_cast<off_t>(region_right_padding)))
|
|
|
|
|
throw Exception("An overflow occurred during file operation", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
2015-03-25 15:30:48 +00:00
|
|
|
|
const off_t region_aligned_end = region_end + region_right_padding;
|
2015-04-06 22:33:52 +00:00
|
|
|
|
region_aligned_size = region_aligned_end - region_aligned_begin;
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
2015-04-07 11:43:23 +00:00
|
|
|
|
bytes_to_write = region_aligned_size;
|
|
|
|
|
|
2015-03-27 16:53:01 +00:00
|
|
|
|
/*
|
2015-03-30 08:55:40 +00:00
|
|
|
|
Представление данных в буфере до обработки
|
|
|
|
|
|
2015-03-30 09:59:16 +00:00
|
|
|
|
XXX : данные, которые хотим записать
|
|
|
|
|
|
2015-03-30 08:55:40 +00:00
|
|
|
|
buffer_begin buffer_end
|
|
|
|
|
: :
|
|
|
|
|
: :
|
|
|
|
|
+---------------+---------------+---------------+-------------:-+
|
|
|
|
|
| | | | : |
|
|
|
|
|
+---------------+---------------+---------------+-------------+ |
|
|
|
|
|
|XXXXXXXXXXXXXXX|XXXXXXXXXXXXXXX|XXXXXXXXXXXXXXX|XXXXXXXXXXXXX| |
|
|
|
|
|
|XXXXXXXXXXXXXXX|XXXXXXXXXXXXXXX|XXXXXXXXXXXXXXX|XXXXXXXXXXXXX| |
|
|
|
|
|
+---------------+---------------+---------------+-------------+ |
|
|
|
|
|
| | | | |
|
|
|
|
|
+---------------+---------------+---------------+---------------+
|
|
|
|
|
|
|
|
|
|
<------------------------------------------------------------->
|
|
|
|
|
:
|
|
|
|
|
:
|
|
|
|
|
buffer_size
|
|
|
|
|
*/
|
2015-03-27 16:53:01 +00:00
|
|
|
|
|
2015-03-24 11:03:26 +00:00
|
|
|
|
/// Буфер данных, которые хотим записать на диск.
|
2015-04-06 22:33:52 +00:00
|
|
|
|
buffer_begin = flush_buffer.buffer().begin();
|
2015-03-24 11:03:26 +00:00
|
|
|
|
Position buffer_end = buffer_begin + region_size;
|
|
|
|
|
size_t buffer_size = buffer_end - buffer_begin;
|
2015-03-19 10:06:11 +00:00
|
|
|
|
|
2015-03-25 14:17:51 +00:00
|
|
|
|
/// Обработать буфер, чтобы он отражал структуру региона диска.
|
2015-03-19 10:06:11 +00:00
|
|
|
|
|
2015-03-27 16:53:01 +00:00
|
|
|
|
/*
|
|
|
|
|
Представление данных в буфере после обработки
|
|
|
|
|
|
2015-03-30 09:59:16 +00:00
|
|
|
|
XXX : данные, которые хотим записать
|
|
|
|
|
ZZZ : данные из диска или нули, если отсутствуют данные
|
|
|
|
|
|
2015-04-07 11:43:23 +00:00
|
|
|
|
buffer_begin buffer_end дополнительная страница
|
2015-03-27 16:53:01 +00:00
|
|
|
|
: : :
|
|
|
|
|
: : :
|
|
|
|
|
+---:-----------+---------------+---------------+---------------+--:------------+
|
|
|
|
|
| | | | | : |
|
|
|
|
|
| +-----------+---------------+---------------+---------------+--+ |
|
|
|
|
|
|ZZZ|XXXXXXXXXXX|XXXXXXXXXXXXXXX|XXXXXXXXXXXXXXX|XXXXXXXXXXXXXXX|XX|ZZZZZZZZZZZZ|
|
|
|
|
|
|ZZZ|XXXXXXXXXXX|XXXXXXXXXXXXXXX|XXXXXXXXXXXXXXX|XXXXXXXXXXXXXXX|XX|ZZZZZZZZZZZZ|
|
|
|
|
|
| +-----------+---------------+---------------+---------------+--+ |
|
|
|
|
|
| | | | | |
|
|
|
|
|
+---------------+---------------+---------------+---------------+---------------+
|
|
|
|
|
|
|
|
|
|
<--><--------------------------------------------------------------><----------->
|
|
|
|
|
: : :
|
|
|
|
|
: : :
|
|
|
|
|
region_left_padding region_size region_right_padding
|
|
|
|
|
|
|
|
|
|
<------------------------------------------------------------------------------->
|
2015-03-30 08:55:40 +00:00
|
|
|
|
:
|
|
|
|
|
:
|
|
|
|
|
region_aligned_size
|
2015-03-27 16:53:01 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2015-03-24 11:03:26 +00:00
|
|
|
|
if (region_left_padding > 0)
|
2015-03-06 10:29:58 +00:00
|
|
|
|
{
|
2015-03-30 09:59:16 +00:00
|
|
|
|
/// Сдвинуть данные буфера вправо. Дополнить начало буфера данными из диска.
|
2015-04-07 10:49:48 +00:00
|
|
|
|
buffer_size += region_left_padding;
|
2015-03-25 15:30:48 +00:00
|
|
|
|
buffer_end = buffer_begin + buffer_size;
|
2015-03-24 11:03:26 +00:00
|
|
|
|
|
|
|
|
|
::memmove(buffer_begin + region_left_padding, buffer_begin, buffer_size - region_left_padding);
|
|
|
|
|
|
|
|
|
|
ssize_t read_count = ::pread(fd2, buffer_begin, region_left_padding, region_aligned_begin);
|
2015-03-19 10:06:11 +00:00
|
|
|
|
if (read_count < 0)
|
2015-03-20 16:58:57 +00:00
|
|
|
|
throw Exception("Read error", ErrorCodes::AIO_READ_ERROR);
|
2015-03-25 14:44:55 +00:00
|
|
|
|
|
|
|
|
|
::memset(buffer_begin + read_count, 0, region_left_padding - read_count);
|
2015-03-19 10:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-24 11:03:26 +00:00
|
|
|
|
if (region_right_padding > 0)
|
2015-03-19 10:06:11 +00:00
|
|
|
|
{
|
2015-04-07 11:43:23 +00:00
|
|
|
|
/// Дополнить конец буфера данными из диска.
|
|
|
|
|
ssize_t read_count = ::pread(fd2, buffer_end, region_right_padding, region_end);
|
2015-03-24 11:03:26 +00:00
|
|
|
|
if (read_count < 0)
|
2015-03-25 14:14:06 +00:00
|
|
|
|
throw Exception("Read error", ErrorCodes::AIO_READ_ERROR);
|
2015-03-25 14:44:55 +00:00
|
|
|
|
|
2015-03-25 16:44:30 +00:00
|
|
|
|
truncation_count = region_right_padding - read_count;
|
2015-04-07 11:43:23 +00:00
|
|
|
|
::memset(buffer_end + read_count, 0, truncation_count);
|
2015-03-19 10:06:11 +00:00
|
|
|
|
}
|
2015-03-04 10:47:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-06 22:33:52 +00:00
|
|
|
|
void WriteBufferAIO::finalize()
|
2015-03-04 10:47:53 +00:00
|
|
|
|
{
|
2015-04-06 23:21:20 +00:00
|
|
|
|
if (bytes_written < bytes_to_write)
|
|
|
|
|
throw Exception("Asynchronous write error on file " + filename, ErrorCodes::AIO_WRITE_ERROR);
|
|
|
|
|
|
2015-03-25 16:44:30 +00:00
|
|
|
|
bytes_written -= truncation_count;
|
2015-03-24 11:03:26 +00:00
|
|
|
|
|
2015-03-25 16:44:30 +00:00
|
|
|
|
off_t pos_offset = bytes_written - (pos_in_file - request.aio_offset);
|
|
|
|
|
if (pos_in_file > (std::numeric_limits<off_t>::max() - pos_offset))
|
2015-04-07 12:36:51 +00:00
|
|
|
|
throw Exception("An overflow occurred during file operation", ErrorCodes::LOGICAL_ERROR);
|
2015-03-25 16:44:30 +00:00
|
|
|
|
pos_in_file += pos_offset;
|
2015-03-24 11:03:26 +00:00
|
|
|
|
|
2015-03-25 16:44:30 +00:00
|
|
|
|
if (pos_in_file > max_pos_in_file)
|
|
|
|
|
max_pos_in_file = pos_in_file;
|
2015-03-23 09:43:06 +00:00
|
|
|
|
|
2015-03-25 16:44:30 +00:00
|
|
|
|
if (truncation_count > 0)
|
|
|
|
|
{
|
|
|
|
|
/// Укоротить файл, чтобы удалить из него излишние нули.
|
|
|
|
|
int res = ::ftruncate(fd, max_pos_in_file);
|
|
|
|
|
if (res == -1)
|
|
|
|
|
throwFromErrno("Cannot truncate file " + filename, ErrorCodes::CANNOT_TRUNCATE_FILE);
|
2015-03-04 10:47:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|