Search in sources :

Example 1 with StateSynchronizer

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

the class SynchronizerTest method testReturnValue.

@Test(timeout = 20000)
public void testReturnValue() {
    String streamName = "streamName";
    String scope = "scope";
    MockSegmentStreamFactory ioFactory = new MockSegmentStreamFactory();
    @Cleanup MockClientFactory clientFactory = new MockClientFactory(scope, ioFactory);
    createScopeAndStream(streamName, scope, clientFactory.getController());
    StateSynchronizer<RevisionedImpl> sync = clientFactory.createStateSynchronizer(streamName, new JavaSerializer<>(), new JavaSerializer<>(), SynchronizerConfig.builder().build());
    StateSynchronizer<RevisionedImpl> syncA = clientFactory.createStateSynchronizer(streamName, new JavaSerializer<>(), new JavaSerializer<>(), SynchronizerConfig.builder().build());
    StateSynchronizer<RevisionedImpl> syncB = clientFactory.createStateSynchronizer(streamName, new JavaSerializer<>(), new JavaSerializer<>(), SynchronizerConfig.builder().build());
    syncA.initialize(new RegularUpdate("Foo"));
    AtomicInteger callCount = new AtomicInteger(0);
    String previous = syncB.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("Bar"));
        return state.getValue();
    });
    assertEquals(previous, "Foo");
    assertEquals(1, callCount.get());
    assertEquals("Foo", syncA.getState().value);
    previous = syncA.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("Baz"));
        return state.getValue();
    });
    assertEquals(previous, "Bar");
    assertEquals(3, callCount.get());
    assertEquals("Baz", syncA.getState().value);
    syncB.fetchUpdates();
    assertEquals("Baz", syncA.getState().value);
    previous = syncB.updateState((state, updates) -> {
        callCount.incrementAndGet();
        updates.add(new RegularUpdate("Bat"));
        return state.getValue();
    });
    assertEquals(previous, "Baz");
    assertEquals(4, callCount.get());
    assertEquals("Baz", syncA.getState().value);
    syncA.fetchUpdates();
    assertEquals("Bat", syncA.getState().value);
}
Also used : AssertExtensions(io.pravega.test.common.AssertExtensions) Cleanup(lombok.Cleanup) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) StreamSegments(io.pravega.client.stream.impl.StreamSegments) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) URI(java.net.URI) InitialUpdate(io.pravega.client.state.InitialUpdate) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Serializable(java.io.Serializable) CountDownLatch(java.util.concurrent.CountDownLatch) SetSynchronizer(io.pravega.client.state.examples.SetSynchronizer) Assert.assertFalse(org.junit.Assert.assertFalse) Entry(java.util.Map.Entry) SegmentAttribute(io.pravega.client.segment.impl.SegmentAttribute) Controller(io.pravega.client.control.impl.Controller) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) Mockito.mock(org.mockito.Mockito.mock) StateSynchronizer(io.pravega.client.state.StateSynchronizer) NotImplementedException(org.apache.commons.lang3.NotImplementedException) Segment(io.pravega.client.segment.impl.Segment) TruncatedDataException(io.pravega.client.stream.TruncatedDataException) CompletableFuture(java.util.concurrent.CompletableFuture) ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) ByteArraySerializer(io.pravega.client.stream.impl.ByteArraySerializer) Update(io.pravega.client.state.Update) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RevisionedStreamClient(io.pravega.client.state.RevisionedStreamClient) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) ReusableLatch(io.pravega.common.util.ReusableLatch) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Revisioned(io.pravega.client.state.Revisioned) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractMap(java.util.AbstractMap) Assert.assertNull(org.junit.Assert.assertNull) TreeMap(java.util.TreeMap) SynchronizerClientFactory(io.pravega.client.SynchronizerClientFactory) Data(lombok.Data) Revision(io.pravega.client.state.Revision) InvalidStreamException(io.pravega.client.stream.InvalidStreamException) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) Collections(java.util.Collections) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) Assert.assertEquals(org.junit.Assert.assertEquals) ClientConfig(io.pravega.client.ClientConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) Test(org.junit.Test)

