{"record":{"id":"9a61c2adbb048a46","repo":"apache/hadoop","slug":"not-implemented-by-the-filesystem-implementatio","errorCode":null,"errorMessage":"Not implemented by the {} FileSystem implementation","messagePattern":"Not implemented by the (.+?) FileSystem implementation","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java","lineNumber":1750,"sourceCode":"   *   <li>Fails if path does not exist.</li>\n   *   <li>Fails if path is not closed.</li>\n   *   <li>Fails if new size is greater than current size.</li>\n   * </ul>\n   * @param f The path to the file to be truncated\n   * @param newLength The size the file is to be truncated to\n   *\n   * @return <code>true</code> if the file has been truncated to the desired\n   * <code>newLength</code> and is immediately available to be reused for\n   * write operations such as <code>append</code>, or\n   * <code>false</code> if a background process of adjusting the length of\n   * the last block has been started, and clients should wait for it to\n   * complete before proceeding with further file updates.\n   * @throws IOException IO failure\n   * @throws UnsupportedOperationException if the operation is unsupported\n   *         (default).\n   */\n  public boolean truncate(Path f, long newLength) throws IOException {\n    throw new UnsupportedOperationException(\"Not implemented by the \" +\n        getClass().getSimpleName() + \" FileSystem implementation\");\n  }\n\n  /**\n   * Delete a file/directory.\n   * @param f the path.\n   * @throws IOException IO failure.\n   * @return if delete success true, not false.\n   * @deprecated Use {@link #delete(Path, boolean)} instead.\n   */\n  @Deprecated\n  public boolean delete(Path f) throws IOException {\n    return delete(f, true);\n  }\n\n  /** Delete a file.\n   *\n   * @param f the path to delete.","sourceCodeStart":1732,"sourceCodeEnd":1768,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java#L1732-L1768","documentation":"truncate(Path, newLength) was added in Hadoop 2.7 (HDFS-3107) with no generic implementation: the base FileSystem throws UnsupportedOperationException. Truncation requires block-level recovery, so only HDFS implements it natively; most other filesystems (and older connector versions) fail here.","triggerScenarios":"Calling fs.truncate(path, len) on a non-HDFS FileSystem or on an implementation built from an older Hadoop that lacks the override (old local FS, har://, various connectors).","commonSituations":"Applications written against HDFS truncate semantics and ported to S3/local storage; libraries using truncate to maintain fixed-size record files run in local tests.","solutions":["Gate on implementation: only truncate when fs instanceof DistributedFileSystem","Emulate for other stores: copy the first newLength bytes to a temp file, delete the original, rename temp back","Upgrade the connector/Hadoop if a newer version implements truncate for your store"],"exampleFix":"// before\nfs.truncate(path, newLen); // UnsupportedOperationException\n\n// after\nif (fs instanceof DistributedFileSystem) {\n  fs.truncate(path, newLen);\n} else {\n  Path tmp = path.suffix(\".trunc\");\n  byte[] buf = new byte[8192];\n  try (FSDataInputStream in = fs.open(path);\n       FSDataOutputStream out = fs.create(tmp, true)) {\n    long remaining = newLen;\n    while (remaining > 0) {\n      int n = in.read(buf, 0, (int) Math.min(buf.length, remaining));\n      if (n < 0) break;\n      out.write(buf, 0, n);\n      remaining -= n;\n    }\n  }\n  fs.delete(path, false);\n  fs.rename(tmp, path);\n}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"public static boolean canTruncateInPlace(FileSystem fs) {\n  return fs instanceof DistributedFileSystem;\n}","tryCatchPattern":"try {\n  fs.truncate(path, newLen);\n} catch (UnsupportedOperationException e) {\n  truncateByCopy(fs, path, newLen); // copy prefix, delete, rename back\n}","preventionTips":["Probe filesystem capabilities before using optional APIs","Keep a copy-based truncate fallback for portable code","Pin the minimum Hadoop/connector version that supports the APIs you call"],"tags":["hadoop","filesystem","truncate","unsupported-operation","hdfs"],"backgroundTag":"unsupported-filesystem-operation","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}