Search in sources :

Example 1 with ReleaseSegment

use of io.pravega.client.stream.impl.ReaderGroupState.ReleaseSegment 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)1 ReinitializationRequiredException (io.pravega.client.stream.ReinitializationRequiredException)1 AcquireSegment (io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment)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