Example 2 with StateSynchronizer

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

the class ReaderGroupManagerImpl method createReaderGroup.

@Override
public boolean createReaderGroup(String groupName, ReaderGroupConfig config) throws ConfigMismatchException {
    log.info("Creating reader group: {} for streams: {} with configuration: {}", groupName, Arrays.toString(config.getStartingStreamCuts().keySet().toArray()), config);
    NameUtils.validateReaderGroupName(groupName);
    if (config.getReaderGroupId() == ReaderGroupConfig.DEFAULT_UUID) {
        // make sure we never attempt to create a ReaderGroup with default ReadrGroupId which is 0-0-0
        config = ReaderGroupConfig.cloneConfig(config, UUID.randomUUID(), 0L);
    }
    ReaderGroupConfig controllerConfig = getThrowingException(controller.createReaderGroup(scope, groupName, config));
    if (!controllerConfig.equals(config)) {
        log.warn("ReaderGroup {} already exists with pre-existing configuration {}", groupName, controllerConfig);
        throw new ConfigMismatchException(groupName, controllerConfig);
    } else if (controllerConfig.getGeneration() > 0) {
        log.info("ReaderGroup {} already exists", groupName);
        return false;
    } else {
        @Cleanup StateSynchronizer<ReaderGroupState> synchronizer = clientFactory.createStateSynchronizer(NameUtils.getStreamForReaderGroup(groupName), new ReaderGroupStateUpdatesSerializer(), new ReaderGroupStateInitSerializer(), SynchronizerConfig.builder().build());
        Map<SegmentWithRange, Long> segments = ReaderGroupImpl.getSegmentsForStreams(controller, controllerConfig);
        synchronizer.initialize(new ReaderGroupState.ReaderGroupStateInit(controllerConfig, segments, getEndSegmentsForStreams(controllerConfig), false));
        return true;
    }
}
Also used : ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) StateSynchronizer(io.pravega.client.state.StateSynchronizer) ConfigMismatchException(io.pravega.client.stream.ConfigMismatchException) Map(java.util.Map)

Example 3 with StateSynchronizer

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

the class ReaderGroupManagerImpl method deleteReaderGroup.

