use of com.palantir.lock.LockMode in project atlasdb by palantir.
the class LockServiceImpl method tryLocks.
private void tryLocks(LockClient client, LockRequest request, BlockingMode blockingMode, @Nullable Long deadline, LockGroupBehavior lockGroupBehavior, Map<? super ClientAwareReadWriteLock, ? super LockMode> locks, Map<? super LockDescriptor, ? super LockClient> failedLocks) throws InterruptedException {
String previousThreadName = null;
try {
previousThreadName = updateThreadName(request);
for (Entry<LockDescriptor, LockMode> entry : request.getLockDescriptors().entries()) {
if (blockingMode == BlockingMode.BLOCK_INDEFINITELY_THEN_RELEASE && !descriptorToLockMap.asMap().containsKey(entry.getKey())) {
continue;
}
ClientAwareReadWriteLock lock;
try {
lock = descriptorToLockMap.get(entry.getKey());
} catch (ExecutionException e) {
throw new RuntimeException(e.getCause());
}
if (locks.containsKey(lock)) {
// This is the 2nd time we are calling tryLocks and we already locked this one.
continue;
}
long startTime = System.currentTimeMillis();
@Nullable LockClient currentHolder = tryLock(lock.get(client, entry.getValue()), blockingMode, deadline);
if (log.isDebugEnabled() || isSlowLogEnabled()) {
long responseTimeMillis = System.currentTimeMillis() - startTime;
logSlowLockAcquisition(entry.getKey().toString(), currentHolder, responseTimeMillis);
}
if (currentHolder == null) {
locks.put(lock, entry.getValue());
} else {
failedLocks.put(entry.getKey(), currentHolder);
if (lockGroupBehavior == LOCK_ALL_OR_NONE) {
return;
}
}
}
} finally {
if (previousThreadName != null) {
tryRenameThread(previousThreadName);
}
}
}
use of com.palantir.lock.LockMode in project atlasdb by palantir.
the class LockServiceSerDeTest method testSerialisationAndDeserialisationOfHeldLocksGrant.
@Test
public void testSerialisationAndDeserialisationOfHeldLocksGrant() throws Exception {
ImmutableSortedMap<LockDescriptor, LockMode> lockDescriptorLockMode = LockServiceTestUtils.getLockDescriptorLockMode(ImmutableList.of("lock1", "lock2"));
HeldLocksGrant heldLocksGrant = new HeldLocksGrant(BigInteger.ONE, System.currentTimeMillis(), System.currentTimeMillis() + 10L, LockCollections.of(lockDescriptorLockMode), SimpleTimeDuration.of(100, TimeUnit.SECONDS), 10L);
ObjectMapper mapper = new ObjectMapper();
String serializedForm = mapper.writeValueAsString(heldLocksGrant);
HeldLocksGrant deserialzedlockServerOptions = mapper.readValue(serializedForm, HeldLocksGrant.class);
assertEquals(heldLocksGrant, deserialzedlockServerOptions);
}
use of com.palantir.lock.LockMode in project atlasdb by palantir.
the class LockServiceTestUtils method getLockDescriptorLockMode.
public static ImmutableSortedMap<LockDescriptor, LockMode> getLockDescriptorLockMode(List<String> descriptors) {
ImmutableSortedMap.Builder<LockDescriptor, LockMode> builder = ImmutableSortedMap.naturalOrder();
for (String descriptor : descriptors) {
LockDescriptor descriptor1 = StringLockDescriptor.of(descriptor);
builder.put(descriptor1, LockMode.WRITE);
}
return builder.build();
}
Aggregations