@Test public void noOpPersistentLockCanLockTwice() { PersistentLockManager noOpManager = new PersistentLockManager( metricsManager, new NoOpPersistentLockService(), 0L); assertTrue("NoOpPersistentLockService should return true when acquiring lock", noOpManager.tryAcquirePersistentLock()); assertTrue("NoOpPersistentLockService should return true when acquiring lock for the second time", noOpManager.tryAcquirePersistentLock()); }
@VisibleForTesting synchronized boolean tryAcquirePersistentLock() { Preconditions.checkState(!isShutDown, "This PersistentLockManager is shut down, and cannot be used to acquire locks."); try { lockId = persistentLockService.acquireBackupLock("Sweep"); referenceCount++; log.info("Successfully acquired persistent lock for sweep: {}", SafeArg.of("lockId", lockId)); return true; } catch (CheckAndSetException e) { List<byte[]> actualValues = e.getActualValues(); if (!actualValues.isEmpty()) { LockEntry actualEntry = LockEntry.fromStoredValue(actualValues.get(0)); log.info("CAS failed on lock acquire. We thought the lockId was {}, and the database has {}", SafeArg.of("lockId", lockId), SafeArg.of("actualEntry", actualEntry)); if (lockId != null && actualEntry.instanceId().equals(lockId.value())) { referenceCount++; log.info("Attempted to acquire a new lock when we already held a lock." + " The acquire failed, but our lock is still valid, so we still hold the lock."); return true; } else { referenceCount = 0; lockId = null; } } lockFailureMeter.mark(); log.warn("Failed to acquire persistent lock for sweep. Waiting and retrying."); return false; } catch (NotInitializedException e) { log.info("The LockStore is not initialized yet. Waiting and retrying."); return false; } }
PersistentLockManager implements AutoCloseable { @VisibleForTesting synchronized boolean tryAcquirePersistentLock() { Preconditions.checkState(!isShutDown, "This PersistentLockManager is shut down, and cannot be used to acquire locks."); try { lockId = persistentLockService.acquireBackupLock("Sweep"); referenceCount++; log.info("Successfully acquired persistent lock for sweep: {}", SafeArg.of("lockId", lockId)); return true; } catch (CheckAndSetException e) { List<byte[]> actualValues = e.getActualValues(); if (!actualValues.isEmpty()) { LockEntry actualEntry = LockEntry.fromStoredValue(actualValues.get(0)); log.info("CAS failed on lock acquire. We thought the lockId was {}, and the database has {}", SafeArg.of("lockId", lockId), SafeArg.of("actualEntry", actualEntry)); if (lockId != null && actualEntry.instanceId().equals(lockId.value())) { referenceCount++; log.info("Attempted to acquire a new lock when we already held a lock." + " The acquire failed, but our lock is still valid, so we still hold the lock."); return true; } else { referenceCount = 0; lockId = null; } } lockFailureMeter.mark(); log.warn("Failed to acquire persistent lock for sweep. Waiting and retrying."); return false; } catch (NotInitializedException e) { log.info("The LockStore is not initialized yet. Waiting and retrying."); return false; } } }
PersistentLockManager implements AutoCloseable { @VisibleForTesting synchronized boolean tryAcquirePersistentLock() { Preconditions.checkState(!isShutDown, "This PersistentLockManager is shut down, and cannot be used to acquire locks."); try { lockId = persistentLockService.acquireBackupLock("Sweep"); referenceCount++; log.info("Successfully acquired persistent lock for sweep: {}", SafeArg.of("lockId", lockId)); return true; } catch (CheckAndSetException e) { List<byte[]> actualValues = e.getActualValues(); if (!actualValues.isEmpty()) { LockEntry actualEntry = LockEntry.fromStoredValue(actualValues.get(0)); log.info("CAS failed on lock acquire. We thought the lockId was {}, and the database has {}", SafeArg.of("lockId", lockId), SafeArg.of("actualEntry", actualEntry)); if (lockId != null && actualEntry.instanceId().equals(lockId.value())) { referenceCount++; log.info("Attempted to acquire a new lock when we already held a lock." + " The acquire failed, but our lock is still valid, so we still hold the lock."); return true; } else { referenceCount = 0; lockId = null; } } lockFailureMeter.mark(); log.warn("Failed to acquire persistent lock for sweep. Waiting and retrying."); return false; } catch (NotInitializedException e) { log.info("The LockStore is not initialized yet. Waiting and retrying."); return false; } } PersistentLockManager(MetricsManager metricsManager,
            PersistentLockService persistentLockService, long persistentLockRetryWaitMillis); }
