{"record":{"id":"d6929891bb3e09c4","repo":"apache/hadoop","slug":"reached-eof-when-reading-log-header","errorCode":null,"errorMessage":"Reached EOF when reading log header","messagePattern":"Reached EOF when reading log header","errorType":"exception","errorClass":"LogHeaderCorruptException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/EditLogFileInputStream.java","lineNumber":382,"sourceCode":"    } finally {\n      IOUtils.closeStream(in);\n    }\n  }\n\n  /**\n   * Read the header of fsedit log\n   * @param in fsedit stream\n   * @return the edit log version number\n   * @throws IOException if error occurs\n   */\n  @VisibleForTesting\n  static int readLogVersion(DataInputStream in, boolean verifyLayoutVersion)\n      throws IOException, LogHeaderCorruptException {\n    int logVersion;\n    try {\n      logVersion = in.readInt();\n    } catch (EOFException eofe) {\n      throw new LogHeaderCorruptException(\n          \"Reached EOF when reading log header\");\n    }\n    if (verifyLayoutVersion &&\n        (logVersion < HdfsServerConstants.NAMENODE_LAYOUT_VERSION || // future version\n         logVersion > Storage.LAST_UPGRADABLE_LAYOUT_VERSION)) { // unsupported\n      throw new LogHeaderCorruptException(\n          \"Unexpected version of the file system log file: \"\n          + logVersion + \". Current version = \"\n          + HdfsServerConstants.NAMENODE_LAYOUT_VERSION + \".\");\n    }\n    return logVersion;\n  }\n  \n  /**\n   * Exception indicating that the header of an edits log file is\n   * corrupted. This can be because the header is not present,\n   * or because the header data is invalid (eg claims to be\n   * over a newer version than the running NameNode)","sourceCodeStart":364,"sourceCodeEnd":400,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/EditLogFileInputStream.java#L364-L400","documentation":"LogHeaderCorruptException('Reached EOF when reading log header') raised inside EditLogFileInputStream.readLogVersion(): the initial in.readInt() for the layout version hit EOF, i.e., the file ended before the 4-byte version could be read. This is the same 'empty/truncated before the header' condition as the caller-side check, surfaced on code paths that call readLogVersion directly (validation of candidate logs, offline tooling).","triggerScenarios":"Any consumer calling readLogVersion on an edits file with fewer than 4 bytes: zero-length segment, or a file whose first 4 bytes were never flushed; exercised by validateLog, FSEditLogLoader startup replay and offlineEditsViewer -r recovery mode.","commonSituations":"Crash or disk-full during segment creation; truncated journal file copies; name-dir restored from an incomplete backup.","solutions":["Run hdfs namenode -recover to sideline the corrupt segment (it holds no readable transactions).","Use offlineEditsViewer with the -r (recover) flag to inspect what is salvageable: hadoop org.apache.hadoop.hdfs.tools.offlineEditsViewer.OfflineEditsViewer -r -i edits_* -o /tmp/out.xml.","Restore missing journal range from an HA peer (bootstrapStandby) or checkpoint backup if transactions were actually lost."],"exampleFix":"# before: 'Reached EOF when reading log header' during edits validation\nhadoop org.apache.hadoop.hdfs.tools.offlineEditsViewer.OfflineEditsViewer \\\n  -r -i edits_in_progress_0000000000020000 -o /tmp/check.xml\n# after: confirms the segment is empty; discard it and restart the NameNode","handlingStrategy":"try-catch","validationCode":"if (Files.size(editsFile) < 4) {\n  // cannot even hold the layout-version int: every reader will fail with a header exception\n}","typeGuard":"static boolean isCorruptLogHeader(IOException e) {\n  return e instanceof EditLogFileInputStream.LogHeaderCorruptException;\n}","tryCatchPattern":"try {\n  int v = EditLogFileInputStream.readLogVersion(in, true);\n} catch (EditLogFileInputStream.LogHeaderCorruptException e) {\n  // empty/sub-4-byte log: no transactions; drop it and move to the previous segment\n}","preventionTips":["Pre-size-check candidate segments (< 4 bytes) in journal tooling.","Use offlineEditsViewer -r to triage salvageable transactions.","Keep checkpoint/peer backups so journal gaps are recoverable."],"tags":["hdfs","edit-log","corruption","eof","namenode-startup"],"backgroundTag":"edit-log-corruption","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}