Skip to content

Fix: PostingCache promise should fetch data only once #6314

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

Merged

Conversation

alanprot
Copy link
Member

@alanprot alanprot commented Nov 6, 2024

What this PR does:
Follow up of #6296

Under concurrency, we are fetching the postings multiples times for the same key.

The reason for that is we load the previous promise from the cache but the promise does not have the ts set yet and so, we assume is expired:

See:

loaded, ok := c.cachedValues.LoadOrStore(k, r)
if !ok {
r.v, r.sizeBytes, r.err = fetch()
r.sizeBytes += int64(len(k))
r.ts = c.timeNow()
c.created(k, r.sizeBytes)
c.expire()
}
// If is cached but is expired, lets try to replace the cache value
if ok && loaded.(*cacheEntryPromise[V]).isExpired(c.cfg.Ttl, c.timeNow()) {
if c.cachedValues.CompareAndSwap(k, loaded, r) {

This PR is simplifying the implementation and waiting the loaded promise to finish before checking if is expired.

First commit of the PR has only the test showing the problem.

Which issue(s) this PR fixes:
Fixes #

Checklist

  • Tests updated
  • [NA] Documentation added
  • [NA] CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

@alanprot alanprot force-pushed the fix-postings-cache-concurrency branch from 0e8f9a1 to 1f3441c Compare November 6, 2024 00:58
@alanprot alanprot force-pushed the fix-postings-cache-concurrency branch from 1f3441c to 49429c9 Compare November 6, 2024 01:00
@alanprot alanprot marked this pull request as ready for review November 6, 2024 01:04
Signed-off-by: alanprot <[email protected]>
@alanprot alanprot force-pushed the fix-postings-cache-concurrency branch from a5e8997 to b159f09 Compare November 6, 2024 01:07
Copy link
Contributor

@yeya24 yeya24 left a comment

Choose a reason for hiding this comment

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

Thanks. Make sense to me

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Nov 6, 2024
@alanprot alanprot merged commit f658039 into cortexproject:master Nov 6, 2024
16 checks passed
@alanprot alanprot deleted the fix-postings-cache-concurrency branch November 6, 2024 17:17
CharlieTLe pushed a commit to CharlieTLe/cortex that referenced this pull request Dec 3, 2024
…6314)

* Fix: PostingCache promise should fetch data only once

Signed-off-by: alanprot <[email protected]>

* fix

Signed-off-by: alanprot <[email protected]>

* fix test using default prometheus registerer

Signed-off-by: alanprot <[email protected]>

---------

Signed-off-by: alanprot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm This PR has been approved by a maintainer size/M
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants