private Optional<ValueAndBound<T>> readLatestValueFromStore() { Optional<ValueAndBound<T>> storedValueAndBound = store.getAgreedValue(); accumulateCachedValue(storedValueAndBound); return storedValueAndBound; }
public static CoordinationService<InternalSchemaMetadata> wrapHidingVersionSerialization( CoordinationService<VersionedInternalSchemaMetadata> rawCoordinationService) { return new TransformingCoordinationService<>( rawCoordinationService, InternalSchemaMetadataPayloadCodec::decode, InternalSchemaMetadataPayloadCodec::encode); }
static SequenceAndBound of(long sequence, long bound) { return ImmutableSequenceAndBound.of(sequence, bound); } }
@Override public CheckAndSetResult<ValueAndBound<T>> transformAgreedValue(Function<ValueAndBound<T>, T> transform) { Optional<SequenceAndBound> coordinationValue = getCoordinationValue(); ValueAndBound<T> extantValueAndBound = ValueAndBound.of(coordinationValue.flatMap( sequenceAndBound -> getValue(sequenceAndBound.sequence())), coordinationValue.map(SequenceAndBound::bound).orElse(SequenceAndBound.INVALID_BOUND)); T targetValue = transform.apply(extantValueAndBound); SequenceAndBound newSequenceAndBound = determineNewSequenceAndBound(coordinationValue, extantValueAndBound, targetValue); CheckAndSetResult<SequenceAndBound> casResult = checkAndSetCoordinationValue( coordinationValue, newSequenceAndBound); return extractRelevantValues(targetValue, newSequenceAndBound.bound(), casResult); }
@Override public Optional<ValueAndBound<T>> getAgreedValue() { return getCoordinationValue() .map(sequenceAndBound -> ValueAndBound.of( getValue(sequenceAndBound.sequence()), sequenceAndBound.bound())); }
private CheckAndSetResult<ValueAndBound<InternalSchemaMetadata>> tryPerpetuateExistingState() { return coordinationService.tryTransformCurrentValue(valueAndBound -> valueAndBound.value().orElseGet(InternalSchemaMetadata::defaultValue)); }
@Before public void setUp() { when(metadataCoordinationService.getLastKnownLocalValue()) .thenReturn(Optional.of(ValueAndBound.of(INTERNAL_SCHEMA_METADATA, TIMESTAMP_1))); MetadataCoordinationServiceMetrics.registerMetrics(metricsManager, metadataCoordinationService); }
@Override public Optional<ValueAndBound<T2>> getLastKnownLocalValue() { return delegate.getLastKnownLocalValue() .map(preservingBounds(transformFromUnderlying)); }
@Override public Optional<ValueAndBound<T2>> getValueForTimestamp(long timestamp) { return delegate.getValueForTimestamp(timestamp) .map(preservingBounds(transformFromUnderlying)); }
@Override public Optional<ValueAndBound<T>> getValueForTimestamp(long timestamp) { ValueAndBound<T> cachedReference = cache.get(); if (cachedReference.bound() < timestamp) { return readLatestValueFromStore() .filter(valueAndBound -> valueAndBound.bound() >= timestamp); } return Optional.of(cachedReference); }
@Override public CheckAndSetResult<ValueAndBound<T>> tryTransformCurrentValue(Function<ValueAndBound<T>, T> transform) { CheckAndSetResult<ValueAndBound<T>> transformResult = store.transformAgreedValue(transform); ValueAndBound<T> existingValue = Iterables.getOnlyElement(transformResult.existingValues()); accumulateCachedValue(Optional.of(existingValue)); return transformResult; }
private ValueAndBound<T> chooseValueWithGreaterBound( ValueAndBound<T> currentValue, ValueAndBound<T> nextValue) { if (currentValue.bound() > nextValue.bound()) { return currentValue; } log.info("Updating cached coordination value to a new value, valid till {}", SafeArg.of("newBound", nextValue.bound())); return nextValue; }
private static <T> ValueAndBound<T> getInitialCacheValue() { return ValueAndBound.of(Optional.empty(), ValueAndBound.INVALID_BOUND); } }
@Override public long resetStateAndGetFreshTimestamp() { long freshTimestamp = currentDelegate.get().resetStateAndGetFreshTimestamp(); currentDelegate.set(SimpleCoordinationResource.create(txManager)); return freshTimestamp; } }
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") static <T> ValueAndBound<T> of(Optional<T> value, long bound) { return ImmutableValueAndBound.of(value, bound); }
public static CoordinationResource create(TransactionManager txManager) { return new InstanceManagingCoordinationResource( new AtomicReference<>(SimpleCoordinationResource.create(txManager)), txManager); }
private void assertTransactionsSchemaVersionIsNow(int expectedVersion) { assertThat(coordinationResource.getTransactionsSchemaVersion(coordinationResource.getFreshTimestamp())) .isEqualTo(expectedVersion); } }
@Before public void setUp() { lowerBoundOnTimestamps = coordinationResource.resetStateAndGetFreshTimestamp(); }
static <T> ValueAndBound<T> of(T value, long bound) { return ImmutableValueAndBound.of(Optional.of(value), bound); } }