{"record":{"id":"fbfbce019e1fcaf1","repo":"apache/hadoop","slug":"cannot-truncate-block-to-from-oldlen-oldlen","errorCode":null,"errorMessage":"Cannot truncate block to from oldlen (=${oldlen}) to newlen (=${newlen})","messagePattern":"Cannot truncate block to from oldlen \\(=(.+?)\\) to newlen \\(=(.+?)\\)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/LocalReplica.java","lineNumber":488,"sourceCode":"    FsPermission permission = new FsPermission(oldPermission.getUserAction(),\n        oldPermission.getGroupAction(), oldPermission.getOtherAction(), true);\n    localFS.setPermission(path, permission);\n  }\n\n  public static void truncateBlock(\n      FsVolumeSpi volume, File blockFile, File metaFile,\n      long oldlen, long newlen, FileIoProvider fileIoProvider)\n      throws IOException {\n    LOG.info(\"truncateBlock: blockFile=\" + blockFile\n        + \", metaFile=\" + metaFile\n        + \", oldlen=\" + oldlen\n        + \", newlen=\" + newlen);\n\n    if (newlen == oldlen) {\n      return;\n    }\n    if (newlen > oldlen) {\n      throw new IOException(\"Cannot truncate block to from oldlen (=\" + oldlen\n          + \") to newlen (=\" + newlen + \")\");\n    }\n\n    // fis is closed by BlockMetadataHeader.readHeader.\n    final FileInputStream fis = fileIoProvider.getFileInputStream(\n        volume, metaFile);\n    DataChecksum dcs = BlockMetadataHeader.readHeader(fis).getChecksum();\n    int checksumsize = dcs.getChecksumSize();\n    int bpc = dcs.getBytesPerChecksum();\n    long n = (newlen - 1)/bpc + 1;\n    long newmetalen = BlockMetadataHeader.getHeaderSize() + n*checksumsize;\n    long lastchunkoffset = (n - 1)*bpc;\n    int lastchunksize = (int)(newlen - lastchunkoffset);\n    byte[] b = new byte[Math.max(lastchunksize, checksumsize)];\n\n    try (RandomAccessFile blockRAF = fileIoProvider.getRandomAccessFile(\n        volume, blockFile, \"rw\")) {\n      //truncate blockFile","sourceCodeStart":470,"sourceCodeEnd":506,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/LocalReplica.java#L470-L506","documentation":"Error \"Cannot truncate block to from oldlen (=${oldlen}) to newlen (=${newlen})\" thrown in apache/hadoop.","triggerScenarios":"A truncate request asks to change a block from oldlen to newlen in a way the replica does not support (e.g., newlen > oldlen or invalid state).","commonSituations":"A truncate was requested with a new length inconsistent with the current block length. Ensure the truncate length is smaller than the current length.","solutions":["Verify the truncate length is not larger than the current block length and the replica is in a state that allows truncation.","Retry the operation; if it repeats, check the block's recovery state and DataNode logs for conflicting writers."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}