Search in sources :

Example 1 with HeldLocksToken

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

the class SnapshotTransactionTest method getExpiredHeldLocksToken.

private HeldLocksToken getExpiredHeldLocksToken() {
    ImmutableSortedMap.Builder<LockDescriptor, LockMode> builder = ImmutableSortedMap.naturalOrder();
    builder.put(AtlasRowLockDescriptor.of(TransactionConstants.TRANSACTION_TABLE.getQualifiedName(), TransactionConstants.getValueForTimestamp(0L)), LockMode.WRITE);
    long creationDateMs = System.currentTimeMillis();
    long expirationDateMs = creationDateMs - 1;
    TimeDuration lockTimeout = SimpleTimeDuration.of(0, TimeUnit.SECONDS);
    long versionId = 0L;
    return new HeldLocksToken(BigInteger.ZERO, lockClient, creationDateMs, expirationDateMs, LockCollections.of(builder.build()), lockTimeout, versionId, "Dummy thread");
}
Also used : AtlasRowLockDescriptor(com.palantir.lock.AtlasRowLockDescriptor) LockDescriptor(com.palantir.lock.LockDescriptor) HeldLocksToken(com.palantir.lock.HeldLocksToken) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) SimpleTimeDuration(com.palantir.lock.SimpleTimeDuration) TimeDuration(com.palantir.lock.TimeDuration) LockMode(com.palantir.lock.LockMode)

Example 2 with HeldLocksToken

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

the class LockRemotingTest method testLock.

@Test
public void testLock() throws InterruptedException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    LockDescriptor desc = StringLockDescriptor.of("1234");
    String writeValueAsString = mapper.writeValueAsString(desc);
    LockDescriptor desc2 = mapper.readValue(writeValueAsString, LockDescriptor.class);
    long minVersion = 123;
    LockRequest request = LockRequest.builder(ImmutableSortedMap.of(desc, LockMode.WRITE)).doNotBlock().withLockedInVersionId(minVersion).build();
    writeValueAsString = mapper.writeValueAsString(request);
    LockRequest request2 = mapper.readValue(writeValueAsString, LockRequest.class);
    LockRefreshToken lockResponse = rawLock.lock(LockClient.ANONYMOUS.getClientId(), request);
    rawLock.unlock(lockResponse);
    writeValueAsString = mapper.writeValueAsString(lockResponse);
    LockRefreshToken lockResponse2 = mapper.readValue(writeValueAsString, LockRefreshToken.class);
    LockService lock = AtlasDbFeignTargetFactory.createProxy(Optional.empty(), lockService.baseUri().toString(), LockService.class, UserAgents.DEFAULT_USER_AGENT);
    String lockClient = "23234";
    LockRefreshToken token = lock.lock(lockClient, request);
    long minLockedInVersionId = lock.getMinLockedInVersionId(lockClient);
    Assert.assertEquals(minVersion, minLockedInVersionId);
    lock.unlock(token);
    token = lock.lock(LockClient.ANONYMOUS.getClientId(), request);
    Set<LockRefreshToken> refreshed = lock.refreshLockRefreshTokens(ImmutableList.of(token));
    Assert.assertEquals(1, refreshed.size());
    lock.unlock(token);
    try {
        lock.logCurrentState();
    } finally {
        LockServiceTestUtils.cleanUpLogStateDir();
    }
    lock.currentTimeMillis();
    HeldLocksToken token1 = lock.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), request);
    HeldLocksToken token2 = lock.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), request2);
    Assert.assertNull(token2);
    lock.unlock(token1.getLockRefreshToken());
    token2 = lock.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), request2);
    Assert.assertNotNull(token2);
    lock.unlock(token2.getLockRefreshToken());
}
Also used : StringLockDescriptor(com.palantir.lock.StringLockDescriptor) LockDescriptor(com.palantir.lock.LockDescriptor) LockService(com.palantir.lock.LockService) HeldLocksToken(com.palantir.lock.HeldLocksToken) LockRequest(com.palantir.lock.LockRequest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LockRefreshToken(com.palantir.lock.LockRefreshToken) Test(org.junit.Test)

Example 3 with HeldLocksToken

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

the class AdvisoryLocksConditionTest method getHeldLocksToken.

private static HeldLocksToken getHeldLocksToken(BigInteger tokenId) {
    long creationDateMs = System.currentTimeMillis();
    long expirationDateMs = creationDateMs - 1;
    TimeDuration lockTimeout = SimpleTimeDuration.of(0, TimeUnit.SECONDS);
    long versionId = 0L;
    return new HeldLocksToken(tokenId, LockClient.of("fake lock client"), creationDateMs, expirationDateMs, LOCK_DESCRIPTORS, lockTimeout, versionId, "Dummy thread");
}
Also used : HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleTimeDuration(com.palantir.lock.SimpleTimeDuration) TimeDuration(com.palantir.lock.TimeDuration)

Example 4 with HeldLocksToken

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

the class AdvisoryLockConditionSuppliers method acquireLock.

private static HeldLocksToken acquireLock(LockService lockService, LockRequest lockRequest) {
    int failureCount = 0;
    while (true) {
        HeldLocksToken response;
        try {
            response = lockService.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), lockRequest);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RuntimeException(e);
        }
        if (response == null) {
            RuntimeException ex = new LockAcquisitionException("Failed to lock using the provided lock request: " + lockRequest);
            log.warn("Count not lock successfully", ex);
            ++failureCount;
            if (failureCount >= NUM_RETRIES) {
                log.warn("Failing after {} tries", failureCount, ex);
                throw ex;
            }
            AbstractTransactionManager.sleepForBackoff(failureCount);
        } else {
            return response;
        }
    }
}
Also used : HeldLocksToken(com.palantir.lock.HeldLocksToken) LockAcquisitionException(com.palantir.atlasdb.transaction.api.LockAcquisitionException)

Example 5 with HeldLocksToken

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

the class LockServiceImpl method getTokens.

@Override
public Set<HeldLocksToken> getTokens(LockClient client) {
    Preconditions.checkNotNull(client);
    if (client.isAnonymous()) {
        throw new IllegalArgumentException("client must not be anonymous");
    } else if (client.equals(INTERNAL_LOCK_GRANT_CLIENT)) {
        throw new IllegalArgumentException("Illegal client!");
    }
    ImmutableSet.Builder<HeldLocksToken> tokens = ImmutableSet.builder();
    synchronized (lockClientMultimap) {
        for (HeldLocksToken token : lockClientMultimap.get(client)) {
            @Nullable HeldLocks<HeldLocksToken> heldLocks = heldLocksTokenMap.get(token);
            if ((heldLocks != null) && !isFrozen(heldLocks.locks.getKeys())) {
                tokens.add(token);
            }
        }
    }
    ImmutableSet<HeldLocksToken> tokenSet = tokens.build();
    if (log.isTraceEnabled()) {
        log.trace(".getTokens({}) returns {}", client, Iterables.transform(tokenSet, TOKEN_TO_ID));
    }
    return tokenSet;
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) 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