Search in sources :

Example 1 with SegmentInputStream

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

the class ClientFactoryImpl method createRevisionedStreamClient.

@Override
public <T> RevisionedStreamClient<T> createRevisionedStreamClient(String streamName, Serializer<T> serializer, SynchronizerConfig config) {
    log.info("Creating revisioned stream client for stream: {} with synchronizer configuration: {}", streamName, config);
    Segment segment = new Segment(scope, streamName, 0);
    SegmentInputStream in = inFactory.createInputStreamForSegment(segment);
    // Segment sealed is not expected for Revisioned Stream Client.
    Consumer<Segment> segmentSealedCallBack = s -> {
        throw new IllegalStateException("RevisionedClient: Segmentsealed exception observed for segment:" + s);
    };
    String delegationToken = Futures.getAndHandleExceptions(controller.getOrRefreshDelegationTokenFor(segment.getScope(), segment.getStreamName()), RuntimeException::new);
    SegmentOutputStream out = outFactory.createOutputStreamForSegment(segment, segmentSealedCallBack, config.getEventWriterConfig(), delegationToken);
    SegmentMetadataClient meta = metaFactory.createSegmentMetadataClient(segment, delegationToken);
    return new RevisionedStreamClientImpl<>(segment, in, out, meta, serializer, controller, delegationToken);
}
Also used : StateSynchronizer(io.pravega.client.state.StateSynchronizer) Segment(io.pravega.client.segment.impl.Segment) EventStreamWriter(io.pravega.client.stream.EventStreamWriter) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SegmentMetadataClientFactoryImpl(io.pravega.client.segment.impl.SegmentMetadataClientFactoryImpl) Supplier(java.util.function.Supplier) SegmentMetadataClientFactory(io.pravega.client.segment.impl.SegmentMetadataClientFactory) Update(io.pravega.client.state.Update) Stream(io.pravega.client.stream.Stream) RevisionedStreamClient(io.pravega.client.state.RevisionedStreamClient) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) SegmentInputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) RevisionedStreamClientImpl(io.pravega.client.state.impl.RevisionedStreamClientImpl) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) Serializer(io.pravega.client.stream.Serializer) NameUtils(io.pravega.shared.NameUtils) BatchClientImpl(io.pravega.client.batch.impl.BatchClientImpl) SegmentInputStreamFactory(io.pravega.client.segment.impl.SegmentInputStreamFactory) StateSynchronizerImpl(io.pravega.client.state.impl.StateSynchronizerImpl) lombok.val(lombok.val) EventStreamReader(io.pravega.client.stream.EventStreamReader) InitialUpdate(io.pravega.client.state.InitialUpdate) Revisioned(io.pravega.client.state.Revisioned) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) Consumer(java.util.function.Consumer) Slf4j(lombok.extern.slf4j.Slf4j) SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) SegmentOutputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentOutputStreamFactoryImpl) UpdateOrInitSerializer(io.pravega.client.state.impl.UpdateOrInitSerializer) ConnectionFactoryImpl(io.pravega.client.netty.impl.ConnectionFactoryImpl) ClientFactory(io.pravega.client.ClientFactory) BatchClient(io.pravega.client.batch.BatchClient) InvalidStreamException(io.pravega.client.stream.InvalidStreamException) ReaderConfig(io.pravega.client.stream.ReaderConfig) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) Futures(io.pravega.common.concurrent.Futures) ClientConfig(io.pravega.client.ClientConfig) SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) RevisionedStreamClientImpl(io.pravega.client.state.impl.RevisionedStreamClientImpl) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Segment(io.pravega.client.segment.impl.Segment) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient)

Example 2 with SegmentInputStream

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

the class EventStreamReaderImpl method acquireSegmentsIfNeeded.

@GuardedBy("readers")
private void acquireSegmentsIfNeeded() throws ReinitializationRequiredException {
    Map<Segment, Long> newSegments = groupState.acquireNewSegmentsIfNeeded(getLag());
    if (!newSegments.isEmpty()) {
        log.info("{} acquiring segments {}", this, newSegments);
        for (Entry<Segment, Long> newSegment : newSegments.entrySet()) {
            SegmentInputStream in = inputStreamFactory.createInputStreamForSegment(newSegment.getKey());
            in.setOffset(newSegment.getValue());
            readers.add(in);
        }
    }
}
Also used : SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) Segment(io.pravega.client.segment.impl.Segment) GuardedBy(javax.annotation.concurrent.GuardedBy)

