@JsonIgnore default LockToken getToken() { if (!wasSuccessful()) { throw new IllegalStateException("This lock response was not successful"); } return getTokenOrEmpty().get(); }
@Override public LockToken lock(String client, String lockName) throws InterruptedException { LockRequest lockRequest = LockRequest.of( ImmutableSet.of(StringLockDescriptor.of(lockName)), Long.MAX_VALUE, client); LockResponse lockResponse = timelockService.lock(lockRequest); Preconditions.checkState(lockResponse.wasSuccessful(), "Jepsen failed to lock a lock, but it would wait for Long.MAX_VALUE, so this is unexpected."); return lockResponse.getToken(); }
@Override public LockResponse lock(LockRequest request) { LockResponse response = executeOnTimeLock(() -> delegate.lock(request)); if (response.wasSuccessful()) { lockRefresher.registerLock(response.getToken()); } return response; }
@Theory public void shouldAcquireRowAndCellLockIfRequiresBoth(ConflictHandler conflictHandler) { Assume.assumeTrue(conflictHandler.lockCellsForConflicts() && conflictHandler.lockRowsForConflicts()); PreCommitCondition cellAndRowLockAcquired = (ignored) -> { LockResponse cellLockResponse = acquireCellLock(ROW, COLUMN); LockResponse rowLockResponse = acquireRowLock(ROW); assertFalse(cellLockResponse.wasSuccessful()); assertFalse(rowLockResponse.wasSuccessful()); }; commitWriteWith(cellAndRowLockAcquired, conflictHandler); }
@Theory public void shouldNotAcquireCellLockIfDoesNotLockAtCellLevel(ConflictHandler conflictHandler) { Assume.assumeFalse(conflictHandler.lockCellsForConflicts()); PreCommitCondition canAcquireCellLock = (ignored) -> { LockResponse response = acquireCellLock(ROW, COLUMN); //current lock implementation allows you to get a cell lock on a row that is already locked assertTrue(response.wasSuccessful()); }; commitWriteWith(canAcquireCellLock, conflictHandler); }
@Theory public void shouldAcquireRowLockIfLocksAtRowLevel(ConflictHandler conflictHandler) { Assume.assumeTrue(conflictHandler.lockRowsForConflicts()); PreCommitCondition rowLocksAcquired = (ignored) -> { LockResponse response = acquireRowLock(ROW); assertFalse(response.wasSuccessful()); }; commitWriteWith(rowLocksAcquired, conflictHandler); }
@Theory public void shouldNotAcquireRowLockIfDoesNotLockAtRowLevel(ConflictHandler conflictHandler) { Assume.assumeFalse(conflictHandler.lockRowsForConflicts()); PreCommitCondition canAcquireRowLock = (ignored) -> { LockResponse response = acquireRowLock(ROW); assertTrue(response.wasSuccessful()); }; commitWriteWith(canAcquireRowLock, conflictHandler); }
@Theory public void shouldAcquireCellLockIfLocksAtCellLevel(ConflictHandler conflictHandler) { Assume.assumeTrue(conflictHandler.lockCellsForConflicts()); PreCommitCondition cellLocksAcquired = (ignored) -> { LockResponse response = acquireCellLock(ROW, COLUMN); assertFalse(response.wasSuccessful()); }; commitWriteWith(cellLocksAcquired, conflictHandler); }
@Theory public void canAcquireLockOnMultipleCellsOnSameRow(ConflictHandler conflictHandler) { Assume.assumeTrue(conflictHandler.lockCellsForConflicts()); PreCommitCondition canAcquireLockOnDifferentCell = (ignored) -> { LockResponse response = acquireCellLock(ROW, OTHER_COLUMN); assertTrue(response.wasSuccessful()); }; commitWriteWith(canAcquireLockOnDifferentCell, conflictHandler); }
/** * This method should acquire any locks needed to do proper concurrency control at commit time. */ protected LockToken acquireLocksForCommit() { Set<LockDescriptor> lockDescriptors = getLocksForWrites(); LockRequest request = LockRequest.of(lockDescriptors, transactionConfig.get().getLockAcquireTimeoutMillis()); LockResponse lockResponse = timelockService.lock(request); if (!lockResponse.wasSuccessful()) { log.error("Timed out waiting while acquiring commit locks. Request id was {}. Timeout was {} ms. " + "First ten required locks were {}.", SafeArg.of("requestId", request.getRequestId()), SafeArg.of("acquireTimeoutMs", transactionConfig.get().getLockAcquireTimeoutMillis()), UnsafeArg.of("firstTenLockDescriptors", Iterables.limit(lockDescriptors, 10))); throw new TransactionLockAcquisitionTimeoutException("Timed out while acquiring commit locks."); } return lockResponse.getToken(); }
@JsonIgnore default LockToken getToken() { if (!wasSuccessful()) { throw new IllegalStateException("This lock response was not successful"); } return getTokenOrEmpty().get(); }
@Override public LockResponse lock(LockRequest request) { LockResponse response = executeOnTimeLock(() -> delegate.lock(request)); if (response.wasSuccessful()) { lockRefresher.registerLock(response.getToken()); } return response; }
@Theory public void shouldAcquireRowAndCellLockIfRequiresBoth(ConflictHandler conflictHandler) { Assume.assumeTrue(conflictHandler.lockCellsForConflicts() && conflictHandler.lockRowsForConflicts()); PreCommitCondition cellAndRowLockAcquired = (ignored) -> { LockResponse cellLockResponse = acquireCellLock(ROW, COLUMN); LockResponse rowLockResponse = acquireRowLock(ROW); assertFalse(cellLockResponse.wasSuccessful()); assertFalse(rowLockResponse.wasSuccessful()); }; commitWriteWith(cellAndRowLockAcquired, conflictHandler); }
@Theory public void shouldNotAcquireCellLockIfDoesNotLockAtCellLevel(ConflictHandler conflictHandler) { Assume.assumeFalse(conflictHandler.lockCellsForConflicts()); PreCommitCondition canAcquireCellLock = (ignored) -> { LockResponse response = acquireCellLock(ROW, COLUMN); //current lock implementation allows you to get a cell lock on a row that is already locked assertTrue(response.wasSuccessful()); }; commitWriteWith(canAcquireCellLock, conflictHandler); }
@Theory public void canAcquireLockOnMultipleCellsOnSameRow(ConflictHandler conflictHandler) { Assume.assumeTrue(conflictHandler.lockCellsForConflicts()); PreCommitCondition canAcquireLockOnDifferentCell = (ignored) -> { LockResponse response = acquireCellLock(ROW, OTHER_COLUMN); assertTrue(response.wasSuccessful()); }; commitWriteWith(canAcquireLockOnDifferentCell, conflictHandler); }
@Theory public void shouldAcquireCellLockIfLocksAtCellLevel(ConflictHandler conflictHandler) { Assume.assumeTrue(conflictHandler.lockCellsForConflicts()); PreCommitCondition cellLocksAcquired = (ignored) -> { LockResponse response = acquireCellLock(ROW, COLUMN); assertFalse(response.wasSuccessful()); }; commitWriteWith(cellLocksAcquired, conflictHandler); }
@Theory public void shouldNotAcquireRowLockIfDoesNotLockAtRowLevel(ConflictHandler conflictHandler) { Assume.assumeFalse(conflictHandler.lockRowsForConflicts()); PreCommitCondition canAcquireRowLock = (ignored) -> { LockResponse response = acquireRowLock(ROW); assertTrue(response.wasSuccessful()); }; commitWriteWith(canAcquireRowLock, conflictHandler); }
@Theory public void shouldAcquireRowLockIfLocksAtRowLevel(ConflictHandler conflictHandler) { Assume.assumeTrue(conflictHandler.lockRowsForConflicts()); PreCommitCondition rowLocksAcquired = (ignored) -> { LockResponse response = acquireRowLock(ROW); assertFalse(response.wasSuccessful()); }; commitWriteWith(rowLocksAcquired, conflictHandler); }
/** * This method should acquire any locks needed to do proper concurrency control at commit time. */ protected LockToken acquireLocksForCommit() { Set<LockDescriptor> lockDescriptors = getLocksForWrites(); LockRequest request = LockRequest.of(lockDescriptors, transactionConfig.get().getLockAcquireTimeoutMillis()); LockResponse lockResponse = timelockService.lock(request); if (!lockResponse.wasSuccessful()) { log.error("Timed out waiting while acquiring commit locks. Request id was {}. Timeout was {} ms. " + "First ten required locks were {}.", SafeArg.of("requestId", request.getRequestId()), SafeArg.of("acquireTimeoutMs", transactionConfig.get().getLockAcquireTimeoutMillis()), UnsafeArg.of("firstTenLockDescriptors", Iterables.limit(lockDescriptors, 10))); throw new TransactionLockAcquisitionTimeoutException("Timed out while acquiring commit locks."); } return lockResponse.getToken(); }