|
| 1 | +/* |
| 2 | + * Copyright (c) 2017-2020. 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 | +package org.dizitart.no2.mvstore; |
| 18 | + |
| 19 | +import org.dizitart.no2.Nitrite; |
| 20 | +import org.h2.mvstore.MVStore; |
| 21 | +import org.junit.After; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import java.io.File; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Path; |
| 28 | + |
| 29 | +import static org.junit.Assert.assertEquals; |
| 30 | +import static org.junit.Assert.assertNull; |
| 31 | + |
| 32 | +/** |
| 33 | + * The adapter must not force MVStore's chunk retention time and versions-to-keep to 0. With both |
| 34 | + * at 0, H2 may reuse a chunk's blocks while the chunk map it writes at close still lists that |
| 35 | + * chunk, and the file then refuses to open with "Double mark" (h2database/h2database#2752, |
| 36 | + * #4083). A soak of a 24-thread workload with a close every 20 seconds hit that on every run at |
| 37 | + * 0/0 and on none with either of H2's defaults in place. |
| 38 | + */ |
| 39 | +public class MVStoreRetentionDefaultsTest { |
| 40 | + private Path directory; |
| 41 | + private Nitrite db; |
| 42 | + |
| 43 | + @Before |
| 44 | + public void setUp() throws Exception { |
| 45 | + directory = Files.createTempDirectory("nitrite-retention"); |
| 46 | + } |
| 47 | + |
| 48 | + @After |
| 49 | + public void tearDown() throws Exception { |
| 50 | + if (db != null && !db.isClosed()) { |
| 51 | + db.close(); |
| 52 | + } |
| 53 | + try (var files = Files.walk(directory)) { |
| 54 | + files.sorted((a, b) -> b.compareTo(a)).map(Path::toFile).forEach(File::delete); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testDefaultsAreLeftToH2() { |
| 60 | + MVStoreModuleBuilder builder = MVStoreModule.withConfig(); |
| 61 | + assertNull(builder.retentionTime()); |
| 62 | + assertNull(builder.versionsToKeep()); |
| 63 | + |
| 64 | + db = Nitrite.builder().loadModule(builder.filePath(directory.resolve("test.db").toFile()).build()).openOrCreate(); |
| 65 | + MVStore store = mvStore(db); |
| 66 | + assertEquals(45_000, store.getRetentionTime()); |
| 67 | + assertEquals(5, store.getVersionsToKeep()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testConfiguredValuesAreApplied() { |
| 72 | + MVStoreModule module = MVStoreModule.withConfig() |
| 73 | + .filePath(directory.resolve("test.db").toFile()) |
| 74 | + .retentionTime(1_000) |
| 75 | + .versionsToKeep(2) |
| 76 | + .build(); |
| 77 | + db = Nitrite.builder().loadModule(module).openOrCreate(); |
| 78 | + MVStore store = mvStore(db); |
| 79 | + assertEquals(1_000, store.getRetentionTime()); |
| 80 | + assertEquals(2, store.getVersionsToKeep()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testConfigCloneCarriesTheOptions() { |
| 85 | + MVStoreConfig config = new MVStoreConfig(); |
| 86 | + config.retentionTime(7); |
| 87 | + config.versionsToKeep(3); |
| 88 | + MVStoreConfig clone = config.clone(); |
| 89 | + assertEquals(Integer.valueOf(7), clone.retentionTime()); |
| 90 | + assertEquals(Integer.valueOf(3), clone.versionsToKeep()); |
| 91 | + } |
| 92 | + |
| 93 | + private static MVStore mvStore(Nitrite db) { |
| 94 | + try { |
| 95 | + var field = NitriteMVStore.class.getDeclaredField("mvStore"); |
| 96 | + field.setAccessible(true); |
| 97 | + return (MVStore) field.get(db.getStore()); |
| 98 | + } catch (ReflectiveOperationException e) { |
| 99 | + throw new AssertionError(e); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments