use of io.pravega.shared.protocol.netty.ConnectionFailedException in project pravega by pravega.
the class SegmentOutputStreamTest method testFailDurringFlush.
@Test(timeout = 10000)
public void testFailDurringFlush() 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);
ClientConnection connection = mock(ClientConnection.class);
cf.provideConnection(uri, connection);
InOrder order = Mockito.inOrder(connection);
SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, "");
output.reconnect();
order.verify(connection).send(new SetupAppend(1, cid, SEGMENT, ""));
cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
ByteBuffer data = getBuffer("test");
CompletableFuture<Boolean> ack = new CompletableFuture<>();
output.write(new PendingEvent(null, data, ack));
order.verify(connection).send(new Append(SEGMENT, cid, 1, Unpooled.wrappedBuffer(data), null));
assertEquals(false, ack.isDone());
Mockito.doThrow(new ConnectionFailedException()).when(connection).send(new WireCommands.KeepAlive());
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
cf.getProcessor(uri).appendSetup(new AppendSetup(3, SEGMENT, cid, 1));
return null;
}
}).when(connection).send(new SetupAppend(3, cid, SEGMENT, ""));
Async.testBlocking(() -> {
output.flush();
}, () -> {
cf.getProcessor(uri).connectionDropped();
});
order.verify(connection).send(new SetupAppend(3, cid, SEGMENT, ""));
assertEquals(true, ack.isDone());
}
use of io.pravega.shared.protocol.netty.ConnectionFailedException in project pravega by pravega.
the class SegmentHelper method sendRequestAsync.
private <ResultT> void sendRequestAsync(final WireCommand request, final ReplyProcessor replyProcessor, final CompletableFuture<ResultT> resultFuture, final ConnectionFactory connectionFactory, final PravegaNodeUri uri) {
CompletableFuture<ClientConnection> connectionFuture = connectionFactory.establishConnection(uri, replyProcessor);
connectionFuture.whenComplete((connection, e) -> {
if (connection == null) {
resultFuture.completeExceptionally(new WireCommandFailedException(new ConnectionFailedException(e), request.getType(), WireCommandFailedException.Reason.ConnectionFailed));
} else {
try {
connection.send(request);
} catch (ConnectionFailedException cfe) {
throw new WireCommandFailedException(cfe, request.getType(), WireCommandFailedException.Reason.ConnectionFailed);
} catch (Exception e2) {
throw new RuntimeException(e2);
}
}
}).exceptionally(e -> {
Throwable cause = Exceptions.unwrap(e);
if (cause instanceof WireCommandFailedException) {
resultFuture.completeExceptionally(cause);
} else if (cause instanceof ConnectionFailedException) {
resultFuture.completeExceptionally(new WireCommandFailedException(cause, request.getType(), WireCommandFailedException.Reason.ConnectionFailed));
} else {
resultFuture.completeExceptionally(new RuntimeException(cause));
}
return null;
});
resultFuture.whenComplete((result, e) -> {
connectionFuture.thenAccept(ClientConnection::close);
});
}
use of io.pravega.shared.protocol.netty.ConnectionFailedException in project pravega by pravega.
the class ClientConnectionInboundHandler method sendAsync.
@Override
public void sendAsync(WireCommand cmd) throws ConnectionFailedException {
recentMessage.set(true);
Channel channel = getChannel();
try {
channel.writeAndFlush(cmd, channel.voidPromise());
} catch (RuntimeException e) {
throw new ConnectionFailedException(e);
}
}
use of io.pravega.shared.protocol.netty.ConnectionFailedException in project pravega by pravega.
the class SegmentOutputStreamImpl method write.
/**
* @see SegmentOutputStream#write(PendingEvent)
*/
@Override
public void write(PendingEvent event) {
checkState(!state.isAlreadySealed(), "Segment: %s is already sealed", segmentName);
synchronized (writeOrderLock) {
ClientConnection connection;
try {
// if connection is null getConnection() establishes a connection and retransmits all events in inflight
// list.
connection = Futures.getThrowingException(getConnection());
} catch (SegmentSealedException e) {
// Add the event to inflight and indicate to the caller that the segment is sealed.
state.addToInflight(event);
return;
}
long eventNumber = state.addToInflight(event);
try {
Append append = new Append(segmentName, writerId, eventNumber, Unpooled.wrappedBuffer(event.getData()), event.getExpectedOffset());
log.trace("Sending append request: {}", append);
connection.send(append);
} catch (ConnectionFailedException e) {
log.warn("Connection " + writerId + " failed due to: ", e);
// As the message is inflight, this will perform the retransmission.
reconnect();
}
}
}
use of io.pravega.shared.protocol.netty.ConnectionFailedException in project pravega by pravega.
the class SegmentInputStreamTest method testTimeout.
@Test(timeout = 10000)
public void testTimeout() throws EndOfSegmentException, SegmentTruncatedException {
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ByteBuffer wireData = createEventFromData(data);
TestAsyncSegmentInputStream fakeNetwork = new TestAsyncSegmentInputStream(segment, 7);
@Cleanup SegmentInputStreamImpl stream = new SegmentInputStreamImpl(fakeNetwork, 0);
testBlocking(() -> stream.read(), () -> fakeNetwork.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice())));
ByteBuffer read = stream.read(10);
assertNull(read);
fakeNetwork.completeExceptionally(1, new ConnectionFailedException());
AssertExtensions.assertThrows(ConnectionFailedException.class, () -> stream.read());
stream.read(10);
assertNull(read);
read = stream.read(10);
assertNull(read);
}
Aggregations