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

Fix DefaultResizer for suspended mailboxes (ReceiveAsync) #5333

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/core/Akka.Tests/Routing/ResizerSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Configuration;
using Akka.Routing;
Expand Down Expand Up @@ -65,6 +66,24 @@ public PressureActor()
}
}

private class PressureAsyncActor : ReceiveActor
{
public PressureAsyncActor()
{
ReceiveAsync<TimeSpan>(async d =>
{
await Task.Delay(d);
Sender.Tell("done");
});

ReceiveAsync<string>(s => s == "echo", s =>
{
Sender.Tell("reply");
return Task.CompletedTask;
});
}
}

private class BackoffActor : ReceiveActor
{
private readonly Func<TimeSpan, TimeSpan> _dilated;
Expand Down Expand Up @@ -266,6 +285,55 @@ public void DefaultResizer_must_grow_as_needed_under_pressure()
RouteeSize(router).Should().Be(resizer.UpperBound);
}

[Fact]
public async Task DefaultResizer_with_ReceiveAsync_must_grow_as_needed_under_pressure()
Copy link
Member

Choose a reason for hiding this comment

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

How does this test perform? Resizer specs are notoriously racy, but if we force the Resizer to spin up more actors while the current ones are await-ing over a sufficiently long duration that should be reasonably stable.

{
var resizer = new DefaultResizer(
lower: 3,
upper: 5,
rampupRate: 0.1,
backoffRate: 0.0,
pressureThreshold: 1,
messagesPerResize: 1,
backoffThreshold: 0.0);

var router = Sys.ActorOf(new RoundRobinPool(0, resizer).Props(Props.Create<PressureAsyncActor>()));

// first message should create the minimum number of routees
router.Tell("echo");
ExpectMsg("reply");

RouteeSize(router).Should().Be(resizer.LowerBound);

Func<int, TimeSpan, Task> loop = async (loops, d) =>
{
for (var i = 0; i < loops; i++)
{
router.Tell(d);

//sending too quickly will result in skipped resize due to many ResizeInProgress conflicts
await Task.Delay(Dilated(20.Milliseconds()));
}

var max = d.TotalMilliseconds * loops / resizer.LowerBound + Dilated(2.Seconds()).TotalMilliseconds;
Within(TimeSpan.FromMilliseconds(max), () =>
{
for (var i = 0; i < loops; i++)
{
ExpectMsg("done");
}
});
};

// 2 more should go through without triggering more
await loop(2, 200.Milliseconds());
RouteeSize(router).Should().Be(resizer.LowerBound);

// a whole bunch should max it out
await loop(20, 500.Milliseconds());
RouteeSize(router).Should().Be(resizer.UpperBound);
}

[Fact(Skip = "Racy due to Resizer / Mailbox impl")]
public void DefaultResizer_must_backoff()
{
Expand Down
6 changes: 3 additions & 3 deletions src/core/Akka/Routing/Resizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public int Capacity(IEnumerable<Routee> currentRoutees)
var pressure = Pressure(routees);
var delta = Filter(pressure, currentSize);
var proposed = currentSize + delta;

if (proposed < LowerBound)
return delta + (LowerBound - proposed);
if (proposed > UpperBound)
Expand Down Expand Up @@ -227,9 +227,9 @@ public int Pressure(IEnumerable<Routee> currentRoutees)
if (underlying is ActorCell cell)
{
if (PressureThreshold == 1)
return cell.Mailbox.IsScheduled() && cell.Mailbox.HasMessages;
return (cell.Mailbox.IsScheduled() || cell.Mailbox.IsSuspended()) && cell.Mailbox.HasMessages;
if (PressureThreshold < 1)
return cell.Mailbox.IsScheduled() && cell.CurrentMessage != null;
return (cell.Mailbox.IsScheduled() || cell.Mailbox.IsSuspended()) && cell.CurrentMessage != null;

return cell.Mailbox.NumberOfMessages >= PressureThreshold;
}
Expand Down