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

Generalize memory and cpu tracking #19839

Closed

Conversation

AlessandroPatti
Copy link
Contributor

Remove special handling of cpu and memory, and instead track them as any other resource. This has a few advantages:

  • Significant code clean up, keep only the logic for generic resource around
  • Consistent resources type (double) for all resource
  • Consistent resource tag for all resources: resources:memory:<n> works

Relates to #19679

@AlessandroPatti AlessandroPatti requested a review from a team as a code owner October 17, 2023 08:50
@AlessandroPatti AlessandroPatti requested review from gregestren and removed request for a team October 17, 2023 08:50
@github-actions github-actions bot added awaiting-review PR is awaiting review from an assigned reviewer team-Configurability platforms, toolchains, cquery, select(), config transitions team-Local-Exec Issues and PRs for the Execution (Local) team team-Performance Issues for Performance teams labels Oct 17, 2023
@AlessandroPatti AlessandroPatti force-pushed the apatti/19679/general branch 2 times, most recently from 5c9ac6d to 76f6134 Compare October 17, 2023 09:21
@zhengwei143
Copy link
Contributor

Hiya, I'm handling the review but not too familiar with this area of the code yet, will spend some time looking at this and will get a review out by this week.

@@ -37,19 +37,13 @@
public class ResourceSet implements ResourceSetOrBuilder {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we consolidate "memory" and "cpu" string usage as a static string defined here?

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

ImmutableMap<String, Double> cpuRam =
ImmutableMap.of("cpu", options.localCpuResources, "memory", options.localRamResources);
ImmutableMap<String, Double> resources =
java.util.stream.Stream.concat(cpuRam.entrySet().stream(), options.localExtraResources.stream())
Copy link
Contributor

Choose a reason for hiding this comment

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

Please import java.util.stream.Stream above and use Stream.concat() instead.

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

ImmutableMap<String, Double> cpuRam =
ImmutableMap.of("cpu", options.localCpuResources, "memory", options.localRamResources);
ImmutableMap<String, Double> resources =
java.util.stream.Stream.concat(cpuRam.entrySet().stream(), options.localExtraResources.stream())
.collect(
ImmutableMap.toImmutableMap(
Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v2));
Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC, users will be able to override --local_{cpu,ram}_resources here because there isn't a check to ensure that users don't specify --local_extra_resources={cpu,memory}=?. Because localExtraResources.stream() is concatenated after cpuRam.stream() and are collected into a single map here, the former overrides the latter.

Since this map is now the source of truth (i.e. cpu and memory don't have separate fields in the ResourceSet), Bazel won't ignore any "cpu" or "memory" keys in the map as it previously did.

Lets keep the previous behavior of still having the --local_{cpu,ram}_resources flag (at least in this change). So you'd want to either flip the order of the concat, or filter out "cpu" and "memory" strings from localExtraResources.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated. I was a bit on the fence on what was best when I first made this change, because ideally I'd be nice to have one attribute to deal with all resources (see proposal in #19679). I do agree however that --local_extra_resources still carries that meaning of "resources other than some others". Might be worth adopting an incompatible flag to introduce --local_resources, WDYT? Happy to do it as a follow up as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, let's follow up with a separate PR (feel free to send it to me directly). --local_resources wouldn't need to be an incompatible flag; we can just have it override all of --local_{cpu,ram,extra}_resources when specified, then we can mark the old flags for deprecation and remove them in the following major release.

+ cpuUsage
+ "\n"
+ Joiner.on("\n").withKeyValueSeparator(": ").join(extraResourceUsage.entrySet())
+ Joiner.on("\n").withKeyValueSeparator(": ").join(resources.entrySet())
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you preserve the behavior of this? I think it'd be better to show Memory and CPU at the top since they are the more typical resources, then the rest of the resources below.

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

// requested resource sets use pessimistic estimations. Note that this
// ratio is used only during comparison - for tracking we will actually
// mark whole requested amount as used.
double requested = resource.getValue() * MIN_NECESSARY_RATIO.getOrDefault(key, 1.0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you define 1.0 in a static constant above together with MIN_NECESSARY_RATIO (e.g. DEFAULT_RATIO)? That'll make it clearer what happens if it's not specified in the MIN_NECESSARY_RATIO map.

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

Copy link
Contributor

@zhengwei143 zhengwei143 left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution! I'll get this imported internally ASAP.

@fmeum
Copy link
Collaborator

fmeum commented Nov 21, 2023

@bazel-io flag

@zhengwei143
Copy link
Contributor

Update: It's failing some internal tests that I'll have to spend some time fixing

@github-actions github-actions bot removed the awaiting-review PR is awaiting review from an assigned reviewer label Nov 28, 2023
@brentleyjones
Copy link
Contributor

@bazel-io flag

@bazel-io bazel-io added the potential release blocker Flagged by community members using "@bazel-io flag". Should be added to a release blocker milestone label Nov 28, 2023
@keertk
Copy link
Member

keertk commented Nov 28, 2023

@brentleyjones @fmeum is this flagged for 7.0? Or 7.1?

@fmeum
Copy link
Collaborator

fmeum commented Nov 28, 2023

@keertk I would say 7.1.0 at this point.

@keertk
Copy link
Member

keertk commented Nov 28, 2023

@bazel-io fork 7.1.0

@bazel-io bazel-io removed the potential release blocker Flagged by community members using "@bazel-io flag". Should be added to a release blocker milestone label Nov 28, 2023
@AlessandroPatti
Copy link
Contributor Author

@keertk @fmeum Looks like 7.0 was delayd again #18548 (comment), maybe there's still a chance for this to go in?

@keertk
Copy link
Member

keertk commented Nov 30, 2023

@AlessandroPatti we're actually planning on getting the final RC out in a little bit, but we can try to include this.

iancha1992 pushed a commit to iancha1992/bazel that referenced this pull request Nov 30, 2023
Remove special handling of cpu and memory, and instead track them as any other resource. This has a few advantages:
  - Significant code clean up, keep only the logic for generic resource around
  - Consistent resources type (double) for all resource

Relates to bazelbuild#19679

Closes bazelbuild#19839.

PiperOrigin-RevId: 585926931
Change-Id: Id0d6a14e9c151c3895f55d1808b6bd66eecf98b3
iancha1992 added a commit that referenced this pull request Nov 30, 2023
Remove special handling of cpu and memory, and instead track them as any
other resource. This has a few advantages:
- Significant code clean up, keep only the logic for generic resource
around
  - Consistent resources type (double) for all resource

Relates to #19679

Closes #19839.

Commit
ebe4d0d

PiperOrigin-RevId: 585926931
Change-Id: Id0d6a14e9c151c3895f55d1808b6bd66eecf98b3

Co-authored-by: Alessandro Patti <[email protected]>
@iancha1992
Copy link
Member

The changes in this PR have been included in Bazel 7.0.0 RC5. Please test out the release candidate and report any issues as soon as possible. If you're using Bazelisk, you can point to the latest RC by setting USE_BAZEL_VERSION=last_rc.
Thanks!

copybara-service bot pushed a commit that referenced this pull request Jan 9, 2024
Follow up for #19839 (comment), introduce new flag that will replace all the others.

Closes #20398.

PiperOrigin-RevId: 596905096
Change-Id: Ic2c20dae40efa797953cde6934085f899acb9b52
bazel-io pushed a commit to bazel-io/bazel that referenced this pull request Feb 13, 2024
Follow up for bazelbuild#19839 (comment), introduce new flag that will replace all the others.

Closes bazelbuild#20398.

PiperOrigin-RevId: 596905096
Change-Id: Ic2c20dae40efa797953cde6934085f899acb9b52
github-merge-queue bot pushed a commit that referenced this pull request Feb 14, 2024
Follow up for
#19839 (comment),
introduce new flag that will replace all the others.

Closes #20398.

Commit
ea75928

PiperOrigin-RevId: 596905096
Change-Id: Ic2c20dae40efa797953cde6934085f899acb9b52

Co-authored-by: Alessandro Patti <[email protected]>
AlessandroPatti added a commit to AlessandroPatti/bazel that referenced this pull request Feb 19, 2024
Remove special handling of cpu and memory, and instead track them as any other resource. This has a few advantages:
  - Significant code clean up, keep only the logic for generic resource around
  - Consistent resources type (double) for all resource

Relates to bazelbuild#19679

Closes bazelbuild#19839.

PiperOrigin-RevId: 585926931
Change-Id: Id0d6a14e9c151c3895f55d1808b6bd66eecf98b3
AlessandroPatti added a commit to AlessandroPatti/bazel that referenced this pull request Feb 19, 2024
Follow up for bazelbuild#19839 (comment), introduce new flag that will replace all the others.

Closes bazelbuild#20398.

PiperOrigin-RevId: 596905096
Change-Id: Ic2c20dae40efa797953cde6934085f899acb9b52
apattidb pushed a commit to databricks/bazel that referenced this pull request Feb 20, 2024
Remove special handling of cpu and memory, and instead track them as any other resource. This has a few advantages:
  - Significant code clean up, keep only the logic for generic resource around
  - Consistent resources type (double) for all resource

Relates to bazelbuild#19679

Closes bazelbuild#19839.

PiperOrigin-RevId: 585926931
Change-Id: Id0d6a14e9c151c3895f55d1808b6bd66eecf98b3
apattidb pushed a commit to databricks/bazel that referenced this pull request Feb 20, 2024
Follow up for bazelbuild#19839 (comment), introduce new flag that will replace all the others.

Closes bazelbuild#20398.

PiperOrigin-RevId: 596905096
Change-Id: Ic2c20dae40efa797953cde6934085f899acb9b52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-Configurability platforms, toolchains, cquery, select(), config transitions team-Local-Exec Issues and PRs for the Execution (Local) team team-Performance Issues for Performance teams
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants