-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Fix MemPostings.Add and MemPostings.Get data race
#15141
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
Fix MemPostings.Add and MemPostings.Get data race
#15141
Conversation
Signed-off-by: Oleg Zaytsev <[email protected]>
We can't modify the postings list that are held in MemPostings as they might already be in use by some readers. Signed-off-by: Oleg Zaytsev <[email protected]>
I used `require.Equal` instead of `require.Len` on purpose, as when the slice is getting bigger, the error message that `require.Len` prints is somtimes too verbose. However that makes linter unhappy, linter thinks that `require.Len` is always better. Signed-off-by: Oleg Zaytsev <[email protected]>
|
Running some benchmarks: |
bboreham
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change seems fine; couple of thoughts below.
tsdb/index/postings.go
Outdated
| old := list | ||
| list = make([]storage.SeriesRef, len(old), cap(old)) | ||
| copy(list, old) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use slices.Clone ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked, and slices.Clone doesn't create a slice of same capacity, which is what I wanted here.
| for i := len(list) - 1; i >= 1; i-- { | ||
| if list[i] >= list[i-1] { | ||
| break | ||
| } | ||
| list[i], list[i-1] = list[i-1], list[i] | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idea: this loop could be modified to find the insertion point, then copy the head, insert new value, copy the tail.
(and not copy on line 422).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that's better given that we expect this to happen few times and very few last elements would need to be swapped. Can we experiment with that in a follow up?
|
|
||
| // We don't read the value of the postings here, | ||
| // this is tested in TestMemPostings_Unordered_Add_Get where it's easier to achieve the determinism. | ||
| // This test just checks that there's no data race. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reminded me of
Line 8644 in 53726de
| copy(bb, b) // This copying of chunk bytes detects any race. |
aknuds1
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, should there be a changelog entry though?
|
It's not typical to add changelog entries in Prometheus. |
jesusvazquez
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Signed-off-by: Oleg Zaytsev <[email protected]>
512cb16
|
I pushed a commit changing the logic to avoid copying again when we just copied for appending. |
If there are no common labels on the series, we don't excercise the ordering part of MemSeries, as we're just creating slices of one element for each label value. Signed-off-by: Oleg Zaytsev <[email protected]>
|
Thanks! |
…heus#15141)" This reverts commit 50ef0dc. Memory allocation goes so high in Prombench that the system is unusable.
…heus#15141)" This reverts commit 50ef0dc. Memory allocation goes so high in Prombench that the system is unusable.
…heus#15141)" This reverts commit 50ef0dc. Memory allocation goes so high in Prombench that the system is unusable. Signed-off-by: Bryan Boreham <[email protected]>
|
Apparently the case where we copy is quite common when running Prombench, and therefore I think this will impact real users. I have proposed to revert at #15316. |
|
Maybe, instead of adding all the burden to Is there an issue detailing how this problem was found? |
@bboreham A public facing issue wasn't added, I can make one. |
…heus#15141)" This reverts commit 50ef0dc. Memory allocation goes so high in Prombench that the system is unusable. Signed-off-by: Bryan Boreham <[email protected]>
Revert "Fix `MemPostings.Add` and `MemPostings.Get` data race (#15141)"
* Tests for Mempostings.{Add,Get} data race
* Fix MemPostings.{Add,Get} data race
We can't modify the postings list that are held in MemPostings as they
might already be in use by some readers.
* Modify BenchmarkHeadStripeSeriesCreate to have common labels
If there are no common labels on the series, we don't excercise the
ordering part of MemSeries, as we're just creating slices of one element
for each label value.
---------
Signed-off-by: Oleg Zaytsev <[email protected]>
…heus#15141)" This reverts commit b6285be. Memory allocation went through the roof.
Revert "Fix `MemPostings.Add` and `MemPostings.Get` data race (prometheus#15141)"
Hi @bboreham, if we remove the sorting part from |
|
@kushalShukla-web I was thinking that we could involve isolation here, and committing to the sorted slice once all related transactions are complete, while keeping an unsorted tail for the series created in the ongoing transactions. This would require having isolation enabled everywhere though. |
|
@colega In that case, we need to change |
When references are not added in order, we are sorting in place the tail of the postings list held by
MemPostings, however the array backing that slice might already being used by some readers.Corresponds to #15317.