PersistentLockManager implements AutoCloseable { @VisibleForTesting synchronized boolean tryAcquirePersistentLock() { Preconditions.checkState(!isShutDown, "This PersistentLockManager is shut down, and cannot be used to acquire locks."); try { lockId = persistentLockService.acquireBackupLock("Sweep"); referenceCount++; log.info("Successfully acquired persistent lock for sweep: {}", SafeArg.of("lockId", lockId)); return true; } catch (CheckAndSetException e) { List<byte[]> actualValues = e.getActualValues(); if (!actualValues.isEmpty()) { LockEntry actualEntry = LockEntry.fromStoredValue(actualValues.get(0)); log.info("CAS failed on lock acquire. We thought the lockId was {}, and the database has {}", SafeArg.of("lockId", lockId), SafeArg.of("actualEntry", actualEntry)); if (lockId != null && actualEntry.instanceId().equals(lockId.value())) { referenceCount++; log.info("Attempted to acquire a new lock when we already held a lock." + " The acquire failed, but our lock is still valid, so we still hold the lock."); return true; } else { referenceCount = 0; lockId = null; } } lockFailureMeter.mark(); log.warn("Failed to acquire persistent lock for sweep. Waiting and retrying."); return false; } catch (NotInitializedException e) { log.info("The LockStore is not initialized yet. Waiting and retrying."); return false; } } PersistentLockManager(MetricsManager metricsManager,
            PersistentLockService persistentLockService, long persistentLockRetryWaitMillis); @Override void close(); synchronized void shutdown(); void acquirePersistentLockWithRetry(); synchronized void releasePersistentLock(); }
PersistentLockManager implements AutoCloseable { @VisibleForTesting synchronized boolean tryAcquirePersistentLock() { Preconditions.checkState(!isShutDown, "This PersistentLockManager is shut down, and cannot be used to acquire locks."); try { lockId = persistentLockService.acquireBackupLock("Sweep"); referenceCount++; log.info("Successfully acquired persistent lock for sweep: {}", SafeArg.of("lockId", lockId)); return true; } catch (CheckAndSetException e) { List<byte[]> actualValues = e.getActualValues(); if (!actualValues.isEmpty()) { LockEntry actualEntry = LockEntry.fromStoredValue(actualValues.get(0)); log.info("CAS failed on lock acquire. We thought the lockId was {}, and the database has {}", SafeArg.of("lockId", lockId), SafeArg.of("actualEntry", actualEntry)); if (lockId != null && actualEntry.instanceId().equals(lockId.value())) { referenceCount++; log.info("Attempted to acquire a new lock when we already held a lock." + " The acquire failed, but our lock is still valid, so we still hold the lock."); return true; } else { referenceCount = 0; lockId = null; } } lockFailureMeter.mark(); log.warn("Failed to acquire persistent lock for sweep. Waiting and retrying."); return false; } catch (NotInitializedException e) { log.info("The LockStore is not initialized yet. Waiting and retrying."); return false; } } PersistentLockManager(MetricsManager metricsManager,
            PersistentLockService persistentLockService, long persistentLockRetryWaitMillis); @Override void close(); synchronized void shutdown(); void acquirePersistentLockWithRetry(); synchronized void releasePersistentLock();  }