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

Add max spin count limit to circular buffer #1088

Merged
merged 2 commits into from
Aug 17, 2020
Merged

Add max spin count limit to circular buffer #1088

merged 2 commits into from
Aug 17, 2020

Conversation

reyang
Copy link
Member

@reyang reyang commented Aug 16, 2020

@ThomsonTan brought up a good point that we might want to avoid spinning indefinitely.

This could happen on a highly concurrent environment since spin lock does not guarantee ordering, so mathematically it is possible that one unfortunate thread ended up to be always spinning while in its own quorum, which is scary for customers who use telemetry SDK.

@reyang reyang requested a review from a team August 16, 2020 20:57
@reyang reyang mentioned this pull request Aug 16, 2020
2 tasks
@@ -113,7 +113,7 @@ internal long ProcessedCount
/// <inheritdoc/>
public override void OnEnd(Activity activity)
{
if (this.queue.TryAdd(activity))
if (this.queue.TryAdd(activity, maxSpinCount: 50000))
Copy link
Member Author

Choose a reason for hiding this comment

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

Do we want to make this configurable?


var spinCountDown = maxSpinCount;

while (true)
Copy link
Member Author

Choose a reason for hiding this comment

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

Duplicate the code from Add instead of reusing the code - for better perf.

/// Attempts to add the specified item to the buffer.
/// </summary>
/// <param name="value">The value to add.</param>
/// <param name="maxSpinCount">The maximum allowed spin count, when set to a negative number of zero, will spin indefinitely.</param>
Copy link
Member Author

Choose a reason for hiding this comment

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

We might want to avoid spin entirely if we are running on a single core machine. I guess it is a low priority at this moment.

@codecov
Copy link

codecov bot commented Aug 16, 2020

Codecov Report

Merging #1088 into master will decrease coverage by 0.15%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1088      +/-   ##
==========================================
- Coverage   76.01%   75.85%   -0.16%     
==========================================
  Files         225      225              
  Lines        6208     6225      +17     
==========================================
+ Hits         4719     4722       +3     
- Misses       1489     1503      +14     
Impacted Files Coverage Δ
src/OpenTelemetry/Internal/CircularBuffer.cs 0.00% <0.00%> (ø)
...penTelemetry/Trace/BatchExportActivityProcessor.cs 0.00% <0.00%> (ø)
...nTelemetry/Internal/OpenTelemetrySdkEventSource.cs 82.35% <0.00%> (+4.41%) ⬆️

@CodeBlanch CodeBlanch merged commit 0d7ef90 into master Aug 17, 2020
@CodeBlanch CodeBlanch deleted the reyang/spin branch August 17, 2020 01:27
}
}
}

public IEnumerable<T> Consume(int count)
Copy link
Member

Choose a reason for hiding this comment

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

@reyang FYI just noticed you don't have any XML comments for Consume. Also might want to rename count -> maxCount or something, because you can actually get less?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, let me send a small PR to fix it.

Copy link
Member Author

Choose a reason for hiding this comment

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

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