Search in sources :

Example 1 with ExodusException

use of jetbrains.exodus.ExodusException 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)

Example 2 with ExodusException

use of jetbrains.exodus.ExodusException in project xodus by JetBrains.

the class FileDataReader method removeBlock.

@Override
public void removeBlock(long blockAddress, @NotNull final RemoveBlockType rbt) {
    final File file = new File(dir, LogUtil.getLogFilename(blockAddress));
    removeFileFromFileCache(file);
    setWritable(file);
    final boolean deleted = rbt == RemoveBlockType.Delete ? file.delete() : renameFile(file);
    if (!deleted) {
        throw new ExodusException("Failed to delete " + file.getAbsolutePath());
    } else if (logger.isInfoEnabled()) {
        logger.info("Deleted file " + file.getAbsolutePath());
    }
}
Also used : SharedRandomAccessFile(jetbrains.exodus.util.SharedRandomAccessFile) File(java.io.File) ExodusException(jetbrains.exodus.ExodusException)

Example 3 with ExodusException

use of jetbrains.exodus.ExodusException in project xodus by JetBrains.

the class FileDataWriter method forceSync.

private static void forceSync(@NotNull final RandomAccessFile file) {
    try {
        final FileChannel channel = file.getChannel();
        channel.force(false);
    } catch (ClosedChannelException e) {
    // ignore
    } catch (IOException ioe) {
        if (file.getChannel().isOpen()) {
            throw new ExodusException(ioe);
        }
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) ExodusException(jetbrains.exodus.ExodusException)

Example 4 with ExodusException

use of jetbrains.exodus.ExodusException in project xodus by JetBrains.

the class Memory method dump.

public void dump(@NotNull final File location) {
    location.mkdirs();
    final ObjectProcedure<Map.Entry<Long, Block>> saver = new ObjectProcedure<Map.Entry<Long, Block>>() {

        @Override
        public boolean execute(Map.Entry<Long, Block> object) {
            try {
                final File dest = new File(location, LogUtil.getLogFilename(object.getKey()));
                final RandomAccessFile output = new RandomAccessFile(dest, "rw");
                final Block block = object.getValue();
                output.write(block.getData(), 0, block.getSize());
                output.close();
            // output.getChannel().force(false);
            } catch (IOException e) {
                throw new ExodusException(e);
            }
            return true;
        }
    };
    synchronized (data) {
        data.forEachEntry(saver);
        removedBlocks.forEachEntry(saver);
    }
}
Also used : ObjectProcedure(jetbrains.exodus.core.dataStructures.hash.ObjectProcedure) RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException) Map(java.util.Map) LongHashMap(jetbrains.exodus.core.dataStructures.hash.LongHashMap) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ExodusException(jetbrains.exodus.ExodusException)

Example 5 with ExodusException

use of jetbrains.exodus.ExodusException in project xodus by JetBrains.

the class EnvironmentLockTest method openConcurrentEnvironment.

private void openConcurrentEnvironment() {
    processor.start();
    new Job(processor) {

        @Override
        protected void execute() throws Throwable {
            final File dir = getEnvDirectory();
            try {
                env = newEnvironmentInstance(LogConfig.create(new FileDataReader(dir, 16), new FileDataWriter(dir, LOCK_ID)), new EnvironmentConfig().setLogLockTimeout(5000));
                wasOpened[0] = true;
            } catch (ExodusException e) {
                Assert.assertTrue(e.getMessage().contains(LOCK_ID));
                wasOpened[0] = false;
            }
        }
    };
}
Also used : FileDataReader(jetbrains.exodus.io.FileDataReader) FileDataWriter(jetbrains.exodus.io.FileDataWriter) Job(jetbrains.exodus.core.execution.Job) File(java.io.File) ExodusException(jetbrains.exodus.ExodusException)

Aggregations

ExodusException (jetbrains.exodus.ExodusException)21 File (java.io.File)6 IOException (java.io.IOException)6 RandomAccessFile (java.io.RandomAccessFile)3 CriticalSection (jetbrains.exodus.core.execution.locks.CriticalSection)3 ByteIterable (jetbrains.exodus.ByteIterable)2 CompoundByteIterable (jetbrains.exodus.CompoundByteIterable)2 Job (jetbrains.exodus.core.execution.Job)2 EnvironmentImpl (jetbrains.exodus.env.EnvironmentImpl)2 FileDataReader (jetbrains.exodus.io.FileDataReader)2 FileDataWriter (jetbrains.exodus.io.FileDataWriter)2 SharedRandomAccessFile (jetbrains.exodus.util.SharedRandomAccessFile)2 UncheckedIOException (java.io.UncheckedIOException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 FileChannel (java.nio.channels.FileChannel)1 Map (java.util.Map)1 Condition (java.util.concurrent.locks.Condition)1 Pair (jetbrains.exodus.core.dataStructures.Pair)1 LongHashMap (jetbrains.exodus.core.dataStructures.hash.LongHashMap)1 LongSet (jetbrains.exodus.core.dataStructures.hash.LongSet)1