Example 3 with SegmentInputStream

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

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

the class EventStreamReaderTest method testReleaseSegment.

@Test(timeout = 10000)
public void testReleaseSegment() throws SegmentSealedException, ReinitializationRequiredException {
    AtomicLong clock = new AtomicLong();
    MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
    Orderer orderer = new Orderer();
    ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
    EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(segmentStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build());
    Segment segment1 = Segment.fromScopedName("Foo/Bar/0");
    Segment segment2 = Segment.fromScopedName("Foo/Bar/1");
    Mockito.when(groupState.acquireNewSegmentsIfNeeded(0L)).thenReturn(ImmutableMap.of(segment1, 0L, segment2, 0L)).thenReturn(Collections.emptyMap());
    SegmentOutputStream stream1 = segmentStreamFactory.createOutputStreamForSegment(segment1, segmentSealedCallback, writerConfig, "");
    SegmentOutputStream stream2 = segmentStreamFactory.createOutputStreamForSegment(segment2, segmentSealedCallback, writerConfig, "");
    writeInt(stream1, 1);
    writeInt(stream2, 2);
    reader.readNextEvent(0);
    List<SegmentInputStream> readers = reader.getReaders();
    assertEquals(2, readers.size());
    Assert.assertEquals(segment1, readers.get(0).getSegmentId());
    Assert.assertEquals(segment2, readers.get(1).getSegmentId());
    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), Mockito.anyLong(), Mockito.anyLong())).thenReturn(true);
    assertFalse(reader.readNextEvent(0).isCheckpoint());
    Mockito.verify(groupState).releaseSegment(Mockito.eq(segment2), Mockito.anyLong(), Mockito.anyLong());
    readers = reader.getReaders();
    assertEquals(1, readers.size());
    Assert.assertEquals(segment1, readers.get(0).getSegmentId());
    reader.close();
}
Also used : SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) AtomicLong(java.util.concurrent.atomic.AtomicLong) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Example 5 with SegmentInputStream

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

the class EventStreamReaderTest method testAcquireSegment.

@SuppressWarnings("unused")
@Test(timeout = 10000)
public void testAcquireSegment() throws SegmentSealedException, ReinitializationRequiredException {
    AtomicLong clock = new AtomicLong();
    MockSegmentStreamFactory segmentStreamFactory = new MockSegmentStreamFactory();
    Orderer orderer = new Orderer();
    ReaderGroupStateManager groupState = Mockito.mock(ReaderGroupStateManager.class);
    EventStreamReaderImpl<byte[]> reader = new EventStreamReaderImpl<>(segmentStreamFactory, segmentStreamFactory, new ByteArraySerializer(), groupState, orderer, clock::get, ReaderConfig.builder().build());
    Segment segment1 = Segment.fromScopedName("Foo/Bar/0");
    Segment segment2 = Segment.fromScopedName("Foo/Bar/1");
    Mockito.when(groupState.acquireNewSegmentsIfNeeded(0L)).thenReturn(ImmutableMap.of(segment1, 0L)).thenReturn(ImmutableMap.of(segment2, 0L)).thenReturn(Collections.emptyMap());
    SegmentOutputStream stream1 = segmentStreamFactory.createOutputStreamForSegment(segment1, segmentSealedCallback, writerConfig, "");
    SegmentOutputStream stream2 = segmentStreamFactory.createOutputStreamForSegment(segment2, segmentSealedCallback, writerConfig, "");
    writeInt(stream1, 1);
    writeInt(stream1, 2);
    writeInt(stream2, 3);
    writeInt(stream2, 4);
    reader.readNextEvent(0);
    List<SegmentInputStream> readers = reader.getReaders();
    assertEquals(1, readers.size());
    Assert.assertEquals(segment1, readers.get(0).getSegmentId());
    reader.readNextEvent(0);
    readers = reader.getReaders();
    assertEquals(2, readers.size());
    Assert.assertEquals(segment1, readers.get(0).getSegmentId());
    Assert.assertEquals(segment2, readers.get(1).getSegmentId());
    reader.close();
}
Also used : SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) AtomicLong(java.util.concurrent.atomic.AtomicLong) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

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