-
-
Notifications
You must be signed in to change notification settings - Fork 462
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
Locking in ProviderGraph to prevent duplicate code generation #3109
Merged
jeremydmiller
merged 4 commits into
JasperFx:master
from
felixkmh:3083_duplicate_code_generation
Apr 3, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c538b7c
Added lock to prevent duplicate code generation
felixkmh d791b5a
Added test for concurrent code generation
felixkmh cfa8215
Extracted CreateDocumentProvider method
felixkmh 83a2aab
Added test for concurrently appending DocumentProviders
felixkmh 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Marten; | ||
using Marten.Internal; | ||
using Marten.Internal.Storage; | ||
using Marten.Schema.BulkLoading; | ||
using Marten.Testing.Harness; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace CoreTests.Bugs; | ||
|
||
public class Bug_3083_concurrent_type_generation: BugIntegrationContext | ||
{ | ||
[Fact] | ||
public async Task concurrent_type_generation() | ||
{ | ||
var graph = new ProviderGraph(new StoreOptions()); | ||
|
||
var tasks = new List<Task<DocumentProvider<SomeDocument>>>(); | ||
|
||
for (var i = 0; i < 15; ++i) | ||
{ | ||
var task = Task.Run(() => graph.StorageFor<SomeDocument>()); | ||
|
||
tasks.Add(task); | ||
} | ||
|
||
var storages = new HashSet<DocumentProvider<SomeDocument>>(ReferenceEqualityComparer.Instance); | ||
|
||
foreach (var task in tasks) | ||
{ | ||
var storage = await task; | ||
storages.Add(storage); | ||
} | ||
|
||
storages.ShouldHaveSingleItem(); | ||
} | ||
|
||
[Fact] | ||
public async Task concurrent_append_providers() | ||
{ | ||
var graph = new ProviderGraph(new StoreOptions()); | ||
|
||
var tasks = new List<Task>(); | ||
|
||
var documentProvider1 = new MockDocumentProvider<SomeDocument>(); | ||
var documentProvider2 = new MockDocumentProvider<OtherDocument>(); | ||
var documentProvider3 = new MockDocumentProvider<ThirdDocument>(); | ||
var documentProvider4 = new MockDocumentProvider<ForthDocument>(); | ||
|
||
tasks.Add(Task.Run(() => graph.Append(documentProvider1))); | ||
tasks.Add(Task.Run(() => graph.Append(documentProvider2))); | ||
tasks.Add(Task.Run(() => graph.Append(documentProvider3))); | ||
tasks.Add(Task.Run(() => graph.Append(documentProvider4))); | ||
|
||
await Task.WhenAll(tasks); | ||
|
||
graph.StorageFor<SomeDocument>().ShouldBeTheSameAs(documentProvider1); | ||
graph.StorageFor<OtherDocument>().ShouldBeTheSameAs(documentProvider2); | ||
graph.StorageFor<ThirdDocument>().ShouldBeTheSameAs(documentProvider3); | ||
graph.StorageFor<ForthDocument>().ShouldBeTheSameAs(documentProvider4); | ||
} | ||
|
||
public class MockDocumentProvider<T>: DocumentProvider<T> where T : notnull | ||
{ | ||
public MockDocumentProvider(): this(null, null, null, null, null) | ||
{ | ||
} | ||
|
||
public MockDocumentProvider(IBulkLoader<T> bulkLoader, IDocumentStorage<T> queryOnly, | ||
IDocumentStorage<T> lightweight, IDocumentStorage<T> identityMap, IDocumentStorage<T> dirtyTracking): base( | ||
bulkLoader, queryOnly, lightweight, identityMap, dirtyTracking) | ||
{ | ||
} | ||
} | ||
|
||
public class SomeDocument | ||
{ | ||
public string Id { get; set; } = string.Empty; | ||
} | ||
|
||
public class OtherDocument | ||
{ | ||
public string Id { get; set; } = string.Empty; | ||
} | ||
|
||
public class ThirdDocument | ||
{ | ||
public string Id { get; set; } = string.Empty; | ||
} | ||
|
||
public class ForthDocument | ||
{ | ||
public string Id { get; set; } = string.Empty; | ||
} | ||
} |
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.
Don't do the lock here, it's unnecessary w/ the immutable hash map
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'm not sure how
ImHashMap
s work exactly, but the reason I also locked it is because I thought that if two threads enter this method concurrently, then they callAddOrUpdate
on the same instance. Then they each would create a new map based on that same map instance, resulting in two separate instances that do not contain the item the other thread tried to add. The assignment only happens afterAddOrUpdate
returns, so whoever assigns to_storage
last "wins".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.
Maybe I'll just add that as another test case and see if that can happen. Although tests of that nature aren't the most reliable.
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.
Seems that it is at least possible:
I can't speak to how likely that is in practice, but I'd argue that the impact of keeping the lock would be negligible.