Search in sources :

Example 36 with Lifespan

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

the class CountsTrackerTest method shouldBeAbleToReadUpToDateValueWhileAnotherThreadIsPerformingRotation.

@Test
public void shouldBeAbleToReadUpToDateValueWhileAnotherThreadIsPerformingRotation() throws Exception {
    // given
    CountsOracle oracle = someData();
    final int firstTransaction = 2, secondTransaction = 3;
    try (Lifespan life = new Lifespan()) {
        CountsTracker tracker = life.add(newTracker());
        oracle.update(tracker, firstTransaction);
        tracker.rotate(firstTransaction);
    }
    // when
    final CountsOracle delta = new CountsOracle();
    {
        CountsOracle.Node n1 = delta.node(1);
        // Label 4 has not been used before...
        CountsOracle.Node n2 = delta.node(1, 4);
        delta.relationship(n1, 1, n2);
        // relationshipType 2 has not been used before...
        delta.relationship(n2, 2, n1);
    }
    delta.update(oracle);
    try (Lifespan life = new Lifespan()) {
        final Barrier.Control barrier = new Barrier.Control();
        CountsTracker tracker = life.add(new CountsTracker(resourceManager.logProvider(), resourceManager.fileSystem(), resourceManager.pageCache(), Config.empty(), resourceManager.testPath()) {

            @Override
            protected boolean include(CountsKey countsKey, ReadableBuffer value) {
                barrier.reached();
                return super.include(countsKey, value);
            }
        });
        Future<Void> task = threading.execute((ThrowingFunction<CountsTracker, Void, RuntimeException>) t -> {
            try {
                delta.update(t, secondTransaction);
                t.rotate(secondTransaction);
            } catch (IOException e) {
                throw new AssertionError(e);
            }
            return null;
        }, tracker);
        // then
        barrier.await();
        oracle.verify(tracker);
        barrier.release();
        task.get();
        oracle.verify(tracker);
    }
}
Also used : CountsKey(org.neo4j.kernel.impl.store.counts.keys.CountsKey) Registers(org.neo4j.register.Registers) Assert.assertSame(org.junit.Assert.assertSame) Future(java.util.concurrent.Future) FakeClock(org.neo4j.time.FakeClock) CountsVisitor(org.neo4j.kernel.impl.api.CountsVisitor) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) CountsKeyFactory(org.neo4j.kernel.impl.store.counts.keys.CountsKeyFactory) DebugUtil.classNameContains(org.neo4j.kernel.impl.util.DebugUtil.classNameContains) Assert.fail(org.junit.Assert.fail) ReadableBuffer(org.neo4j.kernel.impl.store.kvstore.ReadableBuffer) STARTED(org.neo4j.test.rule.Resources.InitialLifecycle.STARTED) Config(org.neo4j.kernel.configuration.Config) CountsOracle(org.neo4j.kernel.impl.store.CountsOracle) Barrier(org.neo4j.test.Barrier) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Predicate(java.util.function.Predicate) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Register(org.neo4j.register.Register) IOFunction(org.neo4j.function.IOFunction) ThrowingFunction(org.neo4j.function.ThrowingFunction) File(java.io.File) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) DataInitializer(org.neo4j.kernel.impl.store.kvstore.DataInitializer) FILE_IN_EXISTING_DIRECTORY(org.neo4j.test.rule.Resources.TestPath.FILE_IN_EXISTING_DIRECTORY) DebugUtil.stackTraceContains(org.neo4j.kernel.impl.util.DebugUtil.stackTraceContains) RotationTimeoutException(org.neo4j.kernel.impl.store.kvstore.RotationTimeoutException) Rule(org.junit.Rule) CountsAccessor(org.neo4j.kernel.impl.api.CountsAccessor) Resources(org.neo4j.test.rule.Resources) ThreadingRule(org.neo4j.test.rule.concurrent.ThreadingRule) Clock(java.time.Clock) DebugUtil.methodIs(org.neo4j.kernel.impl.util.DebugUtil.methodIs) GraphDatabaseSettings(org.neo4j.graphdb.factory.GraphDatabaseSettings) Clocks(org.neo4j.time.Clocks) Assert.assertEquals(org.junit.Assert.assertEquals) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Predicates.all(org.neo4j.function.Predicates.all) Mockito.mock(org.mockito.Mockito.mock) Barrier(org.neo4j.test.Barrier) CountsKey(org.neo4j.kernel.impl.store.counts.keys.CountsKey) IOException(java.io.IOException) ReadableBuffer(org.neo4j.kernel.impl.store.kvstore.ReadableBuffer) CountsOracle(org.neo4j.kernel.impl.store.CountsOracle) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Test(org.junit.Test)

Example 37 with Lifespan

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

the class CountsTrackerTest method shouldUpdateCountsOnExistingStore.

