Search in sources :

Example 1 with EventSegmentReader

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);
}
Also used : ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) CompletableFuture(java.util.concurrent.CompletableFuture) SegmentOutputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentOutputStreamFactoryImpl) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) EventSegmentReader(io.pravega.client.segment.impl.EventSegmentReader) MockController(io.pravega.client.stream.mock.MockController) SegmentInputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl) Test(org.junit.Test)

Example 2 with EventSegmentReader

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());
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) SynchronizerClientFactory(io.pravega.client.SynchronizerClientFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) Entry(java.util.Map.Entry) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) EventSegmentReader(io.pravega.client.segment.impl.EventSegmentReader) SegmentMetadataClientFactory(io.pravega.client.segment.impl.SegmentMetadataClientFactory) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) SegmentInfo(io.pravega.client.segment.impl.SegmentInfo) SegmentInputStreamFactory(io.pravega.client.segment.impl.SegmentInputStreamFactory) DelegationTokenProvider(io.pravega.client.security.auth.DelegationTokenProvider) Test(org.junit.Test)

Example 3 with EventSegmentReader

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();
            }
        }
    }
}
Also used : EventSegmentReader(io.pravega.client.segment.impl.EventSegmentReader) Segment(io.pravega.client.segment.impl.Segment) GuardedBy(javax.annotation.concurrent.GuardedBy)

Example 4 with EventSegmentReader

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());
}
Also used : EventSegmentReader(io.pravega.client.segment.impl.EventSegmentReader) SegmentInputStreamFactory(io.pravega.client.segment.impl.SegmentInputStreamFactory) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Example 5 with EventSegmentReader

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();
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) MockController(io.pravega.client.stream.mock.MockController) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) AtomicLong(java.util.concurrent.atomic.AtomicLong) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) EventSegmentReader(io.pravega.client.segment.impl.EventSegmentReader) Test(org.junit.Test)

Aggregations

EventSegmentReader (io.pravega.client.segment.impl.EventSegmentReader)18 Segment (io.pravega.client.segment.impl.Segment)14 Cleanup (lombok.Cleanup)12 Test (org.junit.Test)11 MockController (io.pravega.client.stream.mock.MockController)10 Controller (io.pravega.client.control.impl.Controller)9 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)8 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 SegmentInputStreamFactory (io.pravega.client.segment.impl.SegmentInputStreamFactory)6 ByteBuffer (java.nio.ByteBuffer)6 EndOfSegmentException (io.pravega.client.segment.impl.EndOfSegmentException)4 Semaphore (java.util.concurrent.Semaphore)4 SegmentMetadataClient (io.pravega.client.segment.impl.SegmentMetadataClient)3 SegmentTruncatedException (io.pravega.client.segment.impl.SegmentTruncatedException)3 InOrder (org.mockito.InOrder)3 ConnectionPoolImpl (io.pravega.client.connection.impl.ConnectionPoolImpl)2 SocketConnectionFactoryImpl (io.pravega.client.connection.impl.SocketConnectionFactoryImpl)2 DelegationTokenProvider (io.pravega.client.security.auth.DelegationTokenProvider)2 ConditionalOutputStream (io.pravega.client.segment.impl.ConditionalOutputStream)2