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

ddl notifier: use pagination for SELECT to reduce memory usage #58376

Merged
merged 6 commits into from
Dec 23, 2024

Conversation

lance6716
Copy link
Contributor

@lance6716 lance6716 commented Dec 18, 2024

What problem does this PR solve?

Issue Number: close #58368

Problem Summary:

What changed and how does it work?

as title. refine the Store.List.

// Store is the (de)serialization and persistent layer.
type Store interface {
...
	// List will start a transaction of given session and read all schema changes
	// through a ListResult. The ownership of session is occupied Store until CloseFn
	// is called.
	List(ctx context.Context, se *sess.Session) ListResult
}

// ListResult is the result stream of a List operation.
type ListResult interface {
	// Read tries to decode at most `len(changes)` SchemaChange into given slices. It
	// returns the number of schemaChanges decoded, 0 means no more schemaChanges.
	//
	// Note that the previous SchemaChange in the slice will be overwritten when call
	// Read.
	Read(changes []*SchemaChange) (int, error)
}

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

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

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/needs-triage-completed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Dec 18, 2024
Copy link

tiprow bot commented Dec 18, 2024

Hi @lance6716. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

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 kubernetes-sigs/prow repository.

@lance6716 lance6716 requested a review from Copilot December 18, 2024 08:16
@Rustin170506 Rustin170506 requested review from Rustin170506 and time-and-fate and removed request for Copilot December 18, 2024 08:17
@lance6716 lance6716 requested a review from Copilot December 18, 2024 08:20
@lance6716
Copy link
Contributor Author

/check-issue-triage-complete

Choose a reason for hiding this comment

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

pkg/ddl/notifier/subscribe.go:171

  • The variable name ProcessEventsBatchSize is clear and consistent with the changes made throughout the code.
var ProcessEventsBatchSize = 1024
Signed-off-by: lance6716 <[email protected]>
Copy link

codecov bot commented Dec 18, 2024

Codecov Report

Attention: Patch coverage is 82.45614% with 20 lines in your changes missing coverage. Please review.

Project coverage is 75.2997%. Comparing base (1405c5e) to head (9536b86).
Report is 44 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #58376        +/-   ##
================================================
+ Coverage   73.1849%   75.2997%   +2.1148%     
================================================
  Files          1681       1727        +46     
  Lines        463027     480149     +17122     
