{"record":{"id":"e3ec243829b6cabe","repo":"apache/hadoop","slug":"failed-to-rename-s-to-s-file-already-exists","errorCode":null,"errorMessage":"Failed to rename %s to %s, file already exists!","messagePattern":"Failed to rename (.+?) to (.+?), file already exists!","errorType":"exception","errorClass":"FileAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"hadoop-tools/hadoop-aliyun/src/main/java/org/apache/hadoop/fs/aliyun/oss/AliyunOSSFileSystem.java","lineNumber":671,"sourceCode":"        return !srcStatus.isDirectory();\n      } else if (dstStatus.isDirectory()) {\n        // If dst is a directory\n        dstPath = new Path(dstPath, srcPath.getName());\n        FileStatus[] statuses;\n        try {\n          statuses = listStatus(dstPath);\n        } catch (FileNotFoundException fnde) {\n          statuses = null;\n        }\n        if (statuses != null && statuses.length > 0) {\n          // If dst exists and not a directory / not empty\n          throw new FileAlreadyExistsException(String.format(\n              \"Failed to rename %s to %s, file already exists or not empty!\",\n              srcPath, dstPath));\n        }\n      } else {\n        // If dst is not a directory\n        throw new FileAlreadyExistsException(String.format(\n            \"Failed to rename %s to %s, file already exists!\", srcPath,\n            dstPath));\n      }\n    }\n\n    boolean succeed;\n    if (srcStatus.isDirectory()) {\n      succeed = copyDirectory(srcPath, dstPath);\n    } else {\n      succeed = copyFile(srcPath, srcStatus.getLen(), dstPath);\n    }\n\n    return srcPath.equals(dstPath) || (succeed && delete(srcPath, true));\n  }\n\n  /**\n   * Copy file from source path to destination path.\n   * (the caller should make sure srcPath is a file and dstPath is valid)","sourceCodeStart":653,"sourceCodeEnd":689,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-tools/hadoop-aliyun/src/main/java/org/apache/hadoop/fs/aliyun/oss/AliyunOSSFileSystem.java#L653-L689","documentation":"Thrown by AliyunOSSFileSystem.rename(): the destination path exists and is a file (not a directory). Hadoop rename semantics do not overwrite an existing file in this connector, so it fails with FileAlreadyExistsException(\"Failed to rename %s to %s, file already exists!\") and the source is left untouched.","triggerScenarios":"Calling rename(src, dstFile) when an object already exists at dstFile; e.g., re-running a move phase that first succeeded, or archive layouts where source and destination names coincide on retry.","commonSituations":"Non-idempotent pipelines rerun after partial completion; commit retry moving an already-committed file; users expecting 'mv -f' overwrite behavior, which OSS rename does not implement.","solutions":["Delete the existing destination object first if replacement is intended, then rename","Design moves to be idempotent: check fs.exists(dst) and skip/rename-with-suffix instead of colliding","Clean stale outputs from prior runs before the move stage"],"exampleFix":"// before\nfs.rename(src, dst); // dst exists as a file\n\n// after\nif (fs.exists(dst)) {\n  fs.delete(dst, false);\n}\nfs.rename(src, dst);","handlingStrategy":"validation","validationCode":"if (fs.exists(dst) && fs.getFileStatus(dst).isFile()) {\n  boolean replace = /* policy */ false;\n  if (!replace) throw new IOException(\"Destination exists: \" + dst);\n  fs.delete(dst, false);\n}\nfs.rename(src, dst);","typeGuard":null,"tryCatchPattern":"catch (FileAlreadyExistsException e) { /* destination file exists: apply replace-or-skip policy explicitly */ throw e; }","preventionTips":["Check fs.exists(dst) before rename and apply an explicit replace policy","Remember OSS rename never overwrites; build overwrite in yourself when intended"],"tags":["aliyun-oss","rename","destination-exists","hadoop-connector"],"backgroundTag":"rename-target-exists","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}