ZhYQ1 commented on code in PR #439:
URL: https://github.com/apache/hugegraph-ai/pull/439#discussion_r4661683123


##########
ontogeny/server/ontogeny/api/routes_objects.py:
##########
@@ -203,18 +204,49 @@ async def execute_action(
         started = time.perf_counter()
         from ..errors import PolicyDeniedError, RuleRejectedError
 
-        async with sc.sessionmaker() as s:
+        # Keep same-key executions serialized until the revision is committed;
+        # exiting the session first also rolls back failures before unlocking.
+        async with (
+            sc.runtime.idempotency_guard(action_name, principal, 
body.idempotency_key),
+            sc.sessionmaker() as s,
+        ):
             try:
-                rev = await sc.runtime.execute(
-                    s,
-                    action_name,
-                    principal,
-                    body.parameters,
-                    body.target_id,
-                    expected_revision=body.expected_revision,
-                    idempotency_key=body.idempotency_key,
-                )
-                await s.commit()
+                try:
+                    rev = await sc.runtime.execute(
+                        s,
+                        action_name,
+                        principal,
+                        body.parameters,
+                        body.target_id,
+                        expected_revision=body.expected_revision,
+                        idempotency_key=body.idempotency_key,
+                    )
+                    await s.commit()
+                except IntegrityError:
+                    # The per-key lock only serializes one process. When 
another
+                    # worker wins the same key, roll back and resolve the 
winner's
+                    # committed revision instead of surfacing a 500.
+                    if not body.idempotency_key:
+                        raise
+                    await s.rollback()
+                    rev = await sc.runtime.await_committed_revision(s, 
action_name, principal, body.idempotency_key)
+                    if rev is None:
+                        raise
+                except ConflictError:

Review Comment:
   Fixed. Added a sibling`except NotFoundError` branch: when the request 
carries an`idempotency_key` , the route rolls back, rechecks the key once 
(`attempts=1` ), replays the committed revision if found, and otherwise 
re-raises the genuine 404. One recheck is sufficient because the archive and 
the revision are committed in the same transaction — by the time the loser's 
target load sees the row gone, the winner's revision is already visible.
   
   Test:`test_concurrent_archive_replays_committed_revision` 
uses`retract-inspection` to archive`QI-3002` with key K; the second request is 
forced to miss the replay lookup, loads the now-archived target and 
raises`NotFoundError` inside`execute()` . It asserts`200` with the 
same`revision_id` and`object_id` as the first response.



-- 
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