Search in sources :

Example 1 with RetryLaterRuntimeException

use of de.invesdwin.context.integration.retry.RetryLaterRuntimeException in project invesdwin-context-persistence by subes.

the class ATimeSeriesUpdater method update.

public final boolean update() throws IncompleteUpdateFoundException {
    try {
        if (!table.getTableLock(key).writeLock().tryLock(1, TimeUnit.MINUTES)) {
            throw new RetryLaterRuntimeException("Write lock could not be acquired for table [" + table.getName() + "] and key [" + key + "]. Please ensure all iterators are closed!");
        }
    } catch (final InterruptedException e1) {
        throw new RuntimeException(e1);
    }
    try {
        if (updateLockFile.exists()) {
            throw new IncompleteUpdateFoundException("Incomplete update found for table [" + table.getName() + "], need to clean everything up to restore all from scratch.");
        }
        try {
            FileUtils.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;
    } finally {
        table.getTableLock(key).writeLock().unlock();
    }
}
Also used : RetryLaterRuntimeException(de.invesdwin.context.integration.retry.RetryLaterRuntimeException) Instant(de.invesdwin.util.time.Instant) RetryLaterRuntimeException(de.invesdwin.context.integration.retry.RetryLaterRuntimeException) IOException(java.io.IOException)

Example 2 with RetryLaterRuntimeException

use of de.invesdwin.context.integration.retry.RetryLaterRuntimeException in project invesdwin-context-persistence by subes.

the class ADelegateTable method initializeTableLocked.

private void initializeTableLocked() {
    if (tableFinalizer.table == null) {
        if (getTableCreationTime() == null) {
            if (getPersistenceMode().isDisk()) {
                try {
                    Files.touch(timestampFile);
                } catch (final IOException e) {
                    throw Err.process(e);
                }
            }
            tableCreationTime = new FDate();
        }
        try {
            tableFinalizer.table = db.getTable(name);
            if (tableFinalizer.table == null) {
                throw new IllegalStateException("table should not be null");
            }
            tableFinalizer.register(this);
            RangeTableCloseManager.register(this);
        } catch (final Throwable e) {
            if (Strings.containsIgnoreCase(e.getMessage(), "LOCK")) {
                // at de.invesdwin.context.persistence.leveldb.ADelegateRangeTable.getTableWithReadLock(ADelegateRangeTable.java:144)
                throw new RetryLaterRuntimeException(e);
            } else {
                Err.process(new RuntimeException("Table data for [" + getDirectory() + "/" + getName() + "] is inconsistent. Resetting data and trying again.", e));
                innerDeleteTable();
                tableFinalizer.table = db.getRangeTable(name);
                if (tableFinalizer.table == null) {
                    throw new IllegalStateException("table should not be null");
                }
                tableFinalizer.register(this);
            }
        }
    }
}
Also used : RetryLaterRuntimeException(de.invesdwin.context.integration.retry.RetryLaterRuntimeException) RetryLaterRuntimeException(de.invesdwin.context.integration.retry.RetryLaterRuntimeException) IOException(java.io.IOException) FDate(de.invesdwin.util.time.date.FDate)

Example 3 with RetryLaterRuntimeException

use of de.invesdwin.context.integration.retry.RetryLaterRuntimeException in project invesdwin-context-persistence by subes.

the class ADelegateRangeTable method initializeTableLocked.

private void initializeTableLocked() {
    if (tableFinalizer.table == null) {
        if (getTableCreationTime() == null) {
            if (getPersistenceMode().isDisk()) {
                try {
                    Files.touch(timestampFile);
                } catch (final IOException e) {
                    throw Err.process(e);
                }
            }
            tableCreationTime = new FDate();
        }
        try {
            tableFinalizer.table = db.getRangeTable(name);
            if (tableFinalizer.table == null) {
                throw new IllegalStateException("table should not be null");
            }
            tableFinalizer.register(this);
            RangeTableCloseManager.register(this);
        } catch (final Throwable e) {
            if (Strings.containsIgnoreCase(e.getMessage(), "LOCK")) {
                // at de.invesdwin.context.persistence.leveldb.ADelegateRangeTable.getTableWithReadLock(ADelegateRangeTable.java:144)
                throw new RetryLaterRuntimeException(e);
            } else {
                Err.process(new RuntimeException("Table data for [" + getDirectory() + "/" + getName() + "] is inconsistent. Resetting data and trying again.", e));
                innerDeleteTable();
                tableFinalizer.table = db.getRangeTable(name);
                if (tableFinalizer.table == null) {
                    throw new IllegalStateException("table should not be null");
                }
                tableFinalizer.register(this);
            }
        }
    }
}
Also used : RetryLaterRuntimeException(de.invesdwin.context.integration.retry.RetryLaterRuntimeException) RetryLaterRuntimeException(de.invesdwin.context.integration.retry.RetryLaterRuntimeException) IOException(java.io.IOException) FDate(de.invesdwin.util.time.date.FDate)

Example 4 with RetryLaterRuntimeException

use of de.invesdwin.context.integration.retry.RetryLaterRuntimeException 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)

