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

Improve the performance of partition table in extreme case #4988

Merged
merged 31 commits into from
Jun 8, 2022

Conversation

bestwoody
Copy link
Contributor

@bestwoody bestwoody commented May 24, 2022

What problem does this PR solve?

Issue Number: close #4474

Problem Summary:
Improve the performance of partition table in extreme case

What is changed and how it works?

use Multiplex technique to balancely read underlying DMSegmentInputstream to avoid consume starvation.
design doc: https://pingcap.feishu.cn/wiki/wikcntp2B8GEXyFLSbIbAtMJzBG?useNewLarklet=1
benchmark result: https://pingcap.feishu.cn/wiki/wikcntp2B8GEXyFLSbIbAtMJzBG?useNewLarklet=1#0fWIDd

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

None

Signed-off-by: bestwoody <bestwoody@163.com>
Signed-off-by: bestwoody <bestwoody@163.com>
Signed-off-by: bestwoody <bestwoody@163.com>
@ti-chi-bot
Copy link
Member

ti-chi-bot commented May 24, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • windtalker
  • yibin87

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added the release-note-none Denotes a PR that doesn't merit a release note. label May 24, 2022
@ti-chi-bot ti-chi-bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label May 24, 2022
Signed-off-by: bestwoody <bestwoody@163.com>
@ti-chi-bot ti-chi-bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 30, 2022
dbms/src/DataStreams/MultiplexInputStream.h Show resolved Hide resolved
streams_queue_id = 0;

auto & q_ptr = streams_queue_by_partition[streams_queue_id];
auto & q = *q_ptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why need q?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just make code easier to read&write... and to show it will not be a nullptr.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, I figure out, since it's a std::shared_ptr, use dereference tech to avoid triger atomic counter of std::shared_ptr.

Copy link
Contributor

Choose a reason for hiding this comment

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

Then why not merge L56 and L57 as
auto & q = *streams_queue_by_partition[stream_queue_id]?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good idea

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

@@ -777,7 +780,10 @@ void DAGStorageInterpreter::buildLocalStreams(DAGPipeline & pipeline, size_t max
throw;
}
}
pipeline.streams.insert(pipeline.streams.end(), current_pipeline.streams.begin(), current_pipeline.streams.end());
}
for (int i = 0; i < static_cast<int>(max_streams); i++)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we don't need to use MultiplexInputStream for non-partition table scan or partition table scan with only one partition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with you, the only trouble is the code will be more dirty when two cases are not in a universal code path.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

dbms/src/Flash/Coprocessor/DAGStorageInterpreter.cpp Outdated Show resolved Hide resolved
dbms/src/Flash/Coprocessor/DAGStorageInterpreter.cpp Outdated Show resolved Hide resolved
dbms/src/DataStreams/MultiplexInputStream.h Show resolved Hide resolved
dbms/src/DataStreams/MultiplexInputStream.h Outdated Show resolved Hide resolved
dbms/src/DataStreams/MultiplexInputStream.h Outdated Show resolved Hide resolved
@ti-chi-bot ti-chi-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jun 1, 2022
Copy link
Contributor

@yibin87 yibin87 left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Jun 1, 2022
dbms/src/Flash/Coprocessor/DAGStorageInterpreter.cpp Outdated Show resolved Hide resolved
@@ -642,8 +646,7 @@ void DAGStorageInterpreter::buildLocalStreams(DAGPipeline & pipeline, size_t max
if (region_num == 0)
continue;
/// calculate weighted max_streams for each partition, note at least 1 stream is needed for each partition
size_t current_max_streams = table_query_infos.size() == 1 ? max_streams : (max_streams * region_num + total_local_region_num - 1) / total_local_region_num;

size_t current_max_streams = max_streams;
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems we don't need current_max_streams anymore, just use max_streams is enough.

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

if (has_multiple_partitions)
{
String req_info = dag_context.isMPPTask() ? dag_context.getMPPTaskId().toString() : "";
for (int i = 0; i < static_cast<int>(max_streams); ++i)
Copy link
Contributor

Choose a reason for hiding this comment

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

The stream number should be std::min(max_streams, the stream number in stream_pool)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, nice find

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

streams_queue_id = 0;

auto & q_ptr = streams_queue_by_partition[streams_queue_id];
auto & q = *q_ptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

Then why not merge L56 and L57 as
auto & q = *streams_queue_by_partition[stream_queue_id]?

@ti-chi-bot ti-chi-bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 6, 2022
Comment on lines +42 to +44
std::make_shared<std::queue<std::shared_ptr<IBlockInputStream>>>());
for (const auto & stream : cur_streams)
streams_queue_by_partition.back()->push(stream);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: you could construct the queue before entering the critical area.

auto queue = std::make_shared<std::queue<BlockInputStreamPtr>>(cur_streams.begin(), cur_streams.end());

std::unique_lock lk(mu);

Comment on lines +101 to +104
if (queue_id + 1 < static_cast<int>(streams_queue_by_partition.size()))
return queue_id + 1;
else
return 0;
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
if (queue_id + 1 < static_cast<int>(streams_queue_by_partition.size()))
return queue_id + 1;
else
return 0;
return (queue_id + 1) % streams_queue_by_partition.size();

dbms/src/DataStreams/MultiplexInputStream.h Outdated Show resolved Hide resolved
std::shared_ptr<std::queue<
std::shared_ptr<IBlockInputStream>>>>
streams_queue_by_partition;
std::vector<std::shared_ptr<IBlockInputStream>> added_streams;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use BlockInputStreamPtr other than std::shared_ptr<IBlockInputStream> .

Comment on lines +116 to +117
std::shared_ptr<std::queue<
std::shared_ptr<IBlockInputStream>>>>
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: according to ClickHouse's coding style, better to add an alias for std::queue<std::shared_ptr<IBlockInputStream>> like:

using BlockInputStreamPtrQueue = std::queue<std::shared_ptr<IBlockInputStream>>;

Co-authored-by: Fu Zhe <fuzhe1989@gmail.com>
@ti-chi-bot ti-chi-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jun 7, 2022
Copy link
Contributor

@windtalker windtalker left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Jun 8, 2022
@bestwoody
Copy link
Contributor Author

/merge

@ti-chi-bot
Copy link
Member

@bestwoody: It seems you want to merge this PR, I will help you trigger all the tests:

/run-all-tests

You only need to trigger /merge once, and if the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

If you have any questions about the PR merge process, please refer to pr process.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: b90332f

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Jun 8, 2022
@sre-bot
Copy link
Collaborator

sre-bot commented Jun 8, 2022

Coverage for changed files

Filename                                        Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DataStreams/MultiplexInputStream.h                   73                73     0.00%          16                16     0.00%         134               134     0.00%          48                48     0.00%
Flash/Coprocessor/DAGStorageInterpreter.cpp         436               436     0.00%          32                32     0.00%         883               883     0.00%         290               290     0.00%
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                               509               509     0.00%          48                48     0.00%        1017              1017     0.00%         338               338     0.00%

Coverage summary

Functions  MissedFunctions  Executed  Lines   MissedLines  Cover
18327      9657             47.31%    205617  96719        52.96%

full coverage report (for internal network access only)

@ti-chi-bot ti-chi-bot merged commit 5b61ae7 into pingcap:master Jun 8, 2022
@bestwoody bestwoody deleted the multiplex_dmsegstream branch June 8, 2022 06:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve the performance of partition table in extreme case
7 participants