private LockRefreshToken lockAnonymous(com.palantir.lock.LockRequest request) { try { return lockService.lock(LockClient.ANONYMOUS.getClientId(), request); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new RuntimeException(ex); } }
private long getImmutableTimestampInternal(long ts) { Long minLocked = lockService.getMinLockedInVersionId(immutableTsLockClient.getClientId()); return minLocked == null ? ts : minLocked; }
private List<SimpleLockRequest> getDescriptorSimpleRequestMap(LockClient client, LockRequest request) { return request.getLocks().stream() .map(lock -> SimpleLockRequest.of(request, this.lockDescriptorMapper.getDescriptorMapping(lock.getLockDescriptor()), lock.getLockMode(), client.getClientId())) .collect(Collectors.toList()); }
@Override public LockResponse lockWithFullLockResponse(LockClient client, LockRequest request) throws InterruptedException { return callWithTimeLimit( () -> delegate.lockWithFullLockResponse(client, request), ImmutableLockRequestSpecification.of("lockWithFullLockResponse", client.getClientId(), request)); }
private void mockMinLockedInVersionIdResponse(Long immutableTs) { when(lockService.getMinLockedInVersionId(LOCK_CLIENT.getClientId())).thenReturn(immutableTs); }
@Test public void rethrowsExceptionOccurringFromInterruptibleOperation() { assertThatThrownBy(() -> throwingService.lock(LockClient.ANONYMOUS.getClientId(), LOCK_REQUEST)) .isInstanceOf(IllegalStateException.class); }
@Test public void conditionSupplier_transactionLockFailure() throws InterruptedException { Supplier<AdvisoryLocksCondition> conditionSupplier = AdvisoryLockConditionSuppliers.get(lockService, ImmutableSet.of(), LOCK_REQUEST_SUPPLIER); when(lockService.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), LOCK_REQUEST)) .thenReturn(null); assertThatThrownBy(() -> conditionSupplier.get()) .isInstanceOf(LockAcquisitionException.class) .hasMessageContaining("Failed to lock using the provided lock request"); }
@Test public void conditionSupplier_transactionLockFailureBeforeSuccess() throws InterruptedException { Supplier<AdvisoryLocksCondition> conditionSupplier = AdvisoryLockConditionSuppliers.get(lockService, ImmutableSet.of(), LOCK_REQUEST_SUPPLIER); when(lockService.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), LOCK_REQUEST)) .thenReturn(null) .thenReturn(null) .thenReturn(TRANSACTION_LOCK_TOKEN); assertThat(conditionSupplier.get().getLocks()).containsOnly(TRANSACTION_LOCK_TOKEN); }
@Test public void testSerializationDeserialization() throws Exception { LockClient lockClient = new LockClient("foo"); LockClient deserializedLockClient = mapper.readValue(mapper.writeValueAsString(lockClient), LockClient.class); assertThat(lockClient.getClientId(), is(deserializedLockClient.getClientId())); assertThat(lockClient.isAnonymous(), is(deserializedLockClient.isAnonymous())); }
@Test public void conditionSupplier_bothLocks() throws InterruptedException { Supplier<AdvisoryLocksCondition> conditionSupplier = AdvisoryLockConditionSuppliers.get( lockService, ImmutableSet.of(EXTERNAL_LOCK_TOKEN), LOCK_REQUEST_SUPPLIER); when(lockService.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), LOCK_REQUEST)) .thenReturn(TRANSACTION_LOCK_TOKEN); assertThat(conditionSupplier.get().getLocks()).containsOnly(EXTERNAL_LOCK_TOKEN, TRANSACTION_LOCK_TOKEN); }
@Test public void conditionSupplier_transactionLockSuccess() throws InterruptedException { Supplier<AdvisoryLocksCondition> conditionSupplier = AdvisoryLockConditionSuppliers.get(lockService, ImmutableSet.of(), LOCK_REQUEST_SUPPLIER); when(lockService.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), LOCK_REQUEST)) .thenReturn(TRANSACTION_LOCK_TOKEN); assertThat(conditionSupplier.get().getLocks()).containsOnly(TRANSACTION_LOCK_TOKEN); }
public static SimpleTokenInfo of(HeldLocksToken token, LockMode lockMode) { return ImmutableSimpleTokenInfo.builder() .lockMode(lockMode) .expiresIn(token.getExpirationDateMs() - System.currentTimeMillis()) .createdAtTs(token.getCreationDateMs()) .tokenId(token.getTokenId().toString()) .clientId(Preconditions.checkNotNull(token.getClient()).getClientId()) .requestThread(token.getRequestingThread()) .createAt(new Date(token.getCreationDateMs()).toString()) .versionId(token.getVersionId()) .build(); }
public void lockOrRefresh() throws InterruptedException { if (token != null) { Set<LockRefreshToken> refreshedTokens = lockService.refreshLockRefreshTokens(ImmutableList.of(token)); log.info("Refreshed an existing lock token for {} in a single lock service (token {}); got {}", getLockIdLoggingArg(), SafeArg.of("existingLockToken", token), SafeArg.of("refreshedTokens", refreshedTokens)); if (refreshedTokens.isEmpty()) { token = null; } } else { LockDescriptor lock = StringLockDescriptor.of(lockId); LockRequest request = LockRequest.builder( ImmutableSortedMap.of(lock, LockMode.WRITE)).doNotBlock().build(); token = lockService.lock(LockClient.ANONYMOUS.getClientId(), request); log.info("Attempted to acquire the lock {} in a single lock service; got {}", getLockIdLoggingArg(), SafeArg.of("acquiredToken", token)); } }
@Test public void waitForLocksDelegatesToLockService() throws InterruptedException { com.palantir.lock.LockRequest legacyRequest = com.palantir.lock.LockRequest.builder(buildLockMap(LockMode.READ)).lockAndRelease().build(); when(lockService.lock(LockClient.ANONYMOUS.getClientId(), legacyRequest)).thenReturn(LOCK_REFRESH_TOKEN); timelock.waitForLocks(WaitForLocksRequest.of(ImmutableSet.of(LOCK_A, LOCK_B), TIMEOUT)); verify(lockService).lock(LockClient.ANONYMOUS.getClientId(), legacyRequest); }
@Test public void getImmutableTimestampDelegatesInProperOrder() throws InterruptedException { long immutableTs = 3L; InOrder inOrder = Mockito.inOrder(timestampService, lockService); mockMinLockedInVersionIdResponse(immutableTs); assertEquals(immutableTs, timelock.getImmutableTimestamp()); inOrder.verify(timestampService).getFreshTimestamp(); inOrder.verify(lockService).getMinLockedInVersionId(LOCK_CLIENT.getClientId()); }
/** Tests that RemoteLockService api (that internal forwards to LockService api) passes a sanity check. */ @Test public void testRemoteLockServiceApi() throws InterruptedException { LockRequest request = LockRequest.builder(ImmutableSortedMap.of( lock1, LockMode.READ, lock2, LockMode.WRITE)) .withLockedInVersionId(10).doNotBlock().build(); Assert.assertNull(server.getMinLockedInVersionId(LockClient.ANONYMOUS.getClientId())); LockRefreshToken token = server.lock(LockClient.ANONYMOUS.getClientId(), request); Assert.assertEquals(10, (long) server.getMinLockedInVersionId(LockClient.ANONYMOUS.getClientId())); Assert.assertNull(server.lock(LockClient.ANONYMOUS.getClientId(), request)); server.unlock(token); Assert.assertNull(server.getMinLockedInVersionId(LockClient.ANONYMOUS.getClientId())); HeldLocksToken heldToken = server.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), request); Assert.assertEquals(10, (long) server.getMinLockedInVersionId(LockClient.ANONYMOUS.getClientId())); Assert.assertNull(server.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), request)); server.unlock(heldToken.getLockRefreshToken()); }
@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); }
/** Tests identity operations */ @Test public void testIdentity() { Assert.assertEquals("a client", client.getClientId()); Assert.assertEquals("", LockClient.ANONYMOUS.getClientId()); Assert.assertEquals("lock1", lock1.getLockIdAsString()); Assert.assertEquals("lock2", lock2.getLockIdAsString()); }
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; }
/** Tests that our objects have {@code toString()} methods defined. */ @Test public void testToStrings() { Assert.assertEquals("client", client.getClientId()); Assert.assertEquals("lock", readWriteLock.getDescriptor().getLockIdAsString()); assertGoodToString(readWriteLock); assertGoodToString(anonymousReadLock); assertGoodToString(knownClientWriteLock); }