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

membuffer: fix memory leak in red-black tree #1483

Merged
merged 9 commits into from
Nov 14, 2024

Conversation

you06
Copy link
Contributor

@you06 you06 commented Oct 31, 2024

close #1375, ref pingcap/tidb#56837

The issue #1375 figures out that the RBT membuffer cannot reuse free nodes which causes a memory leak. This pr uses marking the node as deleted to replace the actual deletion.

Pros:

  • Because the memory arena cannot handle the memory fragmentation well, we avoid the memory leak by this mark-delete strategy.

Cons:

  • In certain cases, the mark-delete tree may be taller than the actual deletion, which can slightly impact the performance.

Signed-off-by: you06 <you1474600@gmail.com>
@ti-chi-bot ti-chi-bot bot added dco-signoff: yes Indicates the PR's author has signed the dco. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 31, 2024
you06 added 3 commits October 31, 2024 17:22
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
@you06
Copy link
Contributor Author

you06 commented Nov 12, 2024

@cfzjywxk I test this PR (with RBT enabled) is ok to pass TiDB's CI, PTAL.

@ti-chi-bot ti-chi-bot bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Nov 13, 2024
internal/unionstore/rbt/rbt.go Show resolved Hide resolved
internal/unionstore/rbt/rbt_test.go Outdated Show resolved Hide resolved
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Copy link
Contributor

@cfzjywxk cfzjywxk left a comment

Choose a reason for hiding this comment

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

@you06
Do we have a benchmark test with tidb to verify the memory leak issue is resolved?

internal/unionstore/rbt/rbt.go Outdated Show resolved Hide resolved
Copy link
Contributor

@crazycs520 crazycs520 left a comment

Choose a reason for hiding this comment

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

REST LGTM

you06 and others added 2 commits November 13, 2024 23:01
Co-authored-by: cfzjywxk <cfzjywxk@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
@you06
Copy link
Contributor Author

you06 commented Nov 14, 2024

Do we have a benchmark test with tidb to verify the memory leak issue is resolved?

This test can reproduce the memory leak issue in TiDB side, will add it to TiDB.

func TestInsertRollbackMemoryLeak(t *testing.T) {
	store := testkit.CreateMockStore(t)
	tk := testkit.NewTestKit(t, store)
	tk.MustExec("use test")
	tk.MustExec("create table t(a varchar(255) primary key)")
	key1 := strings.Repeat("a", 255)
	key2 := strings.Repeat("b", 255)
	tk.MustExec(`set global tidb_mem_oom_action='cancel'`)
	tk.MustExec("set session tidb_mem_quota_query=10240")
	tk.MustExec("begin")
	tk.MustExec("insert into t values(?)", key2)
	for i := 0; i < 100; i++ {
		err := tk.ExecToErr("insert into t values(?), (?)", key1, key2)
		require.Error(t, err)
		if strings.Contains(err.Error(), "Duplicate") {
			continue
		}
		require.NoError(t, err)
	}
	tk.MustExec("commit")
}

Copy link
Contributor

@cfzjywxk cfzjywxk 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 bot added the lgtm label Nov 14, 2024
Copy link

ti-chi-bot bot commented Nov 14, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cfzjywxk, zyguan

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 removed the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Nov 14, 2024
Copy link

ti-chi-bot bot commented Nov 14, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-11-13 06:07:49.885070903 +0000 UTC m=+422832.075939892: ☑️ agreed by zyguan.
  • 2024-11-14 14:37:56.801349824 +0000 UTC m=+539838.992218823: ☑️ agreed by cfzjywxk.

@ti-chi-bot ti-chi-bot bot merged commit 86678db into tikv:master Nov 14, 2024
12 checks passed
you06 added a commit to you06/client-go that referenced this pull request Nov 18, 2024
Signed-off-by: you06 <you1474600@gmail.com>
you06 added a commit to you06/client-go that referenced this pull request Nov 18, 2024
Signed-off-by: you06 <you1474600@gmail.com>
you06 added a commit to you06/client-go that referenced this pull request Nov 18, 2024
Signed-off-by: you06 <you1474600@gmail.com>
you06 added a commit to you06/client-go that referenced this pull request Nov 18, 2024
Signed-off-by: you06 <you1474600@gmail.com>
you06 added a commit to you06/client-go that referenced this pull request Nov 18, 2024
Signed-off-by: you06 <you1474600@gmail.com>
ti-chi-bot bot pushed a commit that referenced this pull request Nov 25, 2024
 

Signed-off-by: you06 <you1474600@gmail.com>
ti-chi-bot bot pushed a commit that referenced this pull request Nov 25, 2024
 

Signed-off-by: you06 <you1474600@gmail.com>
ti-chi-bot bot pushed a commit that referenced this pull request Nov 25, 2024
 

Signed-off-by: you06 <you1474600@gmail.com>
ti-chi-bot bot pushed a commit that referenced this pull request Nov 25, 2024
 

Signed-off-by: you06 <you1474600@gmail.com>
ti-chi-bot bot pushed a commit that referenced this pull request Nov 25, 2024
 

Signed-off-by: you06 <you1474600@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved dco-signoff: yes Indicates the PR's author has signed the dco. lgtm size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MemDB does not reuse free nodes
4 participants