Search in sources :

Example 1 with RetryWithBackoff

use of io.pravega.common.util.Retry.RetryWithBackoff in project pravega by pravega.

the class SegmentOutputStreamTest method testConnectWithMultipleFailures.

@Test(timeout = 10000)
public void testConnectWithMultipleFailures() throws Exception {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    RetryWithBackoff retryConfig = Retry.withExpBackoff(1, 1, 4);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
    // Ensure task submitted to executor is run inline.
    implementAsDirectExecutor(executor);
    cf.setExecutor(executor);
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf, true);
    ClientConnection connection = mock(ClientConnection.class);
    InOrder verify = inOrder(connection);
    cf.provideConnection(uri, connection);
    SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, segmentSealedCallback, retryConfig, DelegationTokenProviderFactory.createWithEmptyToken());
    try {
        output.reconnect();
        verify.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
        // simulate a processing Failure and ensure SetupAppend is executed.
        ReplyProcessor processor = cf.getProcessor(uri);
        processor.processingFailure(new IOException());
        verify.verify(connection).close();
        verify.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
        verifyNoMoreInteractions(connection);
        processor.connectionDropped();
        verify.verify(connection).close();
        verify.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
        processor.wrongHost(new WireCommands.WrongHost(output.getRequestId(), SEGMENT, "newHost", "SomeException"));
        verify.verify(connection).close();
        verify.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
        verifyNoMoreInteractions(connection);
        processor.processingFailure(new IOException());
        assertTrue("Connection is  exceptionally closed with RetriesExhaustedException", output.getConnection().isCompletedExceptionally());
        AssertExtensions.assertThrows(RetriesExhaustedException.class, () -> Futures.getThrowingException(output.getConnection()));
        verify.verify(connection).close();
        verifyNoMoreInteractions(connection);
    } finally {
        // Verify that a close on the SegmentOutputStream does throw a RetriesExhaustedException.
        AssertExtensions.assertThrows(RetriesExhaustedException.class, output::close);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InOrder(org.mockito.InOrder) IOException(java.io.IOException) RetryWithBackoff(io.pravega.common.util.Retry.RetryWithBackoff) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) UUID(java.util.UUID) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 2 with RetryWithBackoff

use of io.pravega.common.util.Retry.RetryWithBackoff in project pravega by pravega.

the class SegmentOutputStreamTest method testFlushWithMultipleConnectFailures.

@Test(timeout = 10000)
public void testFlushWithMultipleConnectFailures() throws Exception {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    RetryWithBackoff retryConfig = Retry.withExpBackoff(1, 1, 1);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
    // Ensure task submitted to executor is run inline.
    implementAsDirectExecutor(executor);
    cf.setExecutor(executor);
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf, true);
    ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(uri, connection);
    SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, segmentSealedCallback, retryConfig, DelegationTokenProviderFactory.createWithEmptyToken());
    try {
        output.reconnect();
        verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
        // Simulate a successful connection setup.
        cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
        // try sending an event.
        byte[] eventData = "test data".getBytes();
        CompletableFuture<Void> acked = new CompletableFuture<>();
        // this is an inflight event and the client will track it until there is a response from SSS.
        output.write(PendingEvent.withoutHeader(null, ByteBuffer.wrap(eventData), acked));
        verify(connection).send(new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(eventData), null, output.getRequestId()));
        reset(connection);
        // simulate a connection drop and verify if the writer tries to establish a new connection.
        cf.getProcessor(uri).connectionDropped();
        verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
        reset(connection);
        // Verify flush blocks until there is a response from SSS. Incase of connection error the client retries. If the
        // retry count more than the configuration ensure flush returns exceptionally.
        AssertExtensions.assertBlocks(() -> AssertExtensions.assertThrows(RetriesExhaustedException.class, output::flush), () -> cf.getProcessor(uri).connectionDropped());
        assertTrue("Connection is  exceptionally closed with RetriesExhaustedException", output.getConnection().isCompletedExceptionally());
        AssertExtensions.assertThrows(RetriesExhaustedException.class, () -> Futures.getThrowingException(output.getConnection()));
        // Verify that the inflight event future is completed exceptionally.
        AssertExtensions.assertThrows(RetriesExhaustedException.class, () -> Futures.getThrowingException(acked));
    } finally {
        // Verify that a close on the SegmentOutputStream does throw a RetriesExhaustedException.
        AssertExtensions.assertThrows(RetriesExhaustedException.class, output::close);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RetryWithBackoff(io.pravega.common.util.Retry.RetryWithBackoff) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) CompletableFuture(java.util.concurrent.CompletableFuture) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) RetriesExhaustedException(io.pravega.common.util.RetriesExhaustedException) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) UUID(java.util.UUID) Test(org.junit.Test)

