use of org.neo4j.io.pagecache.PageSwapperFactory in project neo4j by neo4j.
the class SingleFilePageSwapperTest method reportExternalIoOnSwapInWithLength.
@Test
void reportExternalIoOnSwapInWithLength() throws IOException {
byte[] bytes = new byte[] { 1, 2, 3, 4 };
try (StoreChannel channel = getFs().write(getPath())) {
channel.writeAll(wrap(bytes));
}
PageSwapperFactory factory = createSwapperFactory(getFs());
CountingIOController controller = new CountingIOController();
try (var swapper = createSwapper(factory, getPath(), 4, null, false, false, true, controller)) {
long target = createPage(4);
int numberOfReads = 12;
for (int i = 0; i < numberOfReads; i++) {
assertEquals(4, swapper.read(0, target, bytes.length));
}
assertEquals(numberOfReads, controller.getExternalIOCounter());
}
}
use of org.neo4j.io.pagecache.PageSwapperFactory 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);
}
use of org.neo4j.io.pagecache.PageSwapperFactory in project neo4j by neo4j.
the class SingleFilePageSwapperTest method swappingInMustFillPageWithData.
@Test
void swappingInMustFillPageWithData() throws Exception {
byte[] bytes = new byte[] { 1, 2, 3, 4 };
StoreChannel channel = getFs().write(getPath());
channel.writeAll(wrap(bytes));
channel.close();
PageSwapperFactory factory = createSwapperFactory(getFs());
PageSwapper swapper = createSwapper(factory, getPath(), 4, null, false);
long target = createPage(4);
assertEquals(4, swapper.read(0, target));
assertThat(array(target)).containsExactly(bytes);
}
use of org.neo4j.io.pagecache.PageSwapperFactory in project neo4j by neo4j.
the class SingleFilePageSwapperTest method swappingOutMustNotOverwriteDataBeyondPage.
@Test
void swappingOutMustNotOverwriteDataBeyondPage() throws Exception {
byte[] initialData = new byte[] { // --- page 0:
1, 2, 3, 4, // --- page 1:
5, 6, 7, 8, // --- page 2:
9, 10 };
byte[] finalData = new byte[] { // --- page 0:
1, 2, 3, 4, // --- page 1:
8, 7, 6, 5, // --- page 2:
9, 10 };
StoreChannel channel = getFs().write(getPath());
channel.writeAll(wrap(initialData));
channel.close();
byte[] change = new byte[] { 8, 7, 6, 5 };
long page = createPage(change);
PageSwapperFactory factory = createSwapperFactory(getFs());
PageSwapper swapper = createSwapper(factory, getPath(), 4, null, false);
swapper.write(1, page);
try (InputStream stream = getFs().openAsInputStream(getPath())) {
byte[] actual = new byte[(int) getFs().getFileSize(getPath())];
assertThat(stream.read(actual)).isEqualTo(actual.length);
assertThat(actual).containsExactly(finalData);
}
}
use of org.neo4j.io.pagecache.PageSwapperFactory in project neo4j by neo4j.
the class SingleFilePageSwapperTest method mustHandleMischiefInPositionedVectoredWrite.
@Test
void mustHandleMischiefInPositionedVectoredWrite() throws Exception {
int bytesTotal = 512;
int bytesPerPage = 32;
int pageCount = bytesTotal / bytesPerPage;
byte[] data = new byte[bytesTotal];
ThreadLocalRandom.current().nextBytes(data);
long zeroPage = createPage(bytesPerPage);
clear(zeroPage);
Path file = getPath();
RandomAdversary adversary = new RandomAdversary(0.5, 0.0, 0.0);
PageSwapperFactory factory = createSwapperFactory(new AdversarialFileSystemAbstraction(adversary, getFs()));
PageSwapper swapper = createSwapper(factory, file, bytesPerPage, NO_CALLBACK, true);
long[] writePages = new long[pageCount];
long[] readPages = new long[pageCount];
long[] zeroPages = new long[pageCount];
int[] pageLengths = new int[pageCount];
for (int i = 0; i < pageCount; i++) {
writePages[i] = createPage(bytesPerPage);
putBytes(writePages[i], data, 0, i * bytesPerPage, bytesPerPage);
readPages[i] = createPage(bytesPerPage);
zeroPages[i] = zeroPage;
pageLengths[i] = bytesPerPage;
}
try {
for (int i = 0; i < 10_000; i++) {
adversary.setProbabilityFactor(0);
swapper.write(0, zeroPages, pageLengths, pageCount, pageCount);
adversary.setProbabilityFactor(1);
swapper.write(0, writePages, pageLengths, pageCount, pageCount);
for (long readPage : readPages) {
clear(readPage);
}
adversary.setProbabilityFactor(0);
assertThat(swapper.read(0, readPages, pageLengths, pageCount)).isEqualTo(bytesTotal);
for (int j = 0; j < pageCount; j++) {
assertThat(array(readPages[j])).containsExactly(array(writePages[j]));
}
}
} finally {
swapper.close();
}
}
Aggregations