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);
}
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;
}
}
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);
}
Aggregations