Search in sources :

Example 11 with HeldLocksToken

use of com.palantir.lock.HeldLocksToken in project atlasdb by palantir.

the class RangeVisitor method visitRange.

private void visitRange(Visitor visitor, MutableRange range) {
    do {
        final RangeRequest request = range.getRangeRequest();
        try {
            long startTime = System.currentTimeMillis();
            long numVisited = txManager.runTaskWithLocksWithRetry(lockTokens, Suppliers.ofInstance(null), new LockAwareTransactionTask<Long, RuntimeException>() {

                @Override
                public Long execute(Transaction tx, Iterable<HeldLocksToken> heldLocks) {
                    return visitInternal(tx, visitor, request, range);
                }

                @Override
                public String toString() {
                    return "visitRange(" + request + ")";
                }
            });
            counter.addAndGet(numVisited);
            log.info("Visited {} rows from {} in {} ms.", numVisited, tableRef.getQualifiedName(), System.currentTimeMillis() - startTime);
        } catch (InterruptedException e) {
            throw Throwables.rewrapAndThrowUncheckedException(e);
        }
    } while (!range.isComplete());
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) HeldLocksToken(com.palantir.lock.HeldLocksToken) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 12 with HeldLocksToken

use of com.palantir.lock.HeldLocksToken in project atlasdb by palantir.

the class SnapshotTransactionTest method noRetryOnExpiredLockTokens.

@Test
public void noRetryOnExpiredLockTokens() throws InterruptedException {
    HeldLocksToken expiredLockToken = getExpiredHeldLocksToken();
    try {
        txManager.runTaskWithLocksWithRetry(ImmutableList.of(expiredLockToken), () -> null, (tx, locks) -> {
            tx.put(TABLE, ImmutableMap.of(TEST_CELL, PtBytes.toBytes("value")));
            return null;
        });
        fail();
    } catch (TransactionLockTimeoutNonRetriableException e) {
        Set<LockRefreshToken> expectedTokens = ImmutableSet.of(expiredLockToken.getLockRefreshToken());
        assertThat(e.getMessage(), containsString(expectedTokens.toString()));
        assertThat(e.getMessage(), containsString("Retry is not possible."));
    }
}
Also used : Set(java.util.Set) ImmutableSet(com.google.common.collect.ImmutableSet) HeldLocksToken(com.palantir.lock.HeldLocksToken) TransactionLockTimeoutNonRetriableException(com.palantir.atlasdb.transaction.api.TransactionLockTimeoutNonRetriableException) Test(org.junit.Test)

Example 13 with HeldLocksToken

use of com.palantir.lock.HeldLocksToken in project atlasdb by palantir.

the class LockServiceImpl method lockWithFullLockResponse.

