dynamo-kv-router 1.3.0-dev.1

KV Router - Radix tree for LLM KV cache routing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use super::*;

struct SplitLookupFinish<'a> {
    split: SplitLookupData,
    prefix_node: &'a SharedNode,
    prefix_blocks: &'a [KvCacheStoredBlockData],
    tail_blocks: &'a [KvCacheStoredBlockData],
    tail_node: &'a SharedNode,
}

struct StoreParentCursor<'a> {
    parent: &'a SharedNode,
    parent_is_anchor: bool,
    last_ext_hash: Option<ExternalSequenceBlockHash>,
}

impl ConcurrentRadixTreeCompressed {
    // ------------------------------------------------------------------
    // apply_stored
    // ------------------------------------------------------------------

    #[cfg_attr(feature = "profile", inline(never))]
    pub(super) fn apply_stored(
        &self,
        lookup: &mut FxHashMap<WorkerWithDpRank, WorkerLookup>,
        worker: WorkerWithDpRank,
        op: KvCacheStoreData,
        id: u64,
        counters: Option<&PreBoundEventCounters>,
    ) -> Result<(), KvCacheEventError> {
        lookup.entry(worker).or_default();

        let parent_resolution = self.resolve_store_parent(lookup, worker, &op, id)?;
        let outcome = self.insert_for_resolved_parent(lookup, worker, parent_resolution, &op)?;
        self.record_store_outcome(&outcome, counters);

        Ok(())
    }

    fn resolve_store_parent(
        &self,
        lookup: &mut FxHashMap<WorkerWithDpRank, WorkerLookup>,
        worker: WorkerWithDpRank,
        op: &KvCacheStoreData,
        id: u64,
    ) -> Result<StoreParentResolution, KvCacheEventError> {
        let Some(parent_hash) = op.parent_hash else {
            return Ok(StoreParentResolution::InsertFrom {
                parent: self.root.clone(),
                parent_is_anchor: false,
            });
        };

        self.resolve_explicit_store_parent(lookup, worker, parent_hash, op, id)
    }

    fn resolve_explicit_store_parent(
        &self,
        lookup: &mut FxHashMap<WorkerWithDpRank, WorkerLookup>,
        worker: WorkerWithDpRank,
        parent_hash: ExternalSequenceBlockHash,
        op: &KvCacheStoreData,
        id: u64,
    ) -> Result<StoreParentResolution, KvCacheEventError> {
        loop {
            let node = self.lookup_store_parent_node(lookup, worker, parent_hash, op, id)?;
            self.reject_uncovered_store_parent(lookup, worker, &node, parent_hash, id)?;

            let Some(plan) = node.plan_store_parent_edge(parent_hash, &op.blocks) else {
                continue;
            };
            let edge_action = node.apply_store_parent_edge_plan(worker, plan, &op.blocks);

            match edge_action {
                ParentEdgeAction::Stale => continue,
                ParentEdgeAction::ReuseExistingEdge { coverage_changed } => {
                    return Ok(StoreParentResolution::ReusedExistingEdge {
                        node,
                        coverage_changed,
                    });
                }
                ParentEdgeAction::InsertFromParent(split_data) => {
                    if let Some(split) = split_data {
                        self.apply_split_lookup(lookup, split);
                    }
                    return Ok(StoreParentResolution::InsertFrom {
                        parent_is_anchor: self.is_anchor_node(parent_hash, &node),
                        parent: node,
                    });
                }
            }
        }
    }

    fn lookup_store_parent_node(
        &self,
        lookup: &mut FxHashMap<WorkerWithDpRank, WorkerLookup>,
        worker: WorkerWithDpRank,
        parent_hash: ExternalSequenceBlockHash,
        op: &KvCacheStoreData,
        id: u64,
    ) -> Result<SharedNode, KvCacheEventError> {
        if let Some(node) = self.resolve_lookup(
            lookup,
            worker,
            parent_hash,
            LookupRepairDirection::TowardTail,
        ) {
            return Ok(node);
        }

        if let Some(node) = self.resolve_anchor_lookup(lookup, worker, parent_hash) {
            return Ok(node);
        }

        tracing::warn!(
            worker_id = worker.worker_id.to_string(),
            dp_rank = worker.dp_rank,
            id,
            parent_hash = ?op.parent_hash,
            num_blocks = op.blocks.len(),
            "Failed to find parent block; skipping store operation"
        );
        Err(KvCacheEventError::ParentBlockNotFound)
    }

