Search in sources :

Example 1 with Adversary

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

the class FileSenderTest method sendLargeFileWithUnreliableReadBufferSize.

@Test
public void sendLargeFileWithUnreliableReadBufferSize() throws Exception {
    // given
    byte[] bytes = new byte[MAX_SIZE * 3];
    random.nextBytes(bytes);
    File smallFile = testDirectory.file("smallFile");
    try (StoreChannel storeChannel = fs.create(smallFile)) {
        storeChannel.write(ByteBuffer.wrap(bytes));
    }
    Adversary adversary = new RandomAdversary(0.9, 0.0, 0.0);
    AdversarialFileSystemAbstraction afs = new AdversarialFileSystemAbstraction(adversary, fs);
    FileSender fileSender = new FileSender(afs.open(smallFile, "r"));
    // when + then
    assertFalse(fileSender.isEndOfInput());
    assertEquals(FileChunk.create(copyOfRange(bytes, 0, MAX_SIZE), false), fileSender.readChunk(allocator));
    assertEquals(FileChunk.create(copyOfRange(bytes, MAX_SIZE, MAX_SIZE * 2), false), fileSender.readChunk(allocator));
    assertEquals(FileChunk.create(copyOfRange(bytes, MAX_SIZE * 2, bytes.length), true), fileSender.readChunk(allocator));
    assertNull(fileSender.readChunk(allocator));
    assertTrue(fileSender.isEndOfInput());
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) RandomAdversary(org.neo4j.adversaries.RandomAdversary) Adversary(org.neo4j.adversaries.Adversary) RandomAdversary(org.neo4j.adversaries.RandomAdversary) Test(org.junit.Test)

Example 2 with Adversary

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

the class BatchingTransactionAppenderConcurrencyTest method shouldHaveAllConcurrentAppendersSeePanic.

/*
     * There was an issue where if multiple concurrent appending threads did append and they moved on
     * to await a force, where the force would fail and the one doing the force would raise a panic...
     * the other threads may not notice the panic and move on to mark those transactions as committed
     * and notice the panic later (which would be too late).
     */
@Test
void shouldHaveAllConcurrentAppendersSeePanic() throws Throwable {
    // GIVEN
    Adversary adversary = new ClassGuardedAdversary(new CountingAdversary(1, true), failMethod(TransactionLogFile.class, "force"));
    EphemeralFileSystemAbstraction efs = new EphemeralFileSystemAbstraction();
    FileSystemAbstraction fs = new AdversarialFileSystemAbstraction(adversary, efs);
    life.add(new FileSystemLifecycleAdapter(fs));
    DatabaseHealth databaseHealth = new DatabaseHealth(mock(DatabasePanicEventGenerator.class), NullLog.getInstance());
    LogFiles logFiles = LogFilesBuilder.builder(databaseLayout, fs).withLogVersionRepository(logVersionRepository).withTransactionIdStore(transactionIdStore).withDatabaseHealth(databaseHealth).withLogEntryReader(new VersionAwareLogEntryReader(new TestCommandReaderFactory())).withStoreId(StoreId.UNKNOWN).build();
    life.add(logFiles);
    final BatchingTransactionAppender appender = life.add(new BatchingTransactionAppender(logFiles, logRotation, transactionMetadataCache, transactionIdStore, databaseHealth));
    life.start();
    // WHEN
    int numberOfAppenders = 10;
    final CountDownLatch trap = new CountDownLatch(numberOfAppenders);
    final LogAppendEvent beforeForceTrappingEvent = new LogAppendEvent.Empty() {

        @Override
        public LogForceWaitEvent beginLogForceWait() {
            trap.countDown();
            awaitLatch(trap);
            return super.beginLogForceWait();
        }
    };
    Race race = new Race();
    for (int i = 0; i < numberOfAppenders; i++) {
        race.addContestant(() -> {
            // Good, we know that this test uses an adversarial file system which will throw
            // an exception in LogFile#force, and since all these transactions
            // will append and be forced in the same batch, where the force will fail then
            // all these transactions should fail. If there's any transaction not failing then
            // it just didn't notice the panic, which would be potentially hazardous.
            assertThrows(IOException.class, () -> appender.append(tx(), beforeForceTrappingEvent));
        });
    }
    // THEN perform the race. The relevant assertions are made inside the contestants.
    race.go();
}
Also used : FileSystemLifecycleAdapter(org.neo4j.io.fs.FileSystemLifecycleAdapter) DatabaseHealth(org.neo4j.monitoring.DatabaseHealth) CountingAdversary(org.neo4j.adversaries.CountingAdversary) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) ClassGuardedAdversary(org.neo4j.adversaries.ClassGuardedAdversary) TransactionLogFile(org.neo4j.kernel.impl.transaction.log.files.TransactionLogFile) EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction) LogFiles(org.neo4j.kernel.impl.transaction.log.files.LogFiles) TransactionLogFiles(org.neo4j.kernel.impl.transaction.log.files.TransactionLogFiles) DatabasePanicEventGenerator(org.neo4j.kernel.monitoring.DatabasePanicEventGenerator) TestCommandReaderFactory(org.neo4j.kernel.impl.api.TestCommandReaderFactory) CountDownLatch(java.util.concurrent.CountDownLatch) LogAppendEvent(org.neo4j.kernel.impl.transaction.tracing.LogAppendEvent) Race(org.neo4j.test.Race) VersionAwareLogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) Adversary(org.neo4j.adversaries.Adversary) CountingAdversary(org.neo4j.adversaries.CountingAdversary) ClassGuardedAdversary(org.neo4j.adversaries.ClassGuardedAdversary) Test(org.junit.jupiter.api.Test)

