use of io.pravega.client.netty.impl.ClientConnection 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.client.netty.impl.ClientConnection 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.client.netty.impl.ClientConnection in project pravega by pravega.
the class SegmentOutputStreamImpl method flush.
/**
* @see SegmentOutputStream#flush()
*/
@Override
public void flush() throws SegmentSealedException {
if (!state.isInflightEmpty()) {
log.debug("Flushing writer: {}", writerId);
try {
ClientConnection connection = Futures.getThrowingException(getConnection());
connection.send(new KeepAlive());
} catch (Exception e) {
failConnection(e);
}
state.waitForInflight();
Exceptions.checkNotClosed(state.isClosed(), this);
if (state.isAlreadySealed()) {
throw new SegmentSealedException(segmentName + " sealed");
}
}
}
use of io.pravega.client.netty.impl.ClientConnection in project pravega by pravega.
the class BatchClientImplTest method testSegmentIterator.
@Test(timeout = 5000)
public void testSegmentIterator() throws ConnectionFailedException {
MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
ClientConnection connection = Mockito.mock(ClientConnection.class);
PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
CreateSegment request = (CreateSegment) invocation.getArgument(0);
connectionFactory.getProcessor(location).process(new SegmentCreated(request.getRequestId(), request.getSegment()));
return null;
}
}).when(connection).send(Mockito.any(CreateSegment.class));
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
GetStreamSegmentInfo request = (GetStreamSegmentInfo) invocation.getArgument(0);
connectionFactory.getProcessor(location).process(new StreamSegmentInfo(request.getRequestId(), request.getSegmentName(), true, false, false, 0, 0, 0));
return null;
}
}).when(connection).send(Mockito.any(GetStreamSegmentInfo.class));
connectionFactory.provideConnection(location, connection);
MockController mockController = new MockController(location.getEndpoint(), location.getPort(), connectionFactory);
BatchClientImpl client = new BatchClientImpl(mockController, connectionFactory);
Stream stream = new StreamImpl("scope", "stream");
mockController.createScope("scope");
mockController.createStream(StreamConfiguration.builder().scope("scope").streamName("stream").scalingPolicy(ScalingPolicy.fixed(3)).build()).join();
Iterator<SegmentRange> segments = client.getSegments(stream, null, null).getIterator();
assertTrue(segments.hasNext());
assertEquals(0, segments.next().asImpl().getSegment().getSegmentNumber());
assertTrue(segments.hasNext());
assertEquals(1, segments.next().asImpl().getSegment().getSegmentNumber());
assertTrue(segments.hasNext());
assertEquals(2, segments.next().asImpl().getSegment().getSegmentNumber());
assertFalse(segments.hasNext());
}
use of io.pravega.client.netty.impl.ClientConnection in project pravega by pravega.
the class AsyncSegmentInputStreamTest method testRead.
@Test(timeout = 10000)
public void testRead() throws ConnectionFailedException {
Segment segment = new Segment("scope", "testRead", 1);
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
@Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, "");
ClientConnection c = mock(ClientConnection.class);
connectionFactory.provideConnection(endpoint, c);
WireCommands.SegmentRead segmentRead = new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, ByteBuffer.allocate(0));
CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
Async.testBlocking(() -> readFuture.get(), () -> {
ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
processor.segmentRead(segmentRead);
});
verify(c).sendAsync(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, ""));
assertTrue(Futures.isSuccessful(readFuture));
assertEquals(segmentRead, readFuture.join());
verifyNoMoreInteractions(c);
}
Aggregations