Search in sources :

Example 46 with EventWriterConfig

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

the class EventStreamWriterTest method testNoNextSegmentMidClose.

@Test
public void testNoNextSegmentMidClose() {
    String scope = "scope";
    String streamName = "stream";
    String routingKey = "RoutingKey";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment1 = new Segment(scope, streamName, 0);
    EventWriterConfig config = EventWriterConfig.builder().build();
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Controller controller = Mockito.mock(Controller.class);
    FakeSegmentOutputStream outputStream1 = new FakeSegmentOutputStream(segment1);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> {
        outputStream1.callBackForSealed = i.getArgument(1);
        return outputStream1;
    });
    JavaSerializer<String> serializer = new JavaSerializer<>();
    val empty = CompletableFuture.completedFuture(new StreamSegments(new TreeMap<>()));
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1)).thenReturn(empty);
    @Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService(), executorService(), null);
    CompletableFuture<Void> future = writer.writeEvent(routingKey, "Foo");
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(empty);
    val noFutures = CompletableFuture.completedFuture(new StreamSegmentsWithPredecessors(new HashMap<>(), ""));
    Mockito.when(controller.getSuccessors(segment1)).thenReturn(noFutures);
    // invoke the sealed callback invocation simulating a netty call back with segment sealed exception.
    outputStream1.sealed = true;
    assertBlocks(() -> writer.close(), () -> outputStream1.invokeSealedCallBack());
    assertTrue(future.isDone());
    assertTrue(future.isCompletedExceptionally());
}
Also used : lombok.val(lombok.val) HashMap(java.util.HashMap) Controller(io.pravega.client.control.impl.Controller) TreeMap(java.util.TreeMap) 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 47 with EventWriterConfig

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

the class EventStreamWriterTest method testAutoNoteTime.

@Test
public void testAutoNoteTime() {
    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));
    Mockito.when(mockOutputStream.getLastObservedWriteOffset()).thenReturn(1111L);
    CollectingExecutor executor = new CollectingExecutor();
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executor, executor, null);
    List<Runnable> tasks = executor.getScheduledTasks();
    assertEquals(1, tasks.size());
    tasks.get(0).run();
    ImmutableMap<Segment, Long> expectedOffsets = ImmutableMap.of(segment1, 1111L);
    Mockito.verify(controller).noteTimestampFromWriter(eq("id"), eq(stream), Mockito.anyLong(), 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) CollectingExecutor(io.pravega.test.common.CollectingExecutor) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Test(org.junit.Test)

Example 48 with EventWriterConfig

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

the class EventStreamWriterTest method testNoNextSegment.

@Test
public void testNoNextSegment() {
    String scope = "scope";
    String streamName = "stream";
    String routingKey = "RoutingKey";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment1 = new Segment(scope, streamName, 0);
    EventWriterConfig config = EventWriterConfig.builder().build();
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Controller controller = Mockito.mock(Controller.class);
    FakeSegmentOutputStream outputStream1 = new FakeSegmentOutputStream(segment1);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> {
        outputStream1.callBackForSealed = i.getArgument(1);
        return outputStream1;
    });
    JavaSerializer<String> serializer = new JavaSerializer<>();
    val empty = CompletableFuture.completedFuture(new StreamSegments(new TreeMap<>()));
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1)).thenReturn(empty);
    @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(empty);
    val noFutures = CompletableFuture.completedFuture(new StreamSegmentsWithPredecessors(new HashMap<>(), ""));
    Mockito.when(controller.getSuccessors(segment1)).thenReturn(noFutures);
    // invoke the sealed callback invocation simulating a netty call back with segment sealed exception.
    outputStream1.sealed = true;
    assertBlocks(() -> writer.flush(), () -> outputStream1.invokeSealedCallBack());
    assertThrows("Stream should be sealed", () -> writer.writeEvent(routingKey, "Bar"), e -> e.getMessage().contains("sealed"));
    writer.close();
}
Also used : lombok.val(lombok.val) HashMap(java.util.HashMap) Controller(io.pravega.client.control.impl.Controller) TreeMap(java.util.TreeMap) 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 49 with EventWriterConfig

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

the class LargeEventWriterTest method testUnexpectedErrors.

