Search in sources :

Example 1 with Checkpoint

use of io.pravega.client.stream.Checkpoint in project pravega by pravega.

the class UnreadBytesTest method testUnreadBytes.

@Test(timeout = 50000)
public void testUnreadBytes() throws Exception {
    StreamConfiguration config = StreamConfiguration.builder().scope("unreadbytes").streamName("unreadbytes").scalingPolicy(ScalingPolicy.byEventRate(10, 2, 1)).build();
    Controller controller = controllerWrapper.getController();
    controllerWrapper.getControllerService().createScope("unreadbytes").get();
    controller.createStream(config).get();
    @Cleanup ClientFactory clientFactory = ClientFactory.withScope("unreadbytes", ClientConfig.builder().controllerURI(controllerUri).build());
    @Cleanup EventStreamWriter<String> writer = clientFactory.createEventWriter("unreadbytes", new JavaSerializer<>(), EventWriterConfig.builder().build());
    @Cleanup ReaderGroupManager groupManager = ReaderGroupManager.withScope("unreadbytes", ClientConfig.builder().controllerURI(controllerUri).build());
    ReaderGroup readerGroup = groupManager.createReaderGroup("group", ReaderGroupConfig.builder().disableAutomaticCheckpoints().stream("unreadbytes/unreadbytes").build());
    @Cleanup EventStreamReader<String> reader = clientFactory.createReader("readerId", "group", new JavaSerializer<>(), ReaderConfig.builder().build());
    long unreadBytes = readerGroup.getMetrics().unreadBytes();
    assertTrue("Unread bvtes: " + unreadBytes, unreadBytes == 0);
    writer.writeEvent("0", "data of size 30").get();
    writer.writeEvent("0", "data of size 30").get();
    EventRead<String> firstEvent = reader.readNextEvent(15000);
    EventRead<String> secondEvent = reader.readNextEvent(15000);
    assertNotNull(firstEvent);
    assertEquals("data of size 30", firstEvent.getEvent());
    assertNotNull(secondEvent);
    assertEquals("data of size 30", secondEvent.getEvent());
    // trigger a checkpoint.
    CompletableFuture<Checkpoint> chkPointResult = readerGroup.initiateCheckpoint("test", executor);
    EventRead<String> chkpointEvent = reader.readNextEvent(15000);
    assertEquals("test", chkpointEvent.getCheckpointName());
    chkPointResult.join();
    unreadBytes = readerGroup.getMetrics().unreadBytes();
    assertTrue("Unread bvtes: " + unreadBytes, unreadBytes == 0);
    writer.writeEvent("0", "data of size 30").get();
    unreadBytes = readerGroup.getMetrics().unreadBytes();
    assertTrue("Unread bytes: " + unreadBytes, unreadBytes == 30);
}
Also used : ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) ReaderGroup(io.pravega.client.stream.ReaderGroup) ClientFactory(io.pravega.client.ClientFactory) Controller(io.pravega.client.stream.impl.Controller) Cleanup(lombok.Cleanup) Checkpoint(io.pravega.client.stream.Checkpoint) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) Test(org.junit.Test)

Example 2 with Checkpoint

use of io.pravega.client.stream.Checkpoint in project pravega by pravega.

the class CheckpointTest method testCheckpointAndRestore.

