Search in sources :

Example 16 with SegmentOutputStream

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

the class SegmentSelector method createMissingWriters.

private void createMissingWriters(Consumer<Segment> segmentSealedCallBack, String delegationToken) {
    for (Segment segment : currentSegments.getSegments()) {
        if (!writers.containsKey(segment)) {
            SegmentOutputStream out = outputStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallBack, config, delegationToken);
            writers.put(segment, out);
        }
    }
}
Also used : SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Segment(io.pravega.client.segment.impl.Segment)

Example 17 with SegmentOutputStream

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

the class SegmentSelector method updateSegments.

@Synchronized
private List<PendingEvent> updateSegments(StreamSegments newSteamSegments, Consumer<Segment> segmentSealedCallBack) {
    currentSegments = newSteamSegments;
    createMissingWriters(segmentSealedCallBack, newSteamSegments.getDelegationToken());
    List<PendingEvent> toResend = new ArrayList<>();
    Iterator<Entry<Segment, SegmentOutputStream>> iter = writers.entrySet().iterator();
    while (iter.hasNext()) {
        Entry<Segment, SegmentOutputStream> entry = iter.next();
        if (!currentSegments.getSegments().contains(entry.getKey())) {
            SegmentOutputStream writer = entry.getValue();
            iter.remove();
            try {
                writer.close();
            } catch (SegmentSealedException e) {
                log.info("Caught segment sealed while refreshing on segment {}", entry.getKey());
            }
            toResend.addAll(writer.getUnackedEventsOnSeal());
        }
    }
    return toResend;
}
Also used : Entry(java.util.Map.Entry) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) ArrayList(java.util.ArrayList) SegmentSealedException(io.pravega.client.segment.impl.SegmentSealedException) Segment(io.pravega.client.segment.impl.Segment) Synchronized(lombok.Synchronized)

Example 18 with SegmentOutputStream

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

the class EventStreamReaderTest method testDataTruncated.

@Test(timeout = 10000)
public void testDataTruncated() 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 segment = Segment.fromScopedName("Foo/Bar/0");
    Mockito.when(groupState.acquireNewSegmentsIfNeeded(0L)).thenReturn(ImmutableMap.of(segment, 0L)).thenReturn(Collections.emptyMap());
    SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, "");
    SegmentMetadataClient metadataClient = segmentStreamFactory.createSegmentMetadataClient(segment, "");
    ByteBuffer buffer1 = writeInt(stream, 1);
    ByteBuffer buffer2 = writeInt(stream, 2);
    ByteBuffer buffer3 = writeInt(stream, 3);
    long length = metadataClient.fetchCurrentSegmentLength();
    assertEquals(0, length % 3);
    EventRead<byte[]> event1 = reader.readNextEvent(0);
    assertEquals(buffer1, ByteBuffer.wrap(event1.getEvent()));
    metadataClient.truncateSegment(segment, length / 3);
    assertEquals(buffer2, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
    metadataClient.truncateSegment(segment, length);
    ByteBuffer buffer4 = writeInt(stream, 4);
    AssertExtensions.assertThrows(TruncatedDataException.class, () -> reader.readNextEvent(0));
    assertEquals(buffer4, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
    assertNull(reader.readNextEvent(0).getEvent());
    AssertExtensions.assertThrows(NoSuchEventException.class, () -> reader.fetchEvent(event1.getEventPointer()));
    reader.close();
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) AtomicLong(java.util.concurrent.atomic.AtomicLong) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Test(org.junit.Test)

Example 19 with SegmentOutputStream

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

the class EventStreamReaderTest method testRead.

@Test(timeout = 10000)
public void testRead() 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 segment = Segment.fromScopedName("Foo/Bar/0");
    Mockito.when(groupState.acquireNewSegmentsIfNeeded(0L)).thenReturn(ImmutableMap.of(segment, 0L)).thenReturn(Collections.emptyMap());
    SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, "");
    ByteBuffer buffer1 = writeInt(stream, 1);
    ByteBuffer buffer2 = writeInt(stream, 2);
    ByteBuffer buffer3 = writeInt(stream, 3);
    assertEquals(buffer1, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
    assertEquals(buffer2, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
    assertEquals(buffer3, ByteBuffer.wrap(reader.readNextEvent(0).getEvent()));
    assertNull(reader.readNextEvent(0).getEvent());
    reader.close();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Example 20 with SegmentOutputStream

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

the class EventStreamReaderTest method testEventPointer.

@Test
public void testEventPointer() throws SegmentSealedException, NoSuchEventException, 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 segment = Segment.fromScopedName("Foo/Bar/0");
    Mockito.when(groupState.acquireNewSegmentsIfNeeded(0L)).thenReturn(ImmutableMap.of(segment, 0L)).thenReturn(Collections.emptyMap());
    SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, "");
    ByteBuffer buffer1 = writeInt(stream, 1);
    ByteBuffer buffer2 = writeInt(stream, 2);
    ByteBuffer buffer3 = writeInt(stream, 3);
    EventRead<byte[]> event1 = reader.readNextEvent(0);
    EventRead<byte[]> event2 = reader.readNextEvent(0);
    EventRead<byte[]> event3 = reader.readNextEvent(0);
    assertEquals(buffer1, ByteBuffer.wrap(event1.getEvent()));
    assertEquals(buffer2, ByteBuffer.wrap(event2.getEvent()));
    assertEquals(buffer3, ByteBuffer.wrap(event3.getEvent()));
    assertNull(reader.readNextEvent(0).getEvent());
    assertEquals(buffer1, ByteBuffer.wrap(reader.fetchEvent(event1.getEventPointer())));
    assertEquals(buffer3, ByteBuffer.wrap(reader.fetchEvent(event3.getEventPointer())));
    assertEquals(buffer2, ByteBuffer.wrap(reader.fetchEvent(event2.getEventPointer())));
    reader.close();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Aggregations

SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)25 Segment (io.pravega.client.segment.impl.Segment)19 Test (org.junit.Test)16 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)11 ByteBuffer (java.nio.ByteBuffer)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 SegmentMetadataClient (io.pravega.client.segment.impl.SegmentMetadataClient)5 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)5 Cleanup (lombok.Cleanup)5 SegmentInputStream (io.pravega.client.segment.impl.SegmentInputStream)4 ConnectionFactory (io.pravega.client.netty.impl.ConnectionFactory)3 ConnectionFactoryImpl (io.pravega.client.netty.impl.ConnectionFactoryImpl)3 SegmentOutputStreamFactoryImpl (io.pravega.client.segment.impl.SegmentOutputStreamFactoryImpl)3 SegmentSealedException (io.pravega.client.segment.impl.SegmentSealedException)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 SegmentInputStreamFactoryImpl (io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl)2 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)2 Controller (io.pravega.client.stream.impl.Controller)2 PendingEvent (io.pravega.client.stream.impl.PendingEvent)2 MockController (io.pravega.client.stream.mock.MockController)2