{"record":{"id":"6baa33c6d61334c6","repo":"apache/hadoop","slug":"negative-length-of-the-file","errorCode":null,"errorMessage":"Negative length of the file","messagePattern":"Negative length of the file","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"critical","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSImageUtil.java","lineNumber":61,"sourceCode":"      return false;\n\n    byte[] magic = new byte[MAGIC_HEADER.length];\n    file.readFully(magic);\n    if (!Arrays.equals(MAGIC_HEADER, magic))\n      return false;\n\n    return true;\n  }\n\n  public static FileSummary loadSummary(RandomAccessFile file)\n      throws IOException {\n    final int FILE_LENGTH_FIELD_SIZE = 4;\n    long fileLength = file.length();\n    file.seek(fileLength - FILE_LENGTH_FIELD_SIZE);\n    int summaryLength = file.readInt();\n\n    if (summaryLength <= 0) {\n      throw new IOException(\"Negative length of the file\");\n    }\n    file.seek(fileLength - FILE_LENGTH_FIELD_SIZE - summaryLength);\n\n    byte[] summaryBytes = new byte[summaryLength];\n    file.readFully(summaryBytes);\n\n    FileSummary summary = FileSummary\n        .parseDelimitedFrom(new ByteArrayInputStream(summaryBytes));\n    if (summary.getOndiskVersion() != FILE_VERSION) {\n      throw new IOException(\"Unsupported file version \"\n          + summary.getOndiskVersion());\n    }\n\n    if (!NameNodeLayoutVersion.supports(Feature.PROTOBUF_FORMAT,\n        summary.getLayoutVersion())) {\n      throw new IOException(\"Unsupported layout version \"\n          + summary.getLayoutVersion());\n    }","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSImageUtil.java#L43-L79","documentation":"A protobuf fsimage ends with a length-prefixed FileSummary: the last 4 bytes are an int holding the summary length. FSImageUtil.loadSummary() seeks to the tail, reads that int, and rejects values <= 0 — exactly what a truncated or zero-filled tail produces. It means the image file is incomplete (disk full while saving, crash mid-write, interrupted copy), not a version problem.","triggerScenarios":"FSImageUtil.loadSummary() on a file whose final 4 bytes decode to a non-positive int: truncated fsimage, an empty/partial copy, or random bytes at the tail.","commonSituations":"NameNode killed or the disk full during saveNamespace; partial scp/cp of fsimage; image written to a full or flaky NFS volume.","solutions":["Check the file size and verify against the paired fsimage.md5; recopy if the checksum fails","Restore the image from another name.dir or backup — the truncated copy is unusable","If a save failed, the previous fsimage is usually still present: remove the partial newest image and boot from the prior txid","Monitor free space on the name dirs so checkpoints never run against a full disk"],"exampleFix":"# before: truncated image fails to load\nls -l /dfs/name/current/   # fsimage_0000000000000090000 suspiciously small\n# after: drop the partial file, boot from the previous good image\ncd /dfs/name/current && rm fsimage_0000000000000090000*\ncp /backup/fsimage_0000000000000085000* /backup/seen_txid .","handlingStrategy":"validation","validationCode":"RandomAccessFile raf = new RandomAccessFile(img, \"r\");\nlong len = raf.length();\nif (len < 8) throw new IOException(\"fsimage too small: \" + len);\nraf.seek(len - 4);\nint summaryLen = raf.readInt();\nif (summaryLen <= 0 || summaryLen > len) {\n  throw new IOException(\"fsimage tail invalid (truncated?): \" + summaryLen);\n}","typeGuard":null,"tryCatchPattern":"catch IOException from FSImageUtil.loadSummary — 'Negative length of the file' means the copy is incomplete; re-transfer from the source and re-verify md5 rather than retrying the same bytes.","preventionTips":["After any fsimage transfer, verify size and md5 before treating it as a checkpoint","Disk-space alarms on name dirs so saveNamespace never writes onto a full disk","Prefer atomic move-into-place (save to temp, rename) semantics in automation that stages images"],"tags":["hadoop","hdfs","namenode","fsimage","truncated-file","disk-full"],"backgroundTag":"truncated-file-read","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}