Search in sources :

Example 56 with Segment

use of io.pravega.client.segment.impl.Segment in project pravega by pravega.

the class ReaderGroupImplTest method getUnreadBytesBasedOnLastPosition.

@Test
public void getUnreadBytesBasedOnLastPosition() {
    final String stream = "s1";
    final StreamCut startStreamCut = getStreamCut(stream, 10L, 1, 2);
    final StreamCut endStreamCut = getStreamCut(stream, 30L, 1, 2);
    // setup mocks
    // simulate zero checkpoints.
    when(state.getPositionsForLastCompletedCheckpoint()).thenReturn(Optional.empty());
    Map<SegmentWithRange, Long> positions = startStreamCut.asImpl().getPositions().entrySet().stream().collect(Collectors.toMap(e -> new SegmentWithRange(e.getKey(), null), e -> e.getValue()));
    when(state.getPositions()).thenReturn(ImmutableMap.of(Stream.of(SCOPE, stream), positions));
    when(state.getEndSegments()).thenReturn(endStreamCut.asImpl().getPositions());
    when(synchronizer.getState()).thenReturn(state);
    ImmutableSet<Segment> r = ImmutableSet.<Segment>builder().addAll(startStreamCut.asImpl().getPositions().keySet()).addAll(endStreamCut.asImpl().getPositions().keySet()).build();
    when(controller.getSegments(startStreamCut, endStreamCut)).thenReturn(CompletableFuture.completedFuture(new StreamSegmentSuccessors(r, "")));
    assertEquals(40L, readerGroup.unreadBytes());
}
Also used : ReaderGroupStateInitSerializer(io.pravega.client.admin.impl.ReaderGroupManagerImpl.ReaderGroupStateInitSerializer) Arrays(java.util.Arrays) StreamCut(io.pravega.client.stream.StreamCut) AssertExtensions.assertSuppliedFutureThrows(io.pravega.test.common.AssertExtensions.assertSuppliedFutureThrows) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) AssertExtensions(io.pravega.test.common.AssertExtensions) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Cleanup(lombok.Cleanup) ArgumentMatcher(org.mockito.ArgumentMatcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Stream(io.pravega.client.stream.Stream) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Checkpoint(io.pravega.client.stream.Checkpoint) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) UUID(java.util.UUID) InitialUpdate(io.pravega.client.state.InitialUpdate) Collectors(java.util.stream.Collectors) ReaderGroupStateUpdatesSerializer(io.pravega.client.admin.impl.ReaderGroupManagerImpl.ReaderGroupStateUpdatesSerializer) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Controller(io.pravega.client.control.impl.Controller) Futures(io.pravega.common.concurrent.Futures) ClearCheckpointsBefore(io.pravega.client.stream.impl.ReaderGroupState.ClearCheckpointsBefore) Mockito.mock(org.mockito.Mockito.mock) StateSynchronizer(io.pravega.client.state.StateSynchronizer) IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Segment(io.pravega.client.segment.impl.Segment) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) ReaderGroupConfigRejectedException(io.pravega.client.control.impl.ReaderGroupConfigRejectedException) Update(io.pravega.client.state.Update) HashSet(java.util.HashSet) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Before(org.junit.Before) Serializer(io.pravega.client.stream.Serializer) NameUtils(io.pravega.shared.NameUtils) ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Mockito(org.mockito.Mockito) SynchronizerClientFactory(io.pravega.client.SynchronizerClientFactory) ReaderSegmentDistribution(io.pravega.client.stream.ReaderSegmentDistribution) InlineExecutor(io.pravega.test.common.InlineExecutor) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StreamCut(io.pravega.client.stream.StreamCut) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Example 57 with Segment

use of io.pravega.client.segment.impl.Segment in project pravega by pravega.

the class ReaderGroupImplTest method getStreamCut.

private StreamCut getStreamCut(String streamName, long offset, int... segmentNumbers) {
    ImmutableMap.Builder<Segment, Long> builder = ImmutableMap.<Segment, Long>builder();
    Arrays.stream(segmentNumbers).forEach(seg -> {
        builder.put(new Segment(SCOPE, streamName, seg), offset);
    });
    return new StreamCutImpl(Stream.of(SCOPE, streamName), builder.build());
}
Also used : ImmutableMap(com.google.common.collect.ImmutableMap) Segment(io.pravega.client.segment.impl.Segment)

Example 58 with Segment

