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()));
}
}
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
}
}
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);
}
}
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);
}
}
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);
}
Aggregations