private void listCorruptOpenFiles() throws IOException { int matchedCorruptFilesCount = 0; // directory representation of path String pathdir = path.endsWith(Path.SEPARATOR) ? path : path + Path.SEPARATOR; FileStatus pathFileStatus = nn.getNamesystem().getFileInfo(pathdir); List<FileStatus> corruptFileStatusList = new ArrayList<FileStatus>(); checkForCorruptOpenFiles(pathFileStatus, corruptFileStatusList); for (FileStatus fileStatus : corruptFileStatusList) { String currentPath = fileStatus.getPath().toString(); if (currentPath.startsWith(pathdir) || currentPath.equals(path)) { matchedCorruptFilesCount++; // print the header before listing first item if (matchedCorruptFilesCount == 1 ) { out.println("Here are a few files that may be corrupted:"); out.println("==========================================="); } out.println(currentPath); } } out.println(); out.println(buildSummaryResultForListCorruptFiles(matchedCorruptFilesCount, path)); }
private void checkForCorruptOpenFiles( FileStatus file, List<FileStatus> corruptFiles ) throws IOException { String filePath = file.getPath().toUri().getPath(); if (file.isDir()) { for (FileStatus fileStatus : nn.namesystem.dir.getListing(filePath)) { checkForCorruptOpenFiles(fileStatus, corruptFiles); } } else { LeaseManager.Lease lease = nn.getNamesystem().leaseManager.getLeaseByPath(filePath); // Condition: // 1. lease has expired hard limit // 2. the file is open for write // 3. the last block has 0 locations if (lease != null && lease.expiredHardLimit()) { LocatedBlocks blocks = nn.getNamesystem().getBlockLocations(filePath, 0, file.getLen()); List<LocatedBlock> locatedBlockList = blocks.getLocatedBlocks(); LocatedBlock lastBlock = locatedBlockList.get(locatedBlockList.size() - 1); if (blocks.isUnderConstruction() && lastBlock.getLocations().length == 0) { corruptFiles.add(file); } } } }