{"record":{"id":"e05ea786914a58e3","repo":"apache/hadoop","slug":"cannot-truncate-to-a-larger-file-size-current-siz-e05ea7","errorCode":null,"errorMessage":"Cannot truncate to a larger file size. Current size: <oldLength>, truncate size: <newLength>.","messagePattern":"Cannot truncate to a larger file size\\. Current size: <oldLength>, truncate size: <newLength>\\.","errorType":"exception","errorClass":"HadoopIllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirTruncateOp.java","lineNumber":133,"sourceCode":"            return new TruncateResult(false, fsd.getAuditFileInfo(iip));\n          } else {\n            throw new AlreadyBeingCreatedException(\n                RecoverLeaseOp.TRUNCATE_FILE.getExceptionMessage(src,\n                    clientName, clientMachine, src + \" is being truncated.\"));\n          }\n        }\n      }\n\n      // Opening an existing file for truncate. May need lease recovery.\n      fsn.recoverLeaseInternal(RecoverLeaseOp.TRUNCATE_FILE, iip, src,\n          clientName, clientMachine, false);\n      // Truncate length check.\n      long oldLength = file.computeFileSize();\n      if (oldLength == newLength) {\n        return new TruncateResult(true, fsd.getAuditFileInfo(iip));\n      }\n      if (oldLength < newLength) {\n        throw new HadoopIllegalArgumentException(\n            \"Cannot truncate to a larger file size. Current size: \" + oldLength\n                + \", truncate size: \" + newLength + \".\");\n      }\n      // Perform INodeFile truncation.\n      final QuotaCounts delta = new QuotaCounts.Builder().build();\n      onBlockBoundary = unprotectedTruncate(fsn, iip, newLength,\n          toRemoveBlocks, mtime, delta);\n      if (!onBlockBoundary) {\n        // Open file for write, but don't log into edits\n        long lastBlockDelta = file.computeFileSize() - newLength;\n        assert lastBlockDelta > 0 : \"delta is 0 only if on block bounday\";\n        truncateBlock = prepareFileForTruncate(fsn, iip, clientName,\n            clientMachine, lastBlockDelta, null);\n      }\n\n      // update the quota: use the preferred block size for UC block\n      fsd.updateCountNoQuotaCheck(iip, iip.length() - 1, delta);\n    } finally {","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirTruncateOp.java#L115-L151","documentation":"HDFS truncate can only shrink a file. FSDirTruncateOp compares the current file size with the requested newLength after lease recovery and throws HadoopIllegalArgumentException when newLength is greater than the current size; equal lengths are a no-op that returns success. This is pure client-argument validation that fails before any block is modified.","triggerScenarios":"Calling DistributedFileSystem.truncate(path, newLength) with newLength greater than the file's current length: stale cached size, a race with another truncate that already shrank the file, or an offset/unit computation bug in the caller.","commonSituations":"Log rotation truncating to a remembered offset after the file was already cut shorter; bytes-vs-blocks math errors; retrying a truncate after a concurrent job reduced the file.","solutions":["Re-stat the file immediately before truncating and clamp: newLength = Math.min(newLength, fs.getFileStatus(path).getLen())","If growing the file is the goal, use append() plus writes instead of truncate()","Derive lengths from authoritative getFileStatus() values, not cached ones"],"exampleFix":"// before\ndfs.truncate(path, rememberedSize);\n\n// after\nlong current = fs.getFileStatus(path).getLen();\ndfs.truncate(path, Math.min(newLength, current));","handlingStrategy":"validation","validationCode":"long current = fs.getFileStatus(path).getLen();\nlong target = Math.min(newLength, current);\nif (target != current) {\n  dfs.truncate(path, target);\n}","typeGuard":null,"tryCatchPattern":"try {\n  dfs.truncate(path, newLength);\n} catch (HadoopIllegalArgumentException e) {\n  // re-stat and clamp, or switch to append() if growth was intended\n  long current = fs.getFileStatus(path).getLen();\n  dfs.truncate(path, Math.min(newLength, current));\n}","preventionTips":["Never derive newLength from cached sizes; stat first","Clamp truncate targets to the range [0, current length]","Use append() to grow a file; truncate only shrinks"],"tags":["hdfs","truncate","argument-validation","file-size"],"backgroundTag":"invalid-argument-value","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}