Search in sources :

Example 1 with SynchronizerConfig

use of io.pravega.client.state.SynchronizerConfig in project pravega by pravega.

the class ClientFactoryImpl method createReader.

@VisibleForTesting
public <T> EventStreamReader<T> createReader(String readerId, String readerGroup, Serializer<T> s, ReaderConfig config, Supplier<Long> nanoTime, Supplier<Long> milliTime) {
    log.info("Creating reader: {} under readerGroup: {} with configuration: {}", readerId, readerGroup, config);
    SynchronizerConfig synchronizerConfig = SynchronizerConfig.builder().build();
    StateSynchronizer<ReaderGroupState> sync = createStateSynchronizer(NameUtils.getStreamForReaderGroup(readerGroup), new JavaSerializer<>(), new JavaSerializer<>(), synchronizerConfig);
    ReaderGroupStateManager stateManager = new ReaderGroupStateManager(readerId, sync, controller, nanoTime);
    stateManager.initializeReader(config.getInitialAllocationDelay());
    return new EventStreamReaderImpl<T>(inFactory, metaFactory, s, stateManager, new Orderer(), milliTime, config);
}
Also used : SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with SynchronizerConfig

use of io.pravega.client.state.SynchronizerConfig in project pravega by pravega.

the class ClientFactoryImpl method createRevisionedStreamClient.

@Override
public <T> RevisionedStreamClient<T> createRevisionedStreamClient(String streamName, Serializer<T> serializer, SynchronizerConfig config) {
    log.info("Creating revisioned stream client for stream: {} with synchronizer configuration: {}", streamName, config);
    Segment segment = new Segment(scope, streamName, 0);
    SegmentInputStream in = inFactory.createInputStreamForSegment(segment);
    // Segment sealed is not expected for Revisioned Stream Client.
    Consumer<Segment> segmentSealedCallBack = s -> {
        throw new IllegalStateException("RevisionedClient: Segmentsealed exception observed for segment:" + s);
    };
    String delegationToken = Futures.getAndHandleExceptions(controller.getOrRefreshDelegationTokenFor(segment.getScope(), segment.getStreamName()), RuntimeException::new);
    SegmentOutputStream out = outFactory.createOutputStreamForSegment(segment, segmentSealedCallBack, config.getEventWriterConfig(), delegationToken);
    SegmentMetadataClient meta = metaFactory.createSegmentMetadataClient(segment, delegationToken);
    return new RevisionedStreamClientImpl<>(segment, in, out, meta, serializer, controller, delegationToken);
}
Also used : StateSynchronizer(io.pravega.client.state.StateSynchronizer) Segment(io.pravega.client.segment.impl.Segment) EventStreamWriter(io.pravega.client.stream.EventStreamWriter) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SegmentMetadataClientFactoryImpl(io.pravega.client.segment.impl.SegmentMetadataClientFactoryImpl) Supplier(java.util.function.Supplier) SegmentMetadataClientFactory(io.pravega.client.segment.impl.SegmentMetadataClientFactory) Update(io.pravega.client.state.Update) Stream(io.pravega.client.stream.Stream) RevisionedStreamClient(io.pravega.client.state.RevisionedStreamClient) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) SegmentInputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) RevisionedStreamClientImpl(io.pravega.client.state.impl.RevisionedStreamClientImpl) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) Serializer(io.pravega.client.stream.Serializer) NameUtils(io.pravega.shared.NameUtils) BatchClientImpl(io.pravega.client.batch.impl.BatchClientImpl) SegmentInputStreamFactory(io.pravega.client.segment.impl.SegmentInputStreamFactory) StateSynchronizerImpl(io.pravega.client.state.impl.StateSynchronizerImpl) lombok.val(lombok.val) EventStreamReader(io.pravega.client.stream.EventStreamReader) InitialUpdate(io.pravega.client.state.InitialUpdate) Revisioned(io.pravega.client.state.Revisioned) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) Consumer(java.util.function.Consumer) Slf4j(lombok.extern.slf4j.Slf4j) SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) SegmentOutputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentOutputStreamFactoryImpl) UpdateOrInitSerializer(io.pravega.client.state.impl.UpdateOrInitSerializer) ConnectionFactoryImpl(io.pravega.client.netty.impl.ConnectionFactoryImpl) ClientFactory(io.pravega.client.ClientFactory) BatchClient(io.pravega.client.batch.BatchClient) InvalidStreamException(io.pravega.client.stream.InvalidStreamException) ReaderConfig(io.pravega.client.stream.ReaderConfig) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) Futures(io.pravega.common.concurrent.Futures) ClientConfig(io.pravega.client.ClientConfig) SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) RevisionedStreamClientImpl(io.pravega.client.state.impl.RevisionedStreamClientImpl) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Segment(io.pravega.client.segment.impl.Segment) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient)

Example 3 with SynchronizerConfig

use of io.pravega.client.state.SynchronizerConfig 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 4 with SynchronizerConfig

use of io.pravega.client.state.SynchronizerConfig 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 5 with SynchronizerConfig

use of io.pravega.client.state.SynchronizerConfig 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

SynchronizerConfig (io.pravega.client.state.SynchronizerConfig)19 ClientFactory (io.pravega.client.ClientFactory)16 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 Cleanup (lombok.Cleanup)15 Test (org.junit.Test)15 Segment (io.pravega.client.segment.impl.Segment)12 AtomicLong (java.util.concurrent.atomic.AtomicLong)11 HashMap (java.util.HashMap)9 lombok.val (lombok.val)6 Revision (io.pravega.client.state.Revision)4 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ReaderGroupImpl (io.pravega.client.stream.impl.ReaderGroupImpl)2 CreateCheckpoint (io.pravega.client.stream.impl.ReaderGroupState.CreateCheckpoint)2 Entry (java.util.Map.Entry)2 Preconditions (com.google.common.base.Preconditions)1 ClientConfig (io.pravega.client.ClientConfig)1