    fn reject_uncovered_store_parent(
        &self,
        lookup: &mut FxHashMap<WorkerWithDpRank, WorkerLookup>,
        worker: WorkerWithDpRank,
        node: &SharedNode,
        parent_hash: ExternalSequenceBlockHash,
        id: u64,
    ) -> Result<(), KvCacheEventError> {
        let Some(uncovered) = node.reject_uncovered_parent(worker, parent_hash) else {
            return Ok(());
        };
        tracing::warn!(
            worker_id = worker.worker_id.to_string(),
            dp_rank = worker.dp_rank,
            id,
            parent_hash = ?parent_hash,
            pos = uncovered.pos,
            cutoff = uncovered.cutoff,
            "Stale parent: worker no longer covers parent_hash; rejecting store"
        );

        let wl = lookup.get_mut(&worker).unwrap();
        wl.remove(&parent_hash);
        Err(KvCacheEventError::ParentBlockNotFound)
    }

    #[cfg_attr(feature = "profile", inline(never))]
    fn insert_for_resolved_parent(
        &self,
        lookup: &mut FxHashMap<WorkerWithDpRank, WorkerLookup>,
        worker: WorkerWithDpRank,
        parent_resolution: StoreParentResolution,
        op: &KvCacheStoreData,
    ) -> Result<StoreInsertOutcome, KvCacheEventError> {
        let outcome = match parent_resolution {
            StoreParentResolution::InsertFrom {
                parent,
                parent_is_anchor,
            } => {
                return self.insert_blocks_from(
                    lookup,
                    worker,
                    &parent,
                    parent_is_anchor,
                    op.parent_hash,
                    &op.blocks,
                );
            }
            StoreParentResolution::ReusedExistingEdge {
                node,
                coverage_changed,
            } => Self::finish_with_lookup_update(
                lookup,
                worker,
                &op.blocks,
                &node,
                !op.blocks.is_empty() && !coverage_changed,
            ),
        };
        Ok(outcome)
    }

    fn finish_with_lookup_update(
        lookup: &mut FxHashMap<WorkerWithDpRank, WorkerLookup>,
        worker: WorkerWithDpRank,
        blocks: &[KvCacheStoredBlockData],
        node: &SharedNode,
        duplicate_store: bool,
    ) -> StoreInsertOutcome {
        let wl = lookup.get_mut(&worker).unwrap();
        let lookup_changed = Self::update_lookup_for_blocks(wl, blocks, node);

        StoreInsertOutcome {
            duplicate_store: duplicate_store && !lookup_changed,
        }
    }

    fn finish_after_split_lookup(
        &self,
        lookup: &mut FxHashMap<WorkerWithDpRank, WorkerLookup>,
        worker: WorkerWithDpRank,
        finish: SplitLookupFinish<'_>,
    ) -> StoreInsertOutcome {
        self.apply_split_lookup(lookup, finish.split);

        let wl = lookup.get_mut(&worker).unwrap();
        Self::update_lookup_for_blocks(wl, finish.prefix_blocks, finish.prefix_node);
        Self::update_lookup_for_blocks(wl, finish.tail_blocks, finish.tail_node);

        StoreInsertOutcome {
            duplicate_store: false,
        }
    }

    fn record_store_outcome(
        &self,
        outcome: &StoreInsertOutcome,
        counters: Option<&PreBoundEventCounters>,
    ) {
        if outcome.duplicate_store
            && let Some(counters) = counters
        {
            counters.inc_warning(EventWarningKind::DuplicateStore);
        }
    }

