Search in sources :

Example 1 with EventRead

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());
}
Also used : EventStreamWriter(io.pravega.client.stream.EventStreamWriter) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) Cleanup(lombok.Cleanup) ReaderGroup(io.pravega.client.stream.ReaderGroup) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) ServiceBuilderConfig(io.pravega.segmentstore.server.store.ServiceBuilderConfig) EventRead(io.pravega.client.stream.EventRead) EndOfDataNotification(io.pravega.client.stream.notifications.EndOfDataNotification) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) Stream(io.pravega.client.stream.Stream) Map(java.util.Map) After(org.junit.After) URI(java.net.URI) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) Executors(java.util.concurrent.Executors) Slf4j(lombok.extern.slf4j.Slf4j) ClientFactory(io.pravega.client.ClientFactory) TestUtils(io.pravega.test.common.TestUtils) Controller(io.pravega.client.stream.impl.Controller) ControllerWrapper(io.pravega.test.integration.demo.ControllerWrapper) BeforeClass(org.junit.BeforeClass) StreamImpl(io.pravega.client.stream.impl.StreamImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) SegmentNotification(io.pravega.client.stream.notifications.SegmentNotification) TestingServerStarter(io.pravega.test.common.TestingServerStarter) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TestingServer(org.apache.curator.test.TestingServer) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) ReusableLatch(io.pravega.common.util.ReusableLatch) Before(org.junit.Before) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) Listener(io.pravega.client.stream.notifications.Listener) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) EventStreamReader(io.pravega.client.stream.EventStreamReader) Test(org.junit.Test) Assert.assertNull(org.junit.Assert.assertNull) ConnectionFactoryImpl(io.pravega.client.netty.impl.ConnectionFactoryImpl) ReaderConfig(io.pravega.client.stream.ReaderConfig) Collections(java.util.Collections) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) Assert.assertEquals(org.junit.Assert.assertEquals) ClientConfig(io.pravega.client.ClientConfig) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) HashMap(java.util.HashMap) ReaderGroup(io.pravega.client.stream.ReaderGroup) ClientFactory(io.pravega.client.ClientFactory) Controller(io.pravega.client.stream.impl.Controller) Cleanup(lombok.Cleanup) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) EndOfDataNotification(io.pravega.client.stream.notifications.EndOfDataNotification) StreamImpl(io.pravega.client.stream.impl.StreamImpl) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) Stream(io.pravega.client.stream.Stream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) ConnectionFactoryImpl(io.pravega.client.netty.impl.ConnectionFactoryImpl) Test(org.junit.Test)

Example 2 with EventRead

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());
}
Also used : EventStreamWriter(io.pravega.client.stream.EventStreamWriter) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) Cleanup(lombok.Cleanup) ReaderGroup(io.pravega.client.stream.ReaderGroup) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) ServiceBuilderConfig(io.pravega.segmentstore.server.store.ServiceBuilderConfig) EventRead(io.pravega.client.stream.EventRead) EndOfDataNotification(io.pravega.client.stream.notifications.EndOfDataNotification) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) Stream(io.pravega.client.stream.Stream) Map(java.util.Map) After(org.junit.After) URI(java.net.URI) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) Executors(java.util.concurrent.Executors) Slf4j(lombok.extern.slf4j.Slf4j) ClientFactory(io.pravega.client.ClientFactory) TestUtils(io.pravega.test.common.TestUtils) Controller(io.pravega.client.stream.impl.Controller) ControllerWrapper(io.pravega.test.integration.demo.ControllerWrapper) BeforeClass(org.junit.BeforeClass) StreamImpl(io.pravega.client.stream.impl.StreamImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) SegmentNotification(io.pravega.client.stream.notifications.SegmentNotification) TestingServerStarter(io.pravega.test.common.TestingServerStarter) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TestingServer(org.apache.curator.test.TestingServer) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) ReusableLatch(io.pravega.common.util.ReusableLatch) Before(org.junit.Before) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) Listener(io.pravega.client.stream.notifications.Listener) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) EventStreamReader(io.pravega.client.stream.EventStreamReader) Test(org.junit.Test) Assert.assertNull(org.junit.Assert.assertNull) ConnectionFactoryImpl(io.pravega.client.netty.impl.ConnectionFactoryImpl) ReaderConfig(io.pravega.client.stream.ReaderConfig) Collections(java.util.Collections) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) Assert.assertEquals(org.junit.Assert.assertEquals) ClientConfig(io.pravega.client.ClientConfig) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) HashMap(java.util.HashMap) ReaderGroup(io.pravega.client.stream.ReaderGroup) ClientFactory(io.pravega.client.ClientFactory) Controller(io.pravega.client.stream.impl.Controller) Cleanup(lombok.Cleanup) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) StreamImpl(io.pravega.client.stream.impl.StreamImpl) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) Stream(io.pravega.client.stream.Stream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SegmentNotification(io.pravega.client.stream.notifications.SegmentNotification) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) ConnectionFactoryImpl(io.pravega.client.netty.impl.ConnectionFactoryImpl) Test(org.junit.Test)

