Search in sources :

Example 16 with SynchronizerConfig

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

the class ReaderGroupStateManagerTest method testRemoveReader.

@Test(timeout = 10000)
public void testRemoveReader() 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), 123L);
    segments.put(new Segment(scope, stream, 1), 456L);
    ReaderGroupStateManager.initializeReaderGroup(stateSynchronizer, ReaderGroupConfig.builder().stream(Stream.of(scope, stream)).build(), segments);
    ReaderGroupStateManager readerState1 = new ReaderGroupStateManager("testReader", stateSynchronizer, controller, clock::get);
    readerState1.initializeReader(0);
    Segment toRelease = readerState1.findSegmentToReleaseIfRequired();
    assertNull(toRelease);
    Map<Segment, Long> newSegments = readerState1.acquireNewSegmentsIfNeeded(0);
    assertFalse(newSegments.isEmpty());
    assertEquals(2, newSegments.size());
    ReaderGroupStateManager readerState2 = new ReaderGroupStateManager("testReader2", stateSynchronizer, controller, clock::get);
    readerState2.initializeReader(0);
    clock.addAndGet(ReaderGroupStateManager.UPDATE_WINDOW.toNanos());
    assertNotNull(readerState1.findSegmentToReleaseIfRequired());
    boolean released = readerState1.releaseSegment(new Segment(scope, stream, 0), 789L, 0L);
    assertTrue(released);
    newSegments = readerState2.acquireNewSegmentsIfNeeded(0);
    assertEquals(1, newSegments.size());
    assertEquals(Long.valueOf(789L), newSegments.get(new Segment(scope, stream, 0)));
    ReaderGroupStateManager.readerShutdown("testReader2", null, stateSynchronizer);
    AssertExtensions.assertThrows(ReinitializationRequiredException.class, () -> readerState2.releaseSegment(new Segment(scope, stream, 0), 711L, 0L));
    clock.addAndGet(ReaderGroupStateManager.UPDATE_WINDOW.toNanos());
    newSegments = readerState1.acquireNewSegmentsIfNeeded(0);
    assertEquals(1, newSegments.size());
    assertEquals(Long.valueOf(789L), newSegments.get(new Segment(scope, stream, 0)));
    AssertExtensions.assertThrows(ReinitializationRequiredException.class, () -> readerState2.acquireNewSegmentsIfNeeded(0L));
}
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)

Example 17 with SynchronizerConfig

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

the class ReaderGroupStateManagerTest method testSegmentSplit.

@Test(timeout = 20000)
public void testSegmentSplit() throws ReinitializationRequiredException {
    String scope = "scope";
    String stream = "stream";
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    Segment initialSegment = new Segment(scope, stream, 0);
    Segment successorA = new Segment(scope, stream, 1);
    Segment successorB = new Segment(scope, stream, 2);
    MockController controller = new MockControllerWithSuccessors(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, new StreamSegmentsWithPredecessors(ImmutableMap.of(new SegmentWithRange(successorA, 0.0, 0.5), singletonList(0), new SegmentWithRange(successorB, 0.5, 1.0), singletonList(0)), ""));
    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);
    Map<Segment, Long> segments = new HashMap<>();
    segments.put(initialSegment, 1L);
    ReaderGroupStateManager.initializeReaderGroup(stateSynchronizer, ReaderGroupConfig.builder().stream(Stream.of(scope, stream)).build(), segments);
    val readerState = new ReaderGroupStateManager("testReader", stateSynchronizer, controller, null);
    readerState.initializeReader(0);
    Map<Segment, Long> newSegments = readerState.acquireNewSegmentsIfNeeded(0);
    assertEquals(1, newSegments.size());
    assertEquals(Long.valueOf(1), newSegments.get(initialSegment));
    readerState.handleEndOfSegment(initialSegment);
    newSegments = readerState.acquireNewSegmentsIfNeeded(0);
    assertEquals(2, newSegments.size());
    assertEquals(Long.valueOf(0), newSegments.get(successorA));
    assertEquals(Long.valueOf(0), newSegments.get(successorB));
    newSegments = readerState.acquireNewSegmentsIfNeeded(0);
    assertTrue(newSegments.isEmpty());
}
Also used : lombok.val(lombok.val) 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) 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)

Example 18 with SynchronizerConfig

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

the class MockStreamManager method createReaderGroup.

@Override
public ReaderGroup createReaderGroup(String groupName, ReaderGroupConfig config) {
    NameUtils.validateReaderGroupName(groupName);
    createStreamHelper(NameUtils.getStreamForReaderGroup(groupName), StreamConfiguration.builder().scope(scope).streamName(NameUtils.getStreamForReaderGroup(groupName)).scalingPolicy(ScalingPolicy.fixed(1)).build());
    SynchronizerConfig synchronizerConfig = SynchronizerConfig.builder().build();
    ReaderGroupImpl result = new ReaderGroupImpl(scope, groupName, synchronizerConfig, new JavaSerializer<>(), new JavaSerializer<>(), clientFactory, controller, connectionFactory);
    result.initializeGroup(config);
    return result;
}
Also used : ReaderGroupImpl(io.pravega.client.stream.impl.ReaderGroupImpl) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig)

Example 19 with SynchronizerConfig

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

the class ReaderGroupManagerImpl method createReaderGroup.

@Override
public ReaderGroup createReaderGroup(String groupName, ReaderGroupConfig config) {
    log.info("Creating reader group: {} for streams: {} with configuration: {}", groupName, Arrays.toString(config.getStartingStreamCuts().keySet().toArray()), config);
    NameUtils.validateReaderGroupName(groupName);
    createStreamHelper(getStreamForReaderGroup(groupName), StreamConfiguration.builder().scope(scope).streamName(getStreamForReaderGroup(groupName)).scalingPolicy(ScalingPolicy.fixed(1)).build());
    SynchronizerConfig synchronizerConfig = SynchronizerConfig.builder().build();
    ReaderGroupImpl result = new ReaderGroupImpl(scope, groupName, synchronizerConfig, new JavaSerializer<>(), new JavaSerializer<>(), clientFactory, controller, connectionFactory);
    result.initializeGroup(config);
    return result;
}
Also used : ReaderGroupImpl(io.pravega.client.stream.impl.ReaderGroupImpl) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig)

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