@Override
public // We're concerned about sanitizing logs at the info level and above. This method just logs at debug and info.
LockResponse lockWithFullLockResponse(LockClient client, LockRequest request) throws InterruptedException {
    Preconditions.checkNotNull(client);
    Preconditions.checkArgument(!client.equals(INTERNAL_LOCK_GRANT_CLIENT));
    Preconditions.checkArgument(request.getLockTimeout().compareTo(maxAllowedLockTimeout) <= 0, "Requested lock timeout (%s) is greater than maximum allowed lock timeout (%s)", request.getLockTimeout(), maxAllowedLockTimeout);
    long startTime = System.currentTimeMillis();
    if (requestLogger.isDebugEnabled()) {
        requestLogger.debug("LockServiceImpl processing lock request {} for requesting thread {}", UnsafeArg.of("lockRequest", request), SafeArg.of("requestingThread", request.getCreatingThreadName()));
    }
    Map<ClientAwareReadWriteLock, LockMode> locks = Maps.newLinkedHashMap();
    if (isShutDown.get()) {
        throw new ServiceNotAvailableException("This lock server is shut down.");
    }
    try {
        boolean indefinitelyBlocking = isIndefinitelyBlocking(request.getBlockingMode());
        if (indefinitelyBlocking) {
            indefinitelyBlockingThreads.add(Thread.currentThread());
        }
        outstandingLockRequestMultimap.put(client, request);
        Map<LockDescriptor, LockClient> failedLocks = Maps.newHashMap();
        @Nullable Long deadline = (request.getBlockingDuration() == null) ? null : System.nanoTime() + request.getBlockingDuration().toNanos();
        if (request.getBlockingMode() == BLOCK_UNTIL_TIMEOUT) {
            if (request.getLockGroupBehavior() == LOCK_AS_MANY_AS_POSSIBLE) {
                tryLocks(client, request, DO_NOT_BLOCK, null, LOCK_AS_MANY_AS_POSSIBLE, locks, failedLocks);
            }
        }
        tryLocks(client, request, request.getBlockingMode(), deadline, request.getLockGroupBehavior(), locks, failedLocks);
        if (request.getBlockingMode() == BlockingMode.BLOCK_INDEFINITELY_THEN_RELEASE) {
            if (log.isTraceEnabled()) {
                logNullResponse(client, request, null);
            }
            if (requestLogger.isDebugEnabled()) {
                requestLogger.debug("Timed out requesting {} for requesting thread {} after {} ms", UnsafeArg.of("request", request), SafeArg.of("threadName", request.getCreatingThreadName()), SafeArg.of("timeoutMillis", System.currentTimeMillis() - startTime));
            }
            return new LockResponse(failedLocks);
        }
        if (locks.isEmpty() || ((request.getLockGroupBehavior() == LOCK_ALL_OR_NONE) && (locks.size() < request.getLockDescriptors().size()))) {
            if (log.isTraceEnabled()) {
                logNullResponse(client, request, null);
            }
            if (requestLogger.isDebugEnabled()) {
                requestLogger.debug("Failed to acquire all locks for {} for requesting thread {} after {} ms", UnsafeArg.of("request", request), SafeArg.of("threadName", request.getCreatingThreadName()), SafeArg.of("waitMillis", System.currentTimeMillis() - startTime));
            }
            if (requestLogger.isTraceEnabled()) {
                logLockAcquisitionFailure(failedLocks);
            }
            return new LockResponse(null, failedLocks);
        }
        Builder<LockDescriptor, LockMode> lockDescriptorMap = ImmutableSortedMap.naturalOrder();
        for (Entry<ClientAwareReadWriteLock, LockMode> entry : locks.entrySet()) {
            lockDescriptorMap.put(entry.getKey().getDescriptor(), entry.getValue());
        }
        if (request.getVersionId() != null) {
            versionIdMap.put(client, request.getVersionId());
        }
        HeldLocksToken token = createHeldLocksToken(client, LockCollections.of(lockDescriptorMap.build()), LockCollections.of(locks), request.getLockTimeout(), request.getVersionId(), request.getCreatingThreadName());
        locks.clear();
        if (log.isTraceEnabled()) {
            logNullResponse(client, request, token);
        }
        if (Thread.interrupted()) {
            throw new InterruptedException("Interrupted while locking.");
        }
        if (requestLogger.isDebugEnabled()) {
            requestLogger.debug("Successfully acquired locks {} for requesting thread {} after {} ms", UnsafeArg.of("request", request), SafeArg.of("threadName", request.getCreatingThreadName()), SafeArg.of("waitMillis", System.currentTimeMillis() - startTime));
        }
        return new LockResponse(token, failedLocks);
    } finally {
        outstandingLockRequestMultimap.remove(client, request);
        indefinitelyBlockingThreads.remove(Thread.currentThread());
        try {
            for (Entry<ClientAwareReadWriteLock, LockMode> entry : locks.entrySet()) {
                entry.getKey().get(client, entry.getValue()).unlock();
            }
        } catch (Throwable e) {
            // (authorized)
            log.error("Internal lock server error: state has been corrupted!!", UnsafeArg.of("exception", e), SafeArg.of("stacktrace", e.getStackTrace()));
            throw Throwables.throwUncheckedException(e);
        }
    }
}
Also used : ServiceNotAvailableException(com.palantir.common.remoting.ServiceNotAvailableException) LockMode(com.palantir.lock.LockMode) LockDescriptor(com.palantir.lock.LockDescriptor) StringLockDescriptor(com.palantir.lock.StringLockDescriptor) LockResponse(com.palantir.lock.LockResponse) HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleHeldLocksToken(com.palantir.lock.SimpleHeldLocksToken) LockClient(com.palantir.lock.LockClient) Nullable(javax.annotation.Nullable)

