Search in sources :

Example 51 with Lifespan

use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.

the class DegreesRebuildFromStoreTest method skipNotUsedRecordsOnDegreeStoreRebuild.

@Test
void skipNotUsedRecordsOnDegreeStoreRebuild() throws Exception {
    // given a dataset containing mixed sparse and dense nodes with relationships in random directions,
    // where some chains have been marked as having external degrees
    int denseThreshold = dense_node_threshold.defaultValue();
    DatabaseLayout layout = DatabaseLayout.ofFlat(directory.homePath());
    int[] relationshipTypes;
    MutableLongLongMap expectedDegrees = LongLongMaps.mutable.empty();
    try (Lifespan life = new Lifespan()) {
        RecordStorageEngine storageEngine = openStorageEngine(layout, denseThreshold);
        relationshipTypes = createRelationshipTypes(storageEngine);
        life.add(storageEngine);
        generateData(storageEngine, denseThreshold, relationshipTypes);
        storageEngine.relationshipGroupDegreesStore().accept((groupId, direction, degree) -> expectedDegrees.put(combinedKeyOnGroupAndDirection(groupId, direction), degree), NULL);
        assertThat(expectedDegrees.isEmpty()).isFalse();
        RelationshipGroupStore groupStore = storageEngine.testAccessNeoStores().getRelationshipGroupStore();
        long highId = groupStore.getHighId();
        assertThat(highId).isGreaterThan(1);
        for (int i = 10; i < highId; i++) {
            RelationshipGroupRecord record = groupStore.getRecord(i, new RelationshipGroupRecord(i), RecordLoad.ALWAYS, NULL);
            record.setInUse(false);
            groupStore.updateRecord(record, NULL);
        }
        storageEngine.flushAndForce(NULL);
    }
    // when
    directory.getFileSystem().deleteFile(layout.relationshipGroupDegreesStore());
    try (Lifespan life = new Lifespan()) {
        RecordStorageEngine storageEngine = assertDoesNotThrow(() -> life.add(openStorageEngine(layout, denseThreshold)));
        // then
        storageEngine.relationshipGroupDegreesStore().accept((groupId, direction, degree) -> {
            long key = combinedKeyOnGroupAndDirection(groupId, direction);
            assertThat(expectedDegrees.containsKey(key)).isTrue();
            long expectedDegree = expectedDegrees.get(key);
            expectedDegrees.remove(key);
            assertThat(degree).isEqualTo(expectedDegree);
        }, NULL);
        assertThat(expectedDegrees.size()).isGreaterThan(0);
    }
}
Also used : RelationshipGroupRecord(org.neo4j.kernel.impl.store.record.RelationshipGroupRecord) RecordStorageEngine(org.neo4j.internal.recordstorage.RecordStorageEngine) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) RelationshipGroupStore(org.neo4j.kernel.impl.store.RelationshipGroupStore) MutableLongLongMap(org.eclipse.collections.api.map.primitive.MutableLongLongMap) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Test(org.junit.jupiter.api.Test)

Example 52 with Lifespan

use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.

the class CompositeDatabaseAvailabilityGuardTest method stopOfAvailabilityGuardDeregisterItInCompositeParent.

@Test
void stopOfAvailabilityGuardDeregisterItInCompositeParent() throws Exception {
    int initialGuards = compositeGuard.getGuards().size();
    DatabaseAvailabilityGuard firstGuard = createDatabaseAvailabilityGuard(randomNamedDatabaseId(), mockClock, compositeGuard);
    DatabaseAvailabilityGuard secondGuard = createDatabaseAvailabilityGuard(randomNamedDatabaseId(), mockClock, compositeGuard);
    firstGuard.start();
    secondGuard.start();
    assertEquals(2, countNewGuards(initialGuards));
    new Lifespan(firstGuard).close();
    assertEquals(1, countNewGuards(initialGuards));
    new Lifespan(secondGuard).close();
    assertEquals(0, countNewGuards(initialGuards));
}
Also used : Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Test(org.junit.jupiter.api.Test)

Example 53 with Lifespan

use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.

the class SetInitialPasswordCommand method realUsersExist.

private boolean realUsersExist(Config config) {
    boolean result = false;
    Path authFile = CommunitySecurityModule.getUserRepositoryFile(config);
    if (ctx.fs().fileExists(authFile)) {
        result = true;
        // Check if it only contains the default neo4j user
        FileUserRepository userRepository = new FileUserRepository(ctx.fs(), authFile, NullLogProvider.getInstance());
        try (Lifespan life = new Lifespan(userRepository)) {
            ListSnapshot<User> users = userRepository.getSnapshot();
            if (users.values().size() == 1) {
                User user = users.values().get(0);
                if (INITIAL_USER_NAME.equals(user.name()) && user.credentials().matchesPassword(UTF8.encode(INITIAL_PASSWORD))) {
                    // We allow overwriting an unmodified default neo4j user
                    result = false;
                }
            }
        } catch (IOException e) {
        // Do not allow overwriting if we had a problem reading the file
        }
    }
    return result;
}
Also used : Path(java.nio.file.Path) FileUserRepository(org.neo4j.server.security.auth.FileUserRepository) User(org.neo4j.kernel.impl.security.User) IOException(java.io.IOException) Lifespan(org.neo4j.kernel.lifecycle.Lifespan)

Aggregations

Lifespan (org.neo4j.kernel.lifecycle.Lifespan)53 Test (org.junit.jupiter.api.Test)21 Test (org.junit.Test)16 LogFiles (org.neo4j.kernel.impl.transaction.log.files.LogFiles)8 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)8 Path (java.nio.file.Path)7 SimpleTransactionIdStore (org.neo4j.kernel.impl.transaction.SimpleTransactionIdStore)7 LogPosition (org.neo4j.kernel.impl.transaction.log.LogPosition)7 LogFile (org.neo4j.kernel.impl.transaction.log.files.LogFile)7 JobScheduler (org.neo4j.scheduler.JobScheduler)7 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)6 File (java.io.File)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 StoreFileChannel (org.neo4j.io.fs.StoreFileChannel)5 PageCache (org.neo4j.io.pagecache.PageCache)5 SimpleLogVersionRepository (org.neo4j.kernel.impl.transaction.SimpleLogVersionRepository)5 IOException (java.io.IOException)4 StoreChannel (org.neo4j.io.fs.StoreChannel)4 NodeStore (org.neo4j.kernel.impl.store.NodeStore)4 ArrayList (java.util.ArrayList)3