@Test(timeout = 5000)
public void testUnexpectedErrors() throws ConnectionFailedException, NoSuchSegmentException, AuthenticationException, SegmentSealedException {
    Segment segment = Segment.fromScopedName("foo/bar/1");
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    MockController controller = new MockController("localhost", 0, connectionFactory, false);
    ClientConnection connection = Mockito.mock(ClientConnection.class);
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    connectionFactory.provideConnection(location, connection);
    EmptyTokenProviderImpl tokenProvider = new EmptyTokenProviderImpl();
    EventWriterConfig config = EventWriterConfig.builder().initialBackoffMillis(0).build();
    ArrayList<ByteBuffer> events = new ArrayList<>();
    events.add(ByteBuffer.allocate(1));
    AtomicBoolean failed = new AtomicBoolean(false);
    AtomicBoolean succeeded = new AtomicBoolean(false);
    answerRequest(connectionFactory, connection, location, CreateTransientSegment.class, r -> new SegmentCreated(r.getRequestId(), "transient-segment"));
    answerRequest(connectionFactory, connection, location, SetupAppend.class, r -> new AppendSetup(r.getRequestId(), segment.getScopedName(), r.getWriterId(), WireCommands.NULL_ATTRIBUTE_VALUE));
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ConditionalBlockEnd argument = (ConditionalBlockEnd) invocation.getArgument(0);
            failed.set(true);
            connectionFactory.getProcessor(location).process(new InvalidEventNumber(argument.getWriterId(), argument.getEventNumber(), "stacktrace"));
            return null;
        }
    }).doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ConditionalBlockEnd argument = (ConditionalBlockEnd) invocation.getArgument(0);
            ByteBuf data = argument.getData();
            succeeded.set(true);
            connectionFactory.getProcessor(location).process(new DataAppended(argument.getRequestId(), argument.getWriterId(), argument.getEventNumber(), argument.getEventNumber() - 1, argument.getExpectedOffset() + data.readableBytes()));
            return null;
        }
    }).when(connection).send(any(ConditionalBlockEnd.class));
    answerRequest(connectionFactory, connection, location, MergeSegments.class, r -> {
        return new SegmentsMerged(r.getRequestId(), r.getSource(), r.getTarget(), -1);
    });
    LargeEventWriter writer = new LargeEventWriter(writerId, controller, connectionFactory);
    writer.writeLargeEvent(segment, events, tokenProvider, EventWriterConfig.builder().build());
    assertTrue(failed.getAndSet(false));
    assertTrue(succeeded.getAndSet(false));
    answerRequest(connectionFactory, connection, location, CreateTransientSegment.class, r -> new SegmentCreated(r.getRequestId(), "transient-segment"));
    answerRequest(connectionFactory, connection, location, SetupAppend.class, r -> new AppendSetup(r.getRequestId(), segment.getScopedName(), r.getWriterId(), WireCommands.NULL_ATTRIBUTE_VALUE));
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ConditionalBlockEnd argument = (ConditionalBlockEnd) invocation.getArgument(0);
            failed.set(true);
            connectionFactory.getProcessor(location).process(new ConditionalCheckFailed(argument.getWriterId(), argument.getEventNumber(), argument.getRequestId()));
            return null;
        }
    }).doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ConditionalBlockEnd argument = (ConditionalBlockEnd) invocation.getArgument(0);
            ByteBuf data = argument.getData();
            succeeded.set(true);
            connectionFactory.getProcessor(location).process(new DataAppended(argument.getRequestId(), argument.getWriterId(), argument.getEventNumber(), argument.getEventNumber() - 1, argument.getExpectedOffset() + data.readableBytes()));
            return null;
        }
    }).when(connection).send(any(ConditionalBlockEnd.class));
    answerRequest(connectionFactory, connection, location, MergeSegments.class, r -> {
        return new SegmentsMerged(r.getRequestId(), r.getSource(), r.getTarget(), -1);
    });
    writer = new LargeEventWriter(writerId, controller, connectionFactory);
    writer.writeLargeEvent(segment, events, tokenProvider, EventWriterConfig.builder().build());
    assertTrue(failed.getAndSet(false));
    assertTrue(succeeded.getAndSet(false));
}
Also used : ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Segment(io.pravega.client.segment.impl.Segment) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) CreateTransientSegment(io.pravega.shared.protocol.netty.WireCommands.CreateTransientSegment) ConditionalCheckFailed(io.pravega.shared.protocol.netty.WireCommands.ConditionalCheckFailed) ConditionalBlockEnd(io.pravega.shared.protocol.netty.WireCommands.ConditionalBlockEnd) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvalidEventNumber(io.pravega.shared.protocol.netty.WireCommands.InvalidEventNumber) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) ClientConnection(io.pravega.client.connection.impl.ClientConnection) ByteBuffer(java.nio.ByteBuffer) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) EmptyTokenProviderImpl(io.pravega.client.security.auth.EmptyTokenProviderImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SegmentCreated(io.pravega.shared.protocol.netty.WireCommands.SegmentCreated) Answer(org.mockito.stubbing.Answer) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) MockController(io.pravega.client.stream.mock.MockController) SegmentsMerged(io.pravega.shared.protocol.netty.WireCommands.SegmentsMerged) Test(org.junit.Test)

