-
-
Notifications
You must be signed in to change notification settings - Fork 95
test: regression test for the legacy index drop race (#1315) #1319
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
+90
−1
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
88 changes: 88 additions & 0 deletions
88
...-mvstore-adapter/src/test/java/org/dizitart/no2/integration/collection/Issue1315Test.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,88 @@ | ||
| /* | ||
| * Copyright (c) 2017-2021 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.integration.collection; | ||
|
|
||
| import org.dizitart.no2.Nitrite; | ||
| import org.dizitart.no2.collection.Document; | ||
| import org.dizitart.no2.collection.NitriteCollection; | ||
| import org.dizitart.no2.filters.FluentFilter; | ||
| import org.dizitart.no2.index.IndexOptions; | ||
| import org.dizitart.no2.index.IndexType; | ||
| import org.dizitart.no2.mvstore.MVStoreModule; | ||
| import org.junit.Test; | ||
|
|
||
| import java.io.File; | ||
| import java.util.List; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
| import java.util.concurrent.CyclicBarrier; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| /** | ||
| * Regression test for <a href="https://github.com/nitrite/nitrite-java/issues/1315">Issue 1315</a>. | ||
| * <p> | ||
| * On 5.3.0 every close left an empty legacy index map behind, and the first finds on a | ||
| * reopened file database raced to migrate and drop it, failing with a | ||
| * {@code NullPointerException} in {@code NitriteMVStore.removeMap}. Fixed by #1295 and #1309. | ||
| */ | ||
| public class Issue1315Test { | ||
|
|
||
| private static Nitrite open(File file) { | ||
| return Nitrite.builder() | ||
| .loadModule(MVStoreModule.withConfig().filePath(file.getPath()).build()) | ||
| .openOrCreate(); | ||
| } | ||
|
|
||
| @Test | ||
| public void firstUseOfANonUniqueIndexFromSeveralThreads() throws Exception { | ||
| File file = File.createTempFile("nitrite-legacy-race", ".db"); | ||
| file.delete(); | ||
| try { | ||
| Nitrite db = open(file); | ||
| NitriteCollection items = db.getCollection("items"); | ||
| items.createIndex(IndexOptions.indexOptions(IndexType.NON_UNIQUE), "state"); | ||
| items.insert(Document.createDocument("state", "new")); | ||
| db.close(); | ||
|
|
||
| int threads = 4, openings = 100; | ||
| List<Throwable> failures = new CopyOnWriteArrayList<>(); | ||
| for (int opening = 0; opening < openings; opening++) { | ||
| Nitrite reopened = open(file); | ||
| NitriteCollection collection = reopened.getCollection("items"); | ||
| CyclicBarrier start = new CyclicBarrier(threads); | ||
| Thread[] workers = new Thread[threads]; | ||
| for (int i = 0; i < threads; i++) { | ||
| workers[i] = new Thread(() -> { | ||
| try { | ||
| start.await(); | ||
| assertEquals(1, collection.find(FluentFilter.where("state").eq("new")).toList().size()); | ||
| } catch (Throwable t) { | ||
| failures.add(t); | ||
| } | ||
| }); | ||
| workers[i].start(); | ||
| } | ||
| for (Thread worker : workers) worker.join(); | ||
| reopened.close(); | ||
| } | ||
| assertEquals(failures.toString(), 0, failures.size()); | ||
| } finally { | ||
| file.delete(); | ||
| } | ||
| } | ||
| } | ||
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: nitrite/nitrite-java
Length of output: 9522
🏁 Script executed:
Repository: nitrite/nitrite-java
Length of output: 41759
🏁 Script executed:
Repository: nitrite/nitrite-java
Length of output: 11420
🏁 Script executed:
Repository: nitrite/nitrite-java
Length of output: 41725
Seed a legacy index map before the concurrent finds.
The insert writes the non-unique index in the current composite layout. After reopening, each indexed find checks for the legacy map, but the fresh database does not establish one. The workers therefore do not exercise
migrateLegacyIndex()clearing and dropping that map. The test can pass even if the#1315migration race returns.Use a pre-fix database fixture or explicitly seed the legacy-layout map before reopening.
🤖 Prompt for AI Agents