Search in sources :

Example 1 with Update

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

the class ReaderGroupImplTest method testAsyncResetReaderGroup.

@Test(timeout = 10000L)
@SuppressWarnings("unchecked")
public void testAsyncResetReaderGroup() {
    UUID rgId = UUID.randomUUID();
    ReaderGroupConfig config1 = ReaderGroupConfig.builder().startFromStreamCuts(ImmutableMap.<Stream, StreamCut>builder().put(createStream("s1"), createStreamCut("s1", 2)).build()).build();
    config1 = ReaderGroupConfig.cloneConfig(config1, rgId, 0L);
    ReaderGroupConfig config2 = ReaderGroupConfig.builder().startFromStreamCuts(ImmutableMap.<Stream, StreamCut>builder().put(createStream("s2"), createStreamCut("s2", 2)).build()).build();
    config2 = ReaderGroupConfig.cloneConfig(config2, rgId, 0L);
    ReaderGroupConfig config3 = ReaderGroupConfig.builder().startFromStreamCuts(ImmutableMap.<Stream, StreamCut>builder().put(createStream("s3"), createStreamCut("s3", 2)).build()).build();
    config3 = ReaderGroupConfig.cloneConfig(config3, rgId, 0L);
    AtomicInteger x = new AtomicInteger(0);
    CompletableFuture<Void> wait = new CompletableFuture<>();
    CompletableFuture<Void> signal = new CompletableFuture<>();
    // The client's config.
    AtomicReference<ReaderGroupConfig> atomicConfig = new AtomicReference<>(config1);
    // The controller's config.
    AtomicReference<ReaderGroupConfig> atomicConfigController = new AtomicReference<>(config1);
    // return the client's config when calling state.getConfig.
    doAnswer(a -> atomicConfig.get()).when(state).getConfig();
    when(synchronizer.getState()).thenReturn(state);
    // return controllerConfig when calling getReaderGroupConfig.
    doAnswer(a -> CompletableFuture.completedFuture(atomicConfigController.get())).when(controller).getReaderGroupConfig(anyString(), anyString());
    // update the client config to the controller config whenever we update the StateSync.
    doAnswer(a -> {
        atomicConfig.set(atomicConfigController.get());
        return null;
    }).when(synchronizer).updateState(any(StateSynchronizer.UpdateGenerator.class));
    // update the controller config with the incremented generation if the generations match.
    doAnswer(a -> {
        ReaderGroupConfig c = a.getArgument(2);
        // the first one to call needs to wait until the second call has been completed.
        if (x.getAndIncrement() == 0) {
            signal.complete(null);
            wait.join();
        }
        if (c.getGeneration() == atomicConfigController.get().getGeneration()) {
            long incGen = c.getGeneration() + 1;
            atomicConfigController.set(ReaderGroupConfig.cloneConfig(c, c.getReaderGroupId(), incGen));
            return CompletableFuture.completedFuture(incGen);
        } else {
            CompletableFuture<Long> badFuture = new CompletableFuture<>();
            badFuture.completeExceptionally(new ReaderGroupConfigRejectedException("handle"));
            return badFuture;
        }
    }).when(controller).updateReaderGroup(anyString(), anyString(), any(ReaderGroupConfig.class));
    // run the first call async.
    final ReaderGroupConfig newConf = config2;
    CompletableFuture<Void> future = CompletableFuture.runAsync(() -> readerGroup.resetReaderGroup(newConf));
    // Once the first call has reached the controller.updateReaderGroup step then signal so he waits.
    signal.join();
    // start the second call.
    readerGroup.resetReaderGroup(config3);
    // Once the second is completed stop the wait so the first call can go ahead.
    wait.complete(null);
    // wait for the first call to complete.
    assertTrue(Futures.await(future));
    // assert the generation was incremented twice due to two updates.
    assertEquals(2L, atomicConfig.get().getGeneration());
    assertEquals(2L, atomicConfigController.get().getGeneration());
    // assert the first call happened last and the streams are s2.
    assertTrue(atomicConfig.get().getStartingStreamCuts().keySet().stream().anyMatch(s -> s.getStreamName().equals("s2")));
    assertTrue(atomicConfigController.get().getStartingStreamCuts().keySet().stream().anyMatch(s -> s.getStreamName().equals("s2")));
}
Also used : ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) ReaderGroupStateInitSerializer(io.pravega.client.admin.impl.ReaderGroupManagerImpl.ReaderGroupStateInitSerializer) Arrays(java.util.Arrays) StreamCut(io.pravega.client.stream.StreamCut) AssertExtensions.assertSuppliedFutureThrows(io.pravega.test.common.AssertExtensions.assertSuppliedFutureThrows) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) AssertExtensions(io.pravega.test.common.AssertExtensions) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Cleanup(lombok.Cleanup) ArgumentMatcher(org.mockito.ArgumentMatcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Stream(io.pravega.client.stream.Stream) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Checkpoint(io.pravega.client.stream.Checkpoint) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) UUID(java.util.UUID) InitialUpdate(io.pravega.client.state.InitialUpdate) Collectors(java.util.stream.Collectors) ReaderGroupStateUpdatesSerializer(io.pravega.client.admin.impl.ReaderGroupManagerImpl.ReaderGroupStateUpdatesSerializer) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Controller(io.pravega.client.control.impl.Controller) Futures(io.pravega.common.concurrent.Futures) ClearCheckpointsBefore(io.pravega.client.stream.impl.ReaderGroupState.ClearCheckpointsBefore) Mockito.mock(org.mockito.Mockito.mock) StateSynchronizer(io.pravega.client.state.StateSynchronizer) IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Segment(io.pravega.client.segment.impl.Segment) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) ReaderGroupConfigRejectedException(io.pravega.client.control.impl.ReaderGroupConfigRejectedException) Update(io.pravega.client.state.Update) HashSet(java.util.HashSet) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Before(org.junit.Before) Serializer(io.pravega.client.stream.Serializer) NameUtils(io.pravega.shared.NameUtils) ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Mockito(org.mockito.Mockito) SynchronizerClientFactory(io.pravega.client.SynchronizerClientFactory) ReaderSegmentDistribution(io.pravega.client.stream.ReaderSegmentDistribution) InlineExecutor(io.pravega.test.common.InlineExecutor) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StreamCut(io.pravega.client.stream.StreamCut) AtomicReference(java.util.concurrent.atomic.AtomicReference) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReaderGroupConfigRejectedException(io.pravega.client.control.impl.ReaderGroupConfigRejectedException) Stream(io.pravega.client.stream.Stream) IntStream(java.util.stream.IntStream) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 SynchronizerClientFactory (io.pravega.client.SynchronizerClientFactory)1 ReaderGroupStateInitSerializer (io.pravega.client.admin.impl.ReaderGroupManagerImpl.ReaderGroupStateInitSerializer)1 ReaderGroupStateUpdatesSerializer (io.pravega.client.admin.impl.ReaderGroupManagerImpl.ReaderGroupStateUpdatesSerializer)1 ConnectionPool (io.pravega.client.connection.impl.ConnectionPool)1 Controller (io.pravega.client.control.impl.Controller)1 ReaderGroupConfigRejectedException (io.pravega.client.control.impl.ReaderGroupConfigRejectedException)1 Segment (io.pravega.client.segment.impl.Segment)1 InitialUpdate (io.pravega.client.state.InitialUpdate)1 StateSynchronizer (io.pravega.client.state.StateSynchronizer)1 SynchronizerConfig (io.pravega.client.state.SynchronizerConfig)1 Update (io.pravega.client.state.Update)1 Checkpoint (io.pravega.client.stream.Checkpoint)1 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)1 ReaderSegmentDistribution (io.pravega.client.stream.ReaderSegmentDistribution)1 Serializer (io.pravega.client.stream.Serializer)1 Stream (io.pravega.client.stream.Stream)1 StreamCut (io.pravega.client.stream.StreamCut)1 ClearCheckpointsBefore (io.pravega.client.stream.impl.ReaderGroupState.ClearCheckpointsBefore)1