Search in sources :

Example 1 with LockDescriptor

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

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

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

the class LockCollection method getAll.

public OrderedLocks getAll(Set<LockDescriptor> descriptors) {
    List<LockDescriptor> orderedDescriptors = sort(descriptors);
    List<AsyncLock> locks = Lists.newArrayListWithExpectedSize(descriptors.size());
    for (LockDescriptor descriptor : orderedDescriptors) {
        locks.add(getLock(descriptor));
    }
    return OrderedLocks.fromOrderedList(locks);
}
Also used : LockDescriptor(com.palantir.lock.LockDescriptor)

Example 4 with LockDescriptor

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

the class SnapshotTransaction method waitForCommitToComplete.

/**
 * We will block here until the passed transactions have released their lock.  This means that
 * the committing transaction is either complete or it has failed and we are allowed to roll
 * it back.
 */
private void waitForCommitToComplete(Iterable<Long> startTimestamps) {
    boolean isEmpty = true;
    Set<LockDescriptor> lockDescriptors = Sets.newHashSet();
    for (long start : startTimestamps) {
        if (start < immutableTimestamp) {
            // We don't need to block in this case because this transaction is already complete
            continue;
        }
        isEmpty = false;
        lockDescriptors.add(AtlasRowLockDescriptor.of(TransactionConstants.TRANSACTION_TABLE.getQualifiedName(), TransactionConstants.getValueForTimestamp(start)));
    }
    if (isEmpty) {
        return;
    }
    WaitForLocksRequest request = WaitForLocksRequest.of(lockDescriptors, lockAcquireTimeoutMs);
    WaitForLocksResponse response = timelockService.waitForLocks(request);
    if (!response.wasSuccessful()) {
        log.error("Timed out waiting for commits to complete. Timeout was {} ms. First ten locks were {}.", SafeArg.of("requestId", request.getRequestId()), SafeArg.of("acquireTimeoutMs", lockAcquireTimeoutMs), UnsafeArg.of("firstTenLockDescriptors", Iterables.limit(lockDescriptors, 10)));
        throw new TransactionLockAcquisitionTimeoutException("Timed out waiting for commits to complete.");
    }
}
Also used : WaitForLocksRequest(com.palantir.lock.v2.WaitForLocksRequest) AtlasCellLockDescriptor(com.palantir.lock.AtlasCellLockDescriptor) AtlasRowLockDescriptor(com.palantir.lock.AtlasRowLockDescriptor) LockDescriptor(com.palantir.lock.LockDescriptor) WaitForLocksResponse(com.palantir.lock.v2.WaitForLocksResponse) TransactionLockAcquisitionTimeoutException(com.palantir.atlasdb.transaction.api.TransactionLockAcquisitionTimeoutException)

Example 5 with LockDescriptor

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

Aggregations

LockDescriptor (com.palantir.lock.LockDescriptor)19 StringLockDescriptor (com.palantir.lock.StringLockDescriptor)12 HeldLocksToken (com.palantir.lock.HeldLocksToken)8 LockMode (com.palantir.lock.LockMode)7 AtlasRowLockDescriptor (com.palantir.lock.AtlasRowLockDescriptor)5 LockClient (com.palantir.lock.LockClient)5 LockRefreshToken (com.palantir.lock.LockRefreshToken)5 LockRequest (com.palantir.lock.LockRequest)5 Test (org.junit.Test)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)3 AtlasCellLockDescriptor (com.palantir.lock.AtlasCellLockDescriptor)3 SimpleHeldLocksToken (com.palantir.lock.SimpleHeldLocksToken)3 BigInteger (java.math.BigInteger)3 Cell (com.palantir.atlasdb.keyvalue.api.Cell)2 TransactionLockAcquisitionTimeoutException (com.palantir.atlasdb.transaction.api.TransactionLockAcquisitionTimeoutException)2 AtlasTimestampLockDescriptor (com.palantir.lock.AtlasTimestampLockDescriptor)2 LockResponse (com.palantir.lock.LockResponse)2 SimpleTimeDuration (com.palantir.lock.SimpleTimeDuration)2 Map (java.util.Map)2