Search in sources :

Example 6 with LockMode

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);
        }
    }
}
Also used : LockDescriptor(com.palantir.lock.LockDescriptor) StringLockDescriptor(com.palantir.lock.StringLockDescriptor) LockClient(com.palantir.lock.LockClient) LockMode(com.palantir.lock.LockMode) ExecutionException(java.util.concurrent.ExecutionException) Nullable(javax.annotation.Nullable)

Example 7 with LockMode

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);
}
Also used : StringLockDescriptor(com.palantir.lock.StringLockDescriptor) LockDescriptor(com.palantir.lock.LockDescriptor) HeldLocksGrant(com.palantir.lock.HeldLocksGrant) LockMode(com.palantir.lock.LockMode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 8 with LockMode

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();
}
Also used : StringLockDescriptor(com.palantir.lock.StringLockDescriptor) LockDescriptor(com.palantir.lock.LockDescriptor) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) LockMode(com.palantir.lock.LockMode)

Aggregations

LockMode (com.palantir.lock.LockMode)8 LockDescriptor (com.palantir.lock.LockDescriptor)6 StringLockDescriptor (com.palantir.lock.StringLockDescriptor)4 LockClient (com.palantir.lock.LockClient)3 Nullable (javax.annotation.Nullable)3 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)2 AtlasRowLockDescriptor (com.palantir.lock.AtlasRowLockDescriptor)2 HeldLocksToken (com.palantir.lock.HeldLocksToken)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Cell (com.palantir.atlasdb.keyvalue.api.Cell)1 ServiceNotAvailableException (com.palantir.common.remoting.ServiceNotAvailableException)1 BLOCK_UNTIL_TIMEOUT (com.palantir.lock.BlockingMode.BLOCK_UNTIL_TIMEOUT)1 HeldLocksGrant (com.palantir.lock.HeldLocksGrant)1 INTERNAL_LOCK_GRANT_CLIENT (com.palantir.lock.LockClient.INTERNAL_LOCK_GRANT_CLIENT)1 LockRefreshToken (com.palantir.lock.LockRefreshToken)1 LockRequest (com.palantir.lock.LockRequest)1 LockResponse (com.palantir.lock.LockResponse)1 SimpleHeldLocksToken (com.palantir.lock.SimpleHeldLocksToken)1 SimpleTimeDuration (com.palantir.lock.SimpleTimeDuration)1 TimeDuration (com.palantir.lock.TimeDuration)1