Search in sources :

Example 1 with RandomAdversary

use of org.neo4j.adversaries.RandomAdversary in project neo4j by neo4j.

the class SingleFilePageSwapperTest method mustHandleMischiefInPositionedRead.

@Test
public void mustHandleMischiefInPositionedRead() throws Exception {
    int bytesTotal = 512;
    byte[] data = new byte[bytesTotal];
    ThreadLocalRandom.current().nextBytes(data);
    PageSwapperFactory factory = createSwapperFactory();
    factory.setFileSystemAbstraction(getFs());
    File file = getFile();
    PageSwapper swapper = createSwapper(factory, file, bytesTotal, NO_CALLBACK, true);
    try {
        swapper.write(0, new ByteBufferPage(wrap(data)));
    } finally {
        swapper.close();
    }
    RandomAdversary adversary = new RandomAdversary(0.5, 0.0, 0.0);
    factory.setFileSystemAbstraction(new AdversarialFileSystemAbstraction(adversary, getFs()));
    swapper = createSwapper(factory, file, bytesTotal, NO_CALLBACK, false);
    ByteBufferPage page = createPage(bytesTotal);
    try {
        for (int i = 0; i < 10_000; i++) {
            clear(page);
            assertThat(swapper.read(0, page), is((long) bytesTotal));
            assertThat(array(page.buffer), is(data));
        }
    } finally {
        swapper.close();
    }
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) PageSwapper(org.neo4j.io.pagecache.PageSwapper) File(java.io.File) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) RandomAdversary(org.neo4j.adversaries.RandomAdversary) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Example 2 with RandomAdversary

use of org.neo4j.adversaries.RandomAdversary in project neo4j by neo4j.

the class SingleFilePageSwapperTest method mustHandleMischiefInPositionedWrite.

@Test
public void mustHandleMischiefInPositionedWrite() throws Exception {
    int bytesTotal = 512;
    byte[] data = new byte[bytesTotal];
    ThreadLocalRandom.current().nextBytes(data);
    ByteBufferPage zeroPage = createPage(bytesTotal);
    clear(zeroPage);
    File file = getFile();
    PageSwapperFactory factory = createSwapperFactory();
    RandomAdversary adversary = new RandomAdversary(0.5, 0.0, 0.0);
    factory.setFileSystemAbstraction(new AdversarialFileSystemAbstraction(adversary, getFs()));
    PageSwapper swapper = createSwapper(factory, file, bytesTotal, NO_CALLBACK, true);
    ByteBufferPage page = createPage(bytesTotal);
    try {
        for (int i = 0; i < 10_000; i++) {
            adversary.setProbabilityFactor(0);
            swapper.write(0, zeroPage);
            page.putBytes(data, 0, 0, data.length);
            adversary.setProbabilityFactor(1);
            assertThat(swapper.write(0, page), is((long) bytesTotal));
            clear(page);
            adversary.setProbabilityFactor(0);
            swapper.read(0, page);
            assertThat(array(page.buffer), is(data));
        }
    } finally {
        swapper.close();
    }
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) PageSwapper(org.neo4j.io.pagecache.PageSwapper) File(java.io.File) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) RandomAdversary(org.neo4j.adversaries.RandomAdversary) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Example 3 with RandomAdversary

use of org.neo4j.adversaries.RandomAdversary in project neo4j by neo4j.

the class SingleFilePageSwapperTest method mustHandleMischiefInPositionedVectoredRead.

@Test
public void mustHandleMischiefInPositionedVectoredRead() throws Exception {
    int bytesTotal = 512;
    int bytesPerPage = 32;
    int pageCount = bytesTotal / bytesPerPage;
    byte[] data = new byte[bytesTotal];
    ThreadLocalRandom.current().nextBytes(data);
    PageSwapperFactory factory = createSwapperFactory();
    factory.setFileSystemAbstraction(getFs());
    File file = getFile();
    PageSwapper swapper = createSwapper(factory, file, bytesTotal, NO_CALLBACK, true);
    try {
        swapper.write(0, new ByteBufferPage(wrap(data)));
    } finally {
        swapper.close();
    }
    RandomAdversary adversary = new RandomAdversary(0.5, 0.0, 0.0);
    factory.setFileSystemAbstraction(new AdversarialFileSystemAbstraction(adversary, getFs()));
    swapper = createSwapper(factory, file, bytesPerPage, NO_CALLBACK, false);
    ByteBufferPage[] pages = new ByteBufferPage[pageCount];
    for (int i = 0; i < pageCount; i++) {
        pages[i] = createPage(bytesPerPage);
    }
    byte[] temp = new byte[bytesPerPage];
    try {
        for (int i = 0; i < 10_000; i++) {
            for (ByteBufferPage page : pages) {
                clear(page);
            }
            assertThat(swapper.read(0, pages, 0, pages.length), is((long) bytesTotal));
            for (int j = 0; j < pageCount; j++) {
                System.arraycopy(data, j * bytesPerPage, temp, 0, bytesPerPage);
                assertThat(array(pages[j].buffer), is(temp));
            }
        }
    } finally {
        swapper.close();
    }
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) PageSwapper(org.neo4j.io.pagecache.PageSwapper) File(java.io.File) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) RandomAdversary(org.neo4j.adversaries.RandomAdversary) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Example 4 with RandomAdversary

