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);
}
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();
}
}
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();
}
}
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();
}
}
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();
}
Aggregations