lawofcycles commented on code in PR #3320:
URL: https://github.com/apache/iceberg-python/pull/3320#discussion_r3609827211


##########
pyiceberg/table/update/snapshot.py:
##########
@@ -353,11 +400,101 @@ def new_manifest_output(self) -> OutputFile:
         location_provider = self._transaction._table.location_provider()
         file_name = 
_new_manifest_file_name(num=next(self._manifest_num_counter), 
commit_uuid=self.commit_uuid)
         file_path = location_provider.new_metadata_location(file_name)
+        self._written_manifests.append(file_path)
         return self._io.new_output(file_path)
 
     def fetch_manifest_entry(self, manifest: ManifestFile, discard_deleted: 
bool = True) -> list[ManifestEntry]:
         return manifest.fetch_manifest_entry(io=self._io, 
discard_deleted=discard_deleted)
 
+    def commit(self) -> None:
+        self._transaction._register_snapshot_producer(self)
+        super().commit()
+
+    def _cleanup_uncommitted(self) -> None:
+        """Delete manifest files and manifest lists from failed retry 
attempts."""
+        for path in self._uncommitted_manifests:
+            try:
+                self._io.delete(path)
+            except Exception:
+                logger.warning("Failed to delete uncommitted manifest: %s", 
path, exc_info=True)
+        self._uncommitted_manifests.clear()
+        # Delete all manifest lists except the last one (which the committed 
snapshot references)
+        if len(self._written_manifest_lists) > 1:
+            for path in self._written_manifest_lists[:-1]:
+                try:
+                    self._io.delete(path)
+                except Exception:
+                    logger.warning("Failed to delete uncommitted manifest 
list: %s", path, exc_info=True)
+            self._written_manifest_lists = self._written_manifest_lists[-1:]
+
+    def _clean_all_uncommitted(self) -> None:
+        """Clean up all manifests and manifest lists on abort."""
+        for path in itertools.chain(self._uncommitted_manifests, 
self._written_manifests):
+            try:
+                self._io.delete(path)
+            except Exception:
+                logger.warning("Failed to delete uncommitted manifest: %s", 
path, exc_info=True)
+        for path in self._written_manifest_lists:
+            try:
+                self._io.delete(path)
+            except Exception:
+                logger.warning("Failed to delete uncommitted manifest list: 
%s", path, exc_info=True)
+        self._uncommitted_manifests.clear()
+        self._written_manifests.clear()
+        self._written_manifest_lists.clear()
+
+    def _refresh_for_retry(self) -> None:
+        """Reset state for a retry attempt with refreshed metadata."""
+        self._uncommitted_manifests.extend(self._written_manifests)
+        self._written_manifests.clear()
+        self._parent_snapshot_id = self._current_branch_head_id()
+        self._snapshot_id = self._transaction.table_metadata.new_snapshot_id()

Review Comment:
   Good catch. The snapshot id is now stable across attempts, so it works as an 
idempotency key: on retry I stop if it is already in the refreshed metadata. 
That fixes the double commit your test covers, where the landed commit is 
reported as CommitFailedException. Added it as a regression.
   
   This recognizes a landed commit only when the failure is a 
CommitFailedException. Errors like a connection drop or a 5xx 
(CommitStateUnknownException) never retry, so they will not double commit, but 
they surface as errors instead of being recognized as landed. Handling those 
needs an active catalog status check like Java's checkCommitStatus, which I 
will leave as a follow-up.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to