use of org.neo4j.dbms.api.DatabaseManagementService in project neo4j by neo4j.
the class CountsComputerTest method shouldCreateACountsStoreWhenThereAreNodesAndRelationshipsInTheDB.
@Test
void shouldCreateACountsStoreWhenThereAreNodesAndRelationshipsInTheDB() throws IOException, KernelException {
DatabaseManagementService managementService = dbBuilder.build();
final GraphDatabaseAPI db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
Label[] labels = null;
int[] labelIds = null;
RelationshipType[] relTypes = null;
int[] relTypeIds = null;
Node[] nodes = null;
Relationship[] rels = null;
try (Transaction tx = db.beginTx()) {
labels = createLabels(4);
labelIds = getLabelIdsFrom(tx, labels);
relTypes = createRelationShipTypes(3);
relTypeIds = getRelTypeIdsFrom(tx, relTypes);
nodes = new Node[] { tx.createNode(labels[0]), tx.createNode(labels[1]), tx.createNode(labels[2]), tx.createNode() };
rels = new Relationship[] { nodes[0].createRelationshipTo(nodes[2], relTypes[0]), nodes[3].createRelationshipTo(nodes[1], relTypes[1]) };
tx.commit();
}
long lastCommittedTransactionId = getLastTxId(db);
managementService.shutdown();
rebuildCounts(lastCommittedTransactionId);
try (GBPTreeCountsStore store = createCountsStore()) {
softly.assertThat(store.txId()).as("Store Transaction id").isEqualTo(lastCommittedTransactionId);
softly.assertThat(store.nodeCount(ANY_LABEL, NULL)).as("count: ()").isEqualTo(nodes.length);
softly.assertThat(store.nodeCount(labelIds[0], NULL)).as("count: (:%s)", labels[0]).isEqualTo(1);
softly.assertThat(store.nodeCount(labelIds[1], NULL)).as("count: (:%s)", labels[1]).isEqualTo(1);
softly.assertThat(store.nodeCount(labelIds[2], NULL)).as("count: (:%s)", labels[2]).isEqualTo(1);
softly.assertThat(store.nodeCount(labelIds[3], NULL)).as("count: (:%s)", labels[3]).isEqualTo(0);
softly.assertThat(store.relationshipCount(ANY_LABEL, ANY_RELATIONSHIP_TYPE, ANY_LABEL, NULL)).as("()-[]->()").isEqualTo(rels.length);
softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[0], ANY_LABEL, NULL)).as("count: ()-[:%s]->()", relTypes[0]).isEqualTo(1);
softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[1], ANY_LABEL, NULL)).as("count: ()-[:%s]->()", relTypes[1]).isEqualTo(1);
softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[2], ANY_LABEL, NULL)).as("count: ()-[:%s]->()", relTypes[2]).isEqualTo(0);
softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[1], labelIds[1], NULL)).as("count: ()-[:%s]->(:%s)", relTypes[1], labels[1]).isEqualTo(1);
softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[0], labelIds[1], NULL)).as("count: ()-[:%s]->(:%s)", relTypes[0], labels[1]).isEqualTo(0);
}
}
use of org.neo4j.dbms.api.DatabaseManagementService in project neo4j by neo4j.
the class CountsComputerTest method shouldCreateACountsStoreWhenThereAreUnusedNodeRecordsInTheDB.
@Test
void shouldCreateACountsStoreWhenThereAreUnusedNodeRecordsInTheDB() throws IOException, KernelException {
DatabaseManagementService managementService = dbBuilder.build();
final GraphDatabaseAPI db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
Label[] labels = null;
int[] labelIds = null;
Node[] nodes = null;
try (Transaction tx = db.beginTx()) {
labels = createLabels(4);
labelIds = getLabelIdsFrom(tx, labels);
nodes = new Node[] { tx.createNode(labels[0]), tx.createNode(labels[1]), tx.createNode(labels[2]), tx.createNode() };
nodes[2].delete();
tx.commit();
}
long lastCommittedTransactionId = getLastTxId(db);
managementService.shutdown();
rebuildCounts(lastCommittedTransactionId);
try (GBPTreeCountsStore store = createCountsStore()) {
softly.assertThat(store.txId()).as("Store Transaction id").isEqualTo(lastCommittedTransactionId);
softly.assertThat(store.nodeCount(ANY_LABEL, NULL)).as("count: ()").isEqualTo(nodes.length - 1);
softly.assertThat(store.nodeCount(labelIds[0], NULL)).as("count: (:%s)", labels[0]).isEqualTo(1);
softly.assertThat(store.nodeCount(labelIds[1], NULL)).as("count: (:%s)", labels[1]).isEqualTo(1);
softly.assertThat(store.nodeCount(labelIds[2], NULL)).as("count: (:%s)", labels[2]).isEqualTo(0);
softly.assertThat(store.nodeCount(labelIds[3], NULL)).as("count: (:%s)", labels[3]).isEqualTo(0);
}
}
use of org.neo4j.dbms.api.DatabaseManagementService in project neo4j by neo4j.
the class CountsComputerTest method shouldCreateACountsStoreWhenThereAreUnusedRelationshipRecordsInTheDB.
@Test
void shouldCreateACountsStoreWhenThereAreUnusedRelationshipRecordsInTheDB() throws IOException, KernelException {
DatabaseManagementService managementService = dbBuilder.build();
final GraphDatabaseAPI db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
Label[] labels = null;
int[] labelIds = null;
RelationshipType[] relTypes = null;
int[] relTypeIds = null;
Node[] nodes = null;
Relationship[] rels = null;
try (Transaction tx = db.beginTx()) {
labels = createLabels(4);
labelIds = getLabelIdsFrom(tx, labels);
relTypes = createRelationShipTypes(2);
relTypeIds = getRelTypeIdsFrom(tx, relTypes);
nodes = new Node[] { tx.createNode(labels[0]), tx.createNode(labels[1]) };
rels = new Relationship[] { nodes[0].createRelationshipTo(nodes[1], relTypes[0]), nodes[1].createRelationshipTo(nodes[0], relTypes[1]) };
rels[0].delete();
tx.commit();
}
long lastCommittedTransactionId = getLastTxId(db);
managementService.shutdown();
rebuildCounts(lastCommittedTransactionId);
try (GBPTreeCountsStore store = createCountsStore()) {
softly.assertThat(store.txId()).as("Store Transaction id").isEqualTo(lastCommittedTransactionId);
softly.assertThat(store.nodeCount(ANY_LABEL, NULL)).as("count: ()").isEqualTo(nodes.length);
softly.assertThat(store.nodeCount(labelIds[0], NULL)).as("count: (:%s)", labels[0]).isEqualTo(1);
softly.assertThat(store.nodeCount(labelIds[1], NULL)).as("count: (:%s)", labels[1]).isEqualTo(1);
softly.assertThat(store.nodeCount(labelIds[2], NULL)).as("count: (:%s)", labels[2]).isEqualTo(0);
softly.assertThat(store.nodeCount(labelIds[3], NULL)).as("count: (:%s)", labels[3]).isEqualTo(0);
softly.assertThat(store.relationshipCount(ANY_LABEL, ANY_RELATIONSHIP_TYPE, ANY_LABEL, NULL)).as("()-[]->()").isEqualTo(rels.length - 1);
softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[0], ANY_LABEL, NULL)).as("count: ()-[:%s]->()", relTypes[0]).isEqualTo(0);
softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[1], ANY_LABEL, NULL)).as("count: ()-[:%s]->()", relTypes[1]).isEqualTo(1);
}
}
use of org.neo4j.dbms.api.DatabaseManagementService in project neo4j by neo4j.
the class StoreMigratorTest method extractTransactionalInformationFromLogs.
private void extractTransactionalInformationFromLogs(Path customLogsLocation) throws IOException {
Config config = Config.defaults(transaction_logs_root_path, customLogsLocation);
LogService logService = new SimpleLogService(NullLogProvider.getInstance(), NullLogProvider.getInstance());
DatabaseManagementService managementService = new TestDatabaseManagementServiceBuilder(databaseLayout).setConfig(transaction_logs_root_path, customLogsLocation).build();
GraphDatabaseAPI database = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
StorageEngineFactory storageEngineFactory = database.getDependencyResolver().resolveDependency(StorageEngineFactory.class);
for (int i = 0; i < 10; i++) {
try (Transaction transaction = database.beginTx()) {
transaction.createNode();
transaction.commit();
}
}
DatabaseLayout databaseLayout = database.databaseLayout();
Path neoStore = databaseLayout.metadataStore();
managementService.shutdown();
MetaDataStore.setRecord(pageCache, neoStore, MetaDataStore.Position.LAST_CLOSED_TRANSACTION_LOG_VERSION, MetaDataRecordFormat.FIELD_NOT_PRESENT, databaseLayout.getDatabaseName(), NULL);
RecordStorageMigrator migrator = new RecordStorageMigrator(fileSystem, pageCache, config, logService, jobScheduler, PageCacheTracer.NULL, batchImporterFactory, INSTANCE);
LogPosition logPosition = migrator.extractTransactionLogPosition(neoStore, databaseLayout, 100, NULL);
LogFiles logFiles = LogFilesBuilder.activeFilesBuilder(databaseLayout, fileSystem, pageCache).withConfig(config).withCommandReaderFactory(storageEngineFactory.commandReaderFactory()).build();
assertEquals(0, logPosition.getLogVersion());
assertEquals(Files.size(logFiles.getLogFile().getHighestLogFile()), logPosition.getByteOffset());
}
use of org.neo4j.dbms.api.DatabaseManagementService in project neo4j by neo4j.
the class CheckPointerIntegrationTest method shouldCheckPointBasedOnTxCount.
@Test
void shouldCheckPointBasedOnTxCount() throws Throwable {
// given
DatabaseManagementService managementService = builder.setConfig(check_point_interval_time, ofMillis(300)).setConfig(check_point_interval_tx, 1).setConfig(logical_log_rotation_threshold, gibiBytes(1)).build();
int counter;
try {
GraphDatabaseService db = managementService.database(DEFAULT_DATABASE_NAME);
// when
try (Transaction tx = db.beginTx()) {
tx.createNode();
tx.commit();
}
// Instead of waiting 10s for the background job to do this check, perform the check right here
triggerCheckPointAttempt(db);
List<CheckpointInfo> checkpoints = checkPointsInTxLog(db);
assertThat(checkpoints).isNotEmpty();
counter = checkpoints.size();
} finally {
managementService.shutdown();
}
managementService = builder.build();
try {
// then - checkpoints + shutdown checkpoint have been written in the log
var checkpointInfos = checkPointsInTxLog(managementService.database(DEFAULT_DATABASE_NAME));
// Use greater-than-or-equal-to in order to accommodate the following data-race:
// Since the `threshold.isCheckPointingNeeded()` call in CheckPointerImpl is done outside of the `mutex.checkPoint()` lock,
// and also the `check_point_interval_time` is 300 milliseconds, it means that our direct `triggerCheckPointAttempt( db )` call
// can race with the scheduled checkpoints, and both can decide that a checkpoint is needed. They will then coordinate via the
// lock to do two checkpoints, one after the other. If our direct call wins the race and goes first, then the scheduled
// checkpoint will race with our `checkPointInTxLog( db )` call, which can then count only one checkpoint in the log when there
// are actually two.
assertThat(checkpointInfos.size()).isGreaterThanOrEqualTo(counter + 1);
} finally {
managementService.shutdown();
}
}
Aggregations