Search in sources :

Example 1 with Lifespan

use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.

the class CountsRotationTest method shouldCreateEmptyCountsTrackerStoreWhenCreatingDatabase.

@Test
public void shouldCreateEmptyCountsTrackerStoreWhenCreatingDatabase() throws IOException {
    // GIVEN
    GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.newGraphDatabase();
    // WHEN
    db.shutdown();
    // THEN
    assertTrue(fs.fileExists(alphaStoreFile()));
    assertFalse(fs.fileExists(betaStoreFile()));
    try (Lifespan life = new Lifespan()) {
        CountsTracker store = life.add(createCountsTracker(pageCache));
        assertEquals(BASE_TX_ID, store.txId());
        assertEquals(INITIAL_MINOR_VERSION, store.minorVersion());
        assertEquals(0, store.totalEntriesStored());
        assertEquals(0, allRecords(store).size());
    }
    try (Lifespan life = new Lifespan()) {
        CountsTracker store = life.add(createCountsTracker(pageCache));
        assertEquals(BASE_TX_ID, store.txId());
        assertEquals(INITIAL_MINOR_VERSION, store.minorVersion());
        assertEquals(0, store.totalEntriesStored());
        assertEquals(0, allRecords(store).size());
    }
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Test(org.junit.Test)

Example 2 with Lifespan

use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.

the class CountsRotationTest method shouldRotateCountsStoreWhenRotatingLog.

@Test
public void shouldRotateCountsStoreWhenRotatingLog() throws IOException {
    // GIVEN
    GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.newGraphDatabase();
    // WHEN doing a transaction (actually two, the label-mini-tx also counts)
    try (Transaction tx = db.beginTx()) {
        db.createNode(B);
        tx.success();
    }
    // and rotating the log (which implies flushing)
    checkPoint(db);
    // and creating another node after it
    try (Transaction tx = db.beginTx()) {
        db.createNode(C);
        tx.success();
    }
    // THEN
    assertTrue(fs.fileExists(alphaStoreFile()));
    assertTrue(fs.fileExists(betaStoreFile()));
    final PageCache pageCache = db.getDependencyResolver().resolveDependency(PageCache.class);
    try (Lifespan life = new Lifespan()) {
        CountsTracker store = life.add(createCountsTracker(pageCache));
        // NOTE since the rotation happens before the second transaction is committed we do not see those changes
        // in the stats
        // a transaction for creating the label and a transaction for the node
        assertEquals(BASE_TX_ID + 1 + 1, store.txId());
        assertEquals(INITIAL_MINOR_VERSION, store.minorVersion());
        // one for all nodes and one for the created "B" label
        assertEquals(1 + 1, store.totalEntriesStored());
        assertEquals(1 + 1, allRecords(store).size());
    }
    // on the other hand the tracker should read the correct value by merging data on disk and data in memory
    final CountsTracker tracker = db.getDependencyResolver().resolveDependency(RecordStorageEngine.class).testAccessNeoStores().getCounts();
    assertEquals(1 + 1, tracker.nodeCount(-1, newDoubleLongRegister()).readSecond());
    final LabelTokenHolder holder = db.getDependencyResolver().resolveDependency(LabelTokenHolder.class);
    int labelId = holder.getIdByName(C.name());
    assertEquals(1, tracker.nodeCount(labelId, newDoubleLongRegister()).readSecond());
    db.shutdown();
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) LabelTokenHolder(org.neo4j.kernel.impl.core.LabelTokenHolder) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Example 3 with Lifespan

use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.

the class CountsTrackerTest method shouldStoreCounts.

@Test
public void shouldStoreCounts() throws Exception {
    // given
    CountsOracle oracle = someData();
    // when
    try (Lifespan life = new Lifespan()) {
        CountsTracker tracker = life.add(newTracker());
        oracle.update(tracker, 2);
        tracker.rotate(2);
    }
    // then
    try (Lifespan life = new Lifespan()) {
        oracle.verify(life.add(newTracker()));
    }
}
Also used : CountsOracle(org.neo4j.kernel.impl.store.CountsOracle) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Test(org.junit.Test)

Example 4 with Lifespan

use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.

the class AbstractKeyValueStoreTest method shouldPickTheUncorruptedStoreWhenTruncatingAfterTheHeader.

@Test
public void shouldPickTheUncorruptedStoreWhenTruncatingAfterTheHeader() throws IOException {
    /*
         * The problem was that if we were succesfull in writing the header but failing immediately after, we would
         *  read 0 as counter for entry data and pick the corrupted store thinking that it was simply empty.
         */
    Store store = createTestStore();
    Pair<File, KeyValueStoreFile> file = store.rotationStrategy.create(EMPTY_DATA_PROVIDER, 1);
    Pair<File, KeyValueStoreFile> next = store.rotationStrategy.next(file.first(), Headers.headersBuilder().put(TX_ID, (long) 42).headers(), data(new Entry() {

        @Override
        public void write(WritableBuffer key, WritableBuffer value) {
            key.putByte(0, (byte) 'f');
            key.putByte(1, (byte) 'o');
            key.putByte(2, (byte) 'o');
            value.putInt(0, 42);
        }
    }));
    file.other().close();
    File correct = next.first();
    Pair<File, KeyValueStoreFile> nextNext = store.rotationStrategy.next(correct, Headers.headersBuilder().put(TX_ID, (long) 43).headers(), data(new Entry() {

        @Override
        public void write(WritableBuffer key, WritableBuffer value) {
            key.putByte(0, (byte) 'f');
            key.putByte(1, (byte) 'o');
            key.putByte(2, (byte) 'o');
            value.putInt(0, 42);
        }
    }, new Entry() {

        @Override
        public void write(WritableBuffer key, WritableBuffer value) {
            key.putByte(0, (byte) 'b');
            key.putByte(1, (byte) 'a');
            key.putByte(2, (byte) 'r');
            value.putInt(0, 4242);
        }
    }));
    next.other().close();
    File corrupted = nextNext.first();
    nextNext.other().close();
    try (StoreChannel channel = resourceManager.fileSystem().open(corrupted, "rw")) {
        channel.truncate(16 * 4);
    }
    // then
    try (Lifespan life = new Lifespan()) {
        life.add(store);
        assertNotNull(store.get("foo"));
        assertEquals(42L, store.headers().get(TX_ID).longValue());
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) File(java.io.File) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Test(org.junit.Test)

Example 5 with Lifespan

use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.

the class AbstractKeyValueStoreTest method shouldPickFileWithGreatestTransactionId.

@Test
public void shouldPickFileWithGreatestTransactionId() throws Exception {
    try (Lifespan life = new Lifespan()) {
        Store store = life.add(createTestStore());
        // when
        for (long txId = 2; txId <= 10; txId++) {
            store.updater(txId).get().close();
            store.prepareRotation(txId).rotate();
        }
    }
    // then
    try (Lifespan life = new Lifespan()) {
        Store store = life.add(createTestStore());
        assertEquals(10L, store.headers().get(TX_ID).longValue());
    }
}
Also used : TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Test(org.junit.Test)

Aggregations

Lifespan (org.neo4j.kernel.lifecycle.Lifespan)53 Test (org.junit.jupiter.api.Test)21 Test (org.junit.Test)16 LogFiles (org.neo4j.kernel.impl.transaction.log.files.LogFiles)8 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)8 Path (java.nio.file.Path)7 SimpleTransactionIdStore (org.neo4j.kernel.impl.transaction.SimpleTransactionIdStore)7 LogPosition (org.neo4j.kernel.impl.transaction.log.LogPosition)7 LogFile (org.neo4j.kernel.impl.transaction.log.files.LogFile)7 JobScheduler (org.neo4j.scheduler.JobScheduler)7 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)6 File (java.io.File)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 StoreFileChannel (org.neo4j.io.fs.StoreFileChannel)5 PageCache (org.neo4j.io.pagecache.PageCache)5 SimpleLogVersionRepository (org.neo4j.kernel.impl.transaction.SimpleLogVersionRepository)5 IOException (java.io.IOException)4 StoreChannel (org.neo4j.io.fs.StoreChannel)4 NodeStore (org.neo4j.kernel.impl.store.NodeStore)4 ArrayList (java.util.ArrayList)3