Skip to content

bugfix: data race in head.Appender.AppendHistogram and Commit#15142

Merged
krajorama merged 2 commits intoprometheus:mainfrom
krajorama:fix-appendhistogram-race
Oct 14, 2024
Merged

bugfix: data race in head.Appender.AppendHistogram and Commit#15142
krajorama merged 2 commits intoprometheus:mainfrom
krajorama:fix-appendhistogram-race

Conversation

@krajorama
Copy link
Copy Markdown
Member

@krajorama krajorama commented Oct 10, 2024

Fixes: #15139

Two Appenders race when creating a series with a native histogram as the memSeries will be common and the lastHistogram field is written without lock.

Fix by:
Move writing memSeries lastHistogramValue and lastFloatHistogramValue
after series creation under lock.

The resulting code isn't totally correct in the sense that we're setting
these values before Commit() , so they might be overwritten/rolled back
later.

Also Append() of stale sample checks the values without lock, so there's
still a potential race.

The correct solution would be to set these only in Commit() which we
actually do, but then Commit() would also need to process samples in
order and not floats first, then histograms, then float histograms - which
leads to not knowing what stale marker to write for histograms.

Might be easier to do after : #15112

@krajorama krajorama force-pushed the fix-appendhistogram-race branch from 38df6b6 to 6c66c34 Compare October 10, 2024 11:26
Two Appenders race when creating a series with a native histogram
as the memSeries will be common and the lastHistogram field is written
without lock.

Signed-off-by: György Krajcsovits <[email protected]>
@krajorama krajorama force-pushed the fix-appendhistogram-race branch 2 times, most recently from c3437c8 to d2fa80c Compare October 10, 2024 14:46
@krajorama krajorama marked this pull request as ready for review October 10, 2024 14:57
Move writing memSeries lastHistogramValue and lastFloatHistogramValue
after series creation under lock.

The resulting code isn't totally correct in the sense that we're setting
these values before Commit() , so they might be overwritten/rolled back
later.

Also Append of stale sample checks the values without lock, so there's
still a potential race.

The correct solution would be to set these only in Commit() which we
actually do, but then Commit() would also need to process samples in
order and not floats first, then histograms, then float histograms - which
leads to not knowing what stale marker to write for histograms.

Signed-off-by: György Krajcsovits <[email protected]>
@krajorama krajorama force-pushed the fix-appendhistogram-race branch from d2fa80c to bb70370 Compare October 10, 2024 14:59
Copy link
Copy Markdown
Member

@beorn7 beorn7 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 making things less bad… Looking forward to the Commit reorganization. (I have only vague memory why we did the floats first thing. It was probably much easier that way, and we didn't think about staleness back then.)

@Benz1993com
Copy link
Copy Markdown

Ok

Copy link
Copy Markdown
Member

@jesusvazquez jesusvazquez left a comment

Choose a reason for hiding this comment

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

LGTM krajo, looking forward to that Commit reorg refactor. Will help review Nicolas's PR as well.

@jesusvazquez
Copy link
Copy Markdown
Member

Leaving it open for you to merge 👍

@krajorama
Copy link
Copy Markdown
Member Author

Created issue #15177 to track the refactor.

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.

race in tsdb.headAppender.AppendHistogram

6 participants