Skip to content

Implement queue mode for MergeTree#59827

Open
Michicosun wants to merge 57 commits intoClickHouse:masterfrom
Michicosun:issues/42990/block_row_column
Open

Implement queue mode for MergeTree#59827
Michicosun wants to merge 57 commits intoClickHouse:masterfrom
Michicosun:issues/42990/block_row_column

Conversation

@Michicosun
Copy link
Copy Markdown
Member

Changelog category (leave one):

  • New Feature

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Relates to: #42990

Implemented queue feature for [Replicated]MergeTree storage engines. With queue flag enabled 2 materialized columns will be added to table: _queue_block_number, _queue_block_offset, their combination introduces a global order in the rows of the table, which in the future will help to support cursors for streaming queries.

Example:

CREATE TABLE queue(a UInt64, b UInt64) ENGINE=MergeTree() ORDER BY a SETTINGS queue=1;

-- suffix lookup
SELECT * FROM queue WHERE (_queue_block_number, _queue_block_offset) > (3, 1);

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Information about CI checks: https://clickhouse.com/docs/en/development/continuous-integration/

@Michicosun Michicosun changed the title Issues/42990/block row column Implemented queue mode for MergeTree Feb 9, 2024
@Michicosun Michicosun changed the title Implemented queue mode for MergeTree Implement queue mode for MergeTree Feb 9, 2024
@qoega
Copy link
Copy Markdown
Member

qoega commented Feb 9, 2024

_queue_block_number, _queue_block_offset, their combination introduces a global order in the rows of the table, which in the future will help to support cursors for streaming queries.

How this is different from allow_experimental_block_number_column? #47532
It has strict order.

@kssenii kssenii added the can be tested Allows running workflows for external contributors label Feb 9, 2024
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-feature Pull request with new product feature label Feb 9, 2024
@robot-ch-test-poll2
Copy link
Copy Markdown
Contributor

robot-ch-test-poll2 commented Feb 9, 2024

This is an automated comment for commit 11f2a34 with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Check nameDescriptionStatus
A SyncThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS⏳ pending
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR⏳ pending
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests❌ failure
Mergeable CheckChecks if all other necessary checks are successful⏳ pending
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc❌ failure
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts❌ failure
Successful checks
Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help✅ success
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ success
ClickHouse build checkBuilds ClickHouse in various configurations for use in further steps. You have to fix the builds that fail. Build logs often has enough information to fix the error, but you might have to reproduce the failure locally. The cmake options can be found in the build log, grepping for cmake. Use these options and follow the general build process✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker keeper imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docker server imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docs checkBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integration tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
PR CheckThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success

@kssenii kssenii self-assigned this Feb 9, 2024
@Michicosun
Copy link
Copy Markdown
Member Author

_queue_block_number, _queue_block_offset, their combination introduces a global order in the rows of the table, which in the future will help to support cursors for streaming queries.

How this is different from allow_experimental_block_number_column? #47532 It has strict order.

Combination of columns (_queue_block_number, _queue_block_offset) gives a strict order over table rows inside each partition. _block_number by itself only gives strict order over blocks, which is not enough to express the cursor in any case, but if to add offset inside the block as second order column, the combination will.

_queue_block_number is semantically equal to _block_number, but:

  • it gets into the index correctly at 0-level parts because queue mode needs this columns to be in pk.
  • it is not "virtual" column.

@UnamedRus
Copy link
Copy Markdown
Contributor

this columns to be in pk.

Does it mean that for streaming queries, you need to have specific primary key?

Each new insert create new part, so in general data with latest _queue_block_offset will be naturally isolated for a while, so performance wise it can work well even without having those columns in PK.

@Michicosun
Copy link
Copy Markdown
Member Author

Does it mean that for streaming queries, you need to have specific primary key?

Yes, the first idea in issue was to create some column like sequence-id, but it does not link to block numbers synchronization, so it is better to use block number in data ordering.

Primary key and Ordering key will contain _queue_block_number, _queue_block_offset as first columns, so data will be naturally ordered after merges with respect to time of origination.

Each new insert create new part, so in general data with latest _queue_block_offset will be naturally isolated for a while, so performance wise it can work well even without having those columns in PK.

In streaming queries should be possible to create stream from any offset in queue - it is like to cut some prefix and read all suffix as stream, but without this columns in index it will be complicated.

@alexey-milovidov
Copy link
Copy Markdown
Member

@UnamedRus, it is for persistent queues, like Kafka, and they have to support efficient reading from an arbitrary offset - that's why the queue mode requires this offset in the primary key.

@UnamedRus
Copy link
Copy Markdown
Contributor

Ok, i see.

Comment on lines +152 to +160
auto cols = block.block.getColumns();
size_t rows = block.block.rows();

SipHash hash;
for (size_t j = 0; j < rows; ++j)
{
for (const auto & col : cols)
col->updateHashWithValue(j, hash);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This method does the same

void Block::updateHash(SipHash & hash) const
{
for (size_t row_no = 0, num_rows = rows(); row_no < num_rows; ++row_no)
for (const auto & col : data)
col.column->updateHashWithValue(row_no, hash);
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

changed the logic of these methods, added a column filter.

Comment on lines +893 to +894
auto& block_number_lock = lock_holder != nullptr ? lock_holder->block_number_lock : current_try_block_number_lock;
chassert(block_number_lock.has_value());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems a bit useless, it is not used anywhere, but assertion was already performed in line 874.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think it should be leaved, this check helps to understand that in any execution above, lock is initialized.

Removed assert in 874

@Michicosun Michicosun requested a review from kssenii March 22, 2024 08:33
Copy link
Copy Markdown
Member

@kssenii kssenii left a comment

Choose a reason for hiding this comment

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

Sorry for delay in review

if (!partsContainSameProjections(left, right, disable_reason))
return false;

/// If storage in queue mode then block number allocation is performed not under parts lock.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why only in queue mode? Any block number allocation is performed without parts lock

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

For regular MergeTree i think it is not true, as i see final allocation of block number is performed here: https://github.com/ClickHouse/ClickHouse/blob/master/src/Storages/MergeTree/MergeTreeSink.cpp#L174.


CREATE TABLE queue_mode_test(a UInt64, b UInt64) ENGINE=MergeTree() ORDER BY tuple() PARTITION BY a SETTINGS queue_mode=1, index_granularity=1;

INSERT INTO queue_mode_test (*) SELECT 0, number FROM numbers(5); -- _queue_block_number = 1
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

let's add a test with async inserts

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

added for MergeTree

@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Jul 2, 2024

Dear @kssenii, this PR hasn't been updated for a while. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@kssenii kssenii self-assigned this Jul 2, 2024
@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Aug 6, 2024

Dear @kssenii, this PR hasn't been updated for a while. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-feature Pull request with new product feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants