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

util/mon: pass reserved account by reference #83629

Merged
merged 2 commits into from
Jul 6, 2022

Conversation

yuzefovich
Copy link
Member

@yuzefovich yuzefovich commented Jun 30, 2022

util/mon: pass reserved account by reference

This commit makes it so that we now pass the reserved memory account
when starting up memory monitors by reference. Previously, due to
passing by value, when the monitor is stopped, the copy of the value
would get used so that the actual "reserved" memory account would get
out of sync with its user. This is now fixed. However, this bug doesn't
really have any production impact since we use this "reserved" feature
in a handful of places and these "reserved" memory accounts are not
reused between different usages.

Additionally, this commit renames MakeStandaloneBudget to
NewStandaloneBudget (since it now returns a reference) and adds
a separate "start" method when the caller doesn't want to pre-reserve
anything when starting up a monitor.

Release note: None

sql: make sure to close the reserved account for each session

This commit makes it more clear that the "reserved" memory account
created for each connection is closed when that connection is closed.
Previously, the account was already cleared (when "session root" monitor
is stopped which happens when the connExecutor stops), so there was no
leak in the accounting system because of it, yet it wasn't obvious - this
commit makes it more obvious.

Release note: None

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@yuzefovich yuzefovich force-pushed the close-conn-reserved branch 3 times, most recently from 494db82 to 09e4f6b Compare June 30, 2022 22:44
@yuzefovich yuzefovich changed the title sql: make sure to close the reserved account for each session util/mon: pass reserved account by reference Jun 30, 2022
@yuzefovich yuzefovich requested review from knz and a team June 30, 2022 22:46
@yuzefovich yuzefovich marked this pull request as ready for review June 30, 2022 22:46
@yuzefovich yuzefovich requested review from a team as code owners June 30, 2022 22:46
@yuzefovich yuzefovich requested a review from a team June 30, 2022 22:46
@yuzefovich yuzefovich requested review from a team as code owners June 30, 2022 22:46
@yuzefovich yuzefovich requested review from a team, msbutler, miretskiy and michae2 and removed request for a team, msbutler and miretskiy June 30, 2022 22:46
Copy link
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

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

:lgtm: with tip

Reviewed 57 of 57 files at r1, 2 of 2 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @michae2 and @yuzefovich)


pkg/sql/conn_executor.go line 760 at r2 (raw file):

		// also cannot close it before closeWrapper since we need to close the
		// internal monitors of the connExecutor first.
		defer reserved.Close(ctx)
  1. if you pull the defer out of the closure, you'll save some bytes.
defer reserved.Close(ctx)
defer func() { ... }()
return h.ex.run
  1. you can save the entire allocation by passing the outer variables as arg:
defer func(ctx context.Context, h ConnectionHandler) {...} (ctx, h)

This commit makes it so that we now pass the `reserved` memory account
when starting up memory monitors by reference. Previously, due to
passing by value, when the monitor is stopped, the copy of the value
would get used so that the actual "reserved" memory account would get
out of sync with its user. This is now fixed. However, this bug doesn't
really have any production impact since we use this "reserved" feature
in a handful of places and these "reserved" memory accounts are not
reused between different usages.

Additionally, this commit renames `MakeStandaloneBudget` to
`NewStandaloneBudget` (since it now returns a reference) and adds
a separate "start" method when the caller doesn't want to pre-reserve
anything when starting up a monitor.

Release note: None
This commit makes it more clear that the "reserved" memory account
created for each connection is closed when that connection is closed.
Previously, the account was already cleared (when "session root" monitor
is stopped which happens when the connExecutor stops), so there was no
leak in the accounting system because of it, yet it wasn't obvious - this
commit makes it more obvious.

Release note: None
@yuzefovich yuzefovich force-pushed the close-conn-reserved branch from 581c07d to f1a8710 Compare July 6, 2022 00:15
Copy link
Member Author

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

TFTR!

bors r+

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @knz and @michae2)


pkg/sql/conn_executor.go line 760 at r2 (raw file):

Previously, knz (kena) wrote…
  1. if you pull the defer out of the closure, you'll save some bytes.
defer reserved.Close(ctx)
defer func() { ... }()
return h.ex.run
  1. you can save the entire allocation by passing the outer variables as arg:
defer func(ctx context.Context, h ConnectionHandler) {...} (ctx, h)

Done.

@craig
Copy link
Contributor

craig bot commented Jul 6, 2022

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Jul 6, 2022

Build succeeded:

@craig craig bot merged commit d239a2f into cockroachdb:master Jul 6, 2022
@yuzefovich yuzefovich deleted the close-conn-reserved branch July 6, 2022 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants