-
Notifications
You must be signed in to change notification settings - Fork 2
Publish datastore metadata safely for concurrent AST construction #37
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
lm-sousa
wants to merge
5
commits into
dir-fixes
Choose a base branch
from
ast-flatbuffers
base: dir-fixes
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.
+89
−11
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f49ca2c
feat(SpecsIo): add OS-created temp directory and OS cache folder helpers
lm-sousa 5893091
feat(jOptions): prototype memoized values in data store slots
lm-sousa ead48b7
fix: publish shared datastore metadata safely
lm-sousa ff2b543
refactor(jOptions): move deferred stores to lazy experiment branch
lm-sousa 4279dea
Align eager datastore changes with the temporary-directory PR base
lm-sousa 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
84 changes: 84 additions & 0 deletions
84
jOptions/test/org/suikasoft/jOptions/storedefinition/AStoreDefinitionConcurrencyTest.java
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,84 @@ | ||
| package org.suikasoft.jOptions.storedefinition; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CyclicBarrier; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.Future; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.suikasoft.jOptions.DataStore.ListDataStore; | ||
| import org.suikasoft.jOptions.Datakey.DataKey; | ||
| import org.suikasoft.jOptions.Datakey.KeyFactory; | ||
|
|
||
| class AStoreDefinitionConcurrencyTest { | ||
|
|
||
| private static final int THREADS = 32; | ||
| private static final int SHARED_KEYS = 2_048; | ||
| private static final int STORE_KEYS = 8; | ||
| private static final int ROUNDS = 8; | ||
|
|
||
| @Test | ||
| void concurrentFirstReadersAndStoreCreationSeeCompleteDefinitions() throws Exception { | ||
| ExecutorService executor = Executors.newFixedThreadPool(THREADS); | ||
|
|
||
| try { | ||
| for (int round = 0; round < ROUNDS; round++) { | ||
| final int currentRound = round; | ||
| List<DataKey<?>> sharedKeys = keys("shared-" + currentRound + "-", SHARED_KEYS); | ||
| TestStoreDefinition shared = new TestStoreDefinition("shared-" + currentRound, sharedKeys); | ||
| List<TestStoreDefinition> independent = IntStream.range(0, THREADS) | ||
| .mapToObj(thread -> new TestStoreDefinition("independent-" + currentRound + "-" + thread, | ||
| keys("independent-" + currentRound + "-" + thread + "-", STORE_KEYS))) | ||
| .toList(); | ||
| CyclicBarrier gate = new CyclicBarrier(THREADS + 1); | ||
| List<Future<Integer>> futures = new ArrayList<>(THREADS); | ||
|
|
||
| for (int thread = 0; thread < THREADS; thread++) { | ||
| final int threadIndex = thread; | ||
| futures.add(executor.submit(() -> { | ||
| gate.await(); | ||
|
|
||
| Map<String, DataKey<?>> map = shared.getKeyMap(); | ||
| TestStoreDefinition definition = independent.get(threadIndex); | ||
| DataKey<?> firstKey = definition.getKeys().get(0); | ||
| DataKey<?> secondKey = definition.getKeys().get(1); | ||
| ListDataStore eager = new ListDataStore(definition); | ||
| eager.setRaw(firstKey.getName(), "eager"); | ||
| eager.get(firstKey.getName()); | ||
| eager.setRaw(secondKey.getName(), "second"); | ||
| eager.get(secondKey.getName()); | ||
|
|
||
| return map.size(); | ||
| })); | ||
| } | ||
|
|
||
| gate.await(); | ||
| for (Future<Integer> future : futures) { | ||
| assertThat(future.get()).isEqualTo(SHARED_KEYS); | ||
| } | ||
| } | ||
| } finally { | ||
| executor.shutdownNow(); | ||
| } | ||
| } | ||
|
|
||
| private static List<DataKey<?>> keys(String prefix, int count) { | ||
| List<DataKey<?>> keys = new ArrayList<>(count); | ||
| for (int index = 0; index < count; index++) { | ||
| keys.add(KeyFactory.string(prefix + index)); | ||
| } | ||
| return keys; | ||
| } | ||
|
|
||
| private static final class TestStoreDefinition extends AStoreDefinition { | ||
| private TestStoreDefinition(String name, List<DataKey<?>> keys) { | ||
| super(name, keys); | ||
| } | ||
| } | ||
| } |
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.
StoreDefinition.super.getKeyMap()callsgetKeys()virtually. If a downstreamAStoreDefinitionsubclass overridesgetKeys()and reads fields initialized aftersuper(...), construction now sees uninitialized state and can throw or permanently cache an incomplete map; previously the lazy lookup occurred only after construction. Populate the map directly from the initialized sections or through a non-overridable base method.Useful? React with 👍 / 👎.