{"record":{"id":"423c91549d0a9821","repo":"apache/hadoop","slug":"trying-to-remove-a-block-from-file-which-is-not","errorCode":null,"errorMessage":"Trying to remove a block from file {} which is not under construction.","messagePattern":"Trying to remove a block from file (.+?) which is not under construction\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSEditLogLoader.java","lineNumber":1178,"sourceCode":"          setGenerationStampIfGreater(newGenerationStamp);\n\n      if (!oldBlock.isComplete() &&\n          (!isLastBlock || op.shouldCompleteLastBlock())) {\n        changeMade = true;\n        fsNamesys.getBlockManager().forceCompleteBlock(oldBlock);\n      }\n      if (changeMade) {\n        // The state or gen-stamp of the block has changed. So, we may be\n        // able to process some messages from datanodes that we previously\n        // were unable to process.\n        fsNamesys.getBlockManager().processQueuedMessagesForBlock(newBlock);\n      }\n    }\n    \n    if (newBlocks.length < oldBlocks.length) {\n      // We're removing a block from the file, e.g. abandonBlock(...)\n      if (!file.isUnderConstruction()) {\n        throw new IOException(\"Trying to remove a block from file \" +\n            path + \" which is not under construction.\");\n      }\n      if (newBlocks.length != oldBlocks.length - 1) {\n        throw new IOException(\"Trying to remove more than one block from file \"\n            + path);\n      }\n      Block oldBlock = oldBlocks[oldBlocks.length - 1];\n      boolean removed = FSDirWriteFileOp.unprotectedRemoveBlock(\n          fsDir, path, iip, file, oldBlock);\n      if (!removed && !(op instanceof UpdateBlocksOp)) {\n        throw new IOException(\"Trying to delete non-existant block \" + oldBlock);\n      }\n    } else if (newBlocks.length > oldBlocks.length) {\n      final boolean isStriped = ecPolicy != null;\n      // We're adding blocks\n      for (int i = oldBlocks.length; i < newBlocks.length; i++) {\n        Block newBlock = newBlocks[i];\n        final BlockInfo newBI;","sourceCodeStart":1160,"sourceCodeEnd":1196,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSEditLogLoader.java#L1160-L1196","documentation":"In updateBlocks, a new block list shorter than the current one means a block abandon, which is only legal for a file under construction (abandonBlock on a file being written). If the file is already complete, the namespace state and the op disagree and replay throws IOException('Trying to remove a block from file <path> which is not under construction.').","triggerScenarios":"A block-removing list update replayed against a completed file: image and edits from different generations, duplicate replay of an update, or corrupted op data.","commonSituations":"Restored image already contains the file closed while later edits still abandon blocks; duplicated segments after manual recovery.","solutions":["Restore fsimage and edits from the same checkpoint epoch","Run 'hdfs namenode -recover' to skip the inconsistent transaction","Remove duplicated or overlapping segment files so the replay sequence is linear"],"exampleFix":"# before: edits replayed against a stale completed file\n# IOException: Trying to remove a block from file /data/x which is not under construction.\n\n# after: consistent restore\nrm -rf /hadoop/dfs/name/current && cp -a backup/current /hadoop/dfs/name/current","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  loader.loadFSEdits(storage, 0);\n} catch (IOException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"which is not under construction\")) {\n    // a block removal replayed against a completed file: restore consistent\n    // image+edits or run 'hdfs namenode -recover'\n  }\n  throw e;\n}","preventionTips":["Restore name directories as whole units from one backup","Replay in a staging environment before restarting production NameNodes"],"tags":["hdfs","namenode","edit-log","replay","blocks","abandon-block"],"backgroundTag":"edit-log-replay-inconsistency","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}