Search in sources :

Example 66 with TestDatabaseManagementServiceBuilder

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

the class IndexSamplingIntegrationTest method shouldSampleUniqueIndex.

@ParameterizedTest
@EnumSource(value = Entity.class, names = { "NODE" })
void shouldSampleUniqueIndex(Entity entity) throws Throwable {
    GraphDatabaseService db = null;
    DatabaseManagementService managementService = null;
    long deletedNodes = 0;
    try {
        // Given
        managementService = new TestDatabaseManagementServiceBuilder(databaseLayout).build();
        db = managementService.database(DEFAULT_DATABASE_NAME);
        try (Transaction tx = db.beginTx()) {
            entity.createConstraint(tx, schemaName, TOKEN, property);
            tx.commit();
        }
        try (Transaction tx = db.beginTx()) {
            for (int i = 0; i < entities; i++) {
                entity.createEntity(tx, TOKEN, property, "" + i);
            }
            tx.commit();
        }
        try (Transaction tx = db.beginTx()) {
            for (int i = 0; i < entities; i++) {
                if (i % 10 == 0) {
                    entity.deleteFirstFound(tx, TOKEN, property, "" + i);
                    deletedNodes++;
                }
            }
            tx.commit();
        }
    } finally {
        if (db != null) {
            managementService.shutdown();
        }
    }
    // When
    triggerIndexResamplingOnNextStartup();
    // Then
    var indexSample = fetchIndexSamplingValues();
    assertThat(indexSample.uniqueValues()).as("Unique values").isEqualTo(entities - deletedNodes);
    assertThat(indexSample.sampleSize()).as("Sample size").isEqualTo(entities - deletedNodes);
    assertThat(indexSample.updates()).as("Updates").isEqualTo(0);
    assertThat(indexSample.indexSize()).as("Index size").isEqualTo(entities - deletedNodes);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 67 with TestDatabaseManagementServiceBuilder

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

the class NonUniqueIndexTest method newEmbeddedGraphDatabaseWithSlowJobScheduler.

private GraphDatabaseService newEmbeddedGraphDatabaseWithSlowJobScheduler() {
    // Inject JobScheduler
    Dependencies dependencies = new Dependencies();
    dependencies.satisfyDependencies(createJobScheduler());
    managementService = new TestDatabaseManagementServiceBuilder(testDirectory.homePath()).setExternalDependencies(dependencies).build();
    return managementService.database(GraphDatabaseSettings.DEFAULT_DATABASE_NAME);
}
Also used : TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) Dependencies(org.neo4j.collection.Dependencies)

Example 68 with TestDatabaseManagementServiceBuilder

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

the class ConstraintIndexFailureIT method shouldFailToValidateConstraintsIfUnderlyingIndexIsFailed.

@Test
void shouldFailToValidateConstraintsIfUnderlyingIndexIsFailed() throws Exception {
    // given a perfectly normal constraint
    Path dir = directory.homePath();
    DatabaseManagementService managementService = new TestDatabaseManagementServiceBuilder(dir).build();
    GraphDatabaseService db = managementService.database(DEFAULT_DATABASE_NAME);
    try (Transaction tx = db.beginTx()) {
        tx.schema().constraintFor(label("Label1")).assertPropertyIsUnique("key1").create();
        tx.commit();
    } finally {
        managementService.shutdown();
    }
    // Remove the indexes offline and start up with an index provider which reports FAILED as initial state. An ordeal, I know right...
    FileUtils.deleteDirectory(IndexDirectoryStructure.baseSchemaIndexFolder(dir));
    managementService = new TestDatabaseManagementServiceBuilder(dir).removeExtensions(INDEX_PROVIDERS_FILTER).addExtension(new FailingGenericNativeIndexProviderFactory(INITIAL_STATE)).addExtension(new TokenIndexProviderFactory()).noOpSystemGraphInitializer().build();
    db = managementService.database(DEFAULT_DATABASE_NAME);
    // when
    try (Transaction tx = db.beginTx()) {
        var e = assertThrows(ConstraintViolationException.class, () -> tx.createNode(label("Label1")).setProperty("key1", "value1"));
        assertThat(e.getCause()).isInstanceOf(UnableToValidateConstraintException.class);
        assertThat(e.getCause().getCause().getMessage()).contains("The index is in a failed state:").contains(INITIAL_STATE_FAILURE_MESSAGE);
    } finally {
        managementService.shutdown();
    }
}
Also used : Path(java.nio.file.Path) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) Transaction(org.neo4j.graphdb.Transaction) TokenIndexProviderFactory(org.neo4j.kernel.impl.index.schema.TokenIndexProviderFactory) FailingGenericNativeIndexProviderFactory(org.neo4j.kernel.impl.index.schema.FailingGenericNativeIndexProviderFactory) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test)

