@@ -383,40 +383,50 @@ def clear_execution(self, execution_id: bytes) -> None:
383383
384384
385385class _AggregateConstArgsCache :
386- """Process-local LRU of aggregate const_args, keyed by execution_id.
386+ """Process-local LRU of aggregate const_args, keyed by (shard_key, execution_id) .
387387
388388 Const args are written once at aggregate_bind (under group_id=-2) and
389389 never change. Every aggregate_update / aggregate_window / aggregate_finalize
390390 needs them, and ``_load_aggregate_const_args`` would otherwise issue one
391391 storage read per call — pathological under remote storage backends.
392392
393+ Scoping by ``(shard_key, execution_id)`` mirrors the storage row key:
394+ const args live under ``(shard_key) -> aggregate_state(execution_id,
395+ group_id=-2)``. Including shard_key in the cache key prevents a stale
396+ hit if two different attaches in the same worker process ever produce
397+ colliding execution_id bytes (UUIDs make this vanishingly rare, but the
398+ bound is free and matches storage semantics).
399+
393400 Cache holds either ``Arguments`` (positive hit), ``_ABSENT_SENTINEL``
394401 (the worker has no const params; remember the negative result to skip
395- future reads), or absent (not yet looked up).
402+ future reads), or absent (not yet looked up). Bounded LRU eviction caps
403+ memory; aggregate_destructor proactively evicts.
396404 """
397405
398- def __init__ (self , max_size : int = 1024 ) -> None :
399- self ._entries : OrderedDict [bytes , Any ] = OrderedDict ()
406+ def __init__ (self , max_size : int = 256 ) -> None :
407+ self ._entries : OrderedDict [tuple [ str , bytes ] , Any ] = OrderedDict ()
400408 self ._lock = Lock ()
401409 self ._max_size = max_size
402410
403- def get (self , execution_id : bytes ) -> Any :
411+ def get (self , shard_key : str , execution_id : bytes ) -> Any :
412+ key = (shard_key , execution_id )
404413 with self ._lock :
405- entry = self ._entries .get (execution_id )
414+ entry = self ._entries .get (key )
406415 if entry is not None :
407- self ._entries .move_to_end (execution_id )
416+ self ._entries .move_to_end (key )
408417 return entry
409418
410- def put (self , execution_id : bytes , value : Any ) -> None :
419+ def put (self , shard_key : str , execution_id : bytes , value : Any ) -> None :
420+ key = (shard_key , execution_id )
411421 with self ._lock :
412- self ._entries [execution_id ] = value
413- self ._entries .move_to_end (execution_id )
422+ self ._entries [key ] = value
423+ self ._entries .move_to_end (key )
414424 while len (self ._entries ) > self ._max_size :
415425 self ._entries .popitem (last = False )
416426
417- def clear_execution (self , execution_id : bytes ) -> None :
427+ def clear_execution (self , shard_key : str , execution_id : bytes ) -> None :
418428 with self ._lock :
419- self ._entries .pop (execution_id , None )
429+ self ._entries .pop (( shard_key , execution_id ) , None )
420430
421431
422432_aggregate_const_args_cache = _AggregateConstArgsCache ()
@@ -1781,7 +1791,9 @@ def _load_aggregate_const_args(
17811791 """
17821792 from vgi .arguments import Arguments
17831793
1784- cached = _aggregate_const_args_cache .get (storage ._execution_id )
1794+ shard_key = storage ._shard_key
1795+ execution_id = storage ._execution_id
1796+ cached = _aggregate_const_args_cache .get (shard_key , execution_id )
17851797 if cached is _ABSENT_SENTINEL :
17861798 return None
17871799 if cached is not None :
@@ -1792,10 +1804,10 @@ def _load_aggregate_const_args(
17921804 # Most aggregates have no const params; cache the negative result
17931805 # so we don't re-hit storage on every aggregate_window /
17941806 # aggregate_update / etc.
1795- _aggregate_const_args_cache .put (storage . _execution_id , _ABSENT_SENTINEL )
1807+ _aggregate_const_args_cache .put (shard_key , execution_id , _ABSENT_SENTINEL )
17961808 return None
17971809 parsed = Arguments .deserialize_from_bytes (result [0 ][1 ])
1798- _aggregate_const_args_cache .put (storage . _execution_id , parsed )
1810+ _aggregate_const_args_cache .put (shard_key , execution_id , parsed )
17991811 return parsed
18001812
18011813 def aggregate_bind (
@@ -2116,7 +2128,7 @@ def aggregate_destructor(
21162128 # RPC was dropped mid-query.
21172129 storage .aggregate_window_partition_clear ()
21182130 _window_partition_cache .clear_execution (request .execution_id )
2119- _aggregate_const_args_cache .clear_execution (request .execution_id )
2131+ _aggregate_const_args_cache .clear_execution (storage . _shard_key , request .execution_id )
21202132
21212133 return AggregateDestructorResponse ()
21222134
0 commit comments