private LockRequest nextRequest() { LockDescriptor lockDescriptor = lockDescriptors.get((int) (counter.incrementAndGet() % lockDescriptors.size())); return LockRequest.of(ImmutableSet.of(lockDescriptor), ACQUIRE_TIMEOUT_MS); } }
@Override public AsyncResult<LockToken> lock(LockRequest request) { return lockService.lock( request.getRequestId(), request.getLockDescriptors(), TimeLimit.of(request.getAcquireTimeoutMs())); }
static RequestInfo of(LockRequest request) { return ImmutableRequestInfo.of( request.getRequestId(), request.getClientDescription().orElse(EMPTY_DESCRIPTION), request.getLockDescriptors()); }
private com.palantir.lock.LockRequest toLegacyLockRequest(LockRequest request) { SortedMap<LockDescriptor, LockMode> locks = buildLockMap(request.getLockDescriptors(), LockMode.WRITE); return com.palantir.lock.LockRequest.builder(locks) .blockForAtMost(SimpleTimeDuration.of(request.getAcquireTimeoutMs(), TimeUnit.MILLISECONDS)) .build(); }
/** * This method should acquire any locks needed to do proper concurrency control at commit time. */ protected LockToken acquireLocksForCommit() { Set<LockDescriptor> lockDescriptors = getLocksForWrites(); LockRequest request = LockRequest.of(lockDescriptors, transactionConfig.get().getLockAcquireTimeoutMillis()); LockResponse lockResponse = timelockService.lock(request); if (!lockResponse.wasSuccessful()) { log.error("Timed out waiting while acquiring commit locks. Request id was {}. Timeout was {} ms. " + "First ten required locks were {}.", SafeArg.of("requestId", request.getRequestId()), SafeArg.of("acquireTimeoutMs", transactionConfig.get().getLockAcquireTimeoutMillis()), UnsafeArg.of("firstTenLockDescriptors", Iterables.limit(lockDescriptors, 10))); throw new TransactionLockAcquisitionTimeoutException("Timed out while acquiring commit locks."); } return lockResponse.getToken(); }
/** * Creates a mock of a LockService that only gives out a lock once per unique request and never releases it, even * if unlock is called. The returned tokens are monotonically increasing in the tokenId. */ private TimelockService createStickyLockService() { AtomicLong lockToken = new AtomicLong(0); Set<LockDescriptor> requestedLocks = new ConcurrentHashSet<>(); TimelockService stickyLockService = mock(TimelockService.class); doAnswer(invocation -> { LockRequest request = invocation.getArgument(0); if (requestedLocks.add(Iterables.getOnlyElement(request.getLockDescriptors()))) { return (LockResponse) () -> Optional.of(LockToken.of(new UUID(lockToken.getAndIncrement(), 0L))); } else { return (LockResponse) Optional::empty; } }).when(stickyLockService).lock(any()); return stickyLockService; }
/** * This method should acquire any locks needed to do proper concurrency control at commit time. */ protected LockToken acquireLocksForCommit() { Set<LockDescriptor> lockDescriptors = getLocksForWrites(); LockRequest request = LockRequest.of(lockDescriptors, transactionConfig.get().getLockAcquireTimeoutMillis()); LockResponse lockResponse = timelockService.lock(request); if (!lockResponse.wasSuccessful()) { log.error("Timed out waiting while acquiring commit locks. Request id was {}. Timeout was {} ms. " + "First ten required locks were {}.", SafeArg.of("requestId", request.getRequestId()), SafeArg.of("acquireTimeoutMs", transactionConfig.get().getLockAcquireTimeoutMillis()), UnsafeArg.of("firstTenLockDescriptors", Iterables.limit(lockDescriptors, 10))); throw new TransactionLockAcquisitionTimeoutException("Timed out while acquiring commit locks."); } return lockResponse.getToken(); }
@Override public AsyncResult<LockToken> lock(LockRequest request) { return lockService.lock( request.getRequestId(), request.getLockDescriptors(), TimeLimit.of(request.getAcquireTimeoutMs())); }
@Override public LockToken lock(String client, String lockName) throws InterruptedException { LockRequest lockRequest = LockRequest.of( ImmutableSet.of(StringLockDescriptor.of(lockName)), Long.MAX_VALUE, client); LockResponse lockResponse = timelockService.lock(lockRequest); Preconditions.checkState(lockResponse.wasSuccessful(), "Jepsen failed to lock a lock, but it would wait for Long.MAX_VALUE, so this is unexpected."); return lockResponse.getToken(); }
static RequestInfo of(LockRequest request) { return ImmutableRequestInfo.of( request.getRequestId(), request.getClientDescription().orElse(EMPTY_DESCRIPTION), request.getLockDescriptors()); }
public static Optional<TargetedSweeperLock> tryAcquire(int shard, TableMetadataPersistence.SweepStrategy strategy, TimelockService timeLock) { ShardAndStrategy shardStrategy = ShardAndStrategy.of(shard, strategy); LockDescriptor lock = StringLockDescriptor.of(shardStrategy.toText()); // We do not want the timeout to be too low to avoid a race condition where we give up too soon LockRequest request = LockRequest.of(ImmutableSet.of(lock), 100L); return timeLock.lock(request) .getTokenOrEmpty() .map(lockToken -> new TargetedSweeperLock(shardStrategy, timeLock, lockToken)); }
@Test public void registersLocks() { LockRequest request = LockRequest.of(LOCKS, TIMEOUT); when(delegate.lock(request)).thenReturn(LockResponse.successful(TOKEN_1)); timelock.lock(request); verify(refresher).registerLock(TOKEN_1); }
@Test public void lockDelegatesToLockService() throws InterruptedException { com.palantir.lock.LockRequest legacyRequest = com.palantir.lock.LockRequest.builder(buildLockMap(LockMode.WRITE)) .blockForAtMost(SimpleTimeDuration.of(TIMEOUT, TimeUnit.MILLISECONDS)) .build(); when(lockService.lock(LockClient.ANONYMOUS.getClientId(), legacyRequest)).thenReturn(LOCK_REFRESH_TOKEN); assertEquals(LockResponse.successful(LOCK_TOKEN_V2), timelock.lock( LockRequest.of(ImmutableSet.of(LOCK_A, LOCK_B), TIMEOUT))); verify(lockService).lock(LockClient.ANONYMOUS.getClientId(), legacyRequest); }
public static Optional<TargetedSweeperLock> tryAcquire(int shard, TableMetadataPersistence.SweepStrategy strategy, TimelockService timeLock) { ShardAndStrategy shardStrategy = ShardAndStrategy.of(shard, strategy); LockDescriptor lock = StringLockDescriptor.of(shardStrategy.toText()); // We do not want the timeout to be too low to avoid a race condition where we give up too soon LockRequest request = LockRequest.of(ImmutableSet.of(lock), 100L); return timeLock.lock(request) .getTokenOrEmpty() .map(lockToken -> new TargetedSweeperLock(shardStrategy, timeLock, lockToken)); }