Search in sources :

Example 21 with LockToken

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();
}
Also used : LockResponse(com.palantir.lock.v2.LockResponse) LockRequest(com.palantir.lock.v2.LockRequest)

Example 22 with LockToken

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));
    }
}
Also used : Timer(com.codahale.metrics.Timer) LockToken(com.palantir.lock.v2.LockToken) SafeAndUnsafeTableReferences(com.palantir.atlasdb.logging.LoggingArgs.SafeAndUnsafeTableReferences)

Example 23 with LockToken

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();
}
Also used : AtlasCellLockDescriptor(com.palantir.lock.AtlasCellLockDescriptor) AtlasRowLockDescriptor(com.palantir.lock.AtlasRowLockDescriptor) LockDescriptor(com.palantir.lock.LockDescriptor) LockResponse(com.palantir.lock.v2.LockResponse) TransactionLockAcquisitionTimeoutException(com.palantir.atlasdb.transaction.api.TransactionLockAcquisitionTimeoutException) LockRequest(com.palantir.lock.v2.LockRequest)

Example 24 with LockToken

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;
    }
}
Also used : TransactionLockTimeoutException(com.palantir.atlasdb.transaction.api.TransactionLockTimeoutException) LockToken(com.palantir.lock.v2.LockToken)

Example 25 with LockToken

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);
    }
}
Also used : LockImmutableTimestampResponse(com.palantir.lock.v2.LockImmutableTimestampResponse) LockToken(com.palantir.lock.v2.LockToken) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Aggregations

LockToken (com.palantir.lock.v2.LockToken)42 Test (org.junit.Test)32 LockResponse (com.palantir.lock.v2.LockResponse)5 TimelockService (com.palantir.lock.v2.TimelockService)4 LockRequest (com.palantir.lock.v2.LockRequest)3 WaitForLocksResponse (com.palantir.lock.v2.WaitForLocksResponse)3 Timer (com.codahale.metrics.Timer)1 SafeAndUnsafeTableReferences (com.palantir.atlasdb.logging.LoggingArgs.SafeAndUnsafeTableReferences)1 TransactionLockAcquisitionTimeoutException (com.palantir.atlasdb.transaction.api.TransactionLockAcquisitionTimeoutException)1 TransactionLockTimeoutException (com.palantir.atlasdb.transaction.api.TransactionLockTimeoutException)1 ShouldRetry (com.palantir.flake.ShouldRetry)1 AtlasCellLockDescriptor (com.palantir.lock.AtlasCellLockDescriptor)1 AtlasRowLockDescriptor (com.palantir.lock.AtlasRowLockDescriptor)1 LockDescriptor (com.palantir.lock.LockDescriptor)1 LockRefreshToken (com.palantir.lock.LockRefreshToken)1 LockImmutableTimestampResponse (com.palantir.lock.v2.LockImmutableTimestampResponse)1 WaitForLocksRequest (com.palantir.lock.v2.WaitForLocksRequest)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1