Search in sources :

Example 1 with ClientFactory

use of io.pravega.client.ClientFactory in project pravega by pravega.

the class RevisionedStreamClientTest method testMark.

@Test
public void testMark() {
    String scope = "scope";
    String stream = "stream";
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
    MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
    @Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
    SynchronizerConfig config = SynchronizerConfig.builder().build();
    RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, new JavaSerializer<>(), config);
    client.writeUnconditionally("a");
    Revision ra = client.fetchLatestRevision();
    client.writeUnconditionally("b");
    Revision rb = client.fetchLatestRevision();
    client.writeUnconditionally("c");
    Revision rc = client.fetchLatestRevision();
    assertTrue(client.compareAndSetMark(null, ra));
    assertEquals(ra, client.getMark());
    assertTrue(client.compareAndSetMark(ra, rb));
    assertEquals(rb, client.getMark());
    assertFalse(client.compareAndSetMark(ra, rc));
    assertEquals(rb, client.getMark());
    assertTrue(client.compareAndSetMark(rb, rc));
    assertEquals(rc, client.getMark());
    assertTrue(client.compareAndSetMark(rc, ra));
    assertEquals(ra, client.getMark());
    assertTrue(client.compareAndSetMark(ra, null));
    assertEquals(null, client.getMark());
}
Also used : ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) Revision(io.pravega.client.state.Revision) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) ClientFactory(io.pravega.client.ClientFactory) MockController(io.pravega.client.stream.mock.MockController) Cleanup(lombok.Cleanup) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Test(org.junit.Test)

Example 2 with ClientFactory

use of io.pravega.client.ClientFactory in project pravega by pravega.

the class RevisionedStreamClientTest method testWriteWhileReading.

@Test
public void testWriteWhileReading() {
    String scope = "scope";
    String stream = "stream";
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
    MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
    @Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
    SynchronizerConfig config = SynchronizerConfig.builder().build();
    RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, new JavaSerializer<>(), config);
    Revision initialRevision = client.fetchLatestRevision();
    client.writeUnconditionally("a");
    client.writeUnconditionally("b");
    client.writeUnconditionally("c");
    Iterator<Entry<Revision, String>> iter = client.readFrom(initialRevision);
    assertTrue(iter.hasNext());
    assertEquals("a", iter.next().getValue());
    client.writeUnconditionally("d");
    assertTrue(iter.hasNext());
    assertEquals("b", iter.next().getValue());
    assertTrue(iter.hasNext());
    Entry<Revision, String> entry = iter.next();
    assertEquals("c", entry.getValue());
    assertFalse(iter.hasNext());
    iter = client.readFrom(entry.getKey());
    assertTrue(iter.hasNext());
    assertEquals("d", iter.next().getValue());
    assertFalse(iter.hasNext());
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) ClientFactory(io.pravega.client.ClientFactory) Cleanup(lombok.Cleanup) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) Entry(java.util.Map.Entry) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) Revision(io.pravega.client.state.Revision) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Test(org.junit.Test)

Example 3 with ClientFactory

use of io.pravega.client.ClientFactory in project pravega by pravega.

the class EventProcessorTest method createMockSystem.

@SuppressWarnings("unchecked")
private EventProcessorSystemImpl createMockSystem(final String name, final String processId, final String scope, final SequenceAnswer<EventStreamReader<TestEvent>> readers, final EventStreamWriter<TestEvent> writer, final String readerGroupName) {
    ClientFactory clientFactory = Mockito.mock(ClientFactory.class);
    Mockito.when(clientFactory.createReader(anyString(), anyString(), any(), any())).thenAnswer(readers);
    Mockito.when(clientFactory.<TestEvent>createEventWriter(anyString(), any(), any())).thenReturn(writer);
    ReaderGroup readerGroup = Mockito.mock(ReaderGroup.class);
    Mockito.when(readerGroup.getGroupName()).thenReturn(readerGroupName);
    ReaderGroupManager readerGroupManager = Mockito.mock(ReaderGroupManager.class);
    Mockito.when(readerGroupManager.createReaderGroup(anyString(), any(ReaderGroupConfig.class))).then(invocation -> readerGroup);
    return new EventProcessorSystemImpl(name, processId, scope, clientFactory, readerGroupManager);
}
Also used : ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) ReaderGroup(io.pravega.client.stream.ReaderGroup) ClientFactory(io.pravega.client.ClientFactory)

Example 4 with ClientFactory

use of io.pravega.client.ClientFactory 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)

Example 5 with ClientFactory

use of io.pravega.client.ClientFactory in project pravega by pravega.

