use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class ControllerImpl method getSegmentsAtTime.
@Override
public CompletableFuture<Map<Segment, Long>> getSegmentsAtTime(final Stream stream, final long timestamp) {
Exceptions.checkNotClosed(closed.get(), this);
Preconditions.checkNotNull(stream, "stream");
long traceId = LoggerHelpers.traceEnter(log, "getSegmentsAtTime", stream, timestamp);
final CompletableFuture<SegmentsAtTime> result = this.retryConfig.runAsync(() -> {
RPCAsyncCallback<SegmentsAtTime> callback = new RPCAsyncCallback<>();
StreamInfo streamInfo = ModelHelper.createStreamInfo(stream.getScope(), stream.getStreamName());
GetSegmentsRequest request = GetSegmentsRequest.newBuilder().setStreamInfo(streamInfo).setTimestamp(timestamp).build();
client.getSegments(request, callback);
return callback.getFuture();
}, this.executor);
return result.thenApply(segments -> {
log.debug("Received the following data from the controller {}", segments.getSegmentsList());
return segments.getSegmentsList().stream().collect(Collectors.toMap(location -> ModelHelper.encode(location.getSegmentId()), location -> location.getOffset()));
}).whenComplete((x, e) -> {
if (e != null) {
log.warn("getSegmentsAtTime failed: ", e);
}
LoggerHelpers.traceLeave(log, "getSegmentsAtTime", traceId);
});
}
use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class ControllerImpl method getSegmentsInRange.
private Set<Segment> getSegmentsInRange(final StreamCut lowerBound, final Collection<Segment> upperBound) {
// input validation.
val fromSCSummary = lowerBound.asImpl().getPositions().keySet().stream().collect(summarizingInt(Segment::getSegmentNumber));
val toSCSummary = upperBound.stream().collect(summarizingInt(Segment::getSegmentNumber));
Preconditions.checkArgument(fromSCSummary.getMin() <= toSCSummary.getMin(), "Overlapping StreamCuts cannot be provided");
Preconditions.checkArgument(fromSCSummary.getMax() <= toSCSummary.getMax(), "Overlapping StreamCuts cannot be provided");
final HashSet<Segment> segments = new HashSet<>(upperBound);
segments.addAll(getKnownSegmentsInRange(lowerBound, upperBound));
ArrayDeque<Segment> toFetchSuccessors = new ArrayDeque<>();
for (Segment toFetch : lowerBound.asImpl().getPositions().keySet()) {
if (!segments.contains(toFetch)) {
toFetchSuccessors.add(toFetch);
}
}
segments.addAll(lowerBound.asImpl().getPositions().keySet());
while (!toFetchSuccessors.isEmpty()) {
Segment segment = toFetchSuccessors.remove();
Set<Segment> successors = getAndHandleExceptions(getSuccessors(segment), RuntimeException::new).getSegmentToPredecessor().keySet();
for (Segment successor : successors) {
// Successor segment number can never be larger than the highest segment number in upperBound.
Preconditions.checkArgument(successor.getSegmentNumber() <= toSCSummary.getMax(), "Overlapping streamCuts, lowerBound streamCut should be strictly lower than the upperBound StreamCut.");
if (!segments.contains(successor)) {
segments.add(successor);
toFetchSuccessors.add(successor);
}
}
}
return segments;
}
use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class EventStreamReaderImpl method acquireSegmentsIfNeeded.
@GuardedBy("readers")
private void acquireSegmentsIfNeeded() throws ReinitializationRequiredException {
Map<Segment, Long> newSegments = groupState.acquireNewSegmentsIfNeeded(getLag());
if (!newSegments.isEmpty()) {
log.info("{} acquiring segments {}", this, newSegments);
for (Entry<Segment, Long> newSegment : newSegments.entrySet()) {
SegmentInputStream in = inputStreamFactory.createInputStreamForSegment(newSegment.getKey());
in.setOffset(newSegment.getValue());
readers.add(in);
}
}
}
use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class EventStreamReaderImpl method readNextEvent.
@Override
public EventRead<Type> readNextEvent(long timeout) throws ReinitializationRequiredException, TruncatedDataException {
synchronized (readers) {
Preconditions.checkState(!closed, "Reader is closed");
long waitTime = Math.min(timeout, ReaderGroupStateManager.TIME_UNIT.toMillis());
Timer timer = new Timer();
Segment segment = null;
long offset = -1;
ByteBuffer buffer;
do {
String checkpoint = updateGroupStateIfNeeded();
if (checkpoint != null) {
return createEmptyEvent(checkpoint);
}
SegmentInputStream segmentReader = orderer.nextSegment(readers);
if (segmentReader == null) {
Exceptions.handleInterrupted(() -> Thread.sleep(waitTime));
buffer = null;
} else {
segment = segmentReader.getSegmentId();
offset = segmentReader.getOffset();
try {
buffer = segmentReader.read(waitTime);
} catch (EndOfSegmentException e) {
handleEndOfSegment(segmentReader);
buffer = null;
} catch (SegmentTruncatedException e) {
handleSegmentTruncated(segmentReader);
buffer = null;
}
}
} while (buffer == null && timer.getElapsedMillis() < timeout);
if (buffer == null) {
return createEmptyEvent(null);
}
lastRead = Sequence.create(segment.getSegmentNumber(), offset);
int length = buffer.remaining() + WireCommands.TYPE_PLUS_LENGTH_SIZE;
return new EventReadImpl<>(lastRead, deserializer.deserialize(buffer), getPosition(), new EventPointerImpl(segment, offset, length), null);
}
}
use of io.pravega.client.segment.impl.Segment in project pravega by pravega.
the class EventStreamReaderImpl method handleSegmentTruncated.
private void handleSegmentTruncated(SegmentInputStream segmentReader) throws ReinitializationRequiredException, TruncatedDataException {
Segment segmentId = segmentReader.getSegmentId();
log.info("{} encountered truncation for segment {} ", this, segmentId);
String delegationToken = groupState.getLatestDelegationToken();
@Cleanup SegmentMetadataClient metadataClient = metadataClientFactory.createSegmentMetadataClient(segmentId, delegationToken);
try {
long startingOffset = metadataClient.getSegmentInfo().getStartingOffset();
segmentReader.setOffset(startingOffset);
} catch (NoSuchSegmentException e) {
handleEndOfSegment(segmentReader);
}
throw new TruncatedDataException();
}
Aggregations