use of io.pravega.client.segment.impl.EventSegmentReader in project pravega by pravega.
the class ReadTest method readThroughSegmentClient.
@Test(timeout = 10000)
public void readThroughSegmentClient() throws SegmentSealedException, EndOfSegmentException, SegmentTruncatedException {
String endpoint = "localhost";
String scope = "scope";
String stream = "readThroughSegmentClient";
int port = TestUtils.getAvailableListenPort();
String testString = "Hello world\n";
StreamSegmentStore store = SERVICE_BUILDER.createStreamSegmentService();
TableStore tableStore = SERVICE_BUILDER.createTableStoreService();
@Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store, tableStore, SERVICE_BUILDER.getLowPriorityExecutor());
server.startListening();
@Cleanup SocketConnectionFactoryImpl clientCF = new SocketConnectionFactoryImpl(ClientConfig.builder().build());
@Cleanup ConnectionPoolImpl connectionPool = new ConnectionPoolImpl(ClientConfig.builder().build(), clientCF);
@Cleanup Controller controller = new MockController(endpoint, port, connectionPool, true);
controller.createScope(scope);
controller.createStream(scope, stream, StreamConfiguration.builder().build());
SegmentOutputStreamFactoryImpl segmentproducerClient = new SegmentOutputStreamFactoryImpl(controller, connectionPool);
SegmentInputStreamFactoryImpl segmentConsumerClient = new SegmentInputStreamFactoryImpl(controller, connectionPool);
Segment segment = Futures.getAndHandleExceptions(controller.getCurrentSegments(scope, stream), RuntimeException::new).getSegments().iterator().next();
@Cleanup SegmentOutputStream out = segmentproducerClient.createOutputStreamForSegment(segment, segmentSealedCallback, EventWriterConfig.builder().build(), DelegationTokenProviderFactory.createWithEmptyToken());
out.write(PendingEvent.withHeader(null, ByteBuffer.wrap(testString.getBytes()), new CompletableFuture<>()));
out.flush();
@Cleanup EventSegmentReader in = segmentConsumerClient.createEventReaderForSegment(segment);
ByteBuffer result = in.read();
assertEquals(ByteBuffer.wrap(testString.getBytes()), result);
// Test large write followed by read
out.write(PendingEvent.withHeader(null, ByteBuffer.wrap(new byte[15]), new CompletableFuture<>()));
out.write(PendingEvent.withHeader(null, ByteBuffer.wrap(new byte[15]), new CompletableFuture<>()));
out.write(PendingEvent.withHeader(null, ByteBuffer.wrap(new byte[150000]), new CompletableFuture<>()));
assertEquals(in.read().capacity(), 15);
assertEquals(in.read().capacity(), 15);
assertEquals(in.read().capacity(), 150000);
}
use of io.pravega.client.segment.impl.EventSegmentReader in project pravega by pravega.
the class RevisionedStreamClientTest method testTimeoutWithStreamIterator.
@Test
public void testTimeoutWithStreamIterator() throws Exception {
String scope = "scope";
String stream = "stream";
// Setup Environment
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
JavaSerializer<String> serializer = new JavaSerializer<>();
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, false);
createScopeAndStream(scope, stream, controller);
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
// Setup mock
SegmentOutputStreamFactory outFactory = mock(SegmentOutputStreamFactory.class);
SegmentOutputStream out = mock(SegmentOutputStream.class);
Segment segment = new Segment(scope, stream, 0);
when(outFactory.createOutputStreamForSegment(eq(segment), any(), any(), any(DelegationTokenProvider.class))).thenReturn(out);
SegmentInputStreamFactory inFactory = mock(SegmentInputStreamFactory.class);
EventSegmentReader in = mock(EventSegmentReader.class);
when(inFactory.createEventReaderForSegment(eq(segment), anyInt())).thenReturn(in);
when(in.read(anyLong())).thenReturn(null).thenReturn(serializer.serialize("testData"));
SegmentMetadataClientFactory metaFactory = mock(SegmentMetadataClientFactory.class);
SegmentMetadataClient metaClient = mock(SegmentMetadataClient.class);
when(metaFactory.createSegmentMetadataClient(any(Segment.class), any(DelegationTokenProvider.class))).thenReturn(metaClient);
when(metaClient.getSegmentInfo()).thenReturn(CompletableFuture.completedFuture(new SegmentInfo(segment, 0, 30, false, System.currentTimeMillis())));
@Cleanup SynchronizerClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, inFactory, streamFactory, streamFactory, metaFactory);
@Cleanup RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, serializer, SynchronizerConfig.builder().build());
Iterator<Entry<Revision, String>> iterator = client.readFrom(new RevisionImpl(segment, 15, 1));
assertTrue("True is expected since offset is less than end offset", iterator.hasNext());
assertNotNull("Verify the entry is not null", iterator.next());
verify(in, times(2)).read(anyLong());
}
use of io.pravega.client.segment.impl.EventSegmentReader in project pravega by pravega.
the class EventStreamReaderImpl method releaseSegmentsIfNeeded.
/**
* Releases segments. This must not be invoked except immediately after a checkpoint.
*/
@GuardedBy("readers")
private void releaseSegmentsIfNeeded(PositionInternal position) throws ReaderNotInReaderGroupException {
releaseSealedSegments();
Segment segment = groupState.findSegmentToReleaseIfRequired();
if (segment != null) {
log.info("{} releasing segment {}", this, segment);
EventSegmentReader reader = readers.stream().filter(r -> r.getSegmentId().equals(segment)).findAny().orElse(null);
if (reader != null) {
if (groupState.releaseSegment(segment, reader.getOffset(), getLag(), position)) {
readers.remove(reader);
ranges.remove(reader.getSegmentId());
reader.close();
}
}
}
}
use of io.pravega.client.segment.impl.EventSegmentReader in project pravega by pravega.
the class SegmentIteratorTest method testTimeoutError.
@Test(timeout = 5000)
public void testTimeoutError() throws SegmentTruncatedException, EndOfSegmentException {
Segment segment = new Segment("Scope", "Stream", 1);
int endOffset = 10;
SegmentInputStreamFactory factory = mock(SegmentInputStreamFactory.class);
EventSegmentReader input = mock(EventSegmentReader.class);
when(factory.createEventReaderForSegment(segment, 0, endOffset)).thenReturn(input);
when(input.read()).thenReturn(null).thenReturn(stringSerializer.serialize("s"));
@Cleanup SegmentIteratorImpl<String> iter = new SegmentIteratorImpl<>(factory, segment, stringSerializer, 0, endOffset);
assertEquals("s", iter.next());
verify(input, times(2)).read();
when(input.read()).thenThrow(SegmentTruncatedException.class);
assertThrows(TruncatedDataException.class, () -> iter.next());
}
use of io.pravega.client.segment.impl.EventSegmentReader in project pravega by pravega.
the class EventStreamReaderTest method testReleaseSegment.
@Test(timeout = 10000)
public void testReleaseSegment() throws SegmentSealedException, ReaderNotInReaderGroupException {
AtomicLong clock = new AtomicLong();
MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
Orderer orderer = new Orderer();
ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
@Cleanup EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(segmentStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build(), createWatermarkReaders(), Mockito.mock(Controller.class));
Segment segment1 = Segment.fromScopedName("Foo/Bar/0");
Segment segment2 = Segment.fromScopedName("Foo/Bar/1");
Mockito.when(groupState.acquireNewSegmentsIfNeeded(eq(0L), any())).thenReturn(ImmutableMap.of(new SegmentWithRange(segment1, 0, 0.5), 0L, new SegmentWithRange(segment2, 0, 0.5), 0L)).thenReturn(Collections.emptyMap());
Mockito.when(groupState.getEndOffsetForSegment(any(Segment.class))).thenReturn(Long.MAX_VALUE);
@Cleanup SegmentOutputStream stream1 = segmentStreamFactory.createOutputStreamForSegment(segment1, segmentSealedCallback, writerConfig, DelegationTokenProviderFactory.createWithEmptyToken());
@Cleanup SegmentOutputStream stream2 = segmentStreamFactory.createOutputStreamForSegment(segment2, segmentSealedCallback, writerConfig, DelegationTokenProviderFactory.createWithEmptyToken());
writeInt(stream1, 1);
writeInt(stream2, 2);
reader.readNextEvent(0);
List<EventSegmentReader> readers = reader.getReaders();
assertEquals(2, readers.size());
Assert.assertEquals(segment1, readers.get(0).getSegmentId());
Assert.assertEquals(segment2, readers.get(1).getSegmentId());
// Checkpoint is required to release a segment.
Mockito.when(groupState.getCheckpoint()).thenReturn("checkpoint");
assertTrue(reader.readNextEvent(0).isCheckpoint());
Mockito.when(groupState.getCheckpoint()).thenReturn(null);
Mockito.when(groupState.findSegmentToReleaseIfRequired()).thenReturn(segment2);
Mockito.when(groupState.releaseSegment(Mockito.eq(segment2), anyLong(), anyLong(), any())).thenReturn(true);
assertFalse(reader.readNextEvent(0).isCheckpoint());
Mockito.verify(groupState).releaseSegment(Mockito.eq(segment2), anyLong(), anyLong(), any());
readers = reader.getReaders();
assertEquals(1, readers.size());
Assert.assertEquals(segment1, readers.get(0).getSegmentId());
reader.close();
}
Aggregations