use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class SegmentOutputStreamTest method testSealedAfterFlush.
@Test(timeout = 10000)
public void testSealedAfterFlush() 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);
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(output.getRequestId(), SEGMENT, cid, 0));
ByteBuffer data = getBuffer("test");
CompletableFuture<Void> ack = new CompletableFuture<>();
output.write(PendingEvent.withoutHeader(null, data, ack));
order.verify(connection).send(new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId()));
assertEquals(false, ack.isDone());
AssertExtensions.assertBlocks(() -> {
AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.flush());
}, () -> {
cf.getProcessor(uri).segmentIsSealed(new WireCommands.SegmentIsSealed(output.getRequestId(), SEGMENT, "SomeException", 1));
output.getUnackedEventsOnSeal();
});
AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.flush());
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class SegmentOutputStreamTest method testCloseDuringTransactionAbort.
@Test(timeout = 10000)
public void testCloseDuringTransactionAbort() 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);
InOrder order = Mockito.inOrder(connection);
SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(TXN_SEGMENT, true, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
output.reconnect();
order.verify(connection).send(new SetupAppend(output.getRequestId(), cid, TXN_SEGMENT, ""));
cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), TXN_SEGMENT, cid, 0));
ByteBuffer data = getBuffer("test");
// Write an Event.
CompletableFuture<Void> ack1 = new CompletableFuture<>();
output.write(PendingEvent.withoutHeader(null, data, ack1));
order.verify(connection).send(new Append(TXN_SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId()));
// writer is not complete until a response from Segment Store is received.
assertFalse(ack1.isDone());
// Simulate a NoSuchSegment response from SegmentStore due to a Transaction abort.
cf.getProcessor(uri).noSuchSegment(new WireCommands.NoSuchSegment(output.getRequestId(), TXN_SEGMENT, "SomeException", -1L));
// Trigger a second write.
CompletableFuture<Void> ack2 = new CompletableFuture<>();
output.write(PendingEvent.withoutHeader(null, data, ack2));
// Closing the Segment writer should cause a SegmentSealedException.
AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.close());
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class SegmentOutputStreamTest method testFailDuringFlush.
@Test(timeout = 10000)
public void testFailDuringFlush() throws Exception {
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);
cf.provideConnection(uri, connection);
InOrder order = Mockito.inOrder(connection);
@Cleanup 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(output.getRequestId(), SEGMENT, cid, 0));
ByteBuffer data = getBuffer("test");
CompletableFuture<Void> ack = new CompletableFuture<>();
output.write(PendingEvent.withoutHeader(null, data, ack));
order.verify(connection).send(new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId()));
assertEquals(false, ack.isDone());
final CountDownLatch connectionDroppedLatch = new CountDownLatch(1);
Mockito.doThrow(new ConnectionFailedException()).when(connection).send(new WireCommands.KeepAlive());
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
// The segment writer will try to reconnect once the connection is failed post sending a KeepAlive.
// enable a response for AppendSetup only after the connection dropped is dropped.
connectionDroppedLatch.await();
cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 1));
return null;
}
}).when(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
AssertExtensions.assertBlocks(() -> {
output.flush();
}, () -> {
cf.getProcessor(uri).connectionDropped();
connectionDroppedLatch.countDown();
});
order.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
assertEquals(true, ack.isDone());
}
use of io.pravega.client.connection.impl.ClientConnection 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 in project pravega by pravega.
the class SegmentOutputStreamTest method testReconnectDoesNotSetupAppendOnTokenCheckFailure.
@Test(timeout = 10000)
public void testReconnectDoesNotSetupAppendOnTokenCheckFailure() throws ConnectionFailedException, SegmentSealedException {
UUID writerId = UUID.randomUUID();
PravegaNodeUri segmentStoreUri = new PravegaNodeUri("endpoint", SERVICE_PORT);
MockConnectionFactoryImpl mockConnectionFactory = spy(new MockConnectionFactoryImpl());
ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
// Ensure task submitted to executor is run inline.
implementAsDirectExecutor(executor);
mockConnectionFactory.setExecutor(executor);
MockController controller = new MockController(segmentStoreUri.getEndpoint(), segmentStoreUri.getPort(), mockConnectionFactory, true);
ClientConnection mockConnection = mock(ClientConnection.class);
mockConnectionFactory.provideConnection(segmentStoreUri, mockConnection);
@Cleanup SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, mockConnectionFactory, writerId, segmentSealedCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
output.reconnect();
verify(mockConnection).send(new SetupAppend(output.getRequestId(), writerId, SEGMENT, ""));
reset(mockConnection);
// Simulate token check failure
WireCommands.AuthTokenCheckFailed authTokenCheckFailed = new WireCommands.AuthTokenCheckFailed(1, "server-stacktrace", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_CHECK_FAILED);
mockConnectionFactory.getProcessor(segmentStoreUri).authTokenCheckFailed(authTokenCheckFailed);
// Verify that SetupAppend is NOT sent. We expect the client to get an exception.
verify(mockConnection, times(0)).send(new SetupAppend(output.getRequestId(), writerId, SEGMENT, ""));
}
Aggregations