Search in sources :

Example 11 with SegmentSealedException

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

the class TransactionalEventStreamWriterTest method testTxnCommitFailureRetry.

@Test
public void testTxnCommitFailureRetry() 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();
    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));
    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.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);
    // Simulate a Controller client throwing a Deadline Exceeded exception.
    CompletableFuture<Void> failedCommit = new CompletableFuture<>();
    failedCommit.completeExceptionally(new RetriesExhaustedException(new StatusRuntimeException(Status.DEADLINE_EXCEEDED)));
    Mockito.when(controller.commitTransaction(eq(stream), anyString(), isNull(), eq(txid))).thenReturn(// simulate a failure
    failedCommit).thenReturn(// a success.
    CompletableFuture.completedFuture(null));
    @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);
    // invoke commit of transaction.
    try {
        txn.commit();
    } catch (Exception e) {
        assertTrue(e instanceof RetriesExhaustedException && e.getCause() instanceof StatusRuntimeException);
        // the user retries the commit since the TxnFailedException is not thrown.
        txn.commit();
    }
    // verify if segments are flushed and closed.
    Mockito.verify(outputStream, Mockito.times(1)).close();
    Mockito.verify(controller, Mockito.times(2)).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) SegmentSealedException(io.pravega.client.segment.impl.SegmentSealedException) RetriesExhaustedException(io.pravega.common.util.RetriesExhaustedException) StatusRuntimeException(io.grpc.StatusRuntimeException) TxnFailedException(io.pravega.client.stream.TxnFailedException) CompletableFuture(java.util.concurrent.CompletableFuture) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) RetriesExhaustedException(io.pravega.common.util.RetriesExhaustedException) StatusRuntimeException(io.grpc.StatusRuntimeException) FakeSegmentOutputStream(io.pravega.client.stream.impl.EventStreamWriterTest.FakeSegmentOutputStream) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

SegmentSealedException (io.pravega.client.segment.impl.SegmentSealedException)11 Segment (io.pravega.client.segment.impl.Segment)5 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)5 ByteBuffer (java.nio.ByteBuffer)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 AuthenticationException (io.pravega.auth.AuthenticationException)3 Controller (io.pravega.client.control.impl.Controller)3 NoSuchSegmentException (io.pravega.client.segment.impl.NoSuchSegmentException)3 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)3 RetriesExhaustedException (io.pravega.common.util.RetriesExhaustedException)3 ArrayList (java.util.ArrayList)3 UUID (java.util.UUID)3 ConnectionPool (io.pravega.client.connection.impl.ConnectionPool)2 DelegationTokenProvider (io.pravega.client.security.auth.DelegationTokenProvider)2 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)2 Serializer (io.pravega.client.stream.Serializer)2 TxnFailedException (io.pravega.client.stream.TxnFailedException)2 Exceptions (io.pravega.common.Exceptions)2 Retry (io.pravega.common.util.Retry)2 Cleanup (lombok.Cleanup)2