Search in sources :

Example 1 with OOMGuard

use of jetbrains.exodus.runtime.OOMGuard in project xodus by JetBrains.

the class GarbageCollector method doCleanFiles.

private boolean doCleanFiles(@NotNull final Iterator<Long> fragmentedFiles) {
    // if there are no more files then even don't start a txn
    if (!fragmentedFiles.hasNext()) {
        return true;
    }
    final LongSet cleanedFiles = new PackedLongHashSet();
    final ReadWriteTransaction txn;
    try {
        final TransactionBase tx = useRegularTxn ? env.beginTransaction() : env.beginGCTransaction();
        // tx can be read-only, so we should manually finish it (see XD-667)
        if (tx.isReadonly()) {
            tx.abort();
            return false;
        }
        txn = (ReadWriteTransaction) tx;
    } catch (TransactionAcquireTimeoutException ignore) {
        return false;
    }
    final boolean isTxnExclusive = txn.isExclusive();
    try {
        final OOMGuard guard = new OOMGuard();
        final long started = System.currentTimeMillis();
        while (fragmentedFiles.hasNext()) {
            final long fileAddress = fragmentedFiles.next();
            cleanSingleFile(fileAddress, txn);
            cleanedFiles.add(fileAddress);
            if (!isTxnExclusive) {
                // do not process more than one file in a non-exclusive txn
                break;
            }
            if (started + ec.getGcTransactionTimeout() < System.currentTimeMillis()) {
                // break by timeout
                break;
            }
            if (guard.isItCloseToOOM()) {
                // break because of the risk of OutOfMemoryError
                break;
            }
        }
        if (!txn.forceFlush()) {
            // paranoiac check
            if (isTxnExclusive) {
                throw new ExodusException("Can't be: exclusive txn should be successfully flushed");
            }
            return false;
        }
    } catch (Throwable e) {
        throw ExodusException.toExodusException(e);
    } finally {
        txn.abort();
    }
    if (!cleanedFiles.isEmpty()) {
        for (final Long file : cleanedFiles) {
            pendingFilesToDelete.add(file);
            utilizationProfile.removeFile(file);
        }
        utilizationProfile.estimateTotalBytes();
        env.executeTransactionSafeTask(new Runnable() {

            @Override
            public void run() {
                final int filesDeletionDelay = ec.getGcFilesDeletionDelay();
                if (filesDeletionDelay == 0) {
                    for (final Long file : cleanedFiles) {
                        deletionQueue.offer(file);
                    }
                } else {
                    DeferredIO.getJobProcessor().queueIn(new Job() {

                        @Override
                        protected void execute() {
                            for (final Long file : cleanedFiles) {
                                deletionQueue.offer(file);
                            }
                        }
                    }, filesDeletionDelay);
                }
            }
        });
    }
    return true;
}
Also used : LongSet(jetbrains.exodus.core.dataStructures.hash.LongSet) PackedLongHashSet(jetbrains.exodus.core.dataStructures.hash.PackedLongHashSet) OOMGuard(jetbrains.exodus.runtime.OOMGuard) Job(jetbrains.exodus.core.execution.Job) ExodusException(jetbrains.exodus.ExodusException)

Aggregations

ExodusException (jetbrains.exodus.ExodusException)1 LongSet (jetbrains.exodus.core.dataStructures.hash.LongSet)1 PackedLongHashSet (jetbrains.exodus.core.dataStructures.hash.PackedLongHashSet)1 Job (jetbrains.exodus.core.execution.Job)1 OOMGuard (jetbrains.exodus.runtime.OOMGuard)1