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