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