Example 3 with EventRead

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);
}
Also used : EventRead(io.pravega.client.stream.EventRead) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Checkpoint(io.pravega.client.stream.Checkpoint)

Example 4 with EventRead

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;
}
Also used : EventRead(io.pravega.client.stream.EventRead) ReinitializationRequiredException(io.pravega.client.stream.ReinitializationRequiredException) ArrayList(java.util.ArrayList) ClientFactory(io.pravega.client.ClientFactory)

Example 5 with EventRead

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());
}
Also used : IntStream(java.util.stream.IntStream) EventStreamWriter(io.pravega.client.stream.EventStreamWriter) StreamManager(io.pravega.client.admin.StreamManager) RunWith(org.junit.runner.RunWith) Cleanup(lombok.Cleanup) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) ReaderGroup(io.pravega.client.stream.ReaderGroup) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) Supplier(java.util.function.Supplier) EventRead(io.pravega.client.stream.EventRead) Service(io.pravega.test.system.framework.services.Service) ArrayList(java.util.ArrayList) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Timeout(org.junit.rules.Timeout) Checkpoint(io.pravega.client.stream.Checkpoint) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) Utils(io.pravega.test.system.framework.Utils) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) Before(org.junit.Before) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) Environment(io.pravega.test.system.framework.Environment) Assert.assertTrue(org.junit.Assert.assertTrue) EventStreamReader(io.pravega.client.stream.EventStreamReader) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Rule(org.junit.Rule) ClientFactory(io.pravega.client.ClientFactory) ReaderConfig(io.pravega.client.stream.ReaderConfig) ReinitializationRequiredException(io.pravega.client.stream.ReinitializationRequiredException) IntSummaryStatistics(java.util.IntSummaryStatistics) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) Futures(io.pravega.common.concurrent.Futures) SystemTestRunner(io.pravega.test.system.framework.SystemTestRunner) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) IntStream(java.util.stream.IntStream) Stream(java.util.stream.Stream) IntSummaryStatistics(java.util.IntSummaryStatistics)

Aggregations

EventRead (io.pravega.client.stream.EventRead)5 ClientFactory (io.pravega.client.ClientFactory)4 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)3 EventStreamReader (io.pravega.client.stream.EventStreamReader)3 EventStreamWriter (io.pravega.client.stream.EventStreamWriter)3 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)3 ReaderConfig (io.pravega.client.stream.ReaderConfig)3 ReaderGroup (io.pravega.client.stream.ReaderGroup)3 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)3 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)3 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)3 JavaSerializer (io.pravega.client.stream.impl.JavaSerializer)3 URI (java.net.URI)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 Cleanup (lombok.Cleanup)3 ClientConfig (io.pravega.client.ClientConfig)2 ReaderGroupManagerImpl (io.pravega.client.admin.impl.ReaderGroupManagerImpl)2 ConnectionFactory (io.pravega.client.netty.impl.ConnectionFactory)2 ConnectionFactoryImpl (io.pravega.client.netty.impl.ConnectionFactoryImpl)2 ReinitializationRequiredException (io.pravega.client.stream.ReinitializationRequiredException)2