use of io.pravega.client.segment.impl.NoSuchSegmentException in project pravega by pravega.
the class EventStreamReaderImpl method handleSegmentTruncated.
private void handleSegmentTruncated(SegmentInputStream segmentReader) throws ReinitializationRequiredException, TruncatedDataException {
Segment segmentId = segmentReader.getSegmentId();
log.info("{} encountered truncation for segment {} ", this, segmentId);
String delegationToken = groupState.getLatestDelegationToken();
@Cleanup SegmentMetadataClient metadataClient = metadataClientFactory.createSegmentMetadataClient(segmentId, delegationToken);
try {
long startingOffset = metadataClient.getSegmentInfo().getStartingOffset();
segmentReader.setOffset(startingOffset);
} catch (NoSuchSegmentException e) {
handleEndOfSegment(segmentReader);
}
throw new TruncatedDataException();
}
use of io.pravega.client.segment.impl.NoSuchSegmentException in project pravega by pravega.
the class EventStreamReaderImpl method handleSegmentTruncated.
private void handleSegmentTruncated(EventSegmentReader segmentReader) throws TruncatedDataException {
Segment segmentId = segmentReader.getSegmentId();
log.info("{} encountered truncation for segment {} ", this, segmentId);
@Cleanup SegmentMetadataClient metadataClient = metadataClientFactory.createSegmentMetadataClient(segmentId, DelegationTokenProviderFactory.create(controller, segmentId, AccessOperation.READ));
try {
long startingOffset = Futures.getThrowingException(metadataClient.getSegmentInfo()).getStartingOffset();
if (segmentReader.getOffset() == startingOffset) {
log.warn("Attempt to fetch the next available read offset on the segment {} returned a truncated offset {}", segmentId, startingOffset);
}
segmentReader.setOffset(startingOffset);
} catch (NoSuchSegmentException e) {
handleEndOfSegment(segmentReader, true);
}
throw new TruncatedDataException();
}
use of io.pravega.client.segment.impl.NoSuchSegmentException in project pravega by pravega.
the class SegmentSelectorTest method testStreamDeletion.
@Test
public void testStreamDeletion() {
final Segment segment0 = new Segment(scope, streamName, 0);
final Segment segment1 = new Segment(scope, streamName, 1);
final CompletableFuture<Void> writerFuture = new CompletableFuture<>();
// Setup Mock.
SegmentOutputStream s0Writer = Mockito.mock(SegmentOutputStream.class);
SegmentOutputStream s1Writer = Mockito.mock(SegmentOutputStream.class);
when(s0Writer.getUnackedEventsOnSeal()).thenReturn(ImmutableList.of(PendingEvent.withHeader("0", ByteBuffer.wrap("e".getBytes()), writerFuture)));
SegmentOutputStreamFactory factory = Mockito.mock(SegmentOutputStreamFactory.class);
when(factory.createOutputStreamForSegment(eq(segment0), ArgumentMatchers.<Consumer<Segment>>any(), any(EventWriterConfig.class), any(DelegationTokenProvider.class))).thenReturn(s0Writer);
when(factory.createOutputStreamForSegment(eq(segment1), ArgumentMatchers.<Consumer<Segment>>any(), any(EventWriterConfig.class), any(DelegationTokenProvider.class))).thenReturn(s1Writer);
Controller controller = Mockito.mock(Controller.class);
SegmentSelector selector = new SegmentSelector(new StreamImpl(scope, streamName), controller, factory, config, DelegationTokenProviderFactory.createWithEmptyToken());
TreeMap<Double, SegmentWithRange> segments = new TreeMap<>();
addNewSegment(segments, 0, 0.0, 0.5);
addNewSegment(segments, 1, 0.5, 1.0);
StreamSegments streamSegments = new StreamSegments(segments);
when(controller.getCurrentSegments(scope, streamName)).thenReturn(CompletableFuture.completedFuture(streamSegments));
// trigger refresh.
selector.refreshSegmentEventWriters(segmentSealedCallback);
// simulate stream deletion where controller.getSuccessors() is completed exceptionally.
when(controller.getSuccessors(segment0)).thenAnswer(i -> {
CompletableFuture<StreamSegmentsWithPredecessors> result = new CompletableFuture<>();
// Controller throws io.pravega.controller.store.stream.StoreException$DataNotFoundException which is type RuntimeException.
// Using RunTimeException here as the controller exception is not visible.
result.completeExceptionally(new RuntimeException());
return result;
});
assertEquals(Collections.emptyList(), selector.refreshSegmentEventWritersUponSealed(segment0, segmentSealedCallback));
assertFutureThrows("Writer Future", writerFuture, t -> t instanceof NoSuchSegmentException);
}
use of io.pravega.client.segment.impl.NoSuchSegmentException in project pravega by pravega.
the class EndToEndTruncationTest method testWriteDuringTruncationAndDeletion.
@Test(timeout = 30000)
public void testWriteDuringTruncationAndDeletion() throws Exception {
StreamConfiguration config = StreamConfiguration.builder().scalingPolicy(ScalingPolicy.byEventRate(10, 2, 2)).build();
LocalController controller = (LocalController) PRAVEGA.getLocalController();
String streamName = "testWriteDuringTruncationAndDeletion";
controller.createScope("test").get();
controller.createStream("test", streamName, config).get();
config = StreamConfiguration.builder().scalingPolicy(ScalingPolicy.byEventRate(10, 2, 1)).build();
controller.updateStream("test", streamName, config).get();
@Cleanup ConnectionFactory connectionFactory = new SocketConnectionFactoryImpl(ClientConfig.builder().controllerURI(PRAVEGA.getControllerURI()).build());
@Cleanup ClientFactoryImpl clientFactory = new ClientFactoryImpl("test", controller, connectionFactory);
@Cleanup EventStreamWriter<String> writer = clientFactory.createEventWriter(streamName, new JavaSerializer<>(), EventWriterConfig.builder().build());
// routing key "0" translates to key 0.8. This write happens to segment 1.
writer.writeEvent("0", "truncationTest1").get();
// scale down to one segment.
Stream stream = new StreamImpl("test", streamName);
Map<Double, Double> map = new HashMap<>();
map.put(0.0, 1.0);
assertTrue("Stream Scale down", controller.scaleStream(stream, Lists.newArrayList(0L, 1L), map, executorService()).getFuture().get());
// truncate stream at segment 2, offset 0.
Map<Long, Long> streamCutPositions = new HashMap<>();
streamCutPositions.put(computeSegmentId(2, 1), 0L);
assertTrue("Truncate stream", controller.truncateStream("test", streamName, streamCutPositions).get());
// routing key "2" translates to key 0.2.
// this write translates to a write to Segment 0, but since segment 0 is truncated the write should happen on segment 2.
// write to segment 0
writer.writeEvent("2", "truncationTest2").get();
String group = "testWriteDuringTruncationAndDeletion-group";
@Cleanup ReaderGroupManager groupManager = new ReaderGroupManagerImpl("test", controller, clientFactory);
groupManager.createReaderGroup(group, ReaderGroupConfig.builder().disableAutomaticCheckpoints().stream("test/" + streamName).build());
@Cleanup EventStreamReader<String> reader = clientFactory.createReader("readerId", group, new JavaSerializer<>(), ReaderConfig.builder().build());
EventRead<String> event = reader.readNextEvent(10000);
assertNotNull(event);
assertEquals("truncationTest2", event.getEvent());
// Seal and Delete stream.
assertTrue(controller.sealStream("test", streamName).get());
assertTrue(controller.deleteStream("test", streamName).get());
// write by an existing writer to a deleted stream should complete exceptionally.
assertFutureThrows("Should throw NoSuchSegmentException", writer.writeEvent("2", "write to deleted stream"), e -> NoSuchSegmentException.class.isAssignableFrom(e.getClass()));
// subsequent writes will throw an exception to the application.
assertThrows(RuntimeException.class, () -> writer.writeEvent("test"));
}
use of io.pravega.client.segment.impl.NoSuchSegmentException in project pravega by pravega.
the class SegmentTransactionTest method testSegmentDoesNotExist.
@Test(timeout = 5000)
public void testSegmentDoesNotExist() {
UUID uuid = UUID.randomUUID();
SegmentOutputStream outputStream = Mockito.mock(SegmentOutputStream.class);
@SuppressWarnings("resource") SegmentTransactionImpl<String> txn = new SegmentTransactionImpl<>(uuid, outputStream, new JavaSerializer<String>());
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
PendingEvent event = (PendingEvent) invocation.getArgument(0);
event.getAckFuture().completeExceptionally(new NoSuchSegmentException("segment"));
return null;
}
}).when(outputStream).write(Mockito.any(PendingEvent.class));
AssertExtensions.assertThrows(TxnFailedException.class, () -> txn.writeEvent("hi"));
verify(outputStream).write(Mockito.any(PendingEvent.class));
AssertExtensions.assertThrows(TxnFailedException.class, () -> txn.flush());
Mockito.verifyNoMoreInteractions(outputStream);
}
Aggregations