ClickHouse/src/IO/AsynchronousInsertionQueue.h

69 lines
2.4 KiB
C++
Raw Normal View History

2021-03-04 11:10:21 +00:00
#pragma once
#include <Parsers/IAST_fwd.h>
#include <Common/RWLock.h>
#include <Common/ThreadPool.h>
2021-03-17 14:11:47 +00:00
#include <Core/Settings.h>
2021-03-04 11:10:21 +00:00
#include <unordered_map>
namespace DB
{
class ASTInsertQuery;
struct BlockIO;
class AsynchronousInsertQueue
{
public:
AsynchronousInsertQueue(size_t pool_size, size_t max_data_size);
bool push(ASTInsertQuery * query, const Settings & settings);
void push(ASTInsertQuery * query, BlockIO && io, const Settings & settings);
private:
2021-03-17 14:11:47 +00:00
struct InsertQuery
{
ASTPtr query;
Settings settings;
};
2021-03-04 11:10:21 +00:00
struct InsertData;
struct InsertQueryHash
{
std::size_t operator () (const InsertQuery &) const;
};
struct InsertQueryEquality
{
bool operator () (const InsertQuery &, const InsertQuery &) const;
};
2021-03-17 14:11:47 +00:00
/// Logic and events behind queue are as follows:
/// - reset_timeout: if queue is empty for some time, then we delete the queue and free all associated resources, e.g. tables.
/// - dump_timeout: if queue is active for too long and there are a lot of rapid inserts, then we dump the data, so it doesn't
/// grow for a long period of time and users will be able to select new data in determenistic manner.
/// - stale_timeout: if queue is stale for too long, then we dump the data too, so that users will be able to select the last
/// piece of inserted data.
/// - access_timeout: also we have to check if user still has access to the tables periodically, and if the access is lost, then
/// we dump pending data and delete queue immediately.
/// - max_data_size: if the maximum size of data is reached, then again we dump the data.
2021-03-04 11:10:21 +00:00
using Queue = std::unordered_map<InsertQuery, std::shared_ptr<InsertData>, InsertQueryHash, InsertQueryEquality>;
using QueueIterator = Queue::iterator;
2021-03-17 14:11:47 +00:00
const size_t max_data_size; /// in bytes
2021-03-04 11:10:21 +00:00
RWLock lock;
2021-03-17 14:11:47 +00:00
std::unique_ptr<Queue> queue;
2021-03-04 11:10:21 +00:00
2021-03-17 14:11:47 +00:00
ThreadPool pool; /// dump the data only inside this pool.
2021-03-04 11:10:21 +00:00
/// TODO: ThreadFromGlobalPool remove_empty_thread, check_access_thread;
void pushImpl(ASTInsertQuery * query, QueueIterator & it); /// use only under lock
2021-03-17 14:11:47 +00:00
static void processData(std::shared_ptr<InsertData> data);
2021-03-04 11:10:21 +00:00
};
}