-
Notifications
You must be signed in to change notification settings - Fork 0
feat(metrics): track thread pool backlog #358
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
57 changes: 57 additions & 0 deletions
57
src/EventStore.Core.XUnit.Tests/Metrics/QueueStatsCollectorTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| using EventStore.Core.Bus; | ||
| using EventStore.Core.Metrics; | ||
| using Xunit; | ||
|
|
||
| namespace EventStore.Core.XUnit.Tests.Metrics; | ||
|
|
||
| public class QueueStatsCollectorTests | ||
| { | ||
| [Fact] | ||
| public void statistics_include_the_current_queue_length_in_the_peak() | ||
| { | ||
| var collector = new QueueStatsCollector("queue"); | ||
| collector.Start(); | ||
| try | ||
| { | ||
| var stats = collector.GetStatistics(currentQueueLength: 5); | ||
|
|
||
| Assert.Equal(5, stats.Length); | ||
| Assert.Equal(5, stats.LengthCurrentTryPeak); | ||
| Assert.Equal(5, stats.LengthLifetimePeak); | ||
| } | ||
| finally | ||
| { | ||
| collector.Stop(); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void reported_queue_length_updates_the_peak() | ||
| { | ||
| var collector = new QueueStatsCollector("queue"); | ||
| collector.Start(); | ||
| try | ||
| { | ||
| collector.ReportQueueLength(7); | ||
| var stats = collector.GetStatistics(currentQueueLength: 3); | ||
|
|
||
| Assert.Equal(3, stats.Length); | ||
| Assert.Equal(7, stats.LengthCurrentTryPeak); | ||
| Assert.Equal(7, stats.LengthLifetimePeak); | ||
| } | ||
| finally | ||
| { | ||
| collector.Stop(); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void thread_pool_backlog_monitor_dispose_is_idempotent() | ||
| { | ||
| var monitor = new ThreadPoolBacklogMonitor(new QueueStatsManager(), new QueueTrackers()); | ||
|
|
||
| monitor.Start(); | ||
| monitor.Dispose(); | ||
| monitor.Dispose(); | ||
| } | ||
| } |
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
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
125 changes: 125 additions & 0 deletions
125
src/EventStore.Core/Metrics/ThreadPoolBacklogMonitor.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using EventStore.Core.Bus; | ||
| using EventStore.Core.Services.Monitoring.Stats; | ||
| using EventStore.Core.Time; | ||
|
|
||
| namespace EventStore.Core.Metrics; | ||
|
|
||
| public sealed class ThreadPoolBacklogMonitor : IMonitoredQueue, IThreadPoolWorkItem, IDisposable | ||
| { | ||
| private static readonly TimeSpan SampleInterval = TimeSpan.FromSeconds(1); | ||
| private const string QueueName = "ThreadPoolBacklog"; | ||
|
|
||
| private readonly QueueStatsCollector _queueStats; | ||
| private readonly QueueTracker _tracker; | ||
| private readonly Timer _timer; | ||
| private readonly object _timerGate = new(); | ||
| private Instant _enqueuedAt; | ||
| private bool _timerDisposed; | ||
| private int _stopped; | ||
|
|
||
| public ThreadPoolBacklogMonitor(QueueStatsManager queueStatsManager, QueueTrackers trackers) | ||
| { | ||
| _queueStats = queueStatsManager.CreateQueueStatsCollector(QueueName); | ||
| _tracker = trackers.GetTrackerForQueue(QueueName); | ||
| _timer = new Timer(_ => QueueWorkItem()); | ||
| } | ||
|
|
||
| public string Name => _queueStats.Name; | ||
|
|
||
| public void Start() | ||
| { | ||
| _queueStats.Start(); | ||
| QueueMonitor.Default.Register(this); | ||
| QueueWorkItem(); | ||
| } | ||
|
|
||
| public void Stop() | ||
| { | ||
| if (Interlocked.Exchange(ref _stopped, 1) != 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| lock (_timerGate) | ||
| { | ||
| if (!_timerDisposed) | ||
| { | ||
| _timer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); | ||
| } | ||
| } | ||
|
|
||
| QueueMonitor.Default.Unregister(this); | ||
| _queueStats.Stop(); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| Stop(); | ||
| lock (_timerGate) | ||
| { | ||
| if (_timerDisposed) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _timerDisposed = true; | ||
| _timer.Dispose(); | ||
| } | ||
| } | ||
|
|
||
| public void Execute() | ||
| { | ||
| if (Volatile.Read(ref _stopped) != 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _queueStats.EnterBusy(); | ||
| try | ||
| { | ||
| var length = GetPendingWorkItemCount(); | ||
| _queueStats.ProcessingStarted<ThreadPoolBacklogSample>(length); | ||
| _tracker.RecordQueueLength(length); | ||
| _tracker.RecordMessageDequeued(_enqueuedAt); | ||
| _queueStats.ProcessingEnded(1); | ||
| } | ||
| finally | ||
| { | ||
| _queueStats.EnterIdle(); | ||
| } | ||
|
|
||
| lock (_timerGate) | ||
| { | ||
| if (Volatile.Read(ref _stopped) == 0 && !_timerDisposed) | ||
| { | ||
| _timer.Change(SampleInterval, Timeout.InfiniteTimeSpan); | ||
| } | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| public QueueStats GetStatistics() => | ||
| _queueStats.GetStatistics(GetPendingWorkItemCount()); | ||
|
|
||
| private void QueueWorkItem() | ||
| { | ||
| if (Volatile.Read(ref _stopped) != 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _enqueuedAt = _tracker.Now; | ||
| ThreadPool.UnsafeQueueUserWorkItem(this, preferLocal: false); | ||
| } | ||
|
|
||
| private static int GetPendingWorkItemCount() | ||
| { | ||
| var count = ThreadPool.PendingWorkItemCount; | ||
| return count > int.MaxValue ? int.MaxValue : (int)count; | ||
| } | ||
|
|
||
| private sealed class ThreadPoolBacklogSample | ||
| { | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.