Search in sources :

Example 6 with SegmentInputStream

use of io.pravega.client.segment.impl.SegmentInputStream in project pravega by pravega.

the class ReadTest method readThroughSegmentClient.

@Test
public void readThroughSegmentClient() throws SegmentSealedException, EndOfSegmentException, SegmentTruncatedException {
    String endpoint = "localhost";
    String scope = "scope";
    String stream = "stream";
    int port = TestUtils.getAvailableListenPort();
    String testString = "Hello world\n";
    StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
    server.startListening();
    ConnectionFactory clientCF = new ConnectionFactoryImpl(ClientConfig.builder().build());
    Controller controller = new MockController(endpoint, port, clientCF);
    controller.createScope(scope);
    controller.createStream(StreamConfiguration.builder().scope(scope).streamName(stream).build());
    SegmentOutputStreamFactoryImpl segmentproducerClient = new SegmentOutputStreamFactoryImpl(controller, clientCF);
    SegmentInputStreamFactoryImpl segmentConsumerClient = new SegmentInputStreamFactoryImpl(controller, clientCF);
    Segment segment = Futures.getAndHandleExceptions(controller.getCurrentSegments(scope, stream), RuntimeException::new).getSegments().iterator().next();
    @Cleanup("close") SegmentOutputStream out = segmentproducerClient.createOutputStreamForSegment(segment, segmentSealedCallback, EventWriterConfig.builder().build(), "");
    out.write(new PendingEvent(null, ByteBuffer.wrap(testString.getBytes()), new CompletableFuture<>()));
    out.flush();
    @Cleanup("close") SegmentInputStream in = segmentConsumerClient.createInputStreamForSegment(segment);
    ByteBuffer result = in.read();
    assertEquals(ByteBuffer.wrap(testString.getBytes()), result);
    // Test large write followed by read
    out.write(new PendingEvent(null, ByteBuffer.wrap(new byte[15]), new CompletableFuture<>()));
    out.write(new PendingEvent(null, ByteBuffer.wrap(new byte[15]), new CompletableFuture<>()));
    out.write(new PendingEvent(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 : MockController(io.pravega.client.stream.mock.MockController) Controller(io.pravega.client.stream.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) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) CompletableFuture(java.util.concurrent.CompletableFuture) PendingEvent(io.pravega.client.stream.impl.PendingEvent) SegmentOutputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentOutputStreamFactoryImpl) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockController(io.pravega.client.stream.mock.MockController) SegmentInputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl) ConnectionFactoryImpl(io.pravega.client.netty.impl.ConnectionFactoryImpl) Test(org.junit.Test)

Example 7 with SegmentInputStream

use of io.pravega.client.segment.impl.SegmentInputStream in project pravega by pravega.

the class EventStreamReaderImpl method close.

@Override
public void close() {
    synchronized (readers) {
        if (!closed) {
            log.info("Closing reader {} ", this);
            closed = true;
            groupState.readerShutdown(getPosition());
            for (SegmentInputStream reader : readers) {
                reader.close();
            }
            readers.clear();
            groupState.close();
        }
    }
}
Also used : SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream)

Example 8 with SegmentInputStream

use of io.pravega.client.segment.impl.SegmentInputStream in project pravega by pravega.

the class EventStreamReaderImpl method releaseSegmentsIfNeeded.

@GuardedBy("readers")
private void releaseSegmentsIfNeeded() throws ReinitializationRequiredException {
    Segment segment = groupState.findSegmentToReleaseIfRequired();
    if (segment != null) {
        log.info("{} releasing segment {}", this, segment);
        SegmentInputStream reader = readers.stream().filter(r -> r.getSegmentId().equals(segment)).findAny().orElse(null);
        if (reader != null) {
            if (groupState.releaseSegment(segment, reader.getOffset(), getLag())) {
                readers.remove(reader);
                reader.close();
            }
        }
    }
}
Also used : SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) Segment(io.pravega.client.segment.impl.Segment) GuardedBy(javax.annotation.concurrent.GuardedBy)

Example 9 with SegmentInputStream

use of io.pravega.client.segment.impl.SegmentInputStream 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)

Aggregations

SegmentInputStream (io.pravega.client.segment.impl.SegmentInputStream)9 Segment (io.pravega.client.segment.impl.Segment)7 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)4 ByteBuffer (java.nio.ByteBuffer)3 Test (org.junit.Test)3 ConnectionFactory (io.pravega.client.netty.impl.ConnectionFactory)2 ConnectionFactoryImpl (io.pravega.client.netty.impl.ConnectionFactoryImpl)2 EndOfSegmentException (io.pravega.client.segment.impl.EndOfSegmentException)2 SegmentInputStreamFactoryImpl (io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl)2 SegmentOutputStreamFactoryImpl (io.pravega.client.segment.impl.SegmentOutputStreamFactoryImpl)2 SegmentTruncatedException (io.pravega.client.segment.impl.SegmentTruncatedException)2 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions (com.google.common.base.Preconditions)1 ClientConfig (io.pravega.client.ClientConfig)1 ClientFactory (io.pravega.client.ClientFactory)1 BatchClient (io.pravega.client.batch.BatchClient)1 BatchClientImpl (io.pravega.client.batch.impl.BatchClientImpl)1 NoSuchEventException (io.pravega.client.segment.impl.NoSuchEventException)1