ClickHouse/src/IO/AsynchronousReader.h

70 lines
2.4 KiB
C++
Raw Normal View History

2021-07-26 00:34:36 +00:00
#pragma once
#include <Core/Types.h>
#include <optional>
#include <memory>
2021-08-04 00:07:04 +00:00
#include <future>
2021-07-26 00:34:36 +00:00
namespace DB
{
/** Interface for asynchronous reads from file descriptors.
* It can abstract Linux AIO, io_uring or normal reads from separate thread pool,
* and also reads from non-local filesystems.
* The implementation not necessarily to be efficient for large number of small requests,
* instead it should be ok for moderate number of sufficiently large requests
* (e.g. read 1 MB of data 50 000 times per seconds; BTW this is normal performance for reading from page cache).
* For example, this interface may not suffice if you want to serve 10 000 000 of 4 KiB requests per second.
* This interface is fairly limited.
*/
class IAsynchronousReader
{
public:
/// For local filesystems, the file descriptor is simply integer
/// but it can be arbitrary opaque object for remote filesystems.
struct IFileDescriptor
{
virtual ~IFileDescriptor() = default;
};
using FileDescriptorPtr = std::shared_ptr<IFileDescriptor>;
struct LocalFileDescriptor : public IFileDescriptor
{
LocalFileDescriptor(int fd_) : fd(fd_) {}
int fd;
};
/// Read from file descriptor at specified offset up to size bytes into buf.
/// Some implementations may require alignment and it is responsibility of
/// the caller to provide conforming requests.
struct Request
{
FileDescriptorPtr descriptor;
2021-07-28 05:28:30 +00:00
size_t offset = 0;
size_t size = 0;
char * buf = nullptr;
2021-08-24 23:56:14 +00:00
int64_t priority = 0;
2021-07-26 00:34:36 +00:00
};
/// Less than requested amount of data can be returned.
2021-08-04 00:07:04 +00:00
/// If size is zero - the file has ended.
2021-07-28 06:08:29 +00:00
/// (for example, EINTR must be handled by implementation automatically)
2021-08-04 00:07:04 +00:00
using Result = size_t;
2021-07-26 00:34:36 +00:00
/// Submit request and obtain a handle. This method don't perform any waits.
/// If this method did not throw, the caller must wait for the result with 'wait' method
/// or destroy the whole reader before destroying the buffer for request.
2021-08-04 00:07:04 +00:00
/// The method can be called concurrently from multiple threads.
virtual std::future<Result> submit(Request request) = 0;
2021-07-26 00:34:36 +00:00
/// Destructor must wait for all not completed request and ignore the results.
/// It may also cancel the requests.
virtual ~IAsynchronousReader() = default;
};
using AsynchronousReaderPtr = std::shared_ptr<IAsynchronousReader>;
}