private void logUnlockException(Throwable th, Optional<TargetedSweeperLock> maybeLock) { if (maybeLock.isPresent()) { log.info("Failed to unlock targeted sweep lock for {}.", SafeArg.of("shardStrategy", maybeLock.get().getShardAndStrategy().toText()), th); } else { log.info("Failed to unlock targeted sweep lock for sweep strategy {}.", SafeArg.of("sweepStrategy", sweepStrategy), th); } }
private void logException(Throwable th, Optional<TargetedSweeperLock> maybeLock) { if (maybeLock.isPresent()) { log.warn("Targeted sweep for {} failed and will be retried later.", SafeArg.of("shardStrategy", maybeLock.get().getShardAndStrategy().toText()), th); } else { log.warn("Targeted sweep for sweep strategy {} failed and will be retried later.", SafeArg.of("sweepStrategy", sweepStrategy), th); } }
private void runOneIteration() { Optional<TargetedSweeperLock> maybeLock = Optional.empty(); try { maybeLock = tryToAcquireLockForNextShardAndStrategy(); maybeLock.ifPresent(lock -> sweepNextBatch(lock.getShardAndStrategy())); } catch (InsufficientConsistencyException e) { metrics.registerOccurrenceOf(SweepOutcome.NOT_ENOUGH_DB_NODES_ONLINE); logException(e, maybeLock); } catch (Throwable th) { metrics.registerOccurrenceOf(SweepOutcome.ERROR); logException(th, maybeLock); } finally { try { maybeLock.ifPresent(TargetedSweeperLock::unlock); } catch (Throwable th) { logUnlockException(th, maybeLock); } } }
@Test public void successfulLockAndUnlock() throws InterruptedException { LockToken lockToken = LockToken.of(UUID.randomUUID()); when(mockLockService.lock(any())) .thenReturn(() -> Optional.of(lockToken)); Optional<TargetedSweeperLock> maybeLock = TargetedSweeperLock .tryAcquire(1, TableMetadataPersistence.SweepStrategy.CONSERVATIVE, mockLockService); assertThat(maybeLock).isPresent(); TargetedSweeperLock lock = maybeLock.get(); assertThat(lock.getShardAndStrategy()).isEqualTo(ShardAndStrategy.conservative(1)); lock.unlock(); verify(mockLockService, times(1)).unlock(ImmutableSet.of(lockToken)); verify(mockLockService, times(1)).lock(any()); verifyNoMoreInteractions(mockLockService); }
private void logUnlockException(Throwable th, Optional<TargetedSweeperLock> maybeLock) { if (maybeLock.isPresent()) { log.info("Failed to unlock targeted sweep lock for {}.", SafeArg.of("shardStrategy", maybeLock.get().getShardAndStrategy().toText()), th); } else { log.info("Failed to unlock targeted sweep lock for sweep strategy {}.", SafeArg.of("sweepStrategy", sweepStrategy), th); } }
private void logException(Throwable th, Optional<TargetedSweeperLock> maybeLock) { if (maybeLock.isPresent()) { log.warn("Targeted sweep for {} failed and will be retried later.", SafeArg.of("shardStrategy", maybeLock.get().getShardAndStrategy().toText()), th); } else { log.warn("Targeted sweep for sweep strategy {} failed and will be retried later.", SafeArg.of("sweepStrategy", sweepStrategy), th); } }
private void runOneIteration() { Optional<TargetedSweeperLock> maybeLock = Optional.empty(); try { maybeLock = tryToAcquireLockForNextShardAndStrategy(); maybeLock.ifPresent(lock -> sweepNextBatch(lock.getShardAndStrategy())); } catch (InsufficientConsistencyException e) { metrics.registerOccurrenceOf(SweepOutcome.NOT_ENOUGH_DB_NODES_ONLINE); logException(e, maybeLock); } catch (Throwable th) { metrics.registerOccurrenceOf(SweepOutcome.ERROR); logException(th, maybeLock); } finally { try { maybeLock.ifPresent(TargetedSweeperLock::unlock); } catch (Throwable th) { logUnlockException(th, maybeLock); } } }