{"record":{"id":"41ae6b172b770f05","repo":"apache/hadoop","slug":"invalid-block-array-length","errorCode":null,"errorMessage":"Invalid block array length: {}","messagePattern":"Invalid block array length: (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSImageSerialization.java","lineNumber":506,"sourceCode":"    WritableUtils.writeVInt(out, blocks.length);\n    Block prev = null;\n    for (Block b : blocks) {\n      long szDelta = b.getNumBytes() -\n          (prev != null ? prev.getNumBytes() : 0);\n      long gsDelta = b.getGenerationStamp() -\n          (prev != null ? prev.getGenerationStamp() : 0);\n      out.writeLong(b.getBlockId()); // blockid is random\n      WritableUtils.writeVLong(out, szDelta);\n      WritableUtils.writeVLong(out, gsDelta);\n      prev = b;\n    }\n  }\n  \n  public static Block[] readCompactBlockArray(\n      DataInput in, int logVersion) throws IOException {\n    int num = WritableUtils.readVInt(in);\n    if (num < 0) {\n      throw new IOException(\"Invalid block array length: \" + num);\n    }\n    Block prev = null;\n    Block[] ret = new Block[num];\n    for (int i = 0; i < num; i++) {\n      long id = in.readLong();\n      long sz = WritableUtils.readVLong(in) +\n          ((prev != null) ? prev.getNumBytes() : 0);\n      long gs = WritableUtils.readVLong(in) +\n          ((prev != null) ? prev.getGenerationStamp() : 0);\n      ret[i] = new Block(id, sz, gs);\n      prev = ret[i];\n    }\n    return ret;\n  }\n\n  public static void writeCacheDirectiveInfo(DataOutputStream out,\n      CacheDirectiveInfo directive) throws IOException {\n    writeLong(directive.getId(), out);","sourceCodeStart":488,"sourceCodeEnd":524,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSImageSerialization.java#L488-L524","documentation":"readCompactBlockArray reads a length-prefixed, delta-encoded block list from a legacy-format fsimage/edits stream. The count is a WritableUtils VInt; a negative value means the bytes do not parse as the expected format. The stream is corrupt or truncated, or a layout-version mismatch makes the reader's field expectations disagree with what was written.","triggerScenarios":"Loading a legacy image or replaying edits where the block-count vint decodes negative: byte-level corruption, truncated file, or reading a stream produced by an incompatible Hadoop layout version.","commonSituations":"Disk corruption or partial copy of fsimage/edits; replaying edits recovered from a journal with a hole; version skew on legacy-format files.","solutions":["Verify integrity: compare fsimage against its fsimage.md5; run 'hdfs oev' on the edits to locate the bad record","Read the file with the Hadoop version/layout that wrote it","Restore the affected file from another storage directory or backup and retry","If corruption is confirmed and no copy exists, fall back to the previous checkpoint plus whatever subsequent edits survive"],"exampleFix":"# before: corrupt/truncated file fails to load\nmd5sum -c /dfs/name/current/fsimage.md5   # FAILED\n# after: replace with a verified copy from another dir or backup\ncp /dfs/name2/current/fsimage* /dfs/name/current/","handlingStrategy":"fallback","validationCode":"// verify checksum before loading a legacy image\nMessageDigest md = MessageDigest.getInstance(\"MD5\");\ntry (InputStream in = new FileInputStream(img)) {\n  byte[] b = new byte[8192]; for (int n; (n = in.read(b)) != -1;) md.update(b, 0, n);\n}\nString actual = String.format(\"%d\", new BigInteger(1, md.digest()));\n// compare against the hex in the sibling fsimage.md5 file","typeGuard":null,"tryCatchPattern":"catch (IOException e) when the message begins with \"Invalid block array length\" — the bytes are corrupt; do not re-read the same file. Fall back to another storage directory's copy or the previous checkpoint, then replay surviving edits.","preventionTips":["Always keep and verify fsimage.md5 when archiving images","Smoke-test archived journals with 'hdfs oev' before you need them for recovery","Treat any unexplained truncation as a disk problem and check dmesg/SMART"],"tags":["hadoop","hdfs","fsimage","edits","serialization","corruption"],"backgroundTag":"corrupt-file-parse","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}