Search in sources :

Example 1 with IdRange

use of org.neo4j.internal.id.IdRange in project neo4j by neo4j.

the class IndexedIdGeneratorTest method shouldAllocateConsecutiveIdBatches.

@Test
void shouldAllocateConsecutiveIdBatches() {
    // given
    AtomicInteger numAllocations = new AtomicInteger();
    Race race = new Race().withEndCondition(() -> numAllocations.get() >= 10_000);
    Collection<IdRange> allocations = ConcurrentHashMap.newKeySet();
    race.addContestants(4, () -> {
        int size = ThreadLocalRandom.current().nextInt(10, 1_000);
        IdRange idRange = idGenerator.nextIdBatch(size, true, NULL);
        assertEquals(0, idRange.getDefragIds().length);
        assertEquals(size, idRange.getRangeLength());
        allocations.add(idRange);
        numAllocations.incrementAndGet();
    });
    // when
    race.goUnchecked();
    // then
    IdRange[] sortedAllocations = allocations.toArray(new IdRange[allocations.size()]);
    Arrays.sort(sortedAllocations, Comparator.comparingLong(IdRange::getRangeStart));
    long prevEndExclusive = 0;
    for (IdRange allocation : sortedAllocations) {
        assertEquals(prevEndExclusive, allocation.getRangeStart());
        prevEndExclusive = allocation.getRangeStart() + allocation.getRangeLength();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Race(org.neo4j.test.Race) IdRange(org.neo4j.internal.id.IdRange) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with IdRange

use of org.neo4j.internal.id.IdRange in project neo4j by neo4j.

the class BatchingIdGetter method nextId.

@Override
public long nextId(CursorContext cursorContext) {
    long id;
    if (batch == null || (id = batch.nextId(cursorContext)) == VALUE_REPRESENTING_NULL) {
        idExpectation = NO_ID_EXPECTATION;
        IdRange idRange = source.nextIdBatch(batchSize, true, cursorContext);
        while (IdValidator.hasReservedIdInRange(idRange.getRangeStart(), idRange.getRangeStart() + idRange.getRangeLength())) {
            idRange = source.nextIdBatch(batchSize, true, cursorContext);
        }
        batch = new IdRangeIterator(idRange);
        id = batch.nextId(cursorContext);
    }
    if (idExpectation != NO_ID_EXPECTATION) {
        if (id != idExpectation) {
            throw new IllegalStateException(format("Id generator allocated range with non-consecutive IDs, expected:%d, but got:%d", idExpectation, id));
        }
    }
    idExpectation = id + 1;
    return id;
}
Also used : IdRangeIterator(org.neo4j.internal.id.IdRangeIterator) IdRange(org.neo4j.internal.id.IdRange)

Example 3 with IdRange

use of org.neo4j.internal.id.IdRange in project neo4j by neo4j.

the class IndexedIdGeneratorTest method shouldNotAllocateReservedIdsInBatchedAllocation.

@ValueSource(booleans = { true, false })
@ParameterizedTest
void shouldNotAllocateReservedIdsInBatchedAllocation(boolean consecutive) throws IOException {
    // given
    idGenerator.start(NO_FREE_IDS, NULL);
    idGenerator.setHighId(IdValidator.INTEGER_MINUS_ONE - 100);
    // when
    IdRange batch = idGenerator.nextIdBatch(200, consecutive, NULL);
    // then
    assertFalse(IdValidator.hasReservedIdInRange(batch.getRangeStart(), batch.getRangeStart() + batch.getRangeLength()));
    for (long defragId : batch.getDefragIds()) {
        assertFalse(IdValidator.isReservedId(defragId));
    }
}
Also used : IdRange(org.neo4j.internal.id.IdRange) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

IdRange (org.neo4j.internal.id.IdRange)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.jupiter.api.Test)1 ValueSource (org.junit.jupiter.params.provider.ValueSource)1 IdRangeIterator (org.neo4j.internal.id.IdRangeIterator)1 Race (org.neo4j.test.Race)1