@Test(timeout = 20000)
public void testCheckpointAndRestore() throws ReinitializationRequiredException, InterruptedException, ExecutionException, TimeoutException {
    String endpoint = "localhost";
    String streamName = "abc";
    String readerName = "reader";
    String readerGroupName = "group";
    int port = TestUtils.getAvailableListenPort();
    String testString = "Hello world\n";
    String scope = "Scope1";
    StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
    server.startListening();
    @Cleanup MockStreamManager streamManager = new MockStreamManager(scope, endpoint, port);
    MockClientFactory clientFactory = streamManager.getClientFactory();
    ReaderGroupConfig groupConfig = ReaderGroupConfig.builder().disableAutomaticCheckpoints().stream(Stream.of(scope, streamName)).build();
    streamManager.createScope(scope);
    streamManager.createStream(scope, streamName, StreamConfiguration.builder().scope(scope).streamName(streamName).scalingPolicy(ScalingPolicy.fixed(1)).build());
    ReaderGroup readerGroup = streamManager.createReaderGroup(readerGroupName, groupConfig);
    JavaSerializer<String> serializer = new JavaSerializer<>();
    EventStreamWriter<String> producer = clientFactory.createEventWriter(streamName, serializer, EventWriterConfig.builder().build());
    producer.writeEvent(testString);
    producer.writeEvent(testString);
    producer.writeEvent(testString);
    producer.flush();
    AtomicLong clock = new AtomicLong();
    @Cleanup EventStreamReader<String> reader = clientFactory.createReader(readerName, readerGroupName, serializer, ReaderConfig.builder().build(), clock::get, clock::get);
    clock.addAndGet(CLOCK_ADVANCE_INTERVAL);
    EventRead<String> read = reader.readNextEvent(60000);
    assertEquals(testString, read.getEvent());
    clock.addAndGet(CLOCK_ADVANCE_INTERVAL);
    read = reader.readNextEvent(60000);
    assertEquals(testString, read.getEvent());
    clock.addAndGet(CLOCK_ADVANCE_INTERVAL);
    @Cleanup("shutdown") final InlineExecutor backgroundExecutor = new InlineExecutor();
    CompletableFuture<Checkpoint> checkpoint = readerGroup.initiateCheckpoint("Checkpoint", backgroundExecutor);
    assertFalse(checkpoint.isDone());
    read = reader.readNextEvent(60000);
    assertTrue(read.isCheckpoint());
    assertEquals("Checkpoint", read.getCheckpointName());
    assertNull(read.getEvent());
    clock.addAndGet(CLOCK_ADVANCE_INTERVAL);
    read = reader.readNextEvent(60000);
    assertEquals(testString, read.getEvent());
    Checkpoint cpResult = checkpoint.get(5, TimeUnit.SECONDS);
    assertTrue(checkpoint.isDone());
    assertEquals("Checkpoint", cpResult.getName());
    read = reader.readNextEvent(100);
    assertNull(read.getEvent());
    assertFalse(read.isCheckpoint());
    clock.addAndGet(CLOCK_ADVANCE_INTERVAL);
    readerGroup.resetReaderGroup(ReaderGroupConfig.builder().startFromCheckpoint(cpResult).build());
    try {
        reader.readNextEvent(60000);
        fail();
    } catch (ReinitializationRequiredException e) {
    // Expected
    }
    reader.close();
    reader = clientFactory.createReader(readerName, readerGroupName, serializer, ReaderConfig.builder().build());
    clock.addAndGet(CLOCK_ADVANCE_INTERVAL);
    read = reader.readNextEvent(60000);
    assertEquals(testString, read.getEvent());
    clock.addAndGet(CLOCK_ADVANCE_INTERVAL);
    read = reader.readNextEvent(100);
    assertNull(read.getEvent());
    assertFalse(read.isCheckpoint());
}
Also used : ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) ReaderGroup(io.pravega.client.stream.ReaderGroup) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) Checkpoint(io.pravega.client.stream.Checkpoint) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) AtomicLong(java.util.concurrent.atomic.AtomicLong) Checkpoint(io.pravega.client.stream.Checkpoint) InlineExecutor(io.pravega.test.common.InlineExecutor) ReinitializationRequiredException(io.pravega.client.stream.ReinitializationRequiredException) MockStreamManager(io.pravega.client.stream.mock.MockStreamManager) Test(org.junit.Test)

Example 3 with Checkpoint

use of io.pravega.client.stream.Checkpoint in project pravega by pravega.

the class CheckpointTest method testMoreReadersThanSegments.

