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);
}
}
}
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;
}
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();
}
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();
}
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();
}
Aggregations