Search in sources :

Example 1 with NoSuchSegmentException

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();
}
Also used : TruncatedDataException(io.pravega.client.stream.TruncatedDataException) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) NoSuchSegmentException(io.pravega.client.segment.impl.NoSuchSegmentException)

Example 2 with NoSuchSegmentException

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();
}
Also used : TruncatedDataException(io.pravega.client.stream.TruncatedDataException) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) NoSuchSegmentException(io.pravega.client.segment.impl.NoSuchSegmentException)

Example 3 with NoSuchSegmentException

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);
}
Also used : Controller(io.pravega.client.control.impl.Controller) TreeMap(java.util.TreeMap) Segment(io.pravega.client.segment.impl.Segment) CompletableFuture(java.util.concurrent.CompletableFuture) StatusRuntimeException(io.grpc.StatusRuntimeException) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) DelegationTokenProvider(io.pravega.client.security.auth.DelegationTokenProvider) NoSuchSegmentException(io.pravega.client.segment.impl.NoSuchSegmentException) Test(org.junit.Test)

Example 4 with 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"));
}
Also used : ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) HashMap(java.util.HashMap) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) Cleanup(lombok.Cleanup) ConnectionFactory(io.pravega.client.connection.impl.ConnectionFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) SegmentMetadataClientFactoryImpl(io.pravega.client.segment.impl.SegmentMetadataClientFactoryImpl) LocalController(io.pravega.controller.server.eventProcessor.LocalController) StreamImpl(io.pravega.client.stream.impl.StreamImpl) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) Stream(io.pravega.client.stream.Stream) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) NoSuchSegmentException(io.pravega.client.segment.impl.NoSuchSegmentException) Test(org.junit.Test)

Example 5 with NoSuchSegmentException

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);
}
Also used : SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) UUID(java.util.UUID) NoSuchSegmentException(io.pravega.client.segment.impl.NoSuchSegmentException) Test(org.junit.Test)

Aggregations

NoSuchSegmentException (io.pravega.client.segment.impl.NoSuchSegmentException)10 Segment (io.pravega.client.segment.impl.Segment)6 Cleanup (lombok.Cleanup)5 Controller (io.pravega.client.control.impl.Controller)3 DelegationTokenProvider (io.pravega.client.security.auth.DelegationTokenProvider)3 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)3 SegmentSealedException (io.pravega.client.segment.impl.SegmentSealedException)3 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)3 Stream (io.pravega.client.stream.Stream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 AuthenticationException (io.pravega.auth.AuthenticationException)2 SegmentMetadataClient (io.pravega.client.segment.impl.SegmentMetadataClient)2 TruncatedDataException (io.pravega.client.stream.TruncatedDataException)2 Exceptions (io.pravega.common.Exceptions)2 Futures (io.pravega.common.concurrent.Futures)2 RandomFactory (io.pravega.common.hash.RandomFactory)2