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

Do not call caching functions with mutex in bucketIndexReader #3400

Merged
merged 1 commit into from
Nov 10, 2022

Conversation

pracucci
Copy link
Collaborator

@pracucci pracucci commented Nov 7, 2022

What this PR does

In bucketIndexReader there are a couple of places where we call the caching functions while holding the bucketIndexReader mutex. I think this is not required, as also pointed by Dimitar in this comment #3396 (comment).

Why I think it"s not necessary:

  1. We have two implementations of the index cache (in-memory and memcached), and both are concurrency safe. A counterproof of the safety is the fact that caches are shared across all tenants, while the bucketIndexReader.mtx is per tenant and block.
  2. I checked the arguments passed to the caching functions from bucketIndexReader and I can"t see anything that requires holding bucketIndexReader.mtx

This PR is on top of #3398.

Which issue(s) this PR fixes or relates to

N/A

Checklist

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

@pracucci pracucci requested a review from a team as a code owner November 7, 2022 17:07
Copy link
Contributor

@dimitarvdimitrov dimitarvdimitrov left a comment

Choose a reason for hiding this comment

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

thank you for addressing my feedback

pkg/storegateway/bucket_index_reader.go Outdated Show resolved Hide resolved
@@ -413,9 +413,9 @@ func (r *bucketIndexReader) fetchPostings(ctx context.Context, keys []labels.Lab
// Return postings and fill LRU cache.
// Truncate first 4 bytes which are length of posting.
output[p.keyID] = newBigEndianPostings(pBytes[4:])
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need a lock around this access too? It is a slice and I assume we only assign each element once

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Follow up PR :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The answer is that we need to protect output from concurrency, so the lock is required. But we don"t need a per bucketIndexReader mutex: we can just have a local one. In the next PR I will get rid of bucketIndexReader.mtx completely.

Copy link
Contributor

@dimitarvdimitrov dimitarvdimitrov Nov 8, 2022

Choose a reason for hiding this comment

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

but isn"t output[p.keyID] = x executed only once for every possible p.keyID? My argument is that there is no concurrent access on the slice elements. Each goroutine accesses its own subset of output. Isn"t that true?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You"re right. Unless bugs in the postings resizing, there"s no concurrent access for a given key and the slices is preallocated. I"m convinced we can remove it, but I still would prefer to do it in a separate PR (the one removing mtx at all) given this PR focus is "Do not call caching functions with mutex in bucketIndexReader".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See: #3500

Base automatically changed from move-bucket-index-reader-to-dedicated-file to main November 10, 2022 04:52
@pracucci pracucci force-pushed the do-not-call-cache-functions-with-mutex branch from 8659f2a to e6278c6 Compare November 10, 2022 05:25
@pracucci pracucci force-pushed the do-not-call-cache-functions-with-mutex branch from e6278c6 to bca260a Compare November 10, 2022 06:52
Copy link
Contributor

@colega colega left a comment

Choose a reason for hiding this comment

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

LGTM

Your counterargument is enough argument for the change for me.

@pracucci pracucci merged commit 15f7f4a into main Nov 10, 2022
@pracucci pracucci deleted the do-not-call-cache-functions-with-mutex branch November 10, 2022 12:19
masonmei pushed a commit to udmire/mimir that referenced this pull request Dec 16, 2022
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