Search in sources :

Example 1 with FileChannelLock

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();
    }
}
Also used : FileChannelLock(de.invesdwin.util.concurrent.lock.FileChannelLock) RetryLaterRuntimeException(de.invesdwin.context.integration.retry.RetryLaterRuntimeException) Instant(de.invesdwin.util.time.Instant) RetryLaterRuntimeException(de.invesdwin.context.integration.retry.RetryLaterRuntimeException) IOException(java.io.IOException) ILock(de.invesdwin.util.concurrent.lock.ILock) IncompleteUpdateFoundException(de.invesdwin.context.persistence.timeseriesdb.IncompleteUpdateFoundException) File(java.io.File)

Aggregations

RetryLaterRuntimeException (de.invesdwin.context.integration.retry.RetryLaterRuntimeException)1 IncompleteUpdateFoundException (de.invesdwin.context.persistence.timeseriesdb.IncompleteUpdateFoundException)1 FileChannelLock (de.invesdwin.util.concurrent.lock.FileChannelLock)1 ILock (de.invesdwin.util.concurrent.lock.ILock)1 Instant (de.invesdwin.util.time.Instant)1 File (java.io.File)1 IOException (java.io.IOException)1