the class ReaderGroupStateManagerTest method testSegmentsAssigned.

@Test(timeout = 10000)
public void testSegmentsAssigned() throws ReinitializationRequiredException {
    String scope = "scope";
    String stream = "stream";
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
    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 = new HashMap<>();
    segments.put(new Segment(scope, stream, 0), 0L);
    segments.put(new Segment(scope, stream, 1), 1L);
    segments.put(new Segment(scope, stream, 2), 2L);
    segments.put(new Segment(scope, stream, 3), 3L);
    ReaderGroupStateManager.initializeReaderGroup(stateSynchronizer, ReaderGroupConfig.builder().stream(Stream.of(scope, stream)).build(), segments);
    ReaderGroupStateManager reader1 = new ReaderGroupStateManager("reader1", stateSynchronizer, controller, clock::get);
    reader1.initializeReader(0);
    ReaderGroupStateManager reader2 = new ReaderGroupStateManager("reader2", stateSynchronizer, controller, clock::get);
    reader2.initializeReader(0);
    Map<Segment, Long> segments1 = reader1.acquireNewSegmentsIfNeeded(0);
    assertFalse(segments1.isEmpty());
    assertEquals(2, segments1.size());
    assertTrue(reader1.acquireNewSegmentsIfNeeded(0).isEmpty());
    assertNull(reader1.findSegmentToReleaseIfRequired());
    Map<Segment, Long> segments2 = reader2.acquireNewSegmentsIfNeeded(0);
    assertFalse(segments2.isEmpty());
    assertEquals(2, segments2.size());
    assertTrue(reader2.acquireNewSegmentsIfNeeded(0).isEmpty());
    assertNull(reader2.findSegmentToReleaseIfRequired());
    assertTrue(Sets.intersection(segments1.keySet(), segments2.keySet()).isEmpty());
    clock.addAndGet(ReaderGroupStateManager.UPDATE_WINDOW.toNanos());
    assertFalse(reader1.releaseSegment(new Segment(scope, stream, 0), 0, 0));
    clock.addAndGet(ReaderGroupStateManager.UPDATE_WINDOW.toNanos());
    assertTrue(reader1.acquireNewSegmentsIfNeeded(0).isEmpty());
    assertNull(reader1.findSegmentToReleaseIfRequired());
    assertTrue(reader2.acquireNewSegmentsIfNeeded(0).isEmpty());
    assertNull(reader2.findSegmentToReleaseIfRequired());
    reader1.readerShutdown(new PositionImpl(segments1));
    Map<Segment, Long> segmentsRecovered = reader2.acquireNewSegmentsIfNeeded(0);
    assertFalse(segmentsRecovered.isEmpty());
    assertEquals(2, segmentsRecovered.size());
    assertEquals(segments1, segmentsRecovered);
    assertTrue(reader2.acquireNewSegmentsIfNeeded(0).isEmpty());
    assertNull(reader2.findSegmentToReleaseIfRequired());
    segments2.putAll(segmentsRecovered);
    reader2.readerShutdown(new PositionImpl(segments2));
    reader1.initializeReader(0);
    segments1 = reader1.acquireNewSegmentsIfNeeded(0);
    assertEquals(4, segments1.size());
    assertEquals(segments2, segments1);
}
Also used : HashMap(java.util.HashMap) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) ClientFactory(io.pravega.client.ClientFactory) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) AtomicLong(java.util.concurrent.atomic.AtomicLong) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) AtomicLong(java.util.concurrent.atomic.AtomicLong) MockController(io.pravega.client.stream.mock.MockController) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Test(org.junit.Test)

Aggregations

ClientFactory (io.pravega.client.ClientFactory)47 Cleanup (lombok.Cleanup)39 Test (org.junit.Test)36 HashMap (java.util.HashMap)22 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)21 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)20 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)20 Controller (io.pravega.client.stream.impl.Controller)17 SynchronizerConfig (io.pravega.client.state.SynchronizerConfig)15 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)15 MockController (io.pravega.client.stream.mock.MockController)15 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)15 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)15 ConnectionFactoryImpl (io.pravega.client.netty.impl.ConnectionFactoryImpl)14 Stream (io.pravega.client.stream.Stream)13 AtomicLong (java.util.concurrent.atomic.AtomicLong)13 Segment (io.pravega.client.segment.impl.Segment)12 StreamImpl (io.pravega.client.stream.impl.StreamImpl)12 ReaderGroupManagerImpl (io.pravega.client.admin.impl.ReaderGroupManagerImpl)10 ConnectionFactory (io.pravega.client.netty.impl.ConnectionFactory)9