2015-03-04 10:47:53 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBufferFromFileBase.h>
|
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <IO/BufferWithOwnMemory.h>
|
|
|
|
#include <Core/Defines.h>
|
|
|
|
#include <Common/AIO.h>
|
|
|
|
#include <Common/CurrentMetrics.h>
|
2015-03-04 10:47:53 +00:00
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
#include <string>
|
2015-03-05 14:21:55 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
|
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Metric OpenFileForWrite;
|
2016-10-24 04:06:27 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 10:47:53 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/** Class for asynchronous data writing.
|
2015-03-10 17:14:07 +00:00
|
|
|
*/
|
2015-03-30 15:39:55 +00:00
|
|
|
class WriteBufferAIO : public WriteBufferFromFileBase
|
2015-03-04 10:47:53 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
WriteBufferAIO(const std::string & filename_, size_t buffer_size_ = DBMS_DEFAULT_BUFFER_SIZE, int flags_ = -1, mode_t mode_ = 0666,
|
|
|
|
char * existing_memory_ = nullptr);
|
|
|
|
~WriteBufferAIO() override;
|
2015-03-04 10:47:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
WriteBufferAIO(const WriteBufferAIO &) = delete;
|
|
|
|
WriteBufferAIO & operator=(const WriteBufferAIO &) = delete;
|
2015-03-04 10:47:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
off_t getPositionInFile() override;
|
|
|
|
void sync() override;
|
|
|
|
std::string getFileName() const override { return filename; }
|
|
|
|
int getFD() const override { return fd; }
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
void nextImpl() override;
|
|
|
|
off_t doSeek(off_t off, int whence) override;
|
|
|
|
void doTruncate(off_t length) override;
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// If there's still data in the buffer, we'll write them.
|
2017-04-01 07:20:54 +00:00
|
|
|
void flush();
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Wait for the end of the current asynchronous task.
|
2017-04-01 07:20:54 +00:00
|
|
|
bool waitForAIOCompletion();
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Prepare an asynchronous request.
|
2017-04-01 07:20:54 +00:00
|
|
|
void prepare();
|
|
|
|
///
|
|
|
|
void finalize();
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
|
|
private:
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Buffer for asynchronous data writes.
|
2017-04-01 07:20:54 +00:00
|
|
|
BufferWithOwnMemory<WriteBuffer> flush_buffer;
|
2015-03-10 11:43:30 +00:00
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Description of the asynchronous write request.
|
2017-04-01 07:20:54 +00:00
|
|
|
iocb request = { 0 };
|
|
|
|
std::vector<iocb *> request_ptrs{&request};
|
|
|
|
std::vector<io_event> events{1};
|
2015-03-10 11:43:30 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
AIOContext aio_context{1};
|
2015-03-11 14:32:32 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::string filename;
|
2015-03-11 14:32:32 +00:00
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The number of bytes to be written to the disk.
|
2017-04-01 07:20:54 +00:00
|
|
|
off_t bytes_to_write = 0;
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Number of bytes written with the last request.
|
2017-04-01 07:20:54 +00:00
|
|
|
off_t bytes_written = 0;
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The number of zero bytes to be cut from the end of the file
|
|
|
|
/// after the data write operation completes.
|
2017-04-01 07:20:54 +00:00
|
|
|
off_t truncation_count = 0;
|
2015-03-20 16:58:57 +00:00
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The current position in the file.
|
2017-04-01 07:20:54 +00:00
|
|
|
off_t pos_in_file = 0;
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The maximum position reached in the file.
|
2017-04-01 07:20:54 +00:00
|
|
|
off_t max_pos_in_file = 0;
|
2015-03-25 14:58:23 +00:00
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The starting position of the aligned region of the disk to which the data is written.
|
2017-04-01 07:20:54 +00:00
|
|
|
off_t region_aligned_begin = 0;
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The size of the aligned region of the disk.
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t region_aligned_size = 0;
|
2015-04-06 22:33:52 +00:00
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The file descriptor for writing.
|
2017-04-01 07:20:54 +00:00
|
|
|
int fd = -1;
|
2015-03-10 11:43:30 +00:00
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The data buffer that we want to write to the disk.
|
2017-04-01 07:20:54 +00:00
|
|
|
Position buffer_begin = nullptr;
|
2015-04-07 11:43:23 +00:00
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Is the asynchronous write operation still in progress?
|
2017-04-01 07:20:54 +00:00
|
|
|
bool is_pending_write = false;
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Did the asynchronous operation fail?
|
2017-04-01 07:20:54 +00:00
|
|
|
bool aio_failed = false;
|
2016-01-21 01:47:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
CurrentMetrics::Increment metric_increment{CurrentMetrics::OpenFileForWrite};
|
2015-03-04 10:47:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|