Search in sources :

Example 26 with FileSystemAbstraction

use of org.neo4j.io.fs.FileSystemAbstraction in project neo4j by neo4j.

the class NeoStoresTest method shouldAddUpgradeFieldsToTheNeoStoreIfNotPresent.

@Test
public void shouldAddUpgradeFieldsToTheNeoStoreIfNotPresent() throws IOException {
    FileSystemAbstraction fileSystem = fs.get();
    File neoStoreDir = new File("/tmp/graph.db/neostore").getAbsoluteFile();
    StoreFactory factory = newStoreFactory(neoStoreDir, pageCache, fileSystem);
    long recordVersion = defaultStoreVersion();
    try (NeoStores neoStores = factory.openAllNeoStores(true)) {
        MetaDataStore metaDataStore = neoStores.getMetaDataStore();
        metaDataStore.setCreationTime(3);
        metaDataStore.setRandomNumber(4);
        metaDataStore.setCurrentLogVersion(5);
        metaDataStore.setLastCommittedAndClosedTransactionId(6, 42, BASE_TX_COMMIT_TIMESTAMP, 43, 44);
        metaDataStore.setStoreVersion(recordVersion);
        metaDataStore.setGraphNextProp(8);
        metaDataStore.setLatestConstraintIntroducingTx(9);
    }
    File file = new File(neoStoreDir, MetaDataStore.DEFAULT_NAME);
    assertNotEquals(10, MetaDataStore.getRecord(pageCache, file, Position.UPGRADE_TRANSACTION_ID));
    assertNotEquals(11, MetaDataStore.getRecord(pageCache, file, Position.UPGRADE_TRANSACTION_CHECKSUM));
    MetaDataStore.setRecord(pageCache, file, Position.UPGRADE_TRANSACTION_ID, 10);
    MetaDataStore.setRecord(pageCache, file, Position.UPGRADE_TRANSACTION_CHECKSUM, 11);
    MetaDataStore.setRecord(pageCache, file, Position.UPGRADE_TIME, 12);
    try (NeoStores neoStores = factory.openAllNeoStores()) {
        MetaDataStore metaDataStore = neoStores.getMetaDataStore();
        assertEquals(3, metaDataStore.getCreationTime());
        assertEquals(4, metaDataStore.getRandomNumber());
        assertEquals(5, metaDataStore.getCurrentLogVersion());
        assertEquals(6, metaDataStore.getLastCommittedTransactionId());
        assertEquals(recordVersion, metaDataStore.getStoreVersion());
        assertEquals(8, metaDataStore.getGraphNextProp());
        assertEquals(9, metaDataStore.getLatestConstraintIntroducingTx());
        assertEquals(new TransactionId(10, 11, BASE_TX_COMMIT_TIMESTAMP), metaDataStore.getUpgradeTransaction());
        assertEquals(12, metaDataStore.getUpgradeTime());
        assertArrayEquals(metaDataStore.getLastClosedTransaction(), new long[] { 6, 44, 43 });
    }
    MetaDataStore.setRecord(pageCache, file, Position.UPGRADE_TRANSACTION_COMMIT_TIMESTAMP, 13);
    try (NeoStores neoStores = factory.openAllNeoStores()) {
        MetaDataStore metaDataStore = neoStores.getMetaDataStore();
        assertEquals(new TransactionId(10, 11, 13), metaDataStore.getUpgradeTransaction());
    }
}
Also used : UncloseableDelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.UncloseableDelegatingFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) File(java.io.File) Test(org.junit.Test)

Example 27 with FileSystemAbstraction

use of org.neo4j.io.fs.FileSystemAbstraction in project neo4j by neo4j.

the class NeoStoresTest method shouldCloseAllTheStoreEvenIfExceptionsAreThrown.

