use of com.palantir.lock.HeldLocksToken in project atlasdb by palantir.
the class LockServiceImpl method refreshLockRefreshTokens.
@Override
public Set<LockRefreshToken> refreshLockRefreshTokens(Iterable<LockRefreshToken> tokens) {
List<HeldLocksToken> fakeTokens = Lists.newArrayList();
LockDescriptor fakeLockDesc = StringLockDescriptor.of("refreshLockRefreshTokens");
SortedLockCollection<LockDescriptor> fakeLockSet = LockCollections.of(ImmutableSortedMap.of(fakeLockDesc, LockMode.READ));
for (LockRefreshToken token : tokens) {
fakeTokens.add(new HeldLocksToken(token.getTokenId(), LockClient.ANONYMOUS, 0L, 0L, fakeLockSet, maxAllowedLockTimeout, 0L, "UnknownThread-refreshLockRefreshTokens"));
}
return ImmutableSet.copyOf(Iterables.transform(refreshTokens(fakeTokens), HeldLocksTokens.getRefreshTokenFun()));
}
use of com.palantir.lock.HeldLocksToken in project atlasdb by palantir.
the class LockServiceImpl method refreshTokens.
@Override
public Set<HeldLocksToken> refreshTokens(Iterable<HeldLocksToken> tokens) {
Preconditions.checkNotNull(tokens);
ImmutableSet.Builder<HeldLocksToken> refreshedTokens = ImmutableSet.builder();
for (HeldLocksToken token : tokens) {
@Nullable HeldLocksToken refreshedToken = refreshToken(token);
if (refreshedToken != null) {
refreshedTokens.add(refreshedToken);
}
}
Set<HeldLocksToken> refreshedTokenSet = refreshedTokens.build();
if (log.isTraceEnabled()) {
log.trace(".refreshTokens({}) returns {}", Iterables.transform(tokens, TOKEN_TO_ID), Iterables.transform(refreshedTokenSet, TOKEN_TO_ID));
}
return refreshedTokenSet;
}
use of com.palantir.lock.HeldLocksToken in project atlasdb by palantir.
the class LockServiceImpl method convertToGrant.
@Override
public HeldLocksGrant convertToGrant(HeldLocksToken token) {
Preconditions.checkNotNull(token);
@Nullable HeldLocks<HeldLocksToken> heldLocks = heldLocksTokenMap.remove(token);
if (heldLocks == null) {
log.warn("Cannot convert to grant; invalid token: {} (token ID {})", UnsafeArg.of("token", token), SafeArg.of("tokenId", token.getTokenId()));
throw new IllegalArgumentException("token is invalid: " + token);
}
if (isFrozen(heldLocks.locks.getKeys())) {
heldLocksTokenMap.put(token, heldLocks);
lockTokenReaperQueue.add(token);
log.warn("Cannot convert to grant because token is frozen: {} (token ID {})", UnsafeArg.of("token", token), SafeArg.of("tokenId", token.getTokenId()));
throw new IllegalArgumentException("token is frozen: " + token);
}
try {
changeOwner(heldLocks.locks, heldLocks.realToken.getClient(), INTERNAL_LOCK_GRANT_CLIENT);
} catch (IllegalMonitorStateException e) {
heldLocksTokenMap.put(token, heldLocks);
lockTokenReaperQueue.add(token);
log.warn("Failure converting {} (token ID {}) to grant", UnsafeArg.of("token", token), SafeArg.of("tokenId", token.getTokenId()), e);
throw e;
}
lockClientMultimap.remove(heldLocks.realToken.getClient(), token);
HeldLocksGrant grant = createHeldLocksGrant(heldLocks.realToken.getLockDescriptors(), heldLocks.locks, heldLocks.realToken.getLockTimeout(), heldLocks.realToken.getVersionId());
if (log.isTraceEnabled()) {
log.trace(".convertToGrant({}) (token ID {}) returns {} (grant ID {})", UnsafeArg.of("token", token), SafeArg.of("tokenId", token.getTokenId()), UnsafeArg.of("grant", grant), SafeArg.of("grantId", grant.getGrantId()));
}
return grant;
}
use of com.palantir.lock.HeldLocksToken in project atlasdb by palantir.
the class LockServiceSerDeTest method testSerialisationAndDeserialisationOfLockResponseWithLockHolders.
@Test
public void testSerialisationAndDeserialisationOfLockResponseWithLockHolders() throws Exception {
HeldLocksToken token = LockServiceTestUtils.getFakeHeldLocksToken("client A", "Fake thread", new BigInteger("1"), "held-lock-1", "logger-lock");
Map<LockDescriptor, LockClient> lockHolders = ImmutableMap.of(StringLockDescriptor.of("lock_id"), LockClient.ANONYMOUS, StringLockDescriptor.of("lock_id2"), LockClient.of("client"));
LockResponse response = new LockResponse(token, lockHolders);
ObjectMapper mapper = new ObjectMapper();
LockResponse deserializedLockResponse = mapper.readValue(mapper.writeValueAsString(response), LockResponse.class);
assertEquals(lockHolders, deserializedLockResponse.getLockHolders());
}
use of com.palantir.lock.HeldLocksToken in project atlasdb by palantir.
the class LockServiceStateLoggerTest method setUp.
@BeforeClass
public static void setUp() throws Exception {
LockServiceTestUtils.cleanUpLogStateDir();
LockClient clientA = LockClient.of("Client A");
LockClient clientB = LockClient.of("Client B");
LockDescriptor descriptor1 = StringLockDescriptor.of("logger-lock");
LockDescriptor descriptor2 = StringLockDescriptor.of("logger-AAA");
LockRequest request1 = LockRequest.builder(LockCollections.of(ImmutableSortedMap.of(descriptor1, LockMode.WRITE))).blockForAtMost(SimpleTimeDuration.of(1000, TimeUnit.MILLISECONDS)).build();
LockRequest request2 = LockRequest.builder(LockCollections.of(ImmutableSortedMap.of(descriptor2, LockMode.WRITE))).blockForAtMost(SimpleTimeDuration.of(1000, TimeUnit.MILLISECONDS)).build();
outstandingLockRequestMultimap.put(clientA, request1);
outstandingLockRequestMultimap.put(clientB, request2);
outstandingLockRequestMultimap.put(clientA, request2);
HeldLocksToken token = LockServiceTestUtils.getFakeHeldLocksToken("client A", "Fake thread", new BigInteger("1"), "held-lock-1", "logger-lock");
HeldLocksToken token2 = LockServiceTestUtils.getFakeHeldLocksToken("client B", "Fake thread 2", new BigInteger("2"), "held-lock-2", "held-lock-3");
heldLocksTokenMap.putIfAbsent(token, LockServiceImpl.HeldLocks.of(token, LockCollections.of()));
heldLocksTokenMap.putIfAbsent(token2, LockServiceImpl.HeldLocks.of(token2, LockCollections.of()));
LockServiceStateLogger logger = new LockServiceStateLogger(heldLocksTokenMap, outstandingLockRequestMultimap, LockServiceTestUtils.TEST_LOG_STATE_DIR);
logger.logLocks();
}
Aggregations