[Fix] radix-cache: Separate logical and reclaimable value lengths in tree components (#45238)

Co-authored-by: metamergebot <metamergebot@users.noreply.github.com>
Co-authored-by: Jialin <Jialin@users.noreply.github.com>
This commit is contained in:
metamergebot
2026-10-20 13:24:20 -07:00
committed by GitHub
co-authored by metamergebot Jialin
parent 468e26f253
commit 0b536ca112
16 changed files with 285 additions and 84 deletions
@@ -368,10 +368,11 @@ class TreeComponent(ABC):
return cd.host_value is not None
def value_len(self, node: UnifiedTreeNode) -> int:
"""Length of the stored device indices, including entries without backing."""
value = node.component_data[self.component_type].value
return len(value) if value is not None else 0
def reclaimable_tokens(self, node: UnifiedTreeNode) -> int:
def reclaimable_value_len(self, node: UnifiedTreeNode) -> int:
"""Pool tokens the allocator gets back when this node's device value
is evicted. The evictable/protected ledgers count these (prefill
admission adds the evictable ledger to the allocator's free space, and
@@ -181,8 +181,8 @@ class FullComponent(TreeComponent):
if EvictLayer.DEVICE in target and cd.value is not None:
device_frees[self.component_type].append(cd.value)
# Progress toward an eviction request and the ledger move are
# both in allocator units (see reclaimable_tokens).
freed = self.reclaimable_tokens(node)
# both in allocator units (see reclaimable_value_len).
freed = self.reclaimable_value_len(node)
self.tree_core.component_evictable_size_[self.component_type] -= freed
# NOTE: cd.value = None is deferred to _cascade_evict (Full as trigger)
# because SWA's free_swa still needs to read Full.value.
@@ -310,7 +310,7 @@ class FullComponent(TreeComponent):
f"FULL invariant broken: evicted ancestor {cur.id} above device-on segment"
)
if cd.lock_ref == 0:
key_len = self.reclaimable_tokens(cur)
key_len = self.reclaimable_value_len(cur)
self.tree_core.component_evictable_size_[ct] -= key_len
self.tree_core.component_protected_size_[ct] += key_len
delta += key_len
@@ -359,7 +359,7 @@ class FullComponent(TreeComponent):
f"FULL segment release hit lock_ref=0 on node {cur.id}"
)
if cd.lock_ref == 1 and cd.value is not None:
key_len = self.reclaimable_tokens(cur)
key_len = self.reclaimable_value_len(cur)
self.tree_core.component_evictable_size_[ct] += key_len
self.tree_core.component_protected_size_[ct] -= key_len
cd.lock_ref -= 1
@@ -453,7 +453,7 @@ class FullComponent(TreeComponent):
offset += n_len
# Full uses leaf sets, not LRU. A value materialized under
# lock is protected; the last release moves it to evictable.
ledger = self.reclaimable_tokens(n)
ledger = self.reclaimable_value_len(n)
if cd.lock_ref > 0:
self.tree_core.component_protected_size_[ct] += ledger
else:
@@ -234,8 +234,8 @@ class MambaComponent(TreeComponent):
if is_new_leaf:
node.component_data[self.component_type].value = params.mamba_value
self.tree_core.lru_lists[self.component_type].insert_mru(node)
self.tree_core.component_evictable_size_[self.component_type] += len(
params.mamba_value
self.tree_core.component_evictable_size_[self.component_type] += (
self.reclaimable_value_len(node)
)
self._emit_excess_path_states_eviction(node, cache_actions)
return
@@ -334,7 +334,7 @@ class MambaComponent(TreeComponent):
# Device layer
if EvictLayer.DEVICE in target and cd.value is not None:
device_frees[self.component_type].append(cd.value)
freed = len(cd.value)
freed = self.reclaimable_value_len(node)
self.tree_core.component_evictable_size_[self.component_type] -= freed
cd.value = None
@@ -465,7 +465,7 @@ class MambaComponent(TreeComponent):
cd.host_lock_ref += 1
else:
if cd.lock_ref == 0 and value is not None:
vlen = len(value)
vlen = self.reclaimable_value_len(node)
self.tree_core.component_evictable_size_[ct] -= vlen
self.tree_core.component_protected_size_[ct] += vlen
cd.lock_ref += 1
@@ -498,7 +498,7 @@ class MambaComponent(TreeComponent):
assert cd.lock_ref > 0, f"Mamba release hit lock_ref=0 on node {node.id}"
if cd.lock_ref == 1 and value is not None:
vlen = len(value)
vlen = self.reclaimable_value_len(node)
self.tree_core.component_evictable_size_[ct] += vlen
self.tree_core.component_protected_size_[ct] -= vlen
cd.lock_ref -= 1
@@ -701,7 +701,7 @@ class SWAComponent(TreeComponent):
device_frees[self.component_type].append(
node.component_data[BASE_COMPONENT_TYPE].value
)
freed = len(cd.value)
freed = self.reclaimable_value_len(node)
self.tree_core.component_evictable_size_[ct] -= freed
cd.value = None
@@ -866,7 +866,7 @@ class SWAComponent(TreeComponent):
if lru.in_list(cur):
lru.remove_node(cur)
else:
key_len = len(value)
key_len = self.reclaimable_value_len(cur)
self.tree_core.component_evictable_size_[ct] -= key_len
self.tree_core.component_protected_size_[ct] += key_len
if lock_host:
@@ -915,7 +915,7 @@ class SWAComponent(TreeComponent):
if not host_lru.in_list(cur):
host_lru.insert_mru(cur)
else:
key_len = len(value)
key_len = self.reclaimable_value_len(cur)
self.tree_core.component_evictable_size_[ct] += key_len
self.tree_core.component_protected_size_[ct] -= key_len
if lock_host:
@@ -960,7 +960,7 @@ class SWAComponent(TreeComponent):
if cd.lock_ref == 0:
self.tree_core._update_evictable_leaf_sets(cur)
if cd.lock_ref == 0 and cd.value is not None:
value_len = len(cd.value)
value_len = self.reclaimable_value_len(cur)
self.tree_core.component_protected_size_[ct] -= value_len
self.tree_core.component_evictable_size_[ct] += value_len
if self.tree_core._is_device_leaf(cur):
@@ -1529,7 +1529,7 @@ class UnifiedTreeCore(UnifiedTreeCoreInterface):
parent.children[key.child_key(self.page_size)] = new_node
self.component_evictable_size_[BASE_COMPONENT_TYPE] += self.components_by_type[
BASE_COMPONENT_TYPE
].reclaimable_tokens(new_node)
].reclaimable_value_len(new_node)
if self.enable_storage or self.enable_external_cache_linker:
new_node.hash_value = compute_node_hash_values(new_node, self.page_size)
@@ -1550,7 +1550,7 @@ class UnifiedTreeCore(UnifiedTreeCoreInterface):
cd = node.component_data[ct]
assert cd.value is None
cd.value = fresh_value.clone()
n = self.components_by_type[ct].reclaimable_tokens(node)
n = self.components_by_type[ct].reclaimable_value_len(node)
if cd.lock_ref > 0:
self.component_protected_size_[ct] += n
else:
@@ -1675,6 +1675,8 @@ class UnifiedTreeCore(UnifiedTreeCoreInterface):
or result.mamba_backup_node_id is not None
or result.swa_backup_node_id is not None
or bool(result.tracker)
or any(result.device_frees.values())
or any(result.host_frees.values())
)
return result
@@ -2887,7 +2889,7 @@ class UnifiedTreeCore(UnifiedTreeCoreInterface):
self.lru_lists[component_type].insert_mru(node)
# A value materialized under lock is protected; the last release
# moves it to evictable.
ledger = self.components_by_type[component_type].reclaimable_tokens(node)
ledger = self.components_by_type[component_type].reclaimable_value_len(node)
if cd.lock_ref > 0:
self.component_protected_size_[component_type] += ledger
else:
@@ -2902,10 +2904,15 @@ class UnifiedTreeCore(UnifiedTreeCoreInterface):
cd = node.component_data[component_type]
if node is self.root_node or cd.value is None:
return
if cd.lock_ref > 0:
self.component_protected_size_[component_type] += delta
else:
self.component_evictable_size_[component_type] += delta
sizes = (
self.component_protected_size_
if cd.lock_ref > 0
else self.component_evictable_size_
)
updated = sizes[component_type] + delta
if updated < 0:
raise ValueError(f"component size cannot be negative: {updated}")
sizes[component_type] = updated
def get_component_device_value(
self, node_id: NodeId, component_type: ComponentType
@@ -3126,7 +3133,7 @@ class UnifiedTreeCore(UnifiedTreeCoreInterface):
continue
cd = n.component_data[ct]
if cd.value is not None:
toks = self.components_by_type[ct].reclaimable_tokens(n)
toks = self.components_by_type[ct].reclaimable_value_len(n)
if cd.lock_ref > 0:
protected += toks
else:
@@ -431,6 +431,8 @@ class UnifiedTreeCoreInterface(ABC):
Roots and nodes without device values are no-ops. Missing handles raise
KeyError; disabled components raise ValueError.
Negative totals raise ValueError without updating the count. Rust also
rejects fixed-width overflow; Python integers are unbounded.
"""
...
@@ -89,7 +89,7 @@ class PagedFullComponent(FullComponent):
super().__init__(*args, **kwargs)
self.rows_handed_back: dict[int, int] = {}
def reclaimable_tokens(self, node):
def reclaimable_value_len(self, node):
return self.value_len(node) // 2 - self.rows_handed_back.get(node.id, 0)