@Override public Optional<ValueAndBound<T2>> getValueForTimestamp(long timestamp) { return delegate.getValueForTimestamp(timestamp) .map(preservingBounds(transformFromUnderlying)); }
@Test public void getsEmptyFromDelegateIfEmpty() { when(delegate.getValueForTimestamp(anyLong())).thenReturn(Optional.empty()); assertThat(coordinationService.getValueForTimestamp(BOUND)).isEmpty(); verify(delegate).getValueForTimestamp(BOUND); }
public Integer getTransactionsSchemaVersion(long timestamp) { if (timestamp < AtlasDbConstants.STARTING_TS) { throw new SafeIllegalStateException("Query attempted for timestamp {} which was never given out by the" + " timestamp service, as timestamps start at {}", SafeArg.of("queriedTimestamp", timestamp), SafeArg.of("startOfTime", AtlasDbConstants.STARTING_TS)); } Optional<Integer> possibleVersion = extractTimestampVersion(coordinationService.getValueForTimestamp(timestamp), timestamp); return possibleVersion.orElse(null); }
@Test public void getsValueFromDelegateIfPresent() { when(delegate.getValueForTimestamp(anyLong())).thenReturn(Optional.of(ValueAndBound.of(INTEGER_1, BOUND))); assertThat(coordinationService.getValueForTimestamp(BOUND)).contains(ValueAndBound.of(STRING_1, BOUND)); verify(delegate).getValueForTimestamp(BOUND); verify(intToStringTransform).apply(INTEGER_1); }
@Test public void canLookUpNewValueIfCacheOutOfDate() { when(coordinationStore.getAgreedValue()).thenReturn(Optional.of(STRING_AND_ONE_HUNDRED)); assertThat(stringCoordinationService.getValueForTimestamp(42)).contains(STRING_AND_ONE_HUNDRED); assertThat(stringCoordinationService.getValueForTimestamp(742)).isEmpty(); when(coordinationStore.getAgreedValue()).thenReturn(Optional.of(OTHER_STRING_AND_ONE_THOUSAND)); assertThat(stringCoordinationService.getValueForTimestamp(742)).contains(OTHER_STRING_AND_ONE_THOUSAND); }
@Test public void canCacheValues() { when(coordinationStore.getAgreedValue()).thenReturn(Optional.of(STRING_AND_ONE_HUNDRED)); assertThat(stringCoordinationService.getValueForTimestamp(42)).contains(STRING_AND_ONE_HUNDRED); assertThat(stringCoordinationService.getValueForTimestamp(42)).contains(STRING_AND_ONE_HUNDRED); assertThat(stringCoordinationService.getValueForTimestamp(42)).contains(STRING_AND_ONE_HUNDRED); verify(coordinationStore, times(1)).getAgreedValue(); }
@Test public void getValueForWhichNoRecentValueExistsReturnsEmpty() { when(coordinationStore.getAgreedValue()).thenReturn(Optional.of(STRING_AND_ONE_HUNDRED)); assertThat(stringCoordinationService.getValueForTimestamp(142)).isEmpty(); }
@Test public void getValueWithNoValuesStoredReturnsEmpty() { when(coordinationStore.getAgreedValue()).thenReturn(Optional.empty()); assertThat(stringCoordinationService.getValueForTimestamp(42)).isEmpty(); }
@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(); }
@Test public void canRetrieveValues() { when(coordinationStore.getAgreedValue()).thenReturn(Optional.of(STRING_AND_ONE_HUNDRED)); assertThat(stringCoordinationService.getValueForTimestamp(42)).contains(STRING_AND_ONE_HUNDRED); }
/** * Returns the version of the transactions schema associated with the provided timestamp. * * This method may perpetuate the existing state one or more times to achieve consensus. It will repeatedly * attempt to perpetuate the existing state until a consensus for the provided timestamp argument is achieved. * * This method should only be called with timestamps that have already been given out by the timestamp service; * otherwise, achieving a consensus may take a long time. */ public int getTransactionsSchemaVersion(long timestamp) { if (timestamp < AtlasDbConstants.STARTING_TS) { throw new SafeIllegalStateException("Query attempted for timestamp {} which was never given out by the" + " timestamp service, as timestamps start at {}", SafeArg.of("queriedTimestamp", timestamp), SafeArg.of("startOfTime", AtlasDbConstants.STARTING_TS)); } Optional<Integer> possibleVersion = extractTimestampVersion(coordinationService.getValueForTimestamp(timestamp), timestamp); while (!possibleVersion.isPresent()) { CheckAndSetResult<ValueAndBound<InternalSchemaMetadata>> casResult = tryPerpetuateExistingState(); possibleVersion = extractTimestampVersion(casResult.existingValues() .stream() .filter(valueAndBound -> valueAndBound.bound() >= timestamp) .findAny(), timestamp); } return possibleVersion.get(); }
@Test public void retrieveLocalValueRetrievesCachedValue() { when(coordinationStore.getAgreedValue()).thenReturn(Optional.of(STRING_AND_ONE_HUNDRED)); assertThat(stringCoordinationService.getValueForTimestamp(42)).contains(STRING_AND_ONE_HUNDRED); when(coordinationStore.getAgreedValue()).thenReturn(Optional.of(OTHER_STRING_AND_ONE_THOUSAND)); assertThat(stringCoordinationService.getLastKnownLocalValue()).contains(STRING_AND_ONE_HUNDRED); verify(coordinationStore, times(1)).getAgreedValue(); }
@Override public Optional<ValueAndBound<T2>> getValueForTimestamp(long timestamp) { return delegate.getValueForTimestamp(timestamp) .map(preservingBounds(transformFromUnderlying)); }
public Integer getTransactionsSchemaVersion(long timestamp) { if (timestamp < AtlasDbConstants.STARTING_TS) { throw new SafeIllegalStateException("Query attempted for timestamp {} which was never given out by the" + " timestamp service, as timestamps start at {}", SafeArg.of("queriedTimestamp", timestamp), SafeArg.of("startOfTime", AtlasDbConstants.STARTING_TS)); } Optional<Integer> possibleVersion = extractTimestampVersion(coordinationService.getValueForTimestamp(timestamp), timestamp); return possibleVersion.orElse(null); }
/** * Returns the version of the transactions schema associated with the provided timestamp. * * This method may perpetuate the existing state one or more times to achieve consensus. It will repeatedly * attempt to perpetuate the existing state until a consensus for the provided timestamp argument is achieved. * * This method should only be called with timestamps that have already been given out by the timestamp service; * otherwise, achieving a consensus may take a long time. */ public int getTransactionsSchemaVersion(long timestamp) { if (timestamp < AtlasDbConstants.STARTING_TS) { throw new SafeIllegalStateException("Query attempted for timestamp {} which was never given out by the" + " timestamp service, as timestamps start at {}", SafeArg.of("queriedTimestamp", timestamp), SafeArg.of("startOfTime", AtlasDbConstants.STARTING_TS)); } Optional<Integer> possibleVersion = extractTimestampVersion(coordinationService.getValueForTimestamp(timestamp), timestamp); while (!possibleVersion.isPresent()) { CheckAndSetResult<ValueAndBound<InternalSchemaMetadata>> casResult = tryPerpetuateExistingState(); possibleVersion = extractTimestampVersion(casResult.existingValues() .stream() .filter(valueAndBound -> valueAndBound.bound() >= timestamp) .findAny(), timestamp); } return possibleVersion.get(); }