Search in sources :

Example 26 with Controller

use of io.pravega.client.control.impl.Controller in project pravega by pravega.

the class TransactionalEventStreamWriterTest method testGetTxnAborted.

@Test(expected = TxnFailedException.class)
public void testGetTxnAborted() throws TxnFailedException {
    String scope = "scope";
    String streamName = "stream";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment = new Segment(scope, streamName, 0);
    UUID txnId = UUID.randomUUID();
    EventWriterConfig config = EventWriterConfig.builder().build();
    JavaSerializer<String> serializer = new JavaSerializer<>();
    // Setup mocks
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Controller controller = Mockito.mock(Controller.class);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment));
    Mockito.when(controller.getEpochSegments(scope, streamName, NameUtils.getEpoch(txnId))).thenReturn(getSegmentsFuture(segment));
    Mockito.when(controller.checkTransactionStatus(Stream.of(scope, streamName), txnId)).thenReturn(CompletableFuture.completedFuture(Transaction.Status.ABORTED));
    Mockito.when(controller.pingTransaction(eq(Stream.of(scope, streamName)), eq(txnId), anyLong())).thenReturn(CompletableFuture.completedFuture(Transaction.PingStatus.OPEN));
    FakeSegmentOutputStream outputStream = new FakeSegmentOutputStream(segment);
    FakeSegmentOutputStream bad = new FakeSegmentOutputStream(segment);
    Mockito.when(controller.createTransaction(eq(stream), anyLong())).thenReturn(CompletableFuture.completedFuture(new TxnSegments(getSegments(segment), txnId)));
    Mockito.when(streamFactory.createOutputStreamForTransaction(eq(segment), eq(txnId), any(), any())).thenReturn(outputStream);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(bad);
    // Create a transactional event Writer
    @Cleanup TransactionalEventStreamWriter<String> writer = new TransactionalEventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService());
    writer.beginTxn();
    // fetch the txn when it is already aborted
    Transaction<String> txn = writer.getTxn(txnId);
    txn.writeEvent("Foo");
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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) FakeSegmentOutputStream(io.pravega.client.stream.impl.EventStreamWriterTest.FakeSegmentOutputStream) UUID(java.util.UUID) Test(org.junit.Test)

Example 27 with Controller

use of io.pravega.client.control.impl.Controller in project pravega by pravega.

the class TransactionalEventStreamWriterTest method testTxnCommit.

@Test
public void testTxnCommit() throws TxnFailedException, SegmentSealedException {
    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().build();
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Controller controller = Mockito.mock(Controller.class);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment));
    FakeSegmentOutputStream outputStream = spy(new FakeSegmentOutputStream(segment));
    FakeSegmentOutputStream bad = new FakeSegmentOutputStream(segment);
    Mockito.when(controller.createTransaction(eq(stream), anyLong())).thenReturn(CompletableFuture.completedFuture(new TxnSegments(getSegments(segment), txid)));
    Mockito.when(controller.commitTransaction(eq(stream), anyString(), isNull(), eq(txid))).thenReturn(CompletableFuture.completedFuture(null));
    Mockito.when(controller.pingTransaction(eq(stream), eq(txid), anyLong())).thenReturn(CompletableFuture.completedFuture(Transaction.PingStatus.OPEN));
    Mockito.when(controller.checkTransactionStatus(eq(stream), eq(txid))).thenReturn(CompletableFuture.completedFuture(Transaction.Status.OPEN));
    Mockito.when(streamFactory.createOutputStreamForTransaction(eq(segment), eq(txid), any(), any())).thenReturn(outputStream);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(bad);
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup TransactionalEventStreamWriter<String> writer = new TransactionalEventStreamWriterImpl<>(stream, "id", controller, streamFactory, serializer, config, executorService());
    Transaction<String> txn = writer.beginTxn();
    txn.writeEvent("Foo");
    assertTrue(bad.unacked.isEmpty());
    assertEquals(1, outputStream.unacked.size());
    outputStream.unacked.get(0).getAckFuture().complete(null);
    txn.checkStatus();
    Mockito.verify(controller, Mockito.times(1)).checkTransactionStatus(eq(stream), eq(txid));
    // invoke commit of transaction.
    txn.commit();
    // verify if segments are flushed and closed.
    Mockito.verify(outputStream, Mockito.times(1)).close();
    Mockito.verify(controller, Mockito.times(1)).commitTransaction(eq(stream), anyString(), isNull(), eq(txid));
    assertTrue(bad.unacked.isEmpty());
    assertTrue(outputStream.unacked.isEmpty());
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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) FakeSegmentOutputStream(io.pravega.client.stream.impl.EventStreamWriterTest.FakeSegmentOutputStream) UUID(java.util.UUID) Test(org.junit.Test)