Example 3 with Adversary

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

the class PageCacheRule method pageCachePostConstruct.

protected void pageCachePostConstruct(PageCacheConfig overriddenConfig) {
    if (selectConfig(baseConfig.inconsistentReads, overriddenConfig.inconsistentReads, TRUE)) {
        AtomicBoolean controller = selectConfig(baseConfig.nextReadIsInconsistent, overriddenConfig.nextReadIsInconsistent, null);
        Adversary adversary = controller != null ? new AtomicBooleanInconsistentReadAdversary(controller) : new RandomInconsistentReadAdversary();
        pageCache = new AdversarialPageCache(pageCache, adversary);
    }
    if (selectConfig(baseConfig.accessChecks, overriddenConfig.accessChecks, false)) {
        pageCache = new AccessCheckingPageCache(pageCache);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AccessCheckingPageCache(org.neo4j.io.pagecache.checking.AccessCheckingPageCache) AdversarialPageCache(org.neo4j.adversaries.pagecache.AdversarialPageCache) Adversary(org.neo4j.adversaries.Adversary)

Aggregations

Adversary (org.neo4j.adversaries.Adversary)3 AdversarialFileSystemAbstraction (org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction)2 File (java.io.File)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1 ClassGuardedAdversary (org.neo4j.adversaries.ClassGuardedAdversary)1 CountingAdversary (org.neo4j.adversaries.CountingAdversary)1 RandomAdversary (org.neo4j.adversaries.RandomAdversary)1 AdversarialPageCache (org.neo4j.adversaries.pagecache.AdversarialPageCache)1 EphemeralFileSystemAbstraction (org.neo4j.io.fs.EphemeralFileSystemAbstraction)1 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)1 FileSystemLifecycleAdapter (org.neo4j.io.fs.FileSystemLifecycleAdapter)1 StoreChannel (org.neo4j.io.fs.StoreChannel)1 AccessCheckingPageCache (org.neo4j.io.pagecache.checking.AccessCheckingPageCache)1 TestCommandReaderFactory (org.neo4j.kernel.impl.api.TestCommandReaderFactory)1 VersionAwareLogEntryReader (org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader)1 LogFiles (org.neo4j.kernel.impl.transaction.log.files.LogFiles)1 TransactionLogFile (org.neo4j.kernel.impl.transaction.log.files.TransactionLogFile)1