Search in sources :

Example 41 with TestGraphDatabaseFactory

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

the class IndexSamplingIntegrationTest method shouldSampleNotUniqueIndex.

@Test
public void shouldSampleNotUniqueIndex() throws Throwable {
    GraphDatabaseService db = null;
    long deletedNodes = 0;
    try {
        // Given
        db = new TestGraphDatabaseFactory().newEmbeddedDatabase(testDirectory.graphDbDir());
        IndexDefinition indexDefinition;
        try (Transaction tx = db.beginTx()) {
            indexDefinition = db.schema().indexFor(label).on(property).create();
            tx.success();
        }
        try (Transaction tx = db.beginTx()) {
            db.schema().awaitIndexOnline(indexDefinition, 10, TimeUnit.SECONDS);
            tx.success();
        }
        try (Transaction tx = db.beginTx()) {
            for (int i = 0; i < nodes; i++) {
                db.createNode(label).setProperty(property, names[i % names.length]);
                tx.success();
            }
        }
        try (Transaction tx = db.beginTx()) {
            for (int i = 0; i < (nodes / 10); i++) {
                db.findNodes(label, property, names[i % names.length]).next().delete();
                deletedNodes++;
                tx.success();
            }
        }
    } finally {
        if (db != null) {
            db.shutdown();
        }
    }
    // When
    triggerIndexResamplingOnNextStartup();
    // Then
    // sampling will consider also the delete nodes till the next lucene compaction
    DoubleLongRegister register = fetchIndexSamplingValues(db);
    assertEquals(names.length, register.readFirst());
    assertEquals(nodes, register.readSecond());
    // but the deleted nodes should not be considered in the index size value
    DoubleLongRegister indexSizeRegister = fetchIndexSizeValues(db);
    assertEquals(0, indexSizeRegister.readFirst());
    assertEquals(nodes - deletedNodes, indexSizeRegister.readSecond());
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Transaction(org.neo4j.graphdb.Transaction) DoubleLongRegister(org.neo4j.register.Register.DoubleLongRegister) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) Test(org.junit.Test)

Example 42 with TestGraphDatabaseFactory

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

the class DynamicIndexStoreViewIT method populateDbWithConcurrentUpdates.

@Test
public void populateDbWithConcurrentUpdates() throws Exception {
    GraphDatabaseService database = new TestGraphDatabaseFactory().newEmbeddedDatabase(testDirectory.graphDbDir());
    try {
        int counter = 1;
        for (int j = 0; j < 100; j++) {
            try (Transaction transaction = database.beginTx()) {
                for (int i = 0; i < 5; i++) {
                    Node node = database.createNode(Label.label("label" + counter));
                    node.setProperty("property", ThreadLocalRandom.current().nextInt());
                }
                transaction.success();
            }
            counter++;
        }
        int populatorCount = 5;
        ExecutorService executor = Executors.newFixedThreadPool(populatorCount);
        CountDownLatch startSignal = new CountDownLatch(1);
        AtomicBoolean endSignal = new AtomicBoolean();
        for (int i = 0; i < populatorCount; i++) {
            executor.submit(new Populator(database, counter, startSignal, endSignal));
        }
        try {
            try (Transaction transaction = database.beginTx()) {
                database.schema().indexFor(Label.label("label10")).on("property").create();
                transaction.success();
            }
            startSignal.countDown();
            try (Transaction transaction = database.beginTx()) {
                database.schema().awaitIndexesOnline(populatorCount, TimeUnit.MINUTES);
                transaction.success();
            }
        } finally {
            endSignal.set(true);
            executor.shutdown();
        // Basically we don't care to await their completion because they've done their job
        }
    } finally {
        database.shutdown();
        ConsistencyCheckService consistencyCheckService = new ConsistencyCheckService();
        Config config = Config.defaults();
        config = config.with(stringMap(GraphDatabaseSettings.pagecache_memory.name(), "8m"));
        consistencyCheckService.runFullConsistencyCheck(testDirectory.graphDbDir(), config, ProgressMonitorFactory.NONE, FormattedLogProvider.toOutputStream(System.out), false);
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.neo4j.graphdb.Transaction) Config(org.neo4j.kernel.configuration.Config) Node(org.neo4j.graphdb.Node) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) ExecutorService(java.util.concurrent.ExecutorService) ConsistencyCheckService(org.neo4j.consistency.ConsistencyCheckService) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 43 with TestGraphDatabaseFactory

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

the class TestRecoveryMultipleDataSources method main.

