-
-
Notifications
You must be signed in to change notification settings - Fork 24
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 #115 Concurrency issue when adding documents. #116
Open
niemyjski
wants to merge
2
commits into
main
Choose a base branch
from
bugfix/race-condition-index-creation
base: main
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
89 changes: 89 additions & 0 deletions
89
tests/Foundatio.Repositories.Elasticsearch.Tests/DailyRepositoryTests.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,89 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Foundatio.Repositories.Elasticsearch.Tests.Repositories; | ||
using Foundatio.Repositories.Elasticsearch.Tests.Repositories.Models; | ||
using Foundatio.Repositories.Models; | ||
using Foundatio.Utility; | ||
using Microsoft.Extensions.Time.Testing; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Foundatio.Repositories.Elasticsearch.Tests; | ||
|
||
public sealed class DailyRepositoryTests : ElasticRepositoryTestBase | ||
{ | ||
private readonly IFileAccessHistoryRepository _fileAccessHistoryRepository; | ||
|
||
public DailyRepositoryTests(ITestOutputHelper output) : base(output) | ||
{ | ||
_fileAccessHistoryRepository = new FileAccessHistoryRepository(_configuration.DailyFileAccessHistory); | ||
} | ||
|
||
public override async Task InitializeAsync() | ||
{ | ||
await base.InitializeAsync(); | ||
await RemoveDataAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task AddAsyncWithCustomDateIndex() | ||
{ | ||
var utcNow = new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc); | ||
var history = await _fileAccessHistoryRepository.AddAsync(new FileAccessHistory { Path = "path1", AccessedDateUtc = utcNow }, o => o.ImmediateConsistency()); | ||
Assert.NotNull(history?.Id); | ||
|
||
var result = await _fileAccessHistoryRepository.FindOneAsync(f => f.Id(history.Id)); | ||
Assert.Equal("file-access-history-daily-v1-2023.01.01", result.Data.GetString("index")); | ||
} | ||
|
||
[Fact] | ||
public async Task AddAsyncWithCurrentDateViaDocumentsAdding() | ||
{ | ||
_configuration.TimeProvider = new FakeTimeProvider(new DateTimeOffset(2023, 02, 1, 0, 0, 0, TimeSpan.Zero)); | ||
|
||
try | ||
{ | ||
// NOTE: This has to be async handler as there is no way to remove a sync handler. | ||
_fileAccessHistoryRepository.DocumentsAdding.AddHandler(OnDocumentsAdding); | ||
|
||
var history = await _fileAccessHistoryRepository.AddAsync(new FileAccessHistory { Path = "path2" }, o => o.ImmediateConsistency()); | ||
Assert.NotNull(history?.Id); | ||
|
||
var result = await _fileAccessHistoryRepository.FindOneAsync(f => f.Id(history.Id)); | ||
Assert.Equal("file-access-history-daily-v1-2023.02.01", result.Data.GetString("index")); | ||
} | ||
finally | ||
{ | ||
_fileAccessHistoryRepository.DocumentsAdding.RemoveHandler(OnDocumentsAdding); | ||
} | ||
} | ||
|
||
private Task OnDocumentsAdding(object sender, DocumentsEventArgs<FileAccessHistory> arg) | ||
{ | ||
foreach (var document in arg.Documents) | ||
{ | ||
if (document.AccessedDateUtc == DateTime.MinValue || document.AccessedDateUtc > _configuration.TimeProvider.GetUtcNow().UtcDateTime) | ||
document.AccessedDateUtc = _configuration.TimeProvider.GetUtcNow().UtcDateTime; | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
[Fact] | ||
public async Task CanAddAsync() | ||
{ | ||
var history = await _fileAccessHistoryRepository.AddAsync(new FileAccessHistory { AccessedDateUtc = DateTime.UtcNow }); | ||
Assert.NotNull(history?.Id); | ||
} | ||
|
||
[Fact] | ||
public Task AddAsyncConcurrentUpdates() | ||
{ | ||
return Parallel.ForEachAsync(Enumerable.Range(0, 50), async (i, _) => | ||
{ | ||
var history = await _fileAccessHistoryRepository.AddAsync(new FileAccessHistory { AccessedDateUtc = DateTime.UtcNow }); | ||
Assert.NotNull(history?.Id); | ||
}); | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
...ies.Elasticsearch.Tests/Repositories/Configuration/Indexes/DailyFileAccessHistoryIndex.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,17 @@ | ||
using Foundatio.Repositories.Elasticsearch.Configuration; | ||
using Foundatio.Repositories.Elasticsearch.Tests.Repositories.Models; | ||
using Nest; | ||
|
||
namespace Foundatio.Repositories.Elasticsearch.Tests.Repositories.Configuration.Indexes; | ||
|
||
public sealed class DailyFileAccessHistoryIndex : DailyIndex<FileAccessHistory> | ||
{ | ||
public DailyFileAccessHistoryIndex(IElasticConfiguration configuration) : base(configuration, "file-access-history-daily", 1, d => ((FileAccessHistory)d).AccessedDateUtc) | ||
{ | ||
} | ||
|
||
public override CreateIndexDescriptor ConfigureIndex(CreateIndexDescriptor idx) | ||
{ | ||
return base.ConfigureIndex(idx.Settings(s => s.NumberOfReplicas(0).NumberOfShards(1))); | ||
} | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It kind of feels like we should be locking on the index name here, but this allows us to go wide. I'm wondering if we should also change this to a concurrent dictionary.