2014-03-13 12:48:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Storages/StorageMergeTree.h>
|
2015-04-16 06:12:35 +00:00
|
|
|
#include <DB/DataStreams/IBlockOutputStream.h>
|
2014-05-08 07:30:00 +00:00
|
|
|
#include <iomanip>
|
2014-03-13 12:48:07 +00:00
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
|
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event SynchronousMergeOnInsert;
|
|
|
|
}
|
|
|
|
|
2014-03-13 12:48:07 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class MergeTreeBlockOutputStream : public IBlockOutputStream
|
|
|
|
{
|
|
|
|
public:
|
2014-03-19 10:45:13 +00:00
|
|
|
MergeTreeBlockOutputStream(StorageMergeTree & storage_)
|
|
|
|
: storage(storage_) {}
|
2014-03-13 12:48:07 +00:00
|
|
|
|
2014-09-17 22:57:02 +00:00
|
|
|
void writePrefix() override
|
|
|
|
{
|
2016-09-04 17:12:07 +00:00
|
|
|
/// If too much parts, do extra merges synchronously, in current thread.
|
|
|
|
/// Why 10? - just in case, it is better than possibly infinite loop.
|
2015-06-05 17:34:57 +00:00
|
|
|
for (size_t i = 0; i < 10; ++i)
|
|
|
|
{
|
|
|
|
size_t parts_count = storage.data.getMaxPartsCountForMonth();
|
|
|
|
if (parts_count <= storage.data.settings.parts_to_delay_insert)
|
|
|
|
break;
|
|
|
|
|
|
|
|
ProfileEvents::increment(ProfileEvents::SynchronousMergeOnInsert);
|
2016-05-16 18:43:38 +00:00
|
|
|
storage.merge(0, {}, {}, {}, {});
|
2015-06-05 17:34:57 +00:00
|
|
|
}
|
2014-09-17 22:57:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void write(const Block & block) override
|
2014-03-13 12:48:07 +00:00
|
|
|
{
|
2014-03-19 10:45:13 +00:00
|
|
|
auto part_blocks = storage.writer.splitBlockIntoParts(block);
|
2014-03-13 17:44:00 +00:00
|
|
|
for (auto & current_block : part_blocks)
|
2014-03-13 12:48:07 +00:00
|
|
|
{
|
2015-08-17 21:09:36 +00:00
|
|
|
Int64 temp_index = storage.increment.get();
|
2014-03-19 10:45:13 +00:00
|
|
|
MergeTreeData::MutableDataPartPtr part = storage.writer.writeTempPart(current_block, temp_index);
|
|
|
|
storage.data.renameTempPartAndAdd(part, &storage.increment);
|
2015-06-05 17:34:57 +00:00
|
|
|
|
2016-09-04 17:12:07 +00:00
|
|
|
/// Initiate async merge - it will be done if it's good time for merge and if there are space in 'background_pool'.
|
2014-07-03 08:51:11 +00:00
|
|
|
storage.merge_task_handle->wake();
|
2014-03-13 12:48:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
StorageMergeTree & storage;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|