use of io.pravega.shared.protocol.netty.PravegaNodeUri in project pravega by pravega.
the class ReaderGroupStateManagerTest method testSegmentMerge.
@Test(timeout = 20000)
public void testSegmentMerge() throws ReinitializationRequiredException {
String scope = "scope";
String stream = "stream";
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
Segment initialSegmentA = new Segment(scope, stream, 0);
Segment initialSegmentB = new Segment(scope, stream, 1);
Segment successor = new Segment(scope, stream, 2);
MockController controller = new MockControllerWithSuccessors(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, new StreamSegmentsWithPredecessors(Collections.singletonMap(new SegmentWithRange(successor, 0.0, 1.0), ImmutableList.of(0, 1)), ""));
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
@Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
SynchronizerConfig config = SynchronizerConfig.builder().build();
@Cleanup StateSynchronizer<ReaderGroupState> stateSynchronizer = createState(stream, clientFactory, config);
Map<Segment, Long> segments = new HashMap<>();
segments.put(initialSegmentA, 1L);
segments.put(initialSegmentB, 2L);
ReaderGroupStateManager.initializeReaderGroup(stateSynchronizer, ReaderGroupConfig.builder().stream(Stream.of(scope, stream)).build(), segments);
val readerState = new ReaderGroupStateManager("testReader", stateSynchronizer, controller, null);
readerState.initializeReader(0);
Map<Segment, Long> newSegments = readerState.acquireNewSegmentsIfNeeded(0);
assertEquals(2, newSegments.size());
assertEquals(Long.valueOf(1), newSegments.get(initialSegmentA));
assertEquals(Long.valueOf(2), newSegments.get(initialSegmentB));
readerState.handleEndOfSegment(initialSegmentA);
newSegments = readerState.acquireNewSegmentsIfNeeded(0);
assertTrue(newSegments.isEmpty());
readerState.handleEndOfSegment(initialSegmentB);
newSegments = readerState.acquireNewSegmentsIfNeeded(0);
assertEquals(1, newSegments.size());
assertEquals(Long.valueOf(0), newSegments.get(successor));
newSegments = readerState.acquireNewSegmentsIfNeeded(0);
assertTrue(newSegments.isEmpty());
}
use of io.pravega.shared.protocol.netty.PravegaNodeUri in project pravega by pravega.
the class MockController method deleteStream.
@Override
@Synchronized
public CompletableFuture<Boolean> deleteStream(String scope, String streamName) {
Stream stream = new StreamImpl(scope, streamName);
if (createdStreams.get(stream) == null) {
return CompletableFuture.completedFuture(false);
}
for (Segment segment : getSegmentsForStream(stream)) {
deleteSegment(segment.getScopedName(), new PravegaNodeUri(endpoint, port));
}
createdStreams.remove(stream);
createdScopes.get(scope).remove(stream);
return CompletableFuture.completedFuture(true);
}
use of io.pravega.shared.protocol.netty.PravegaNodeUri in project pravega by pravega.
the class MockController method sendRequestOverNewConnection.
private <T> void sendRequestOverNewConnection(WireCommand request, ReplyProcessor replyProcessor, CompletableFuture<T> resultFuture) {
ClientConnection connection = getAndHandleExceptions(connectionFactory.establishConnection(new PravegaNodeUri(endpoint, port), replyProcessor), RuntimeException::new);
resultFuture.whenComplete((result, e) -> {
connection.close();
});
try {
connection.send(request);
} catch (Exception e) {
resultFuture.completeExceptionally(e);
}
}
use of io.pravega.shared.protocol.netty.PravegaNodeUri 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.PravegaNodeUri in project pravega by pravega.
the class ControllerImpl method getEndpointForSegment.
@Override
public CompletableFuture<PravegaNodeUri> getEndpointForSegment(final String qualifiedSegmentName) {
Exceptions.checkNotClosed(closed.get(), this);
Exceptions.checkNotNullOrEmpty(qualifiedSegmentName, "qualifiedSegmentName");
long traceId = LoggerHelpers.traceEnter(log, "getEndpointForSegment", qualifiedSegmentName);
final CompletableFuture<NodeUri> result = this.retryConfig.runAsync(() -> {
RPCAsyncCallback<NodeUri> callback = new RPCAsyncCallback<>();
Segment segment = Segment.fromScopedName(qualifiedSegmentName);
client.getURI(ModelHelper.createSegmentId(segment.getScope(), segment.getStreamName(), segment.getSegmentNumber()), callback);
return callback.getFuture();
}, this.executor);
return result.thenApply(ModelHelper::encode).whenComplete((x, e) -> {
if (e != null) {
log.warn("getEndpointForSegment failed: ", e);
}
LoggerHelpers.traceLeave(log, "getEndpointForSegment", traceId);
});
}
Aggregations