Example 14 with HeldLocksToken

use of com.palantir.lock.HeldLocksToken in project atlasdb by palantir.

the class LockServiceImpl method unlockSimple.

@Override
public boolean unlockSimple(SimpleHeldLocksToken token) {
    Preconditions.checkNotNull(token);
    LockDescriptor fakeLockDesc = StringLockDescriptor.of("unlockSimple");
    SortedLockCollection<LockDescriptor> fakeLockSet = LockCollections.of(ImmutableSortedMap.of(fakeLockDesc, LockMode.READ));
    return unlock(new HeldLocksToken(token.getTokenId(), LockClient.ANONYMOUS, token.getCreationDateMs(), 0L, fakeLockSet, maxAllowedLockTimeout, 0L, "UnknownThread-unlockSimple"));
}
Also used : LockDescriptor(com.palantir.lock.LockDescriptor) StringLockDescriptor(com.palantir.lock.StringLockDescriptor) HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleHeldLocksToken(com.palantir.lock.SimpleHeldLocksToken)

Example 15 with HeldLocksToken

use of com.palantir.lock.HeldLocksToken in project atlasdb by palantir.

the class LockServiceImpl method useGrant.

@Override
public HeldLocksToken useGrant(LockClient client, BigInteger grantId) {
    Preconditions.checkNotNull(client);
    Preconditions.checkArgument(client != INTERNAL_LOCK_GRANT_CLIENT);
    Preconditions.checkNotNull(grantId);
    HeldLocksGrant grant = new HeldLocksGrant(grantId);
    @Nullable HeldLocks<HeldLocksGrant> heldLocks = heldLocksGrantMap.remove(grant);
    if (heldLocks == null) {
        log.warn(GRANT_MESSAGE, client, grantId.toString(Character.MAX_RADIX));
        String formattedMessage = MessageFormatter.format(GRANT_MESSAGE, client, grantId.toString(Character.MAX_RADIX)).getMessage();
        throw new IllegalArgumentException(formattedMessage);
    }
    HeldLocksGrant realGrant = heldLocks.realToken;
    changeOwner(heldLocks.locks, INTERNAL_LOCK_GRANT_CLIENT, client);
    HeldLocksToken token = createHeldLocksToken(client, realGrant.getLockDescriptors(), heldLocks.locks, realGrant.getLockTimeout(), realGrant.getVersionId(), "Converted from Grant, Missing Thread Name");
    if (log.isTraceEnabled()) {
        log.trace(".useGrant({}, {}) returns {}", client, grantId.toString(Character.MAX_RADIX), token);
    }
    return token;
}
Also used : HeldLocksGrant(com.palantir.lock.HeldLocksGrant) HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleHeldLocksToken(com.palantir.lock.SimpleHeldLocksToken) Nullable(javax.annotation.Nullable)

Aggregations

HeldLocksToken (com.palantir.lock.HeldLocksToken)29 SimpleHeldLocksToken (com.palantir.lock.SimpleHeldLocksToken)19 Test (org.junit.Test)13 LockDescriptor (com.palantir.lock.LockDescriptor)8 Nullable (javax.annotation.Nullable)8 StringLockDescriptor (com.palantir.lock.StringLockDescriptor)7 HeldLocksGrant (com.palantir.lock.HeldLocksGrant)5 LockClient (com.palantir.lock.LockClient)5 LockRefreshToken (com.palantir.lock.LockRefreshToken)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 LockResponse (com.palantir.lock.LockResponse)4 BigInteger (java.math.BigInteger)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 LockMode (com.palantir.lock.LockMode)3 SimpleTimeDuration (com.palantir.lock.SimpleTimeDuration)3 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)2 LockRequest (com.palantir.lock.LockRequest)2 TimeDuration (com.palantir.lock.TimeDuration)2 Set (java.util.Set)2 ImmutableList (com.google.common.collect.ImmutableList)1