Search in sources :

Example 31 with Segment

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

the class EventStreamReaderTest method testAcquireSegment.

@SuppressWarnings("unused")
@Test(timeout = 10000)
public void testAcquireSegment() 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 segment1 = Segment.fromScopedName("Foo/Bar/0");
    Segment segment2 = Segment.fromScopedName("Foo/Bar/1");
    Mockito.when(groupState.acquireNewSegmentsIfNeeded(0L)).thenReturn(ImmutableMap.of(segment1, 0L)).thenReturn(ImmutableMap.of(segment2, 0L)).thenReturn(Collections.emptyMap());
    SegmentOutputStream stream1 = segmentStreamFactory.createOutputStreamForSegment(segment1, segmentSealedCallback, writerConfig, "");
    SegmentOutputStream stream2 = segmentStreamFactory.createOutputStreamForSegment(segment2, segmentSealedCallback, writerConfig, "");
    writeInt(stream1, 1);
    writeInt(stream1, 2);
    writeInt(stream2, 3);
    writeInt(stream2, 4);
    reader.readNextEvent(0);
    List<SegmentInputStream> readers = reader.getReaders();
    assertEquals(1, readers.size());
    Assert.assertEquals(segment1, readers.get(0).getSegmentId());
    reader.readNextEvent(0);
    readers = reader.getReaders();
    assertEquals(2, readers.size());
    Assert.assertEquals(segment1, readers.get(0).getSegmentId());
    Assert.assertEquals(segment2, readers.get(1).getSegmentId());
    reader.close();
}
Also used : SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) AtomicLong(java.util.concurrent.atomic.AtomicLong) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Example 32 with Segment

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

the class EventStreamReaderTest method testEndOfSegmentWithoutSuccessors.

@Test(timeout = 10000)
public void testEndOfSegmentWithoutSuccessors() 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 buffer = writeInt(stream, 1);
    EventRead<byte[]> read = reader.readNextEvent(0);
    byte[] event = read.getEvent();
    assertEquals(buffer, ByteBuffer.wrap(event));
    read = reader.readNextEvent(0);
    assertNull(read.getEvent());
    read = reader.readNextEvent(0);
    assertNull(read.getEvent());
    assertEquals(0, reader.getReaders().size());
    reader.close();
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) AtomicLong(java.util.concurrent.atomic.AtomicLong) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Test(org.junit.Test)

Example 33 with Segment

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

the class SynchronizerTest method testLocking.

@Test(timeout = 20000)
public void testLocking() {
    String streamName = "streamName";
    String scope = "scope";
    Segment segment = new Segment(scope, streamName, 0);
    BlockingUpdate[] updates = new BlockingUpdate[] { new BlockingUpdate(1), new BlockingUpdate(2), new BlockingUpdate(3), new BlockingUpdate(4) };
    MockRevisionedStreamClient client = new MockRevisionedStreamClient();
    client.segment = segment;
    @Cleanup StateSynchronizerImpl<RevisionedImpl> sync = new StateSynchronizerImpl<RevisionedImpl>(segment, client);
    client.init = new BlockingUpdate(0);
    client.updates = updates;
    client.visableLength = 2;
    client.init.latch.release();
    updates[0].latch.release();
    updates[1].latch.release();
    sync.fetchUpdates();
    RevisionedImpl state1 = sync.getState();
    assertEquals(new RevisionImpl(segment, 2, 2), state1.getRevision());
    client.visableLength = 3;
    Async.testBlocking(() -> {
        sync.fetchUpdates();
    }, () -> updates[2].latch.release());
    RevisionedImpl state2 = sync.getState();
    assertEquals(new RevisionImpl(segment, 3, 3), state2.getRevision());
    client.visableLength = 4;
    Async.testBlocking(() -> {
        sync.fetchUpdates();
    }, () -> {
        client.visableLength = 3;
        sync.getState();
        updates[3].latch.release();
    });
    RevisionedImpl state3 = sync.getState();
    assertEquals(new RevisionImpl(segment, 4, 4), state3.getRevision());
}
Also used : Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Example 34 with Segment

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

the class EventPointerTest method testEventPointerImpl.

/**
 * Simple exercise of event pointers. The test case
 * creates an impl instance with some arbitrary values,
 * serializes the pointer, deserializes it, and asserts
 * that the values obtained are the expected ones.
 */