@Override
public void deleteReaderGroup(String groupName) {
    UUID readerGroupId = null;
    ReaderGroupConfig syncConfig = null;
    try {
        @Cleanup StateSynchronizer<ReaderGroupState> synchronizer = clientFactory.createStateSynchronizer(NameUtils.getStreamForReaderGroup(groupName), new ReaderGroupStateUpdatesSerializer(), new ReaderGroupStateInitSerializer(), SynchronizerConfig.builder().build());
        synchronizer.fetchUpdates();
        syncConfig = synchronizer.getState().getConfig();
        readerGroupId = syncConfig.getReaderGroupId();
        if (ReaderGroupConfig.DEFAULT_UUID.equals(syncConfig.getReaderGroupId()) && ReaderGroupConfig.DEFAULT_GENERATION == syncConfig.getGeneration()) {
            // migrate RG case
            try {
                controller.getReaderGroupConfig(scope, groupName).thenCompose(conf -> controller.deleteReaderGroup(scope, groupName, conf.getReaderGroupId())).join();
            } catch (ReaderGroupNotFoundException ex) {
                controller.sealStream(scope, getStreamForReaderGroup(groupName)).thenCompose(b -> controller.deleteStream(scope, getStreamForReaderGroup(groupName))).exceptionally(e -> {
                    log.warn("Failed to delete ReaderGroup Stream {}", getStreamForReaderGroup(groupName), e);
                    throw Exceptions.sneakyThrow(e);
                }).join();
            }
            return;
        }
    } catch (InvalidStreamException ex) {
        log.warn("State-Synchronizer Stream for ReaderGroup {} was not found.", NameUtils.getScopedReaderGroupName(scope, groupName));
        // if the State Synchronizer Stream was deleted, but the RG still exists, get Id from Controller
        readerGroupId = getAndHandleExceptions(controller.getReaderGroupConfig(scope, groupName), RuntimeException::new).getReaderGroupId();
    }
    // normal delete
    getAndHandleExceptions(controller.deleteReaderGroup(scope, groupName, readerGroupId), RuntimeException::new);
}
Also used : ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) StateSynchronizer(io.pravega.client.state.StateSynchronizer) Arrays(java.util.Arrays) ReaderGroupImpl(io.pravega.client.stream.impl.ReaderGroupImpl) Getter(lombok.Getter) SneakyThrows(lombok.SneakyThrows) ConnectionFactory(io.pravega.client.connection.impl.ConnectionFactory) Exceptions(io.pravega.common.Exceptions) Cleanup(lombok.Cleanup) ReaderGroup(io.pravega.client.stream.ReaderGroup) ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) ByteBuffer(java.nio.ByteBuffer) NameUtils.getStreamForReaderGroup(io.pravega.shared.NameUtils.getStreamForReaderGroup) Update(io.pravega.client.state.Update) ReaderGroupNotFoundException(io.pravega.client.stream.ReaderGroupNotFoundException) AccessLevel(lombok.AccessLevel) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) Map(java.util.Map) Futures.getThrowingException(io.pravega.common.concurrent.Futures.getThrowingException) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) Serializer(io.pravega.client.stream.Serializer) NameUtils(io.pravega.shared.NameUtils) Futures.getAndHandleExceptions(io.pravega.common.concurrent.Futures.getAndHandleExceptions) IOException(java.io.IOException) UUID(java.util.UUID) InitialUpdate(io.pravega.client.state.InitialUpdate) SegmentWithRange(io.pravega.client.stream.impl.SegmentWithRange) AbstractClientFactoryImpl(io.pravega.client.stream.impl.AbstractClientFactoryImpl) ControllerImplConfig(io.pravega.client.control.impl.ControllerImplConfig) Slf4j(lombok.extern.slf4j.Slf4j) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ConfigMismatchException(io.pravega.client.stream.ConfigMismatchException) ReaderGroupImpl.getEndSegmentsForStreams(io.pravega.client.stream.impl.ReaderGroupImpl.getEndSegmentsForStreams) ControllerImpl(io.pravega.client.control.impl.ControllerImpl) InvalidStreamException(io.pravega.client.stream.InvalidStreamException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Controller(io.pravega.client.control.impl.Controller) ClientConfig(io.pravega.client.ClientConfig) ReaderGroupState(io.pravega.client.stream.impl.ReaderGroupState) ReaderGroupNotFoundException(io.pravega.client.stream.ReaderGroupNotFoundException) UUID(java.util.UUID) ReaderGroupState(io.pravega.client.stream.impl.ReaderGroupState) Cleanup(lombok.Cleanup) InvalidStreamException(io.pravega.client.stream.InvalidStreamException)

Aggregations

StateSynchronizer (io.pravega.client.state.StateSynchronizer)3 ClientConfig (io.pravega.client.ClientConfig)2 ConnectionPoolImpl (io.pravega.client.connection.impl.ConnectionPoolImpl)2 Controller (io.pravega.client.control.impl.Controller)2 InitialUpdate (io.pravega.client.state.InitialUpdate)2 SynchronizerConfig (io.pravega.client.state.SynchronizerConfig)2 Update (io.pravega.client.state.Update)2 ConfigMismatchException (io.pravega.client.stream.ConfigMismatchException)2 InvalidStreamException (io.pravega.client.stream.InvalidStreamException)2 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)2 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)2 Map (java.util.Map)2 Cleanup (lombok.Cleanup)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 SynchronizerClientFactory (io.pravega.client.SynchronizerClientFactory)1 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)1 ConnectionFactory (io.pravega.client.connection.impl.ConnectionFactory)1 ControllerImpl (io.pravega.client.control.impl.ControllerImpl)1 ControllerImplConfig (io.pravega.client.control.impl.ControllerImplConfig)1 Segment (io.pravega.client.segment.impl.Segment)1