use of io.pravega.client.stream.EventRead in project pravega by pravega.
the class ReaderGroupNotificationTest method testEndOfStreamNotifications.
@Test(timeout = 40000)
public void testEndOfStreamNotifications() throws Exception {
final String streamName = "stream2";
StreamConfiguration config = StreamConfiguration.builder().scope(SCOPE).streamName(streamName).scalingPolicy(ScalingPolicy.byEventRate(10, 2, 1)).build();
Controller controller = controllerWrapper.getController();
controllerWrapper.getControllerService().createScope(SCOPE).get();
controller.createStream(config).get();
@Cleanup ConnectionFactory connectionFactory = new ConnectionFactoryImpl(ClientConfig.builder().controllerURI(URI.create("tcp://localhost")).build());
@Cleanup ClientFactory clientFactory = new ClientFactoryImpl(SCOPE, controller, connectionFactory);
@Cleanup EventStreamWriter<String> writer = clientFactory.createEventWriter(streamName, new JavaSerializer<>(), EventWriterConfig.builder().build());
writer.writeEvent("0", "data1").get();
// scale
Stream stream = new StreamImpl(SCOPE, streamName);
Map<Double, Double> map = new HashMap<>();
map.put(0.0, 0.5);
map.put(0.5, 1.0);
Boolean result = controller.scaleStream(stream, Collections.singletonList(0), map, executor).getFuture().get();
assertTrue(result);
writer.writeEvent("0", "data2").get();
// seal stream
assertTrue(controller.sealStream(SCOPE, streamName).get());
@Cleanup ReaderGroupManager groupManager = new ReaderGroupManagerImpl(SCOPE, controller, clientFactory, connectionFactory);
ReaderGroup readerGroup = groupManager.createReaderGroup("reader", ReaderGroupConfig.builder().disableAutomaticCheckpoints().stream(Stream.of(SCOPE, streamName)).build());
@Cleanup EventStreamReader<String> reader1 = clientFactory.createReader("readerId", "reader", new JavaSerializer<>(), ReaderConfig.builder().build());
// Add segment event listener
Listener<EndOfDataNotification> l1 = notification -> {
listenerInvoked.set(true);
listenerLatch.release();
};
readerGroup.getEndOfDataNotifier(executor).registerListener(l1);
EventRead<String> event1 = reader1.readNextEvent(10000);
EventRead<String> event2 = reader1.readNextEvent(10000);
EventRead<String> event3 = reader1.readNextEvent(10000);
assertNotNull(event1);
assertEquals("data1", event1.getEvent());
assertNotNull(event2);
assertEquals("data2", event2.getEvent());
assertNull(event3.getEvent());
listenerLatch.await();
assertTrue("Listener invoked", listenerInvoked.get());
}
use of io.pravega.client.stream.EventRead in project pravega by pravega.
the class ReaderGroupNotificationTest method testSegmentNotifications.
@Test(timeout = 40000)
public void testSegmentNotifications() throws Exception {
final String streamName = "stream1";
StreamConfiguration config = StreamConfiguration.builder().scope(SCOPE).streamName(streamName).scalingPolicy(ScalingPolicy.byEventRate(10, 2, 1)).build();
Controller controller = controllerWrapper.getController();
controllerWrapper.getControllerService().createScope(SCOPE).get();
controller.createStream(config).get();
@Cleanup ConnectionFactory connectionFactory = new ConnectionFactoryImpl(ClientConfig.builder().controllerURI(URI.create("tcp://localhost")).build());
@Cleanup ClientFactory clientFactory = new ClientFactoryImpl(SCOPE, controller, connectionFactory);
@Cleanup EventStreamWriter<String> writer = clientFactory.createEventWriter(streamName, new JavaSerializer<>(), EventWriterConfig.builder().build());
writer.writeEvent("0", "data1").get();
// scale
Stream stream = new StreamImpl(SCOPE, streamName);
Map<Double, Double> map = new HashMap<>();
map.put(0.0, 0.5);
map.put(0.5, 1.0);
Boolean result = controller.scaleStream(stream, Collections.singletonList(0), map, executor).getFuture().get();
assertTrue(result);
writer.writeEvent("0", "data2").get();
@Cleanup ReaderGroupManager groupManager = new ReaderGroupManagerImpl(SCOPE, controller, clientFactory, connectionFactory);
ReaderGroup readerGroup = groupManager.createReaderGroup("reader", ReaderGroupConfig.builder().disableAutomaticCheckpoints().stream(Stream.of(SCOPE, streamName)).build());
@Cleanup EventStreamReader<String> reader1 = clientFactory.createReader("readerId", "reader", new JavaSerializer<>(), ReaderConfig.builder().build());
// Add segment event listener
Listener<SegmentNotification> l1 = notification -> {
listenerInvoked.set(true);
numberOfReaders.set(notification.getNumOfReaders());
numberOfSegments.set(notification.getNumOfSegments());
listenerLatch.release();
};
readerGroup.getSegmentNotifier(executor).registerListener(l1);
EventRead<String> event1 = reader1.readNextEvent(15000);
EventRead<String> event2 = reader1.readNextEvent(15000);
assertNotNull(event1);
assertEquals("data1", event1.getEvent());
assertNotNull(event2);
assertEquals("data2", event2.getEvent());
listenerLatch.await();
assertTrue("Listener invoked", listenerInvoked.get());
assertEquals(2, numberOfSegments.get());
assertEquals(1, numberOfReaders.get());
}
use of io.pravega.client.stream.EventRead in project pravega by pravega.
the class ReaderCheckpointTest method readEventsAndVerify.
private void readEventsAndVerify(int startInclusive, int endExclusive) {
log.info("Read and Verify events between [{},{})", startInclusive, endExclusive);
final List<CompletableFuture<List<EventRead<Integer>>>> readResults = new ArrayList<>();
// start reading using configured number of readers
for (int i = 0; i < NUMBER_OF_READERS; i++) {
readResults.add(asyncReadEvents("reader-" + i));
}
// results from all readers
List<List<EventRead<Integer>>> results = Futures.allOfWithResults(readResults).join();
List<EventRead<Integer>> eventsRead = results.stream().flatMap(List::stream).collect(Collectors.toList());
verifyEvents(eventsRead, startInclusive, endExclusive);
}
use of io.pravega.client.stream.EventRead in project pravega by pravega.
the class ReaderCheckpointTest method readEvents.
private <T extends Serializable> List<EventRead<T>> readEvents(final String readerId) {
List<EventRead<T>> events = new ArrayList<>();
try (ClientFactory clientFactory = ClientFactory.withScope(SCOPE, controllerURI);
EventStreamReader<T> reader = clientFactory.createReader(readerId, READER_GROUP_NAME, new JavaSerializer<T>(), readerConfig)) {
log.info("Reading events from {}/{} with readerId: {}", SCOPE, STREAM, readerId);
EventRead<T> event = null;
do {
try {
event = reader.readNextEvent(READ_TIMEOUT);
if (event.getEvent() != null) {
log.info("Read event {}", event.getEvent());
events.add(event);
}
if (event.isCheckpoint()) {
log.info("Read a check point event, checkpointName: {}", event.getCheckpointName());
}
} catch (ReinitializationRequiredException e) {
log.error("Exception while reading event using readerId: {}", readerId, e);
fail("Reinitialization Exception is not expected");
}
} while (event.isCheckpoint() || event.getEvent() != null);
// stop reading if event read(non-checkpoint) is null.
log.info("No more events from {}/{} for readerId: {}", SCOPE, STREAM, readerId);
}
// reader.close() will automatically invoke ReaderGroup#readerOffline(String, Position)
return events;
}
use of io.pravega.client.stream.EventRead in project pravega by pravega.
the class ReaderCheckpointTest method verifyEvents.
private void verifyEvents(final List<EventRead<Integer>> events, int startInclusive, int endExclusive) {
Supplier<Stream<Integer>> streamSupplier = () -> events.stream().map(i -> i.getEvent()).sorted();
IntSummaryStatistics stats = streamSupplier.get().collect(Collectors.summarizingInt(value -> value));
assertTrue(String.format("Check for first event: %d, %d", stats.getMin(), startInclusive), stats.getMin() == startInclusive);
assertTrue(String.format("Check for last event: %d, %d", stats.getMax(), endExclusive), stats.getMax() == endExclusive - 1);
// Check for missing events
assertEquals(String.format("Check for number of events: %d, %d, %d", endExclusive, startInclusive, stats.getCount()), endExclusive - startInclusive, stats.getCount());
assertEquals(String.format("Check for duplicate events: %d, %d, %d", endExclusive, startInclusive, streamSupplier.get().distinct().count()), endExclusive - startInclusive, streamSupplier.get().distinct().count());
}
Aggregations