use of com.palantir.lock.v2.LockToken in project atlasdb by palantir.
the class AsyncLockClient method lock.
@Override
public LockToken lock(String client, String lockName) throws InterruptedException {
LockRequest lockRequest = LockRequest.of(ImmutableSet.of(StringLockDescriptor.of(lockName)), Long.MAX_VALUE, client);
LockResponse lockResponse = timelockService.lock(lockRequest);
Preconditions.checkState(lockResponse.wasSuccessful(), "Jepsen failed to lock a lock, but it would wait for Long.MAX_VALUE, so this is unexpected.");
return lockResponse.getToken();
}
use of com.palantir.lock.v2.LockToken in project atlasdb by palantir.
the class SnapshotTransaction method commitWrites.
private void commitWrites(TransactionService transactionService) {
if (!hasWrites()) {
if (hasReads()) {
// verify any pre-commit conditions on the transaction
preCommitCondition.throwIfConditionInvalid(getStartTimestamp());
// to ensure that sweep hasn't thoroughly deleted cells we tried to read
if (validationNecessaryForInvolvedTables()) {
throwIfImmutableTsOrCommitLocksExpired(null);
}
return;
}
return;
}
Timer.Context acquireLocksTimer = getTimer("commitAcquireLocks").time();
LockToken commitLocksToken = acquireLocksForCommit();
long millisForLocks = TimeUnit.NANOSECONDS.toMillis(acquireLocksTimer.stop());
try {
Timer.Context conflictsTimer = getTimer("commitCheckingForConflicts").time();
throwIfConflictOnCommit(commitLocksToken, transactionService);
long millisCheckingForConflicts = TimeUnit.NANOSECONDS.toMillis(conflictsTimer.stop());
Timer.Context writesTimer = getTimer("commitWrite").time();
keyValueService.multiPut(writesByTable, getStartTimestamp());
long millisForWrites = TimeUnit.NANOSECONDS.toMillis(writesTimer.stop());
// Now that all writes are done, get the commit timestamp
// We must do this before we check that our locks are still valid to ensure that
// other transactions that will hold these locks are sure to have start
// timestamps after our commit timestamp.
long commitTimestamp = timelockService.getFreshTimestamp();
commitTsForScrubbing = commitTimestamp;
// punch on commit so that if hard delete is the only thing happening on a system,
// we won't block forever waiting for the unreadable timestamp to advance past the
// scrub timestamp (same as the hard delete transaction's start timestamp)
Timer.Context punchTimer = getTimer("millisForPunch").time();
cleaner.punch(commitTimestamp);
long millisForPunch = TimeUnit.NANOSECONDS.toMillis(punchTimer.stop());
throwIfReadWriteConflictForSerializable(commitTimestamp);
// Verify that our locks and pre-commit conditions are still valid before we actually commit;
// this throwIfPreCommitRequirementsNotMet is required by the transaction protocol for correctness
throwIfPreCommitRequirementsNotMet(commitLocksToken, commitTimestamp);
Timer.Context commitTsTimer = getTimer("commitPutCommitTs").time();
putCommitTimestamp(commitTimestamp, commitLocksToken, transactionService);
long millisForCommitTs = TimeUnit.NANOSECONDS.toMillis(commitTsTimer.stop());
long millisSinceCreation = System.currentTimeMillis() - timeCreated;
getTimer("commitTotalTimeSinceTxCreation").update(millisSinceCreation, TimeUnit.MILLISECONDS);
getHistogram(AtlasDbMetricNames.SNAPSHOT_TRANSACTION_BYTES_WRITTEN).update(byteCount.get());
if (perfLogger.isDebugEnabled()) {
SafeAndUnsafeTableReferences tableRefs = LoggingArgs.tableRefs(writesByTable.keySet());
perfLogger.debug("Committed {} bytes with locks, start ts {}, commit ts {}, " + "acquiring locks took {} ms, checking for conflicts took {} ms, " + "writing took {} ms, punch took {} ms, putCommitTs took {} ms, " + "total time since tx creation {} ms, tables: {}.", SafeArg.of("numBytes", byteCount.get()), SafeArg.of("startTs", getStartTimestamp()), SafeArg.of("commitTs", commitTimestamp), SafeArg.of("millisForLocks", millisForLocks), SafeArg.of("millisCheckForConflicts", millisCheckingForConflicts), SafeArg.of("millisForWrites", millisForWrites), SafeArg.of("millisForPunch", millisForPunch), SafeArg.of("millisForCommitTs", millisForCommitTs), SafeArg.of("millisSinceCreation", millisSinceCreation), tableRefs.safeTableRefs(), tableRefs.unsafeTableRefs());
}
sweepQueue.enqueue(writesByTable, getStartTimestamp());
} finally {
timelockService.unlock(ImmutableSet.of(commitLocksToken));
}
}
use of com.palantir.lock.v2.LockToken in project atlasdb by palantir.
the class SnapshotTransaction method acquireLocksForCommit.
// /////////////////////////////////////////////////////////////////////////
// / Locking
// /////////////////////////////////////////////////////////////////////////
/**
* This method should acquire any locks needed to do proper concurrency control at commit time.
*/
protected LockToken acquireLocksForCommit() {
Set<LockDescriptor> lockDescriptors = getLocksForWrites();
LockRequest request = LockRequest.of(lockDescriptors, lockAcquireTimeoutMs);
LockResponse lockResponse = timelockService.lock(request);
if (!lockResponse.wasSuccessful()) {
log.error("Timed out waiting while acquiring commit locks. Request id was {}. Timeout was {} ms. " + "First ten required locks were {}.", SafeArg.of("requestId", request.getRequestId()), SafeArg.of("acquireTimeoutMs", lockAcquireTimeoutMs), UnsafeArg.of("firstTenLockDescriptors", Iterables.limit(lockDescriptors, 10)));
throw new TransactionLockAcquisitionTimeoutException("Timed out while acquiring commit locks.");
}
return lockResponse.getToken();
}
use of com.palantir.lock.v2.LockToken in project atlasdb by palantir.
the class SnapshotTransaction method throwIfImmutableTsOrCommitLocksExpired.
private void throwIfImmutableTsOrCommitLocksExpired(@Nullable LockToken commitLocksToken) {
Set<LockToken> expiredLocks = refreshCommitAndImmutableTsLocks(commitLocksToken);
if (!expiredLocks.isEmpty()) {
final String baseMsg = "Required locks are no longer valid. ";
String expiredLocksErrorString = getExpiredLocksErrorString(commitLocksToken, expiredLocks);
TransactionLockTimeoutException ex = new TransactionLockTimeoutException(baseMsg + expiredLocksErrorString);
log.error(baseMsg + "{}", expiredLocksErrorString, ex);
throw ex;
}
}
use of com.palantir.lock.v2.LockToken in project atlasdb by palantir.
the class SnapshotTransactionManager method setupRunTaskWithConditionThrowOnConflict.
public RawTransaction setupRunTaskWithConditionThrowOnConflict(PreCommitCondition condition) {
LockImmutableTimestampResponse immutableTsResponse = timelockService.lockImmutableTimestamp(LockImmutableTimestampRequest.create());
try {
LockToken immutableTsLock = immutableTsResponse.getLock();
long immutableTs = immutableTsResponse.getImmutableTimestamp();
recordImmutableTimestamp(immutableTs);
Supplier<Long> startTimestampSupplier = getStartTimestampSupplier();
SnapshotTransaction transaction = createTransaction(immutableTs, startTimestampSupplier, immutableTsLock, condition);
return new RawTransaction(transaction, immutableTsLock);
} catch (Throwable e) {
timelockService.unlock(ImmutableSet.of(immutableTsResponse.getLock()));
throw Throwables.rewrapAndThrowUncheckedException(e);
}
}
Aggregations