Skip to content

harden ReliableDeliveryShardingSpecs #6750

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public async Task ReliableDelivery_with_Sharding_must_illustrate_Sharding_usage(
$"producer-{_idCount}");

// expecting 3 end messages, one for each entity: "entity-0", "entity-1", "entity-2"
await consumerEndProbe.ReceiveNAsync(3, TimeSpan.FromSeconds(25)).ToListAsync();
await consumerEndProbe.ReceiveNAsync(3, TimeSpan.FromSeconds(5)).ToListAsync();
}

[Fact]
Expand Down Expand Up @@ -128,12 +128,13 @@ public async Task ReliableDelivery_with_Sharding_must_illustrate_Sharding_usage_
$"p2-{_idCount}");

// expecting 3 end messages, one for each entity: "entity-0", "entity-1", "entity-2"
var endMessages = await consumerEndProbe.ReceiveNAsync(3, TimeSpan.FromSeconds(25)).ToListAsync();
var endMessages = await consumerEndProbe.ReceiveNAsync(3, TimeSpan.FromSeconds(5)).ToListAsync();

var producerIds = endMessages.Cast<Collected>().SelectMany(c => c.ProducerIds).ToList();
producerIds
.Should().BeEquivalentTo($"p1-{_idCount}-entity-0", $"p1-{_idCount}-entity-1", $"p1-{_idCount}-entity-2",
$"p2-{_idCount}-entity-0", $"p2-{_idCount}-entity-1", $"p2-{_idCount}-entity-2");

}

[Fact]
Expand Down Expand Up @@ -413,7 +414,7 @@ public async Task
}

// redeliver also when no more messages are sent to the entity
await consumerProbes[1].GracefulStop(RemainingOrDefault);
Sys.Stop(consumerProbes[1]); // don't wait for termination
Copy link
Member Author

Choose a reason for hiding this comment

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

This might not be the only fix this tests needs, FYI. No way to know for certain other than gathering more data.


var delivery4b = await consumerProbes[2].ExpectMsgAsync<ConsumerController.Delivery<Job>>();
delivery4b.Message.Should().BeEquivalentTo(new Job("msg-4"));
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Tests/Delivery/ReliableDeliverySpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public async Task ReliableDelivery_must_allow_replacement_of_destination()

var consumerEndProbe2 = CreateTestProbe();
var consumerController2 = Sys.ActorOf(ConsumerController.Create<Job>(Sys, Option<IActorRef>.None), $"consumerController2-{_idCount}");
var testConsumer2 = Sys.ActorOf(TestConsumer.PropsFor(DefaultConsumerDelay, 42, consumerEndProbe2.Ref, consumerController2), $"destination2-{_idCount}");
var testConsumer2 = Sys.ActorOf(TestConsumer.PropsFor(DefaultConsumerDelay, 42, consumerEndProbe2.Ref, consumerController2, supportsRestarts:true), $"destination2-{_idCount}");

consumerController2.Tell(new ConsumerController.RegisterToProducerController<Job>(producerController));

Expand Down
20 changes: 14 additions & 6 deletions src/core/Akka.Tests/Delivery/TestConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ public sealed class TestConsumer : ReceiveActor, IWithTimers

private readonly ILoggingAdapter _log = Context.GetLogger();
private ImmutableHashSet<(string, long)> _processed = ImmutableHashSet<(string, long)>.Empty;
private readonly bool _supportRestarts = false;
private int _messageCount = 0;

public TestConsumer(TimeSpan delay, Func<SomeAsyncJob, bool> endCondition, IActorRef endReplyTo,
IActorRef consumerController)
IActorRef consumerController, bool supportRestarts = false)
{
Delay = delay;
EndCondition = endCondition;
EndReplyTo = endReplyTo;
ConsumerController = consumerController;
_supportRestarts = supportRestarts;

Active();
}
Expand Down Expand Up @@ -73,12 +75,18 @@ private void Active()
_log.Info("processed [{0}] [msg: {1}] from [{2}]", job.SeqNr, job.Msg.Payload, job.ProducerId);
job.ConfirmTo.Tell(global::Akka.Delivery.ConsumerController.Confirmed.Instance);

if (EndCondition(job))
if (EndCondition(job) && (_messageCount > 0 || _supportRestarts))
{
_log.Debug("End at [{0}]", job.SeqNr);
EndReplyTo.Tell(new Collected(_processed.Select(c => c.Item1).ToImmutableHashSet(), _messageCount + 1));
Context.Stop(Self);
}
else if (!_supportRestarts && EndCondition(job))
{
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the figure - if the EndCondition is satisfied before we've processed any messages, we're probably being recreated via the ProducerController<T> + ShardRegion, therefore we want to ignore without sending anymore Collected responses.

// BugFix: TestConsumer was recreated by a message sent by the Sharding system, but the EndCondition was already met
// and we don't want to send another Collected that is missing some of the figures. Ignore.

}
else
{
_processed = cleanProcessed.Add(nextMsg);
Expand Down Expand Up @@ -181,12 +189,12 @@ public static ConsumerController.SequencedMessage<Job> SequencedMessage(string p

private static Func<SomeAsyncJob, bool> ConsumerEndCondition(long seqNr) => msg => msg.SeqNr >= seqNr;

public static Props PropsFor(TimeSpan delay, long seqNr, IActorRef endReplyTo, IActorRef consumerController) =>
Props.Create(() => new TestConsumer(delay, ConsumerEndCondition(seqNr), endReplyTo, consumerController));
public static Props PropsFor(TimeSpan delay, long seqNr, IActorRef endReplyTo, IActorRef consumerController, bool supportsRestarts = false) =>
Props.Create(() => new TestConsumer(delay, ConsumerEndCondition(seqNr), endReplyTo, consumerController, supportsRestarts));

public static Props PropsFor(TimeSpan delay, Func<SomeAsyncJob, bool> endCondition, IActorRef endReplyTo,
IActorRef consumerController) =>
Props.Create(() => new TestConsumer(delay, endCondition, endReplyTo, consumerController));
IActorRef consumerController, bool supportsRestarts = false) =>
Props.Create(() => new TestConsumer(delay, endCondition, endReplyTo, consumerController, supportsRestarts));

public ITimerScheduler Timers { get; set; } = null!;
}
Expand Down