-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix persistence liveness check deadlock (#269)
- Loading branch information
Showing
10 changed files
with
263 additions
and
19 deletions.
There are no files selected for viewing
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
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
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
58 changes: 58 additions & 0 deletions
58
src/Akka.HealthCheck.Persistence.Tests/JournalInterceptors.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,58 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="JournalInterceptors.cs" company="Petabridge, LLC"> | ||
// Copyright (C) 2015 - 2024 Petabridge, LLC <https://petabridge.com> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Akka.Persistence; | ||
using Akka.Persistence.TestKit; | ||
|
||
namespace Akka.HealthCheck.Persistence.Tests; | ||
|
||
public static class JournalInterceptors | ||
{ | ||
internal class Noop : IJournalInterceptor | ||
{ | ||
public static readonly Noop Instance = new (); | ||
|
||
private Noop() | ||
{} | ||
|
||
public Task InterceptAsync(IPersistentRepresentation message) => Task.FromResult(true); | ||
} | ||
|
||
public class CancelableDelay: IJournalInterceptor | ||
{ | ||
public CancelableDelay(TimeSpan delay, IJournalInterceptor next, CancellationToken cancellationToken) | ||
{ | ||
_delay = delay; | ||
_next = next; | ||
_cancellationToken = cancellationToken; | ||
} | ||
|
||
private readonly TimeSpan _delay; | ||
private readonly IJournalInterceptor _next; | ||
private readonly CancellationToken _cancellationToken; | ||
|
||
public async Task InterceptAsync(IPersistentRepresentation message) | ||
{ | ||
try | ||
{ | ||
await Task.Delay(_delay, _cancellationToken); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
// no-op | ||
} | ||
catch (TimeoutException) | ||
{ | ||
// no-op | ||
} | ||
await _next.InterceptAsync(message); | ||
} | ||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
src/Akka.HealthCheck.Persistence.Tests/LivenessProbeTimeoutSpec.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,92 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="LivenessProbeTimeoutSpec.cs" company="Petabridge, LLC"> | ||
// Copyright (C) 2015 - 2024 Petabridge, LLC <https://petabridge.com> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Akka.Actor; | ||
using Akka.HealthCheck.Liveness; | ||
using Akka.Persistence.TestKit; | ||
using FluentAssertions; | ||
using FluentAssertions.Extensions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Akka.HealthCheck.Persistence.Tests; | ||
|
||
public class LivenessProbeTimeoutSpec: PersistenceTestKit | ||
{ | ||
public LivenessProbeTimeoutSpec(ITestOutputHelper output) : base(nameof(LivenessProbeTimeoutSpec), output) | ||
{ | ||
} | ||
|
||
[Fact(DisplayName = "AkkaPersistenceLivenessProbe should time out if SaveSnapshot does not respond")] | ||
public async Task SaveSnapshotTimeoutTest() | ||
{ | ||
using var cts = new CancellationTokenSource(); | ||
var delay = new SnapshotInterceptors.CancelableDelay(30.Minutes(), SnapshotInterceptors.Noop.Instance, cts.Token); | ||
|
||
await WithSnapshotSave( | ||
save => save.SetInterceptorAsync(delay), | ||
() => TestTimeout(cts)); | ||
} | ||
|
||
[Fact(DisplayName = "AkkaPersistenceLivenessProbe should time out if snapshot recovery does not respond")] | ||
public async Task SnapshotLoadTimeoutTest() | ||
{ | ||
using var cts = new CancellationTokenSource(); | ||
var delay = new SnapshotInterceptors.CancelableDelay(30.Minutes(), SnapshotInterceptors.Noop.Instance, cts.Token); | ||
|
||
await WithSnapshotLoad( | ||
save => save.SetInterceptorAsync(delay), | ||
() => TestTimeout(cts)); | ||
} | ||
|
||
[Fact(DisplayName = "AkkaPersistenceLivenessProbe should time out if journal Persist does not respond")] | ||
public async Task JournalPersistTimeoutTest() | ||
{ | ||
using var cts = new CancellationTokenSource(); | ||
var delay = new JournalInterceptors.CancelableDelay(30.Minutes(), JournalInterceptors.Noop.Instance, cts.Token); | ||
|
||
await WithJournalWrite( | ||
save => save.SetInterceptorAsync(delay), | ||
() => TestTimeout(cts)); | ||
} | ||
|
||
[Fact(DisplayName = "AkkaPersistenceLivenessProbe should time out if journal recovery does not respond")] | ||
public async Task JournalRecoveryTimeoutTest() | ||
{ | ||
using var cts = new CancellationTokenSource(); | ||
var delay = new JournalInterceptors.CancelableDelay(30.Minutes(), JournalInterceptors.Noop.Instance, cts.Token); | ||
|
||
await WithJournalRecovery( | ||
save => save.SetInterceptorAsync(delay), | ||
() => TestTimeout(cts)); | ||
} | ||
|
||
private async Task TestTimeout(CancellationTokenSource cts) | ||
{ | ||
var probeActor = Sys.ActorOf(Props.Create(() => new AkkaPersistenceLivenessProbe(true, 250.Milliseconds(), 500.Milliseconds()))); | ||
probeActor.Tell(new SubscribeToLiveness(TestActor)); | ||
var status = ExpectMsg<LivenessStatus>(); | ||
status.IsLive.Should().BeFalse(); | ||
status.StatusMessage.Should().StartWith("Warming up probe."); | ||
|
||
var timeoutStatusObj = await FishForMessageAsync( | ||
msg => msg is LivenessStatus stat && !stat.StatusMessage.StartsWith("Warming up probe."), | ||
6.Seconds()); | ||
|
||
var timeoutStatus = (LivenessStatus)timeoutStatusObj; | ||
timeoutStatus.IsLive.Should().BeFalse(); | ||
timeoutStatus.StatusMessage.Should().StartWith("Timeout while checking persistence liveness."); | ||
|
||
cts.Cancel(); | ||
|
||
await AwaitAssertAsync( | ||
() => ExpectMsg<LivenessStatus>().IsLive.Should().BeTrue(), | ||
TimeSpan.FromSeconds(10)); | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
src/Akka.HealthCheck.Persistence.Tests/SnapshotInterceptors.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,58 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="FailingTestSnapshotStore.cs" company="Petabridge, LLC"> | ||
// Copyright (C) 2015 - 2024 Petabridge, LLC <https://petabridge.com> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Akka.Persistence; | ||
using Akka.Persistence.TestKit; | ||
|
||
namespace Akka.HealthCheck.Persistence.Tests; | ||
|
||
public static class SnapshotInterceptors | ||
{ | ||
public class Noop : ISnapshotStoreInterceptor | ||
{ | ||
public static readonly Noop Instance = new (); | ||
|
||
private Noop() | ||
{ | ||
} | ||
|
||
public Task InterceptAsync(string persistenceId, SnapshotSelectionCriteria criteria) => Task.FromResult(true); | ||
} | ||
|
||
public class CancelableDelay: ISnapshotStoreInterceptor | ||
{ | ||
public CancelableDelay(TimeSpan delay, ISnapshotStoreInterceptor next, CancellationToken cancellationToken) | ||
{ | ||
_delay = delay; | ||
_next = next; | ||
_cancellationToken = cancellationToken; | ||
} | ||
|
||
private readonly TimeSpan _delay; | ||
private readonly ISnapshotStoreInterceptor _next; | ||
private readonly CancellationToken _cancellationToken; | ||
|
||
public async Task InterceptAsync(string persistenceId, SnapshotSelectionCriteria criteria) | ||
{ | ||
try | ||
{ | ||
await Task.Delay(_delay, _cancellationToken); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
// no-op | ||
} | ||
catch (TimeoutException) | ||
{ | ||
// no-op | ||
} | ||
await _next.InterceptAsync(persistenceId, criteria); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.