@Override public long getFreshTimestamp() { return timelockService.getFreshTimestamp(); }
@Override public TimestampRange getFreshTimestamps(int numTimestampsRequested) { return timelockService.getFreshTimestamps(numTimestampsRequested); } }
@Test public void methodsNotOnTimestampServiceRoutedToDelegateService() { decoratingService.currentTimeMillis(); verify(delegate).currentTimeMillis(); IdentifiedTimeLockRequest immutableTimestampRequest = IdentifiedTimeLockRequest.create(); decoratingService.lockImmutableTimestamp(immutableTimestampRequest); verify(delegate).lockImmutableTimestamp(eq(immutableTimestampRequest)); } }
@Test public void checkImmutableTsLockOnceIfThoroughlySwept_WithValidationOnReads() { TimelockService timelockService = spy(new LegacyTimelockService(timestampService, lockService, lockClient)); long transactionTs = timelockService.getFreshTimestamp(); LockImmutableTimestampResponse res = timelockService.lockImmutableTimestamp(IdentifiedTimeLockRequest.create()); SnapshotTransaction transaction = getSnapshotTransactionWith( timelockService, () -> transactionTs, res, PreCommitConditions.NO_OP, true); transaction.get(TABLE_SWEPT_THOROUGH, ImmutableSet.of(TEST_CELL)); transaction.commit(); timelockService.unlock(ImmutableSet.of(res.getLock())); verify(timelockService).refreshLockLeases(ImmutableSet.of(res.getLock())); }
@Test public void validateLocksOnReadsIfThoroughlySwept() { TimelockService timelockService = new LegacyTimelockService(timestampService, lockService, lockClient); long transactionTs = timelockService.getFreshTimestamp(); LockImmutableTimestampResponse res = timelockService.lockImmutableTimestamp(IdentifiedTimeLockRequest.create()); SnapshotTransaction transaction = getSnapshotTransactionWith( timelockService, () -> transactionTs, res, PreCommitConditions.NO_OP, true); timelockService.unlock(ImmutableSet.of(res.getLock())); assertThatExceptionOfType(TransactionLockTimeoutException.class).isThrownBy(() -> transaction.get(TABLE_SWEPT_THOROUGH, ImmutableSet.of(TEST_CELL))); }
() -> ImmutableTransactionConfig.builder().build()); when(timelock.getFreshTimestamp()).thenReturn(1L); when(timelock.lockImmutableTimestamp(any())).thenReturn( LockImmutableTimestampResponse.of(2L, LockToken.of(UUID.randomUUID()))); when(timelock.startIdentifiedAtlasDbTransaction(any())).thenReturn( StartIdentifiedAtlasDbTransactionResponse.of( LockImmutableTimestampResponse.of(2L, LockToken.of(UUID.randomUUID())),
@Override public Set<LockToken> refresh(Set<LockToken> lockTokenV2s) throws InterruptedException { return timelockService.refreshLockLeases(lockTokenV2s); } }
@Override public LockResponse lock(LockRequest request) { return executeWithRecord(() -> timelockService.lock(request)); }
/** * A version of {@link TimelockService#unlock(Set)} where one does not need to know whether the locks associated * with the provided tokens were successfully unlocked or not. * * In some implementations, this may be more performant than a standard unlock. * * @param tokens Tokens for which associated locks should be unlocked. */ default void tryUnlock(Set<LockToken> tokens) { unlock(tokens); }
private void assertThatTimeAndLockMetricsAreRecorded(String timestampMetric, String lockMetric) { assertThat(metricsManager.getRegistry().timer(timestampMetric).getCount(), is(equalTo(0L))); assertThat(metricsManager.getRegistry().timer(lockMetric).getCount(), is(equalTo(0L))); TransactionManagers.LockAndTimestampServices lockAndTimestamp = getLockAndTimestampServices(); lockAndTimestamp.timelock().getFreshTimestamp(); lockAndTimestamp.timelock().currentTimeMillis(); assertThat(metricsManager.getRegistry().timer(timestampMetric).getCount(), is(equalTo(1L))); assertThat(metricsManager.getRegistry().timer(lockMetric).getCount(), is(equalTo(1L))); }
@Override public StartIdentifiedAtlasDbTransactionResponse startIdentifiedAtlasDbTransaction( StartIdentifiedAtlasDbTransactionRequest request) { return executeWithRecord(() -> timelockService.startIdentifiedAtlasDbTransaction(request)); }
@Override public boolean isInitialized() { return timelockService.isInitialized(); }
@Override public StartAtlasDbTransactionResponse startAtlasDbTransaction(IdentifiedTimeLockRequest request) { return checkAndUpdateLowerBound(() -> delegate.startAtlasDbTransaction(request), StartAtlasDbTransactionResponse::freshTimestamp, StartAtlasDbTransactionResponse::freshTimestamp); }
@Override public TransactionAndImmutableTsLock setupRunTaskWithConditionThrowOnConflict(PreCommitCondition condition) { StartIdentifiedAtlasDbTransactionResponse transactionResponse = timelockService.startIdentifiedAtlasDbTransaction( StartIdentifiedAtlasDbTransactionRequest.createForRequestor(clientId)); try { LockToken immutableTsLock = transactionResponse.immutableTimestamp().getLock(); long immutableTs = transactionResponse.immutableTimestamp().getImmutableTimestamp(); recordImmutableTimestamp(immutableTs); cleaner.punch(transactionResponse.startTimestampAndPartition().timestamp()); Supplier<Long> startTimestampSupplier = Suppliers.ofInstance( transactionResponse.startTimestampAndPartition().timestamp()); SnapshotTransaction transaction = createTransaction(immutableTs, startTimestampSupplier, immutableTsLock, condition); return TransactionAndImmutableTsLock.of(transaction, immutableTsLock); } catch (Throwable e) { timelockService.tryUnlock(ImmutableSet.of(transactionResponse.immutableTimestamp().getLock())); throw Throwables.rewrapAndThrowUncheckedException(e); } }
@Override public long currentTimeMillis() { return executeWithRecord(() -> timelockService.currentTimeMillis()); }
@Override public LockImmutableTimestampResponse lockImmutableTimestamp( IdentifiedTimeLockRequest request) { return executeWithRecord(() -> timelockService.lockImmutableTimestamp(request)); }
@Test public void checkImmutableTsLockOnceIfThoroughlySwept_WithoutValidationOnReads() { TimelockService timelockService = spy(new LegacyTimelockService(timestampService, lockService, lockClient)); long transactionTs = timelockService.getFreshTimestamp(); LockImmutableTimestampResponse res = timelockService.lockImmutableTimestamp(IdentifiedTimeLockRequest.create()); SnapshotTransaction transaction = getSnapshotTransactionWith( timelockService, () -> transactionTs, res, PreCommitConditions.NO_OP, false); transaction.get(TABLE_SWEPT_THOROUGH, ImmutableSet.of(TEST_CELL)); transaction.commit(); timelockService.unlock(ImmutableSet.of(res.getLock())); verify(timelockService).refreshLockLeases(ImmutableSet.of(res.getLock())); }
@Test public void validateLocksOnlyOnCommitIfValidationFlagIsFalse() { TimelockService timelockService = new LegacyTimelockService(timestampService, lockService, lockClient); long transactionTs = timelockService.getFreshTimestamp(); LockImmutableTimestampResponse res = timelockService.lockImmutableTimestamp(IdentifiedTimeLockRequest.create()); SnapshotTransaction transaction = getSnapshotTransactionWith( timelockService, () -> transactionTs, res, PreCommitConditions.NO_OP, false); timelockService.unlock(ImmutableSet.of(res.getLock())); transaction.get(TABLE_SWEPT_THOROUGH, ImmutableSet.of(TEST_CELL)); assertThatExceptionOfType(TransactionLockTimeoutException.class).isThrownBy(() -> transaction.commit()); }
private boolean immutableTsLockIsValid() { return !timelockService.refreshLockLeases(ImmutableSet.of(immutableTsLock)).isEmpty(); } }