@Override public HeldLocksToken lockAndGetHeldLocks(String client, LockRequest request) throws InterruptedException { return wrapper.applyWithPermit(lockService -> lockService.lockAndGetHeldLocks(client, request)); }
private static HeldLocksToken acquireLock(LockService lockService, LockRequest lockRequest) { int failureCount = 0; while (true) { HeldLocksToken response; try { response = lockService.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), lockRequest); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } if (response == null) { RuntimeException ex = new LockAcquisitionException( "Failed to lock using the provided lock request: " + lockRequest); log.warn("Could not lock successfully", ex); ++failureCount; if (failureCount >= NUM_RETRIES) { log.warn("Failing after {} tries", failureCount, ex); throw ex; } } else { return response; } } }
@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 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); }
private static HeldLocksToken acquireLock(LockService lockService, LockRequest lockRequest) { int failureCount = 0; while (true) { HeldLocksToken response; try { response = lockService.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), lockRequest); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } if (response == null) { RuntimeException ex = new LockAcquisitionException( "Failed to lock using the provided lock request: " + lockRequest); log.warn("Could not lock successfully", ex); ++failureCount; if (failureCount >= NUM_RETRIES) { log.warn("Failing after {} tries", failureCount, ex); throw ex; } } else { return response; } } }
/** 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()); }