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

Use coarse grained memory reporting to reduce congestion #22841

Merged

Conversation

ShubhamChaurasia
Copy link
Member

Description

This reduces congestion in the memory tracking system by coarsening the granularity of memory tracking. In this paradigm, the most small incremental increases at the byte granularity will not actually result in a different coarse granularity value, so no reporting into memory tracking is necessary.
This has already been addressed previously in #22668, however it under reports memory to the tracking system which can be a problem in high concurrency environment.

In this approach, we prefer to over-report than under report within the small rounding margin. Also, this eliminates the need of an explicit sync() API.

Thanks to @pettyjamesm for the idea and the guidance for this.

Additional context and related issues

Release notes

( ) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
( ) Release notes are required, with the following suggested text:

# Section
* Fix some things. ({issue}`issuenumber`)

Copy link
Member

@pettyjamesm pettyjamesm left a comment

Choose a reason for hiding this comment

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

LGTM, except for some minor comments. cc: @findepi - this avoids under-reporting the amount of memory used which can be dangerous at high concurrency.


// gets set to the next coarse unit
// k*syncThreshold + x in [1, syncThreshold] leads to setting the context to (k+1) * syncThreshold
Random random = new Random();
Copy link
Member

Choose a reason for hiding this comment

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

Using Random and asserting that k * syncThreshold + x == (k + 1) * syncThreshold is probably overkill here since the implementation itself is fairly straightforward math. Let's go with some hard coded example values and then maybe add a loop to assert that for all values in the range of [1, syncThreshold] the result is syncThreshold

{
this(delegate, DEFAULT_SYNC_THRESHOLD);
}

public ThresholdLocalMemoryContext(LocalMemoryContext delegate, long syncThreshold)
public CoarseGrainLocalMemoryContext(LocalMemoryContext delegate, long syncThreshold)
Copy link
Member

Choose a reason for hiding this comment

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

Nit: maybe rename syncThreshold to granularity. If renamed, also update the instance field name and DEFAULT_SYNC_THRESHOLD constant names too.

* In this paradigm, the most small incremental increases at the byte granularity will not actually result
* in a different coarse granularity value, so no reporting into memory tracking is necessary
*/
public class CoarseGrainLocalMemoryContext
Copy link
Member

Choose a reason for hiding this comment

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

All LocalMemoryContext implementations are thread safe. I think this one should be too cc @losipiuk

@ShubhamChaurasia
Copy link
Member Author

thanks for the review @pettyjamesm @sopel39 -- addressed the comments.

public CoarseGrainLocalMemoryContext(LocalMemoryContext delegate, long granularity)
{
this.delegate = requireNonNull(delegate, "delegate is null");
checkArgument(granularity > 0, "syncThreshold must be greater than 0");
Copy link
Member

Choose a reason for hiding this comment

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

"granularity must be greater than 0"

This reduces congestion in the memory tracking system by coarsening the granularity of memory tracking.
In this paradigm, the most small incremental increases at the byte granularity will not actually result in a different coarse granularity value, so no reporting into memory tracking is necessary.

Co-authored-by: James Petty <pettja@amazon.com>
Copy link
Member

@pettyjamesm pettyjamesm left a comment

Choose a reason for hiding this comment

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

LGTM, @losipiuk PTAL

Copy link
Member

@losipiuk losipiuk left a comment

Choose a reason for hiding this comment

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

Thanks. Looks good :)

@pettyjamesm pettyjamesm merged commit 2119edd into trinodb:master Jul 30, 2024
95 checks passed
@github-actions github-actions bot added this to the 454 milestone Jul 30, 2024
private final LocalMemoryContext delegate;
private final long granularity;
private final long mask;
private long currentBytes;
Copy link
Member

Choose a reason for hiding this comment

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

    @GuardedBy("this")

is missing

Copy link
Member

Choose a reason for hiding this comment

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

Added in #22881

@@ -1,93 +0,0 @@
/*
Copy link
Member

Choose a reason for hiding this comment

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

nit: this should be two commits:

  1. rename
  2. changes on top
    this way history is preserved

import static java.util.Objects.requireNonNull;

/**
* This class prevents contention in the memory tracking system by coarsening the granularity of memory tracking.
Copy link

Choose a reason for hiding this comment

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

nit: Possible typo (description says "congestion," here we say "contention").

assertCoarseGrainContextValues(coarseGrainContext, delegate, i, granularity);
}

// threshold not a power of 2
Copy link

Choose a reason for hiding this comment

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

nit: "granularity"

@colebow
Copy link
Member

colebow commented Aug 7, 2024

Does this need release notes? cc @pettyjamesm

@pettyjamesm
Copy link
Member

Sorry about that @colebow, I should have verified the release notes section. No notes should be necessary on this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

6 participants