Search in sources :

Example 26 with Race

use of org.neo4j.test.Race in project neo4j by neo4j.

the class MetaDataStoreTest method setUpgradeTransactionMustBeAtomic.

@Test
public void setUpgradeTransactionMustBeAtomic() throws Throwable {
    try (MetaDataStore store = newMetaDataStore()) {
        PagedFile pf = store.storeFile;
        store.setUpgradeTransaction(0, 0, 0);
        AtomicLong writeCount = new AtomicLong();
        AtomicLong fileReadCount = new AtomicLong();
        AtomicLong apiReadCount = new AtomicLong();
        int upperLimit = 10_000;
        int lowerLimit = 100;
        long endTime = currentTimeMillis() + SECONDS.toMillis(10);
        Race race = new Race();
        race.withEndCondition(() -> writeCount.get() >= upperLimit && fileReadCount.get() >= upperLimit && apiReadCount.get() >= upperLimit);
        race.withEndCondition(() -> writeCount.get() >= lowerLimit && fileReadCount.get() >= lowerLimit && apiReadCount.get() >= lowerLimit && currentTimeMillis() >= endTime);
        // writers
        race.addContestants(3, () -> {
            long count = writeCount.incrementAndGet();
            store.setUpgradeTransaction(count, count, count);
        });
        // file readers
        race.addContestants(3, throwing(() -> {
            try (PageCursor cursor = pf.io(0, PagedFile.PF_SHARED_READ_LOCK)) {
                assertTrue(cursor.next());
                long id, checksum;
                do {
                    id = store.getRecordValue(cursor, MetaDataStore.Position.UPGRADE_TRANSACTION_ID);
                    checksum = store.getRecordValue(cursor, MetaDataStore.Position.UPGRADE_TRANSACTION_CHECKSUM);
                } while (cursor.shouldRetry());
                assertIdEqualsChecksum(id, checksum, "file");
                fileReadCount.incrementAndGet();
            }
        }));
        race.addContestants(3, () -> {
            TransactionId transaction = store.getUpgradeTransaction();
            assertIdEqualsChecksum(transaction.transactionId(), transaction.checksum(), "API");
            apiReadCount.incrementAndGet();
        });
        race.go();
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) PagedFile(org.neo4j.io.pagecache.PagedFile) DelegatingPagedFile(org.neo4j.io.pagecache.DelegatingPagedFile) Race(org.neo4j.test.Race) PageCursor(org.neo4j.io.pagecache.PageCursor) DelegatingPageCursor(org.neo4j.io.pagecache.impl.DelegatingPageCursor) Test(org.junit.Test)

Aggregations

Race (org.neo4j.test.Race)26 Test (org.junit.Test)24 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Transaction (org.neo4j.graphdb.Transaction)5 Node (org.neo4j.graphdb.Node)4 NotFoundException (org.neo4j.graphdb.NotFoundException)4 DelegatingPagedFile (org.neo4j.io.pagecache.DelegatingPagedFile)3 IOException (java.io.IOException)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Relationship (org.neo4j.graphdb.Relationship)2 PageCursor (org.neo4j.io.pagecache.PageCursor)2 PagedFile (org.neo4j.io.pagecache.PagedFile)2 DelegatingPageCursor (org.neo4j.io.pagecache.impl.DelegatingPageCursor)2 File (java.io.File)1 Clock (java.time.Clock)1 ArrayList (java.util.ArrayList)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1