Search in sources :

Example 1 with HeldLocksGrant

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

the class LockServiceImpl method createHeldLocksGrant.

private HeldLocksGrant createHeldLocksGrant(SortedLockCollection<LockDescriptor> lockDescriptorMap, LockCollection<? extends ClientAwareReadWriteLock> heldLocksMap, TimeDuration lockTimeout, @Nullable Long versionId) {
    while (true) {
        BigInteger grantId = new BigInteger(RANDOM_BIT_COUNT, randomPool.getSecureRandom());
        long expirationDateMs = currentTimeMillis() + lockTimeout.toMillis();
        HeldLocksGrant grant = new HeldLocksGrant(grantId, System.currentTimeMillis(), expirationDateMs, lockDescriptorMap, lockTimeout, versionId);
        HeldLocks<HeldLocksGrant> newHeldLocks = HeldLocks.of(grant, heldLocksMap);
        if (heldLocksGrantMap.putIfAbsent(grant, newHeldLocks) == null) {
            lockGrantReaperQueue.add(grant);
            return grant;
        }
        log.error("Lock ID collision! " + "Count of held grants = {}" + "; random bit count = {}", SafeArg.of("heldTokenCount", heldLocksTokenMap.size()), SafeArg.of("randomBitCount", RANDOM_BIT_COUNT));
    }
}
Also used : HeldLocksGrant(com.palantir.lock.HeldLocksGrant) BigInteger(java.math.BigInteger)

Example 2 with HeldLocksGrant

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

the class LockServiceImpl method refreshGrant.

@Override
@Nullable
public HeldLocksGrant refreshGrant(HeldLocksGrant grant) {
    Preconditions.checkNotNull(grant);
    @Nullable HeldLocks<HeldLocksGrant> heldLocks = heldLocksGrantMap.get(grant);
    if (heldLocks == null) {
        if (log.isTraceEnabled()) {
            log.trace(".refreshGrant({}) returns null", grant.getGrantId().toString(Character.MAX_RADIX));
        }
        return null;
    }
    long now = currentTimeMillis();
    long expirationDateMs = now + heldLocks.realToken.getLockTimeout().toMillis();
    heldLocksGrantMap.replace(grant, heldLocks, new HeldLocks<HeldLocksGrant>(heldLocks.realToken.refresh(expirationDateMs), heldLocks.locks));
    heldLocks = heldLocksGrantMap.get(grant);
    if (heldLocks == null) {
        if (log.isTraceEnabled()) {
            log.trace(".refreshGrant({}) returns null", grant.getGrantId().toString(Character.MAX_RADIX));
        }
        return null;
    }
    HeldLocksGrant refreshedGrant = heldLocks.realToken;
    logIfAbnormallyOld(refreshedGrant, now);
    if (log.isTraceEnabled()) {
        log.trace(".refreshGrant({}) returns {}", grant.getGrantId().toString(Character.MAX_RADIX), refreshedGrant.getGrantId().toString(Character.MAX_RADIX));
    }
    return refreshedGrant;
}
Also used : HeldLocksGrant(com.palantir.lock.HeldLocksGrant) Nullable(javax.annotation.Nullable) Nullable(javax.annotation.Nullable)

Example 3 with HeldLocksGrant

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

the class LockServiceImpl method useGrant.

@Override
public HeldLocksToken useGrant(LockClient client, HeldLocksGrant grant) {
    Preconditions.checkNotNull(client);
    Preconditions.checkArgument(client != INTERNAL_LOCK_GRANT_CLIENT);
    Preconditions.checkNotNull(grant);
    @Nullable HeldLocks<HeldLocksGrant> heldLocks = heldLocksGrantMap.remove(grant);
    if (heldLocks == null) {
        log.warn("Tried to use invalid grant: {} (grant ID {})", UnsafeArg.of("grant", grant), SafeArg.of("grantId", grant.getGrantId()));
        throw new IllegalArgumentException("grant is invalid: " + grant);
    }
    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({}, {}) (grant ID {}) returns {} (token ID {})", SafeArg.of("client", client), UnsafeArg.of("grant", grant), SafeArg.of("grantId", grant.getGrantId()), UnsafeArg.of("token", token), SafeArg.of("tokenId", token.getTokenId()));
    }
    return token;
}
Also used : HeldLocksGrant(com.palantir.lock.HeldLocksGrant) HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleHeldLocksToken(com.palantir.lock.SimpleHeldLocksToken) Nullable(javax.annotation.Nullable)

Example 4 with HeldLocksGrant

use of com.palantir.lock.HeldLocksGrant 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)

Example 5 with HeldLocksGrant

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

the class LockServiceImpl method convertToGrant.

@Override
public HeldLocksGrant convertToGrant(HeldLocksToken token) {
    Preconditions.checkNotNull(token);
    @Nullable HeldLocks<HeldLocksToken> heldLocks = heldLocksTokenMap.remove(token);
    if (heldLocks == null) {
        log.warn("Cannot convert to grant; invalid token: {} (token ID {})", UnsafeArg.of("token", token), SafeArg.of("tokenId", token.getTokenId()));
        throw new IllegalArgumentException("token is invalid: " + token);
    }
    if (isFrozen(heldLocks.locks.getKeys())) {
        heldLocksTokenMap.put(token, heldLocks);
        lockTokenReaperQueue.add(token);
        log.warn("Cannot convert to grant because token is frozen: {} (token ID {})", UnsafeArg.of("token", token), SafeArg.of("tokenId", token.getTokenId()));
        throw new IllegalArgumentException("token is frozen: " + token);
    }
    try {
        changeOwner(heldLocks.locks, heldLocks.realToken.getClient(), INTERNAL_LOCK_GRANT_CLIENT);
    } catch (IllegalMonitorStateException e) {
        heldLocksTokenMap.put(token, heldLocks);
        lockTokenReaperQueue.add(token);
        log.warn("Failure converting {} (token ID {}) to grant", UnsafeArg.of("token", token), SafeArg.of("tokenId", token.getTokenId()), e);
        throw e;
    }
    lockClientMultimap.remove(heldLocks.realToken.getClient(), token);
    HeldLocksGrant grant = createHeldLocksGrant(heldLocks.realToken.getLockDescriptors(), heldLocks.locks, heldLocks.realToken.getLockTimeout(), heldLocks.realToken.getVersionId());
    if (log.isTraceEnabled()) {
        log.trace(".convertToGrant({}) (token ID {}) returns {} (grant ID {})", UnsafeArg.of("token", token), SafeArg.of("tokenId", token.getTokenId()), UnsafeArg.of("grant", grant), SafeArg.of("grantId", grant.getGrantId()));
    }
    return grant;
}
Also used : HeldLocksGrant(com.palantir.lock.HeldLocksGrant) HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleHeldLocksToken(com.palantir.lock.SimpleHeldLocksToken) Nullable(javax.annotation.Nullable)

Aggregations

HeldLocksGrant (com.palantir.lock.HeldLocksGrant)7 HeldLocksToken (com.palantir.lock.HeldLocksToken)4 SimpleHeldLocksToken (com.palantir.lock.SimpleHeldLocksToken)4 Nullable (javax.annotation.Nullable)4 Test (org.junit.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 LockDescriptor (com.palantir.lock.LockDescriptor)1 LockMode (com.palantir.lock.LockMode)1 StringLockDescriptor (com.palantir.lock.StringLockDescriptor)1 BigInteger (java.math.BigInteger)1