-
-
Notifications
You must be signed in to change notification settings - Fork 96
fix: keep the collection state check outside the factory-wide lock #1292
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
Merged
anidotnet
merged 1 commit into
nitrite:main
from
brettwooldridge:fix/collection-factory-lock-convoy
Sep 4, 2026
+141
−9
Merged
Changes from all commits
Commits
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
105 changes: 105 additions & 0 deletions
105
nitrite/src/test/java/org/dizitart/no2/collection/CollectionFactoryConvoyTest.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,105 @@ | ||
| /* | ||
| * Copyright (c) 2017-2020. Nitrite author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.dizitart.no2.collection; | ||
|
|
||
| import org.dizitart.no2.Nitrite; | ||
| import org.dizitart.no2.filters.Filter; | ||
| import org.dizitart.no2.integration.Retry; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.junit.Assert.assertSame; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| /** | ||
| * A long write on one collection must not stall {@code getCollection} for every other | ||
| * collection. | ||
| * <p> | ||
| * {@link CollectionFactory#getCollection} decides whether a registered collection is still | ||
| * usable by calling {@code isDropped()} and {@code isOpen()}, which take that collection's | ||
| * read lock. While a write holds the collection's write lock, a caller asking for that | ||
| * collection waits on it, which is expected. The factory used to make that check while | ||
| * holding its own factory-wide lock, so that one waiting caller also blocked every caller | ||
| * asking for any other collection until the write finished. | ||
| */ | ||
| public class CollectionFactoryConvoyTest { | ||
| @Rule | ||
| public Retry retry = new Retry(3); | ||
|
|
||
| private Nitrite db; | ||
| private ExecutorService executor; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| db = Nitrite.builder().openOrCreate(); | ||
| executor = Executors.newCachedThreadPool(); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| executor.shutdownNow(); | ||
| db.close(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWriteOnOneCollectionDoesNotBlockGetCollectionOfAnother() throws Exception { | ||
| NitriteCollection busy = db.getCollection("busy"); | ||
| busy.insert(Document.createDocument("key", 1)); | ||
| NitriteCollection other = db.getCollection("other"); | ||
|
|
||
| // a remove(filter) evaluates the filter under the collection's write lock; park it there | ||
| CountDownLatch inFilter = new CountDownLatch(1); | ||
| CountDownLatch release = new CountDownLatch(1); | ||
| Filter parked = element -> { | ||
| inFilter.countDown(); | ||
| try { | ||
| release.await(); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| return false; | ||
| }; | ||
| Future<?> writer = executor.submit(() -> busy.remove(parked)); | ||
| assertTrue("writer never entered the filter", inFilter.await(5, TimeUnit.SECONDS)); | ||
|
|
||
| // a caller asking for the busy collection has to wait for that write; that is expected | ||
| Future<NitriteCollection> waitingForBusy = executor.submit(() -> db.getCollection("busy")); | ||
| Thread.sleep(200); | ||
| assertTrue("caller for the busy collection should be waiting", !waitingForBusy.isDone()); | ||
|
|
||
| // ... but callers asking for other collections, registered or new, must not queue behind it | ||
| Future<NitriteCollection> registered = executor.submit(() -> db.getCollection("other")); | ||
| Future<NitriteCollection> created = executor.submit(() -> db.getCollection("fresh")); | ||
| try { | ||
| assertSame(other, registered.get(2, TimeUnit.SECONDS)); | ||
| assertTrue(created.get(2, TimeUnit.SECONDS).isOpen()); | ||
| } finally { | ||
| release.countDown(); | ||
| } | ||
|
|
||
| writer.get(5, TimeUnit.SECONDS); | ||
| assertSame(busy, waitingForBusy.get(5, TimeUnit.SECONDS)); | ||
| } | ||
| } |
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.
🚀 Performance & Scalability | 🟡 Minor | ⚡ Quick win
isUsable(current)runs with the factory write lock held.This branch takes the replacement collection's read lock while the factory write lock is held. If the replacement instance is inside a long write, every caller for every other collection queues behind this thread again. The scenario is narrow, but it reintroduces the convoy the rest of the method removes.
Release the factory lock and retry the outer check instead of validating a foreign instance inside the write lock.
♻️ Proposed retry loop
📝 Committable suggestion
🤖 Prompt for AI Agents