Example 50 with EventWriterConfig

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

the class LargeEventWriterTest method testThrownErrors.

@Test(timeout = 5000)
public void testThrownErrors() throws ConnectionFailedException {
    Segment segment = Segment.fromScopedName("foo/bar/1");
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    MockController controller = new MockController("localhost", 0, connectionFactory, false);
    ClientConnection connection = Mockito.mock(ClientConnection.class);
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    connectionFactory.provideConnection(location, connection);
    EmptyTokenProviderImpl tokenProvider = new EmptyTokenProviderImpl();
    EventWriterConfig config = EventWriterConfig.builder().enableLargeEvents(true).build();
    ArrayList<ByteBuffer> events = new ArrayList<>();
    events.add(ByteBuffer.allocate(1));
    answerRequest(connectionFactory, connection, location, CreateTransientSegment.class, r -> new NoSuchSegment(r.getRequestId(), "foo/bar/1", "stacktrace", -1));
    AssertExtensions.assertThrows(NoSuchSegmentException.class, () -> {
        LargeEventWriter writer = new LargeEventWriter(writerId, controller, connectionFactory);
        writer.writeLargeEvent(segment, events, tokenProvider, config);
    });
    answerRequest(connectionFactory, connection, location, CreateTransientSegment.class, r -> new SegmentIsSealed(r.getRequestId(), "foo/bar/1", "stacktrace", -1));
    AssertExtensions.assertThrows(SegmentSealedException.class, () -> {
        LargeEventWriter writer = new LargeEventWriter(writerId, controller, connectionFactory);
        writer.writeLargeEvent(segment, events, tokenProvider, config);
    });
    answerRequest(connectionFactory, connection, location, CreateTransientSegment.class, r -> new OperationUnsupported(r.getRequestId(), "CreateTransientSegment", "stacktrace"));
    AssertExtensions.assertThrows(UnsupportedOperationException.class, () -> {
        LargeEventWriter writer = new LargeEventWriter(writerId, controller, connectionFactory);
        writer.writeLargeEvent(segment, events, tokenProvider, config);
    });
}
Also used : OperationUnsupported(io.pravega.shared.protocol.netty.WireCommands.OperationUnsupported) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) CreateTransientSegment(io.pravega.shared.protocol.netty.WireCommands.CreateTransientSegment) EmptyTokenProviderImpl(io.pravega.client.security.auth.EmptyTokenProviderImpl) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentIsSealed(io.pravega.shared.protocol.netty.WireCommands.SegmentIsSealed) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) Test(org.junit.Test)

Aggregations

EventWriterConfig (io.pravega.client.stream.EventWriterConfig)58 Test (org.junit.Test)51 Cleanup (lombok.Cleanup)46 Segment (io.pravega.client.segment.impl.Segment)39 Controller (io.pravega.client.control.impl.Controller)33 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)30 UUID (java.util.UUID)14 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)11 FakeSegmentOutputStream (io.pravega.client.stream.impl.EventStreamWriterTest.FakeSegmentOutputStream)7 MockSegmentIoStreams (io.pravega.client.stream.mock.MockSegmentIoStreams)7 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)6 ClientConnection (io.pravega.client.connection.impl.ClientConnection)5 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)5 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)5 MockController (io.pravega.client.stream.mock.MockController)5 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)5 CreateTransientSegment (io.pravega.shared.protocol.netty.WireCommands.CreateTransientSegment)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 ByteBuf (io.netty.buffer.ByteBuf)4 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)4