-
-
Notifications
You must be signed in to change notification settings - Fork 97
Advance filtered AllStreamSubscription checkpoints on server checkpointReached #554
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
Open
alexeyzimarev
wants to merge
2
commits into
dev
Choose a base branch
from
feature/all-stream-checkpoint-reached
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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
109 changes: 109 additions & 0 deletions
109
src/KurrentDB/test/Eventuous.Tests.KurrentDB/Subscriptions/CheckpointReachedTests.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,109 @@ | ||
| using Eventuous.KurrentDB.Producers; | ||
| using Eventuous.KurrentDB.Subscriptions; | ||
| using Eventuous.Producers; | ||
| using Eventuous.Subscriptions.Registrations; | ||
| using Eventuous.TestHelpers.TUnit; | ||
| using Eventuous.Tests.Subscriptions.Base; | ||
| using KurrentDB.Client; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Shouldly; | ||
|
|
||
| // ReSharper disable MethodHasAsyncOverload | ||
|
|
||
| namespace Eventuous.Tests.KurrentDB.Subscriptions; | ||
|
|
||
| /// <summary> | ||
| /// Covers the fix for the checkpoint of a filtered <see cref="AllStreamSubscription"/> parking at the | ||
| /// last matched event: with a server-side event filter that never matches, the server-reported | ||
| /// checkpoint position must still flow into the checkpoint store, so a restart doesn't re-scan | ||
| /// everything since the last match. | ||
| /// </summary> | ||
| public class CheckpointReachedTests : StoreFixture { | ||
| readonly string _subscriptionId = $"test-{Guid.NewGuid():N}"; | ||
| readonly StreamName _stream = new($"test-{Guid.NewGuid():N}"); | ||
| IProducer _producer = null!; | ||
| ICheckpointStore _checkpointStore = null!; | ||
| TestEventHandler _handler = null!; | ||
|
|
||
| public CheckpointReachedTests() : base(LogLevel.Information) { | ||
| AutoStart = false; | ||
| TypeMapper.RegisterKnownEventTypes(typeof(TestEvent).Assembly); | ||
| } | ||
|
|
||
| [Test] | ||
| [Category("Special cases")] | ||
| [Timeout(60_000)] | ||
| public async Task CheckpointAdvancesPastUnmatchedEvents(CancellationToken cancellationToken) { | ||
| // Enough unmatched events, and a small MaxSearchWindow, so the server reports a | ||
| // checkpoint position well before it would have scanned the whole write. | ||
| const int count = 500; | ||
|
|
||
| var testEvents = TestEvent.CreateMany(count); | ||
| await _producer.Produce(_stream, testEvents, new(), cancellationToken: cancellationToken); | ||
|
|
||
| await Start(); | ||
|
|
||
| var lastPosition = await GetLastAllStreamPosition(cancellationToken); | ||
|
|
||
| var checkpoint = await PollUntilCheckpointReaches(lastPosition, TimeSpan.FromSeconds(30), cancellationToken); | ||
|
|
||
| await DisposeAsync(); | ||
|
|
||
| // The filter never matched anything, so no event reached the handler... | ||
| _handler.Count.ShouldBe(0); | ||
| // ...yet the checkpoint advanced past the last written (unmatched) event. | ||
| checkpoint.Position.ShouldNotBeNull(); | ||
| checkpoint.Position!.Value.ShouldBeGreaterThanOrEqualTo(lastPosition); | ||
| } | ||
|
|
||
| async Task<ulong> GetLastAllStreamPosition(CancellationToken cancellationToken) { | ||
| var lastEvent = await Client.ReadAllAsync(Direction.Backwards, Position.End, 1, cancellationToken: cancellationToken).ToArrayAsync(cancellationToken); | ||
|
|
||
| return lastEvent.Length == 0 ? 0 : lastEvent[0].Event.Position.CommitPosition; | ||
| } | ||
|
|
||
| // Polls until the checkpoint reaches minPosition, or returns the last-seen checkpoint once the | ||
| // deadline passes (the assertions in the test produce a clear failure message in that case). | ||
| async Task<Checkpoint> PollUntilCheckpointReaches(ulong minPosition, TimeSpan timeout, CancellationToken cancellationToken) { | ||
| var deadline = DateTime.UtcNow + timeout; | ||
| var checkpoint = await _checkpointStore.GetLastCheckpoint(_subscriptionId, cancellationToken); | ||
|
|
||
| while (!(checkpoint.Position is { } position && position >= minPosition) && DateTime.UtcNow < deadline) { | ||
| await Task.Delay(200.Milliseconds(), cancellationToken); | ||
| checkpoint = await _checkpointStore.GetLastCheckpoint(_subscriptionId, cancellationToken); | ||
| } | ||
|
|
||
| return checkpoint; | ||
| } | ||
|
|
||
| protected override void SetupServices(IServiceCollection services) { | ||
| base.SetupServices(services); | ||
| services.AddProducer<KurrentDBProducer>(); | ||
|
|
||
| services.AddSubscription<AllStreamSubscription, AllStreamSubscriptionOptions>( | ||
| _subscriptionId, | ||
| c => c | ||
| .Configure( | ||
| o => { | ||
| // A prefix that will never match the produced test events, so the filter | ||
| // excludes everything, but with a small enough search window that the | ||
| // server still reports checkpoint progress while scanning past them. | ||
| o.EventFilter = EventTypeFilter.Prefix(4, "definitely-does-not-match-anything"); | ||
| o.CheckpointInterval = 1; | ||
|
|
||
| o.CheckpointCommitBatchSize = 1; | ||
| o.CheckpointCommitDelayMs = 100; | ||
| } | ||
| ) | ||
| .UseCheckpointStore<TestCheckpointStore>() | ||
| .AddEventHandler<TestEventHandler>() | ||
| ); | ||
| } | ||
|
|
||
| protected override void GetDependencies(IServiceProvider provider) { | ||
| base.GetDependencies(provider); | ||
| _producer = provider.GetRequiredService<IProducer>(); | ||
| _checkpointStore = provider.GetRequiredKeyedService<TestCheckpointStore>(_subscriptionId); | ||
| _handler = provider.GetRequiredKeyedService<TestEventHandler>(_subscriptionId); | ||
| } | ||
| } |
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.