Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release block earlier in SpillHandler #6992

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions dbms/src/Core/SpillHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool SpillHandler::isSpilledFileFull(UInt64 spilled_rows, UInt64 spilled_bytes)
return (spiller->config.max_spilled_rows_per_file > 0 && spilled_rows >= spiller->config.max_spilled_rows_per_file) || (spiller->config.max_spilled_bytes_per_file > 0 && spilled_bytes >= spiller->config.max_spilled_bytes_per_file);
}

void SpillHandler::spillBlocks(const Blocks & blocks)
void SpillHandler::spillBlocks(Blocks & blocks)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void SpillHandler::spillBlocks(Blocks & blocks)
void SpillHandler::spillBlocks(Blocks && blocks)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

{
/// todo check the disk usage
if (unlikely(blocks.empty()))
Expand All @@ -91,7 +91,7 @@ void SpillHandler::spillBlocks(const Blocks & blocks)
size_t total_rows = 0;
size_t rows_in_file = 0;
size_t bytes_in_file = 0;
for (const auto & block : blocks)
for (auto & block : blocks)
{
if (unlikely(!block || block.rows() == 0))
continue;
Expand All @@ -104,6 +104,7 @@ void SpillHandler::spillBlocks(const Blocks & blocks)
rows_in_file += rows;
bytes_in_file += block.estimateBytesForSpill();
writer->write(block);
block.clear();
if (spiller->enable_append_write && isSpilledFileFull(rows_in_file, bytes_in_file))
{
spilled_files[current_spilled_file_index]->updateSpillDetails(writer->finishWrite());
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Core/SpillHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SpillHandler
{
public:
SpillHandler(Spiller * spiller_, size_t partition_id_);
void spillBlocks(const Blocks & blocks);
void spillBlocks(Blocks & blocks);
void finish();

private:
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Core/Spiller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ SpillHandler Spiller::createSpillHandler(UInt64 partition_id)
return SpillHandler(this, partition_id);
}

void Spiller::spillBlocks(const Blocks & blocks, UInt64 partition_id)
void Spiller::spillBlocks(Blocks && blocks, UInt64 partition_id)
{
if (blocks.empty())
return;
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Core/Spiller.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Spiller
{
public:
Spiller(const SpillConfig & config, bool is_input_sorted, UInt64 partition_num, const Block & input_schema, const LoggerPtr & logger, Int64 spill_version = 1, bool release_spilled_file_on_restore = true);
void spillBlocks(const Blocks & blocks, UInt64 partition_id);
void spillBlocks(Blocks && blocks, UInt64 partition_id);
/// spill blocks by reading from BlockInputStream, this is more memory friendly compared to spillBlocks
void spillBlocksUsingBlockInputStream(IBlockInputStream & block_in, UInt64 partition_id, const std::function<bool()> & is_cancelled);
/// max_stream_size == 0 means the spiller choose the stream size automatically
Expand Down
75 changes: 43 additions & 32 deletions dbms/src/Core/tests/gtest_spiller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ try
auto blocks = generateBlocks(3);
for (const auto & block : blocks)
ref += block.rows();
spiller.spillBlocks(blocks, 0);
spiller.spillBlocks(std::move(blocks), 0);
}
spiller.finishSpill();
GTEST_ASSERT_EQ(ref, spiller.spilledRows(0));
Expand All @@ -227,7 +227,7 @@ try
{
auto blocks = generateBlocks(3);
all_blocks[partition_id].insert(all_blocks[partition_id].end(), blocks.begin(), blocks.end());
spiller.spillBlocks(blocks, partition_id);
spiller.spillBlocks(std::move(blocks), partition_id);
}
}
spiller.finishSpill();
Expand Down Expand Up @@ -294,7 +294,8 @@ try
Blocks blocks = generateBlocks(50);
for (auto & spiller : spillers)
{
spiller->spillBlocks(blocks, 0);
auto blocks_to_spill = blocks;
spiller->spillBlocks(std::move(blocks_to_spill), 0);
spiller->finishSpill();
verifyRestoreBlocks(*spiller, 0, 0, 0, blocks);
if (!spiller->releaseSpilledFileOnRestore())
Expand Down Expand Up @@ -322,7 +323,7 @@ try
{
auto blocks = generateSortedBlocks(3);
all_blocks[partition_id].insert(all_blocks[partition_id].end(), blocks.begin(), blocks.end());
spiller.spillBlocks(blocks, partition_id);
spiller.spillBlocks(std::move(blocks), partition_id);
}
}
spiller.finishSpill();
Expand Down Expand Up @@ -384,11 +385,12 @@ try
Spiller spiller(spiller_config_for_append_write, false, 1, spiller_test_header, logger);
Blocks all_blocks;
auto blocks = generateBlocks(20);
spiller.spillBlocks(blocks, 0);
spiller.spillBlocks(blocks, 0);
spiller.finishSpill();
all_blocks.insert(all_blocks.end(), blocks.begin(), blocks.end());
all_blocks.insert(all_blocks.end(), blocks.begin(), blocks.end());
auto blocks_copy = blocks;
spiller.spillBlocks(std::move(blocks), 0);
spiller.spillBlocks(std::move(blocks_copy), 0);
spiller.finishSpill();
verifyRestoreBlocks(spiller, 0, 20, 1, all_blocks, false);
}
/// append_dummy_read = true
Expand All @@ -397,11 +399,12 @@ try
Spiller spiller(spiller_config_for_append_write, false, 1, spiller_test_header, logger);
Blocks all_blocks;
auto blocks = generateBlocks(20);
spiller.spillBlocks(blocks, 0);
spiller.spillBlocks(blocks, 0);
spiller.finishSpill();
all_blocks.insert(all_blocks.end(), blocks.begin(), blocks.end());
all_blocks.insert(all_blocks.end(), blocks.begin(), blocks.end());
auto blocks_copy = blocks;
spiller.spillBlocks(std::move(blocks), 0);
spiller.spillBlocks(std::move(blocks_copy), 0);
spiller.finishSpill();
verifyRestoreBlocks(spiller, 0, 20, 20, all_blocks, true);
}
}
Expand All @@ -418,42 +421,45 @@ try
Spiller spiller(spiller_config_for_append_write, false, 1, spiller_test_header, logger);
Blocks all_blocks;
auto blocks = generateBlocks(50);
spiller.spillBlocks(blocks, 0);
spiller.spillBlocks(blocks, 0);
spiller.finishSpill();
all_blocks.insert(all_blocks.end(), blocks.begin(), blocks.end());
all_blocks.insert(all_blocks.end(), blocks.begin(), blocks.end());
auto blocks_copy = blocks;
spiller.spillBlocks(std::move(blocks), 0);
spiller.spillBlocks(std::move(blocks_copy), 0);
spiller.finishSpill();
verifyRestoreBlocks(spiller, 0, 2, 1, all_blocks);
}
/// case 2, one spill write to multiple files
{
spiller_config_for_append_write.max_spilled_rows_per_file = 1;
Spiller spiller(spiller_config_for_append_write, false, 1, spiller_test_header, logger);
auto all_blocks = generateBlocks(20);
spiller.spillBlocks(all_blocks, 0);
auto reference = all_blocks;
spiller.spillBlocks(std::move(all_blocks), 0);
spiller.finishSpill();
verifyRestoreBlocks(spiller, 0, 0, 20, all_blocks);
verifyRestoreBlocks(spiller, 0, 0, 20, reference);
}
/// case 3, spill empty blocks to existing spilled file
{
spiller_config_for_append_write.max_spilled_rows_per_file = 1000000000;
Spiller spiller(spiller_config_for_append_write, false, 1, spiller_test_header, logger);
Blocks all_blocks = generateBlocks(20);
spiller.spillBlocks(all_blocks, 0);
auto reference = all_blocks;
spiller.spillBlocks(std::move(all_blocks), 0);
Blocks empty_blocks;
spiller.spillBlocks(empty_blocks, 0);
spiller.spillBlocks(std::move(empty_blocks), 0);
BlocksList empty_blocks_list;
BlocksListBlockInputStream block_input_stream(std::move(empty_blocks_list));
spiller.spillBlocksUsingBlockInputStream(block_input_stream, 0, []() { return false; });
spiller.finishSpill();
verifyRestoreBlocks(spiller, 0, 2, 1, all_blocks);
verifyRestoreBlocks(spiller, 0, 2, 1, reference);
}
/// case 4, spill empty blocks to new spilled file
{
spiller_config_for_append_write.max_spilled_rows_per_file = 1000000000;
Spiller spiller(spiller_config_for_append_write, false, 1, spiller_test_header, logger);
Blocks empty_blocks;
spiller.spillBlocks(empty_blocks, 0);
spiller.spillBlocks(std::move(empty_blocks), 0);
BlocksList empty_blocks_list;
BlocksListBlockInputStream block_input_stream(std::move(empty_blocks_list));
spiller.spillBlocksUsingBlockInputStream(block_input_stream, 0, []() { return false; });
Expand Down Expand Up @@ -497,7 +503,8 @@ try
data.push_back(ColumnWithTypeAndName(std::move(column), type_and_name.type, type_and_name.name));
}
ret.emplace_back(data);
spiller.spillBlocks(ret, 0);
auto reference = ret;
spiller.spillBlocks(std::move(ret), 0);
spiller.finishSpill();
auto block_streams = spiller.restoreBlocks(0, 2);
GTEST_ASSERT_EQ(block_streams.size(), 1);
Expand All @@ -507,10 +514,10 @@ try
for (Block block = block_stream->read(); block; block = block_stream->read())
restored_blocks.push_back(block);
}
GTEST_ASSERT_EQ(ret.size(), restored_blocks.size());
for (size_t i = 0; i < ret.size(); ++i)
GTEST_ASSERT_EQ(reference.size(), restored_blocks.size());
for (size_t i = 0; i < reference.size(); ++i)
{
blockEqual(materializeBlock(ret[i]), restored_blocks[i]);
blockEqual(materializeBlock(reference[i]), restored_blocks[i]);
}
}
CATCH
Expand Down Expand Up @@ -552,9 +559,10 @@ try
data.push_back(column);
}
ret.emplace_back(data);
spiller.spillBlocks(ret, 0);
auto reference = ret;
spiller.spillBlocks(std::move(ret), 0);
spiller.finishSpill();
verifyRestoreBlocks(spiller, 0, 2, 1, ret);
verifyRestoreBlocks(spiller, 0, 2, 1, reference);
}
CATCH

