Search in sources :

Example 1 with SegmentOutputStreamFactory

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

the class EventStreamWriterTest method testSegmentSealedInSegmentSealed.

@Test
public void testSegmentSealedInSegmentSealed() {
    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);
    Segment segment3 = new Segment(scope, streamName, 2);
    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);
    FakeSegmentOutputStream outputStream3 = new FakeSegmentOutputStream(segment3);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1));
    Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2));
    Mockito.when(controller.getSuccessors(segment2)).thenReturn(getReplacement(segment2, segment3));
    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;
    });
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment3), any(), any(), any())).thenAnswer(i -> {
        outputStream3.callBackForSealed = i.getArgument(1);
        return outputStream3;
    });
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, serializer, config, new InlineExecutor());
    writer.writeEvent(routingKey, "Foo");
    Mockito.verify(controller).getCurrentSegments(any(), any());
    assertEquals(1, outputStream1.getUnackedEventsOnSeal().size());
    assertTrue(outputStream2.getUnackedEventsOnSeal().isEmpty());
    outputStream1.invokeSealedCallBack();
    outputStream2.invokeSealedCallBack();
    writer.writeEvent(routingKey, "Bar");
    Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
    assertEquals(1, outputStream2.getUnackedEventsOnSeal().size());
    assertEquals("Foo", serializer.deserialize(outputStream2.getUnackedEventsOnSeal().get(0).getData()));
    assertEquals(2, outputStream3.getUnackedEventsOnSeal().size());
    assertEquals("Foo", serializer.deserialize(outputStream3.getUnackedEventsOnSeal().get(0).getData()));
    assertEquals("Bar", serializer.deserialize(outputStream3.getUnackedEventsOnSeal().get(1).getData()));
}
Also used : Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) InlineExecutor(io.pravega.test.common.InlineExecutor) Test(org.junit.Test)

Example 2 with SegmentOutputStreamFactory

use of io.pravega.client.segment.impl.SegmentOutputStreamFactory 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, controller, streamFactory, serializer, config, new InlineExecutor());
    writer.writeEvent("Foo");
    Mockito.verify(controller).getCurrentSegments(any(), any());
    assertTrue(outputStream.getUnackedEventsOnSeal().size() > 0);
    MockSegmentIoStreams outputStream2 = new MockSegmentIoStreams(segment2);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenReturn(outputStream2);
    Async.testBlocking(() -> {
        // 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() > 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) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) InlineExecutor(io.pravega.test.common.InlineExecutor) Test(org.junit.Test)

Example 3 with SegmentOutputStreamFactory

use of io.pravega.client.segment.impl.SegmentOutputStreamFactory 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, controller, streamFactory, serializer, config, new InlineExecutor());
    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.getUnackedEventsOnSeal().size());
    assertEquals("Foo", serializer.deserialize(outputStream2.getUnackedEventsOnSeal().get(0).getData()));
    assertEquals("Bar", serializer.deserialize(outputStream2.getUnackedEventsOnSeal().get(1).getData()));
}
Also used : Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) InlineExecutor(io.pravega.test.common.InlineExecutor) Test(org.junit.Test)

Example 4 with SegmentOutputStreamFactory

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

the class EventStreamWriterTest method testTxnFailed.

@Test
public void testTxnFailed() {
    String scope = "scope";
    String streamName = "stream";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment = new Segment(scope, streamName, 0);
    UUID txid = UUID.randomUUID();
    EventWriterConfig config = EventWriterConfig.builder().transactionTimeoutTime(0).transactionTimeoutScaleGracePeriod(0).build();
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Controller controller = Mockito.mock(Controller.class);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment));
    FakeSegmentOutputStream outputStream = new FakeSegmentOutputStream(segment);
    FakeSegmentOutputStream bad = new FakeSegmentOutputStream(segment);
    Mockito.when(controller.createTransaction(stream, 0, 0)).thenReturn(CompletableFuture.completedFuture(new TxnSegments(getSegments(segment), txid)));
    Mockito.when(streamFactory.createOutputStreamForTransaction(eq(segment), eq(txid), any(), any(), any())).thenReturn(outputStream);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(bad);
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, serializer, config, new InlineExecutor());
    Transaction<String> txn = writer.beginTxn();
    outputStream.invokeSealedCallBack();
    try {
        txn.writeEvent("Foo");
    } catch (TxnFailedException e) {
    // Expected
    }
    Mockito.verify(controller).getCurrentSegments(any(), any());
    assertTrue(bad.getUnackedEventsOnSeal().isEmpty());
    assertEquals(1, outputStream.getUnackedEventsOnSeal().size());
}
Also used : Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) InlineExecutor(io.pravega.test.common.InlineExecutor) TxnFailedException(io.pravega.client.stream.TxnFailedException) UUID(java.util.UUID) Test(org.junit.Test)

Example 5 with SegmentOutputStreamFactory

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

the class EventStreamWriterTest method testFlush.

@Test
public void testFlush() {
    String scope = "scope";
    String streamName = "stream";
    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);
    FakeSegmentOutputStream outputStream = new FakeSegmentOutputStream(segment);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment));
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(outputStream);
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, serializer, config, new InlineExecutor());
    writer.writeEvent("Foo");
    Mockito.verify(controller).getCurrentSegments(any(), any());
    assertTrue(outputStream.getUnackedEventsOnSeal().size() > 0);
    writer.flush();
    assertTrue(outputStream.getUnackedEventsOnSeal().isEmpty());
}
Also used : Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) InlineExecutor(io.pravega.test.common.InlineExecutor) Test(org.junit.Test)

Aggregations

Segment (io.pravega.client.segment.impl.Segment)15 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)15 Test (org.junit.Test)15 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)12 InlineExecutor (io.pravega.test.common.InlineExecutor)12 Cleanup (lombok.Cleanup)10 MockSegmentIoStreams (io.pravega.client.stream.mock.MockSegmentIoStreams)5 TreeMap (java.util.TreeMap)3 UUID (java.util.UUID)2 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)1 TxnFailedException (io.pravega.client.stream.TxnFailedException)1