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();
}
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);
}
Aggregations