Expand Down Expand Up @@ -588,9 +596,10 @@ try
data.push_back(column);
}
ret.emplace_back(data);
spiller.spillBlocks(ret, 0);
auto reference = ret;
spiller.spillBlocks(std::move(ret), 0);
spiller.finishSpill();
verifyRestoreBlocks(spiller, 0, 2, 1, ret);
verifyRestoreBlocks(spiller, 0, 2, 1, reference);
}
CATCH

Expand Down Expand Up @@ -622,9 +631,10 @@ try
data.push_back(column);
}
ret.emplace_back(data);
spiller.spillBlocks(ret, 0);
auto reference = ret;
spiller.spillBlocks(std::move(ret), 0);
spiller.finishSpill();
verifyRestoreBlocks(spiller, 0, 2, 1, ret);
verifyRestoreBlocks(spiller, 0, 2, 1, reference);
}
CATCH

Expand Down Expand Up @@ -652,11 +662,12 @@ try
data.push_back(column);
}
ret.emplace_back(data);
spiller.spillBlocks(ret, 0);
auto reference = ret;
spiller.spillBlocks(std::move(ret), 0);
spiller.finishSpill();
verifyRestoreBlocks(spiller, 0, 2, 1, ret);
verifyRestoreBlocks(spiller, 0, 2, 1, reference);
}
CATCH

} // namespace tests
} // namespace DB
} // namespace DB
2 changes: 1 addition & 1 deletion dbms/src/Interpreters/Aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ void Aggregator::spillImpl(
blocks.push_back(convertOneBucketToBlock(data_variants, method, data_variants.aggregates_pool, false, bucket));
update_max_sizes(blocks.back());
}
spiller->spillBlocks(blocks, 0);
spiller->spillBlocks(std::move(blocks), 0);

/// Pass ownership of the aggregate functions states:
/// `data_variants` will not destroy them in the destructor, they are now owned by ColumnAggregateFunction objects.
Expand Down