Example 5 with RetryLaterRuntimeException

use of de.invesdwin.context.integration.retry.RetryLaterRuntimeException in project invesdwin-context-persistence by subes.

the class ASegmentedTimeSeriesStorageCache method initSegmentRetry.

private void initSegmentRetry(final SegmentedKey<K> segmentedKey, final Function<SegmentedKey<K>, ICloseableIterable<? extends V>> source) {
    final ARetryCallable<Throwable> retryTask = new ARetryCallable<Throwable>(new RetryOriginator(ASegmentedTimeSeriesDB.class, "initSegment", segmentedKey)) {

        @Override
        protected Throwable callRetry() throws Exception {
            try {
                if (closed) {
                    return new RetryLaterRuntimeException(ASegmentedTimeSeriesStorageCache.class.getSimpleName() + "for [" + hashKey + "] is already closed.");
                } else {
                    initSegment(segmentedKey, source);
                }
                return null;
            } catch (final Throwable t) {
                if (closed) {
                    return t;
                } else {
                    throw t;
                }
            }
        }

        @Override
        protected BackOffPolicy getBackOffPolicyOverride() {
            // randomize backoff to prevent race conditions between multiple processes
            return BackOffPolicies.randomFixedBackOff(Duration.ONE_SECOND);
        }
    };
    final Throwable t = retryTask.call();
    if (t != null) {
        throw Throwables.propagate(t);
    }
}
Also used : RetryOriginator(de.invesdwin.context.integration.retry.task.RetryOriginator) RetryLaterRuntimeException(de.invesdwin.context.integration.retry.RetryLaterRuntimeException) ARetryCallable(de.invesdwin.context.integration.retry.task.ARetryCallable)

Aggregations

RetryLaterRuntimeException (de.invesdwin.context.integration.retry.RetryLaterRuntimeException)9 IOException (java.io.IOException)4 FDate (de.invesdwin.util.time.date.FDate)3 File (java.io.File)3 ARetryCallable (de.invesdwin.context.integration.retry.task.ARetryCallable)2 IncompleteUpdateFoundException (de.invesdwin.context.persistence.timeseriesdb.IncompleteUpdateFoundException)2 ILock (de.invesdwin.util.concurrent.lock.ILock)2 TextDescription (de.invesdwin.util.lang.description.TextDescription)2 PooledFastByteArrayOutputStream (de.invesdwin.util.streams.pool.PooledFastByteArrayOutputStream)2 Instant (de.invesdwin.util.time.Instant)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 RetryOriginator (de.invesdwin.context.integration.retry.task.RetryOriginator)1 SerializingCollection (de.invesdwin.context.persistence.timeseriesdb.SerializingCollection)1 ATimeSeriesUpdater (de.invesdwin.context.persistence.timeseriesdb.updater.ATimeSeriesUpdater)1 FileChannelLock (de.invesdwin.util.concurrent.lock.FileChannelLock)1 IReadWriteLock (de.invesdwin.util.concurrent.lock.readwrite.IReadWriteLock)1 TaskInfoCallable (de.invesdwin.util.concurrent.taskinfo.provider.TaskInfoCallable)1 ISerde (de.invesdwin.util.marshallers.serde.ISerde)1