    fn child_or_insert_remaining(
        &self,
        lookup: &mut FxHashMap<WorkerWithDpRank, WorkerLookup>,
        worker: WorkerWithDpRank,
        cursor: StoreParentCursor<'_>,
        remaining: &[KvCacheStoredBlockData],
    ) -> Result<StoreInsertStep, KvCacheEventError> {
        debug_assert!(!remaining.is_empty());

        let first_local = remaining[0].tokens_hash;
        let plan = cursor
            .parent
            .child_lookup_plan(cursor.last_ext_hash, first_local);

        let shape_version = match plan {
            ParentChildPlan::Stale => {
                return Ok(StoreInsertStep::RetryParent {
                    parent: cursor.parent.clone(),
                    parent_is_anchor: cursor.parent_is_anchor,
                });
            }
            ParentChildPlan::StaleParent { hash } => {
                let Some(resolved) =
                    self.resolve_lookup(lookup, worker, hash, LookupRepairDirection::TowardTail)
                else {
                    tracing::warn!(
                        worker_id = worker.worker_id.to_string(),
                        dp_rank = worker.dp_rank,
                        parent_hash = ?hash,
                        num_blocks = remaining.len(),
                        "Stale parent hash could not be resolved; rejecting store"
                    );
                    return Err(KvCacheEventError::ParentBlockNotFound);
                };

                return Ok(StoreInsertStep::RetryParent {
                    parent_is_anchor: self.is_anchor_node(hash, &resolved),
                    parent: resolved,
                });
            }
            ParentChildPlan::Descend(child) => return Ok(StoreInsertStep::Descend(child)),
            ParentChildPlan::MissingChild { shape_version } => shape_version,
        };

        if let Some(parent_hash) = cursor.last_ext_hash
            && !cursor.parent_is_anchor
        {
            // Parent hashes can point inside a compressed edge. Before attaching a
            // child, try to reuse that existing suffix or split at the parent.
            if let Some(edge_plan) = cursor.parent.plan_store_parent_edge(parent_hash, remaining) {
                let edge_action = cursor
                    .parent
                    .apply_store_parent_edge_plan(worker, edge_plan, remaining);
                match edge_action {
                    ParentEdgeAction::Stale => {
                        return Ok(StoreInsertStep::RetryParent {
                            parent: cursor.parent.clone(),
                            parent_is_anchor: cursor.parent_is_anchor,
                        });
                    }
                    ParentEdgeAction::ReuseExistingEdge { coverage_changed } => {
                        return Ok(StoreInsertStep::Done(Self::finish_with_lookup_update(
                            lookup,
                            worker,
                            remaining,
                            cursor.parent,
                            !remaining.is_empty() && !coverage_changed,
                        )));
                    }
                    ParentEdgeAction::InsertFromParent(Some(split)) => {
                        self.apply_split_lookup(lookup, split);
                        return Ok(StoreInsertStep::RetryParent {
                            parent: cursor.parent.clone(),
                            parent_is_anchor: cursor.parent_is_anchor,
                        });
                    }
                    ParentEdgeAction::InsertFromParent(None) => {}
                }
            }
        }

        if let Some(parent_hash) = cursor.last_ext_hash
            && !cursor.parent_is_anchor
        {
            // Keep the decode-extension happy path as a direct commit attempt:
            // the node method revalidates the shape and appends only if the
            // parent is still the covered tail leaf.
            match cursor.parent.try_extend_leaf_with_version(
                worker,
                parent_hash,
                remaining,
                shape_version,
            ) {
                None => {
                    return Ok(StoreInsertStep::RetryParent {
                        parent: cursor.parent.clone(),
                        parent_is_anchor: cursor.parent_is_anchor,
                    });
                }
                Some(true) => {
                    return Ok(StoreInsertStep::Done(Self::finish_with_lookup_update(
                        lookup,
                        worker,
                        remaining,
                        cursor.parent,
                        false,
                    )));
                }
                Some(false) => {}
            }
        }

        let new_node = Arc::new(Node::from_blocks_for_worker(remaining, worker));
        if let Some(parent_hash) = cursor.last_ext_hash
            && !cursor.parent_is_anchor
        {
            // Allocation can race with another writer leaving the parent
            // extendable. Recheck extension once before publishing a child.
            match cursor.parent.try_extend_leaf_with_version(
                worker,
                parent_hash,
                remaining,
                shape_version,
            ) {
                None => {
                    return Ok(StoreInsertStep::RetryParent {
                        parent: cursor.parent.clone(),
                        parent_is_anchor: cursor.parent_is_anchor,
                    });
                }
                Some(true) => {
                    return Ok(StoreInsertStep::Done(Self::finish_with_lookup_update(
                        lookup,
                        worker,
                        remaining,
                        cursor.parent,
                        false,
                    )));
                }
                Some(false) => {}
            }
        }

        match cursor
            .parent
            .insert_child_if_still_missing(first_local, new_node, shape_version)
        {
            InsertChildOutcome::Stale => Ok(StoreInsertStep::RetryParent {
                parent: cursor.parent.clone(),
                parent_is_anchor: cursor.parent_is_anchor,
            }),
            InsertChildOutcome::Existing(child) => Ok(StoreInsertStep::Descend(child)),
            InsertChildOutcome::Inserted(new_node) => Ok(StoreInsertStep::Done(
                Self::finish_with_lookup_update(lookup, worker, remaining, &new_node, false),
            )),
        }
    }

