2015-03-04 10:47:53 +00:00
|
|
|
#include <DB/IO/ReadBufferAIO.h>
|
|
|
|
#include <DB/Common/ProfileEvents.h>
|
|
|
|
#include <DB/Core/ErrorCodes.h>
|
2015-03-13 11:02:43 +00:00
|
|
|
#include <DB/Core/Defines.h>
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2015-04-04 22:24:41 +00:00
|
|
|
/// Выделяем дополнительную страницу. Содежрит те данные, которые
|
|
|
|
/// не влезают в основной буфер.
|
2015-03-30 22:10:59 +00:00
|
|
|
ReadBufferAIO::ReadBufferAIO(const std::string & filename_, size_t buffer_size_, int flags_,
|
2015-03-04 10:47:53 +00:00
|
|
|
char * existing_memory_)
|
2015-04-04 22:24:41 +00:00
|
|
|
: ReadBufferFromFileBase(buffer_size_ + DEFAULT_AIO_FILE_BLOCK_SIZE, existing_memory_, DEFAULT_AIO_FILE_BLOCK_SIZE),
|
|
|
|
fill_buffer(BufferWithOwnMemory<ReadBuffer>(buffer_size_ + DEFAULT_AIO_FILE_BLOCK_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
|
|
|
{
|
|
|
|
ProfileEvents::increment(ProfileEvents::FileOpen);
|
|
|
|
|
2015-03-11 11:31:09 +00:00
|
|
|
int open_flags = (flags_ == -1) ? O_RDONLY : flags_;
|
2015-03-04 10:47:53 +00:00
|
|
|
open_flags |= O_DIRECT;
|
|
|
|
|
2015-03-30 22:10:59 +00:00
|
|
|
fd = ::open(filename.c_str(), open_flags);
|
2015-03-04 10:47:53 +00:00
|
|
|
if (fd == -1)
|
2015-03-05 11:57:54 +00:00
|
|
|
{
|
|
|
|
got_exception = true;
|
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
|
|
|
|
|
|
|
::memset(&request, 0, sizeof(request));
|
2015-03-04 10:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ReadBufferAIO::~ReadBufferAIO()
|
|
|
|
{
|
2015-03-05 11:57:54 +00:00
|
|
|
if (!got_exception)
|
2015-03-04 10:47:53 +00:00
|
|
|
{
|
2015-03-05 11:57:54 +00:00
|
|
|
try
|
|
|
|
{
|
2015-04-03 13:45:44 +00:00
|
|
|
sync();
|
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-04 10:47:53 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 16:20:15 +00:00
|
|
|
void ReadBufferAIO::setMaxBytes(size_t max_bytes_read_)
|
|
|
|
{
|
|
|
|
if (is_started)
|
|
|
|
{
|
|
|
|
got_exception = true;
|
2015-03-06 10:29:58 +00:00
|
|
|
throw Exception("Illegal attempt to set the maximum number of bytes to read from file " + filename, ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
2015-03-05 16:20:15 +00:00
|
|
|
max_bytes_read = max_bytes_read_;
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:54:16 +00:00
|
|
|
off_t ReadBufferAIO::getPositionInFile()
|
|
|
|
{
|
|
|
|
return seek(0, SEEK_CUR);
|
|
|
|
}
|
|
|
|
|
2015-04-03 13:45:44 +00:00
|
|
|
off_t ReadBufferAIO::doSeek(off_t off, int whence)
|
2015-03-05 14:21:55 +00:00
|
|
|
{
|
2015-03-12 12:36:18 +00:00
|
|
|
off_t new_pos;
|
|
|
|
|
|
|
|
if (whence == SEEK_SET)
|
2015-03-05 14:21:55 +00:00
|
|
|
{
|
2015-03-12 12:36:18 +00:00
|
|
|
if (off < 0)
|
|
|
|
{
|
|
|
|
got_exception = true;
|
|
|
|
throw Exception("SEEK_SET underflow", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
}
|
|
|
|
new_pos = off;
|
2015-03-05 14:21:55 +00:00
|
|
|
}
|
2015-03-12 12:36:18 +00:00
|
|
|
else if (whence == SEEK_CUR)
|
2015-03-05 14:21:55 +00:00
|
|
|
{
|
2015-03-12 12:36:18 +00:00
|
|
|
if (off >= 0)
|
|
|
|
{
|
2015-03-13 11:02:43 +00:00
|
|
|
if (off > (std::numeric_limits<off_t>::max() - getPositionInFileRelaxed()))
|
2015-03-12 12:36:18 +00:00
|
|
|
{
|
|
|
|
got_exception = true;
|
|
|
|
throw Exception("SEEK_CUR overflow", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
}
|
|
|
|
}
|
2015-03-13 11:02:43 +00:00
|
|
|
else if (off < -getPositionInFileRelaxed())
|
2015-03-12 12:36:18 +00:00
|
|
|
{
|
|
|
|
got_exception = true;
|
|
|
|
throw Exception("SEEK_CUR underflow", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
}
|
2015-03-13 11:02:43 +00:00
|
|
|
new_pos = getPositionInFileRelaxed() + off;
|
2015-03-05 14:21:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-12 12:36:18 +00:00
|
|
|
got_exception = true;
|
|
|
|
throw Exception("ReadBufferAIO::seek expects SEEK_SET or SEEK_CUR as whence", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
}
|
2015-03-05 14:21:55 +00:00
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
if (new_pos != getPositionInFileRelaxed())
|
2015-03-12 12:36:18 +00:00
|
|
|
{
|
|
|
|
off_t working_buffer_begin_pos = pos_in_file - static_cast<off_t>(working_buffer.size());
|
|
|
|
if (hasPendingData() && (new_pos >= working_buffer_begin_pos) && (new_pos <= pos_in_file))
|
2015-03-05 14:21:55 +00:00
|
|
|
{
|
2015-03-12 12:36:18 +00:00
|
|
|
/// Свдинулись, но остались в пределах буфера.
|
|
|
|
pos = working_buffer.begin() + (new_pos - working_buffer_begin_pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos = working_buffer.end();
|
|
|
|
pos_in_file = new_pos;
|
2015-03-05 14:21:55 +00:00
|
|
|
}
|
2015-04-03 22:44:32 +00:00
|
|
|
|
|
|
|
/// Сдвинулись, значит не можем использовать результат последнего асинхронного запроса.
|
2015-04-05 15:54:16 +00:00
|
|
|
skipPendingAIO();
|
2015-03-05 14:21:55 +00:00
|
|
|
}
|
2015-03-12 12:36:18 +00:00
|
|
|
|
|
|
|
return new_pos;
|
2015-03-05 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
off_t ReadBufferAIO::getPositionInFileRelaxed() const noexcept
|
|
|
|
{
|
2015-03-13 13:52:28 +00:00
|
|
|
return pos_in_file - (working_buffer.end() - pos);
|
2015-03-13 11:02:43 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 10:47:53 +00:00
|
|
|
bool ReadBufferAIO::nextImpl()
|
|
|
|
{
|
2015-03-11 13:15:19 +00:00
|
|
|
/// Если конец файла уже был достигнут при вызове этой функции,
|
|
|
|
/// то текущий вызов ошибочен.
|
2015-03-05 14:21:55 +00:00
|
|
|
if (is_eof)
|
2015-03-05 08:18:16 +00:00
|
|
|
return false;
|
|
|
|
|
2015-04-03 22:44:32 +00:00
|
|
|
if (!is_started)
|
|
|
|
{
|
|
|
|
synchronousRead();
|
|
|
|
is_started = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sync();
|
2015-03-04 10:47:53 +00:00
|
|
|
|
2015-03-11 13:15:19 +00:00
|
|
|
/// Если конец файла только что достигнут, больше ничего не делаем.
|
2015-03-10 11:43:30 +00:00
|
|
|
if (is_eof)
|
|
|
|
return true;
|
|
|
|
|
2015-04-04 22:24:41 +00:00
|
|
|
/// Создать асинхронный запрос.
|
2015-04-03 22:44:32 +00:00
|
|
|
initRequest();
|
|
|
|
|
2015-04-04 22:24:41 +00:00
|
|
|
request.aio_lio_opcode = IOCB_CMD_PREAD;
|
2015-04-03 22:44:32 +00:00
|
|
|
request.aio_fildes = fd;
|
2015-04-04 22:24:41 +00:00
|
|
|
request.aio_buf = reinterpret_cast<UInt64>(buffer_begin);
|
|
|
|
request.aio_nbytes = region_aligned_size;
|
2015-04-03 22:44:32 +00:00
|
|
|
request.aio_offset = region_aligned_begin;
|
|
|
|
|
|
|
|
/// Отправить запрос.
|
|
|
|
while (io_submit(aio_context.ctx, request_ptrs.size(), &request_ptrs[0]) < 0)
|
|
|
|
{
|
|
|
|
if (errno != EINTR)
|
|
|
|
{
|
|
|
|
got_exception = true;
|
|
|
|
throw Exception("Cannot submit request for asynchronous IO on file " + filename, ErrorCodes::AIO_SUBMIT_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
is_pending_read = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-04 22:24:41 +00:00
|
|
|
void ReadBufferAIO::synchronousRead()
|
|
|
|
{
|
|
|
|
initRequest();
|
|
|
|
bytes_read = ::pread(fd, buffer_begin, region_aligned_size, region_aligned_begin);
|
|
|
|
publishReceivedData();
|
|
|
|
swapBuffers();
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:54:16 +00:00
|
|
|
void ReadBufferAIO::sync()
|
|
|
|
{
|
|
|
|
if (!waitForAIOCompletion())
|
|
|
|
return;
|
|
|
|
publishReceivedData();
|
|
|
|
swapBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReadBufferAIO::skipPendingAIO()
|
|
|
|
{
|
|
|
|
if (!waitForAIOCompletion())
|
|
|
|
return;
|
|
|
|
|
|
|
|
is_started = false;
|
|
|
|
|
|
|
|
/// Несмотря на то, что не станем использовать результат последнего запроса,
|
|
|
|
/// убедимся, что запрос правильно выполнен.
|
|
|
|
|
|
|
|
bytes_read = events[0].res;
|
|
|
|
|
|
|
|
size_t region_left_padding = pos_in_file % DEFAULT_AIO_FILE_BLOCK_SIZE;
|
|
|
|
|
|
|
|
if ((bytes_read < 0) || (static_cast<size_t>(bytes_read) < region_left_padding))
|
|
|
|
{
|
|
|
|
got_exception = true;
|
|
|
|
throw Exception("Asynchronous read error on file " + filename, ErrorCodes::AIO_READ_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReadBufferAIO::waitForAIOCompletion()
|
|
|
|
{
|
|
|
|
if (is_eof || !is_pending_read)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (io_getevents(aio_context.ctx, events.size(), events.size(), &events[0], nullptr) < 0)
|
|
|
|
{
|
|
|
|
if (errno != EINTR)
|
|
|
|
{
|
|
|
|
got_exception = true;
|
|
|
|
throw Exception("Failed to wait for asynchronous IO completion on file " + filename, ErrorCodes::AIO_COMPLETION_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
is_pending_read = false;
|
|
|
|
bytes_read = events[0].res;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReadBufferAIO::swapBuffers() noexcept
|
|
|
|
{
|
|
|
|
internalBuffer().swap(fill_buffer.internalBuffer());
|
|
|
|
buffer().swap(fill_buffer.buffer());
|
|
|
|
std::swap(position(), fill_buffer.position());
|
|
|
|
}
|
|
|
|
|
2015-04-03 22:44:32 +00:00
|
|
|
void ReadBufferAIO::initRequest()
|
|
|
|
{
|
2015-03-17 13:57:24 +00:00
|
|
|
/// Количество запрашиваемых байтов.
|
2015-04-04 22:24:41 +00:00
|
|
|
requested_byte_count = std::min(fill_buffer.internalBuffer().size() - DEFAULT_AIO_FILE_BLOCK_SIZE, max_bytes_read);
|
2015-03-17 13:57:24 +00:00
|
|
|
|
2015-04-03 11:51:41 +00:00
|
|
|
/// Регион диска, из которого хотим читать данные.
|
|
|
|
const off_t region_begin = pos_in_file;
|
|
|
|
const off_t region_end = pos_in_file + requested_byte_count;
|
|
|
|
|
|
|
|
/// Выровненный регион диска, из которого хотим читать данные.
|
|
|
|
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-04-03 22:44:32 +00:00
|
|
|
region_aligned_begin = region_begin - region_left_padding;
|
2015-04-03 11:51:41 +00:00
|
|
|
const off_t region_aligned_end = region_end + region_right_padding;
|
2015-04-04 22:24:41 +00:00
|
|
|
region_aligned_size = region_aligned_end - region_aligned_begin;
|
2015-04-03 11:51:41 +00:00
|
|
|
|
|
|
|
/// Буфер, в который запишем данные из диска.
|
2015-04-04 22:24:41 +00:00
|
|
|
buffer_begin = fill_buffer.internalBuffer().begin();
|
2015-04-03 11:51:41 +00:00
|
|
|
}
|
|
|
|
|
2015-04-03 22:44:32 +00:00
|
|
|
void ReadBufferAIO::publishReceivedData()
|
2015-03-04 10:47:53 +00:00
|
|
|
{
|
2015-04-03 11:51:41 +00:00
|
|
|
size_t region_left_padding = pos_in_file % DEFAULT_AIO_FILE_BLOCK_SIZE;
|
2015-03-06 11:08:25 +00:00
|
|
|
|
2015-04-03 11:51:41 +00:00
|
|
|
if ((bytes_read < 0) || (static_cast<size_t>(bytes_read) < region_left_padding))
|
|
|
|
{
|
|
|
|
got_exception = true;
|
|
|
|
throw Exception("Asynchronous read error on file " + filename, ErrorCodes::AIO_READ_ERROR);
|
|
|
|
}
|
2015-03-17 12:44:49 +00:00
|
|
|
|
2015-04-03 11:51:41 +00:00
|
|
|
/// Игнорируем излишние байты слева.
|
|
|
|
bytes_read -= region_left_padding;
|
2015-03-17 11:30:23 +00:00
|
|
|
|
2015-04-03 11:51:41 +00:00
|
|
|
/// Игнорируем излишние байты справа.
|
|
|
|
bytes_read = std::min(bytes_read, static_cast<off_t>(requested_byte_count));
|
2015-03-17 11:30:23 +00:00
|
|
|
|
2015-04-03 11:51:41 +00:00
|
|
|
if (pos_in_file > (std::numeric_limits<off_t>::max() - bytes_read))
|
|
|
|
{
|
|
|
|
got_exception = true;
|
|
|
|
throw Exception("File position overflowed", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
2015-03-05 14:21:55 +00:00
|
|
|
|
2015-04-04 22:24:41 +00:00
|
|
|
::memmove(buffer_begin, buffer_begin + region_left_padding, bytes_read);
|
2015-04-03 11:51:41 +00:00
|
|
|
|
|
|
|
if (bytes_read > 0)
|
|
|
|
fill_buffer.buffer().resize(bytes_read);
|
|
|
|
if (static_cast<size_t>(bytes_read) < requested_byte_count)
|
|
|
|
is_eof = true;
|
|
|
|
|
|
|
|
pos_in_file += bytes_read;
|
|
|
|
total_bytes_read += bytes_read;
|
|
|
|
|
|
|
|
if (total_bytes_read == max_bytes_read)
|
|
|
|
is_eof = true;
|
2015-03-04 10:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|