Search in sources :

Example 1 with ReaderGroupStateUpdate

use of io.pravega.client.stream.impl.ReaderGroupState.ReaderGroupStateUpdate 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)

Example 2 with ReaderGroupStateUpdate

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

the class ReaderGroupStateManager method releaseSegment.

/**
 * Releases a segment to another reader. This reader should no longer read from the segment.
 *
 * @param segment The segment to be released
 * @param lastOffset The offset from which the new owner should start reading from.
 * @param timeLag How far the reader is from the tail of the stream in time.
 * @return a boolean indicating if the segment was successfully released.
 * @throws ReinitializationRequiredException If the reader has been declared offline.
 */
boolean releaseSegment(Segment segment, long lastOffset, long timeLag) throws ReinitializationRequiredException {
    sync.updateState(state -> {
        Set<Segment> segments = state.getSegments(readerId);
        if (segments == null || !segments.contains(segment) || state.getCheckpointForReader(readerId) != null || !doesReaderOwnTooManySegments(state)) {
            return null;
        }
        List<ReaderGroupStateUpdate> result = new ArrayList<>(2);
        result.add(new ReleaseSegment(readerId, segment, lastOffset));
        result.add(new UpdateDistanceToTail(readerId, timeLag));
        return result;
    });
    ReaderGroupState state = sync.getState();
    releaseTimer.reset(calculateReleaseTime(readerId, state));
    acquireTimer.reset(calculateAcquireTime(readerId, state));
    if (!state.isReaderOnline(readerId)) {
        throw new ReinitializationRequiredException();
    }
    return !state.getSegments(readerId).contains(segment);
}
Also used : ReaderGroupStateUpdate(io.pravega.client.stream.impl.ReaderGroupState.ReaderGroupStateUpdate) ReleaseSegment(io.pravega.client.stream.impl.ReaderGroupState.ReleaseSegment) ReinitializationRequiredException(io.pravega.client.stream.ReinitializationRequiredException) UpdateDistanceToTail(io.pravega.client.stream.impl.ReaderGroupState.UpdateDistanceToTail) ArrayList(java.util.ArrayList) ReleaseSegment(io.pravega.client.stream.impl.ReaderGroupState.ReleaseSegment) Segment(io.pravega.client.segment.impl.Segment) AcquireSegment(io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment)

Aggregations

Segment (io.pravega.client.segment.impl.Segment)2 ReinitializationRequiredException (io.pravega.client.stream.ReinitializationRequiredException)2 AcquireSegment (io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment)2 ReaderGroupStateUpdate (io.pravega.client.stream.impl.ReaderGroupState.ReaderGroupStateUpdate)2 ReleaseSegment (io.pravega.client.stream.impl.ReaderGroupState.ReleaseSegment)2 UpdateDistanceToTail (io.pravega.client.stream.impl.ReaderGroupState.UpdateDistanceToTail)2 ArrayList (java.util.ArrayList)2 CreateCheckpoint (io.pravega.client.stream.impl.ReaderGroupState.CreateCheckpoint)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