public static TimelockService create(TimelockService delegate) { return new TimestampCorroboratingTimelockService(delegate); }
@Override public long getFreshTimestamp() { return checkAndUpdateLowerBound(delegate::getFreshTimestamp, x -> x, x -> x); }
private <T> T checkAndUpdateLowerBound(Supplier<T> timestampContainerSupplier, ToLongFunction<T> lowerBoundExtractor, ToLongFunction<T> upperBoundExtractor) { long threadLocalLowerBound = lowerBound.get(); T timestampContainer = timestampContainerSupplier.get(); checkTimestamp(threadLocalLowerBound, lowerBoundExtractor.applyAsLong(timestampContainer)); updateLowerBound(upperBoundExtractor.applyAsLong(timestampContainer)); return timestampContainer; }
@Before public void setUp() { rawTimelockService = mock(TimelockService.class); timelockService = TimestampCorroboratingTimelockService.create(rawTimelockService); }
private static void checkTimestamp(long timestampLowerBound, long freshTimestamp) { if (freshTimestamp <= timestampLowerBound) { throw clocksWentBackwards(timestampLowerBound, freshTimestamp); } }
private static LockAndTimestampServices withCorroboratingTimestampService( LockAndTimestampServices lockAndTimestampServices) { TimelockService timelockService = TimestampCorroboratingTimelockService .create(lockAndTimestampServices.timelock()); TimestampService corroboratingTimestampService = new TimelockTimestampServiceAdapter(timelockService); return ImmutableLockAndTimestampServices.builder() .from(lockAndTimestampServices) .timelock(timelockService) .timestamp(corroboratingTimestampService) .build(); }
private static void checkTimestamp(long timestampLowerBound, long freshTimestamp) { if (freshTimestamp <= timestampLowerBound) { throw clocksWentBackwards(timestampLowerBound, freshTimestamp); } }
@Override public StartAtlasDbTransactionResponse startAtlasDbTransaction(IdentifiedTimeLockRequest request) { return checkAndUpdateLowerBound(() -> delegate.startAtlasDbTransaction(request), StartAtlasDbTransactionResponse::freshTimestamp, StartAtlasDbTransactionResponse::freshTimestamp); }
private <T> T checkAndUpdateLowerBound(Supplier<T> timestampContainerSupplier, ToLongFunction<T> lowerBoundExtractor, ToLongFunction<T> upperBoundExtractor) { long threadLocalLowerBound = lowerBound.get(); T timestampContainer = timestampContainerSupplier.get(); checkTimestamp(threadLocalLowerBound, lowerBoundExtractor.applyAsLong(timestampContainer)); updateLowerBound(upperBoundExtractor.applyAsLong(timestampContainer)); return timestampContainer; }
private static LockAndTimestampServices withCorroboratingTimestampService( LockAndTimestampServices lockAndTimestampServices) { TimelockService timelockService = TimestampCorroboratingTimelockService .create(lockAndTimestampServices.timelock()); TimestampService corroboratingTimestampService = new TimelockTimestampServiceAdapter(timelockService); return ImmutableLockAndTimestampServices.builder() .from(lockAndTimestampServices) .timelock(timelockService) .timestamp(corroboratingTimestampService) .build(); }
public static TimelockService create(TimelockService delegate) { return new TimestampCorroboratingTimelockService(delegate); }
@Override public TimestampRange getFreshTimestamps(int numTimestampsRequested) { return checkAndUpdateLowerBound(() -> delegate.getFreshTimestamps(numTimestampsRequested), TimestampRange::getLowerBound, TimestampRange::getUpperBound); }
@Override public StartIdentifiedAtlasDbTransactionResponse startIdentifiedAtlasDbTransaction( StartIdentifiedAtlasDbTransactionRequest request) { return checkAndUpdateLowerBound(() -> delegate.startIdentifiedAtlasDbTransaction(request), r -> r.startTimestampAndPartition().timestamp(), r -> r.startTimestampAndPartition().timestamp()); }
@Override public long getFreshTimestamp() { return checkAndUpdateLowerBound(delegate::getFreshTimestamp, x -> x, x -> x); }
@Override public TimestampRange getFreshTimestamps(int numTimestampsRequested) { return checkAndUpdateLowerBound(() -> delegate.getFreshTimestamps(numTimestampsRequested), TimestampRange::getLowerBound, TimestampRange::getUpperBound); }
@Override public StartAtlasDbTransactionResponse startAtlasDbTransaction(IdentifiedTimeLockRequest request) { return checkAndUpdateLowerBound(() -> delegate.startAtlasDbTransaction(request), StartAtlasDbTransactionResponse::freshTimestamp, StartAtlasDbTransactionResponse::freshTimestamp); }
@Override public StartIdentifiedAtlasDbTransactionResponse startIdentifiedAtlasDbTransaction( StartIdentifiedAtlasDbTransactionRequest request) { return checkAndUpdateLowerBound(() -> delegate.startIdentifiedAtlasDbTransaction(request), r -> r.startTimestampAndPartition().timestamp(), r -> r.startTimestampAndPartition().timestamp()); }