Search in sources :

Example 76 with DatabaseManagementService

use of org.neo4j.dbms.api.DatabaseManagementService 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 77 with DatabaseManagementService

use of org.neo4j.dbms.api.DatabaseManagementService 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 78 with DatabaseManagementService

use of org.neo4j.dbms.api.DatabaseManagementService 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)

Example 79 with DatabaseManagementService

use of org.neo4j.dbms.api.DatabaseManagementService in project neo4j by neo4j.

the class RecoverIndexDropIT method prepareDropTransaction.

private CommittedTransactionRepresentation prepareDropTransaction() throws IOException {
    DatabaseManagementService managementService = configure(new TestDatabaseManagementServiceBuilder(directory.directory("preparation"))).build();
    GraphDatabaseAPI db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    try {
        // Create index
        IndexDefinition index;
        index = createIndex(db);
        try (Transaction tx = db.beginTx()) {
            tx.schema().getIndexByName(index.getName()).drop();
            tx.commit();
        }
        return extractLastTransaction(db);
    } finally {
        managementService.shutdown();
    }
}
Also used : TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Transaction(org.neo4j.graphdb.Transaction) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService)

Example 80 with DatabaseManagementService

use of org.neo4j.dbms.api.DatabaseManagementService in project neo4j by neo4j.

the class ConstraintRecoveryIT method shouldHaveAvailableOrphanedConstraintIndexIfUniqueConstraintCreationFails.

@Test
void shouldHaveAvailableOrphanedConstraintIndexIfUniqueConstraintCreationFails() {
    // given
    Path pathToDb = testDirectory.homePath();
    TestDatabaseManagementServiceBuilder dbFactory = new TestDatabaseManagementServiceBuilder(pathToDb);
    dbFactory.setFileSystem(fs);
    final EphemeralFileSystemAbstraction[] storeInNeedOfRecovery = new EphemeralFileSystemAbstraction[1];
    final AtomicBoolean monitorCalled = new AtomicBoolean(false);
    Monitors monitors = new Monitors();
    monitors.addMonitorListener(new IndexingService.MonitorAdapter() {

        @Override
        public void indexPopulationScanComplete() {
            monitorCalled.set(true);
            db.getDependencyResolver().resolveDependency(RecordStorageEngine.class).testAccessNeoStores().getSchemaStore().flush(NULL);
            storeInNeedOfRecovery[0] = fs.snapshot();
        }
    });
    dbFactory.setMonitors(monitors);
    // This test relies on behaviour that is specific to the Lucene populator, where uniqueness is controlled
    // after index has been populated, which is why we're using NATIVE20 and index booleans (they end up in Lucene)
    DatabaseManagementService managementService = configure(dbFactory.impermanent().setConfig(default_schema_provider, NATIVE30.providerName())).build();
    db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    try (Transaction tx = db.beginTx()) {
        for (int i = 0; i < 2; i++) {
            tx.createNode(LABEL).setProperty(KEY, "true");
        }
        tx.commit();
    }
    assertThrows(ConstraintViolationException.class, () -> {
        try (Transaction tx = db.beginTx()) {
            tx.schema().constraintFor(LABEL).assertPropertyIsUnique(KEY).create();
        }
    });
    managementService.shutdown();
    assertTrue(monitorCalled.get());
    // when
    dbFactory = new TestDatabaseManagementServiceBuilder(pathToDb);
    dbFactory.setFileSystem(storeInNeedOfRecovery[0]);
    DatabaseManagementService secondManagementService = configure(dbFactory.impermanent()).build();
    db = (GraphDatabaseAPI) secondManagementService.database(DEFAULT_DATABASE_NAME);
    // then
    try (Transaction tx = db.beginTx()) {
        tx.schema().awaitIndexesOnline(10, TimeUnit.MINUTES);
    }
    try (Transaction transaction = db.beginTx()) {
        assertEquals(2, Iterables.count(transaction.getAllNodes()));
    }
    try (Transaction tx = db.beginTx()) {
        assertEquals(0, Iterables.count(Iterables.asList(tx.schema().getConstraints())));
    }
    try (Transaction tx = db.beginTx()) {
        IndexDefinition orphanedConstraintIndex = single(tx.schema().getIndexes(LABEL));
        assertEquals(LABEL.name(), single(orphanedConstraintIndex.getLabels()).name());
        assertEquals(KEY, single(orphanedConstraintIndex.getPropertyKeys()));
    }
    secondManagementService.shutdown();
}
Also used : Path(java.nio.file.Path) EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) Transaction(org.neo4j.graphdb.Transaction) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) Monitors(org.neo4j.monitoring.Monitors) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test)

Aggregations

DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)155 Test (org.junit.jupiter.api.Test)100 TestDatabaseManagementServiceBuilder (org.neo4j.test.TestDatabaseManagementServiceBuilder)79 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)61 Transaction (org.neo4j.graphdb.Transaction)60 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)56 Path (java.nio.file.Path)37 Node (org.neo4j.graphdb.Node)29 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 Label (org.neo4j.graphdb.Label)16 MethodSource (org.junit.jupiter.params.provider.MethodSource)10 EphemeralFileSystemAbstraction (org.neo4j.io.fs.EphemeralFileSystemAbstraction)9 DatabaseLayout (org.neo4j.io.layout.DatabaseLayout)9 DatabaseStateService (org.neo4j.dbms.DatabaseStateService)8 Config (org.neo4j.configuration.Config)7 DatabaseManagementServiceBuilder (org.neo4j.dbms.api.DatabaseManagementServiceBuilder)7 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)7 GBPTreeCountsStore (org.neo4j.internal.counts.GBPTreeCountsStore)6 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)6 Lifespan (org.neo4j.kernel.lifecycle.Lifespan)6