Search in sources :

Example 6 with SegmentMetadataClient

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

the class SegmentIteratorTest method testTruncate.

@Test(timeout = 5000)
public void testTruncate() {
    MockSegmentStreamFactory factory = new MockSegmentStreamFactory();
    Segment segment = new Segment("Scope", "Stream", 1);
    EventWriterConfig config = EventWriterConfig.builder().build();
    SegmentOutputStream outputStream = factory.createOutputStreamForSegment(segment, c -> {
    }, config, "");
    sendData("1", outputStream);
    sendData("2", outputStream);
    sendData("3", outputStream);
    SegmentMetadataClient metadataClient = factory.createSegmentMetadataClient(segment, "");
    long length = metadataClient.getSegmentInfo().getWriteOffset();
    @Cleanup SegmentIteratorImpl<String> iter = new SegmentIteratorImpl<>(factory, segment, stringSerializer, 0, length);
    assertEquals("1", iter.next());
    long segmentLength = metadataClient.fetchCurrentSegmentLength();
    assertEquals(0, segmentLength % 3);
    metadataClient.truncateSegment(segment, segmentLength * 2 / 3);
    AssertExtensions.assertThrows(TruncatedDataException.class, () -> iter.next());
    @Cleanup SegmentIteratorImpl<String> iter2 = new SegmentIteratorImpl<>(factory, segment, stringSerializer, segmentLength * 2 / 3, length);
    assertTrue(iter2.hasNext());
    assertEquals("3", iter2.next());
    assertFalse(iter.hasNext());
}
Also used : EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) Test(org.junit.Test)

Example 7 with SegmentMetadataClient

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

the class SegmentIteratorTest method testHasNext.

@Test(timeout = 5000)
public void testHasNext() {
    MockSegmentStreamFactory factory = new MockSegmentStreamFactory();
    Segment segment = new Segment("Scope", "Stream", 1);
    EventWriterConfig config = EventWriterConfig.builder().build();
    SegmentOutputStream outputStream = factory.createOutputStreamForSegment(segment, c -> {
    }, config, "");
    sendData("1", outputStream);
    sendData("2", outputStream);
    sendData("3", outputStream);
    SegmentMetadataClient metadataClient = factory.createSegmentMetadataClient(segment, "");
    long length = metadataClient.getSegmentInfo().getWriteOffset();
    @Cleanup SegmentIteratorImpl<String> iter = new SegmentIteratorImpl<>(factory, segment, stringSerializer, 0, length);
    assertTrue(iter.hasNext());
    assertTrue(iter.hasNext());
    assertEquals("1", iter.next());
    assertEquals("2", iter.next());
    assertTrue(iter.hasNext());
    assertEquals("3", iter.next());
    assertFalse(iter.hasNext());
    assertThrows(NoSuchElementException.class, () -> iter.next());
    assertFalse(iter.hasNext());
}
Also used : EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) Test(org.junit.Test)

Example 8 with SegmentMetadataClient

use of io.pravega.client.segment.impl.SegmentMetadataClient 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)

Aggregations

SegmentMetadataClient (io.pravega.client.segment.impl.SegmentMetadataClient)8 Segment (io.pravega.client.segment.impl.Segment)7 Cleanup (lombok.Cleanup)6 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)5 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)4 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)4 Test (org.junit.Test)4 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 ConnectionFactory (io.pravega.client.netty.impl.ConnectionFactory)1 ConnectionFactoryImpl (io.pravega.client.netty.impl.ConnectionFactoryImpl)1 NoSuchSegmentException (io.pravega.client.segment.impl.NoSuchSegmentException)1 SegmentInputStream (io.pravega.client.segment.impl.SegmentInputStream)1 SegmentInputStreamFactory (io.pravega.client.segment.impl.SegmentInputStreamFactory)1 SegmentInputStreamFactoryImpl (io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl)1 SegmentMetadataClientFactory (io.pravega.client.segment.impl.SegmentMetadataClientFactory)1