use of io.pravega.client.segment.impl.EndOfSegmentException 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.EndOfSegmentException in project pravega by pravega.
the class MockSegmentIoStreams method read.
/**
* Byte oriented read.
* @see io.pravega.client.segment.impl.SegmentInputStream#read(java.nio.ByteBuffer, long)
*/
@Override
@Synchronized
public int read(ByteBuffer toFill, long timeout) throws EndOfSegmentException, SegmentTruncatedException {
if (readOffset >= writeOffset) {
throw new EndOfSegmentException();
}
if (readOffset < startingOffset) {
throw new SegmentTruncatedException("Data below " + startingOffset + " has been truncated");
}
int result = 0;
while (toFill.hasRemaining() && readOffset < writeOffset) {
ByteBuffer buffer = dataWritten.floorEntry(readOffset).getValue();
int read = ByteBufferUtils.copy(buffer, toFill);
readOffset += read;
result += read;
}
return result;
}
use of io.pravega.client.segment.impl.EndOfSegmentException in project pravega by pravega.
the class EventStreamReaderTest method testSegmentSplit.
@Test(timeout = 10000)
public void testSegmentSplit() throws EndOfSegmentException, SegmentTruncatedException, SegmentSealedException, ReaderNotInReaderGroupException {
AtomicLong clock = new AtomicLong();
MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
// Prep the mocks.
ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
// Mock for the two SegmentInputStreams.
Segment segment1 = Segment.fromScopedName("Foo/Bar/1");
SegmentWithRange s1range = new SegmentWithRange(segment1, 0, 1);
EventSegmentReader segmentInputStream1 = Mockito.mock(EventSegmentReader.class);
Mockito.when(segmentInputStream1.isSegmentReady()).thenReturn(true);
Mockito.when(segmentInputStream1.read(anyLong())).thenThrow(new EndOfSegmentException(EndOfSegmentException.ErrorType.END_OF_SEGMENT_REACHED));
Mockito.when(segmentInputStream1.getSegmentId()).thenReturn(segment1);
Segment segment2 = Segment.fromScopedName("Foo/Bar/2");
SegmentWithRange s2range = new SegmentWithRange(segment2, 0, 0.5);
EventSegmentReader segmentInputStream2 = Mockito.mock(EventSegmentReader.class);
@Cleanup SegmentOutputStream stream2 = segmentStreamFactory.createOutputStreamForSegment(segment2, segmentSealedCallback, writerConfig, DelegationTokenProviderFactory.createWithEmptyToken());
Mockito.when(segmentInputStream2.read(anyLong())).thenReturn(writeInt(stream2, 2));
Mockito.when(segmentInputStream2.getSegmentId()).thenReturn(segment2);
Segment segment3 = Segment.fromScopedName("Foo/Bar/3");
SegmentWithRange s3range = new SegmentWithRange(segment3, 0.5, 1.0);
EventSegmentReader segmentInputStream3 = Mockito.mock(EventSegmentReader.class);
@Cleanup SegmentOutputStream stream3 = segmentStreamFactory.createOutputStreamForSegment(segment3, segmentSealedCallback, writerConfig, DelegationTokenProviderFactory.createWithEmptyToken());
Mockito.when(segmentInputStream3.read(anyLong())).thenReturn(writeInt(stream3, 3));
Mockito.when(segmentInputStream3.getSegmentId()).thenReturn(segment3);
SegmentInputStreamFactory inputStreamFactory = Mockito.mock(SegmentInputStreamFactory.class);
Mockito.when(inputStreamFactory.createEventReaderForSegment(eq(segment1), anyInt(), any(Semaphore.class), eq(Long.MAX_VALUE))).thenReturn(segmentInputStream1);
Mockito.when(inputStreamFactory.createEventReaderForSegment(eq(segment2), anyInt(), any(Semaphore.class), eq(Long.MAX_VALUE))).thenReturn(segmentInputStream2);
Mockito.when(inputStreamFactory.createEventReaderForSegment(eq(segment3), anyInt(), any(Semaphore.class), eq(Long.MAX_VALUE))).thenReturn(segmentInputStream3);
Mockito.when(groupState.getEndOffsetForSegment(any())).thenReturn(Long.MAX_VALUE);
@Cleanup EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(inputStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, new Orderer(), clock::get, ReaderConfig.builder().build(), createWatermarkReaders(), Mockito.mock(Controller.class));
Mockito.when(groupState.acquireNewSegmentsIfNeeded(anyLong(), any())).thenReturn(ImmutableMap.of(s1range, 0L)).thenReturn(Collections.emptyMap());
InOrder inOrder = Mockito.inOrder(segmentInputStream1, groupState);
EventRead<byte[]> event = reader.readNextEvent(100L);
assertNull(event.getEvent());
event = reader.readNextEvent(100L);
assertNull(event.getEvent());
Mockito.when(groupState.getCheckpoint()).thenReturn("checkpoint").thenReturn(null);
Mockito.when(groupState.acquireNewSegmentsIfNeeded(anyLong(), any())).thenReturn(ImmutableMap.of(s2range, 0L, s3range, 0L)).thenReturn(Collections.emptyMap());
assertEquals("checkpoint", reader.readNextEvent(0).getCheckpointName());
inOrder.verify(groupState).getCheckpoint();
// Ensure groupstate is not updated before the checkpoint.
inOrder.verify(groupState, Mockito.times(0)).handleEndOfSegment(s1range);
event = reader.readNextEvent(0);
assertFalse(event.isCheckpoint());
// Now it is called.
inOrder.verify(groupState, Mockito.times(1)).handleEndOfSegment(s1range);
assertEquals(ImmutableList.of(segmentInputStream2, segmentInputStream3), reader.getReaders());
}
use of io.pravega.client.segment.impl.EndOfSegmentException in project pravega by pravega.
the class MockSegmentIoStreams method read.
/**
* Event read.
* @see io.pravega.client.segment.impl.EventSegmentReader#read(long)
*/
@Override
@Synchronized
public ByteBuffer read(long timeout) throws EndOfSegmentException, SegmentTruncatedException {
if (readOffset >= writeOffset) {
throw new EndOfSegmentException();
}
if (readOffset < startingOffset) {
throw new SegmentTruncatedException("Data below " + startingOffset + " has been truncated");
}
ByteBuffer buffer = dataWritten.floorEntry(readOffset).getValue();
readOffset += buffer.remaining();
ByteBuffer result = buffer.slice();
result.position(WireCommands.TYPE_PLUS_LENGTH_SIZE);
return result;
}
use of io.pravega.client.segment.impl.EndOfSegmentException in project pravega by pravega.
the class EventStreamReaderTest method testReadWithEndOfSegmentException.
@SuppressWarnings("unchecked")
@Test(timeout = 10000)
public void testReadWithEndOfSegmentException() throws Exception {
AtomicLong clock = new AtomicLong();
MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
// Prep the mocks.
ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
// Mock for the two SegmentInputStreams.
Segment segment = Segment.fromScopedName("Foo/Bar/0");
EventSegmentReader segmentInputStream1 = Mockito.mock(EventSegmentReader.class);
Mockito.when(segmentInputStream1.read(anyLong())).thenThrow(new EndOfSegmentException(EndOfSegmentException.ErrorType.END_OFFSET_REACHED));
Mockito.when(segmentInputStream1.getSegmentId()).thenReturn(segment);
EventSegmentReader segmentInputStream2 = Mockito.mock(EventSegmentReader.class);
@Cleanup SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, DelegationTokenProviderFactory.createWithEmptyToken());
ByteBuffer buffer = writeInt(stream, 1);
Mockito.when(segmentInputStream2.read(anyLong())).thenReturn(buffer);
Mockito.when(segmentInputStream2.getSegmentId()).thenReturn(Segment.fromScopedName("Foo/test/0"));
Mockito.when(segmentInputStream2.getOffset()).thenReturn(10L);
SegmentInputStreamFactory inputStreamFactory = Mockito.mock(SegmentInputStreamFactory.class);
Mockito.when(inputStreamFactory.createEventReaderForSegment(any(Segment.class), anyInt(), any(Semaphore.class), anyLong())).thenReturn(segmentInputStream1);
// Mock Orderer
Orderer orderer = Mockito.mock(Orderer.class);
Mockito.when(orderer.nextSegment(any(List.class))).thenReturn(segmentInputStream1).thenReturn(segmentInputStream2);
@Cleanup EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(inputStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build(), createWatermarkReaders(), Mockito.mock(Controller.class));
InOrder inOrder = Mockito.inOrder(segmentInputStream1, groupState);
EventRead<byte[]> event = reader.readNextEvent(100L);
assertNotNull(event.getEvent());
// Validate that segmentInputStream1.close() is invoked on reaching endOffset.
Mockito.verify(segmentInputStream1, Mockito.times(1)).close();
SegmentWithRange segmentWithRange = new SegmentWithRange(segment, 0, 1);
// Verify groupstate is not updated before the checkpoint, but is after.
inOrder.verify(groupState, Mockito.times(0)).handleEndOfSegment(segmentWithRange);
Mockito.when(groupState.getCheckpoint()).thenReturn("checkpoint").thenReturn(null);
assertEquals("checkpoint", reader.readNextEvent(0).getCheckpointName());
inOrder.verify(groupState).getCheckpoint();
// Verify groupstate is not updated before the checkpoint, but is after.
inOrder.verify(groupState, Mockito.times(0)).handleEndOfSegment(segmentWithRange);
event = reader.readNextEvent(0);
assertFalse(event.isCheckpoint());
// Now it is called.
inOrder.verify(groupState, Mockito.times(1)).handleEndOfSegment(segmentWithRange);
}
Aggregations