/** * Finds oldest expired lock file (using modification timestamp), then takes * ownership of the lock file * Impt: Assumes access to lockFilesDir has been externally synchronized such that * only one thread accessing the same thread * @param fs * @param lockFilesDir * @param locktimeoutSec * @return a Pair<lock file path, last entry in lock file> .. if expired lock file found * @throws IOException */ public static HdfsUtils.Pair<Path, LogEntry> locateOldestExpiredLock(FileSystem fs, Path lockFilesDir, int locktimeoutSec) throws IOException { // list files long now = System.currentTimeMillis(); long olderThan = now - (locktimeoutSec * 1000); Collection<Path> listing = HdfsUtils.listFilesByModificationTime(fs, lockFilesDir, olderThan); // locate oldest expired lock file (if any) and take ownership for (Path file : listing) { if (file.getName().equalsIgnoreCase(DirLock.DIR_LOCK_FILE)) { continue; } LogEntry lastEntry = getLastEntryIfStale(fs, file, olderThan); if (lastEntry != null) { return new HdfsUtils.Pair<>(file, lastEntry); } } LOG.debug("No abandoned files found"); return null; }
continue; LogEntry lastEntry = getLastEntryIfStale(fs, file, olderThan); if (lastEntry != null) { FileLock lock = FileLock.takeOwnership(fs, file, lastEntry, spoutId);
/** * Finds oldest expired lock file (using modification timestamp), then takes * ownership of the lock file * Impt: Assumes access to lockFilesDir has been externally synchronized such that * only one thread accessing the same thread * @param fs * @param lockFilesDir * @param locktimeoutSec * @return a Pair<lock file path, last entry in lock file> .. if expired lock file found * @throws IOException */ public static HdfsUtils.Pair<Path,LogEntry> locateOldestExpiredLock(FileSystem fs, Path lockFilesDir, int locktimeoutSec) throws IOException { // list files long now = System.currentTimeMillis(); long olderThan = now - (locktimeoutSec*1000); Collection<Path> listing = HdfsUtils.listFilesByModificationTime(fs, lockFilesDir, olderThan); // locate oldest expired lock file (if any) and take ownership for (Path file : listing) { if(file.getName().equalsIgnoreCase( DirLock.DIR_LOCK_FILE) ) { continue; } LogEntry lastEntry = getLastEntryIfStale(fs, file, olderThan); if(lastEntry!=null) { return new HdfsUtils.Pair<>(file, lastEntry); } } LOG.debug("No abandoned files found"); return null; }
continue; LogEntry lastEntry = getLastEntryIfStale(fs, file, olderThan); if(lastEntry!=null) { FileLock lock = FileLock.takeOwnership(fs, file, lastEntry, spoutId);