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

br: set memory limit #53793

Merged
merged 8 commits into from
Aug 5, 2024
Merged

br: set memory limit #53793

merged 8 commits into from
Aug 5, 2024

Conversation

Leavrth
Copy link
Contributor

@Leavrth Leavrth commented Jun 4, 2024

What problem does this PR solve?

Issue Number: close #53777

Problem Summary:
need to automatically adjust GOMEMLIMIT for br clp

What changed and how does it work?

automatically adjust GOMEMLIMIT for br clp

[INFO] [meminfo.go:196] ["use physical memory hook"] [cgroupMemorySize=9223372036854771712] [physicalMemorySize=33731502080]
[INFO] [cmd.go:168] ["memory limit"] [limit=9223372036854775807]
[INFO] [cmd.go:189] [memory] [memtotal=33731502080] [memused=5004333056]
[INFO] [cmd.go:191] ["set memory limit"] [limit=28190298112]
[INFO] [meminfo.go:179] ["use cgroup memory hook because TiDB is in the container"]
[INFO] [cmd.go:168] ["memory limit"] [limit=9223372036854775807]
[INFO] [cmd.go:189] [memory] [memtotal=8589934592] [memused=971722752]
[INFO] [cmd.go:191] ["set memory limit"] [limit=7081340928]

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

Signed-off-by: Jianjun Liao <jianjun.liao@outlook.com>
@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jun 4, 2024
Copy link

tiprow bot commented Jun 4, 2024

Hi @Leavrth. 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.

Copy link

codecov bot commented Jun 4, 2024

Codecov Report

Attention: Patch coverage is 69.23077% with 12 lines in your changes missing coverage. Please review.

Project coverage is 60.5272%. Comparing base (d760a06) to head (1e15fea).
Report is 474 commits behind head on master.

Additional details and impacted files
@@                Coverage Diff                @@
##             master     #53793         +/-   ##
=================================================
- Coverage   72.5737%   60.5272%   -12.0466%     
=================================================
  Files          1506       1712        +206     
  Lines        431247     673023     +241776     
=================================================
+ Hits         312972     407362      +94390     
- Misses        98987     241598     +142611     
- Partials      19288      24063       +4775     
Flag Coverage Δ
integration 42.0422% <69.2307%> (?)
unit 74.5862% <33.3333%> (+3.0291%) ⬆️

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

Components Coverage Δ
dumpling 53.3749% <ø> (+0.4092%) ⬆️
parser ∅ <ø> (∅)
br 62.9318% <69.2307%> (+21.1187%) ⬆️

Signed-off-by: Jianjun Liao <jianjun.liao@outlook.com>
br/cmd/br/cmd.go Outdated Show resolved Hide resolved
Signed-off-by: Jianjun Liao <jianjun.liao@outlook.com>
@ti-chi-bot ti-chi-bot bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jun 5, 2024
Signed-off-by: Jianjun Liao <jianjun.liao@outlook.com>
@ti-chi-bot ti-chi-bot bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jul 10, 2024
Copy link
Contributor

@Tristan1900 Tristan1900 left a comment

Choose a reason for hiding this comment

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

Overall LGTM, thanks for the change. just a few questions and suggestions,

br/cmd/br/cmd.go Outdated
memlimit := calculateMemoryLimit(memleft)
log.Info("calculate the rest memory",
zap.Uint64("memtotal", memtotal), zap.Uint64("memused", memused), zap.Uint64("memlimit", memlimit))
debug.SetMemoryLimit(int64(memlimit))
Copy link
Contributor

Choose a reason for hiding this comment

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

to be extra safe, can we do a safe cast from uint64 to int64, something like

func safeCastUint64ToInt64(u uint64) int64 {
	if u > uint64(math.MaxInt64) {
		return math.MaxInt64
	}
	return int64(u)
}

and make it a util method?
cuz in the doc
A zero limit or a limit that's lower than the amount of memory used by the Go runtime may cause the garbage collector to run nearly continuously. However, the application may still make progress.

Copy link
Contributor Author

@Leavrth Leavrth Jul 19, 2024

Choose a reason for hiding this comment

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

done.

br/cmd/br/cmd.go Show resolved Hide resolved
const halfGB = uint64(512 * 1024 * 1024)
const fourGB = 8 * halfGB
// memreserved = f(memleft) = 512MB * memleft / (memleft + 4GB)
// * f(0) = 0
Copy link
Contributor

Choose a reason for hiding this comment

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

if the mem unused is 0, do we even bother to start the BR? I guess a correctly configured machine should not use up all its mem and if they do they are facing a bigger problem and we probably don't want to start a BR to make things more complicated. Let me know your thoughts!

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 think maybe it is already OOM. Maybe we can directly limit the minimum remaining memory.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, I feel like we can have some minimum memory not starting from 0, if below that we just throw error and exit.

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 add the minimum value (256MiB) of memory limit. It's hard to determine the minimum memory br usage. In my test, br uses only about 200 MB~300 MB memory to backup data generated by tiup bench tpcc prepare --warehouses 3.

Copy link
Contributor

@Tristan1900 Tristan1900 Jul 22, 2024

Choose a reason for hiding this comment

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

thanks for digging into that, yeah having an estimation of memory usage is very useful.

