Search in sources :

Example 16 with HeldLocksToken

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()));
}
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 17 with HeldLocksToken

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;
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleHeldLocksToken(com.palantir.lock.SimpleHeldLocksToken) Nullable(javax.annotation.Nullable)

Example 18 with HeldLocksToken

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;
}
Also used : HeldLocksGrant(com.palantir.lock.HeldLocksGrant) HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleHeldLocksToken(com.palantir.lock.SimpleHeldLocksToken) Nullable(javax.annotation.Nullable)

Example 19 with HeldLocksToken

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

Example 20 with HeldLocksToken

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();
}
Also used : LockDescriptor(com.palantir.lock.LockDescriptor) StringLockDescriptor(com.palantir.lock.StringLockDescriptor) HeldLocksToken(com.palantir.lock.HeldLocksToken) LockClient(com.palantir.lock.LockClient) BigInteger(java.math.BigInteger) LockRequest(com.palantir.lock.LockRequest) BeforeClass(org.junit.BeforeClass)

Aggregations

HeldLocksToken (com.palantir.lock.HeldLocksToken)29 SimpleHeldLocksToken (com.palantir.lock.SimpleHeldLocksToken)19 Test (org.junit.Test)13 LockDescriptor (com.palantir.lock.LockDescriptor)8 Nullable (javax.annotation.Nullable)8 StringLockDescriptor (com.palantir.lock.StringLockDescriptor)7 HeldLocksGrant (com.palantir.lock.HeldLocksGrant)5 LockClient (com.palantir.lock.LockClient)5 LockRefreshToken (com.palantir.lock.LockRefreshToken)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 LockResponse (com.palantir.lock.LockResponse)4 BigInteger (java.math.BigInteger)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 LockMode (com.palantir.lock.LockMode)3 SimpleTimeDuration (com.palantir.lock.SimpleTimeDuration)3 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)2 LockRequest (com.palantir.lock.LockRequest)2 TimeDuration (com.palantir.lock.TimeDuration)2 Set (java.util.Set)2 ImmutableList (com.google.common.collect.ImmutableList)1