Search in sources :

Example 61 with Segment

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

the class EventStreamWriterTest method testWriteBetweenSeals.

/**
 * This tests lock release ordering. If a write happens after a flush starts, it should block on the flush while the seal proceeds.
 */
@Test(timeout = 10000)
public void testWriteBetweenSeals() throws EndOfSegmentException, SegmentTruncatedException {
    String scope = "scope";
    String streamName = "stream";
    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);
    SealedSegmentOutputStream outputStream = new SealedSegmentOutputStream(segment1);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1));
    Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2));
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> {
        outputStream.callBackForSealed = i.getArgument(1);
        return outputStream;
    });
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService(), executorService(), null);
    writer.writeEvent("Foo");
    Mockito.verify(controller).getCurrentSegments(any(), any());
    MockSegmentIoStreams outputStream2 = new MockSegmentIoStreams(segment2, null);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenReturn(outputStream2);
    AssertExtensions.assertBlocks(() -> {
        // blocking on flush.
        writer.flush();
    }, () -> {
        AssertExtensions.assertBlocks(() -> {
            writer.writeEvent("foo");
        }, () -> {
            // trigger release with a segmentSealedException.
            outputStream.releaseFlush();
            // trigger Sealed Segment call back.
            outputStream.invokeSealedCallBack();
        });
    });
    Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
    assertTrue(outputStream2.fetchCurrentSegmentLength().join() > 0);
    assertEquals(serializer.serialize("Foo"), outputStream2.read());
}
Also used : MockSegmentIoStreams(io.pravega.client.stream.mock.MockSegmentIoStreams) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) Test(org.junit.Test)

Example 62 with Segment

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

the class EventStreamWriterTest method testNoteTimeFail.

@Test
public void testNoteTimeFail() {
    String scope = "scope";
    String streamName = "stream";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment1 = new Segment(scope, streamName, 0);
    EventWriterConfig config = EventWriterConfig.builder().automaticallyNoteTime(true).build();
    SegmentOutputStream mockOutputStream = Mockito.mock(SegmentOutputStream.class);
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenReturn(mockOutputStream);
    Controller controller = Mockito.mock(Controller.class);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1));
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService(), executorService(), null);
    AssertExtensions.assertThrows(IllegalStateException.class, () -> writer.noteTime(123));
}
Also used : EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Example 63 with Segment

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

the class EventStreamWriterTest method testSegmentSealedInClose.

@Test
public void testSegmentSealedInClose() throws EndOfSegmentException, SegmentTruncatedException {
    String scope = "scope";
    String streamName = "stream";
    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);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1));
    Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2));
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> {
        outputStream1.callBackForSealed = i.getArgument(1);
        return outputStream1;
    });
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService(), executorService(), null);
    writer.writeEvent("Foo");
    Mockito.verify(controller).getCurrentSegments(any(), any());
    assertTrue(outputStream1.unacked.size() > 0);
    MockSegmentIoStreams outputStream2 = new MockSegmentIoStreams(segment2, null);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenReturn(outputStream2);
    outputStream1.invokeSealedCallBack();
    writer.close();
    Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
    assertTrue(outputStream2.fetchCurrentSegmentLength().join() > 0);
    assertEquals(serializer.serialize("Foo"), outputStream2.read());
}
Also used : MockSegmentIoStreams(io.pravega.client.stream.mock.MockSegmentIoStreams) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) Test(org.junit.Test)

Example 64 with Segment

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

the class EventStreamWriterTest method testNoteTime.

@Test
public void testNoteTime() {
    String scope = "scope";
    String streamName = "stream";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment1 = new Segment(scope, streamName, 0);
    EventWriterConfig config = EventWriterConfig.builder().build();
    SegmentOutputStream mockOutputStream = Mockito.mock(SegmentOutputStream.class);
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenReturn(mockOutputStream);
    Controller controller = Mockito.mock(Controller.class);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1));
    Mockito.when(mockOutputStream.getLastObservedWriteOffset()).thenReturn(1111L);
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService(), executorService(), null);
    writer.noteTime(123);
    ImmutableMap<Segment, Long> expectedOffsets = ImmutableMap.of(segment1, 1111L);
    Mockito.verify(controller).noteTimestampFromWriter(eq("id"), eq(stream), eq(123L), eq(new WriterPosition(expectedOffsets)));
}
Also used : Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Test(org.junit.Test)

Example 65 with Segment

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

the class EventStreamWriterTest method testRetryCloseSegmentSealed.

@Test
public void testRetryCloseSegmentSealed() throws EndOfSegmentException, SegmentTruncatedException {
    String scope = "scope";
    String streamName = "stream";
    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);
    SealedSegmentOutputStream outputStream = new SealedSegmentOutputStream(segment1);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1));
    Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2));
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> {
        outputStream.callBackForSealed = i.getArgument(1);
        return outputStream;
    });
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService(), executorService(), null);
    writer.writeEvent("Foo");
    Mockito.verify(controller).getCurrentSegments(any(), any());
    assertTrue(outputStream.getUnackedEventsOnSeal().size() > 0);
    MockSegmentIoStreams outputStream2 = new MockSegmentIoStreams(segment2, null);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenReturn(outputStream2);
    AssertExtensions.assertBlocks(() -> {
        // closed invokes flush internally; this call is blocking on flush.
        writer.close();
    }, () -> {
        // trigger release with a segmentSealedException.
        outputStream.releaseFlush();
        // trigger Sealed Segment call back.
        outputStream.invokeSealedCallBack();
    });
    Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
    assertTrue(outputStream2.fetchCurrentSegmentLength().join() > 0);
    assertTrue(outputStream2.isClosed());
    // the connection to outputStream is closed with the failConnection during SegmentSealed Callback.
    assertEquals(serializer.serialize("Foo"), outputStream2.read());
}
Also used : MockSegmentIoStreams(io.pravega.client.stream.mock.MockSegmentIoStreams) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) 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