private static DirLock takeOwnership(FileSystem fs, Path dirLockFile) throws IOException { if (fs instanceof DistributedFileSystem) { if (!((DistributedFileSystem) fs).recoverLease(dirLockFile)) { LOG.warn("Unable to recover lease on dir lock file " + dirLockFile + " right now. Cannot transfer ownership. Will need to try later."); return null; } } // delete and recreate lock file if (fs.delete(dirLockFile, false)) { // returns false if somebody else already deleted it (to take ownership) FSDataOutputStream ostream = HdfsUtils.tryCreateFile(fs, dirLockFile); if (ostream != null) { ostream.close(); } return new DirLock(fs, dirLockFile); } return null; }
/** Get a lock on file if not already locked * * @param fs * @param dir the dir on which to get a lock * @return The lock object if it the lock was acquired. Returns null if the dir is already locked. * @throws IOException if there were errors */ public static DirLock tryLock(FileSystem fs, Path dir) throws IOException { Path lockFile = getDirLockFile(dir); try { FSDataOutputStream ostream = HdfsUtils.tryCreateFile(fs, lockFile); if (ostream != null) { LOG.debug("Thread ({}) Acquired lock on dir {}", threadInfo(), dir); ostream.close(); return new DirLock(fs, lockFile); } else { LOG.debug("Thread ({}) cannot lock dir {} as its already locked.", threadInfo(), dir); return null; } } catch (IOException e) { LOG.error("Error when acquiring lock on dir " + dir, e); throw e; } }
private static DirLock takeOwnership(FileSystem fs, Path dirLockFile) throws IOException { if(fs instanceof DistributedFileSystem) { if (!((DistributedFileSystem) fs).recoverLease(dirLockFile)) { LOG.warn("Unable to recover lease on dir lock file " + dirLockFile + " right now. Cannot transfer ownership. Will need to try later."); return null; } } // delete and recreate lock file if( fs.delete(dirLockFile, false) ) { // returns false if somebody else already deleted it (to take ownership) FSDataOutputStream ostream = HdfsUtils.tryCreateFile(fs, dirLockFile); if(ostream!=null) { ostream.close(); } return new DirLock(fs, dirLockFile); } return null; }
/** Get a lock on file if not already locked * * @param fs * @param dir the dir on which to get a lock * @return The lock object if it the lock was acquired. Returns null if the dir is already locked. * @throws IOException if there were errors */ public static DirLock tryLock(FileSystem fs, Path dir) throws IOException { Path lockFile = getDirLockFile(dir); try { FSDataOutputStream ostream = HdfsUtils.tryCreateFile(fs, lockFile); if (ostream!=null) { LOG.debug("Thread ({}) Acquired lock on dir {}", threadInfo(), dir); ostream.close(); return new DirLock(fs, lockFile); } else { LOG.debug("Thread ({}) cannot lock dir {} as its already locked.", threadInfo(), dir); return null; } } catch (IOException e) { LOG.error("Error when acquiring lock on dir " + dir, e); throw e; } }