Search in sources :

Example 6 with MockSegmentStreamFactory

use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.

the class EventStreamReaderTest method testReleaseSegment.

@Test(timeout = 10000)
public void testReleaseSegment() 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, segment2, 0L)).thenReturn(Collections.emptyMap());
    SegmentOutputStream stream1 = segmentStreamFactory.createOutputStreamForSegment(segment1, segmentSealedCallback, writerConfig, "");
    SegmentOutputStream stream2 = segmentStreamFactory.createOutputStreamForSegment(segment2, segmentSealedCallback, writerConfig, "");
    writeInt(stream1, 1);
    writeInt(stream2, 2);
    reader.readNextEvent(0);
    List<SegmentInputStream> readers = reader.getReaders();
    assertEquals(2, readers.size());
    Assert.assertEquals(segment1, readers.get(0).getSegmentId());
    Assert.assertEquals(segment2, readers.get(1).getSegmentId());
    Mockito.when(groupState.getCheckpoint()).thenReturn("checkpoint");
    assertTrue(reader.readNextEvent(0).isCheckpoint());
    Mockito.when(groupState.getCheckpoint()).thenReturn(null);
    Mockito.when(groupState.findSegmentToReleaseIfRequired()).thenReturn(segment2);
    Mockito.when(groupState.releaseSegment(Mockito.eq(segment2), Mockito.anyLong(), Mockito.anyLong())).thenReturn(true);
    assertFalse(reader.readNextEvent(0).isCheckpoint());
    Mockito.verify(groupState).releaseSegment(Mockito.eq(segment2), Mockito.anyLong(), Mockito.anyLong());
    readers = reader.getReaders();
    assertEquals(1, readers.size());
    Assert.assertEquals(segment1, readers.get(0).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 7 with MockSegmentStreamFactory

use of io.pravega.client.stream.mock.MockSegmentStreamFactory 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 8 with MockSegmentStreamFactory

use of io.pravega.client.stream.mock.MockSegmentStreamFactory 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 9 with MockSegmentStreamFactory

use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.

the class SynchronizerTest method testSetOperations.

@Test(timeout = 20000)
public void testSetOperations() throws EndOfSegmentException {
    String streamName = "testCompactionShrinksSet";
    String scope = "scope";
    MockSegmentStreamFactory ioFactory = new MockSegmentStreamFactory();
    @Cleanup MockClientFactory clientFactory = new MockClientFactory(scope, ioFactory);
    SetSynchronizer<String> set = SetSynchronizer.createNewSet(streamName, clientFactory);
    SetSynchronizer<String> set2 = SetSynchronizer.createNewSet(streamName, clientFactory);
    assertEquals(0, set.getCurrentSize());
    set.add("Foo");
    set2.add("Bar");
    set.update();
    assertEquals(2, set.getCurrentSize());
    set.clear();
    assertEquals(0, set.getCurrentSize());
    set.add("Baz");
    assertEquals(2, set2.getCurrentSize());
    set2.remove("Bar");
    assertEquals(1, set2.getCurrentSize());
    set2.remove("Baz");
    assertEquals(0, set2.getCurrentSize());
    set.update();
    assertEquals(0, set.getCurrentSize());
    SetSynchronizer<String> set3 = SetSynchronizer.createNewSet(streamName, clientFactory);
    set3.update();
    assertEquals(0, set3.getCurrentSize());
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) Test(org.junit.Test)

Example 10 with MockSegmentStreamFactory

use of io.pravega.client.stream.mock.MockSegmentStreamFactory in project pravega by pravega.

the class ReaderGroupStateManagerTest method testSegmentsCannotBeReleasedWithoutCheckpoint.

@Test(timeout = 10000)
public void testSegmentsCannotBeReleasedWithoutCheckpoint() throws ReinitializationRequiredException {
    String scope = "scope";
    String stream = "stream";
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    Segment segment0 = new Segment(scope, stream, 0);
    Segment segment1 = new Segment(scope, stream, 1);
    Segment segment2 = new Segment(scope, stream, 2);
    MockController controller = new MockControllerWithSuccessors(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, new StreamSegmentsWithPredecessors(ImmutableMap.of(), ""));
    MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
    @Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
    SynchronizerConfig config = SynchronizerConfig.builder().build();
    @Cleanup StateSynchronizer<ReaderGroupState> stateSynchronizer = createState(stream, clientFactory, config);
    AtomicLong clock = new AtomicLong();
    Map<Segment, Long> segments = ImmutableMap.of(segment0, 0L, segment1, 1L, segment2, 2L);
    ReaderGroupStateManager.initializeReaderGroup(stateSynchronizer, ReaderGroupConfig.builder().stream(Stream.of(scope, stream)).build(), segments);
    val readerState1 = new ReaderGroupStateManager("reader1", stateSynchronizer, controller, clock::get);
    readerState1.initializeReader(0);
    val readerState2 = new ReaderGroupStateManager("reader2", stateSynchronizer, controller, clock::get);
    readerState2.initializeReader(0);
    assertEquals(segments, stateSynchronizer.getState().getUnassignedSegments());
    stateSynchronizer.updateStateUnconditionally(new CreateCheckpoint("CP1"));
    stateSynchronizer.fetchUpdates();
    assertEquals("CP1", readerState1.getCheckpoint());
    assertEquals(Collections.emptyMap(), readerState1.acquireNewSegmentsIfNeeded(1));
    assertEquals(Collections.emptyMap(), readerState2.acquireNewSegmentsIfNeeded(2));
    assertEquals("CP1", readerState2.getCheckpoint());
    readerState1.checkpoint("CP1", new PositionImpl(Collections.emptyMap()));
    readerState2.checkpoint("CP1", new PositionImpl(Collections.emptyMap()));
    assertEquals(segments, stateSynchronizer.getState().getPositionsForCompletedCheckpoint("CP1"));
    Map<Segment, Long> segments1 = readerState1.acquireNewSegmentsIfNeeded(1);
    Map<Segment, Long> segments2 = readerState2.acquireNewSegmentsIfNeeded(2);
    assertFalse(segments1.isEmpty());
    assertFalse(segments2.isEmpty());
    assertEquals(0, stateSynchronizer.getState().getNumberOfUnassignedSegments());
    // Induce imbalance
    for (Entry<Segment, Long> entry : segments1.entrySet()) {
        stateSynchronizer.updateStateUnconditionally(new ReaderGroupState.ReleaseSegment("reader1", entry.getKey(), entry.getValue()));
        stateSynchronizer.updateStateUnconditionally(new ReaderGroupState.AcquireSegment("reader2", entry.getKey()));
    }
    stateSynchronizer.updateStateUnconditionally(new CreateCheckpoint("CP2"));
    stateSynchronizer.fetchUpdates();
    clock.addAndGet(ReaderGroupStateManager.UPDATE_WINDOW.toNanos());
    assertNull(readerState1.findSegmentToReleaseIfRequired());
    assertNull(readerState2.findSegmentToReleaseIfRequired());
    clock.addAndGet(ReaderGroupStateManager.UPDATE_WINDOW.toNanos());
    assertFalse(readerState2.releaseSegment(segments2.keySet().iterator().next(), 20, 2));
    clock.addAndGet(ReaderGroupStateManager.UPDATE_WINDOW.toNanos());
    readerState1.checkpoint("CP2", new PositionImpl(Collections.emptyMap()));
    readerState2.checkpoint("CP2", new PositionImpl(segments));
    assertEquals(segments, stateSynchronizer.getState().getPositionsForCompletedCheckpoint("CP2"));
    Segment toRelease = readerState2.findSegmentToReleaseIfRequired();
    assertNotNull(toRelease);
    assertTrue(readerState2.releaseSegment(toRelease, 10, 1));
    assertEquals(1, stateSynchronizer.getState().getNumberOfUnassignedSegments());
}
Also used : ClientFactory(io.pravega.client.ClientFactory) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) CreateCheckpoint(io.pravega.client.stream.impl.ReaderGroupState.CreateCheckpoint) lombok.val(lombok.val) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) MockController(io.pravega.client.stream.mock.MockController) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Test(org.junit.Test)

Aggregations

MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)29 Test (org.junit.Test)29 Segment (io.pravega.client.segment.impl.Segment)22 Cleanup (lombok.Cleanup)21 AtomicLong (java.util.concurrent.atomic.AtomicLong)19 ClientFactory (io.pravega.client.ClientFactory)15 SynchronizerConfig (io.pravega.client.state.SynchronizerConfig)15 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)15 MockController (io.pravega.client.stream.mock.MockController)15 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)15 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)11 HashMap (java.util.HashMap)9 ByteBuffer (java.nio.ByteBuffer)6 lombok.val (lombok.val)5 SegmentMetadataClient (io.pravega.client.segment.impl.SegmentMetadataClient)4 Revision (io.pravega.client.state.Revision)4 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)4 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)3 CreateCheckpoint (io.pravega.client.stream.impl.ReaderGroupState.CreateCheckpoint)3 MockClientFactory (io.pravega.client.stream.mock.MockClientFactory)3