-
-
Notifications
You must be signed in to change notification settings - Fork 380
Make FaweStreamChangeSet.blockSize atomic and document read reentrancy #3598
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 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 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
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
104 changes: 104 additions & 0 deletions
104
.../java/com/fastasyncworldedit/core/history/changeset/FaweStreamChangeSetBlockSizeTest.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,104 @@ | ||
| package com.fastasyncworldedit.core.history.changeset; | ||
|
|
||
| import com.fastasyncworldedit.core.configuration.Settings; | ||
| import com.fastasyncworldedit.core.history.MemoryOptimizedHistory; | ||
| import com.sk89q.worldedit.world.World; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.parallel.Isolated; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| /** | ||
| * Regression test for the {@code blockSize} counter on {@link FaweStreamChangeSet}. Historically | ||
| * this was a plain, unsynchronized {@code long} incremented from multiple pipeline worker | ||
| * threads via {@code blockSize++}, which is not atomic and can silently lose increments under | ||
| * real contention. It is now a {@link java.util.concurrent.atomic.LongAdder}, which this test | ||
| * verifies is accurate under many concurrent writers. | ||
| * | ||
| * <p>Forces {@code Settings.settings().HISTORY.COMPRESSION_LEVEL} to 0, which bypasses the | ||
| * compression backend entirely (see {@code MainUtil#getCompressedOS}), so this test only measures | ||
| * counter accuracy and isn't coupled to compression behavior. Because {@link Settings} is | ||
| * process-global mutable state, this class is marked {@link Isolated} so no other test running | ||
| * concurrently in the same JVM observes the temporarily-changed level.</p> | ||
| */ | ||
| @Isolated | ||
| class FaweStreamChangeSetBlockSizeTest { | ||
|
|
||
| private static final int THREADS = 16; | ||
| private static final int CALLS_PER_THREAD = 1000; | ||
|
|
||
| @Test | ||
| void addIsAccurateUnderConcurrentWriters() throws InterruptedException, java.io.IOException { | ||
| int previousCompressionLevel = Settings.settings().HISTORY.COMPRESSION_LEVEL; | ||
| // Compression level 0 skips the LZ4/Zstd codecs entirely (see MainUtil#getCompressedOS), | ||
| // which keeps this test independent of the compileOnly lz4-java dependency. | ||
| Settings.settings().HISTORY.COMPRESSION_LEVEL = 0; | ||
|
|
||
| World world = mock(World.class); | ||
| when(world.getMinY()).thenReturn(-64); | ||
| when(world.getMaxY()).thenReturn(319); | ||
|
|
||
| MemoryOptimizedHistory changeSet = new MemoryOptimizedHistory(world); | ||
| ExecutorService executor = Executors.newFixedThreadPool(THREADS); | ||
|
MattBDev marked this conversation as resolved.
|
||
| try { | ||
| // Pre-initialize the lazy block-output stream single-threaded, before any concurrent | ||
| // add() calls. getBlockOS()'s own double-checked-locking race is fixed independently | ||
| // in a sibling PR, not this one - on this branch it's still present, so without this | ||
| // warm-up the first wave of concurrent add() calls below would race on that unrelated | ||
| // lazy-init path too, rather than exercising only the blockSize counter this test | ||
| // targets. getBlockOS() itself doesn't touch blockSize, so this doesn't affect the | ||
| // expected total asserted below. | ||
| changeSet.getBlockOS(0, 0, 0); | ||
|
|
||
| CountDownLatch startLatch = new CountDownLatch(1); | ||
|
|
||
| List<Future<?>> futures = new ArrayList<>(THREADS); | ||
| for (int t = 0; t < THREADS; t++) { | ||
| final int threadIndex = t; | ||
| Callable<Void> task = () -> { | ||
| startLatch.await(); | ||
| for (int i = 0; i < CALLS_PER_THREAD; i++) { | ||
| changeSet.add(threadIndex, 0, i, 0, 1); | ||
| } | ||
| return null; | ||
| }; | ||
| futures.add(executor.submit(task)); | ||
| } | ||
|
|
||
| startLatch.countDown(); | ||
| for (Future<?> future : futures) { | ||
| try { | ||
| future.get(30, TimeUnit.SECONDS); | ||
| } catch (ExecutionException e) { | ||
| throw new AssertionError("worker thread failed while calling add()", e.getCause()); | ||
| } catch (java.util.concurrent.TimeoutException e) { | ||
| throw new AssertionError("worker thread did not finish in time", e); | ||
| } | ||
|
MattBDev marked this conversation as resolved.
|
||
| } | ||
| executor.shutdown(); | ||
| assertTrue(executor.awaitTermination(30, TimeUnit.SECONDS), "executor did not terminate in time"); | ||
|
|
||
| assertEquals((long) THREADS * CALLS_PER_THREAD, changeSet.longSize()); | ||
| } finally { | ||
| // shutdownNow() runs even on the failure paths above (a worker throwing or timing | ||
| // out), so a stuck/failed worker never leaks non-daemon threads into the rest of the | ||
| // suite. It's a no-op once the graceful shutdown() above has already succeeded. | ||
| executor.shutdownNow(); | ||
| Settings.settings().HISTORY.COMPRESSION_LEVEL = previousCompressionLevel; | ||
| } | ||
| } | ||
|
|
||
| } | ||
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.