@Test
public void shouldCloseAllTheStoreEvenIfExceptionsAreThrown() throws Exception {
    // given
    FileSystemAbstraction fileSystem = fs.get();
    Config defaults = Config.embeddedDefaults(singletonMap(counts_store_rotation_timeout.name(), "60m"));
    StoreFactory factory = new StoreFactory(storeDir, defaults, new DefaultIdGeneratorFactory(fileSystem), pageCache, fileSystem, NullLogProvider.getInstance());
    NeoStores neoStore = factory.openAllNeoStores(true);
    // let's hack the counts store so it fails to rotate and hence it fails to close as well...
    final CountsTracker counts = neoStore.getCounts();
    counts.start();
    long nextTxId = neoStore.getMetaDataStore().getLastCommittedTransactionId() + 1;
    AtomicReference<Throwable> exRef = new AtomicReference<>();
    Thread thread = new Thread(() -> {
        try {
            counts.rotate(nextTxId);
        } catch (InterruptedIOException e) {
        // expected due to the interrupted below
        } catch (Throwable e) {
            exRef.set(e);
            throw new RuntimeException(e);
        }
    });
    thread.start();
    // let's wait for the thread to start waiting for the next transaction id
    ThreadTestUtils.awaitThreadState(thread, 500, Thread.State.TIMED_WAITING, Thread.State.WAITING);
    try {
        // when we close the stores...
        neoStore.close();
        fail("should have thrown2");
    } catch (IllegalStateException ex) {
        // then
        assertEquals("Cannot stop in state: rotating", ex.getMessage());
    }
    thread.interrupt();
    thread.join();
    // and the page cache closes with no errors
    pageCache.close();
    // and only InterruptedIOException is thrown in the other thread
    assertNull(exRef.get());
}
Also used : InterruptedIOException(java.io.InterruptedIOException) UncloseableDelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.UncloseableDelegatingFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) Config(org.neo4j.kernel.configuration.Config) DefaultIdGeneratorFactory(org.neo4j.kernel.impl.store.id.DefaultIdGeneratorFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountsTracker(org.neo4j.kernel.impl.store.counts.CountsTracker) Test(org.junit.Test)

Example 28 with FileSystemAbstraction

use of org.neo4j.io.fs.FileSystemAbstraction in project neo4j by neo4j.

the class NodeStoreTest method shouldCloseStoreFileOnFailureToOpen.

@Test
public void shouldCloseStoreFileOnFailureToOpen() throws Exception {
    // GIVEN
    final AtomicBoolean fired = new AtomicBoolean();
    FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(efs.get()) {

        @Override
        public StoreChannel open(File fileName, String mode) throws IOException {
            return new DelegatingStoreChannel(super.open(fileName, mode)) {

                @Override
                public int read(ByteBuffer dst) throws IOException {
                    Exception stack = new Exception();
                    if (containsStackTraceElement(stack, item -> item.getMethodName().equals("initGenerator")) && !containsStackTraceElement(stack, item -> item.getMethodName().equals("createNodeStore"))) {
                        fired.set(true);
                        throw new IOException("Proving a point here");
                    }
                    return super.read(dst);
                }
            };
        }
    };
    // WHEN
    try (PageCache pageCache = pageCacheRule.getPageCache(fs)) {
        newNodeStore(fs);
        fail("Should fail");
    }// Close the page cache here so that we can see failure to close (due to still mapped files)
     catch (Exception e) {
        // THEN
        assertTrue(contains(e, IOException.class));
        assertTrue(fired.get());
    }
}
Also used : IdGeneratorFactory(org.neo4j.kernel.impl.store.id.IdGeneratorFactory) EphemeralFileSystemRule(org.neo4j.test.rule.fs.EphemeralFileSystemRule) Matchers.not(org.hamcrest.Matchers.not) NORMAL(org.neo4j.kernel.impl.store.record.RecordLoad.NORMAL) NO_NEXT_RELATIONSHIP(org.neo4j.kernel.impl.store.record.Record.NO_NEXT_RELATIONSHIP) NullLogProvider(org.neo4j.logging.NullLogProvider) ByteBuffer(java.nio.ByteBuffer) Assert.assertThat(org.junit.Assert.assertThat) StoreChannel(org.neo4j.io.fs.StoreChannel) Arrays.asList(java.util.Arrays.asList) After(org.junit.After) Assert.fail(org.junit.Assert.fail) ClassRule(org.junit.ClassRule) PageCache(org.neo4j.io.pagecache.PageCache) DefaultIdGeneratorFactory(org.neo4j.kernel.impl.store.id.DefaultIdGeneratorFactory) Predicate(java.util.function.Predicate) Collection(java.util.Collection) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) IdType(org.neo4j.kernel.impl.store.id.IdType) Assert.assertFalse(org.junit.Assert.assertFalse) PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) Mockito.mock(org.mockito.Mockito.mock) Primitive(org.neo4j.collection.primitive.Primitive) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) PageCacheRule(org.neo4j.test.rule.PageCacheRule) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) NO_NEXT_PROPERTY(org.neo4j.kernel.impl.store.record.Record.NO_NEXT_PROPERTY) DynamicArrayStore.allocateFromNumbers(org.neo4j.kernel.impl.store.DynamicArrayStore.allocateFromNumbers) NodeStore.readOwnerFromDynamicLabelsRecord(org.neo4j.kernel.impl.store.NodeStore.readOwnerFromDynamicLabelsRecord) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) Config(org.neo4j.kernel.configuration.Config) Matchers.empty(org.hamcrest.Matchers.empty) LongStream(java.util.stream.LongStream) ReusableRecordsAllocator(org.neo4j.kernel.impl.store.allocator.ReusableRecordsAllocator) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) Exceptions.contains(org.neo4j.helpers.Exceptions.contains) Rule(org.junit.Rule) Iterables(org.neo4j.helpers.collection.Iterables) NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) Visitor(org.neo4j.helpers.collection.Visitor) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Example 29 with FileSystemAbstraction

