-
Notifications
You must be signed in to change notification settings - Fork 1k
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 #7130: Contention scheduling actions in HashedWheelTimerScheduler #7144
Merged
Aaronontheweb
merged 8 commits into
akkadotnet:dev
from
Arkatufus:Fix-HashedWheelTimerScheduler-contention
Apr 11, 2024
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
261e96f
Add unit test
Arkatufus 7ffb044
Add contention fix
Arkatufus c4211b5
Replace CountdownEvent with TaskCompletionSource in net6.0
Arkatufus 5a36672
Cleanup code, revert switch to if
Arkatufus db0f0e3
Revert if block
Arkatufus 11fa9f8
Fix unit test
Arkatufus da95b2d
Merge branch 'dev' into Fix-HashedWheelTimerScheduler-contention
Aaronontheweb 86dbe1c
Merge branch 'dev' into Fix-HashedWheelTimerScheduler-contention
Aaronontheweb 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
136 changes: 136 additions & 0 deletions
136
src/core/Akka.Tests/Actor/Scheduler/HashedWheelTimerSchedulerContentionSpec.cs
This file contains 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,136 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="SchedulerHeavyUse.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2009-2024 Lightbend Inc. <http://www.lightbend.com> | ||
// Copyright (C) 2013-2024 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading; | ||
using Akka.Actor; | ||
using Akka.Event; | ||
using FluentAssertions; | ||
using FluentAssertions.Extensions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Akka.Tests.Actor.Scheduler; | ||
|
||
public class HashedWheelTimerSchedulerContentionSpec: TestKit.Xunit2.TestKit | ||
{ | ||
private const int TotalActor = 5000; | ||
private const int TotalThreads = 10; | ||
private const int ActorsPerThread = TotalActor / TotalThreads; | ||
|
||
public HashedWheelTimerSchedulerContentionSpec(ITestOutputHelper output) : base("{}", output) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public void SchedulerContentionTest() | ||
{ | ||
var collector = CreateTestProbe(); | ||
|
||
foreach (var i in Enumerable.Range(0, TotalActor)) | ||
{ | ||
Sys.ActorOf(Props.Create(() => new DoStuffActor(TestActor, collector)), i.ToString()); | ||
} | ||
|
||
Within(10.Seconds(), () => | ||
{ | ||
for (var i = 0; i < TotalActor; i++) | ||
{ | ||
ExpectMsg<Done>(); | ||
} | ||
}); | ||
|
||
object? received = null; | ||
do | ||
{ | ||
received = collector.ReceiveOne(TimeSpan.Zero); | ||
if (received is long value) | ||
{ | ||
value.Should().BeLessThan(200, "Scheduler should not experience resource contention"); | ||
} | ||
} while (received is not null); | ||
|
||
} | ||
|
||
[Fact] | ||
public void SchedulerContentionThreadedTest() | ||
{ | ||
var collector = CreateTestProbe(); | ||
var threads = new List<Thread>(); | ||
|
||
foreach (var j in Enumerable.Range(0, TotalThreads)) | ||
{ | ||
threads.Add(new Thread(() => RunThread(j))); | ||
} | ||
|
||
foreach (var thread in threads) | ||
{ | ||
thread.Start(); | ||
} | ||
|
||
foreach (var thread in threads) | ||
{ | ||
thread.Join(); | ||
} | ||
|
||
Within(10.Seconds(), () => | ||
{ | ||
for (var i = 0; i < TotalActor; i++) | ||
{ | ||
ExpectMsg<Done>(); | ||
} | ||
}); | ||
|
||
object? received = null; | ||
do | ||
{ | ||
received = collector.ReceiveOne(TimeSpan.Zero); | ||
if (received is long value) | ||
{ | ||
value.Should().BeLessThan(200, "Scheduler should not experience resource contention"); | ||
} | ||
} while (received is not null); | ||
|
||
return; | ||
|
||
void RunThread(int n) | ||
{ | ||
n *= ActorsPerThread; | ||
for (var i = 0; i < ActorsPerThread; i++) | ||
{ | ||
Sys.ActorOf(Props.Create(() => new DoStuffActor(TestActor, collector)), (n + i).ToString()); | ||
} | ||
} | ||
} | ||
|
||
public class DoStuffActor : ReceiveActor, IWithTimers | ||
{ | ||
public ITimerScheduler Timers { get; set; } | ||
|
||
public DoStuffActor(IActorRef probe, IActorRef collector) | ||
{ | ||
Receive<Done>(d => | ||
{ | ||
Context.Stop(Self); | ||
probe.Tell(d); | ||
}); | ||
|
||
var sw = Stopwatch.StartNew(); | ||
Timers.StartSingleTimer("Test", Done.Instance, TimeSpan.FromSeconds(3)); | ||
sw.Stop(); | ||
|
||
if (sw.ElapsedMilliseconds > 0) | ||
{ | ||
Context.GetLogger().Info($"{sw.ElapsedMilliseconds}"); | ||
collector.Tell(sw.ElapsedMilliseconds); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -137,35 +137,41 @@ private static int NormalizeTicksPerWheel(int ticksPerWheel) | |
private readonly HashSet<SchedulerRegistration> _rescheduleRegistrations = new(); | ||
|
||
#if NET6_0_OR_GREATER | ||
private readonly object _lock = new (); | ||
private PeriodicTimer? _timer; | ||
private readonly CancellationTokenSource _cts = new(); | ||
|
||
private void Start() | ||
{ | ||
if (_workerState == WORKER_STATE_STARTED) | ||
var start = false; | ||
lock (_lock) | ||
{ | ||
} // do nothing | ||
else if (_workerState == WORKER_STATE_INIT) | ||
{ | ||
if (Interlocked.CompareExchange(ref _workerState, WORKER_STATE_STARTED, WORKER_STATE_INIT) == | ||
WORKER_STATE_INIT) | ||
switch (_workerState) | ||
{ | ||
var t = TimeSpan.FromTicks(_tickDuration); | ||
_timer = new PeriodicTimer(t); | ||
|
||
Task.Run(() => RunAsync(_cts.Token)); // start the clock | ||
case WORKER_STATE_STARTED: | ||
// do nothing | ||
break; | ||
|
||
case WORKER_STATE_INIT: | ||
_workerState = WORKER_STATE_STARTED; | ||
start = true; | ||
break; | ||
|
||
case WORKER_STATE_SHUTDOWN: | ||
throw new SchedulerException("cannot enqueue after timer shutdown"); | ||
|
||
default: | ||
throw new InvalidOperationException($"Worker in invalid state: {_workerState}"); | ||
} | ||
} | ||
else if (_workerState == WORKER_STATE_SHUTDOWN) | ||
|
||
if (start) | ||
{ | ||
throw new SchedulerException("cannot enqueue after timer shutdown"); | ||
var timerDuration = TimeSpan.FromTicks(_tickDuration); | ||
_timer ??= new PeriodicTimer(timerDuration); | ||
Task.Run(() => RunAsync(_cts.Token)); // start the clock | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not wait on |
||
} | ||
else | ||
{ | ||
throw new InvalidOperationException($"Worker in invalid state: {_workerState}"); | ||
} | ||
|
||
while (_startTime == 0) | ||
{ | ||
_workerInitialized.Wait(); | ||
} | ||
|
@@ -248,7 +254,7 @@ private void Start() | |
if (_workerState == WORKER_STATE_STARTED) { } // do nothing | ||
else if (_workerState == WORKER_STATE_INIT) | ||
{ | ||
_worker = new Thread(Run) { IsBackground = true }; | ||
_worker ??= new Thread(Run) { IsBackground = true }; | ||
if (Interlocked.CompareExchange(ref _workerState, WORKER_STATE_STARTED, WORKER_STATE_INIT) == | ||
WORKER_STATE_INIT) | ||
{ | ||
|
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.
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.
Remove
lock