use of org.neo4j.io.fs.DelegatingStoreChannel in project neo4j by neo4j.
the class MuninnPageCacheTest method mustUnblockPageFaultersWhenEvictionGetsException.
@Test
void mustUnblockPageFaultersWhenEvictionGetsException() {
assertTimeoutPreemptively(ofMillis(SEMI_LONG_TIMEOUT_MILLIS), () -> {
writeInitialDataTo(file("a"));
MutableBoolean throwException = new MutableBoolean(true);
FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(this.fs) {
@Override
public StoreChannel open(Path fileName, Set<OpenOption> options) throws IOException {
return new DelegatingStoreChannel(super.open(fileName, options)) {
@Override
public void writeAll(ByteBuffer src, long position) throws IOException {
if (throwException.booleanValue()) {
throw new IOException("uh-oh...");
} else {
super.writeAll(src, position);
}
}
};
}
};
try (MuninnPageCache pageCache = createPageCache(fs, 2, PageCacheTracer.NULL);
PagedFile pagedFile = map(pageCache, file("a"), 8)) {
// exceptions on all writes.
try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK, NULL)) {
for (int i = 0; i < 1000; i++) {
assertTrue(cursor.next());
}
fail("Expected an exception at this point");
} catch (IOException ignore) {
// Good.
}
throwException.setFalse();
}
});
}
use of org.neo4j.io.fs.DelegatingStoreChannel in project neo4j by neo4j.
the class PageCacheTest method flushingDuringPagedFileCloseMustRetryUntilItSucceeds.
@RepeatedTest(100)
void flushingDuringPagedFileCloseMustRetryUntilItSucceeds() {
assertTimeoutPreemptively(ofMillis(SHORT_TIMEOUT_MILLIS), () -> {
FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(this.fs) {
@Override
public StoreChannel open(Path fileName, Set<OpenOption> options) throws IOException {
return new DelegatingStoreChannel(super.open(fileName, options)) {
private int writeCount;
@Override
public void writeAll(ByteBuffer src, long position) throws IOException {
if (writeCount++ < 10) {
throw new IOException("This is a benign exception that we expect to be thrown " + "during a flush of a PagedFile.");
}
super.writeAll(src, position);
}
};
}
};
getPageCache(fs, maxPages, PageCacheTracer.NULL);
PrintStream oldSystemErr = System.err;
try (PagedFile pf = map(file("a"), filePageSize);
PageCursor cursor = pf.io(0, PF_SHARED_WRITE_LOCK, NULL)) {
assertTrue(cursor.next());
writeRecords(cursor);
// Silence any stack traces the failed flushes might print.
System.setErr(new PrintStream(new ByteArrayOutputStream()));
} finally {
System.setErr(oldSystemErr);
}
verifyRecordsInFile(file("a"), recordsPerFilePage);
});
}
use of org.neo4j.io.fs.DelegatingStoreChannel in project neo4j by neo4j.
the class MuninnPageCacheTest method pageCacheFlushAndForceMustClearBackgroundEvictionException.
@Test
void pageCacheFlushAndForceMustClearBackgroundEvictionException() {
assertTimeoutPreemptively(ofMillis(SEMI_LONG_TIMEOUT_MILLIS), () -> {
MutableBoolean throwException = new MutableBoolean(true);
FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(this.fs) {
@Override
public StoreChannel open(Path fileName, Set<OpenOption> options) throws IOException {
return new DelegatingStoreChannel(super.open(fileName, options)) {
@Override
public void writeAll(ByteBuffer src, long position) throws IOException {
if (throwException.booleanValue()) {
throw new IOException("uh-oh...");
} else {
super.writeAll(src, position);
}
}
};
}
};
try (MuninnPageCache pageCache = createPageCache(fs, 2, PageCacheTracer.NULL);
PagedFile pagedFile = map(pageCache, file("a"), 8)) {
try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK, NULL)) {
// Page 0 is now dirty, but flushing it will throw an exception.
assertTrue(cursor.next());
}
// This will run into that exception, in background eviction:
pageCache.evictPages(1, 0, EvictionRunEvent.NULL);
// We now have a background eviction exception. A successful flushAndForce should clear it, though.
throwException.setFalse();
pageCache.flushAndForce();
// And with a cleared exception, we should be able to work with the page cache without worry.
try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK, NULL)) {
for (int i = 0; i < maxPages * 20; i++) {
assertTrue(cursor.next());
}
}
}
});
}
use of org.neo4j.io.fs.DelegatingStoreChannel in project neo4j by neo4j.
the class FileLockerTest method shouldNotObtainLockWhenAlreadyInUse.
@ParameterizedTest
@MethodSource("lockerFactories")
void shouldNotObtainLockWhenAlreadyInUse(LockerFactory lockerFactory) {
FileSystemAbstraction fileSystemAbstraction = new DelegatingFileSystemAbstraction(fileSystem) {
@Override
public boolean fileExists(Path file) {
return false;
}
@Override
public StoreChannel write(Path fileName) throws IOException {
return new DelegatingStoreChannel(super.write(fileName)) {
@Override
public FileLock tryLock() {
// 'null' implies that the file has been externally locked
return null;
}
};
}
};
FileLockException fileLockException = assertThrows(FileLockException.class, () -> {
try (Locker storeLocker = lockerFactory.createLocker(fileSystemAbstraction, testDirectory)) {
storeLocker.checkLock();
}
});
assertThat(fileLockException.getMessage()).contains("Lock file has been locked by another process");
}
use of org.neo4j.io.fs.DelegatingStoreChannel in project neo4j by neo4j.
the class SingleFilePageSwapperTest method mustCloseFilesIfTakingFileLockThrows.
@Test
@DisabledOnOs(OS.WINDOWS)
void mustCloseFilesIfTakingFileLockThrows() throws Exception {
final AtomicInteger openFilesCounter = new AtomicInteger();
PageSwapperFactory factory = createSwapperFactory(new DelegatingFileSystemAbstraction(fileSystem) {
@Override
public StoreChannel open(Path fileName, Set<OpenOption> options) throws IOException {
openFilesCounter.getAndIncrement();
return new DelegatingStoreChannel(super.open(fileName, options)) {
@Override
public void close() throws IOException {
openFilesCounter.getAndDecrement();
super.close();
}
};
}
});
Path file = testDir.file("file");
try (StoreChannel ch = fileSystem.write(file);
FileLock ignore = ch.tryLock()) {
createSwapper(factory, file, 4, NO_CALLBACK, false).close();
fail("Creating a page swapper for a locked channel should have thrown");
} catch (FileLockException e) {
// As expected.
}
assertThat(openFilesCounter.get()).isEqualTo(0);
}
Aggregations