br/cmd/br/cmd.go Outdated
@@ -107,6 +110,23 @@ func AddFlags(cmd *cobra.Command) {
_ = cmd.PersistentFlags().MarkHidden(FlagRedactLog)
}

func calculateMemoryLimit(memleft uint64) uint64 {
const halfGB = uint64(512 * 1024 * 1024)
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like it would be better to put it into some util file for reuse.

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. Reuse the tidb/pkg/util/size.MB.

br/cmd/br/cmd.go Outdated
// * f(0) = 0
// * f(4GB) = 256MB
// * f(+inf) -> 512MB
memreserved := halfGB / (1 + fourGB/(memleft+1))
Copy link
Contributor

Choose a reason for hiding this comment

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

extreme case, if memleft is unit64 max it's going to overflow I guess

Copy link
Contributor

Choose a reason for hiding this comment

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

in what case would memleft be uint64max (= 16 EiB) 👀

anyway changing memleft + 1 to memleft | 1 should be good enough

Copy link
Contributor

@Tristan1900 Tristan1900 Jul 18, 2024

Choose a reason for hiding this comment

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

in the unexpected error case I would say, the memleft is passed down by memleft := memtotal - memused, if memtotal < memused, it can underflow as in my other comment. it's always better to explicitly check and make sure things won't go wrong for corners cases, cuz a lot of assumptions can turn out to be wrong in production based on my experience : ) (in this case the assumption is memory.MemTotal >= memory.MemUsed )

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. And add check memtotal < memused in another comment.

br/cmd/br/cmd.go Outdated
err = e
return
}
memleft := memtotal - memused
Copy link
Contributor

@Tristan1900 Tristan1900 Jul 15, 2024

Choose a reason for hiding this comment

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

do we need to add a safety check here to make sure memtotal-memused won't underflow? since we own the memory package and it might have a bug that returned memtotal is actually smaller than memused

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 underflow, output a warning log and skip set memory limit.

Signed-off-by: Jianjun Liao <jianjun.liao@outlook.com>
Signed-off-by: Jianjun Liao <jianjun.liao@outlook.com>
Signed-off-by: Jianjun Liao <jianjun.liao@outlook.com>
Signed-off-by: Jianjun Liao <jianjun.liao@outlook.com>
@BornChanger
Copy link
Contributor

/retest

Copy link

tiprow bot commented Jul 22, 2024

@BornChanger: 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.

@kennytm
Copy link
Contributor

kennytm commented Jul 22, 2024

/retest-required

Copy link

tiprow bot commented Jul 22, 2024

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

In response to this:

/retest-required

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.

memleft := memtotal - memused
memlimit := calculateMemoryLimit(memleft)
// BR command needs 256 MiB at least, if the left memory is less than 256 MiB,
// the memory limit cannot limit anyway and then finally OOM.
Copy link
Contributor

@Tristan1900 Tristan1900 Jul 22, 2024

Choose a reason for hiding this comment

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

It seems at here the code will keep running and let the system OOM and stop.
In my mind it's better to explicitly panic or throw error by ourselves and exit early instead of actually OOM the system, OOM might cause unexpected issue to the other processes running on the same machine. what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

rest LGTM!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The memory usage of br depends in part on the cluster tables' information. Only when br starts a domain to loads all the cluster's table's information into memory, it will know how much memory it will actually use at least. Maybe it is 200 MB or 1 GB, so I think it's hard to determine the memory usage here. Besides, setting go memory limit in this PR just makes the process runtime free the unused memory back to system in time, I think estimate the process memory usage is another issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the command is br debug ..., it will use a little memory. Maybe more work is needed.

Copy link
Contributor

@3pointer 3pointer left a comment

Choose a reason for hiding this comment

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

LGTM

log.Info("calculate the rest memory",
zap.Uint64("memtotal", memtotal), zap.Uint64("memused", memused), zap.Uint64("memlimit", memlimit))
// No need to set memory limit because the left memory is sufficient.
if memlimit < uint64(math.MaxInt64) {
Copy link
Contributor

Choose a reason for hiding this comment

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

when will memlimit large than math.MaxInt64

Copy link

ti-chi-bot bot commented Aug 5, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: 3pointer, BornChanger

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 lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Aug 5, 2024
Copy link

ti-chi-bot bot commented Aug 5, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-07-10 08:13:04.635116729 +0000 UTC m=+431681.870350842: ☑️ agreed by BornChanger.
  • 2024-08-05 05:46:12.215652097 +0000 UTC m=+244502.082751186: ☑️ agreed by 3pointer.

@Leavrth
Copy link
Contributor Author

Leavrth commented Aug 5, 2024

/ok-to-test

@ti-chi-bot ti-chi-bot bot added the ok-to-test Indicates a PR is ready to be tested. label Aug 5, 2024
@Leavrth
Copy link
Contributor Author

Leavrth commented Aug 5, 2024

/retest

Copy link

tiprow bot commented Aug 5, 2024

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

Test name Commit Details Required Rerun command
tidb_parser_test 1e15fea link true /test tidb_parser_test
fast_test_tiprow 1e15fea 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.

@ti-chi-bot ti-chi-bot bot merged commit 2c47cd5 into pingcap:master Aug 5, 2024
30 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm ok-to-test Indicates a PR is ready to be tested. release-note-none Denotes a PR that doesn't merit a release note. 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.

Automatically adjust GOMEMLIMIT for br clp
5 participants