-
-
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
base: main
Are you sure you want to change the base?
Conversation
src/Foundatio.Repositories.Elasticsearch/Configuration/DailyIndex.cs
Outdated
Show resolved
Hide resolved
src/Foundatio.Repositories.Elasticsearch/Configuration/DailyIndex.cs
Outdated
Show resolved
Hide resolved
include date on lock Tweaked logic
174add4
to
8b4b117
Compare
protected async Task EnsureDateIndexAsync(DateTime utcDate) | ||
{ | ||
utcDate = utcDate.Date; | ||
if (_ensuredDates.ContainsKey(utcDate)) | ||
return; | ||
|
||
await using var indexLock = await _ensureIndexLock.AcquireAsync($"Index:{GetVersionedIndex(utcDate)}", TimeSpan.FromMinutes(1)).AnyContext(); |
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.
protected async Task EnsureDateIndexAsync(DateTime utcDate) | ||
{ | ||
utcDate = utcDate.Date; | ||
if (_ensuredDates.ContainsKey(utcDate)) | ||
return; | ||
|
||
await using var indexLock = await _ensureIndexLock.AcquireAsync($"Index:{GetVersionedIndex(utcDate)}", TimeSpan.FromMinutes(1)).AnyContext(); | ||
if (indexLock is null) | ||
throw new Exception("Unable to acquire index lock"); |
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.
We should check to see if another process created the index we are looking for instead of just throwing here. Should just be able to check _ensuredDates.
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.
I like this change. Should be good.
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.
Actually, just thought about something. DailyIndex should be a singleton. Couldn't we just change the _ensuredDates to be a concurrent dictionary and not do a distributed lock?
@ejsmith I thought about that, we probably could. That's making assumptions it is registered as a singleton and then there is alias management going on. Seems safer to do it like this. |
There would actually be a single instance per daily index and we can count on that because that's how it has to be registered for a daily index to work. So I think just changing to a concurrent dictionary would work and is what we want to do. |
There are just multiple threads trying to ensure an index for a specific day at the same time. Which is what the error you are seeing is from. |
Fixes #115