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

Add details for setting memory limit automatically. #655

Merged
merged 12 commits into from
Apr 25, 2024
Merged

Conversation

mattdurham
Copy link
Collaborator

PR Description

This will set the automemory limit if cgroups exist and the setting is enabled, the setting defaults to true. If there is a GOMEMLIMIT set already it will not overwrite it. If there are no cgroups then no limit will be applied.

Which issue(s) this PR fixes

Fixes #651

Notes to the Reviewer

PR Checklist

  • CHANGELOG.md updated
  • Documentation added

Copy link
Contributor

@spartan0x117 spartan0x117 left a comment

Choose a reason for hiding this comment

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

LGTM!

// Set the memory limit, this will honor GOMEMLIMIT if set
// If there is a cgroup will follow that
if fr.enableAutoMemoryLimit {
memlimit.SetGoMemLimitWithOpts()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to pass in a compatible logger here? If so, some of the logs might be helpful (e.g. if GOMEMLIMIT is already set it won't honor the cgroups settings, as well as confirmation of the limit)

Copy link
Contributor

Choose a reason for hiding this comment

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

Was thinking something like from the example:

memlimit.SetGoMemLimitWithOpts(
		memlimit.WithRatio(0.9),
		memlimit.WithProvider(memlimit.FromCgroup),
		memlimit.WithLogger(slog.Default()),          // Can we pass a logger here?
	)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can but I have to move it til after our logging is fully initialized, which likely isn't the end of the world its just later in the process to pass down the bool flag.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added the slog.Default, its...a bit different looking but likely works for ease of use.

2024/04/23 17:01:27 INFO memory is not limited, skipping: %v package=github.com/KimMachineGun/automemlimit/memlimit !BADKEY="memory is not limited"

docs/sources/reference/cli/run.md Outdated Show resolved Hide resolved
internal/alloycli/cmd_run.go Outdated Show resolved Hide resolved
mattdurham and others added 4 commits April 23, 2024 16:46
Co-authored-by: Mischa Thompson <mischabear@gmail.com>
Co-authored-by: Mischa Thompson <mischabear@gmail.com>
Comment on lines 200 to 205
// Set the memory limit, this will honor GOMEMLIMIT if set
// If there is a cgroup will follow that
if fr.enableAutoMemoryLimit {
memlimit.SetGoMemLimitWithOpts(memlimit.WithLogger(slog.Default()))
}

Copy link
Member

@rfratto rfratto Apr 24, 2024

Choose a reason for hiding this comment

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

Rather than exposing this as a new configuration option, what do you think about having this automatically be enabled for users using experimental/public-preview, and then once we get enough information on it, we can decide whether it should always be on or needs to be configurable?

(The larger our API surface is the more annoying backwards compatibility can be, so avoiding new configuration options when possible is preferable for me)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

IE if stability is experimental automatically enable? I can also set the flag as experimental, though unsure if we have a precedent for that with alloy.

Copy link
Member

@rfratto rfratto Apr 24, 2024

Choose a reason for hiding this comment

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

IE if stability is experimental automatically enable?

Yeah, like we would say that "auto memory limit is an experimental performance improvement" and only applies when --stability.level=experimental. This is something I expect we'll be doing at some frequency, including for the eventual controller refactor.

I can also set the flag as experimental

We don't have an existing precedent but I expect that most things will support feature gating at some point, including flags.

However in this case I don't think marking the flag as experimental fully addresses my concerns, since presumably we intend for this to GA, and I prefer not adding too many more GA flags because of the increased API surface.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Marked as public preview and added a helper function.

@rfratto rfratto added the backport-to-agent:no PR should NOT be backported to the agent repo. label Apr 24, 2024
Copy link
Member

@rfratto rfratto left a comment

Choose a reason for hiding this comment

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

LGTM following some feedback

@@ -164,6 +164,7 @@ original configuration.
Include `--config.extra-args` to pass additional command line flags from the original format to the converter.
Refer to [alloy convert][] for more details on how `extra-args` work.


Copy link
Member

Choose a reason for hiding this comment

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

IMO I think we should find a way to document this somewhere just so people know which improvements lower stability levels include. I don't know what page it should be on, but I'll leave that up to you since I'm not too opinionated.

// Set the memory limit, this will honor GOMEMLIMIT if set
// If there is a cgroup will follow that
if fr.minStability.AtLeast(featuregate.StabilityPublicPreview) {
memlimit.SetGoMemLimitWithOpts(memlimit.WithLogger(slog.Default()))
Copy link
Member

Choose a reason for hiding this comment

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

I think this should use slog.New(l.Handler()) here instead of slog.Default(); the controller's logger implements the slog.Handler interface. I think this is important so that log entries are emitted in the proper format if the user requests json vs the default.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried that. The format isnt set by this point so it errors, its possible we could default to one?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I could always move it after the logger is resolved but that feels like an odd time to set memory limits.

Copy link
Member

Choose a reason for hiding this comment

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

That seems like a bug in the logging code, it's supposed to buffer logs until the controller evaluates, but it only seems to do that when you're using the go-kit interface and not the slog.Handler interface. I think it might be worth fixing that so we can use the right logger here.

I don't think defaulting to a format is the right move, since when we used to do this, users complained that some of the early log lines weren't in the format they were expecting and parsing the log lines at ingestion time was failing.

Copy link
Member

Choose a reason for hiding this comment

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

I think it might be worth fixing that so we can use the right logger here.

Since we've fixed issues about this in the past I'd say not using the right logger would be a regression here. IMO I think we need to do something to use the right logger, either fixing the buffering issue with the slog.Handler implementation or moving this later in evaluation at least temporarily (I agree it's weird for setting the memory limit to not happen as early as possible)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am going to fork this out into its own PR, making the logger work with slog is bigger than this change.

Copy link
Member

Choose a reason for hiding this comment

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

@mattdurham Is creating a PR to fix the logger still on your radar? Given this was merged first, there's a known regression in main now, and we're running out of time for 1.1 (RC gets cut this Thursday).

If we can't fix this by Thursday we'll have to discuss this feature skipping 1.1 or putting out the release with a known regression.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is, wrapping up testing now, it ended up being slightly more complicated than expected.

internal/featuregate/featuregate.go Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
@mattdurham mattdurham enabled auto-merge (squash) April 25, 2024 19:37
@mattdurham mattdurham merged commit 148f6a3 into main Apr 25, 2024
13 checks passed
@mattdurham mattdurham deleted the auto_set_memlimit branch April 25, 2024 19:40
hainenber pushed a commit to hainenber/alloy that referenced this pull request May 1, 2024
* Add details for setting memory limit automatically.

* fix typo

* Update docs/sources/reference/cli/run.md

Co-authored-by: Mischa Thompson <mischabear@gmail.com>

* Update internal/alloycli/cmd_run.go

Co-authored-by: Mischa Thompson <mischabear@gmail.com>

* add logging

* Mark as public preview.

* Mark as public preview.

* pr feedback

* use default logger

---------

Co-authored-by: Mischa Thompson <mischabear@gmail.com>
@rfratto rfratto mentioned this pull request May 13, 2024
1 task
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 31, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backport-to-agent:no PR should NOT be backported to the agent repo. frozen-due-to-age
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add automatically setting go memory limit if cgroup is available
4 participants