2017-06-25 00:01:10 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeBlockOutputStream.h>
|
2020-05-20 12:02:02 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeDataPartInMemory.h>
|
2017-06-25 00:01:10 +00:00
|
|
|
#include <Storages/StorageMergeTree.h>
|
|
|
|
#include <Interpreters/PartLog.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-04-20 01:38:38 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int TIMEOUT_EXCEEDED;
|
|
|
|
}
|
|
|
|
|
2018-02-19 00:45:32 +00:00
|
|
|
Block MergeTreeBlockOutputStream::getHeader() const
|
|
|
|
{
|
|
|
|
return storage.getSampleBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-25 00:01:10 +00:00
|
|
|
void MergeTreeBlockOutputStream::write(const Block & block)
|
|
|
|
{
|
2019-05-03 02:00:57 +00:00
|
|
|
storage.delayInsertOrThrowIfNeeded();
|
2017-06-25 00:01:10 +00:00
|
|
|
|
2019-03-29 09:33:39 +00:00
|
|
|
auto part_blocks = storage.writer.splitBlockIntoParts(block, max_parts_per_block);
|
2017-06-25 00:01:10 +00:00
|
|
|
for (auto & current_block : part_blocks)
|
|
|
|
{
|
|
|
|
Stopwatch watch;
|
|
|
|
|
|
|
|
MergeTreeData::MutableDataPartPtr part = storage.writer.writeTempPart(current_block);
|
2019-05-03 02:00:57 +00:00
|
|
|
storage.renameTempPartAndAdd(part, &storage.increment);
|
2017-06-25 00:01:10 +00:00
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
PartLog::addNewPart(storage.global_context, part, watch.elapsed());
|
2017-06-25 00:01:10 +00:00
|
|
|
|
2020-06-05 20:47:46 +00:00
|
|
|
if (auto part_in_memory = asInMemoryPart(part))
|
2020-04-20 01:38:38 +00:00
|
|
|
{
|
2020-05-20 12:02:02 +00:00
|
|
|
storage.in_memory_merges_throttler.add(part_in_memory->block.bytes(), part_in_memory->rows_count);
|
|
|
|
|
|
|
|
auto settings = storage.getSettings();
|
|
|
|
if (settings->in_memory_parts_insert_sync)
|
|
|
|
{
|
2020-06-05 20:47:46 +00:00
|
|
|
if (!part_in_memory->waitUntilMerged(in_memory_parts_timeout))
|
2020-05-20 12:02:02 +00:00
|
|
|
throw Exception("Timeout exceeded while waiting to write part "
|
|
|
|
+ part->name + " on disk", ErrorCodes::TIMEOUT_EXCEEDED);
|
|
|
|
}
|
|
|
|
else if (storage.merging_mutating_task_handle && !storage.in_memory_merges_throttler.needDelayMerge())
|
|
|
|
{
|
|
|
|
storage.in_memory_merges_throttler.reset();
|
|
|
|
storage.merging_mutating_task_handle->wake();
|
|
|
|
}
|
|
|
|
|
2020-05-27 20:05:55 +00:00
|
|
|
continue;
|
2020-04-20 01:38:38 +00:00
|
|
|
}
|
2020-05-27 20:05:55 +00:00
|
|
|
else if (storage.merging_mutating_task_handle)
|
|
|
|
{
|
|
|
|
/// Initiate async merge - it will be done if it's good time for merge and if there are space in 'background_pool'.
|
2019-09-05 13:12:29 +00:00
|
|
|
storage.merging_mutating_task_handle->wake();
|
2020-05-27 20:05:55 +00:00
|
|
|
}
|
2017-06-25 00:01:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|