use of org.neo4j.io.fs.FileSystemAbstraction in project neo4j by neo4j.

the class StoreFactoryTest method shouldHandleStoreConsistingOfOneEmptyFile.

@Test
public void shouldHandleStoreConsistingOfOneEmptyFile() throws Exception {
    StoreFactory storeFactory = storeFactory(Config.empty());
    FileSystemAbstraction fs = fsRule.get();
    fs.create(new File(storeDir, "neostore.nodestore.db.labels"));
    storeFactory.openAllNeoStores(true).close();
}
Also used : FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) File(java.io.File) Test(org.junit.Test)

Example 30 with FileSystemAbstraction

use of org.neo4j.io.fs.FileSystemAbstraction in project neo4j by neo4j.

the class StoreFactoryTest method shouldCompleteInitializationOfStoresWithIncompleteHeaders.

@Test
public void shouldCompleteInitializationOfStoresWithIncompleteHeaders() throws Exception {
    StoreFactory storeFactory = storeFactory(Config.empty());
    storeFactory.openAllNeoStores(true).close();
    FileSystemAbstraction fs = fsRule.get();
    for (File f : fs.listFiles(storeDir)) {
        fs.truncate(f, 0);
    }
    storeFactory.openAllNeoStores(true).close();
}
Also used : FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) File(java.io.File) Test(org.junit.Test)

Aggregations

FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)125 File (java.io.File)88 Test (org.junit.Test)82 DefaultFileSystemAbstraction (org.neo4j.io.fs.DefaultFileSystemAbstraction)34 IOException (java.io.IOException)28 Config (org.neo4j.kernel.configuration.Config)23 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)22 PageCache (org.neo4j.io.pagecache.PageCache)22 DelegatingFileSystemAbstraction (org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction)20 ByteBuffer (java.nio.ByteBuffer)13 StoreChannel (org.neo4j.io.fs.StoreChannel)11 LifeSupport (org.neo4j.kernel.lifecycle.LifeSupport)10 UncloseableDelegatingFileSystemAbstraction (org.neo4j.graphdb.mockfs.UncloseableDelegatingFileSystemAbstraction)9 DefaultIdGeneratorFactory (org.neo4j.kernel.impl.store.id.DefaultIdGeneratorFactory)9 OutputStream (java.io.OutputStream)8 AdversarialFileSystemAbstraction (org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction)8 DelegatingStoreChannel (org.neo4j.graphdb.mockfs.DelegatingStoreChannel)8 Map (java.util.Map)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 AdversarialPagedFile (org.neo4j.adversaries.pagecache.AdversarialPagedFile)7