use of org.neo4j.adversaries.RandomAdversary in project neo4j by neo4j.

the class PageCacheTest method readableByteChannelMustReadAllBytesInFileConsistently.

@RepeatRule.Repeat(times = 20)
@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void readableByteChannelMustReadAllBytesInFileConsistently() throws Exception {
    File file = file("a");
    generateFileWithRecords(file, recordCount, recordSize);
    configureStandardPageCache();
    try (PagedFile pf = pageCache.map(file, filePageSize)) {
        RandomAdversary adversary = new RandomAdversary(0.9, 0, 0);
        AdversarialPagedFile apf = new AdversarialPagedFile(pf, adversary);
        try (ReadableByteChannel channel = apf.openReadableByteChannel()) {
            verifyRecordsInFile(channel, recordCount);
        }
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) File(java.io.File) RandomAdversary(org.neo4j.adversaries.RandomAdversary) Test(org.junit.Test)

Example 5 with RandomAdversary

use of org.neo4j.adversaries.RandomAdversary in project neo4j by neo4j.

the class RotatingFileOutputStreamSupplierTest method shouldSurviveFilesystemErrors.

@Test
public void shouldSurviveFilesystemErrors() throws Exception {
    final RandomAdversary adversary = new RandomAdversary(0.1, 0.1, 0);
    adversary.setProbabilityFactor(0);
    AdversarialFileSystemAbstraction adversarialFileSystem = new SensibleAdversarialFileSystemAbstraction(adversary, fileSystem);
    RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier(adversarialFileSystem, logFile, 1000, 0, 9, DIRECT_EXECUTOR);
    adversary.setProbabilityFactor(1);
    writeLines(supplier, 10000);
    // run cleanly for a while, to allow it to fill any gaps left in log archive numbers
    adversary.setProbabilityFactor(0);
    writeLines(supplier, 10000);
    assertThat(fileSystem.fileExists(logFile), is(true));
    assertThat(fileSystem.fileExists(archiveLogFile1), is(true));
    assertThat(fileSystem.fileExists(archiveLogFile2), is(true));
    assertThat(fileSystem.fileExists(archiveLogFile3), is(true));
    assertThat(fileSystem.fileExists(archiveLogFile4), is(true));
    assertThat(fileSystem.fileExists(archiveLogFile5), is(true));
    assertThat(fileSystem.fileExists(archiveLogFile6), is(true));
    assertThat(fileSystem.fileExists(archiveLogFile7), is(true));
    assertThat(fileSystem.fileExists(archiveLogFile8), is(true));
    assertThat(fileSystem.fileExists(archiveLogFile9), is(true));
}
Also used : AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) RandomAdversary(org.neo4j.adversaries.RandomAdversary) Test(org.junit.Test)

Aggregations

RandomAdversary (org.neo4j.adversaries.RandomAdversary)9 Test (org.junit.Test)8 AdversarialFileSystemAbstraction (org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction)8 File (java.io.File)7 PageSwapperFactory (org.neo4j.io.pagecache.PageSwapperFactory)5 PageSwapper (org.neo4j.io.pagecache.PageSwapper)4 PageSwapperTest (org.neo4j.io.pagecache.PageSwapperTest)4 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)2 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 HashMap (java.util.HashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 TimeoutException (java.util.concurrent.TimeoutException)1 Adversary (org.neo4j.adversaries.Adversary)1 AdversarialPagedFile (org.neo4j.adversaries.pagecache.AdversarialPagedFile)1 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)1 StoreChannel (org.neo4j.io.fs.StoreChannel)1 PagedFile (org.neo4j.io.pagecache.PagedFile)1 SingleFilePageSwapperFactory (org.neo4j.io.pagecache.impl.SingleFilePageSwapperFactory)1