public static void main(String[] args) throws IOException {
    if (args.length != 1) {
        exit(1);
    }
    File storeDir = new File(args[0]);
    GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase(storeDir);
    try (Transaction tx = db.beginTx()) {
        db.createNode().createRelationshipTo(db.createNode(), MyRelTypes.TEST);
        tx.success();
    }
    ((GraphDatabaseAPI) db).getDependencyResolver().resolveDependency(CheckPointer.class).forceCheckPoint(new SimpleTriggerInfo("test"));
    try (Transaction tx = db.beginTx()) {
        db.index().forNodes("index").add(db.createNode(), storeDir.getAbsolutePath(), db.createNode());
        tx.success();
    }
    exit(0);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) SimpleTriggerInfo(org.neo4j.kernel.impl.transaction.log.checkpoint.SimpleTriggerInfo) Transaction(org.neo4j.graphdb.Transaction) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) CheckPointer(org.neo4j.kernel.impl.transaction.log.checkpoint.CheckPointer) File(java.io.File)

Example 44 with TestGraphDatabaseFactory

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

the class DatabaseStartupTest method startTheDatabaseWithWrongVersionShouldFailWithUpgradeNotAllowed.

@Test
public void startTheDatabaseWithWrongVersionShouldFailWithUpgradeNotAllowed() throws Throwable {
    // given
    // create a store
    File storeDir = testDirectory.graphDbDir();
    GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase(storeDir);
    try (Transaction tx = db.beginTx()) {
        db.createNode();
        tx.success();
    }
    db.shutdown();
    // mess up the version in the metadatastore
    try (FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
        PageCache pageCache = StandalonePageCacheFactory.createPageCache(fileSystem)) {
        MetaDataStore.setRecord(pageCache, new File(storeDir, MetaDataStore.DEFAULT_NAME), MetaDataStore.Position.STORE_VERSION, MetaDataStore.versionStringToLong("bad"));
    }
    // when
    try {
        new TestGraphDatabaseFactory().newEmbeddedDatabase(storeDir);
        fail("It should have failed.");
    } catch (RuntimeException ex) {
        // then
        assertTrue(ex.getCause() instanceof LifecycleException);
        assertTrue(ex.getCause().getCause() instanceof UpgradeNotAllowedByConfigurationException);
        assertEquals("Failed to start Neo4j with an older data store version. To enable automatic upgrade, " + "please set configuration parameter \"dbms.allow_format_migration=true\"", ex.getCause().getCause().getMessage());
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) UpgradeNotAllowedByConfigurationException(org.neo4j.kernel.impl.storemigration.UpgradeNotAllowedByConfigurationException) LifecycleException(org.neo4j.kernel.lifecycle.LifecycleException) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) Transaction(org.neo4j.graphdb.Transaction) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) File(java.io.File) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Example 45 with TestGraphDatabaseFactory

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

the class MultipleIndexPopulationStressIT method populateDbAndIndexes.

private void populateDbAndIndexes(int nodeCount, boolean multiThreaded) throws InterruptedException {
    final GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabaseBuilder(directory.graphDbDir()).setConfig(GraphDatabaseSettings.multi_threaded_schema_index_population_enabled, multiThreaded + "").newGraphDatabase();
    try {
        createIndexes(db);
        final AtomicBoolean end = new AtomicBoolean();
        ExecutorService executor = cleanup.add(Executors.newCachedThreadPool());
        for (int i = 0; i < 10; i++) {
            executor.submit(() -> {
                Randoms random = new Randoms();
                while (!end.get()) {
                    changeRandomNode(db, nodeCount, random);
                }
            });
        }
        while (!indexesAreOnline(db)) {
            Thread.sleep(100);
        }
        end.set(true);
        executor.shutdown();
        executor.awaitTermination(10, SECONDS);
    } finally {
        db.shutdown();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Randoms(org.neo4j.test.Randoms) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)156 Test (org.junit.Test)70 Transaction (org.neo4j.graphdb.Transaction)62 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)61 File (java.io.File)34 Node (org.neo4j.graphdb.Node)28 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)26 BeforeClass (org.junit.BeforeClass)25 Before (org.junit.Before)22 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)9 Relationship (org.neo4j.graphdb.Relationship)8 Result (org.neo4j.graphdb.Result)6 GraphDatabaseBuilder (org.neo4j.graphdb.factory.GraphDatabaseBuilder)6 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)6 GraphDatabaseShellServer (org.neo4j.shell.kernel.GraphDatabaseShellServer)6 HashMap (java.util.HashMap)5 DependencyResolver (org.neo4j.graphdb.DependencyResolver)5 PageCache (org.neo4j.io.pagecache.PageCache)5 WrappedDatabase (org.neo4j.server.database.WrappedDatabase)5 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)4