Example 28 with Controller

use of io.pravega.client.control.impl.Controller in project pravega by pravega.

the class EventStreamWriterTest method testEndOfSegmentBackgroundRefresh.

@Test
public void testEndOfSegmentBackgroundRefresh() {
    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));
    writer.writeEvent(routingKey, "Bar");
    Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
    assertEquals(2, outputStream1.unacked.size());
    assertEquals("Foo", serializer.deserialize(outputStream1.getUnacked(0)));
    assertEquals("Bar", serializer.deserialize(outputStream1.getUnacked(1)));
    // simulate a segment sealed callback.
    outputStream1.invokeSealedCallBack();
    writer.writeEvent(routingKey, "TestData");
    // This time the actual handleLogSealed is invoked and the resend method resends data to outputStream2.
    assertEquals(3, outputStream2.acked.size());
    assertEquals("Foo", serializer.deserialize(outputStream2.getAcked(0)));
    assertEquals("Bar", serializer.deserialize(outputStream2.getAcked(1)));
    assertEquals(1, outputStream2.unacked.size());
    assertEquals("TestData", 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)

Example 29 with Controller

use of io.pravega.client.control.impl.Controller 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, "id", controller, streamFactory, serializer, config, executorService(), executorService(), null);
    writer.writeEvent("Foo");
    Mockito.verify(controller).getCurrentSegments(any(), any());
    assertTrue(outputStream.unacked.size() > 0);
    writer.flush();
    assertTrue(outputStream.unacked.isEmpty());
}
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)

Example 30 with Controller

use of io.pravega.client.control.impl.Controller 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, "id", controller, streamFactory, serializer, config, executorService(), executorService(), null);
    writer.writeEvent(routingKey, "Foo");
    Mockito.verify(controller).getCurrentSegments(any(), any());
    assertEquals(1, outputStream1.unacked.size());
    assertTrue(outputStream2.getUnackedEventsOnSeal().isEmpty());
    outputStream1.invokeSealedCallBack();
    outputStream2.invokeSealedCallBack();
    writer.writeEvent(routingKey, "Bar");
    Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
    assertEquals(0, outputStream2.acked.size());
    assertEquals(2, outputStream2.unacked.size());
    assertEquals("Foo", serializer.deserialize(outputStream2.getUnacked(0)));
    assertEquals(3, outputStream3.acked.size());
    assertEquals(1, outputStream3.unacked.size());
    assertEquals("Foo", serializer.deserialize(outputStream3.getAcked(0)));
    assertEquals("Bar", serializer.deserialize(outputStream3.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

Controller (io.pravega.client.control.impl.Controller)120 Test (org.junit.Test)95 Cleanup (lombok.Cleanup)81 Segment (io.pravega.client.segment.impl.Segment)53 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)50 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)47 Stream (io.pravega.client.stream.Stream)37 CompletableFuture (java.util.concurrent.CompletableFuture)35 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)34 SocketConnectionFactoryImpl (io.pravega.client.connection.impl.SocketConnectionFactoryImpl)33 HashMap (java.util.HashMap)29 ClientConfig (io.pravega.client.ClientConfig)28 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)28 StreamImpl (io.pravega.client.stream.impl.StreamImpl)25 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)24 Slf4j (lombok.extern.slf4j.Slf4j)24 Before (org.junit.Before)23 ConnectionFactory (io.pravega.client.connection.impl.ConnectionFactory)22 ConnectionPoolImpl (io.pravega.client.connection.impl.ConnectionPoolImpl)21 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)21