{"record":{"id":"1f296faa8f365988","repo":"apache/hadoop","slug":"providedreplica-does-not-yet-support-truncate","errorCode":null,"errorMessage":"ProvidedReplica does not yet support truncate","messagePattern":"ProvidedReplica does not yet support truncate","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/ProvidedReplica.java","lineNumber":324,"sourceCode":"      throws UnsupportedOperationException {\n    throw new UnsupportedOperationException(\n        \"ProvidedReplica does not yet support writes\");\n  }\n\n  @Override\n  public int compareWith(ScanInfo info) {\n    if (info.getFileRegion().equals(\n        new FileRegion(this.getBlockId(), new Path(getRemoteURI()),\n            fileOffset, this.getNumBytes(), this.getGenerationStamp()))) {\n      return 0;\n    } else {\n      return (int) (info.getBlockLength() - getNumBytes());\n    }\n  }\n\n  @Override\n  public void truncateBlock(long newLength) throws IOException {\n    throw new UnsupportedOperationException(\n        \"ProvidedReplica does not yet support truncate\");\n  }\n\n  @Override\n  public void updateWithReplica(StorageLocation replicaLocation) {\n    throw new UnsupportedOperationException(\n        \"ProvidedReplica does not yet support update\");\n  }\n\n  @Override\n  public void copyMetadata(URI destination) throws IOException {\n    throw new UnsupportedOperationException(\n        \"ProvidedReplica does not yet support copy metadata\");\n  }\n\n  @Override\n  public void copyBlockdata(URI destination) throws IOException {\n    throw new UnsupportedOperationException(","sourceCodeStart":306,"sourceCodeEnd":342,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/ProvidedReplica.java#L306-L342","documentation":"truncateBlock(long) shortens a replica's data (and meta) file when a client truncates a file. ProvidedReplica throws UnsupportedOperationException because the bytes live in an immutable external store; the DataNode cannot rewrite them to a new length.","triggerScenarios":"A client calls DistributedFileSystem.truncate() (or HdfsDataOutputStream-level truncate APIs) on a file whose blocks resolve to PROVIDED replicas; the DataNode-side truncate path reaches truncateBlock on a ProvidedReplica and throws.","commonSituations":"Read-only provided mounts (e.g. S3HDFS/aliased imports) exposed as writable-looking namespaces; users run compaction or truncation jobs against them. Also custom FsDataset tests that call truncateBlock on every replica type.","solutions":["Do not issue truncate against files on provided storage; enforce this at the application/job layer or via namespace permissions.","If truncation is genuinely needed, copy the file to local HDFS storage first, truncate there, and write back as a new object.","Pre-check replica type (instanceof ProvidedReplica / volume SupportedOperation) before truncateBlock and fail with a descriptive IOException.","Catch UnsupportedOperationException at the truncation worker boundary and surface a clear 'read-only provided storage' error to the client."],"exampleFix":"// before\nreplica.truncateBlock(newLength);\n\n// after\nif (replica instanceof ProvidedReplica) {\n  throw new IOException(\"PROVIDED replica \" + replica.getBlockId()\n      + \" is read-only; truncate is not supported\");\n}\nreplica.truncateBlock(newLength);","handlingStrategy":"validation","validationCode":"if (replica instanceof ProvidedReplica) {\n  throw new IOException(\"PROVIDED replica \" + replica.getBlockId()\n      + \" is read-only; truncate is not supported\");\n}\nreplica.truncateBlock(newLength);","typeGuard":"static boolean isProvided(ReplicaInfo r) {\n  return r instanceof ProvidedReplica;\n}","tryCatchPattern":"try {\n  replica.truncateBlock(newLength);\n} catch (UnsupportedOperationException e) {\n  throw new IOException(\"Cannot truncate file on provided storage\", e);\n}","preventionTips":["Enforce at the application layer: no truncate jobs against provided mounts.","If truncation is required, copy to local storage, truncate, and write back.","Check replica/volume type before any size-mutating operation."],"tags":["hdfs","datanode","provided-storage","truncate","read-only"],"backgroundTag":"unsupported-operation","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}