apache/hadoop · error · FileNotFoundException

File " + f + " not found

Error message

File " + f + " not found

What it means

RawLocalFileSystem.truncate(Path, long) calls getFileStatus first; if the status lookup signals the file is absent it throws FileNotFoundException('File ... not found'). In practice getFileStatus itself throws FileNotFoundException for missing paths, so this branch guards the degenerate null case, but the user-visible meaning is the same: the local file to truncate does not exist.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/RawLocalFileSystem.java:761

          if (LOG.isDebugEnabled()) {
            LOG.debug("Deleting empty destination and renaming " + src +
                " to " + dst);
          }
          if (this.delete(dst, false) && srcFile.renameTo(dstFile)) {
            return true;
          }
        }
      }
    } catch (FileNotFoundException ignored) {
    }
    return false;
  }

  @Override
  public boolean truncate(Path f, final long newLength) throws IOException {
    FileStatus status = getFileStatus(f);
    if(status == null) {
      throw new FileNotFoundException("File " + f + " not found");
    }
    if(status.isDirectory()) {
      throw new IOException("Cannot truncate a directory (=" + f + ")");
    }
    long oldLength = status.getLen();
    if(newLength > oldLength) {
      throw new IllegalArgumentException(
          "Cannot truncate to a larger file size. Current size: " + oldLength +
          ", truncate size: " + newLength + ".");
    }
    try (FileOutputStream out = new FileOutputStream(pathToFile(f), true)) {
      try {
        out.getChannel().truncate(newLength);
      } catch(IOException e) {
        throw new FSError(e);
      }
    }
    return true;

View on GitHub (pinned to 2add963021)

Solutions

  1. Verify the path: ls -l / the file on local disk; fix typos or stale references in configuration.
  2. Check existence in code before truncating: if (!fs.exists(p)) recreate or skip.
  3. For concurrent writers, move to unique per-writer files or coordinate deletion/truncation with a lock.
  4. Treat FileNotFoundException from truncate as non-retryable unless a concurrent producer may recreate the file, in which case wait-and-recheck once.

Example fix

// before
fs.truncate(new Path("/data/scratch.log"), 0); // file was deleted by cleanup

// after
Path p = new Path("/data/scratch.log");
if (fs.exists(p)) {
  fs.truncate(p, 0);
} else {
  LOG.warn("{} missing; recreating", p);
  fs.create(p, true).close();
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (!fs.exists(p) || !fs.getFileStatus(p).isFile()) {
  throw new FileNotFoundException(p + " missing; run the producer stage first");
}
fs.truncate(p, newLen);

Try / catch

try {
  fs.truncate(p, newLen);
} catch (FileNotFoundException e) {
  LOG.warn("{} vanished before truncate; skipping", p);
}

Prevention

When it happens

Trigger: Calling fs.truncate(path, newLen) on a local path that does not exist: typo'd filename, output moved/cleaned before truncation, a race where another process deleted the file, or truncate issued after a failed create.

Common situations: Post-processing steps that shrink generated files that a cleanup job already removed, concurrent writers on shared scratch space deleting each other's files, or scripts referencing stale paths after a directory restructure.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/70baf5eb4eb80bf3. Report an issue: GitHub.