System.out.println("Number of successfully acquired locks: " + numSuccess.get()); LockRequest request = LockRequest.builder( ImmutableSortedMap.of(lock1, LockMode.WRITE, lock2, LockMode.WRITE)).build(); HeldLocksToken token = server.lockWithFullLockResponse(LockClient.ANONYMOUS, request).getToken(); Assert.assertNotNull(token);
LockRequest requestAllLocks = LockRequest.builder(lockMap).doNotBlock().build(); HeldLocksToken readWriteToken = server.lockWithFullLockResponse(client, requestAllLocks).getToken(); Assert.assertNotNull(readWriteToken); lockMap.put(StringLockDescriptor.of("lock " + i), LockMode.READ); requestAllLocks = LockRequest.builder(lockMap).doNotBlock().build(); HeldLocksToken token = server.lockWithFullLockResponse(client, requestAllLocks).getToken(); Assert.assertNotNull(token); requestAllLocks = LockRequest.builder(lockMap).doNotBlock().build(); server.lockWithFullLockResponse(client, requestAllLocks); lockMap.put(StringLockDescriptor.of("lock " + i), LockMode.WRITE); requestAllLocks = LockRequest.builder(lockMap).doNotBlock().build(); token = server.lockWithFullLockResponse(client2, requestAllLocks).getToken(); Assert.assertNull(token); requestAllLocks = LockRequest.builder(lockMap).doNotBlock() .lockAsManyAsPossible().build(); token = server.lockWithFullLockResponse(client2, requestAllLocks).getToken(); Assert.assertNotNull(token); requestAllLocks = LockRequest.builder(lockMap).doNotBlock() .lockAsManyAsPossible().build(); token = server.lockWithFullLockResponse(client2, requestAllLocks).getToken(); Assert.assertNotNull(token);
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; }
if (!mapToAssertLockHeld.isEmpty()) { LockRequest request = LockRequest.builder(mapToAssertLockHeld) .doNotBlock() .lockAsManyAsPossible() .build(); LockRefreshToken lock = lockService.lock(LockClient.ANONYMOUS.getClientId(), request); Validate.isTrue(lock == null, "these should already be held"); LockRequest request = LockRequest.builder(mapToAssertLockNotHeld).doNotBlock().build(); LockRefreshToken lock = lockService.lock(LockClient.ANONYMOUS.getClientId(), request); Validate.isTrue(lock != null, "these should already be waited for");
&& (request.getBlockingMode() != BlockingMode.BLOCK_INDEFINITELY_THEN_RELEASE)) { LockRequest.Builder newRequest = LockRequest.builder(request.getLockDescriptors()); newRequest.doNotBlock(); newRequest.timeoutAfter(request.getLockTimeout()); if (request.getVersionId() != null) { newRequest.withLockedInVersionId(request.getVersionId());
/** * Instructs the lock server to block for at most the given duration * when acquiring locks. By default, the lock server will block * indefinitely. You may not call this method multiple times, and you * may not call both {@link #doNotBlock()} and this method. */ public Builder blockForAtMost(TimeDuration newBlockingDuration) { Preconditions.checkNotNull(newBlockingDuration, "newBlockingDuration should not be null"); TimeDuration realBlockingDuration = SimpleTimeDuration.of(newBlockingDuration); Preconditions.checkArgument(realBlockingDuration.toNanos() >= 0); if (realBlockingDuration.toNanos() == 0) { return doNotBlock(); } if ((lockMap == null) || (blockingMode != null)) { throw new IllegalStateException(); } blockingMode = BlockingMode.BLOCK_UNTIL_TIMEOUT; this.blockingDuration = realBlockingDuration; return this; }
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)); } }
Object readResolve() { Builder builder = new Builder(lockMap).timeoutAfter(lockTimeout); if (lockGroupBehavior == LockGroupBehavior.LOCK_AS_MANY_AS_POSSIBLE) { builder.lockAsManyAsPossible(); } if (blockingMode == BlockingMode.DO_NOT_BLOCK) { builder.doNotBlock(); } else if (blockingMode == BlockingMode.BLOCK_UNTIL_TIMEOUT) { builder.blockForAtMost(blockingDuration); } else if (blockingMode == BlockingMode.BLOCK_INDEFINITELY_THEN_RELEASE) { builder.lockAndRelease(); } if (versionId != null) { builder.withLockedInVersionId(versionId); } builder.withCreatingThreadName(creatingThreadName); return builder.build(); } }
LockRequest request = LockRequest.builder(ImmutableSortedMap.of( lock1, LockMode.READ, lock2, LockMode.WRITE)) .withLockedInVersionId(10) .blockForAtMost(SimpleTimeDuration.of(10, TimeUnit.MILLISECONDS)).build(); long currentTimeMs = System.currentTimeMillis(); LockResponse response = server.lockWithFullLockResponse(client, request); LockResponse response1 = server.lockWithFullLockResponse(LockClient.ANONYMOUS, LockRequest.builder( ImmutableSortedMap.of(lock2, LockMode.READ)) .blockForAtMost(SimpleTimeDuration.of(10, TimeUnit.MILLISECONDS)).build()); Assert.assertFalse(response1.success()); Assert.assertFalse(response1.getLockHolders().isEmpty()); .blockForAtMost(SimpleTimeDuration.of(100, TimeUnit.MILLISECONDS)).build()); Assert.assertTrue(response1.success()); Assert.assertTrue(response1.getLockHolders().isEmpty()); .blockForAtMost(SimpleTimeDuration.of(0, TimeUnit.MILLISECONDS)).build()); Assert.assertTrue(response.success()); Assert.assertTrue(response.getLockHolders().isEmpty()); .withLockedInVersionId(5) .blockForAtMost(SimpleTimeDuration.of(10, TimeUnit.MILLISECONDS)).build()); Assert.assertTrue(response.success()); Assert.assertTrue(response.getLockHolders().isEmpty()); .blockForAtMost(SimpleTimeDuration.of(10, TimeUnit.MILLISECONDS)).build()); Assert.assertTrue(response.success()); Assert.assertTrue(response.getLockHolders().isEmpty());
request = LockRequest.builder(ImmutableSortedMap.of( lock1, LockMode.READ, lock2, LockMode.WRITE)) .timeoutAfter(SimpleTimeDuration.of(200, TimeUnit.MILLISECONDS)) .doNotBlock().build(); } else if (clientID >= partition[0] && clientID < partition[1]) { request = LockRequest.builder(ImmutableSortedMap.of( lock2, LockMode.READ)) .timeoutAfter(SimpleTimeDuration.of(200, TimeUnit.MILLISECONDS)) .blockForAtMost( SimpleTimeDuration.of(200, TimeUnit.MILLISECONDS)) .build(); } else if (clientID >= partition[1] && clientID < partition[2]) { request = LockRequest.builder(ImmutableSortedMap.of( lock1, LockMode.WRITE, lock2, LockMode.READ)) .timeoutAfter(SimpleTimeDuration.of(200, TimeUnit.MILLISECONDS)) .doNotBlock().build(); } else if (clientID >= partition[2] && clientID < partition[3]) { request = LockRequest.builder(ImmutableSortedMap.of( lock1, LockMode.WRITE)) .timeoutAfter(SimpleTimeDuration.of(200, TimeUnit.MILLISECONDS)) .build(); } else { request = LockRequest.builder(ImmutableSortedMap.of( lock1, LockMode.READ, lock2, LockMode.WRITE)) .timeoutAfter(SimpleTimeDuration.of(200, TimeUnit.MILLISECONDS)) .blockForAtMost( SimpleTimeDuration.of(2000, TimeUnit.MILLISECONDS)) .build();
LockRequest.builder(ImmutableSortedMap.of(lock1, LockMode.READ)).doNotBlock().doNotBlock() .build(); Assert.fail(); } catch (IllegalStateException expected) { LockRequest.builder(ImmutableSortedMap.of(lock1, LockMode.READ)).doNotBlock() .blockForAtMost(SimpleTimeDuration.of(1, TimeUnit.MILLISECONDS)).build(); Assert.fail(); } catch (IllegalStateException expected) { .blockForAtMost(SimpleTimeDuration.of(1, TimeUnit.MILLISECONDS)) .blockForAtMost(SimpleTimeDuration.of(1, TimeUnit.MILLISECONDS)).build(); Assert.fail(); } catch (IllegalStateException expected) { .timeoutAfter(SimpleTimeDuration.of(1, TimeUnit.MILLISECONDS)) .timeoutAfter(SimpleTimeDuration.of(1, TimeUnit.MILLISECONDS)).build(); Assert.fail(); } catch (IllegalStateException expected) { LockRequest.builder(ImmutableSortedMap.of(lock1, LockMode.READ)).lockAsManyAsPossible() .lockAsManyAsPossible().build(); Assert.fail(); } catch (IllegalStateException expected) { LockRequest.builder(ImmutableSortedMap.of(lock1, LockMode.READ)).lockAsManyAsPossible() .build(); Assert.fail(); } catch (IllegalStateException expected) {
Assert.assertEquals(longString, lock.getLockIdAsString()); LockRequest request = LockRequest.builder(ImmutableSortedMap.of(lock, LockMode.READ)).build(); HeldLocksToken token = server.lockWithFullLockResponse(client, request).getToken(); Assert.assertEquals(client, token.getClient()); .blockForAtMost(SimpleTimeDuration.of(0, TimeUnit.SECONDS)).build(); try { server.lockWithFullLockResponse(client, request2); Assert.assertNull(token2); request2 = LockRequest.builder(ImmutableSortedMap.of(lock, LockMode.READ)) .blockForAtMost(server.getLockServerOptions().getMaxAllowedBlockingDuration()).build(); token2 = server.lockWithFullLockResponse(client2, request2).getToken(); Assert.assertNotNull(token2); server.getLockServerOptions().getMaxAllowedBlockingDuration().toSeconds() + 10, TimeUnit.SECONDS); request2 = LockRequest.builder(ImmutableSortedMap.of( lock, LockMode.READ)).blockForAtMost(beyondMaxDuration).build(); TimeDuration negativeDuration = SimpleTimeDuration.of(-10, TimeUnit.SECONDS); try { request2 = LockRequest.builder(ImmutableSortedMap.of( lock, LockMode.READ)).blockForAtMost(negativeDuration).build(); Assert.fail(); } catch (IllegalArgumentException expected) {
.doNotBlock().build(); long currentTimeMs = System.currentTimeMillis(); LockResponse response1 = server.lockWithFullLockResponse(LockClient.ANONYMOUS, request1); LockRequest request2 = LockRequest.builder(ImmutableSortedMap.of(lock1, LockMode.WRITE)).build(); LockResponse response2 = server.lockWithFullLockResponse(LockClient.ANONYMOUS, request2); HeldLocksToken validToken = response2.getToken();
.timeoutAfter(SimpleTimeDuration.of(1, TimeUnit.SECONDS)).doNotBlock().build(); HeldLocksToken token = server.lockWithFullLockResponse(LockClient.ANONYMOUS, request).getToken(); Assert.assertNotNull(token); Assert.assertTrue(server.getTokens(client).isEmpty()); HeldLocksToken token4 = server.lockWithFullLockResponse(client, LockRequest.builder(ImmutableSortedMap.of( lock2, LockMode.WRITE)).doNotBlock().build()).getToken(); Assert.assertNotNull(token4); Assert.assertEquals(ImmutableSet.of(token4), server.getTokens(client));
lock1, LockMode.WRITE)).doNotBlock().build(); LockRequest requestRead = LockRequest.builder(ImmutableSortedMap.of( lock1, LockMode.READ)).doNotBlock().build(); LockRequest requestTwoLocks = LockRequest.builder(ImmutableSortedMap.of( lock1, LockMode.READ, lock2, LockMode.WRITE)).doNotBlock().build(); HeldLocksToken token1 = server.lockWithFullLockResponse(client, requestWrite).getToken(); Assert.assertNotNull(token1); server.unlock(validToken); requestWrite = LockRequest.builder(ImmutableSortedMap.of(lock1, LockMode.WRITE)).build(); token1 = server.lockWithFullLockResponse(client, requestWrite).getToken(); Assert.assertNotNull(token1); Future<?> future = executor.submit((Callable<Void>) () -> { HeldLocksToken validToken1 = server.lockWithFullLockResponse(LockClient.ANONYMOUS, LockRequest.builder( ImmutableSortedMap.of(lock1, LockMode.WRITE)).build()).getToken(); Assert.assertNotNull(validToken1); Assert.assertEquals(LockClient.ANONYMOUS, validToken1.getClient());
.doNotBlock().build(); LockResponse response = server.lockWithFullLockResponse(client, request); Assert.assertTrue(response.success()); .lockAsManyAsPossible() .blockForAtMost(SimpleTimeDuration.of(50, TimeUnit.MILLISECONDS)).build(); response = server.lockWithFullLockResponse(LockClient.ANONYMOUS, request); Assert.assertTrue(response.success()); .lockAsManyAsPossible() .blockForAtMost(SimpleTimeDuration.of(50, TimeUnit.MILLISECONDS)).build(); response = server.lockWithFullLockResponse(LockClient.ANONYMOUS, request); Assert.assertTrue(response.success()); .lockAsManyAsPossible() .blockForAtMost(SimpleTimeDuration.of(50, TimeUnit.MILLISECONDS)).build(); response = server.lockWithFullLockResponse(LockClient.ANONYMOUS, request); Assert.assertTrue(response.success()); .lockAsManyAsPossible() .blockForAtMost(SimpleTimeDuration.of(50, TimeUnit.MILLISECONDS)).build(); response = server.lockWithFullLockResponse(LockClient.ANONYMOUS, request); Assert.assertFalse(response.success());
long lockTimeoutMs = LockRequest.getDefaultLockTimeout().toMillis(); LockRequest request = LockRequest.builder(ImmutableSortedMap.of( lock1, LockMode.READ, lock2, LockMode.WRITE)).withLockedInVersionId(10).build(); long currentTimeMs = System.currentTimeMillis(); LockResponse response = server.lockWithFullLockResponse(client, request); barrier.await(); HeldLocksToken validToken = server.lockWithFullLockResponse(LockClient.ANONYMOUS, LockRequest.builder( ImmutableSortedMap.of(lock2, LockMode.READ)).build()).getToken(); Assert.assertNotNull(validToken); server.unlock(validToken); ImmutableSortedMap.of(lock1, LockMode.READ)).build()).getToken(); Assert.assertNotNull(anonymousReadToken); server.unlock(anonymousReadToken); .withLockedInVersionId(5).build()).getToken(); Assert.assertNotNull(token2); ImmutableSortedMap.of(lock2, LockMode.WRITE)).build()).getToken(); Assert.assertNotNull(token3);
LockRequest request = LockRequest.builder(ImmutableSortedMap.of( lock1, LockMode.READ, lock2, LockMode.WRITE)) .withLockedInVersionId(10).doNotBlock().build(); long currentTimeMs = System.currentTimeMillis(); LockResponse response = server.lockWithFullLockResponse(client, request); ImmutableSortedMap.of(lock2, LockMode.READ)).doNotBlock().build()).getToken(); Assert.assertNull(nullToken); ImmutableSortedMap.of(lock2, LockMode.WRITE)).doNotBlock().build()).getToken(); Assert.assertNull(nullToken); .doNotBlock().build()).getToken(); Assert.assertNotNull(anonymousReadToken); server.unlock(anonymousReadToken); .withLockedInVersionId(5).doNotBlock().build()).getToken(); Assert.assertNotNull(token2); ImmutableSortedMap.of(lock2, LockMode.WRITE)).doNotBlock().build()).getToken(); Assert.assertNotNull(token3);
.blockForAtMost(SimpleTimeDuration.of(1000, TimeUnit.MILLISECONDS)) .build(); LockRequest request2 = LockRequest.builder( LockCollections.of(ImmutableSortedMap.of(DESCRIPTOR_2, LockMode.WRITE))) .blockForAtMost(SimpleTimeDuration.of(1000, TimeUnit.MILLISECONDS)) .build();
if (!mapToAssertLockHeld.isEmpty()) { LockRequest request = LockRequest.builder(mapToAssertLockHeld) .doNotBlock() .lockAsManyAsPossible() .build(); LockRefreshToken lock = lockService.lock(LockClient.ANONYMOUS.getClientId(), request); Validate.isTrue(lock == null, "these should already be held"); LockRequest request = LockRequest.builder(mapToAssertLockNotHeld).doNotBlock().build(); LockRefreshToken lock = lockService.lock(LockClient.ANONYMOUS.getClientId(), request); Validate.isTrue(lock != null, "these should already be waited for");