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