use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class SegmentOutputStreamTest method testNewEventsGoAfterInflight.
@Test(timeout = 20000)
public void testNewEventsGoAfterInflight() throws ConnectionFailedException {
UUID cid = UUID.randomUUID();
PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
cf.setExecutor(executorService());
MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf, true);
ClientConnection connection = mock(ClientConnection.class);
InOrder inOrder = inOrder(connection);
cf.provideConnection(uri, connection);
@SuppressWarnings("resource") SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
output.reconnect();
cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
output.write(PendingEvent.withoutHeader(null, getBuffer("test1"), new CompletableFuture<>()));
output.write(PendingEvent.withoutHeader(null, getBuffer("test2"), new CompletableFuture<>()));
answerSuccess(connection);
cf.getProcessor(uri).connectionDropped();
AssertExtensions.assertBlocks(() -> output.write(PendingEvent.withoutHeader(null, getBuffer("test3"), new CompletableFuture<>())), () -> cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0)));
output.write(PendingEvent.withoutHeader(null, getBuffer("test4"), new CompletableFuture<>()));
Append append1 = new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(getBuffer("test1")), null, output.getRequestId());
Append append2 = new Append(SEGMENT, cid, 2, 1, Unpooled.wrappedBuffer(getBuffer("test2")), null, output.getRequestId());
Append append3 = new Append(SEGMENT, cid, 3, 1, Unpooled.wrappedBuffer(getBuffer("test3")), null, output.getRequestId());
Append append4 = new Append(SEGMENT, cid, 4, 1, Unpooled.wrappedBuffer(getBuffer("test4")), null, output.getRequestId());
inOrder.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
inOrder.verify(connection).send(append1);
inOrder.verify(connection).send(append2);
inOrder.verify(connection).close();
inOrder.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
inOrder.verify(connection).sendAsync(eq(ImmutableList.of(append1, append2)), any());
inOrder.verify(connection).send(append3);
inOrder.verify(connection).send(append4);
verifyNoMoreInteractions(connection);
}
use of io.pravega.client.connection.impl.ClientConnection 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);
}
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class SegmentOutputStreamTest method testReconnectOnMissedAcks.
@Test(timeout = 10000)
public void testReconnectOnMissedAcks() throws ConnectionFailedException {
UUID cid = UUID.randomUUID();
PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
MockConnectionFactoryImpl cf = spy(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);
InOrder order = Mockito.inOrder(connection);
@SuppressWarnings("resource") SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
output.reconnect();
order.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
ByteBuffer data = getBuffer("test");
CompletableFuture<Void> acked1 = new CompletableFuture<>();
output.write(PendingEvent.withoutHeader(null, data, acked1));
order.verify(connection).send(new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId()));
assertEquals(false, acked1.isDone());
AssertExtensions.assertBlocks(() -> output.flush(), () -> cf.getProcessor(uri).dataAppended(new WireCommands.DataAppended(output.getRequestId(), cid, 1, 0, -1)));
assertEquals(false, acked1.isCompletedExceptionally());
assertEquals(true, acked1.isDone());
order.verify(connection).send(new WireCommands.KeepAlive());
// simulate missed ack
CompletableFuture<Void> acked2 = new CompletableFuture<>();
output.write(PendingEvent.withoutHeader(null, data, acked2));
order.verify(connection).send(new Append(SEGMENT, cid, 2, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId()));
assertEquals(false, acked2.isDone());
cf.getProcessor(uri).dataAppended(new WireCommands.DataAppended(output.getRequestId(), cid, 3, 2L, -1));
// check that client reconnected
verify(cf, times(2)).establishConnection(any(), any());
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class SegmentOutputStreamTest method testRecvErrorMessage.
@Test(timeout = 10000)
public void testRecvErrorMessage() throws SegmentSealedException {
int requestId = 0;
UUID cid = UUID.randomUUID();
PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
cf.setExecutor(executorService());
MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf, true);
ClientConnection connection = mock(ClientConnection.class);
cf.provideConnection(uri, connection);
@Cleanup SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
output.reconnect();
ReplyProcessor processor = cf.getProcessor(uri);
WireCommands.ErrorMessage reply = new WireCommands.ErrorMessage(requestId, "segment", "error.", WireCommands.ErrorMessage.ErrorCode.ILLEGAL_ARGUMENT_EXCEPTION);
processor.process(reply);
verify(connection).close();
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class SegmentOutputStreamTest method testConnectionFailure.
@Test(timeout = 10000)
public void testConnectionFailure() throws Exception {
UUID cid = UUID.randomUUID();
PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
cf.setExecutor(executorService());
MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf, true);
ClientConnection connection = mock(ClientConnection.class);
cf.provideConnection(uri, connection);
@SuppressWarnings("resource") SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
output.reconnect();
InOrder inOrder = Mockito.inOrder(connection);
inOrder.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
ByteBuffer data = getBuffer("test");
CompletableFuture<Void> acked = new CompletableFuture<>();
Append append = new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId());
CompletableFuture<Void> acked2 = new CompletableFuture<>();
Append append2 = new Append(SEGMENT, cid, 2, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId());
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
cf.getProcessor(uri).connectionDropped();
throw new ConnectionFailedException();
}
}).when(connection).send(append);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
CompletedCallback callback = (CompletedCallback) invocation.getArgument(1);
callback.complete(null);
return null;
}
}).when(connection).sendAsync(Mockito.eq(Collections.singletonList(append)), Mockito.any());
AssertExtensions.assertBlocks(() -> {
output.write(PendingEvent.withoutHeader(null, data, acked));
output.write(PendingEvent.withoutHeader(null, data, acked2));
}, () -> {
cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
});
inOrder.verify(connection).send(append);
inOrder.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
inOrder.verify(connection).sendAsync(Mockito.eq(Collections.singletonList(append)), Mockito.any());
inOrder.verify(connection).send(append2);
assertEquals(false, acked.isDone());
assertEquals(false, acked2.isDone());
inOrder.verifyNoMoreInteractions();
}
Aggregations