22
33from __future__ import annotations
44
5+ import hashlib
56import inspect
67from typing import TYPE_CHECKING
78
@@ -48,20 +49,24 @@ def token_submitter(
4849 execution_ref : str ,
4950 name : str ,
5051 hand_off : Callable [[str ], object ],
52+ operation_id : str | None = None ,
5153) -> Callable [[str , object ], None ]:
5254 """Build the runtime's submitter: register the callback, then hand its token on.
5355
5456 It runs inside the runtime's checkpointed submitter step, so a replay does
5557 not register the callback again, and a step that runs again registers the
56- same callback and gets the same token.
58+ same callback and gets the same token. `operation_id` names the operation
59+ an earlier invocation opened the callback under; see `replayed_operation`.
5760
5861 Returns:
5962 The submitter the runtime calls with the callback's id.
6063
6164 """
6265
6366 def submit (callback_id : str , _context : object ) -> None :
64- token = register_callback (api_url , execution_ref , callback_id , name )
67+ token = register_callback (
68+ api_url , execution_ref , callback_id , name , operation_id
69+ )
6570 # A closed callback already timed out or was answered; the wait
6671 # resumes as it did.
6772 if token is not None :
@@ -70,6 +75,41 @@ def submit(callback_id: str, _context: object) -> None:
7075 return submit
7176
7277
78+ def replayed_operation (context : object ) -> str | None :
79+ """Name the operation an earlier invocation opened the next callback under.
80+
81+ Volcano then reads back to that callback rather than only through recent
82+ history. Call it just before the runtime's `wait_for_callback`: the wait
83+ runs in a child context named by the operation about to start, and opens
84+ the callback as that context's first operation. Neither the next operation
85+ nor the checkpoint is public, so a runtime without them names no operation
86+ and the registration reads as a first run's does.
87+
88+ Returns:
89+ The callback's operation id, or None when this invocation opens it.
90+
91+ """
92+ peek = getattr (context , "_peek_next_operation_id" , None )
93+ checkpoint = getattr (getattr (context , "state" , None ), "get_checkpoint_result" , None )
94+ if not callable (peek ) or not callable (checkpoint ):
95+ return None
96+ operation_id = callback_operation_id (str (peek ()))
97+ existent = getattr (checkpoint (operation_id ), "is_existent" , None )
98+ if not callable (existent ) or not existent ():
99+ return None
100+ return operation_id
101+
102+
103+ def callback_operation_id (child_id : str ) -> str :
104+ """Name the callback a wait's child context `child_id` opens first.
105+
106+ Returns:
107+ The id the runtime checkpoints the callback under.
108+
109+ """
110+ return hashlib .blake2b (f"{ child_id } -1" .encode ()).hexdigest ()[:64 ]
111+
112+
73113def require_synchronous (submitter : object ) -> None :
74114 """Refuse an `async def` submitter before its callback opens.
75115
@@ -99,7 +139,11 @@ def handed_off(result: object) -> None:
99139
100140
101141def register_callback (
102- api_url : str , execution_ref : str , callback_id : str , name : str
142+ api_url : str ,
143+ execution_ref : str ,
144+ callback_id : str ,
145+ name : str ,
146+ operation_id : str | None = None ,
103147) -> str | None :
104148 """Ask Volcano for the token that completes the callback.
105149
@@ -118,6 +162,8 @@ def register_callback(
118162 "callback_id" : callback_id ,
119163 "name" : name ,
120164 }
165+ if operation_id is not None :
166+ body ["operation_id" ] = operation_id
121167 try :
122168 answer = post_to_platform (
123169 api_url , PlatformRequest ("/durable-callbacks" , body , not_ready = _NOT_READY )
0 commit comments