Skip to content

Commit f349fe1

Browse files
authored
fix: persist the index dirty marker (#1281)
markDirty() flips isDirty on the IndexMeta that get() returned and never puts it back, so nothing tells the store the entry changed and the new value is not guaranteed to be written. Both beginIndexing() and endIndexing() go through it, so the marker is unreliable in both directions: an index left half built by a crash can come back reading as clean, and a completed one can stay marked dirty and be rebuilt on the first write after every open. Put the IndexMeta back after flipping the flag.
1 parent 9f3f256 commit f349fe1

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2017-2021 Nitrite author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.dizitart.no2.collection.operation;
19+
20+
import org.dizitart.no2.Nitrite;
21+
import org.dizitart.no2.collection.Document;
22+
import org.dizitart.no2.collection.NitriteCollection;
23+
import org.dizitart.no2.common.Fields;
24+
import org.dizitart.no2.mvstore.MVStoreModule;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
29+
import java.io.File;
30+
import java.util.UUID;
31+
32+
import static org.junit.Assert.assertFalse;
33+
import static org.junit.Assert.assertTrue;
34+
35+
/**
36+
* The index dirty marker is crash-recovery state, so it has to survive a restart.
37+
*/
38+
public class IndexDirtyMarkerDurabilityTest {
39+
private static final String COLLECTION = "dirty-marker-test";
40+
private static final Fields FIELDS = Fields.withNames("value");
41+
42+
private String filePath;
43+
private Nitrite db;
44+
45+
@Before
46+
public void setUp() {
47+
filePath = System.getProperty("java.io.tmpdir") + File.separator + UUID.randomUUID() + ".db";
48+
db = openDb();
49+
50+
NitriteCollection collection = db.getCollection(COLLECTION);
51+
collection.insert(Document.createDocument("value", 1));
52+
collection.createIndex("value");
53+
54+
// make sure the meta map is on disk and its page is clean again, so that only a
55+
// later write of the marker itself can change what the file holds
56+
db.commit();
57+
}
58+
59+
@After
60+
public void tearDown() {
61+
if (db != null && !db.isClosed()) {
62+
db.close();
63+
}
64+
File file = new File(filePath);
65+
if (file.exists() && !file.delete()) {
66+
file.deleteOnExit();
67+
}
68+
}
69+
70+
private Nitrite openDb() {
71+
return Nitrite.builder()
72+
.loadModule(MVStoreModule.withConfig().filePath(filePath).build())
73+
.openOrCreate();
74+
}
75+
76+
private boolean reopenAndReadDirtyMarker() {
77+
db.close();
78+
db = openDb();
79+
try (IndexManager indexManager = new IndexManager(COLLECTION, db.getConfig())) {
80+
return indexManager.isDirtyIndex(FIELDS);
81+
}
82+
}
83+
84+
@Test
85+
public void testMarkerSurvivesRestartWhenIndexingStarted() {
86+
try (IndexManager indexManager = new IndexManager(COLLECTION, db.getConfig())) {
87+
indexManager.beginIndexing(FIELDS);
88+
}
89+
90+
assertTrue("an index that was left mid-build must still read as dirty after a restart",
91+
reopenAndReadDirtyMarker());
92+
}
93+
94+
@Test
95+
public void testMarkerIsClearedAcrossRestartWhenIndexingCompleted() {
96+
try (IndexManager indexManager = new IndexManager(COLLECTION, db.getConfig())) {
97+
indexManager.beginIndexing(FIELDS);
98+
indexManager.endIndexing(FIELDS);
99+
}
100+
101+
assertFalse("a completed index must not read as dirty after a restart",
102+
reopenAndReadDirtyMarker());
103+
}
104+
}

‎nitrite/src/main/java/org/dizitart/no2/collection/operation/IndexManager.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ private void markDirty(Fields fields, boolean dirty) {
213213
IndexMeta meta = indexMetaMap.get(fields);
214214
if (meta != null && meta.getIndexDescriptor() != null) {
215215
meta.getIsDirty().set(dirty);
216+
// put the meta back, otherwise the flag is only changed on the instance get()
217+
// returned and the store never learns the entry has to be written. The marker is
218+
// crash-recovery state, so both directions of it have to reach the disk.
219+
indexMetaMap.put(fields, meta);
216220
}
217221
}
218222

0 commit comments

Comments
 (0)