@Test public void batchedTimestampCallMakesRequestsAgainIfTimestampRangeDoesNotIncludeCorrectModuli() { when(allocator.getRelevantModuli(UUID_TWO)).thenReturn(RESIDUE_TWO); when(timestamps.getFreshTimestamps(anyInt())) .thenReturn(TIMESTAMP_SEVEN) .thenReturn(TIMESTAMP_SEVEN) .thenReturn(TIMESTAMP_RANGE); assertThat(service.getFreshTimestampsForClient(UUID_TWO, 1).stream()) .containsExactly(RESIDUE_TWO_TIMESTAMP_IN_RANGE.timestamp()); verify(allocator, times(3)).getRelevantModuli(UUID_TWO); verify(timestamps, times(3)).getFreshTimestamps(anyInt()); }
@Override public PartitionedTimestamps getFreshTimestampsForClient(UUID clientIdentifier, int numTransactionsRequested) { while (true) { TimestampRange timestampRange = delegate.getFreshTimestamps(NUM_PARTITIONS * numTransactionsRequested); int targetResidue = allocator.getRelevantModuli(clientIdentifier).iterator().next(); PartitionedTimestamps partitionedTimestamps = TimestampRanges.getPartitionedTimestamps( timestampRange, targetResidue, NUM_PARTITIONS); if (partitionedTimestamps.count() > 0) { return partitionedTimestamps; } log.info("The timestamp range we received from the underlying timestamp service - {} -" + " did not contain a value with residue {} modulo {}. We will try again." + " This is not a bug; but if it happens excessively frequently, please contact support.", SafeArg.of("timestampRange", timestampRange), SafeArg.of("targetResidue", targetResidue), SafeArg.of("modulus", NUM_PARTITIONS)); } }
DelegatingClientAwareManagedTimestampService implements AutoDelegate_ManagedTimestampService, ClientAwareManagedTimestampService { @Override public PartitionedTimestamps getFreshTimestampsForClient(UUID clientIdentifier, int numTransactionsRequested) { while (true) { TimestampRange timestampRange = delegate.getFreshTimestamps(NUM_PARTITIONS * numTransactionsRequested); int targetResidue = allocator.getRelevantModuli(clientIdentifier).iterator().next(); PartitionedTimestamps partitionedTimestamps = TimestampRanges.getPartitionedTimestamps( timestampRange, targetResidue, NUM_PARTITIONS); if (partitionedTimestamps.count() > 0) { return partitionedTimestamps; } log.info("The timestamp range we received from the underlying timestamp service - {} -" + " did not contain a value with residue {} modulo {}. We will try again." + " This is not a bug; but if it happens excessively frequently, please contact support.", SafeArg.of("timestampRange", timestampRange), SafeArg.of("targetResidue", targetResidue), SafeArg.of("modulus", NUM_PARTITIONS)); } } }
DelegatingClientAwareManagedTimestampService implements AutoDelegate_ManagedTimestampService, ClientAwareManagedTimestampService { @Override public PartitionedTimestamps getFreshTimestampsForClient(UUID clientIdentifier, int numTransactionsRequested) { while (true) { TimestampRange timestampRange = delegate.getFreshTimestamps(NUM_PARTITIONS * numTransactionsRequested); int targetResidue = allocator.getRelevantModuli(clientIdentifier).iterator().next(); PartitionedTimestamps partitionedTimestamps = TimestampRanges.getPartitionedTimestamps( timestampRange, targetResidue, NUM_PARTITIONS); if (partitionedTimestamps.count() > 0) { return partitionedTimestamps; } log.info("The timestamp range we received from the underlying timestamp service - {} -" + " did not contain a value with residue {} modulo {}. We will try again." + " This is not a bug; but if it happens excessively frequently, please contact support.", SafeArg.of("timestampRange", timestampRange), SafeArg.of("targetResidue", targetResidue), SafeArg.of("modulus", NUM_PARTITIONS)); } } @VisibleForTesting  DelegatingClientAwareManagedTimestampService(
            NumericPartitionAllocator<UUID> allocator,
            ManagedTimestampService delegate); }
DelegatingClientAwareManagedTimestampService implements AutoDelegate_ManagedTimestampService, ClientAwareManagedTimestampService { @Override public PartitionedTimestamps getFreshTimestampsForClient(UUID clientIdentifier, int numTransactionsRequested) { while (true) { TimestampRange timestampRange = delegate.getFreshTimestamps(NUM_PARTITIONS * numTransactionsRequested); int targetResidue = allocator.getRelevantModuli(clientIdentifier).iterator().next(); PartitionedTimestamps partitionedTimestamps = TimestampRanges.getPartitionedTimestamps( timestampRange, targetResidue, NUM_PARTITIONS); if (partitionedTimestamps.count() > 0) { return partitionedTimestamps; } log.info("The timestamp range we received from the underlying timestamp service - {} -" + " did not contain a value with residue {} modulo {}. We will try again." + " This is not a bug; but if it happens excessively frequently, please contact support.", SafeArg.of("timestampRange", timestampRange), SafeArg.of("targetResidue", targetResidue), SafeArg.of("modulus", NUM_PARTITIONS)); } } @VisibleForTesting  DelegatingClientAwareManagedTimestampService(
            NumericPartitionAllocator<UUID> allocator,
            ManagedTimestampService delegate); static ClientAwareManagedTimestampService createDefault(ManagedTimestampService delegate); @Override PartitionedTimestamps getFreshTimestampsForClient(UUID clientIdentifier, int numTransactionsRequested); @Override ManagedTimestampService delegate(); }
DelegatingClientAwareManagedTimestampService implements AutoDelegate_ManagedTimestampService, ClientAwareManagedTimestampService { @Override public PartitionedTimestamps getFreshTimestampsForClient(UUID clientIdentifier, int numTransactionsRequested) { while (true) { TimestampRange timestampRange = delegate.getFreshTimestamps(NUM_PARTITIONS * numTransactionsRequested); int targetResidue = allocator.getRelevantModuli(clientIdentifier).iterator().next(); PartitionedTimestamps partitionedTimestamps = TimestampRanges.getPartitionedTimestamps( timestampRange, targetResidue, NUM_PARTITIONS); if (partitionedTimestamps.count() > 0) { return partitionedTimestamps; } log.info("The timestamp range we received from the underlying timestamp service - {} -" + " did not contain a value with residue {} modulo {}. We will try again." + " This is not a bug; but if it happens excessively frequently, please contact support.", SafeArg.of("timestampRange", timestampRange), SafeArg.of("targetResidue", targetResidue), SafeArg.of("modulus", NUM_PARTITIONS)); } } @VisibleForTesting  DelegatingClientAwareManagedTimestampService(
            NumericPartitionAllocator<UUID> allocator,
            ManagedTimestampService delegate); static ClientAwareManagedTimestampService createDefault(ManagedTimestampService delegate); @Override PartitionedTimestamps getFreshTimestampsForClient(UUID clientIdentifier, int numTransactionsRequested); @Override ManagedTimestampService delegate();  }