Example 69 with TestDatabaseManagementServiceBuilder

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

the class StartOldDbOnCurrentVersionAndCreateFusionIndexIT method setupDb.

private DatabaseManagementService setupDb(Path storeDir, IndexRecoveryTracker indexRecoveryTracker) {
    Monitors monitors = new Monitors();
    monitors.addMonitorListener(indexRecoveryTracker);
    return new TestDatabaseManagementServiceBuilder(storeDir).setMonitors(monitors).setConfig(GraphDatabaseSettings.allow_upgrade, true).build();
}
Also used : TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) Monitors(org.neo4j.monitoring.Monitors)

Example 70 with TestDatabaseManagementServiceBuilder

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

the class RecoverIndexDropIT method shouldDropIndexOnRecovery.

@Test
void shouldDropIndexOnRecovery() throws IOException {
    // given a transaction stream ending in an INDEX DROP command.
    CommittedTransactionRepresentation dropTransaction = prepareDropTransaction();
    DatabaseManagementService managementService = configure(new TestDatabaseManagementServiceBuilder(databaseLayout)).build();
    GraphDatabaseService db = managementService.database(DEFAULT_DATABASE_NAME);
    long initialIndexCount = currentIndexCount(db);
    createIndex(db);
    StorageEngineFactory storageEngineFactory = ((GraphDatabaseAPI) db).getDependencyResolver().resolveDependency(StorageEngineFactory.class);
    managementService.shutdown();
    appendDropTransactionToTransactionLog(databaseLayout.getTransactionLogsDirectory(), dropTransaction, storageEngineFactory);
    // when recovering this (the drop transaction with the index file intact)
    Monitors monitors = new Monitors();
    AssertRecoveryIsPerformed recoveryMonitor = new AssertRecoveryIsPerformed();
    monitors.addMonitorListener(recoveryMonitor);
    managementService = configure(new TestDatabaseManagementServiceBuilder(databaseLayout).setMonitors(monitors)).build();
    db = managementService.database(DEFAULT_DATABASE_NAME);
    try {
        assertTrue(recoveryMonitor.recoveryWasRequired);
        // then
        assertEquals(initialIndexCount, currentIndexCount(db));
    } finally {
        // and the ability to shut down w/o failing on still open files
        managementService.shutdown();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) StorageEngineFactory(org.neo4j.storageengine.api.StorageEngineFactory) Monitors(org.neo4j.monitoring.Monitors) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test)

Aggregations

TestDatabaseManagementServiceBuilder (org.neo4j.test.TestDatabaseManagementServiceBuilder)135 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)74 Test (org.junit.jupiter.api.Test)48 Transaction (org.neo4j.graphdb.Transaction)42 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)38 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)30 Path (java.nio.file.Path)23 BeforeEach (org.junit.jupiter.api.BeforeEach)18 Node (org.neo4j.graphdb.Node)18 EphemeralFileSystemAbstraction (org.neo4j.io.fs.EphemeralFileSystemAbstraction)13 AssertableLogProvider (org.neo4j.logging.AssertableLogProvider)11 PageCache (org.neo4j.io.pagecache.PageCache)10 UncloseableDelegatingFileSystemAbstraction (org.neo4j.io.fs.UncloseableDelegatingFileSystemAbstraction)9 DatabaseLayout (org.neo4j.io.layout.DatabaseLayout)8 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)7 TokenIndexProviderFactory (org.neo4j.kernel.impl.index.schema.TokenIndexProviderFactory)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 Dependencies (org.neo4j.collection.Dependencies)6 Config (org.neo4j.configuration.Config)6 DatabaseManagementServiceBuilder (org.neo4j.dbms.api.DatabaseManagementServiceBuilder)6