Example 3 with RetryWithBackoff

use of io.pravega.common.util.Retry.RetryWithBackoff in project pravega by pravega.

the class SegmentOutputStreamTest method testInflightWithMultipleConnectFailures.

@Test(timeout = 10000)
public void testInflightWithMultipleConnectFailures() throws Exception {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    RetryWithBackoff retryConfig = Retry.withExpBackoff(1, 1, 1);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
    // Ensure task submitted to executor is run inline.
    implementAsDirectExecutor(executor);
    cf.setExecutor(executor);
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf, true);
    ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(uri, connection);
    SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, segmentSealedCallback, retryConfig, DelegationTokenProviderFactory.createWithEmptyToken());
    try {
        output.reconnect();
        verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
        // Simulate a successful connection setup.
        cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
        // try sending an event.
        byte[] eventData = "test data".getBytes();
        CompletableFuture<Void> ack1 = new CompletableFuture<>();
        output.write(PendingEvent.withoutHeader(null, ByteBuffer.wrap(eventData), ack1));
        verify(connection).send(new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(eventData), null, output.getRequestId()));
        reset(connection);
        // simulate a connection drop and verify if the writer tries to establish a new connection.
        cf.getProcessor(uri).connectionDropped();
        verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
        reset(connection);
        // Simulate a connection drop again.
        cf.getProcessor(uri).connectionDropped();
        // Since we have exceeded the retry attempts verify we do not try to establish a connection.
        verify(connection, never()).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
        assertTrue("Connection is  exceptionally closed with RetriesExhaustedException", output.getConnection().isCompletedExceptionally());
        AssertExtensions.assertThrows(RetriesExhaustedException.class, () -> Futures.getThrowingException(output.getConnection()));
        // Verify that the inflight event future is completed exceptionally.
        AssertExtensions.assertThrows(RetriesExhaustedException.class, () -> Futures.getThrowingException(ack1));
        // Write an additional event to a writer that has failed with RetriesExhaustedException.
        CompletableFuture<Void> ack2 = new CompletableFuture<>();
        output.write(PendingEvent.withoutHeader(null, ByteBuffer.wrap(eventData), ack2));
        verify(connection, never()).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
        AssertExtensions.assertThrows(RetriesExhaustedException.class, () -> Futures.getThrowingException(ack2));
        // Verify that a flush on the SegmentOutputStream does throw a RetriesExhaustedException.
        AssertExtensions.assertThrows(RetriesExhaustedException.class, output::flush);
    } finally {
        // Verify that a close on the SegmentOutputStream does throw a RetriesExhaustedException.
        AssertExtensions.assertThrows(RetriesExhaustedException.class, output::close);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RetryWithBackoff(io.pravega.common.util.Retry.RetryWithBackoff) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) CompletableFuture(java.util.concurrent.CompletableFuture) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

ClientConnection (io.pravega.client.connection.impl.ClientConnection)3 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)3 MockController (io.pravega.client.stream.mock.MockController)3 RetryWithBackoff (io.pravega.common.util.Retry.RetryWithBackoff)3 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)3 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)3 UUID (java.util.UUID)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 Test (org.junit.Test)3 Append (io.pravega.shared.protocol.netty.Append)2 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 RetriesExhaustedException (io.pravega.common.util.RetriesExhaustedException)1 ReplyProcessor (io.pravega.shared.protocol.netty.ReplyProcessor)1 WireCommands (io.pravega.shared.protocol.netty.WireCommands)1 IOException (java.io.IOException)1 InOrder (org.mockito.InOrder)1