-
Notifications
You must be signed in to change notification settings - Fork 0
fix(archive): keep checkpoint reads consistent #472
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
+110
−2
Merged
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
90 changes: 90 additions & 0 deletions
90
src/EventStore.Core.XUnit.Tests/Services/Archive/Storage/LocalArchiveStorageTests.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,90 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using EventStore.Core.Services.Archive.Naming; | ||
| using EventStore.Core.Tests.TransactionLog; | ||
| using EventStore.Core.TransactionLog.FileNamingStrategy; | ||
| using Xunit; | ||
|
|
||
| namespace EventStore.Core.XUnit.Tests.Services.Archive.Storage; | ||
|
|
||
| public class LocalArchiveStorageTests : DirectoryPerTest<LocalArchiveStorageTests> | ||
| { | ||
| private const string ArchiveCheckpointFile = "archive.chk"; | ||
| private const int CheckpointUpdates = 2_000; | ||
|
|
||
| [Fact] | ||
| public async Task checkpoint_can_be_replaced_while_a_reader_is_open() | ||
| { | ||
| var archive = new LocalArchiveStorage( | ||
| Fixture.Directory, | ||
| new ArchiveChunkNamer(new VersionedPatternFileNamingStrategy(Fixture.Directory, "chunk-")), | ||
| ArchiveCheckpointFile); | ||
| await archive.SetCheckpoint(1, CancellationToken.None); | ||
|
|
||
| using var handle = File.OpenHandle( | ||
| Path.Combine(Fixture.Directory, ArchiveCheckpointFile), | ||
| share: FileShare.ReadWrite | FileShare.Delete); | ||
| await archive.SetCheckpoint(2, CancellationToken.None); | ||
|
|
||
| Assert.Equal(2, await archive.GetCheckpoint(CancellationToken.None)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task concurrent_checkpoint_reads_never_observe_partial_updates() | ||
| { | ||
| var archive = new LocalArchiveStorage( | ||
| Fixture.Directory, | ||
| new ArchiveChunkNamer(new VersionedPatternFileNamingStrategy(Fixture.Directory, "chunk-")), | ||
| ArchiveCheckpointFile); | ||
| await archive.SetCheckpoint(0, CancellationToken.None); | ||
|
|
||
| var start = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| var firstCheckpointPublished = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| var checkpointObserved = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| var writerCompleted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| var checkpointReads = 0; | ||
| var writer = Task.Run(async () => | ||
| { | ||
| await start.Task; | ||
| try | ||
| { | ||
| for (var checkpoint = 1; checkpoint <= CheckpointUpdates; checkpoint++) | ||
| { | ||
| await archive.SetCheckpoint(checkpoint, CancellationToken.None); | ||
| if (checkpoint == 1) | ||
| { | ||
| firstCheckpointPublished.SetResult(); | ||
| await checkpointObserved.Task; | ||
| } | ||
| await Task.Yield(); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| writerCompleted.SetResult(); | ||
| } | ||
| }); | ||
|
|
||
| var readers = Enumerable.Range(0, Math.Clamp(Environment.ProcessorCount, 2, 8)) | ||
| .Select(_ => Task.Run(async () => | ||
| { | ||
| await start.Task; | ||
| await firstCheckpointPublished.Task; | ||
| while (!writerCompleted.Task.IsCompleted) | ||
| { | ||
| Assert.InRange(await archive.GetCheckpoint(CancellationToken.None), 0, CheckpointUpdates); | ||
| Interlocked.Increment(ref checkpointReads); | ||
| checkpointObserved.TrySetResult(); | ||
| await Task.Yield(); | ||
| } | ||
| })) | ||
| .ToArray(); | ||
|
|
||
| start.SetResult(); | ||
| await Task.WhenAll(readers.Append(writer)); | ||
| Assert.True(checkpointReads > 0); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.