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

Fix mutations huge memory consumption #9754

Merged
merged 5 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions dbms/src/Interpreters/PartLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Block PartLogElement::createBlock()
{ColumnUInt64::create(), std::make_shared<DataTypeUInt64>(), "bytes_uncompressed"}, // Result bytes
{ColumnUInt64::create(), std::make_shared<DataTypeUInt64>(), "read_rows"},
{ColumnUInt64::create(), std::make_shared<DataTypeUInt64>(), "read_bytes"},
{ColumnUInt64::create(), std::make_shared<DataTypeUInt64>(), "peak_memory_usage"},

/// Is there an error during the execution or commit
{ColumnUInt16::create(), std::make_shared<DataTypeUInt16>(), "error"},
Expand Down Expand Up @@ -87,6 +88,7 @@ void PartLogElement::appendToBlock(Block & block) const
columns[i++]->insert(bytes_uncompressed);
columns[i++]->insert(rows_read);
columns[i++]->insert(bytes_read_uncompressed);
columns[i++]->insert(peak_memory_usage);

columns[i++]->insert(error);
columns[i++]->insert(exception);
Expand Down
2 changes: 2 additions & 0 deletions dbms/src/Interpreters/PartLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ struct PartLogElement
UInt64 bytes_uncompressed = 0;
UInt64 rows_read = 0;
UInt64 bytes_read_uncompressed = 0;
UInt64 peak_memory_usage = 0;

/// Was the operation successful?
UInt16 error = 0;
String exception;


static std::string name() { return "PartLog"; }

static Block createBlock();
Expand Down
1 change: 1 addition & 0 deletions dbms/src/Storages/MergeTree/MergeTreeData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3755,6 +3755,7 @@ try

part_log_elem.rows = (*merge_entry)->rows_written;
part_log_elem.bytes_uncompressed = (*merge_entry)->bytes_written_uncompressed;
part_log_elem.peak_memory_usage = (*merge_entry)->memory_tracker.getPeak();
}

part_log->add(part_log_elem);
Expand Down
5 changes: 5 additions & 0 deletions dbms/src/Storages/MergeTree/StorageFromMergeTreeDataPart.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class StorageFromMergeTreeDataPart : public ext::shared_ptr_helper<StorageFromMe
}


bool hasSortingKey() const { return part->storage.hasSortingKey(); }

Names getSortingKeyColumns() const override { return part->storage.getSortingKeyColumns(); }


protected:
StorageFromMergeTreeDataPart(const MergeTreeData::DataPartPtr & part_)
: IStorage(getIDFromPart(part_), part_->storage.getVirtuals())
Expand Down
21 changes: 18 additions & 3 deletions dbms/src/Storages/ReadInOrderOptimizer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <Storages/ReadInOrderOptimizer.h>
#include <Storages/MergeTree/MergeTreeData.h>
#include <Storages/MergeTree/StorageFromMergeTreeDataPart.h>
#include <Interpreters/AnalyzedJoin.h>
#include <Functions/IFunction.h>

Expand Down Expand Up @@ -31,14 +32,28 @@ ReadInOrderOptimizer::ReadInOrderOptimizer(

InputSortingInfoPtr ReadInOrderOptimizer::getInputOrder(const StoragePtr & storage) const
{
const MergeTreeData * merge_tree = dynamic_cast<const MergeTreeData *>(storage.get());
if (!merge_tree || !merge_tree->hasSortingKey())
Names sorting_key_columns;
if (const auto * merge_tree = dynamic_cast<const MergeTreeData *>(storage.get()))
{
if (!merge_tree->hasSortingKey())
return {};
sorting_key_columns = merge_tree->getSortingKeyColumns();
}
else if (const auto * part = dynamic_cast<const StorageFromMergeTreeDataPart *>(storage.get()))
{
if (!part->hasSortingKey())
return {};
sorting_key_columns = part->getSortingKeyColumns();
}
else /// Inapplicable storage type
{
return {};
}


SortDescription order_key_prefix_descr;
int read_direction = required_sort_description.at(0).direction;

const auto & sorting_key_columns = merge_tree->getSortingKeyColumns();
size_t prefix_size = std::min(required_sort_description.size(), sorting_key_columns.size());

for (size_t i = 0; i < prefix_size; ++i)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
DROP TABLE IF EXISTS table_with_pk;

CREATE TABLE table_with_pk
(
key UInt8,
value String
)
ENGINE = MergeTree
ORDER BY key;

INSERT INTO table_with_pk SELECT number, toString(number % 10) FROM numbers(10000000);

ALTER TABLE table_with_pk DELETE WHERE key % 77 = 0 SETTINGS mutations_sync = 1;

SYSTEM FLUSH LOGS;

-- Memory usage for all mutations must be almost constant and less than
-- read_bytes.
SELECT
DISTINCT read_bytes >= peak_memory_usage
FROM
system.part_log
WHERE event_type = 'MutatePart' AND table = 'table_with_pk' AND database = currentDatabase();

DROP TABLE IF EXISTS table_with_pk;