1313from types import GeneratorType
1414from enum import Enum
1515from typing import Any , Generator , Optional , Sequence , TypeVar , Union
16+ import uuid
1617from packaging .version import InvalidVersion , parse
1718
1819import grpc
3334
3435TInput = TypeVar ("TInput" )
3536TOutput = TypeVar ("TOutput" )
37+ DATETIME_STRING_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
3638
3739
3840class ConcurrencyOptions :
@@ -832,6 +834,7 @@ def __init__(self, instance_id: str, registry: _Registry):
832834 # Maps criticalSectionId to task ID
833835 self ._entity_lock_id_map : dict [str , int ] = {}
834836 self ._sequence_number = 0
837+ self ._new_uuid_counter = 0
835838 self ._current_utc_datetime = datetime (1000 , 1 , 1 )
836839 self ._instance_id = instance_id
837840 self ._registry = registry
@@ -1166,7 +1169,7 @@ def call_entity_function_helper(
11661169 raise RuntimeError (error_message )
11671170
11681171 encoded_input = shared .to_json (input ) if input is not None else None
1169- action = ph .new_call_entity_action (id , self .instance_id , entity_id , operation , encoded_input )
1172+ action = ph .new_call_entity_action (id , self .instance_id , entity_id , operation , encoded_input , self . new_uuid () )
11701173 self ._pending_actions [id ] = action
11711174
11721175 fn_task = task .CompletableTask ()
@@ -1189,7 +1192,7 @@ def signal_entity_function_helper(
11891192
11901193 encoded_input = shared .to_json (input ) if input is not None else None
11911194
1192- action = ph .new_signal_entity_action (id , entity_id , operation , encoded_input )
1195+ action = ph .new_signal_entity_action (id , entity_id , operation , encoded_input , self . new_uuid () )
11931196 self ._pending_actions [id ] = action
11941197
11951198 def lock_entities_function_helper (self , id : int , entities : list [EntityInstanceId ]) -> None :
@@ -1200,7 +1203,7 @@ def lock_entities_function_helper(self, id: int, entities: list[EntityInstanceId
12001203 if not transition_valid :
12011204 raise RuntimeError (error_message )
12021205
1203- critical_section_id = f" { self .instance_id } : { id :04x } "
1206+ critical_section_id = self .new_uuid ()
12041207
12051208 request , target = self ._entity_context .emit_acquire_message (critical_section_id , entities )
12061209
@@ -1252,6 +1255,17 @@ def continue_as_new(self, new_input, *, save_events: bool = False) -> None:
12521255
12531256 self .set_continued_as_new (new_input , save_events )
12541257
1258+ def new_uuid (self ) -> str :
1259+ NAMESPACE_UUID : str = "9e952958-5e33-4daf-827f-2fa12937b875"
1260+
1261+ uuid_name_value = \
1262+ f"{ self ._instance_id } " \
1263+ f"_{ self .current_utc_datetime .strftime (DATETIME_STRING_FORMAT )} " \
1264+ f"_{ self ._new_uuid_counter } "
1265+ self ._new_uuid_counter += 1
1266+ namespace_uuid = uuid .uuid5 (uuid .NAMESPACE_OID , NAMESPACE_UUID )
1267+ return str (uuid .uuid5 (namespace_uuid , uuid_name_value ))
1268+
12551269
12561270class ExecutionResults :
12571271 actions : list [pb .OrchestratorAction ]
0 commit comments