Search in sources :

Example 26 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project helios by spotify.

the class TemporaryPorts method lock.

private AllocatedPort lock(final int port, final String name) {
    final Path path = lockDirectory.resolve(String.valueOf(port));
    try {
        final FileChannel file = FileChannel.open(path, CREATE, WRITE);
        final FileLock lock = file.tryLock();
        if (lock == null) {
            return null;
        }
        file.write(ByteBuffer.wrap(format("%d %s%n", port, name).getBytes(UTF_8)));
        file.force(true);
        return new AllocatedPort(port, path, file, lock);
    } catch (OverlappingFileLockException e) {
        return null;
    } catch (IOException e) {
        log.error("Failed to take port lock: {}", path, e);
        throw Throwables.propagate(e);
    }
}
Also used : Path(java.nio.file.Path) FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 27 with OverlappingFileLockException

use of java.nio.channels.OverlappingFileLockException in project jdk8u_jdk by JetBrains.

the class FileHandler method openFiles.

/**
     * Open the set of output files, based on the configured
     * instance variables.
     */
private void openFiles() throws IOException {
    LogManager manager = LogManager.getLogManager();
    manager.checkPermission();
    if (count < 1) {
        throw new IllegalArgumentException("file count = " + count);
    }
    if (limit < 0) {
        limit = 0;
    }
    // We register our own ErrorManager during initialization
    // so we can record exceptions.
    InitializationErrorManager em = new InitializationErrorManager();
    setErrorManager(em);
    // Create a lock file.  This grants us exclusive access
    // to our set of output files, as long as we are alive.
    int unique = -1;
    for (; ; ) {
        unique++;
        if (unique > MAX_LOCKS) {
            throw new IOException("Couldn't get lock for " + pattern);
        }
        // Generate a lock file name from the "unique" int.
        lockFileName = generate(pattern, 0, unique).toString() + ".lck";
        // if we ourself already have the file locked.
        synchronized (locks) {
            if (locks.contains(lockFileName)) {
                // object.  Try again.
                continue;
            }
            final Path lockFilePath = Paths.get(lockFileName);
            FileChannel channel = null;
            int retries = -1;
            boolean fileCreated = false;
            while (channel == null && retries++ < 1) {
                try {
                    channel = FileChannel.open(lockFilePath, CREATE_NEW, WRITE);
                    fileCreated = true;
                } catch (FileAlreadyExistsException ix) {
                    // but not too frequently.
                    if (Files.isRegularFile(lockFilePath, LinkOption.NOFOLLOW_LINKS) && isParentWritable(lockFilePath)) {
                        try {
                            channel = FileChannel.open(lockFilePath, WRITE, APPEND);
                        } catch (NoSuchFileException x) {
                            // the sequence.
                            continue;
                        } catch (IOException x) {
                            // try the next name in the sequence
                            break;
                        }
                    } else {
                        // break and try the next name in the sequence.
                        break;
                    }
                }
            }
            // try the next name;
            if (channel == null)
                continue;
            lockFileChannel = channel;
            boolean available;
            try {
                available = lockFileChannel.tryLock() != null;
            // We got the lock OK.
            // At this point we could call File.deleteOnExit().
            // However, this could have undesirable side effects
            // as indicated by JDK-4872014. So we will instead
            // rely on the fact that close() will remove the lock
            // file and that whoever is creating FileHandlers should
            // be responsible for closing them.
            } catch (IOException ix) {
                // We got an IOException while trying to get the lock.
                // This normally indicates that locking is not supported
                // on the target directory.  We have to proceed without
                // getting a lock.   Drop through, but only if we did
                // create the file...
                available = fileCreated;
            } catch (OverlappingFileLockException x) {
                // someone already locked this file in this VM, through
                // some other channel - that is - using something else
                // than new FileHandler(...);
                // continue searching for an available lock.
                available = false;
            }
            if (available) {
                // We got the lock.  Remember it.
                locks.add(lockFileName);
                break;
            }
            // We failed to get the lock.  Try next file.
            lockFileChannel.close();
        }
    }
    files = new File[count];
    for (int i = 0; i < count; i++) {
        files[i] = generate(pattern, i, unique);
    }
    // Create the initial log file.
    if (append) {
        open(files[0], true);
    } else {
        rotate();
    }
    // Did we detect any exceptions during initialization?
    Exception ex = em.lastException;
    if (ex != null) {
        if (ex instanceof IOException) {
            throw (IOException) ex;
        } else if (ex instanceof SecurityException) {
            throw (SecurityException) ex;
        } else {
            throw new IOException("Exception: " + ex);
        }
    }
    // Install the normal default ErrorManager.
    setErrorManager(new ErrorManager());
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileChannel(java.nio.channels.FileChannel) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 28 with OverlappingFileLockException

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

the class LocalFileLock method lock.

/**
   * Lock API for locking of the file channel of the lock file.
   *
   * @return
   */
@Override
public boolean lock() {
    try {
        if (!FileFactory.isFileExist(location, FileFactory.getFileType(tmpPath))) {
            FileFactory.mkdirs(location, FileFactory.getFileType(tmpPath));
        }
        lockFilePath = location + CarbonCommonConstants.FILE_SEPARATOR + lockFile;
        if (!FileFactory.isFileExist(lockFilePath, FileFactory.getFileType(location))) {
            FileFactory.createNewLockFile(lockFilePath, FileFactory.getFileType(location));
        }
        fileOutputStream = new FileOutputStream(lockFilePath);
        channel = fileOutputStream.getChannel();
        try {
            fileLock = channel.tryLock();
        } catch (OverlappingFileLockException e) {
            return false;
        }
        if (null != fileLock) {
            return true;
        } else {
            return false;
        }
    } catch (IOException e) {
        LOGGER.error(e, e.getMessage());
        return false;
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) 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