|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Eclipse RDF4J contributors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Distribution License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: BSD-3-Clause |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.rdf4j.sail.lucene.impl; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.Collection; |
| 17 | +import java.util.List; |
| 18 | +import java.util.concurrent.atomic.AtomicInteger; |
| 19 | + |
| 20 | +import org.apache.lucene.analysis.standard.StandardAnalyzer; |
| 21 | +import org.apache.lucene.store.Directory; |
| 22 | +import org.apache.lucene.store.FilterDirectory; |
| 23 | +import org.apache.lucene.store.RAMDirectory; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Piotr Sowiński |
| 28 | + */ |
| 29 | +public class DelayedSyncDirectoryWrapperTest { |
| 30 | + |
| 31 | + private static final class TrackingDirectory extends FilterDirectory { |
| 32 | + private AtomicInteger syncCount = new AtomicInteger(0); |
| 33 | + private AtomicInteger metaSyncCount = new AtomicInteger(0); |
| 34 | + private AtomicInteger closeCount = new AtomicInteger(0); |
| 35 | + |
| 36 | + TrackingDirectory(Directory in) { |
| 37 | + super(in); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void sync(Collection<String> names) throws IOException { |
| 42 | + syncCount.getAndIncrement(); |
| 43 | + super.sync(names); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void syncMetaData() throws IOException { |
| 48 | + metaSyncCount.getAndIncrement(); |
| 49 | + super.syncMetaData(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void close() throws IOException { |
| 54 | + closeCount.getAndIncrement(); |
| 55 | + super.close(); |
| 56 | + } |
| 57 | + |
| 58 | + public int getSyncCount() { |
| 59 | + return syncCount.get(); |
| 60 | + } |
| 61 | + |
| 62 | + public int getMetaSyncCount() { |
| 63 | + return metaSyncCount.get(); |
| 64 | + } |
| 65 | + |
| 66 | + public int getCloseCount() { |
| 67 | + return closeCount.get(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testSyncData() throws IOException { |
| 73 | + final var dir = new TrackingDirectory(new RAMDirectory()); |
| 74 | + final var delayedDir = new DelayedSyncDirectoryWrapper(dir, 500); |
| 75 | + |
| 76 | + assertEquals(0, dir.getSyncCount()); |
| 77 | + assertEquals(0, dir.getMetaSyncCount()); |
| 78 | + |
| 79 | + delayedDir.sync(List.of("file1", "file2")); |
| 80 | + |
| 81 | + // The sync should be delayed, so the count should still be 0 |
| 82 | + assertEquals(0, dir.getSyncCount()); |
| 83 | + |
| 84 | + // Wait for more than the fsync interval to allow the scheduled task to run |
| 85 | + waitFor(700); |
| 86 | + assertEquals(1, dir.getSyncCount()); |
| 87 | + // Meta sync should still be 0 |
| 88 | + assertEquals(0, dir.getMetaSyncCount()); |
| 89 | + |
| 90 | + delayedDir.close(); |
| 91 | + |
| 92 | + // No additional syncs should have occurred after close |
| 93 | + assertEquals(1, dir.getSyncCount()); |
| 94 | + assertEquals(0, dir.getMetaSyncCount()); |
| 95 | + assertEquals(1, dir.getCloseCount()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testSyncMetaData() throws IOException { |
| 100 | + final var dir = new TrackingDirectory(new RAMDirectory()); |
| 101 | + final var delayedDir = new DelayedSyncDirectoryWrapper(dir, 500); |
| 102 | + |
| 103 | + assertEquals(0, dir.getSyncCount()); |
| 104 | + assertEquals(0, dir.getMetaSyncCount()); |
| 105 | + |
| 106 | + delayedDir.syncMetaData(); |
| 107 | + |
| 108 | + // The meta sync should be delayed, so the count should still be 0 |
| 109 | + assertEquals(0, dir.getMetaSyncCount()); |
| 110 | + |
| 111 | + // Wait for more than the fsync interval to allow the scheduled task to run |
| 112 | + waitFor(700); |
| 113 | + assertEquals(1, dir.getMetaSyncCount()); |
| 114 | + // Regular sync should still be 0 |
| 115 | + assertEquals(0, dir.getSyncCount()); |
| 116 | + |
| 117 | + delayedDir.close(); |
| 118 | + |
| 119 | + // No additional syncs should have occurred after close |
| 120 | + assertEquals(0, dir.getSyncCount()); |
| 121 | + assertEquals(1, dir.getMetaSyncCount()); |
| 122 | + assertEquals(1, dir.getCloseCount()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testSyncMixed() throws IOException { |
| 127 | + final var dir = new TrackingDirectory(new RAMDirectory()); |
| 128 | + final var delayedDir = new DelayedSyncDirectoryWrapper(dir, 500); |
| 129 | + |
| 130 | + assertEquals(0, dir.getSyncCount()); |
| 131 | + assertEquals(0, dir.getMetaSyncCount()); |
| 132 | + |
| 133 | + delayedDir.sync(List.of("file1", "file2")); |
| 134 | + delayedDir.sync(List.of("file2", "file456")); |
| 135 | + delayedDir.syncMetaData(); |
| 136 | + delayedDir.syncMetaData(); |
| 137 | + delayedDir.syncMetaData(); |
| 138 | + |
| 139 | + // The syncs should be delayed, so the counts should still be 0 |
| 140 | + assertEquals(0, dir.getSyncCount()); |
| 141 | + assertEquals(0, dir.getMetaSyncCount()); |
| 142 | + |
| 143 | + // Wait for more than the fsync interval to allow the scheduled task to run |
| 144 | + waitFor(700); |
| 145 | + assertEquals(1, dir.getSyncCount()); |
| 146 | + assertEquals(1, dir.getMetaSyncCount()); |
| 147 | + |
| 148 | + delayedDir.sync(List.of("file2", "file456")); |
| 149 | + delayedDir.syncMetaData(); |
| 150 | + |
| 151 | + waitFor(700); |
| 152 | + assertEquals(2, dir.getSyncCount()); |
| 153 | + assertEquals(2, dir.getMetaSyncCount()); |
| 154 | + |
| 155 | + // Wait again to ensure no extra syncs occur |
| 156 | + waitFor(700); |
| 157 | + assertEquals(2, dir.getSyncCount()); |
| 158 | + assertEquals(2, dir.getMetaSyncCount()); |
| 159 | + |
| 160 | + delayedDir.close(); |
| 161 | + |
| 162 | + // No additional syncs should have occurred after close |
| 163 | + assertEquals(2, dir.getSyncCount()); |
| 164 | + assertEquals(2, dir.getMetaSyncCount()); |
| 165 | + assertEquals(1, dir.getCloseCount()); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void testSyncMixed_afterClose() throws IOException { |
| 170 | + final var dir = new TrackingDirectory(new RAMDirectory()); |
| 171 | + final var delayedDir = new DelayedSyncDirectoryWrapper(dir, 500); |
| 172 | + |
| 173 | + assertEquals(0, dir.getSyncCount()); |
| 174 | + assertEquals(0, dir.getMetaSyncCount()); |
| 175 | + |
| 176 | + delayedDir.sync(List.of("file1", "file2")); |
| 177 | + delayedDir.sync(List.of("file2", "file456")); |
| 178 | + delayedDir.syncMetaData(); |
| 179 | + delayedDir.syncMetaData(); |
| 180 | + delayedDir.syncMetaData(); |
| 181 | + |
| 182 | + assertEquals(0, dir.getSyncCount()); |
| 183 | + assertEquals(0, dir.getMetaSyncCount()); |
| 184 | + |
| 185 | + delayedDir.close(); |
| 186 | + |
| 187 | + // The syncs should be executed on close |
| 188 | + assertEquals(1, dir.getSyncCount()); |
| 189 | + assertEquals(1, dir.getMetaSyncCount()); |
| 190 | + assertEquals(1, dir.getCloseCount()); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void testCloseOnIndexShutDown() throws IOException { |
| 195 | + final var dir = new TrackingDirectory(new RAMDirectory()); |
| 196 | + final var delayedDir = new DelayedSyncDirectoryWrapper(dir, 500); |
| 197 | + final var index = new LuceneIndex(delayedDir, new StandardAnalyzer()); |
| 198 | + assertEquals(0, dir.getCloseCount()); |
| 199 | + |
| 200 | + index.shutDown(); |
| 201 | + |
| 202 | + assertEquals(1, dir.getCloseCount()); |
| 203 | + } |
| 204 | + |
| 205 | + private void waitFor(long millis) { |
| 206 | + try { |
| 207 | + Thread.sleep(millis); |
| 208 | + } catch (InterruptedException e) { |
| 209 | + Thread.currentThread().interrupt(); |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments