Search in sources :

Example 1 with NoSuchEventException

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

the class ReadTest method testEventPointer.

@Test(timeout = 10000)
public void testEventPointer() throws ReinitializationRequiredException {
    String endpoint = "localhost";
    String streamName = "abc";
    String readerName = "reader";
    String readerGroup = "group";
    int port = TestUtils.getAvailableListenPort();
    String testString = "Hello world ";
    String scope = "Scope1";
    StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
    server.startListening();
    @Cleanup MockStreamManager streamManager = new MockStreamManager(scope, endpoint, port);
    MockClientFactory clientFactory = streamManager.getClientFactory();
    ReaderGroupConfig groupConfig = ReaderGroupConfig.builder().disableAutomaticCheckpoints().stream(Stream.of(scope, streamName)).build();
    streamManager.createScope(scope);
    streamManager.createStream(scope, streamName, null);
    streamManager.createReaderGroup(readerGroup, groupConfig);
    JavaSerializer<String> serializer = new JavaSerializer<>();
    EventStreamWriter<String> producer = clientFactory.createEventWriter(streamName, serializer, EventWriterConfig.builder().build());
    for (int i = 0; i < 100; i++) {
        producer.writeEvent(testString + i);
    }
    producer.flush();
    @Cleanup EventStreamReader<String> reader = clientFactory.createReader(readerName, readerGroup, serializer, ReaderConfig.builder().build());
    try {
        EventPointer pointer;
        String read;
        for (int i = 0; i < 100; i++) {
            pointer = reader.readNextEvent(5000).getEventPointer();
            read = reader.fetchEvent(pointer);
            assertEquals(testString + i, read);
        }
    } catch (NoSuchEventException e) {
        fail("Failed to read event using event pointer");
    }
}
Also used : ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) NoSuchEventException(io.pravega.client.segment.impl.NoSuchEventException) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) EventPointer(io.pravega.client.stream.EventPointer) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) MockStreamManager(io.pravega.client.stream.mock.MockStreamManager) Test(org.junit.Test)

Example 2 with NoSuchEventException

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

the class EventStreamReaderImpl method fetchEvent.

@Override
public Type fetchEvent(EventPointer pointer) throws NoSuchEventException {
    Preconditions.checkNotNull(pointer);
    // Create SegmentInputStream
    @Cleanup SegmentInputStream inputStream = inputStreamFactory.createInputStreamForSegment(pointer.asImpl().getSegment(), pointer.asImpl().getEventLength());
    inputStream.setOffset(pointer.asImpl().getEventStartOffset());
    // Read event
    try {
        ByteBuffer buffer = inputStream.read();
        Type result = deserializer.deserialize(buffer);
        return result;
    } catch (EndOfSegmentException e) {
        throw new NoSuchEventException(e.getMessage());
    } catch (NoSuchSegmentException | SegmentTruncatedException e) {
        throw new NoSuchEventException("Event no longer exists.");
    }
}
Also used : SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) EndOfSegmentException(io.pravega.client.segment.impl.EndOfSegmentException) NoSuchEventException(io.pravega.client.segment.impl.NoSuchEventException) SegmentTruncatedException(io.pravega.client.segment.impl.SegmentTruncatedException) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) NoSuchSegmentException(io.pravega.client.segment.impl.NoSuchSegmentException)

Aggregations

NoSuchEventException (io.pravega.client.segment.impl.NoSuchEventException)2 Cleanup (lombok.Cleanup)2 EndOfSegmentException (io.pravega.client.segment.impl.EndOfSegmentException)1 NoSuchSegmentException (io.pravega.client.segment.impl.NoSuchSegmentException)1 SegmentInputStream (io.pravega.client.segment.impl.SegmentInputStream)1 SegmentTruncatedException (io.pravega.client.segment.impl.SegmentTruncatedException)1 EventPointer (io.pravega.client.stream.EventPointer)1 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)1 JavaSerializer (io.pravega.client.stream.impl.JavaSerializer)1 MockClientFactory (io.pravega.client.stream.mock.MockClientFactory)1 MockStreamManager (io.pravega.client.stream.mock.MockStreamManager)1 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)1 PravegaConnectionListener (io.pravega.segmentstore.server.host.handler.PravegaConnectionListener)1 ByteBuffer (java.nio.ByteBuffer)1 Test (org.junit.Test)1