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 MultiSegmentStoreTest method testReadWrite.
private void testReadWrite() {
List<URI> ctlURIs = this.controllerInstance.getServiceDetails();
URI controllerUri = ctlURIs.get(0);
String scope = "testscope" + RandomStringUtils.randomAlphanumeric(10);
String stream = "teststream" + RandomStringUtils.randomAlphanumeric(10);
@Cleanup StreamManager streamManager = StreamManager.create(ClientConfig.builder().controllerURI(controllerUri).build());
Assert.assertTrue(streamManager.createScope(scope));
// Create stream with large number of segments so that most segment containers are used.
Assert.assertTrue(streamManager.createStream(scope, stream, StreamConfiguration.builder().scope(scope).streamName(stream).scalingPolicy(ScalingPolicy.fixed(10)).build()));
@Cleanup ClientFactory clientFactory = ClientFactory.withScope(scope, ClientConfig.builder().controllerURI(controllerUri).build());
log.info("Invoking writer with controller URI: {}", controllerUri);
@Cleanup EventStreamWriter<Serializable> writer = clientFactory.createEventWriter(stream, new JavaSerializer<>(), EventWriterConfig.builder().build());
final int numEvents = 1000;
final String fixedEvent = "testevent";
for (int i = 0; i < numEvents; i++) {
log.debug("Producing event: {} ", fixedEvent);
writer.writeEvent(String.valueOf(i), fixedEvent);
}
writer.flush();
log.info("Invoking reader with controller URI: {}", controllerUri);
final String readerGroup = "testreadergroup" + RandomStringUtils.randomAlphanumeric(10);
ReaderGroupManager groupManager = ReaderGroupManager.withScope(scope, ClientConfig.builder().controllerURI(controllerUri).build());
groupManager.createReaderGroup(readerGroup, ReaderGroupConfig.builder().disableAutomaticCheckpoints().stream(Stream.of(scope, stream)).build());
@Cleanup EventStreamReader<String> reader = clientFactory.createReader(UUID.randomUUID().toString(), readerGroup, new JavaSerializer<>(), ReaderConfig.builder().build());
for (int i = 0; i < numEvents; i++) {
try {
String event = reader.readNextEvent(60000).getEvent();
Assert.assertEquals(fixedEvent, event);
} catch (ReinitializationRequiredException e) {
log.error("Unexpected request to reinitialize {}", e);
throw new IllegalStateException("Unexpected request to reinitialize");
}
}
}
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;
}
}
});
}
use of io.pravega.client.stream.ReinitializationRequiredException in project pravega by pravega.
the class ReaderGroupStateManager method handleEndOfSegment.
/**
* Handles a segment being completed by calling the controller to gather all successors to the completed segment.
*/
void handleEndOfSegment(Segment segmentCompleted) throws ReinitializationRequiredException {
val successors = getAndHandleExceptions(controller.getSuccessors(segmentCompleted), RuntimeException::new);
synchronized (this) {
latestDelegationToken = successors.getDelegationToken();
}
AtomicBoolean reinitRequired = new AtomicBoolean(false);
sync.updateState(state -> {
if (!state.isReaderOnline(readerId)) {
reinitRequired.set(true);
return null;
}
return Collections.singletonList(new SegmentCompleted(readerId, segmentCompleted, successors.getSegmentToPredecessor()));
});
if (reinitRequired.get()) {
throw new ReinitializationRequiredException();
}
acquireTimer.zero();
}
use of io.pravega.client.stream.ReinitializationRequiredException in project pravega by pravega.
the class ReaderGroupStateManager method getCheckpoint.
String getCheckpoint() throws ReinitializationRequiredException {
fetchUpdatesIfNeeded();
ReaderGroupState state = sync.getState();
long automaticCpInterval = state.getConfig().getAutomaticCheckpointIntervalMillis();
if (!state.isReaderOnline(readerId)) {
throw new ReinitializationRequiredException();
}
String checkpoint = state.getCheckpointForReader(readerId);
if (checkpoint != null) {
checkpointTimer.reset(Duration.ofMillis(automaticCpInterval));
return checkpoint;
}
if (automaticCpInterval <= 0 || checkpointTimer.hasRemaining() || state.hasOngoingCheckpoint()) {
return null;
}
sync.updateState(s -> s.hasOngoingCheckpoint() ? null : ImmutableList.of(new CreateCheckpoint()));
checkpointTimer.reset(Duration.ofMillis(automaticCpInterval));
return state.getCheckpointForReader(readerId);
}
Aggregations