use of io.pravega.client.segment.impl.Segment in project pravega by pravega.

the class ReaderGroupStateTest method getStreamNames.

@Test
public void getStreamNames() {
    // configured Streams.
    Set<String> configuredStreams = ImmutableSet.of(getStream("S1").getScopedName(), getStream("S2").getScopedName());
    // validate stream names
    assertEquals(configuredStreams, readerState.getStreamNames());
    // Simulate addition of a reader and assigning of segments to the reader.
    new AddReader("reader1").applyTo(readerState, revision);
    new ReaderGroupState.AcquireSegment("reader1", new Segment(SCOPE, "S1", 0)).applyTo(readerState, revision);
    // validate stream names
    assertEquals(configuredStreams, readerState.getStreamNames());
}
Also used : AcquireSegment(io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment) AddReader(io.pravega.client.stream.impl.ReaderGroupState.AddReader) Segment(io.pravega.client.segment.impl.Segment) AcquireSegment(io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment) Test(org.junit.Test)

Example 59 with Segment

use of io.pravega.client.segment.impl.Segment in project pravega by pravega.

the class EventStreamWriterTest method testWriteEvents.

@Test
public void testWriteEvents() {
    String scope = "scope1";
    String streamName = "stream1";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment = new Segment(scope, streamName, 0);
    EventWriterConfig config = EventWriterConfig.builder().build();
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Controller controller = Mockito.mock(Controller.class);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment));
    MockSegmentIoStreams outputStream = new MockSegmentIoStreams(segment, null);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(outputStream);
    EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, new JavaSerializer<>(), config, executorService(), executorService(), null);
    writer.writeEvents("1", Lists.newArrayList("Foo", "Bar")).join();
    writer.writeEvent("1", "Foo2").join();
    writer.close();
}
Also used : EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) MockSegmentIoStreams(io.pravega.client.stream.mock.MockSegmentIoStreams) Controller(io.pravega.client.control.impl.Controller) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Example 60 with Segment

use of io.pravega.client.segment.impl.Segment in project pravega by pravega.

the class EventStreamWriterTest method testEndOfSegment.

@Test
public void testEndOfSegment() {
    String scope = "scope";
    String streamName = "stream";
    String routingKey = "RoutingKey";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment1 = new Segment(scope, streamName, 0);
    Segment segment2 = new Segment(scope, streamName, 1);
    EventWriterConfig config = EventWriterConfig.builder().build();
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Controller controller = Mockito.mock(Controller.class);
    FakeSegmentOutputStream outputStream1 = new FakeSegmentOutputStream(segment1);
    FakeSegmentOutputStream outputStream2 = new FakeSegmentOutputStream(segment2);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> {
        outputStream1.callBackForSealed = i.getArgument(1);
        return outputStream1;
    });
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenAnswer(i -> {
        outputStream2.callBackForSealed = i.getArgument(1);
        return outputStream2;
    });
    JavaSerializer<String> serializer = new JavaSerializer<>();
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1)).thenReturn(getSegmentsFuture(segment2));
    @Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService(), executorService(), null);
    writer.writeEvent(routingKey, "Foo");
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment2));
    Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2));
    // invoke the sealed callback invocation simulating a netty call back with segment sealed exception.
    outputStream1.invokeSealedCallBack();
    writer.writeEvent(routingKey, "Bar");
    Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
    assertEquals(2, outputStream2.acked.size());
    assertEquals(1, outputStream2.unacked.size());
    assertEquals("Foo", serializer.deserialize(outputStream2.getAcked(0)));
    assertEquals("Bar", serializer.deserialize(outputStream2.getUnacked(0)));
}
Also used : EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Aggregations

Segment (io.pravega.client.segment.impl.Segment)285 Test (org.junit.Test)200 Cleanup (lombok.Cleanup)125 HashMap (java.util.HashMap)79 Controller (io.pravega.client.control.impl.Controller)74 Stream (io.pravega.client.stream.Stream)69 MockController (io.pravega.client.stream.mock.MockController)67 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)57 StreamCut (io.pravega.client.stream.StreamCut)57 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)53 AtomicLong (java.util.concurrent.atomic.AtomicLong)53 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)51 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)50 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)46 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)44 StreamCutImpl (io.pravega.client.stream.impl.StreamCutImpl)42 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)41 ArrayList (java.util.ArrayList)40 Map (java.util.Map)40 CompletableFuture (java.util.concurrent.CompletableFuture)37