private CheckAndSetResult<ValueAndBound<InternalSchemaMetadata>> tryPerpetuateExistingState() { return coordinationService.tryTransformCurrentValue(valueAndBound -> valueAndBound.value().orElseGet(InternalSchemaMetadata::defaultValue)); }
@Override public CheckAndSetResult<ValueAndBound<T2>> tryTransformCurrentValue(Function<ValueAndBound<T2>, T2> valueUpdater) { CheckAndSetResult<ValueAndBound<T1>> delegateResult = delegate.tryTransformCurrentValue( preservingBounds(transformFromUnderlying).andThen(valueUpdater).andThen(transformToUnderlying)); return CheckAndSetResult.of( delegateResult.successful(), delegateResult.existingValues() .stream() .map(preservingBounds(transformFromUnderlying)) .collect(Collectors.toList())); }
/** * Attempts to install a new transactions table schema version, by submitting a relevant transform. * * The execution of this method does not guarantee that the provided version will eventually be installed. * This method returns true if and only if in the map agreed by the coordination service evaluated at the validity * bound, the transactions schema version is equal to newVersion. */ public boolean tryInstallNewTransactionsSchemaVersion(int newVersion) { List<Integer> presentVersionPeakValidity = coordinationService.tryTransformCurrentValue( valueAndBound -> installNewVersionInMapOrDefault(newVersion, valueAndBound)) .existingValues() .stream() .map(valueAndBound -> valueAndBound.value() .orElseThrow(() -> new SafeIllegalStateException("Unexpectedly found no value in store")) .timestampToTransactionsTableSchemaVersion() .getValueForTimestamp(valueAndBound.bound())) .collect(Collectors.toList()); return Iterables.getOnlyElement(presentVersionPeakValidity) == newVersion; }
@Test public void checkAndSetFailureCase() { when(delegate.tryTransformCurrentValue(any())).thenReturn( CheckAndSetResult.of(false, ImmutableList.of(ValueAndBound.of(INTEGER_1, BOUND)))); CheckAndSetResult<ValueAndBound<String>> casResult = coordinationService.tryTransformCurrentValue(DUMMY_TRANSFORMATION); assertThat(casResult.successful()).isFalse(); assertThat(casResult.existingValues()).containsExactly(ValueAndBound.of(STRING_1, BOUND)); verify(delegate).tryTransformCurrentValue(any()); }
@Test public void checkAndSetSuccessfulCase() { when(delegate.tryTransformCurrentValue(any())).thenReturn( CheckAndSetResult.of(true, ImmutableList.of(ValueAndBound.of(INTEGER_1, BOUND)))); CheckAndSetResult<ValueAndBound<String>> casResult = coordinationService.tryTransformCurrentValue(DUMMY_TRANSFORMATION); assertThat(casResult.successful()).isTrue(); assertThat(casResult.existingValues()).containsExactly(ValueAndBound.of(STRING_1, BOUND)); verify(delegate).tryTransformCurrentValue(any()); }
@Test public void delegatesTransformationToStore() { when(coordinationStore.transformAgreedValue(any())).thenReturn( CheckAndSetResult.of(true, ImmutableList.of(STRING_AND_ONE_HUNDRED))); CheckAndSetResult<ValueAndBound<String>> casResult = stringCoordinationService.tryTransformCurrentValue(unused -> STRING); assertThat(casResult.successful()).isTrue(); assertThat(Iterables.getOnlyElement(casResult.existingValues())) .isEqualTo(STRING_AND_ONE_HUNDRED); }
@Test public void failedUpdateUpdatesCache() { when(coordinationStore.transformAgreedValue(any())).thenReturn( CheckAndSetResult.of(false, ImmutableList.of(STRING_AND_ONE_HUNDRED))); stringCoordinationService.tryTransformCurrentValue(unused -> STRING); stringCoordinationService.getValueForTimestamp(56); stringCoordinationService.getValueForTimestamp(88); verify(coordinationStore, never()).getAgreedValue(); }
@Test public void successfulUpdateUpdatesCache() { when(coordinationStore.transformAgreedValue(any())).thenReturn( CheckAndSetResult.of(true, ImmutableList.of(STRING_AND_ONE_HUNDRED))); stringCoordinationService.tryTransformCurrentValue(unused -> STRING); stringCoordinationService.getValueForTimestamp(56); stringCoordinationService.getValueForTimestamp(88); verify(coordinationStore, never()).getAgreedValue(); }
private CheckAndSetResult<ValueAndBound<InternalSchemaMetadata>> tryPerpetuateExistingState() { return coordinationService.tryTransformCurrentValue(valueAndBound -> valueAndBound.value().orElseGet(InternalSchemaMetadata::defaultValue)); }
@Override public CheckAndSetResult<ValueAndBound<T2>> tryTransformCurrentValue(Function<ValueAndBound<T2>, T2> valueUpdater) { CheckAndSetResult<ValueAndBound<T1>> delegateResult = delegate.tryTransformCurrentValue( preservingBounds(transformFromUnderlying).andThen(valueUpdater).andThen(transformToUnderlying)); return CheckAndSetResult.of( delegateResult.successful(), delegateResult.existingValues() .stream() .map(preservingBounds(transformFromUnderlying)) .collect(Collectors.toList())); }
/** * Attempts to install a new transactions table schema version, by submitting a relevant transform. * * The execution of this method does not guarantee that the provided version will eventually be installed. * This method returns true if and only if in the map agreed by the coordination service evaluated at the validity * bound, the transactions schema version is equal to newVersion. */ public boolean tryInstallNewTransactionsSchemaVersion(int newVersion) { List<Integer> presentVersionPeakValidity = coordinationService.tryTransformCurrentValue( valueAndBound -> installNewVersionInMapOrDefault(newVersion, valueAndBound)) .existingValues() .stream() .map(valueAndBound -> valueAndBound.value() .orElseThrow(() -> new SafeIllegalStateException("Unexpectedly found no value in store")) .timestampToTransactionsTableSchemaVersion() .getValueForTimestamp(valueAndBound.bound())) .collect(Collectors.toList()); return Iterables.getOnlyElement(presentVersionPeakValidity) == newVersion; }