use of io.pravega.client.segment.impl.SegmentTruncatedException 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.SegmentTruncatedException in project pravega by pravega.
the class EventStreamReaderImpl method fetchEvent.
@Override
public Type fetchEvent(EventPointer pointer) throws NoSuchEventException {
Preconditions.checkNotNull(pointer);
// Create SegmentInputStream
@Cleanup SegmentInputStream inputStream = inputStreamFactory.createInputStreamForSegment(pointer.asImpl().getSegment(), pointer.asImpl().getEventLength());
inputStream.setOffset(pointer.asImpl().getEventStartOffset());
// Read event
try {
ByteBuffer buffer = inputStream.read();
Type result = deserializer.deserialize(buffer);
return result;
} catch (EndOfSegmentException e) {
throw new NoSuchEventException(e.getMessage());
} catch (NoSuchSegmentException | SegmentTruncatedException e) {
throw new NoSuchEventException("Event no longer exists.");
}
}
use of io.pravega.client.segment.impl.SegmentTruncatedException in project pravega by pravega.
the class MockSegmentIoStreams method read.
@Override
@Synchronized
public ByteBuffer read(long timeout) throws EndOfSegmentException, SegmentTruncatedException {
if (readIndex >= eventsWritten) {
throw new EndOfSegmentException();
}
if (readOffset < startingOffset) {
throw new SegmentTruncatedException("Data below " + startingOffset + " has been truncated");
}
ByteBuffer buffer = dataWritten.get(readIndex);
readIndex++;
readOffset += buffer.remaining() + WireCommands.TYPE_PLUS_LENGTH_SIZE;
return buffer.slice();
}
Aggregations