    fn insert_into_child(
        &self,
        lookup: &mut FxHashMap<WorkerWithDpRank, WorkerLookup>,
        worker: WorkerWithDpRank,
        child: &SharedNode,
        remaining: &[KvCacheStoredBlockData],
        duplicate_store: bool,
    ) -> ChildInsertStep {
        loop {
            let scan = child.scan_store_prefix(remaining);
            let mut duplicate_store = duplicate_store;

            debug_assert!(
                scan.match_len >= 1,
                "first hash must match since child was found by it"
            );

            if scan.match_len < scan.edge_len {
                // A partial match either means this worker only covers a prefix
                // of the compressed edge, or the store diverges and must split
                // the edge.
                if scan.match_len == remaining.len() {
                    let Some(coverage_changed) = child.cover_prefix_for_worker_with_version(
                        worker,
                        scan.match_len,
                        scan.shape_version,
                    ) else {
                        continue;
                    };
                    if let Some((expected, actual)) = scan.block_hash_mismatch {
                        duplicate_store = false;
                        tracing::warn!(
                            ?expected,
                            ?actual,
                            "block_hash mismatch: sequence hashes should be uniform across workers"
                        );
                    }

                    return ChildInsertStep::Done(Self::finish_with_lookup_update(
                        lookup,
                        worker,
                        remaining,
                        child,
                        duplicate_store && !remaining.is_empty() && !coverage_changed,
                    ));
                }

                let tail = &remaining[scan.match_len..];
                debug_assert!(!tail.is_empty());

                let tail_candidate = Arc::new(Node::from_blocks_for_worker(tail, worker));
                let SplitStoreOutcome::Done { split, tail_node } = child.split_for_store_tail(
                    worker,
                    scan.match_len,
                    tail[0].tokens_hash,
                    tail_candidate,
                    scan.shape_version,
                ) else {
                    continue;
                };
                if let Some((expected, actual)) = scan.block_hash_mismatch {
                    tracing::warn!(
                        ?expected,
                        ?actual,
                        "block_hash mismatch: sequence hashes should be uniform across workers"
                    );
                }

                return ChildInsertStep::Done(self.finish_after_split_lookup(
                    lookup,
                    worker,
                    SplitLookupFinish {
                        split,
                        prefix_node: child,
                        prefix_blocks: &remaining[..scan.match_len],
                        tail_blocks: tail,
                        tail_node: &tail_node,
                    },
                ));
            }

            // The whole child edge matched. Mark this worker as covering it,
            // update lookup for that edge, then continue with the unmatched
            // suffix.
            let Some(promoted) = child.promote_to_full_with_version(worker, scan.shape_version)
            else {
                continue;
            };
            if let Some((expected, actual)) = scan.block_hash_mismatch {
                duplicate_store = false;
                tracing::warn!(
                    ?expected,
                    ?actual,
                    "block_hash mismatch: sequence hashes should be uniform across workers"
                );
            }
            if promoted {
                duplicate_store = false;
            }

            let wl = lookup.get_mut(&worker).unwrap();
            let lookup_changed =
                Self::update_lookup_for_blocks(wl, &remaining[..scan.edge_len], child);
            if lookup_changed {
                duplicate_store = false;
            }

            return ChildInsertStep::Descend {
                edge_len: scan.edge_len,
                last_ext_hash: remaining[scan.edge_len - 1].block_hash,
                duplicate_store,
            };
        }
    }

    #[cfg_attr(feature = "profile", inline(never))]
    fn insert_blocks_from(
        &self,
        lookup: &mut FxHashMap<WorkerWithDpRank, WorkerLookup>,
        worker: WorkerWithDpRank,
        parent: &SharedNode,
        parent_is_anchor: bool,
        seed_hash: Option<ExternalSequenceBlockHash>,
        blocks: &[KvCacheStoredBlockData],
    ) -> Result<StoreInsertOutcome, KvCacheEventError> {
        let mut current_parent = parent.clone();
        let mut current_parent_is_anchor = parent_is_anchor;
        let mut remaining = blocks;
        let mut duplicate_store = !blocks.is_empty();
        // Track the last ExternalSequenceBlockHash we matched to detect if
        // `current_parent` was split by a concurrent thread between iterations.
        // A split shortens `current_parent`'s edge and moves our last-matched
        // hash into a new suffix child. The child lookup plan detects this under
        // a read lock, then mutation paths revalidate the node shape version.
        //
        // Seeded with parent_hash so the very first iteration detects a split
        // that occurred after apply_stored released its write lock but before
        // we acquired ours here.
        let mut last_ext_hash: Option<ExternalSequenceBlockHash> = seed_hash;

        while !remaining.is_empty() {
            let child = match self.child_or_insert_remaining(
                lookup,
                worker,
                StoreParentCursor {
                    parent: &current_parent,
                    parent_is_anchor: current_parent_is_anchor,
                    last_ext_hash,
                },
                remaining,
            )? {
                StoreInsertStep::RetryParent {
                    parent,
                    parent_is_anchor,
                } => {
                    current_parent = parent;
                    current_parent_is_anchor = parent_is_anchor;
                    continue;
                }
                StoreInsertStep::Descend(child) => child,
                StoreInsertStep::Done(outcome) => return Ok(outcome),
            };

            match self.insert_into_child(lookup, worker, &child, remaining, duplicate_store) {
                ChildInsertStep::Done(outcome) => return Ok(outcome),
                ChildInsertStep::Descend {
                    edge_len,
                    last_ext_hash: matched_hash,
                    duplicate_store: duplicate,
                } => {
                    duplicate_store = duplicate;
                    last_ext_hash = Some(matched_hash);
                    remaining = &remaining[edge_len..];
                    current_parent = child;
                    current_parent_is_anchor = false;
                }
            }
        }

        Ok(StoreInsertOutcome { duplicate_store })
    }
}