com.bigdata.io
Class FileLockUtility

java.lang.Object
  extended by com.bigdata.io.FileLockUtility

Deprecated. This can lead to trouble. Use a FileLock if supported and otherwise proceed without a FileLock.

public class FileLockUtility
extends Object

Utility methods for managing exlusive FileLocks and advisory locks depending on what is supported by the platform, file access mode, and volume on which the file resides.

Version:
$Id: FileLockUtility.java 2265 2009-10-26 12:51:06Z thompsonbry $
Author:
Bryan Thompson

Field Summary
protected static boolean DEBUG
          Deprecated.  
protected static boolean INFO
          Deprecated.  
protected static org.apache.log4j.Logger log
          Deprecated.  
 
Constructor Summary
FileLockUtility()
          Deprecated.  
 
Method Summary
static boolean acquireAdvisoryLock(File file)
          Deprecated. Creates an advisory lock file having the same basename as the given file with a .lock extension.
static void closeFile(File file, RandomAccessFile raf)
          Deprecated. Close the file and automatically releases the FileLock (if any) and removes the advisory lock for that file (if any).
static boolean isOurLockFile(File lockFile)
          Deprecated.  
static RandomAccessFile openFile(File file, String fileMode, boolean useFileLock)
          Deprecated. Create/open the file and obtain an exclusive lock.
static void removeAdvisoryLock(File file)
          Deprecated. Removes the advisory lock for the file if it exists.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static final org.apache.log4j.Logger log
Deprecated. 

INFO

protected static final boolean INFO
Deprecated. 

DEBUG

protected static final boolean DEBUG
Deprecated. 
Constructor Detail

FileLockUtility

public FileLockUtility()
Deprecated. 
Method Detail

openFile

public static RandomAccessFile openFile(File file,
                                        String fileMode,
                                        boolean useFileLock)
                                 throws IOException
Deprecated. 
Create/open the file and obtain an exclusive lock.

A FileLock will be used when supported and requested. An advisory lock will be used if useFileLock == false, if the fileMode is read-only. If tryLock() returns null then the lock exists and this request will fail. However, if FileChannel.tryLock() throws an IOException then the underlying platform does not support FileLock for the named file (memory mapped files, read-only files, and NFS mounted files can all have this problem) and we will attempt to acquire an advisory lock instead. Advisory locks are NOT visible to other applications

Do NOT request a FileLock if you are going to use a memory-mapped buffer. The JDK cautions that these things do not play well together on some platforms.

Parameters:
file - The file.
fileMode - The file mode for RandomAccessFile.RandomAccessFile(File, String)
useFileLock - true if FileChannel.tryLock() should be attempted. when false only an advisory lock will be sought.
Returns:
The RandomAccessFile
Throws:
IOException - If the file could not be opened or someone already holds a lock for that file.
See Also:
FileMetadata#acquireAdvisoryLock(File), Options.FILE_LOCK_ENABLED
TODO:
we really don't need locks for temporary files., handle lock files during StoreManager startup., if you use a FileLock to open a file then you are protected., this ignores locks for read only file modes. in fact we should create the file with a read-only marker and allow more than one process to obtain that lock.

closeFile

public static void closeFile(File file,
                             RandomAccessFile raf)
                      throws IOException
Deprecated. 
Close the file and automatically releases the FileLock (if any) and removes the advisory lock for that file (if any).

Note: This method should be used in combination with openFile(File, String, boolean) in order to ensure that the optional advisory lock file is deleted when the file is closed. The purpose of the advisory lock file is to provide advisory locking file modes (read-only), platforms, or file systems (NFS) that do not support FileLock.

Parameters:
file - The file.
raf - The RandomAccessFile.
Throws:
IOException

acquireAdvisoryLock

public static boolean acquireAdvisoryLock(File file)
                                   throws IOException
Deprecated. 
Creates an advisory lock file having the same basename as the given file with a .lock extension.

Note: This uses File.createNewFile() which is NOT advised for this purpose. However, FileLock does not work in some contexts so this is used as a fallback mechanism. We write a UUID into the advisory lock since Java does not have platform independent PIDs. That UUID allows us to tell whether the advisory lock file was created by this process or by another process.

Note: If a Thread is interrupted during an NIO operation then the FileChannel will be closed asynchronously. While this correctly releases a FileLock it does NOT cause our advisory lock file to be deleted. During a normal shutdown of an AbstractJournal, the advisory lock file is deleted by closeFile(File, RandomAccessFile). However, following an abnormal shutdown the advisory lock file MAY still exist and (assuming that FileLock is not working since we created an advisory lock in the first place) it MUST be removed by hand before the AbstractJournal can be reopened.

Parameters:
file - The given file.
Returns:
true if the advisory lock was created or exists and was created by this process. false if the advisory lock already exists and was created by another process.
Throws:
IOException - If there is a problem.
See Also:
pid

isOurLockFile

public static boolean isOurLockFile(File lockFile)
                             throws IOException
Deprecated. 
Throws:
IOException

removeAdvisoryLock

public static void removeAdvisoryLock(File file)
                               throws IOException
Deprecated. 
Removes the advisory lock for the file if it exists.

Parameters:
file - The file whose .lock file will be removed.
Throws:
IOException - if the lock file exists but does not belong to this process or can not be removed.
See Also:
acquireAdvisoryLock(File)


Copyright © 2006-2009 SYSTAP, LLC. All Rights Reserved.