Search in sources :

Example 16 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project ice by JBEI.

the class BlastPlus method rebuildFeaturesBlastDatabase.

public static void rebuildFeaturesBlastDatabase(String featureFolder) throws IOException {
    String blastInstallDir = Utils.getConfigValue(ConfigurationKey.BLAST_INSTALL_DIR);
    if (StringUtils.isEmpty(blastInstallDir)) {
        Logger.warn("Blast install directory not available. Aborting blast features rebuild");
        return;
    }
    Path blastDir = Paths.get(blastInstallDir);
    if (!Files.exists(blastDir))
        throw new IOException("Could not locate Blast installation in " + blastInstallDir);
    String dataDir = Utils.getConfigValue(ConfigurationKey.DATA_DIRECTORY);
    final Path blastFolder = Paths.get(dataDir, featureFolder);
    File lockFile = Paths.get(blastFolder.toString(), LOCK_FILE_NAME).toFile();
    if (lockFile.exists()) {
        if (lockFile.lastModified() <= (System.currentTimeMillis() - (1000 * 60 * 60 * 24)))
            if (!lockFile.delete()) {
                Logger.warn("Could not delete outdated features blast lockfile. Delete the following file manually: " + lockFile.getAbsolutePath());
            } else {
                Logger.info("Features blast db locked (lockfile - " + lockFile.getAbsolutePath() + "). Rebuild aborted!");
                return;
            }
    }
    try {
        if (!Files.exists(blastFolder)) {
            Logger.info("Features blast folder (" + blastFolder.toString() + ") does not exist. Creating...");
            try {
                Files.createDirectories(blastFolder);
            } catch (Exception e) {
                Logger.warn("Could not create features blast folder. Create it manually or all blast features runs will fail");
                return;
            }
        }
        if (!lockFile.createNewFile()) {
            Logger.warn("Could not create lock file for features blast rebuild");
            return;
        }
        FileOutputStream fos = new FileOutputStream(lockFile);
        try (FileLock lock = fos.getChannel().tryLock()) {
            if (lock == null)
                return;
            Logger.info("Rebuilding features blast database...");
            rebuildSequenceDatabase(blastDir, blastFolder, true);
            Logger.info("Blast features database rebuild complete");
        }
    } catch (OverlappingFileLockException l) {
        Logger.warn("Could not obtain lock file for blast at " + blastFolder.toString());
    } catch (BlastException be) {
        FileUtils.deleteQuietly(lockFile);
        Logger.error(be);
    }
    FileUtils.deleteQuietly(lockFile);
}
Also used : FileLock(java.nio.channels.FileLock) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) IllegalAlphabetException(org.biojava.bio.symbol.IllegalAlphabetException) IllegalSymbolException(org.biojava.bio.symbol.IllegalSymbolException)

Example 17 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project kafka by apache.

the class StateDirectoryTest method shouldLockTaskStateDirectory.

@Test
public void shouldLockTaskStateDirectory() throws Exception {
    final TaskId taskId = new TaskId(0, 0);
    final File taskDirectory = directory.directoryForTask(taskId);
    directory.lock(taskId, 0);
    final FileChannel channel = FileChannel.open(new File(taskDirectory, StateDirectory.LOCK_FILE_NAME).toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
    try {
        channel.tryLock();
        fail("shouldn't be able to lock already locked directory");
    } catch (OverlappingFileLockException e) {
    // pass
    }
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) FileChannel(java.nio.channels.FileChannel) File(java.io.File) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) Test(org.junit.Test)

Example 18 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project kafka by apache.

the class StateDirectory method cleanRemovedTasks.

/**
     * Remove the directories for any {@link TaskId}s that are no-longer
     * owned by this {@link StreamThread} and aren't locked by either
     * another process or another {@link StreamThread}
     * @param cleanupDelayMs only remove directories if they haven't been modified for at least
     *                       this amount of time (milliseconds)
     */