@Test(timeout = 20000)
public void testMoreReadersThanSegments() throws ReinitializationRequiredException, InterruptedException, ExecutionException, TimeoutException {
    String endpoint = "localhost";
    String streamName = "abc";
    String readerGroupName = "group";
    int port = TestUtils.getAvailableListenPort();
    String testString = "Hello world\n";
    String scope = "Scope1";
    StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
    server.startListening();
    @Cleanup MockStreamManager streamManager = new MockStreamManager(scope, endpoint, port);
    MockClientFactory clientFactory = streamManager.getClientFactory();
    ReaderGroupConfig groupConfig = ReaderGroupConfig.builder().stream(Stream.of(scope, streamName)).build();
    streamManager.createScope(scope);
    streamManager.createStream(scope, streamName, StreamConfiguration.builder().scope(scope).streamName(streamName).scalingPolicy(ScalingPolicy.fixed(1)).build());
    ReaderGroup readerGroup = streamManager.createReaderGroup(readerGroupName, groupConfig);
    JavaSerializer<String> serializer = new JavaSerializer<>();
    EventStreamWriter<String> producer = clientFactory.createEventWriter(streamName, serializer, EventWriterConfig.builder().build());
    producer.writeEvent(testString);
    producer.writeEvent(testString);
    producer.writeEvent(testString);
    producer.flush();
    AtomicLong clock = new AtomicLong();
    @Cleanup EventStreamReader<String> reader1 = clientFactory.createReader("reader1", readerGroupName, serializer, ReaderConfig.builder().build(), clock::get, clock::get);
    @Cleanup EventStreamReader<String> reader2 = clientFactory.createReader("reader2", readerGroupName, serializer, ReaderConfig.builder().build(), clock::get, clock::get);
    clock.addAndGet(CLOCK_ADVANCE_INTERVAL);
    @Cleanup("shutdown") final InlineExecutor backgroundExecutor = new InlineExecutor();
    CompletableFuture<Checkpoint> checkpoint = readerGroup.initiateCheckpoint("Checkpoint", backgroundExecutor);
    assertFalse(checkpoint.isDone());
    EventRead<String> read = reader1.readNextEvent(60000);
    assertTrue(read.isCheckpoint());
    assertEquals("Checkpoint", read.getCheckpointName());
    assertNull(read.getEvent());
    read = reader2.readNextEvent(60000);
    assertTrue(read.isCheckpoint());
    assertEquals("Checkpoint", read.getCheckpointName());
    assertNull(read.getEvent());
    Checkpoint cpResult = checkpoint.get(5, TimeUnit.SECONDS);
    assertTrue(checkpoint.isDone());
    assertEquals("Checkpoint", cpResult.getName());
}
Also used : ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) ReaderGroup(io.pravega.client.stream.ReaderGroup) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) Checkpoint(io.pravega.client.stream.Checkpoint) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) AtomicLong(java.util.concurrent.atomic.AtomicLong) Checkpoint(io.pravega.client.stream.Checkpoint) InlineExecutor(io.pravega.test.common.InlineExecutor) MockStreamManager(io.pravega.client.stream.mock.MockStreamManager) Test(org.junit.Test)

Example 4 with Checkpoint

use of io.pravega.client.stream.Checkpoint in project pravega by pravega.

the class ReaderGroupImpl method initiateCheckpoint.

