use of io.pravega.client.connection.impl.ClientConnection.CompletedCallback in project pravega by pravega.
the class SegmentOutputStreamTest method testFailOnClose.
/**
* Verifies that if a exception is encountered while flushing data inside of close, the
* connection is reestablished and the data is retransmitted before close returns.
*/
@Test
public void testFailOnClose() 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);
cf.provideConnection(uri, connection);
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");
// Prep mock: the mockito doAnswers setup below are triggered during the close inside of the testBlocking() call.
CompletableFuture<Void> acked = new CompletableFuture<>();
Append append = new Append(SEGMENT, cid, 1, 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();
}
}).doNothing().when(connection).send(new WireCommands.KeepAlive());
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());
// Queue up event.
output.write(PendingEvent.withoutHeader(null, data, acked));
inOrder.verify(connection).send(append);
// Verify behavior
AssertExtensions.assertBlocks(() -> {
output.close();
}, () -> {
// close is unblocked once the connection is setup and data is appended on Segment store.
cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
cf.getProcessor(uri).dataAppended(new WireCommands.DataAppended(output.getRequestId(), cid, 1, 0, -1));
});
// Verify the order of WireCommands sent.
inOrder.verify(connection).send(new WireCommands.KeepAlive());
// Two SetupAppend WireCommands are sent since the connection is dropped right after the first KeepAlive WireCommand is sent.
// The second SetupAppend WireCommand is sent while trying to re-establish connection.
inOrder.verify(connection, times(2)).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
// Ensure the pending append is sent over the connection. The exact verification of the append data is performed while setting up
// the when clause of setting up append.
inOrder.verify(connection).sendAsync(Mockito.anyList(), Mockito.any());
inOrder.verify(connection).close();
assertEquals(true, acked.isDone());
inOrder.verifyNoMoreInteractions();
}
use of io.pravega.client.connection.impl.ClientConnection.CompletedCallback in project pravega by pravega.
the class SegmentOutputStreamTest method testFailureWhileRetransmittingInflight.
@Test(timeout = 10000)
public void testFailureWhileRetransmittingInflight() throws ConnectionFailedException {
UUID cid = UUID.randomUUID();
PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
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 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(output.getRequestId(), SEGMENT, cid, 0));
output.write(PendingEvent.withoutHeader(null, getBuffer("test1"), new CompletableFuture<>()));
output.write(PendingEvent.withoutHeader(null, getBuffer("test2"), new CompletableFuture<>()));
doAnswer(new Answer<Void>() {
boolean failed = false;
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
CompletedCallback callback = (CompletedCallback) invocation.getArgument(1);
if (failed) {
callback.complete(null);
} else {
failed = true;
callback.complete(new ConnectionFailedException("Injected"));
}
return null;
}
}).when(connection).sendAsync(ArgumentMatchers.<List<Append>>any(), Mockito.any(CompletedCallback.class));
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
return null;
}
}).doNothing().when(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
cf.getProcessor(uri).connectionDropped();
AssertExtensions.assertBlocks(() -> output.write(PendingEvent.withoutHeader(null, getBuffer("test3"), new CompletableFuture<>())), () -> {
// the write should be blocked until the appendSetup is returned by the Segment stores.
cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), 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).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.CompletedCallback 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