Search in sources :

Example 1 with LockResponse

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

the class LockServiceImpl method lock.

@Override
public LockRefreshToken lock(String client, LockRequest request) throws InterruptedException {
    Preconditions.checkArgument(request.getLockGroupBehavior() == LockGroupBehavior.LOCK_ALL_OR_NONE, "lock() only supports LockGroupBehavior.LOCK_ALL_OR_NONE. Consider using lockAndGetHeldLocks().");
    LockResponse result = lockWithFullLockResponse(LockClient.of(client), request);
    return result.success() ? result.getLockRefreshToken() : null;
}
Also used : LockResponse(com.palantir.lock.LockResponse)

Example 2 with LockResponse

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

the class LockServiceSerDeTest method testSerialisationAndDeserialisationOfLockResponse.

@Test
public void testSerialisationAndDeserialisationOfLockResponse() throws Exception {
    HeldLocksToken token = LockServiceTestUtils.getFakeHeldLocksToken("client A", "Fake thread", new BigInteger("1"), "held-lock-1", "logger-lock");
    LockResponse response = new LockResponse(token);
    ObjectMapper mapper = new ObjectMapper();
    LockResponse deserializedLockResponse = mapper.readValue(mapper.writeValueAsString(response), LockResponse.class);
    assertEquals(deserializedLockResponse, response);
}
Also used : HeldLocksToken(com.palantir.lock.HeldLocksToken) LockResponse(com.palantir.lock.LockResponse) BigInteger(java.math.BigInteger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with LockResponse

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

the class LockServiceImpl method lockWithFullLockResponse.

@Override
public // We're concerned about sanitizing logs at the info level and above. This method just logs at debug and info.
LockResponse lockWithFullLockResponse(LockClient client, LockRequest request) throws InterruptedException {
    Preconditions.checkNotNull(client);
    Preconditions.checkArgument(!client.equals(INTERNAL_LOCK_GRANT_CLIENT));
    Preconditions.checkArgument(request.getLockTimeout().compareTo(maxAllowedLockTimeout) <= 0, "Requested lock timeout (%s) is greater than maximum allowed lock timeout (%s)", request.getLockTimeout(), maxAllowedLockTimeout);
    long startTime = System.currentTimeMillis();
    if (requestLogger.isDebugEnabled()) {
        requestLogger.debug("LockServiceImpl processing lock request {} for requesting thread {}", UnsafeArg.of("lockRequest", request), SafeArg.of("requestingThread", request.getCreatingThreadName()));
    }
    Map<ClientAwareReadWriteLock, LockMode> locks = Maps.newLinkedHashMap();
    if (isShutDown.get()) {
        throw new ServiceNotAvailableException("This lock server is shut down.");
    }
    try {
        boolean indefinitelyBlocking = isIndefinitelyBlocking(request.getBlockingMode());
        if (indefinitelyBlocking) {
            indefinitelyBlockingThreads.add(Thread.currentThread());
        }
        outstandingLockRequestMultimap.put(client, request);
        Map<LockDescriptor, LockClient> failedLocks = Maps.newHashMap();
        @Nullable Long deadline = (request.getBlockingDuration() == null) ? null : System.nanoTime() + request.getBlockingDuration().toNanos();
        if (request.getBlockingMode() == BLOCK_UNTIL_TIMEOUT) {
            if (request.getLockGroupBehavior() == LOCK_AS_MANY_AS_POSSIBLE) {
                tryLocks(client, request, DO_NOT_BLOCK, null, LOCK_AS_MANY_AS_POSSIBLE, locks, failedLocks);
            }
        }
        tryLocks(client, request, request.getBlockingMode(), deadline, request.getLockGroupBehavior(), locks, failedLocks);
        if (request.getBlockingMode() == BlockingMode.BLOCK_INDEFINITELY_THEN_RELEASE) {
            if (log.isTraceEnabled()) {
                logNullResponse(client, request, null);
            }
            if (requestLogger.isDebugEnabled()) {
                requestLogger.debug("Timed out requesting {} for requesting thread {} after {} ms", UnsafeArg.of("request", request), SafeArg.of("threadName", request.getCreatingThreadName()), SafeArg.of("timeoutMillis", System.currentTimeMillis() - startTime));
            }
            return new LockResponse(failedLocks);
        }
        if (locks.isEmpty() || ((request.getLockGroupBehavior() == LOCK_ALL_OR_NONE) && (locks.size() < request.getLockDescriptors().size()))) {
            if (log.isTraceEnabled()) {
                logNullResponse(client, request, null);
            }
            if (requestLogger.isDebugEnabled()) {
                requestLogger.debug("Failed to acquire all locks for {} for requesting thread {} after {} ms", UnsafeArg.of("request", request), SafeArg.of("threadName", request.getCreatingThreadName()), SafeArg.of("waitMillis", System.currentTimeMillis() - startTime));
            }
            if (requestLogger.isTraceEnabled()) {
                logLockAcquisitionFailure(failedLocks);
            }
            return new LockResponse(null, failedLocks);
        }
        Builder<LockDescriptor, LockMode> lockDescriptorMap = ImmutableSortedMap.naturalOrder();
        for (Entry<ClientAwareReadWriteLock, LockMode> entry : locks.entrySet()) {
            lockDescriptorMap.put(entry.getKey().getDescriptor(), entry.getValue());
        }
        if (request.getVersionId() != null) {
            versionIdMap.put(client, request.getVersionId());
        }
        HeldLocksToken token = createHeldLocksToken(client, LockCollections.of(lockDescriptorMap.build()), LockCollections.of(locks), request.getLockTimeout(), request.getVersionId(), request.getCreatingThreadName());
        locks.clear();
        if (log.isTraceEnabled()) {
            logNullResponse(client, request, token);
        }
        if (Thread.interrupted()) {
            throw new InterruptedException("Interrupted while locking.");
        }
        if (requestLogger.isDebugEnabled()) {
            requestLogger.debug("Successfully acquired locks {} for requesting thread {} after {} ms", UnsafeArg.of("request", request), SafeArg.of("threadName", request.getCreatingThreadName()), SafeArg.of("waitMillis", System.currentTimeMillis() - startTime));
        }
        return new LockResponse(token, failedLocks);
    } finally {
        outstandingLockRequestMultimap.remove(client, request);
        indefinitelyBlockingThreads.remove(Thread.currentThread());
        try {
            for (Entry<ClientAwareReadWriteLock, LockMode> entry : locks.entrySet()) {
                entry.getKey().get(client, entry.getValue()).unlock();
            }
        } catch (Throwable e) {
            // (authorized)
            log.error("Internal lock server error: state has been corrupted!!", UnsafeArg.of("exception", e), SafeArg.of("stacktrace", e.getStackTrace()));
            throw Throwables.throwUncheckedException(e);
        }
    }
}
Also used : ServiceNotAvailableException(com.palantir.common.remoting.ServiceNotAvailableException) LockMode(com.palantir.lock.LockMode) LockDescriptor(com.palantir.lock.LockDescriptor) StringLockDescriptor(com.palantir.lock.StringLockDescriptor) LockResponse(com.palantir.lock.LockResponse) HeldLocksToken(com.palantir.lock.HeldLocksToken) SimpleHeldLocksToken(com.palantir.lock.SimpleHeldLocksToken) LockClient(com.palantir.lock.LockClient) Nullable(javax.annotation.Nullable)

Example 4 with LockResponse

use of com.palantir.lock.LockResponse 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 5 with LockResponse

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

the class LockRefreshingLockServiceTest method testSimpleRefresh.

@Test
public void testSimpleRefresh() throws InterruptedException {
    Builder builder = LockRequest.builder(ImmutableSortedMap.of(lock1, LockMode.WRITE));
    builder.timeoutAfter(SimpleTimeDuration.of(5, TimeUnit.SECONDS));
    LockResponse lock = server.lockWithFullLockResponse(LockClient.ANONYMOUS, builder.build());
    Thread.sleep(10000);
    Set<HeldLocksToken> refreshTokens = server.refreshTokens(ImmutableList.of(lock.getToken()));
    Assert.assertEquals(1, refreshTokens.size());
}
Also used : LockResponse(com.palantir.lock.LockResponse) HeldLocksToken(com.palantir.lock.HeldLocksToken) Builder(com.palantir.lock.LockRequest.Builder) Test(org.junit.Test)

Aggregations

LockResponse (com.palantir.lock.LockResponse)5 HeldLocksToken (com.palantir.lock.HeldLocksToken)4 Test (org.junit.Test)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 LockClient (com.palantir.lock.LockClient)2 LockDescriptor (com.palantir.lock.LockDescriptor)2 StringLockDescriptor (com.palantir.lock.StringLockDescriptor)2 BigInteger (java.math.BigInteger)2 ServiceNotAvailableException (com.palantir.common.remoting.ServiceNotAvailableException)1 LockMode (com.palantir.lock.LockMode)1 Builder (com.palantir.lock.LockRequest.Builder)1 SimpleHeldLocksToken (com.palantir.lock.SimpleHeldLocksToken)1 Nullable (javax.annotation.Nullable)1