Search in sources :

Example 1 with SegmentTruncatedException

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);
    }
}
Also used : SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) Timer(io.pravega.common.Timer) EndOfSegmentException(io.pravega.client.segment.impl.EndOfSegmentException) SegmentTruncatedException(io.pravega.client.segment.impl.SegmentTruncatedException) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment)

Example 2 with SegmentTruncatedException

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.");
    }
}
Also used : SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) EndOfSegmentException(io.pravega.client.segment.impl.EndOfSegmentException) NoSuchEventException(io.pravega.client.segment.impl.NoSuchEventException) SegmentTruncatedException(io.pravega.client.segment.impl.SegmentTruncatedException) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) NoSuchSegmentException(io.pravega.client.segment.impl.NoSuchSegmentException)

Example 3 with SegmentTruncatedException

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();
}
Also used : EndOfSegmentException(io.pravega.client.segment.impl.EndOfSegmentException) SegmentTruncatedException(io.pravega.client.segment.impl.SegmentTruncatedException) ByteBuffer(java.nio.ByteBuffer) Synchronized(lombok.Synchronized)

Aggregations

EndOfSegmentException (io.pravega.client.segment.impl.EndOfSegmentException)3 SegmentTruncatedException (io.pravega.client.segment.impl.SegmentTruncatedException)3 ByteBuffer (java.nio.ByteBuffer)3 SegmentInputStream (io.pravega.client.segment.impl.SegmentInputStream)2 NoSuchEventException (io.pravega.client.segment.impl.NoSuchEventException)1 NoSuchSegmentException (io.pravega.client.segment.impl.NoSuchSegmentException)1 Segment (io.pravega.client.segment.impl.Segment)1 Timer (io.pravega.common.Timer)1 Cleanup (lombok.Cleanup)1 Synchronized (lombok.Synchronized)1