-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
br: set memory limit #53793
Conversation
Signed-off-by: Jianjun Liao <jianjun.liao@outlook.com>
Hi @Leavrth. Thanks for your PR. PRs from untrusted users cannot be marked as trusted with 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. |
Codecov ReportAttention: Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more.
|
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>
There was a problem hiding this 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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
const halfGB = uint64(512 * 1024 * 1024) | ||
const fourGB = 8 * halfGB | ||
// memreserved = f(memleft) = 512MB * memleft / (memleft + 4GB) | ||
// * f(0) = 0 |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
)
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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>
/retest |
@BornChanger: Cannot trigger testing until a trusted user reviews the PR and leaves an In response to this:
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. |
/retest-required |
@kennytm: Cannot trigger testing until a trusted user reviews the PR and leaves an In response to this:
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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rest LGTM!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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
[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 |
[LGTM Timeline notifier]Timeline:
|
/ok-to-test |
/retest |
@Leavrth: The following tests failed, say
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. |
What problem does this PR solve?
Issue Number: close #53777
Problem Summary:
need to automatically adjust
GOMEMLIMIT
for br clpWhat changed and how does it work?
automatically adjust
GOMEMLIMIT
for br clpCheck List
Tests
Side effects
Documentation
Release note
Please refer to Release Notes Language Style Guide to write a quality release note.