2022-05-08 17:01:47 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <ctime>
|
2017-11-20 04:15:43 +00:00
|
|
|
#include <optional>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
|
|
|
#include <Common/Stopwatch.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/CurrentMetrics.h>
|
2023-03-30 17:02:28 +00:00
|
|
|
#include <Common/Throttler.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferFromFileDescriptor.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2022-05-25 14:49:40 +00:00
|
|
|
#include <Common/filesystemHelpers.h>
|
2021-04-28 16:25:14 +00:00
|
|
|
#include <sys/stat.h>
|
2022-07-20 20:30:16 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2021-04-28 16:25:14 +00:00
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
|
2021-08-23 18:07:42 +00:00
|
|
|
#pragma clang diagnostic ignored "-Wreserved-identifier"
|
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event ReadBufferFromFileDescriptorRead;
|
2016-11-29 21:56:00 +00:00
|
|
|
extern const Event ReadBufferFromFileDescriptorReadFailed;
|
2016-10-25 06:49:24 +00:00
|
|
|
extern const Event ReadBufferFromFileDescriptorReadBytes;
|
2018-05-28 19:53:03 +00:00
|
|
|
extern const Event DiskReadElapsedMicroseconds;
|
2016-10-25 06:49:24 +00:00
|
|
|
extern const Event Seek;
|
2023-03-30 17:02:28 +00:00
|
|
|
extern const Event LocalReadThrottlerBytes;
|
|
|
|
extern const Event LocalReadThrottlerSleepMicroseconds;
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
|
|
|
extern const Metric Read;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_READ_FROM_FILE_DESCRIPTOR;
|
|
|
|
extern const int ARGUMENT_OUT_OF_BOUND;
|
|
|
|
extern const int CANNOT_SEEK_THROUGH_FILE;
|
2021-07-26 00:34:36 +00:00
|
|
|
extern const int CANNOT_ADVISE;
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string ReadBufferFromFileDescriptor::getFileName() const
|
|
|
|
{
|
|
|
|
return "(fd = " + toString(fd) + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-03 13:22:40 +00:00
|
|
|
size_t ReadBufferFromFileDescriptor::readImpl(char * to, size_t min_bytes, size_t max_bytes, size_t offset) const
|
2016-10-25 06:49:24 +00:00
|
|
|
{
|
2023-05-05 03:11:51 +00:00
|
|
|
chassert(min_bytes <= max_bytes);
|
2021-10-15 07:13:11 +00:00
|
|
|
|
2023-05-05 03:11:51 +00:00
|
|
|
/// This is a workaround of a read past EOF bug in linux kernel with pread()
|
|
|
|
if (file_size.has_value() && offset >= *file_size)
|
|
|
|
return 0;
|
2022-07-05 18:15:35 +00:00
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
size_t bytes_read = 0;
|
2023-05-05 03:11:51 +00:00
|
|
|
while (bytes_read < min_bytes)
|
2016-10-25 06:49:24 +00:00
|
|
|
{
|
|
|
|
ProfileEvents::increment(ProfileEvents::ReadBufferFromFileDescriptorRead);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-05-31 15:54:08 +00:00
|
|
|
Stopwatch watch(profile_callback ? clock_type : CLOCK_MONOTONIC);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
ssize_t res = 0;
|
2023-05-05 03:11:51 +00:00
|
|
|
size_t to_read = max_bytes - bytes_read;
|
2016-10-25 06:49:24 +00:00
|
|
|
{
|
|
|
|
CurrentMetrics::Increment metric_increment{CurrentMetrics::Read};
|
2021-07-12 01:12:34 +00:00
|
|
|
|
|
|
|
if (use_pread)
|
2023-05-05 03:11:51 +00:00
|
|
|
res = ::pread(fd, to + bytes_read, to_read, offset + bytes_read);
|
2021-07-12 01:12:34 +00:00
|
|
|
else
|
2023-05-05 03:11:51 +00:00
|
|
|
res = ::read(fd, to + bytes_read, to_read);
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
if (!res)
|
|
|
|
break;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
if (-1 == res && errno != EINTR)
|
2016-11-29 21:56:00 +00:00
|
|
|
{
|
|
|
|
ProfileEvents::increment(ProfileEvents::ReadBufferFromFileDescriptorReadFailed);
|
2023-12-15 18:25:49 +00:00
|
|
|
ErrnoException::throwFromPath(
|
|
|
|
ErrorCodes::CANNOT_READ_FROM_FILE_DESCRIPTOR, getFileName(), "Cannot read from file {}", getFileName());
|
2016-11-29 21:56:00 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
if (res > 0)
|
2023-03-30 17:02:28 +00:00
|
|
|
{
|
2016-10-25 06:49:24 +00:00
|
|
|
bytes_read += res;
|
2023-03-30 17:02:28 +00:00
|
|
|
if (throttler)
|
|
|
|
throttler->add(res, ProfileEvents::LocalReadThrottlerBytes, ProfileEvents::LocalReadThrottlerSleepMicroseconds);
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-08-22 00:24:55 +00:00
|
|
|
/// It reports real time spent including the time spent while thread was preempted doing nothing.
|
|
|
|
/// And it is Ok for the purpose of this watch (it is used to lower the number of threads to read from tables).
|
2020-07-13 00:45:37 +00:00
|
|
|
/// Sometimes it is better to use taskstats::blkio_delay_total, but it is quite expensive to get it
|
2023-07-21 04:45:35 +00:00
|
|
|
/// (NetlinkMetricsProvider has about 500K RPS).
|
2018-05-31 15:54:08 +00:00
|
|
|
watch.stop();
|
|
|
|
ProfileEvents::increment(ProfileEvents::DiskReadElapsedMicroseconds, watch.elapsedMicroseconds());
|
2018-05-28 19:53:03 +00:00
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
if (profile_callback)
|
|
|
|
{
|
|
|
|
ProfileInfo info;
|
2023-05-05 03:11:51 +00:00
|
|
|
info.bytes_requested = to_read;
|
2016-10-25 06:49:24 +00:00
|
|
|
info.bytes_read = res;
|
2018-05-31 15:54:08 +00:00
|
|
|
info.nanoseconds = watch.elapsed();
|
2016-10-25 06:49:24 +00:00
|
|
|
profile_callback(info);
|
|
|
|
}
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2023-05-05 03:11:51 +00:00
|
|
|
if (bytes_read)
|
|
|
|
ProfileEvents::increment(ProfileEvents::ReadBufferFromFileDescriptorReadBytes, bytes_read);
|
|
|
|
|
|
|
|
return bytes_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ReadBufferFromFileDescriptor::nextImpl()
|
|
|
|
{
|
|
|
|
/// If internal_buffer size is empty, then read() cannot be distinguished from EOF
|
|
|
|
assert(!internal_buffer.empty());
|
|
|
|
|
|
|
|
size_t bytes_read = readImpl(internal_buffer.begin(), 1, internal_buffer.size(), file_offset_of_buffer_end);
|
|
|
|
|
2020-07-31 14:53:41 +00:00
|
|
|
file_offset_of_buffer_end += bytes_read;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
if (bytes_read)
|
|
|
|
{
|
2021-02-06 16:30:46 +00:00
|
|
|
working_buffer = internal_buffer;
|
2016-10-25 06:49:24 +00:00
|
|
|
working_buffer.resize(bytes_read);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-26 13:55:30 +00:00
|
|
|
void ReadBufferFromFileDescriptor::prefetch(Priority)
|
2021-07-26 00:34:36 +00:00
|
|
|
{
|
2021-07-27 23:59:56 +00:00
|
|
|
#if defined(POSIX_FADV_WILLNEED)
|
2021-07-26 00:34:36 +00:00
|
|
|
/// For direct IO, loading data into page cache is pointless.
|
|
|
|
if (required_alignment)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/// Ask OS to prefetch data into page cache.
|
|
|
|
if (0 != posix_fadvise(fd, file_offset_of_buffer_end, internal_buffer.size(), POSIX_FADV_WILLNEED))
|
2023-12-15 18:25:49 +00:00
|
|
|
throw ErrnoException(ErrorCodes::CANNOT_ADVISE, "Cannot posix_fadvise");
|
2021-07-27 23:59:56 +00:00
|
|
|
#endif
|
2021-07-26 00:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
/// If 'offset' is small enough to stay in buffer after seek, then true seek in file does not happen.
|
2020-01-27 18:44:30 +00:00
|
|
|
off_t ReadBufferFromFileDescriptor::seek(off_t offset, int whence)
|
2016-10-25 06:49:24 +00:00
|
|
|
{
|
2020-07-31 14:53:41 +00:00
|
|
|
size_t new_pos;
|
2020-01-04 05:46:50 +00:00
|
|
|
if (whence == SEEK_SET)
|
2020-07-31 14:53:41 +00:00
|
|
|
{
|
|
|
|
assert(offset >= 0);
|
2020-01-04 05:46:50 +00:00
|
|
|
new_pos = offset;
|
2020-07-31 14:53:41 +00:00
|
|
|
}
|
2020-01-04 05:46:50 +00:00
|
|
|
else if (whence == SEEK_CUR)
|
2020-07-31 14:53:41 +00:00
|
|
|
{
|
|
|
|
new_pos = file_offset_of_buffer_end - (working_buffer.end() - pos) + offset;
|
|
|
|
}
|
2020-01-04 05:46:50 +00:00
|
|
|
else
|
2020-07-31 14:53:41 +00:00
|
|
|
{
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ARGUMENT_OUT_OF_BOUND, "ReadBufferFromFileDescriptor::seek expects SEEK_SET or SEEK_CUR as whence");
|
2020-07-31 14:53:41 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
/// Position is unchanged.
|
2022-02-07 14:20:26 +00:00
|
|
|
if (new_pos + (working_buffer.end() - pos) == file_offset_of_buffer_end)
|
2016-10-25 06:49:24 +00:00
|
|
|
return new_pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2024-05-09 01:58:34 +00:00
|
|
|
if (file_offset_of_buffer_end - working_buffer.size() <= new_pos
|
2021-08-04 00:07:04 +00:00
|
|
|
&& new_pos <= file_offset_of_buffer_end)
|
2016-10-25 06:49:24 +00:00
|
|
|
{
|
2021-07-12 01:12:34 +00:00
|
|
|
/// Position is still inside the buffer.
|
2021-08-04 00:07:04 +00:00
|
|
|
/// Probably it is at the end of the buffer - then we will load data on the following 'next' call.
|
2021-07-12 01:12:34 +00:00
|
|
|
|
2020-07-31 14:53:41 +00:00
|
|
|
pos = working_buffer.end() - file_offset_of_buffer_end + new_pos;
|
|
|
|
assert(pos >= working_buffer.begin());
|
2021-08-04 00:07:04 +00:00
|
|
|
assert(pos <= working_buffer.end());
|
2020-07-31 14:53:41 +00:00
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
return new_pos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-12 01:12:34 +00:00
|
|
|
/// Position is out of the buffer, we need to do real seek.
|
|
|
|
off_t seek_pos = required_alignment > 1
|
2021-07-11 00:35:43 +00:00
|
|
|
? new_pos / required_alignment * required_alignment
|
|
|
|
: new_pos;
|
|
|
|
|
2021-07-12 01:12:34 +00:00
|
|
|
off_t offset_after_seek_pos = new_pos - seek_pos;
|
|
|
|
|
2022-02-07 14:20:26 +00:00
|
|
|
/// First reset the buffer so the next read will fetch new data to the buffer.
|
|
|
|
resetWorkingBuffer();
|
2022-02-04 22:12:09 +00:00
|
|
|
|
2021-07-12 01:12:34 +00:00
|
|
|
/// In case of using 'pread' we just update the info about the next position in file.
|
|
|
|
/// In case of using 'read' we call 'lseek'.
|
2021-07-11 00:35:43 +00:00
|
|
|
|
2021-07-12 01:12:34 +00:00
|
|
|
/// We account both cases as seek event as it leads to non-contiguous reads from file.
|
2016-10-25 06:49:24 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::Seek);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-07-12 01:12:34 +00:00
|
|
|
if (!use_pread)
|
|
|
|
{
|
|
|
|
Stopwatch watch(profile_callback ? clock_type : CLOCK_MONOTONIC);
|
2018-05-28 19:53:03 +00:00
|
|
|
|
2021-07-12 01:12:34 +00:00
|
|
|
off_t res = ::lseek(fd, seek_pos, SEEK_SET);
|
|
|
|
if (-1 == res)
|
2023-12-15 18:25:49 +00:00
|
|
|
ErrnoException::throwFromPath(
|
|
|
|
ErrorCodes::CANNOT_SEEK_THROUGH_FILE,
|
|
|
|
getFileName(),
|
|
|
|
"Cannot seek through file {} at offset {}",
|
|
|
|
getFileName(),
|
|
|
|
seek_pos);
|
2021-07-12 01:12:34 +00:00
|
|
|
|
|
|
|
/// Also note that seeking past the file size is not allowed.
|
|
|
|
if (res != seek_pos)
|
|
|
|
throw Exception(ErrorCodes::CANNOT_SEEK_THROUGH_FILE,
|
|
|
|
"The 'lseek' syscall returned value ({}) that is not expected ({})", res, seek_pos);
|
|
|
|
|
|
|
|
watch.stop();
|
|
|
|
ProfileEvents::increment(ProfileEvents::DiskReadElapsedMicroseconds, watch.elapsedMicroseconds());
|
|
|
|
}
|
2018-05-28 19:53:03 +00:00
|
|
|
|
2021-07-11 02:43:54 +00:00
|
|
|
file_offset_of_buffer_end = seek_pos;
|
2021-07-11 00:35:43 +00:00
|
|
|
|
|
|
|
if (offset_after_seek_pos > 0)
|
|
|
|
ignore(offset_after_seek_pos);
|
|
|
|
|
2021-07-12 01:12:34 +00:00
|
|
|
return seek_pos;
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-04 03:03:49 +00:00
|
|
|
void ReadBufferFromFileDescriptor::rewind()
|
|
|
|
{
|
2021-07-12 01:12:34 +00:00
|
|
|
if (!use_pread)
|
|
|
|
{
|
|
|
|
ProfileEvents::increment(ProfileEvents::Seek);
|
|
|
|
off_t res = ::lseek(fd, 0, SEEK_SET);
|
|
|
|
if (-1 == res)
|
2023-12-15 18:25:49 +00:00
|
|
|
ErrnoException::throwFromPath(
|
|
|
|
ErrorCodes::CANNOT_SEEK_THROUGH_FILE, getFileName(), "Cannot seek through file {}", getFileName());
|
2021-07-12 01:12:34 +00:00
|
|
|
}
|
|
|
|
/// In case of pread, the ProfileEvents::Seek is not accounted, but it's Ok.
|
2021-07-04 03:03:49 +00:00
|
|
|
|
|
|
|
/// Clearing the buffer with existing data. New data will be read on subsequent call to 'next'.
|
|
|
|
working_buffer.resize(0);
|
|
|
|
pos = working_buffer.begin();
|
2021-07-12 00:24:38 +00:00
|
|
|
file_offset_of_buffer_end = 0;
|
2021-07-04 03:03:49 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 02:54:36 +00:00
|
|
|
std::optional<size_t> ReadBufferFromFileDescriptor::tryGetFileSize()
|
2021-04-28 16:25:14 +00:00
|
|
|
{
|
2022-05-25 14:49:40 +00:00
|
|
|
return getSizeFromFileDescriptor(fd, getFileName());
|
2021-04-28 16:25:14 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 03:11:51 +00:00
|
|
|
bool ReadBufferFromFileDescriptor::checkIfActuallySeekable()
|
|
|
|
{
|
|
|
|
struct stat stat;
|
|
|
|
auto res = ::fstat(fd, &stat);
|
|
|
|
return res == 0 && S_ISREG(stat.st_mode);
|
|
|
|
}
|
|
|
|
|
2024-03-03 13:22:40 +00:00
|
|
|
size_t ReadBufferFromFileDescriptor::readBigAt(char * to, size_t n, size_t offset, const std::function<bool(size_t)> &) const
|
2023-05-05 03:11:51 +00:00
|
|
|
{
|
|
|
|
chassert(use_pread);
|
|
|
|
return readImpl(to, n, n, offset);
|
|
|
|
}
|
|
|
|
|
2016-10-25 06:49:24 +00:00
|
|
|
}
|