use of io.pravega.client.netty.impl.ClientConnection in project pravega by pravega.
the class SegmentOutputStreamImpl method reconnect.
@VisibleForTesting
void reconnect() {
if (state.isClosed()) {
return;
}
log.debug("(Re)connect invoked, Segment: {}, writerID: {}", segmentName, writerId);
state.setupConnection.registerAndRunReleaser(() -> {
Retry.indefinitelyWithExpBackoff(retrySchedule.getInitialMillis(), retrySchedule.getMultiplier(), retrySchedule.getMaxDelay(), t -> log.warn(writerId + " Failed to connect: ", t)).runAsync(() -> {
log.debug("Running reconnect for segment:{} writerID: {}", segmentName, writerId);
if (state.isClosed() || state.isAlreadySealed()) {
return CompletableFuture.completedFuture(null);
}
Preconditions.checkState(state.getConnection() == null);
log.info("Fetching endpoint for segment {}, writerID: {}", segmentName, writerId);
return controller.getEndpointForSegment(segmentName).thenComposeAsync((PravegaNodeUri uri) -> {
log.info("Establishing connection to {} for {}, writerID: {}", uri, segmentName, writerId);
return connectionFactory.establishConnection(uri, responseProcessor);
}, connectionFactory.getInternalExecutor()).thenComposeAsync(connection -> {
CompletableFuture<Void> connectionSetupFuture = state.newConnection(connection);
SetupAppend cmd = new SetupAppend(requestIdGenerator.get(), writerId, segmentName, delegationToken);
try {
connection.send(cmd);
} catch (ConnectionFailedException e1) {
// This needs to be invoked here because call to failConnection from netty may occur before state.newConnection above.
state.failConnection(e1);
throw Lombok.sneakyThrow(e1);
}
return connectionSetupFuture.exceptionally(t -> {
if (Exceptions.unwrap(t) instanceof SegmentSealedException) {
log.info("Ending reconnect attempts to {} because segment is sealed", segmentName);
return null;
}
throw Lombok.sneakyThrow(t);
});
}, connectionFactory.getInternalExecutor());
}, connectionFactory.getInternalExecutor());
}, new CompletableFuture<ClientConnection>());
}
use of io.pravega.client.netty.impl.ClientConnection in project pravega by pravega.
the class SegmentOutputStreamImpl method close.
/**
* @see SegmentOutputStream#close()
*/
@Override
public void close() throws SegmentSealedException {
if (state.isClosed()) {
return;
}
log.debug("Closing writer: {}", writerId);
// Wait until all the inflight events are written
flush();
state.setClosed(true);
ClientConnection connection = state.getConnection();
if (connection != null) {
connection.close();
}
}
use of io.pravega.client.netty.impl.ClientConnection in project pravega by pravega.
the class SegmentMetadataClientTest method testCurrentStreamLength.
@Test(timeout = 10000)
public void testCurrentStreamLength() throws Exception {
Segment segment = new Segment("scope", "testRetry", 4);
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
@Cleanup MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf);
@Cleanup ClientConnection connection = mock(ClientConnection.class);
cf.provideConnection(endpoint, connection);
SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, "");
client.getConnection();
ReplyProcessor processor = cf.getProcessor(endpoint);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
processor.process(new StreamSegmentInfo(1, segment.getScopedName(), true, false, false, 0, 123, 121));
return null;
}
}).when(connection).send(new WireCommands.GetStreamSegmentInfo(1, segment.getScopedName(), ""));
long length = client.fetchCurrentSegmentLength();
assertEquals(123, length);
}
use of io.pravega.client.netty.impl.ClientConnection in project pravega by pravega.
the class SegmentMetadataClientTest method compareAndSetAttribute.
@Test(timeout = 10000)
public void compareAndSetAttribute() throws Exception {
UUID attributeId = SegmentAttribute.RevisionStreamClientMark.getValue();
Segment segment = new Segment("scope", "testRetry", 4);
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf);
ClientConnection connection = mock(ClientConnection.class);
cf.provideConnection(endpoint, connection);
SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, "");
client.getConnection();
ReplyProcessor processor = cf.getProcessor(endpoint);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
processor.process(new SegmentAttributeUpdated(1, true));
return null;
}
}).when(connection).send(new WireCommands.UpdateSegmentAttribute(1, segment.getScopedName(), attributeId, 1234, -1234, ""));
assertTrue(client.compareAndSetAttribute(SegmentAttribute.RevisionStreamClientMark, -1234, 1234));
}
use of io.pravega.client.netty.impl.ClientConnection in project pravega by pravega.
the class SegmentOutputStreamTest method testFlush.
@Test(timeout = 10000)
public void testFlush() throws ConnectionFailedException, SegmentSealedException {
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> acked1 = new CompletableFuture<>();
output.write(new PendingEvent(null, data, acked1));
order.verify(connection).send(new Append(SEGMENT, cid, 1, Unpooled.wrappedBuffer(data), null));
assertEquals(false, acked1.isDone());
Async.testBlocking(() -> output.flush(), () -> cf.getProcessor(uri).dataAppended(new WireCommands.DataAppended(cid, 1, 0)));
assertEquals(false, acked1.isCompletedExceptionally());
assertEquals(true, acked1.isDone());
order.verify(connection).send(new WireCommands.KeepAlive());
CompletableFuture<Boolean> acked2 = new CompletableFuture<>();
output.write(new PendingEvent(null, data, acked2));
order.verify(connection).send(new Append(SEGMENT, cid, 2, Unpooled.wrappedBuffer(data), null));
assertEquals(false, acked2.isDone());
Async.testBlocking(() -> output.flush(), () -> cf.getProcessor(uri).dataAppended(new WireCommands.DataAppended(cid, 2, 1)));
assertEquals(false, acked2.isCompletedExceptionally());
assertEquals(true, acked2.isDone());
order.verify(connection).send(new WireCommands.KeepAlive());
order.verifyNoMoreInteractions();
}
Aggregations