Search in sources :

Example 1 with OverlappingFileLockException

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

the class FsDatasetTestUtil method assertFileLockReleased.

/**
   * Asserts that the storage lock file in the given directory has been
   * released.  This method works by trying to acquire the lock file itself.  If
   * locking fails here, then the main code must have failed to release it.
   *
   * @param dir the storage directory to check
   * @throws IOException if there is an unexpected I/O error
   */
public static void assertFileLockReleased(String dir) throws IOException {
    StorageLocation sl = StorageLocation.parse(dir);
    File lockFile = new File(new File(sl.getUri()), Storage.STORAGE_FILE_LOCK);
    try (RandomAccessFile raf = new RandomAccessFile(lockFile, "rws");
        FileChannel channel = raf.getChannel()) {
        FileLock lock = channel.tryLock();
        assertNotNull(String.format("Lock file at %s appears to be held by a different process.", lockFile.getAbsolutePath()), lock);
        if (lock != null) {
            try {
                lock.release();
            } catch (IOException e) {
                FsDatasetImpl.LOG.warn(String.format("I/O error releasing file lock %s.", lockFile.getAbsolutePath()), e);
                throw e;
            }
        }
    } catch (OverlappingFileLockException e) {
        fail(String.format("Must release lock file at %s.", lockFile.getAbsolutePath()));
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException) StorageLocation(org.apache.hadoop.hdfs.server.datanode.StorageLocation) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 2 with OverlappingFileLockException

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

the class StateDirectoryTest method shouldLockMulitpleTaskDirectories.

@Test
public void shouldLockMulitpleTaskDirectories() throws Exception {
    final TaskId taskId = new TaskId(0, 0);
    final File task1Dir = directory.directoryForTask(taskId);
    final TaskId taskId2 = new TaskId(1, 0);
    final File task2Dir = directory.directoryForTask(taskId2);
    directory.lock(taskId, 0);
    directory.lock(taskId2, 0);
    final FileChannel channel1 = FileChannel.open(new File(task1Dir, StateDirectory.LOCK_FILE_NAME).toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
    final FileChannel channel2 = FileChannel.open(new File(task2Dir, StateDirectory.LOCK_FILE_NAME).toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
    try {
        channel1.tryLock();
        channel2.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 3 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project Mycat-Server by MyCATApache.

the class LogFileLock method acquireLock.

public void acquireLock() throws LogException {
    try {
        File parent = new File(dir);
        if (!parent.exists()) {
            parent.mkdir();
        }
        lockfileToPreventDoubleStartup_ = new File(dir, fileName + ".lck");
        lockfilestream_ = new FileOutputStream(lockfileToPreventDoubleStartup_);
        lock_ = lockfilestream_.getChannel().tryLock();
        lockfileToPreventDoubleStartup_.deleteOnExit();
    } catch (OverlappingFileLockException failedToGetLock) {
        // happens on windows
        lock_ = null;
    } catch (IOException failedToGetLock) {
        // happens on windows
        lock_ = null;
    }
    if (lock_ == null) {
        logger.error("ERROR: the specified log seems to be in use already: " + fileName + " in " + dir + ". Make sure that no other instance is running, or kill any pending process if needed.");
        throw new LogException("Log already in use? " + fileName + " in " + dir);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) LogException(io.mycat.backend.mysql.xa.recovery.LogException)

Example 4 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project neo4j by neo4j.

the class StoreLocker method checkLock.

/**
     * Obtains lock on store file so that we can ensure the store is not shared between database instances
     * <p>
     * Creates store dir if necessary, creates store lock file if necessary
     * <p>
     * Please note that this lock is only valid for as long the {@link #storeLockFileChannel} lives, so make sure the
     * lock cannot be garbage collected as long as the lock should be valid.
     *
     * @throws StoreLockException if lock could not be acquired
     */
public void checkLock(File storeDir) throws StoreLockException {
    File storeLockFile = new File(storeDir, STORE_LOCK_FILENAME);
    try {
        if (!fileSystemAbstraction.fileExists(storeLockFile)) {
            fileSystemAbstraction.mkdirs(storeLockFile.getParentFile());
        }
    } catch (IOException e) {
        String message = "Unable to create path for store dir: " + storeDir;
        throw storeLockException(message, e);
    }
    try {
        storeLockFileChannel = fileSystemAbstraction.open(storeLockFile, "rw");
        storeLockFileLock = storeLockFileChannel.tryLock();
        if (storeLockFileLock == null) {
            String message = "Store and its lock file has been locked by another process: " + storeLockFile;
            throw storeLockException(message, null);
        }
    } catch (OverlappingFileLockException | IOException e) {
        String message = "Unable to obtain lock on store lock file: " + storeLockFile;
        throw storeLockException(message, e);
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 5 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project graphdb by neo4j-attic.

the class FileLock method getWindowsFileLock.

private static FileLock getWindowsFileLock(File storeDir) throws IOException {
    File lockFile = new File(storeDir, "lock");
    if (!lockFile.exists()) {
        if (!lockFile.createNewFile()) {
            throw new IOException("Couldn't create lock file " + lockFile.getAbsolutePath());
        }
    }
    FileChannel fileChannel = new RandomAccessFile(lockFile, "rw").getChannel();
    java.nio.channels.FileLock fileChannelLock = null;
    try {
        fileChannelLock = fileChannel.tryLock();
    } catch (OverlappingFileLockException e) {
    // OK, let fileChannelLock continue to be null and we'll deal with it below
    }
    if (fileChannelLock == null) {
        fileChannel.close();
        return null;
    }
    return new WindowsFileLock(lockFile, fileChannel, fileChannelLock);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) 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