use of io.pravega.client.stream.ReinitializationRequiredException 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.ReinitializationRequiredException 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);
}
use of io.pravega.client.stream.ReinitializationRequiredException in project pravega by pravega.
the class ReadWriteTest method startNewReader.
private CompletableFuture<Void> startNewReader(final String id, final ClientFactory clientFactory, final String readerGroupName, final ConcurrentLinkedQueue<Long> readResult, final AtomicLong writeCount, final AtomicLong readCount, final AtomicBoolean exitFlag) {
return CompletableFuture.runAsync(() -> {
final EventStreamReader<Long> reader = clientFactory.createReader(id, readerGroupName, new JavaSerializer<Long>(), ReaderConfig.builder().build());
while (!(exitFlag.get() && readCount.get() == writeCount.get())) {
try {
final Long longEvent = reader.readNextEvent(SECONDS.toMillis(2)).getEvent();
log.info("Reading event {}", longEvent);
if (longEvent != null) {
// update if event read is not null.
readResult.add(longEvent);
readCount.incrementAndGet();
}
} catch (ReinitializationRequiredException e) {
log.warn("Test Exception while reading from the stream", e);
break;
}
}
log.info("Closing reader {}", reader);
reader.close();
});
}
use of io.pravega.client.stream.ReinitializationRequiredException in project pravega by pravega.
the class MultiReadersEndToEndTest method readAllEvents.
private Collection<Integer> readAllEvents(final int numParallelReaders, ClientFactory clientFactory, final String readerGroupName, final int numSegments) {
ConcurrentLinkedQueue<Integer> read = new ConcurrentLinkedQueue<>();
final ExecutorService executorService = Executors.newFixedThreadPool(numParallelReaders, new ThreadFactoryBuilder().setNameFormat("testreader-pool-%d").build());
List<Future<?>> futures = new ArrayList<>();
for (int i = 0; i < numParallelReaders; i++) {
futures.add(executorService.submit(() -> {
final String readerId = UUID.randomUUID().toString();
@Cleanup final EventStreamReader<Integer> reader = clientFactory.createReader(readerId, readerGroupName, new IntegerSerializer(), ReaderConfig.builder().build());
int emptyCount = 0;
while (emptyCount <= numSegments) {
try {
final Integer integerEventRead = reader.readNextEvent(100).getEvent();
if (integerEventRead != null) {
read.add(integerEventRead);
emptyCount = 0;
} else {
emptyCount++;
}
} catch (ReinitializationRequiredException e) {
throw new RuntimeException(e);
}
}
}));
}
// Wait until all readers are done.
futures.forEach(f -> Futures.getAndHandleExceptions(f, RuntimeException::new));
executorService.shutdownNow();
return read;
}
use of io.pravega.client.stream.ReinitializationRequiredException in project pravega by pravega.
the class ReadWithAutoScaleTest method startReader.
private CompletableFuture<Void> startReader(final String id, final ClientFactory clientFactory, final String readerGroupName, final ConcurrentLinkedQueue<Long> readResult, final AtomicLong writeCount, final AtomicLong readCount, final AtomicBoolean exitFlag) {
return CompletableFuture.runAsync(() -> {
@Cleanup final EventStreamReader<Long> reader = clientFactory.createReader(id, readerGroupName, new JavaSerializer<Long>(), ReaderConfig.builder().build());
long count;
while (!(exitFlag.get() && readCount.get() == writeCount.get())) {
// exit only if exitFlag is true and read Count equals write count.
try {
final Long longEvent = reader.readNextEvent(SECONDS.toMillis(60)).getEvent();
if (longEvent != null) {
// update if event read is not null.
readResult.add(longEvent);
count = readCount.incrementAndGet();
log.debug("Reader {}, read count {}", id, count);
} else {
log.debug("Null event, reader {}, read count {}", id, readCount.get());
}
} catch (ReinitializationRequiredException e) {
log.warn("Test Exception while reading from the stream", e);
break;
}
}
});
}
Aggregations