================================================
+ Hits         338866     361551     +22685     
+ Misses       103358      96359      -6999     
- Partials      20803      22239      +1436     
Flag Coverage Δ
integration 49.3698% <76.3157%> (?)
unit 72.7383% <82.4561%> (+0.4215%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 53.0100% <ø> (+0.3190%) ⬆️
parser ∅ <ø> (∅)
br 60.8815% <ø> (+14.8795%) ⬆️

@lance6716
Copy link
Contributor Author

/retest

Copy link

tiprow bot commented Dec 18, 2024

@lance6716: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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 kubernetes-sigs/prow repository.

pkg/ddl/notifier/store.go Show resolved Hide resolved
changes := make([]*SchemaChange, ProcessEventsBatchSize)

for {
count, err2 := result.Read(changes)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not let read return []*SchemaChange directly?
Return count and use changes[:count] is just a unnecessary detour

Copy link
Contributor Author

@lance6716 lance6716 Dec 19, 2024

Choose a reason for hiding this comment

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

It's like an io.Reader calling convention which is caller pass-in slices so caller can reuse the previous results. Because we are in 1M tables project, I'm afraid that returning 1M []*SchemaChange (it also has internal pointers like TableInfo) will add pressure to GC.

Though it's less common, I think this package / function is the only caller of ListResult.Read. Other developers will not modify this package and will not be confused by it.

However I notice that I use json.Unmarshal on an non-empty object, not sure the leftover of the object will be overwritten or causes some problems. I'll check it tomorrow.

Copy link
Member

Choose a reason for hiding this comment

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

What was your test result?

Copy link
Contributor

Choose a reason for hiding this comment

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

 pass-in slices so caller can reuse the previous results

Yes, I mean the same thing, just that the code can be write as:

func f(changes []*SchemaChange) []*SchemaChange {
     ret := changes[:0]
     ret = append(ret, xxx)
     return ret
}

Anyway, this is just some personal taste, so you can choose to accept it or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What was your test result?

I didn't test it yet 😂 I hope other colleague can help check it. Theoretically there's no big query now.

func f(changes []*SchemaChange) []*SchemaChange {

In this new signature, the batch size of one Read is not obvious when input is nil, or it seems like an append where the slice will be grown and batch size is not be limited. Your suggestion is like EncodeBytes but here we want to control the batch size as well as reuse buffer.

Signed-off-by: lance6716 <[email protected]>
Signed-off-by: lance6716 <[email protected]>
Signed-off-by: lance6716 <[email protected]>
@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Dec 20, 2024
Signed-off-by: lance6716 <[email protected]>
Copy link
Member

@Rustin170506 Rustin170506 left a comment

Choose a reason for hiding this comment

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

Better to verify if this can fix the memory consumption issue. before merging it.

Thanks! :shipit:

/hold

Comment on lines +184 to +192
if changes[i] == nil {
changes[i] = new(SchemaChange)
}
if changes[i].event == nil {
changes[i].event = new(SchemaChangeEvent)
}
if changes[i].event.inner == nil {
changes[i].event.inner = new(jsonSchemaChangeEvent)
}
Copy link
Member

@Rustin170506 Rustin170506 Dec 23, 2024

Choose a reason for hiding this comment

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

How about:

// Add constructor function
func NewSchemaChange() *SchemaChange {
    return &SchemaChange{
        event: &SchemaChangeEvent{
            inner: &jsonSchemaChangeEvent{},
        },
    }
}


if changes[i] == nil {
    changes[i] = NewSchemaChange()
}

Copy link
Member

Choose a reason for hiding this comment

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

Or do you mean there is a case that changes[i] is not nil, but the event or the inner is nil?

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 to improve robustness, I don't want to assume caller uses some kind of SchemaChange.

I'll test the OOM problem soon

@ti-chi-bot ti-chi-bot bot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Dec 23, 2024
Copy link

ti-chi-bot bot commented Dec 23, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-12-20 10:14:14.361822185 +0000 UTC m=+1211044.450624723: ☑️ agreed by tiancaiamao.
  • 2024-12-23 04:11:19.650829735 +0000 UTC m=+1448469.739632278: ☑️ agreed by Rustin170506.

Copy link

tiprow bot commented Dec 23, 2024

@lance6716: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
fast_test_tiprow 9536b86 link true /test fast_test_tiprow

Full PR test history. Your PR dashboard.

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 kubernetes-sigs/prow repository. I understand the commands that are listed here.

@Rustin170506 Rustin170506 added the needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. label Dec 23, 2024
@D3Hunter
Copy link
Contributor

/approve

Copy link

ti-chi-bot bot commented Dec 23, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: D3Hunter, Rustin170506, tiancaiamao

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

The pull request process is described here

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 the approved label Dec 23, 2024
@lance6716
Copy link
Contributor Author

/unhold

due to test resource limit, I can't create a test cluster before the coming version release. So I plan to test the effect later, and don't cherry-pick to v8.5.1 before we get the test result

@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 Dec 23, 2024
@lance6716
Copy link
Contributor Author

/retest

Copy link

tiprow bot commented Dec 23, 2024

@lance6716: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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 kubernetes-sigs/prow repository.

@ti-chi-bot ti-chi-bot bot merged commit 5fbe4fb into pingcap:master Dec 23, 2024
26 of 27 checks passed
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-8.5: #58488.

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.

TiDB OOM after created about 6.5M tables
5 participants