Search in sources :

Example 1 with AcquireSegment

use of io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment in project pravega by pravega.

the class ReaderGroupStateManager method acquireSegment.

private Map<Segment, Long> acquireSegment(long timeLag) throws ReinitializationRequiredException {
    AtomicReference<Map<Segment, Long>> result = new AtomicReference<>();
    AtomicBoolean reinitRequired = new AtomicBoolean(false);
    sync.updateState(state -> {
        result.set(Collections.emptyMap());
        if (!state.isReaderOnline(readerId)) {
            reinitRequired.set(true);
            return null;
        }
        if (state.getCheckpointForReader(readerId) != null) {
            return null;
        }
        int toAcquire = calculateNumSegmentsToAcquire(state);
        if (toAcquire == 0) {
            return null;
        }
        Map<Segment, Long> unassignedSegments = state.getUnassignedSegments();
        Map<Segment, Long> acquired = new HashMap<>(toAcquire);
        List<ReaderGroupStateUpdate> updates = new ArrayList<>(toAcquire);
        Iterator<Entry<Segment, Long>> iter = unassignedSegments.entrySet().iterator();
        for (int i = 0; i < toAcquire; i++) {
            assert iter.hasNext();
            Entry<Segment, Long> segment = iter.next();
            acquired.put(segment.getKey(), segment.getValue());
            updates.add(new AcquireSegment(readerId, segment.getKey()));
        }
        updates.add(new UpdateDistanceToTail(readerId, timeLag));
        result.set(acquired);
        return updates;
    });
    if (reinitRequired.get()) {
        throw new ReinitializationRequiredException();
    }
    releaseTimer.reset(calculateReleaseTime(readerId, sync.getState()));
    acquireTimer.reset(calculateAcquireTime(readerId, sync.getState()));
    return result.get();
}
Also used : HashMap(java.util.HashMap) AcquireSegment(io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) CreateCheckpoint(io.pravega.client.stream.impl.ReaderGroupState.CreateCheckpoint) ReleaseSegment(io.pravega.client.stream.impl.ReaderGroupState.ReleaseSegment) Segment(io.pravega.client.segment.impl.Segment) AcquireSegment(io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment) ReaderGroupStateUpdate(io.pravega.client.stream.impl.ReaderGroupState.ReaderGroupStateUpdate) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Entry(java.util.Map.Entry) ReinitializationRequiredException(io.pravega.client.stream.ReinitializationRequiredException) UpdateDistanceToTail(io.pravega.client.stream.impl.ReaderGroupState.UpdateDistanceToTail) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Segment (io.pravega.client.segment.impl.Segment)1 ReinitializationRequiredException (io.pravega.client.stream.ReinitializationRequiredException)1 AcquireSegment (io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment)1 CreateCheckpoint (io.pravega.client.stream.impl.ReaderGroupState.CreateCheckpoint)1 ReaderGroupStateUpdate (io.pravega.client.stream.impl.ReaderGroupState.ReaderGroupStateUpdate)1 ReleaseSegment (io.pravega.client.stream.impl.ReaderGroupState.ReleaseSegment)1 UpdateDistanceToTail (io.pravega.client.stream.impl.ReaderGroupState.UpdateDistanceToTail)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1