fix Flush deadlock by implementing SpinWait pattern using Interlocked.CompareExchange - #2595
Closed
Timothy Mothra (TimothyMothra) wants to merge 11 commits into
Closed
Timothy Mothra (TimothyMothra) wants to merge 11 commits into
Timothy Mothra (TimothyMothra) wants to merge 11 commits into
Conversation
Timothy Mothra (TimothyMothra)
marked this pull request as ready for review
May 20, 2022 20:34
Timothy Mothra (TimothyMothra)
requested review from
Rajkumar Rangaraj (rajkumar-rangaraj) and
Reiley Yang (reyang)
June 8, 2022 17:37
Author
|
GitHub won't let Kennedy comment. He shared his comments offline:
|
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix Issue #1186.
Metrics Flushing has a deadlock which occurs here:
ApplicationInsights-dotnet/BASE/src/Microsoft.ApplicationInsights/Metrics/Extensibility/MetricSeriesAggregatorBase.cs
Lines 398 to 411 in 5aed71e
This PR removes the lock and implements a SpinWait pattern using Interlocked.CompareExchange().
To make this work, Interlocked.CompareExchange needs access to the index variable in the class
MetricValuesBuffer. Because of this, I've had to do some refactoring.Changes
lockfrom theMetricSeriesAggregatorBaseclass.TryGetFlushIndexes()into theMetricValuesBufferclass.This new method uses Interlocked.CompareExchange to try to advance the index variable.
Alternatives considered
I had a PoC that replaced the lock with Monitor.TryEnter. #2594
This more closely matches the original author's intent using a lock, and adds a timeout to mitigate the deadlock.
This was abandoned in favor of removing the lock entirely.
Checklist
For significant contributions please make sure you have completed the following items:
The PR will trigger build, unit tests, and functional tests automatically. Please follow these instructions to build and test locally.
Notes for authors:
Notes for reviewers:
/AzurePipelines runwill queue all builds/AzurePipelines run <pipeline-name>will queue a specific build