@Test
public void testEventPointerImpl() {
    String scope = "testScope";
    String stream = "testStream";
    int segmentId = 1;
    Segment segment = new Segment(scope, stream, segmentId);
    EventPointer pointer = new EventPointerImpl(segment, 10L, 10);
    ByteArrayOutputStream bos = null;
    // Serialize pointer
    try {
        bos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bos);
        out.writeObject(pointer);
        out.close();
    } catch (IOException e) {
        fail("Failed to serialize pointer object" + e.getMessage());
    }
    // Deserialize pointer
    pointer = null;
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream in = new ObjectInputStream(bis);
        pointer = (EventPointer) in.readObject();
    } catch (IOException | ClassNotFoundException e) {
        fail("Failed to deserialize object" + e.getMessage());
    }
    // Verifies that we have been able to deserialize successfully
    if (pointer == null) {
        fail("Failed to deserialize object");
    } else {
        StringBuilder name = new StringBuilder();
        name.append(scope);
        name.append("/");
        name.append(stream);
        assertTrue("Scoped stream name: " + pointer.asImpl().getSegment().getScopedStreamName(), pointer.asImpl().getSegment().getScopedStreamName().equals(name.toString()));
        name.append("/");
        name.append(segmentId);
        assertTrue("Scoped name: " + pointer.asImpl().getSegment().getScopedName(), pointer.asImpl().getSegment().getScopedName().equals(name.toString()));
        assertTrue(pointer.asImpl().getEventStartOffset() == 10L);
        assertTrue(pointer.asImpl().getEventLength() == 10);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Segment(io.pravega.client.segment.impl.Segment) EventPointer(io.pravega.client.stream.EventPointer) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 35 with Segment

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

the class EventStreamWriterTest method testSegmentSealedInSegmentSealed.

@Test
public void testSegmentSealedInSegmentSealed() {
    String scope = "scope";
    String streamName = "stream";
    String routingKey = "RoutingKey";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment1 = new Segment(scope, streamName, 0);
    Segment segment2 = new Segment(scope, streamName, 1);
    Segment segment3 = new Segment(scope, streamName, 2);
    EventWriterConfig config = EventWriterConfig.builder().build();
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    Controller controller = Mockito.mock(Controller.class);
    FakeSegmentOutputStream outputStream1 = new FakeSegmentOutputStream(segment1);
    FakeSegmentOutputStream outputStream2 = new FakeSegmentOutputStream(segment2);
    FakeSegmentOutputStream outputStream3 = new FakeSegmentOutputStream(segment3);
    Mockito.when(controller.getCurrentSegments(scope, streamName)).thenReturn(getSegmentsFuture(segment1));
    Mockito.when(controller.getSuccessors(segment1)).thenReturn(getReplacement(segment1, segment2));
    Mockito.when(controller.getSuccessors(segment2)).thenReturn(getReplacement(segment2, segment3));
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment1), any(), any(), any())).thenAnswer(i -> {
        outputStream1.callBackForSealed = i.getArgument(1);
        return outputStream1;
    });
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment2), any(), any(), any())).thenAnswer(i -> {
        outputStream2.callBackForSealed = i.getArgument(1);
        return outputStream2;
    });
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment3), any(), any(), any())).thenAnswer(i -> {
        outputStream3.callBackForSealed = i.getArgument(1);
        return outputStream3;
    });
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup EventStreamWriter<String> writer = new EventStreamWriterImpl<>(stream, controller, streamFactory, serializer, config, new InlineExecutor());
    writer.writeEvent(routingKey, "Foo");
    Mockito.verify(controller).getCurrentSegments(any(), any());
    assertEquals(1, outputStream1.getUnackedEventsOnSeal().size());
    assertTrue(outputStream2.getUnackedEventsOnSeal().isEmpty());
    outputStream1.invokeSealedCallBack();
    outputStream2.invokeSealedCallBack();
    writer.writeEvent(routingKey, "Bar");
    Mockito.verify(controller, Mockito.times(1)).getCurrentSegments(any(), any());
    assertEquals(1, outputStream2.getUnackedEventsOnSeal().size());
    assertEquals("Foo", serializer.deserialize(outputStream2.getUnackedEventsOnSeal().get(0).getData()));
    assertEquals(2, outputStream3.getUnackedEventsOnSeal().size());
    assertEquals("Foo", serializer.deserialize(outputStream3.getUnackedEventsOnSeal().get(0).getData()));
    assertEquals("Bar", serializer.deserialize(outputStream3.getUnackedEventsOnSeal().get(1).getData()));
}
Also used : Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) InlineExecutor(io.pravega.test.common.InlineExecutor) Test(org.junit.Test)

Aggregations

Segment (io.pravega.client.segment.impl.Segment)97 Test (org.junit.Test)63 Cleanup (lombok.Cleanup)35 HashMap (java.util.HashMap)27 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)22 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)19 AtomicLong (java.util.concurrent.atomic.AtomicLong)19 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)17 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)16 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)16 MockController (io.pravega.client.stream.mock.MockController)15 ClientFactory (io.pravega.client.ClientFactory)13 Stream (io.pravega.client.stream.Stream)13 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)13 SynchronizerConfig (io.pravega.client.state.SynchronizerConfig)12 InlineExecutor (io.pravega.test.common.InlineExecutor)12 TreeMap (java.util.TreeMap)10 lombok.val (lombok.val)10 StreamCut (io.pravega.client.stream.StreamCut)8 ByteBuffer (java.nio.ByteBuffer)8