Search in sources :

Example 11 with LockDescriptor

use of com.palantir.lock.LockDescriptor in project atlasdb by palantir.

the class LockServiceImpl method unlockSimple.

@Override
public boolean unlockSimple(SimpleHeldLocksToken token) {
    Preconditions.checkNotNull(token);
    LockDescriptor fakeLockDesc = StringLockDescriptor.of("unlockSimple");
    SortedLockCollection<LockDescriptor> fakeLockSet = LockCollections.of(ImmutableSortedMap.of(fakeLockDesc, LockMode.READ));
    return unlock(new HeldLocksToken(token.getTokenId(), LockClient.ANONYMOUS, token.getCreationDateMs(), 0L, fakeLockSet, maxAllowedLockTimeout, 0L, "UnknownThread-unlockSimple"));
}
Also used : LockDescriptor(com.palantir.lock.LockDescriptor) StringLockDescriptor(com.palantir.lock.StringLockDescriptor) HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleHeldLocksToken(com.palantir.lock.SimpleHeldLocksToken)

Example 12 with LockDescriptor

use of com.palantir.lock.LockDescriptor 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()));
}
Also used : LockDescriptor(com.palantir.lock.LockDescriptor) StringLockDescriptor(com.palantir.lock.StringLockDescriptor) HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleHeldLocksToken(com.palantir.lock.SimpleHeldLocksToken) LockRefreshToken(com.palantir.lock.LockRefreshToken)

Example 13 with LockDescriptor

use of com.palantir.lock.LockDescriptor 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 14 with LockDescriptor

use of com.palantir.lock.LockDescriptor in project atlasdb by palantir.

the class LegacyTimelockServiceTest method mockImmutableTsLockResponse.

private LockRefreshToken mockImmutableTsLockResponse() throws InterruptedException {
    LockDescriptor descriptor = AtlasTimestampLockDescriptor.of(FRESH_TIMESTAMP);
    com.palantir.lock.LockRequest expectedRequest = com.palantir.lock.LockRequest.builder(ImmutableSortedMap.of(descriptor, LockMode.READ)).withLockedInVersionId(FRESH_TIMESTAMP).build();
    LockRefreshToken expectedToken = new LockRefreshToken(BigInteger.ONE, 123L);
    when(lockService.lock(LOCK_CLIENT.getClientId(), expectedRequest)).thenReturn(expectedToken);
    return expectedToken;
}
Also used : AtlasTimestampLockDescriptor(com.palantir.lock.AtlasTimestampLockDescriptor) LockDescriptor(com.palantir.lock.LockDescriptor) StringLockDescriptor(com.palantir.lock.StringLockDescriptor) LockRefreshToken(com.palantir.lock.LockRefreshToken)

Example 15 with LockDescriptor

use of com.palantir.lock.LockDescriptor 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());
}
Also used : StringLockDescriptor(com.palantir.lock.StringLockDescriptor) LockDescriptor(com.palantir.lock.LockDescriptor) HeldLocksToken(com.palantir.lock.HeldLocksToken) LockResponse(com.palantir.lock.LockResponse) LockClient(com.palantir.lock.LockClient) BigInteger(java.math.BigInteger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

LockDescriptor (com.palantir.lock.LockDescriptor)19 StringLockDescriptor (com.palantir.lock.StringLockDescriptor)12 HeldLocksToken (com.palantir.lock.HeldLocksToken)8 LockMode (com.palantir.lock.LockMode)7 AtlasRowLockDescriptor (com.palantir.lock.AtlasRowLockDescriptor)5 LockClient (com.palantir.lock.LockClient)5 LockRefreshToken (com.palantir.lock.LockRefreshToken)5 LockRequest (com.palantir.lock.LockRequest)5 Test (org.junit.Test)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)3 AtlasCellLockDescriptor (com.palantir.lock.AtlasCellLockDescriptor)3 SimpleHeldLocksToken (com.palantir.lock.SimpleHeldLocksToken)3 BigInteger (java.math.BigInteger)3 Cell (com.palantir.atlasdb.keyvalue.api.Cell)2 TransactionLockAcquisitionTimeoutException (com.palantir.atlasdb.transaction.api.TransactionLockAcquisitionTimeoutException)2 AtlasTimestampLockDescriptor (com.palantir.lock.AtlasTimestampLockDescriptor)2 LockResponse (com.palantir.lock.LockResponse)2 SimpleTimeDuration (com.palantir.lock.SimpleTimeDuration)2 Map (java.util.Map)2