public void cleanRemovedTasks(final long cleanupDelayMs) {
    final File[] taskDirs = listTaskDirectories();
    if (taskDirs == null || taskDirs.length == 0) {
        // nothing to do
        return;
    }
    for (File taskDir : taskDirs) {
        final String dirName = taskDir.getName();
        TaskId id = TaskId.parse(dirName);
        if (!locks.containsKey(id)) {
            try {
                if (lock(id, 0)) {
                    if (time.milliseconds() > taskDir.lastModified() + cleanupDelayMs) {
                        log.info("Deleting obsolete state directory {} for task {} as cleanup delay of {} ms has passed", dirName, id, cleanupDelayMs);
                        Utils.delete(taskDir);
                    }
                }
            } catch (OverlappingFileLockException e) {
            // locked by another thread
            } catch (IOException e) {
                log.error("Failed to lock the state directory due to an unexpected exception", e);
            } finally {
                try {
                    unlock(id);
                } catch (IOException e) {
                    log.error("Failed to release the state directory lock");
                }
            }
        }
    }
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) IOException(java.io.IOException) File(java.io.File) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 19 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project buck by facebook.

the class Main method obtainResourceFileLock.

/**
   * To prevent 'buck kill' from deleting resources from underneath a 'live' buckd we hold on to
   * the FileLock for the entire lifetime of the process. We depend on the fact that on Linux and
   * MacOS Java FileLock is implemented using the same mechanism as the Python fcntl.lockf method.
   * Should this not be the case we'll simply have a small race between buckd start and `buck kill`.
   */
private static void obtainResourceFileLock() {
    if (resourcesFileLock != null) {
        return;
    }
    String resourceLockFilePath = System.getProperties().getProperty("buck.resource_lock_path");
    if (resourceLockFilePath == null) {
        // Running from ant, no resource lock needed.
        return;
    }
    try {
        // R+W+A is equivalent to 'a+' in Python (which is how the lock file is opened in Python)
        // because WRITE in Java does not imply truncating the file.
        FileChannel fileChannel = FileChannel.open(Paths.get(resourceLockFilePath), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        resourcesFileLock = fileChannel.tryLock(0L, Long.MAX_VALUE, true);
    } catch (IOException | OverlappingFileLockException e) {
        LOG.debug(e, "Error when attempting to acquire resources file lock.");
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 20 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project j2objc by google.

the class FileChannelImpl method addLock.

/**
     * Add a new pending lock to the manager. Throws an exception if the lock
     * would overlap an existing lock. Once the lock is acquired it remains in
     * this set as an acquired lock.
     */
private synchronized void addLock(FileLock lock) throws OverlappingFileLockException {
    long lockEnd = lock.position() + lock.size();
    for (FileLock existingLock : locks) {
        if (existingLock.position() > lockEnd) {
            // cannot overlap).
            break;
        }
        if (existingLock.overlaps(lock.position(), lock.size())) {
            throw new OverlappingFileLockException();
        }
    }
    locks.add(lock);
}
Also used : FileLock(java.nio.channels.FileLock) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Aggregations

OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)28 IOException (java.io.IOException)15 FileLock (java.nio.channels.FileLock)15 File (java.io.File)11 FileChannel (java.nio.channels.FileChannel)10 RandomAccessFile (java.io.RandomAccessFile)7 FileOutputStream (java.io.FileOutputStream)3 TaskId (org.apache.kafka.streams.processor.TaskId)3 Test (org.junit.Test)3 Path (java.nio.file.Path)2 IllegalAlphabetException (org.biojava.bio.symbol.IllegalAlphabetException)2 IllegalSymbolException (org.biojava.bio.symbol.IllegalSymbolException)2 FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)1 AttributeNotSupportedException (com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException)1 FileDoesNotExistException (com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException)1 ParallelTest (com.hazelcast.test.annotation.ParallelTest)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 LogException (io.mycat.backend.mysql.xa.recovery.LogException)1 EOFException (java.io.EOFException)1 FileNotFoundException (java.io.FileNotFoundException)1