static OrderedLocks fromSingleLock(AsyncLock lock) { return fromOrderedList(ImmutableList.of(lock)); }
private AsyncResult<HeldLocks> acquireImmutableTimestampLock(UUID requestId, long timestamp) { AsyncLock immutableTsLock = immutableTsTracker.getLockFor(timestamp); return lockAcquirer.acquireLocks(requestId, OrderedLocks.fromSingleLock(immutableTsLock), TimeLimit.zero()); }
private void timeoutAll() { for (AsyncLock lock : locks.get()) { lock.timeout(requestId); } } }
private void unlockAll() { try { for (AsyncLock lock : locks.get()) { lock.unlock(requestId); } } catch (Throwable t) { log.error("Error while unlocking locks", SafeArg.of("requestId", requestId), t); } }
public OrderedLocks getAll(Set<LockDescriptor> descriptors) { List<LockDescriptor> orderedDescriptors = sort(descriptors); List<AsyncLock> locks = Lists.newArrayListWithExpectedSize(descriptors.size()); for (LockDescriptor descriptor : orderedDescriptors) { locks.add(getLock(descriptor)); } return OrderedLocks.fromOrderedList(locks); }
@Before public void before() { when(acquirer.acquireLocks(any(), any(), any())).thenReturn(new AsyncResult<>()); when(acquirer.waitForLocks(any(), any(), any())).thenReturn(new AsyncResult<>()); when(locks.getAll(any())).thenReturn(OrderedLocks.fromSingleLock(newLock())); when(immutableTimestampTracker.getImmutableTimestamp()).thenReturn(Optional.empty()); when(immutableTimestampTracker.getLockFor(anyLong())).thenReturn(newLock()); }
private void acquireLocks() { try { AsyncResult<Void> lockResult = AsyncResult.completedResult(); for (AsyncLock lock : locks.get()) { lockResult = lockResult.concatWith(() -> lockFunction.apply(lock)); } this.result = lockResult; } catch (Throwable t) { log.error("Error while acquiring locks"); unlockAll(); throw Throwables.propagate(t); } }
private OrderedLocks orderedLocks(AsyncLock... orderedLocks) { return OrderedLocks.fromOrderedList(ImmutableList.copyOf(orderedLocks)); }
private AsyncResult<HeldLocks> acquireImmutableTimestampLock(UUID requestId, long timestamp) { AsyncLock immutableTsLock = immutableTsTracker.getLockFor(timestamp); return lockAcquirer.acquireLocks(requestId, OrderedLocks.fromSingleLock(immutableTsLock), TimeLimit.zero()); }
@Test public void returnsLocksInOrder() { List<LockDescriptor> orderedDescriptors = IntStream.range(0, 10) .mapToObj(i -> UUID.randomUUID().toString()) .map(StringLockDescriptor::of) .sorted().collect(Collectors.toList()); List<AsyncLock> expectedOrder = orderedDescriptors.stream() .map(descriptor -> lockCollection.getAll(ImmutableSet.of(descriptor))) .map(orderedLocks -> orderedLocks.get().get(0)) .collect(Collectors.toList()); List<AsyncLock> actualOrder = lockCollection.getAll(ImmutableSet.copyOf(orderedDescriptors)).get(); assertThat(actualOrder).isEqualTo(expectedOrder); }
private AsyncResult<Void> waitFor(AsyncLock... locks) { return lockAcquirer.waitForLocks(REQUEST_ID, OrderedLocks.fromOrderedList(ImmutableList.copyOf(locks)), TIMEOUT); }
@Test public void createsLocksOnDemand() { Set<LockDescriptor> descriptors = descriptors("foo", "bar"); List<AsyncLock> locks = lockCollection.getAll(descriptors).get(); assertThat(locks.size()).isEqualTo(2); assertThat(ImmutableSet.copyOf(locks).size()).isEqualTo(2); }
private AsyncResult<HeldLocks> acquire(List<AsyncLock> locks) { return lockAcquirer.acquireLocks(REQUEST_ID, OrderedLocks.fromOrderedList(locks), TIMEOUT); }
public AsyncResult<HeldLocks> acquireLocks(UUID requestId, OrderedLocks locks, TimeLimit timeout) { return new Acquisition(requestId, locks, timeout, lock -> lock.lock(requestId)).execute() .map(ignored -> new HeldLocks(lockLog, locks.get(), requestId)); }
static OrderedLocks fromSingleLock(AsyncLock lock) { return fromOrderedList(ImmutableList.of(lock)); }
public OrderedLocks getAll(Set<LockDescriptor> descriptors) { List<LockDescriptor> orderedDescriptors = sort(descriptors); List<AsyncLock> locks = Lists.newArrayListWithExpectedSize(descriptors.size()); for (LockDescriptor descriptor : orderedDescriptors) { locks.add(getLock(descriptor)); } return OrderedLocks.fromOrderedList(locks); }
private void timeoutAll() { for (AsyncLock lock : locks.get()) { lock.timeout(requestId); } } }
private void unlockAll() { try { for (AsyncLock lock : locks.get()) { lock.unlock(requestId); } } catch (Throwable t) { log.error("Error while unlocking locks", SafeArg.of("requestId", requestId), t); } }
private void acquireLocks() { try { AsyncResult<Void> lockResult = AsyncResult.completedResult(); for (AsyncLock lock : locks.get()) { lockResult = lockResult.concatWith(() -> lockFunction.apply(lock)); } this.result = lockResult; } catch (Throwable t) { log.error("Error while acquiring locks"); unlockAll(); throw Throwables.propagate(t); } }