/** * 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; }
if (!mapToAssertLockHeld.isEmpty()) { LockRequest request = LockRequest.builder(mapToAssertLockHeld) .doNotBlock() .lockAsManyAsPossible() .build(); 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");
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)); } }
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(); } }
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( 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(
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(); requestAllLocks = LockRequest.builder(lockMap).doNotBlock() .lockAsManyAsPossible().build(); token = server.lockWithFullLockResponse(client2, requestAllLocks).getToken();
LockRequest.builder(ImmutableSortedMap.of(lock1, LockMode.READ)).doNotBlock().doNotBlock() .build(); Assert.fail(); LockRequest.builder(ImmutableSortedMap.of(lock1, LockMode.READ)).doNotBlock() .blockForAtMost(SimpleTimeDuration.of(1, TimeUnit.MILLISECONDS)).build(); Assert.fail(); ImmutableSortedMap.of(lock1, LockMode.READ)).doNotBlock().build()).getToken(); Assert.assertNotNull(token); Assert.assertEquals(ImmutableSet.of(token), server.getTokens(client));
.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));
.doNotBlock().build(); long currentTimeMs = System.currentTimeMillis(); LockResponse response1 = server.lockWithFullLockResponse(LockClient.ANONYMOUS, request1);
.doNotBlock().build(); LockResponse response = server.lockWithFullLockResponse(client, request); Assert.assertTrue(response.success());
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);
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);
.build()), 100); LockRequest request = LockRequest.builder(ImmutableSortedMap.of(lock1, LockMode.WRITE)) .doNotBlock().timeoutAfter(SimpleTimeDuration.of(500, TimeUnit.MILLISECONDS)) .build(); HeldLocksToken token = server.lockWithFullLockResponse(client, request).getToken();
.doNotBlock().build(); LockRequest request2 = LockRequest.builder(ImmutableSortedMap.of(lock2, LockMode.READ)) .doNotBlock().build();
&& (request.getBlockingMode() != BlockingMode.BLOCK_INDEFINITELY_THEN_RELEASE)) { LockRequest.Builder newRequest = LockRequest.builder(request.getLockDescriptors()); newRequest.doNotBlock(); newRequest.timeoutAfter(request.getLockTimeout()); if (request.getVersionId() != null) {
private void runAndVerifyCli(Verifier verifier) throws Exception { try (SingleBackendCliTestRunner runner = makeRunner(cliArgs.toArray(new String[0]))) { TestAtlasDbServices services = runner.connect(moduleFactory); LockService lockService = services.getLockService(); TimestampService tss = services.getTimestampService(); LockClient client = services.getTestLockClient(); Clock clock = GlobalClock.create(lockService); long prePunch = clock.getTimeMillis(); punch(services, tss, clock); long postPunch = clock.getTimeMillis(); long immutableTs = tss.getFreshTimestamp(); LockRequest request = LockRequest.builder(ImmutableSortedMap.of(lock, LockMode.WRITE)) .withLockedInVersionId(immutableTs) .doNotBlock() .build(); LockRefreshToken token = lockService.lock(client.getClientId(), request); long lastFreshTs = tss.getFreshTimestamps(1000).getUpperBound(); verifier.verify(runner, tss, immutableTs, prePunch, postPunch, lastFreshTs, true); lockService.unlock(token); lastFreshTs = tss.getFreshTimestamps(1000).getUpperBound(); // there are no locks so we now expect immutable to just be a fresh runner.freshCommand(); verifier.verify(runner, tss, immutableTs, prePunch, postPunch, lastFreshTs, false); } }
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(); } }
/** 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()); }
@Override public LockRefreshToken lock(String client, String lockName) throws InterruptedException { LockDescriptor descriptor = StringLockDescriptor.of(lockName); LockRequest request = LockRequest.builder(ImmutableSortedMap.of(descriptor, LockMode.WRITE)) .doNotBlock() .build(); return lockService.lock(client, request); }