Search in sources :

Example 6 with Marker

use of org.neo4j.internal.id.IdGenerator.Marker in project neo4j by neo4j.

the class IndexedIdGeneratorTest method shouldInvokeMonitorOnCorrectCalls.

@Test
void shouldInvokeMonitorOnCorrectCalls() throws IOException {
    stop();
    IndexedIdGenerator.Monitor monitor = mock(IndexedIdGenerator.Monitor.class);
    idGenerator = new IndexedIdGenerator(pageCache, file, immediate(), IdType.LABEL_TOKEN, false, () -> 0, MAX_ID, writable(), Config.defaults(), NULL, monitor, DEFAULT_DATABASE_NAME, immutable.empty());
    verify(monitor).opened(-1, 0);
    idGenerator.start(NO_FREE_IDS, NULL);
    long allocatedHighId = idGenerator.nextId(NULL);
    verify(monitor).allocatedFromHigh(allocatedHighId);
    try (Marker marker = idGenerator.marker(NULL)) {
        marker.markUsed(allocatedHighId);
        verify(monitor).markedAsUsed(allocatedHighId);
        marker.markDeleted(allocatedHighId);
        verify(monitor).markedAsDeleted(allocatedHighId);
        marker.markFree(allocatedHighId);
        verify(monitor).markedAsFree(allocatedHighId);
    }
    long reusedId = idGenerator.nextId(NULL);
    verify(monitor).allocatedFromReused(reusedId);
    idGenerator.checkpoint(NULL);
    // two times, one in start and one now in checkpoint
    verify(monitor, times(2)).checkpoint(anyLong(), anyLong());
    idGenerator.clearCache(NULL);
    verify(monitor).clearingCache();
    verify(monitor).clearedCache();
    try (Marker marker = idGenerator.marker(NULL)) {
        marker.markUsed(allocatedHighId + 3);
        verify(monitor).bridged(allocatedHighId + 1);
        verify(monitor).bridged(allocatedHighId + 2);
    }
    idGenerator.close();
    verify(monitor).close();
    // Also test normalization (which requires a restart)
    idGenerator = new IndexedIdGenerator(pageCache, file, immediate(), IdType.LABEL_TOKEN, false, () -> 0, MAX_ID, writable(), Config.defaults(), NULL, monitor, DEFAULT_DATABASE_NAME, immutable.empty());
    idGenerator.start(NO_FREE_IDS, NULL);
    try (Marker marker = idGenerator.marker(NULL)) {
        marker.markUsed(allocatedHighId + 1);
    }
    verify(monitor).normalized(0);
    idGenerator.close();
    idGenerator = null;
}
Also used : Marker(org.neo4j.internal.id.IdGenerator.Marker) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with Marker

use of org.neo4j.internal.id.IdGenerator.Marker in project neo4j by neo4j.

the class LargeFreelistCreationDeletionIT method shouldAlternateLargeCreationsAndDeletionsAndNotLoseIds.

@Test
void shouldAlternateLargeCreationsAndDeletionsAndNotLoseIds() throws Throwable {
    long[][] allocatedIds = new long[THREADS][];
    for (int i = 0; i < THREADS; i++) {
        allocatedIds[i] = new long[ALLOCATIONS_PER_THREAD];
    }
    for (int r = 0; r < 3; r++) {
        // Create
        try (var freelist = new IndexedIdGenerator(pageCache, directory.file("file.id"), immediate(), IdType.NODE, false, () -> 0, Long.MAX_VALUE, writable(), Config.defaults(), DEFAULT_DATABASE_NAME, NULL)) {
            // Make sure ID cache is filled so that initial allocations won't slide highId unnecessarily.
            freelist.maintenance(NULL);
            Race race = new Race();
            WorkSync<IndexedIdGenerator, Ids> workSync = new WorkSync<>(freelist);
            for (int t = 0; t < THREADS; t++) {
                int thread = t;
                race.addContestant(throwing(() -> {
                    int cursor = 0;
                    for (int i = 0; i < TRANSACTIONS_PER_THREAD; i++) {
                        long[] txIds = new long[ALLOCATIONS_PER_TRANSACTION];
                        for (int a = 0; a < txIds.length; a++) {
                            long id = freelist.nextId(NULL);
                            allocatedIds[thread][cursor++] = id;
                            txIds[a] = id;
                        }
                        workSync.apply(new Ids(txIds));
                        Thread.sleep(1);
                    }
                }), 1);
            }
            race.go();
            assertAllUnique(allocatedIds);
            // Delete
            try (Marker marker = freelist.marker(NULL)) {
                for (long[] perThread : allocatedIds) {
                    for (long id : perThread) {
                        marker.markDeleted(id);
                    }
                }
            }
            // Checkpoint
            freelist.checkpoint(NULL);
            System.out.println(freelist.getHighId());
        }
    }
}
Also used : WorkSync(org.neo4j.util.concurrent.WorkSync) Race(org.neo4j.test.Race) Marker(org.neo4j.internal.id.IdGenerator.Marker) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)7 Marker (org.neo4j.internal.id.IdGenerator.Marker)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 Barrier (org.neo4j.test.Barrier)2 OtherThreadExecutor (org.neo4j.test.OtherThreadExecutor)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Race (org.neo4j.test.Race)1 WorkSync (org.neo4j.util.concurrent.WorkSync)1