@Override public long getFreshTimestamp() { return getFreshTimestamps(1).getLowerBound(); }
@Override public long getFreshTimestamp() { TimestampRange range = getFreshTimestamps(1); return range.getLowerBound(); }
modulus); long lowerBoundResidue = LongMath.mod(range.getLowerBound(), modulus); long shift = residue < lowerBoundResidue ? modulus + residue - lowerBoundResidue : residue - lowerBoundResidue; long candidate = range.getLowerBound() + shift;
public static void handedOutTimestamps(TimestampRange range) { long count = range.getUpperBound() - range.getLowerBound() + 1L; logger.trace("Handing out {} timestamps, taking us to {}.", SafeArg.of("count", count), SafeArg.of("rangeUpperBound", range.getUpperBound())); }
@Test public void timestampRangesAreReturnedInNonOverlappingOrder() { TimestampRange timestampRange1 = timestampService.getFreshTimestamps(10); TimestampRange timestampRange2 = timestampService.getFreshTimestamps(10); long firstUpperBound = timestampRange1.getUpperBound(); long secondLowerBound = timestampRange2.getLowerBound(); Assertions.assertThat(firstUpperBound).isLessThan(secondLowerBound); }
@VisibleForTesting static Consumer<List<BatchElement<Integer, TimestampRange>>> consumer(TimestampService delegate) { return batch -> { long totalTimestamps = batch.stream().mapToLong(BatchElement::argument).reduce(0, Math::addExact); long startInclusive = 0; long endExclusive = 0; for (BatchElement<Integer, TimestampRange> element : batch) { int timestampsRequired = element.argument(); if (element.argument() <= endExclusive - startInclusive) { element.result().set(createExclusiveRange(startInclusive, startInclusive + timestampsRequired)); startInclusive += timestampsRequired; totalTimestamps -= timestampsRequired; } else { TimestampRange requested = getFreshTimestampsFromDelegate( delegate, Ints.saturatedCast(totalTimestamps)); startInclusive = requested.getLowerBound(); endExclusive = Math.addExact(requested.getUpperBound(), 1); int toTake = Math.min(Ints.checkedCast(endExclusive - startInclusive), timestampsRequired); element.result().set(createExclusiveRange(startInclusive, startInclusive + toTake)); startInclusive += toTake; totalTimestamps -= timestampsRequired; } } }; }
@Test public void canRequestTimestampRangeWithGetFreshTimestamps() { int expectedNumTimestamps = 5; TimestampRange range = timestampService.getFreshTimestamps(expectedNumTimestamps); Assertions.assertThat((int) range.size()) .withFailMessage("Expected %d timestamps, got %d timestamps. (The returned range was: %d-%d)", expectedNumTimestamps, range.size(), range.getLowerBound(), range.getUpperBound()) .isGreaterThanOrEqualTo(1) .isLessThanOrEqualTo(expectedNumTimestamps); }
@Test public void canFastForwardToANewMinimumTimestamp() { long newMinimum = 2 * UPPER_LIMIT; timestamp.increaseTo(newMinimum); assertThat(timestamp.incrementBy(1).getLowerBound(), is(newMinimum + 1L)); verify(upperLimit).increaseToAtLeast(longThat(is(greaterThan(newMinimum)))); }
@Test public void shouldHandOutNonOverLappingTimestampRanges() { TimestampRange first = timestamp.incrementBy(10); TimestampRange second = timestamp.incrementBy(10); assertThat(first.getUpperBound(), is(lessThan(second.getLowerBound()))); }
@Override public long getFreshTimestamp() { TimestampRange range = getFreshTimestamps(1); return range.getLowerBound(); }
@Override public long getFreshTimestamp() { return getFreshTimestamps(1).getLowerBound(); }
modulus); long lowerBoundResidue = LongMath.mod(range.getLowerBound(), modulus); long shift = residue < lowerBoundResidue ? modulus + residue - lowerBoundResidue : residue - lowerBoundResidue; long candidate = range.getLowerBound() + shift;
public static void handedOutTimestamps(TimestampRange range) { long count = range.getUpperBound() - range.getLowerBound() + 1L; logger.trace("Handing out {} timestamps, taking us to {}.", SafeArg.of("count", count), SafeArg.of("rangeUpperBound", range.getUpperBound())); }
@Test public void timestampRangesAreReturnedInNonOverlappingOrder() { TimestampRange timestampRange1 = timestampService.getFreshTimestamps(10); TimestampRange timestampRange2 = timestampService.getFreshTimestamps(10); long firstUpperBound = timestampRange1.getUpperBound(); long secondLowerBound = timestampRange2.getLowerBound(); Assertions.assertThat(firstUpperBound).isLessThan(secondLowerBound); }
@VisibleForTesting static Consumer<List<BatchElement<Integer, TimestampRange>>> consumer(TimestampService delegate) { return batch -> { long totalTimestamps = batch.stream().mapToLong(BatchElement::argument).reduce(0, Math::addExact); long startInclusive = 0; long endExclusive = 0; for (BatchElement<Integer, TimestampRange> element : batch) { int timestampsRequired = element.argument(); if (element.argument() <= endExclusive - startInclusive) { element.result().set(createExclusiveRange(startInclusive, startInclusive + timestampsRequired)); startInclusive += timestampsRequired; totalTimestamps -= timestampsRequired; } else { TimestampRange requested = getFreshTimestampsFromDelegate( delegate, Ints.saturatedCast(totalTimestamps)); startInclusive = requested.getLowerBound(); endExclusive = Math.addExact(requested.getUpperBound(), 1); int toTake = Math.min(Ints.checkedCast(endExclusive - startInclusive), timestampsRequired); element.result().set(createExclusiveRange(startInclusive, startInclusive + toTake)); startInclusive += toTake; totalTimestamps -= timestampsRequired; } } }; }
@Test public void canRequestTimestampRangeWithGetFreshTimestamps() { int expectedNumTimestamps = 5; TimestampRange range = timestampService.getFreshTimestamps(expectedNumTimestamps); Assertions.assertThat((int) range.size()) .withFailMessage("Expected %d timestamps, got %d timestamps. (The returned range was: %d-%d)", expectedNumTimestamps, range.size(), range.getLowerBound(), range.getUpperBound()) .isGreaterThanOrEqualTo(1) .isLessThanOrEqualTo(expectedNumTimestamps); }