From b543dd4ac4c8db0bc2a11c519742bb5d6c8c2621 Mon Sep 17 00:00:00 2001 From: aaron Date: Thu, 20 May 2010 16:16:36 +0000 Subject: [PATCH] more aggressive checks for the locking, and some more documentation git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@3404 348d0f76-0448-11de-a6fe-93d51630548a --- .../builders/TribbleRMDTrackBuilder.java | 6 ++--- .../sting/utils/file/FSLock.java | 27 ++++++++++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/java/src/org/broadinstitute/sting/gatk/refdata/tracks/builders/TribbleRMDTrackBuilder.java b/java/src/org/broadinstitute/sting/gatk/refdata/tracks/builders/TribbleRMDTrackBuilder.java index 344a5fa2e..ceb4aed07 100644 --- a/java/src/org/broadinstitute/sting/gatk/refdata/tracks/builders/TribbleRMDTrackBuilder.java +++ b/java/src/org/broadinstitute/sting/gatk/refdata/tracks/builders/TribbleRMDTrackBuilder.java @@ -168,9 +168,9 @@ public class TribbleRMDTrackBuilder extends PluginManager implemen */ private static LinearIndex writeIndexToDisk(File inputFile, FeatureCodec codec, boolean onDisk, File indexFile, boolean obtainedLock) throws IOException { LinearIndexCreator create = new LinearIndexCreator(inputFile, codec); - LinearIndex index = create.createIndex(); + LinearIndex index = create.createIndex(null); // we don't want to write initially, so we pass in null - // if the index doesn't exist, and we can write to the directory, and we got a lock, write to the disk + // if the index doesn't exist, and we can write to the directory, and we got a lock: write to the disk if (indexFile.getParentFile().canWrite() && (!indexFile.exists() || indexFile.canWrite()) && onDisk && @@ -182,7 +182,7 @@ public class TribbleRMDTrackBuilder extends PluginManager implemen // we can't write it to disk, just store it in memory else { // if they wanted to write, let them know we couldn't - if (onDisk) logger.warn("Unable to write to " + indexFile + " for the index file, creating index in memory only"); + if (onDisk) logger.info("Unable to write to " + indexFile + " for the index file, creating index in memory only"); return index; } } diff --git a/java/src/org/broadinstitute/sting/utils/file/FSLock.java b/java/src/org/broadinstitute/sting/utils/file/FSLock.java index 503b99c6a..df5003340 100644 --- a/java/src/org/broadinstitute/sting/utils/file/FSLock.java +++ b/java/src/org/broadinstitute/sting/utils/file/FSLock.java @@ -1,5 +1,6 @@ package org.broadinstitute.sting.utils.file; +import org.apache.log4j.Logger; import org.broadinstitute.sting.utils.StingException; import java.io.File; @@ -13,9 +14,19 @@ import java.nio.channels.FileLock; * a quick implementation of a file based lock, using the Java NIO classes */ public class FSLock { + // connect to the logger + private static Logger logger = Logger.getLogger(FSLock.class); + + // the lock string private static final String lockString = ".lock"; + + // the file we're attempting to lock private final File lockFile; + + // the file lock private FileLock lock = null; + + // the file channel we open FileChannel fc = null; /** @@ -32,20 +43,22 @@ public class FSLock { * * @return boolean true if we obtained a lock */ - public boolean lock() { + public synchronized boolean lock() { if (lock != null) throw new IllegalStateException("Unable to lock on file " + lockFile + " there is already a lock active"); - if (lockFile.exists()) { - System.err.println("exits!!"); + + // if the file already exists, or we can't write to the parent directory, return false + if (lockFile.exists() || !lockFile.getParentFile().canWrite()) return false; - } try { fc = new RandomAccessFile(lockFile,"rw").getChannel(); - lock = fc.lock(); + lock = fc.tryLock(); return lock != null && lock.isValid(); } catch (FileNotFoundException e) { - throw new StingException("Unable to create lock file named " + lockFile,e); + logger.info("Unable to create lock file (due to no file found exception), file = " + lockFile,e); + return false; } catch (IOException e) { - throw new StingException("Unable to create lock file named " + lockFile,e); + logger.info("Unable to create lock file (due to IO exception), file = " + lockFile,e); + return false; } }