{"record":{"id":"4d5b570162001abb","repo":"apache/hadoop","slug":"update-size-to-a-smaller-size-block-si","errorCode":null,"errorMessage":"Update {} (size={}) to a smaller size block {} (size={})","messagePattern":"Update (.+?) \\(size=(.+?)\\) to a smaller size block (.+?) \\(size=(.+?)\\)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java","lineNumber":6208,"sourceCode":"    final INodeFile pendingFile = checkUCBlock(oldBlock, clientName);\n    final String src = pendingFile.getFullPathName();\n    final BlockInfo lastBlock = pendingFile.getLastBlock();\n    assert !lastBlock.isComplete();\n\n    // check new GS & length: this is not expected\n    if (newBlock.getGenerationStamp() <= lastBlock.getGenerationStamp()) {\n      final String msg = \"Update \" + oldBlock + \" but the new block \" + newBlock\n          + \" does not have a larger generation stamp than the last block \"\n          + lastBlock;\n      LOG.warn(msg);\n      throw new IOException(msg);\n    }\n    if (newBlock.getNumBytes() < lastBlock.getNumBytes()) {\n      final String msg = \"Update \" + oldBlock + \" (size=\"\n          + oldBlock.getNumBytes() + \") to a smaller size block \" + newBlock\n          + \" (size=\" + newBlock.getNumBytes() + \")\";\n      LOG.warn(msg);\n      throw new IOException(msg);\n    }\n\n    // Update old block with the new generation stamp and new length\n    blockManager.updateLastBlock(lastBlock, newBlock);\n\n    // find the DatanodeDescriptor objects\n    final DatanodeStorageInfo[] storages = blockManager.getDatanodeManager()\n        .getDatanodeStorageInfos(newNodes, newStorageIDs,\n            \"src=%s, oldBlock=%s, newBlock=%s, clientName=%s\",\n            src, oldBlock, newBlock, clientName);\n    lastBlock.getUnderConstructionFeature().setExpectedLocations(lastBlock,\n        storages, lastBlock.getBlockType());\n\n    FSDirWriteFileOp.persistBlocks(dir, src, pendingFile, logRetryCache);\n  }\n\n  /**\n   * Register a Backup name-node, verifying that it belongs","sourceCodeStart":6190,"sourceCodeEnd":6226,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java#L6190-L6226","documentation":"updateBlock rejects shrinking a block: if newBlock.getNumBytes() is less than the last block's current length, the update is refused. Data already reported cannot be un-reported via updateBlock; length reduction is only legal through the truncate API.","triggerScenarios":"Pipeline recovery retry that carries a smaller length after a partial failure; client code attempting to truncate by submitting an updateBlock with fewer bytes.","commonSituations":"Hand-rolled recovery logic confusing truncate with block update; retries racing replica length reports.","solutions":["Use the truncate API (dfs.truncate / ClientProtocol.truncate) to shorten a file","In recovery paths, never send a new length below the current one; re-read the block's length first","Validate new GS and length against the LocatedBlock before submitting"],"exampleFix":"// before\nExtendedBlock nb = new ExtendedBlock(bp, blockId, smallerLen, newGs);\nnn.updateBlock(pendingFile, lastBlock, nb, false);\n\n// after\nif (smallerLen < lastBlock.getNumBytes()) {\n  dfs.truncate(path, smallerLen); // proper way to shrink\n} else {\n  ExtendedBlock nb = new ExtendedBlock(bp, blockId, newLen, newGs);\n  nn.updateBlock(pendingFile, lastBlock, nb, false);\n}","handlingStrategy":"validation","validationCode":"if (newBlock.getNumBytes() < lastBlock.getNumBytes()) {\n  throw new IllegalArgumentException(\n      \"updateBlock cannot shrink a block; use dfs.truncate instead\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  updateBlock(oldBlock, newBlock, ...);\n} catch (IOException e) {\n  if (e.getMessage().contains(\"smaller size block\")) {\n    dfs.truncate(path, targetLen); // proper shrink path\n  } else { throw e; }\n}","preventionTips":["Never submit a smaller length through updateBlock; use the truncate API","Re-read the last block's length before building recovery updates"],"tags":["hdfs","namenode","block-update","truncate","validation"],"backgroundTag":"invalid-block-update","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}