Skip to content

Limit memory usage of exchange sender#10387

Merged
ti-chi-bot[bot] merged 9 commits intopingcap:masterfrom
windtalker:limit_memory_usage_of_exchange_sender
Sep 5, 2025
Merged

Limit memory usage of exchange sender#10387
ti-chi-bot[bot] merged 9 commits intopingcap:masterfrom
windtalker:limit_memory_usage_of_exchange_sender

Conversation

@windtalker
Copy link
Contributor

@windtalker windtalker commented Sep 1, 2025

What problem does this PR solve?

Issue Number: close #10337

Problem Summary:

The root cause is in current implementation, the buffered block number in ExchangeSender can be upto max_threads * max_threads * fine_grain_stream_count, so if max_threads is large, and the size of each row is big, the buffered block can consume very large memory.

This pr limit the memory usage of ExchangeSender by max(MAX_BATCH_SEND_MIN_LIMIT_MEM_SIZE * max_threads, max_buffered_bytes_in_executor)

What is changed and how it works?


Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
    test in 2-tiflash's cluster, with max_threads = 32 and sort memory limit set to 5G
explain analyze select * from (select *, row_number() over(partition by ps_partkey order by ps_suppkey) as rn from test) d where rn = 10;

Where test has 40000000 rows, each row has ~1200 bytes.
Before this pr
peak memory of each TiFlash is about 30G

[2025/09/01 11:47:48.178 +08:00] [DEBUG] [MemoryTracker.cpp:110] ["Peak memory usage (for query): 33.13 GiB."] [source=MemoryTracker] [thread_id=813]
[2025/09/01 11:47:48.178 +08:00] [DEBUG] [MemoryTracker.cpp:110] ["Peak memory usage (total): 33.13 GiB."] [source=MemoryTracker] [thread_id=813]

After this pr
peak memory of each TiFlash is about 10G

[2025/09/01 11:42:28.895 +08:00] [DEBUG] [MemoryTracker.cpp:110] ["Peak memory usage (for query): 9.86 GiB."] [source=MemoryTracker] [thread_id=635]
[2025/09/01 11:42:28.895 +08:00] [DEBUG] [MemoryTracker.cpp:110] ["Peak memory usage (total): 9.86 GiB."] [source=MemoryTracker] [thread_id=635]
  • 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

@ti-chi-bot ti-chi-bot bot added 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. 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 Sep 1, 2025
@windtalker windtalker requested a review from Copilot September 2, 2025 02:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR limits memory usage of ExchangeSender by adding a max_buffered_bytes parameter to control buffering across all writer types. The root cause of high memory usage was unlimited buffering based only on row counts, which could consume excessive memory with large rows and high thread counts.

Key changes:

  • Adds max_buffered_bytes parameter to all exchange writer constructors and functions
  • Implements needFlush() method that checks both byte and row limits for flushing decisions
  • Introduces getMaxBufferedBytesInResponseWriter() utility function to calculate appropriate buffer limits

Reviewed Changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
PhysicalExchangeSender.cpp Adds concurrency calculation and max_buffered_bytes parameter to writer creation
newMPPExchangeWriter.h/.cpp Adds max_buffered_bytes parameter to function signatures and passes it through
HashPartitionWriter.h/.cpp Refactors to use base class buffering fields and byte-based flushing
FineGrainedShuffleWriter.h/.cpp Similar refactoring to use unified buffering approach
BroadcastOrPassThroughWriter.h/.cpp Updates to use base class buffering and byte limits
StreamingDAGResponseWriter.h/.cpp Refactors buffering logic to use base class implementation
DAGResponseWriter.h Adds base buffering fields and needFlush() method
DAGUtils.h/.cpp Adds utility function to calculate max buffered bytes
DAGDriver.cpp Updates streaming writer creation with new parameter
Test files Updates all test cases to include new max_buffered_bytes parameter

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +160 to +164
if (scattered[col_id][chunk_id]->valuesHaveFixedSize())
{
// Reserve space for each chunk to avoid frequent memory allocation.
scattered[col_id][chunk_id]->reserve(1024);
}
Copy link

Copilot AI Sep 2, 2025

Choose a reason for hiding this comment

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

The hardcoded value 1024 should be defined as a named constant to improve maintainability and make the purpose clearer.

Copilot uses AI. Check for mistakes.
@ti-chi-bot ti-chi-bot bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Sep 2, 2025
@windtalker
Copy link
Contributor Author

/test pull-unit-test

@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Sep 4, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: gengliqi, solotzg

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

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Sep 4, 2025
@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Sep 4, 2025

[LGTM Timeline notifier]

Timeline:

  • 2025-09-02 03:03:23.870779078 +0000 UTC m=+841295.748610022: ☑️ agreed by solotzg.
  • 2025-09-04 05:51:28.679598855 +0000 UTC m=+72632.662696293: ☑️ agreed by gengliqi.

@windtalker
Copy link
Contributor Author

/test pull-unit-test

@windtalker windtalker force-pushed the limit_memory_usage_of_exchange_sender branch from 3e85f09 to 6d89003 Compare September 4, 2025 10:13
@windtalker
Copy link
Contributor Author

/hold

@ti-chi-bot ti-chi-bot bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 4, 2025
@windtalker
Copy link
Contributor Author

/test pull-integration-next-gen

windtalker and others added 9 commits September 5, 2025 10:04
Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
@windtalker windtalker force-pushed the limit_memory_usage_of_exchange_sender branch from 220960d to 5ca77f3 Compare September 5, 2025 02:05
@windtalker
Copy link
Contributor Author

/hold cancel

@ti-chi-bot ti-chi-bot bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 5, 2025
@windtalker
Copy link
Contributor Author

/test pull-unit-test

@ti-chi-bot ti-chi-bot bot merged commit 6fce865 into pingcap:master Sep 5, 2025
7 checks passed
@ti-chi-bot ti-chi-bot bot added the needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. label Sep 8, 2025
ti-chi-bot pushed a commit to ti-chi-bot/tiflash that referenced this pull request Sep 8, 2025
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-8.5: #10417.
But this PR has conflicts, please resolve them!

ti-chi-bot bot pushed a commit that referenced this pull request Sep 9, 2025
close #10337

Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>

Co-authored-by: xufei <xufeixw@mail.ustc.edu.cn>
Co-authored-by: xufei <xufei@pingcap.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved lgtm needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fine grained shuffle occupy too much memory when max_threads is large

5 participants