Search in sources :

Example 6 with HeldLocksToken

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

the class LockServiceImpl method createHeldLocksToken.

private HeldLocksToken createHeldLocksToken(LockClient client, SortedLockCollection<LockDescriptor> lockDescriptorMap, LockCollection<? extends ClientAwareReadWriteLock> heldLocksMap, TimeDuration lockTimeout, @Nullable Long versionId, String requestThread) {
    while (true) {
        BigInteger tokenId = new BigInteger(RANDOM_BIT_COUNT, randomPool.getSecureRandom());
        long expirationDateMs = currentTimeMillis() + lockTimeout.toMillis();
        HeldLocksToken token = new HeldLocksToken(tokenId, client, currentTimeMillis(), expirationDateMs, lockDescriptorMap, lockTimeout, versionId, requestThread);
        HeldLocks<HeldLocksToken> heldLocks = HeldLocks.of(token, heldLocksMap);
        if (heldLocksTokenMap.putIfAbsent(token, heldLocks) == null) {
            lockTokenReaperQueue.add(token);
            if (!client.isAnonymous()) {
                lockClientMultimap.put(client, token);
            }
            return token;
        }
        log.error("Lock ID collision! " + "Count of held tokens = {}" + "; random bit count = {}", SafeArg.of("heldTokenCount", heldLocksTokenMap.size()), SafeArg.of("randomBitCount", RANDOM_BIT_COUNT));
    }
}
Also used : HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleHeldLocksToken(com.palantir.lock.SimpleHeldLocksToken) BigInteger(java.math.BigInteger)

Example 7 with HeldLocksToken

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

the class LockServiceImpl method refreshToken.

@Nullable
private HeldLocksToken refreshToken(HeldLocksToken token) {
    Preconditions.checkNotNull(token);
    @Nullable HeldLocks<HeldLocksToken> heldLocks = heldLocksTokenMap.get(token);
    if ((heldLocks == null) || isFrozen(heldLocks.locks.getKeys())) {
        return null;
    }
    long now = currentTimeMillis();
    long expirationDateMs = now + heldLocks.realToken.getLockTimeout().toMillis();
    heldLocksTokenMap.replace(token, heldLocks, new HeldLocks<HeldLocksToken>(heldLocks.realToken.refresh(expirationDateMs), heldLocks.locks));
    heldLocks = heldLocksTokenMap.get(token);
    if (heldLocks == null) {
        return null;
    }
    HeldLocksToken finalToken = heldLocks.realToken;
    logIfAbnormallyOld(finalToken, now);
    return finalToken;
}
Also used : HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleHeldLocksToken(com.palantir.lock.SimpleHeldLocksToken) Nullable(javax.annotation.Nullable) Nullable(javax.annotation.Nullable)

Example 8 with HeldLocksToken

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

the class LockServiceImpl method unlockAndFreeze.

@Override
public boolean unlockAndFreeze(HeldLocksToken token) {
    Preconditions.checkNotNull(token);
    @Nullable HeldLocks<HeldLocksToken> heldLocks = heldLocksTokenMap.remove(token);
    if (heldLocks == null) {
        if (log.isTraceEnabled()) {
            log.trace(".unlockAndFreeze({}) returns false", token);
        }
        return false;
    }
    LockClient client = heldLocks.realToken.getClient();
    if (client.isAnonymous()) {
        heldLocksTokenMap.put(token, heldLocks);
        lockTokenReaperQueue.add(token);
        log.warn(UNLOCK_AND_FREEZE_FROM_ANONYMOUS_CLIENT, heldLocks.realToken);
        throw new IllegalArgumentException(MessageFormatter.format(UNLOCK_AND_FREEZE_FROM_ANONYMOUS_CLIENT, heldLocks.realToken).getMessage());
    }
    if (heldLocks.locks.hasReadLock()) {
        heldLocksTokenMap.put(token, heldLocks);
        lockTokenReaperQueue.add(token);
        log.warn(UNLOCK_AND_FREEZE, heldLocks.realToken);
        throw new IllegalArgumentException(MessageFormatter.format(UNLOCK_AND_FREEZE, heldLocks.realToken).getMessage());
    }
    for (ClientAwareReadWriteLock lock : heldLocks.locks.getKeys()) {
        lock.get(client, LockMode.WRITE).unlockAndFreeze();
    }
    lockClientMultimap.remove(client, token);
    if (heldLocks.realToken.getVersionId() != null) {
        versionIdMap.remove(client, heldLocks.realToken.getVersionId());
    }
    if (log.isTraceEnabled()) {
        log.trace(".unlockAndFreeze({}) returns true", token);
    }
    return true;
}
Also used : HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleHeldLocksToken(com.palantir.lock.SimpleHeldLocksToken) LockClient(com.palantir.lock.LockClient) Nullable(javax.annotation.Nullable)

Example 9 with HeldLocksToken

use of com.palantir.lock.HeldLocksToken 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 10 with HeldLocksToken

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

the class LockServiceSerDeTest method testSerialisationAndDeserialisationOfLockResponse.

@Test
public void testSerialisationAndDeserialisationOfLockResponse() throws Exception {
    HeldLocksToken token = LockServiceTestUtils.getFakeHeldLocksToken("client A", "Fake thread", new BigInteger("1"), "held-lock-1", "logger-lock");
    LockResponse response = new LockResponse(token);
    ObjectMapper mapper = new ObjectMapper();
    LockResponse deserializedLockResponse = mapper.readValue(mapper.writeValueAsString(response), LockResponse.class);
    assertEquals(deserializedLockResponse, response);
}
Also used : HeldLocksToken(com.palantir.lock.HeldLocksToken) LockResponse(com.palantir.lock.LockResponse) BigInteger(java.math.BigInteger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

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