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");
}
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());
}
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);
}
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.");
}
}
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();
}
Aggregations