@Override
public CompletableFuture<Checkpoint> initiateCheckpoint(String checkpointName, ScheduledExecutorService backgroundExecutor) {
    StateSynchronizer<ReaderGroupState> synchronizer = createSynchronizer();
    synchronizer.updateStateUnconditionally(new CreateCheckpoint(checkpointName));
    AtomicBoolean checkpointPending = new AtomicBoolean(true);
    return Futures.loop(checkpointPending::get, () -> {
        return Futures.delayedTask(() -> {
            synchronizer.fetchUpdates();
            checkpointPending.set(!synchronizer.getState().isCheckpointComplete(checkpointName));
            if (checkpointPending.get()) {
                log.debug("Waiting on checkpoint: {} currentState is: {}", checkpointName, synchronizer.getState());
            }
            return null;
        }, Duration.ofMillis(500), backgroundExecutor);
    }, backgroundExecutor).thenApply(v -> completeCheckpoint(checkpointName, synchronizer)).whenComplete((v, t) -> synchronizer.close());
}
Also used : StreamCut(io.pravega.client.stream.StreamCut) ReaderGroupStateInit(io.pravega.client.stream.impl.ReaderGroupState.ReaderGroupStateInit) SneakyThrows(lombok.SneakyThrows) Cleanup(lombok.Cleanup) ReaderGroup(io.pravega.client.stream.ReaderGroup) ReaderGroupMetrics(io.pravega.client.stream.ReaderGroupMetrics) EndOfDataNotification(io.pravega.client.stream.notifications.EndOfDataNotification) Position(io.pravega.client.stream.Position) Stream(io.pravega.client.stream.Stream) Duration(java.time.Duration) Map(java.util.Map) Checkpoint(io.pravega.client.stream.Checkpoint) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) Set(java.util.Set) Collectors(java.util.stream.Collectors) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) NotifierFactory(io.pravega.client.stream.notifications.NotifierFactory) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) ClientFactory(io.pravega.client.ClientFactory) Observable(io.pravega.client.stream.notifications.Observable) Entry(java.util.Map.Entry) Optional(java.util.Optional) ReaderGroupStateUpdate(io.pravega.client.stream.impl.ReaderGroupState.ReaderGroupStateUpdate) Futures(io.pravega.common.concurrent.Futures) StateSynchronizer(io.pravega.client.state.StateSynchronizer) Segment(io.pravega.client.segment.impl.Segment) NotificationSystem(io.pravega.client.stream.notifications.NotificationSystem) SegmentMetadataClientFactoryImpl(io.pravega.client.segment.impl.SegmentMetadataClientFactoryImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) SegmentMetadataClientFactory(io.pravega.client.segment.impl.SegmentMetadataClientFactory) ArrayList(java.util.ArrayList) SegmentNotification(io.pravega.client.stream.notifications.SegmentNotification) CreateCheckpoint(io.pravega.client.stream.impl.ReaderGroupState.CreateCheckpoint) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) Serializer(io.pravega.client.stream.Serializer) NameUtils(io.pravega.shared.NameUtils) Futures.getAndHandleExceptions(io.pravega.common.concurrent.Futures.getAndHandleExceptions) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Futures.allOfWithResults(io.pravega.common.concurrent.Futures.allOfWithResults) ClearCheckpoints(io.pravega.client.stream.impl.ReaderGroupState.ClearCheckpoints) Data(lombok.Data) InvalidStreamException(io.pravega.client.stream.InvalidStreamException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CreateCheckpoint(io.pravega.client.stream.impl.ReaderGroupState.CreateCheckpoint)

Example 5 with Checkpoint

use of io.pravega.client.stream.Checkpoint in project pravega by pravega.

the class ReaderGroupImplTest method resetReadersToCheckpoint.

@Test
public void resetReadersToCheckpoint() {
    Map<Segment, Long> positions = new HashMap<>();
    IntStream.of(2).forEach(segNum -> positions.put(new Segment(SCOPE, "s1", segNum), 10L));
    Checkpoint checkpoint = new CheckpointImpl("testChkPoint", positions);
    readerGroup.resetReaderGroup(ReaderGroupConfig.builder().startFromCheckpoint(checkpoint).build());
    verify(synchronizer, times(1)).updateStateUnconditionally(any(Update.class));
}
Also used : Checkpoint(io.pravega.client.stream.Checkpoint) HashMap(java.util.HashMap) Update(io.pravega.client.state.Update) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Aggregations

Checkpoint (io.pravega.client.stream.Checkpoint)7 ReaderGroup (io.pravega.client.stream.ReaderGroup)5 Cleanup (lombok.Cleanup)5 Test (org.junit.Test)5 ClientFactory (io.pravega.client.ClientFactory)3 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)3 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)2 Segment (io.pravega.client.segment.impl.Segment)2 ReinitializationRequiredException (io.pravega.client.stream.ReinitializationRequiredException)2 JavaSerializer (io.pravega.client.stream.impl.JavaSerializer)2 MockClientFactory (io.pravega.client.stream.mock.MockClientFactory)2 MockStreamManager (io.pravega.client.stream.mock.MockStreamManager)2 HashMap (java.util.HashMap)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ConnectionFactory (io.pravega.client.netty.impl.ConnectionFactory)1 SegmentMetadataClient (io.pravega.client.segment.impl.SegmentMetadataClient)1 SegmentMetadataClientFactory (io.pravega.client.segment.impl.SegmentMetadataClientFactory)1 SegmentMetadataClientFactoryImpl (io.pravega.client.segment.impl.SegmentMetadataClientFactoryImpl)1 StateSynchronizer (io.pravega.client.state.StateSynchronizer)1