use of de.invesdwin.util.concurrent.lock.FileChannelLock in project invesdwin-context-persistence by subes.
the class ATimeSeriesUpdater method update.
@Override
public final boolean update() throws IncompleteUpdateFoundException {
final ILock writeLock = table.getTableLock(key).writeLock();
try {
if (!writeLock.tryLock(1, TimeUnit.MINUTES)) {
throw Locks.getLockTrace().handleLockException(writeLock.getName(), new RetryLaterRuntimeException("Write lock could not be acquired for table [" + table.getName() + "] and key [" + key + "]. Please ensure all iterators are closed!"));
}
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
final File updateLockSyncFile = new File(updateLockFile.getAbsolutePath() + ".sync");
try (FileChannelLock updateLockSyncFileLock = new FileChannelLock(updateLockSyncFile)) {
if (updateLockSyncFile.exists() || !updateLockSyncFileLock.tryLock() || updateLockFile.exists()) {
throw new IncompleteUpdateFoundException("Incomplete update found for table [" + table.getName() + "] and key [" + key + "], need to clean everything up to restore all from scratch.");
}
try {
try {
Files.touch(updateLockFile);
} catch (final IOException e) {
throw new RuntimeException(e);
}
final Instant updateStart = new Instant();
onUpdateStart();
doUpdate();
onUpdateFinished(updateStart);
Assertions.assertThat(updateLockFile.delete()).isTrue();
return true;
} catch (final Throwable t) {
final IncompleteUpdateFoundException incompleteException = Throwables.getCauseByType(t, IncompleteUpdateFoundException.class);
if (incompleteException != null) {
throw incompleteException;
} else {
throw new IncompleteUpdateFoundException("Something unexpected went wrong", t);
}
}
} finally {
writeLock.unlock();
}
}
Aggregations