@Test
public void shouldUpdateCountsOnExistingStore() throws Exception {
    // given
    CountsOracle oracle = someData();
    int firstTx = 2, secondTx = 3;
    try (Lifespan life = new Lifespan()) {
        CountsTracker tracker = life.add(newTracker());
        oracle.update(tracker, firstTx);
        tracker.rotate(firstTx);
        oracle.verify(tracker);
        // when
        CountsOracle delta = new CountsOracle();
        {
            CountsOracle.Node n1 = delta.node(1);
            // Label 4 has not been used before...
            CountsOracle.Node n2 = delta.node(1, 4);
            delta.relationship(n1, 1, n2);
            // relationshipType 2 has not been used before...
            delta.relationship(n2, 2, n1);
        }
        delta.update(tracker, secondTx);
        delta.update(oracle);
        // then
        oracle.verify(tracker);
        // when
        tracker.rotate(secondTx);
    }
    // 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 38 with Lifespan

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

the class AbstractKeyValueStoreTest method shouldNotPickCorruptStoreFile.

@Test
public void shouldNotPickCorruptStoreFile() throws Exception {
    // given
    Store store = createTestStore();
    RotationStrategy rotation = store.rotationStrategy;
    // when
    File[] files = new File[10];
    {
        Pair<File, KeyValueStoreFile> file = rotation.create(EMPTY_DATA_PROVIDER, 1);
        files[0] = file.first();
        for (int txId = 2, i = 1; i < files.length; txId <<= 1, i++) {
            KeyValueStoreFile old = file.other();
            final int data = txId;
            file = rotation.next(file.first(), Headers.headersBuilder().put(TX_ID, (long) txId).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, data);
                }
            }));
            old.close();
            files[i] = file.first();
        }
        file.other().close();
    }
    // Corrupt the last files
    try (StoreChannel channel = resourceManager.fileSystem().open(files[9], "rw")) {
        // ruin the header
        channel.position(16);
        ByteBuffer value = ByteBuffer.allocate(16);
        value.put((byte) 0);
        value.flip();
        channel.writeAll(value);
    }
    try (StoreChannel channel = resourceManager.fileSystem().open(files[8], "rw")) {
        // ruin the header
        channel.position(32);
        ByteBuffer value = ByteBuffer.allocate(16);
        value.put((byte) 17);
        value.flip();
        channel.writeAll(value);
    }
    try (StoreChannel channel = resourceManager.fileSystem().open(files[7], "rw")) {
        // ruin the header
        channel.position(32 + 32 + 32 + 16);
        ByteBuffer value = ByteBuffer.allocate(16);
        value.putLong(0);
        value.putLong(0);
        value.flip();
        channel.writeAll(value);
    }
    // then
    try (Lifespan life = new Lifespan()) {
        life.add(store);
        assertEquals(64L, 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) ByteBuffer(java.nio.ByteBuffer) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Pair(org.neo4j.helpers.collection.Pair) Test(org.junit.Test)

Example 39 with Lifespan

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

the class CountsComputerTest method rebuildCounts.

private void rebuildCounts(long lastCommittedTransactionId) throws IOException {
    cleanupCountsForRebuilding();
    StoreFactory storeFactory = new StoreFactory(dir, pageCache, fs, NullLogProvider.getInstance());
    try (Lifespan life = new Lifespan();
        NeoStores neoStores = storeFactory.openAllNeoStores()) {
        NodeStore nodeStore = neoStores.getNodeStore();
        RelationshipStore relationshipStore = neoStores.getRelationshipStore();
        int highLabelId = (int) neoStores.getLabelTokenStore().getHighId();
        int highRelationshipTypeId = (int) neoStores.getRelationshipTypeTokenStore().getHighId();
        CountsComputer countsComputer = new CountsComputer(lastCommittedTransactionId, nodeStore, relationshipStore, highLabelId, highRelationshipTypeId);
        CountsTracker countsTracker = createCountsTracker();
        life.add(countsTracker.setInitializer(countsComputer));
    }
}
Also used : NodeStore(org.neo4j.kernel.impl.store.NodeStore) CountsComputer(org.neo4j.kernel.impl.store.CountsComputer) NeoStores(org.neo4j.kernel.impl.store.NeoStores) RelationshipStore(org.neo4j.kernel.impl.store.RelationshipStore) StoreFactory(org.neo4j.kernel.impl.store.StoreFactory) Lifespan(org.neo4j.kernel.lifecycle.Lifespan)

Example 40 with Lifespan

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

the class RecoveryCorruptedTransactionLogIT method startWithoutProblemsIfRotationForcedBeforeFileEnd.

@Test
void startWithoutProblemsIfRotationForcedBeforeFileEnd() throws IOException {
    DatabaseManagementService managementService = databaseFactory.build();
    GraphDatabaseAPI database = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    logFiles = buildDefaultLogFiles(getStoreId(database));
    generateTransaction(database);
    managementService.shutdown();
    try (Lifespan lifespan = new Lifespan(logFiles)) {
        Path originalFile = logFiles.getLogFile().getHighestLogFile();
        // in it its current position.
        try (StoreFileChannel writeChannel = fileSystem.write(originalFile)) {
            writeChannel.position(writeChannel.size());
            for (int i = 0; i < 10; i++) {
                writeChannel.writeAll(ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0 }));
            }
        }
        logFiles.getLogFile().rotate();
    }
    startStopDatabase();
    assertThat(logProvider).doesNotContainMessage("Fail to read transaction log version 0.");
}
Also used : Path(java.nio.file.Path) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) StoreFileChannel(org.neo4j.io.fs.StoreFileChannel) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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