dynamo-kv-router 1.4.0

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
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Shared identities for routing partitions, logical KV indexers, and DC-local producer pools.
//!
//! Hashed indexer identity material is resolved on control paths. Mutation and query paths carry
//! only those fixed-size values or physical lane indices.

use std::collections::BTreeMap;
use std::fmt;

use serde::de::{Error as _, MapAccess, Visitor};
use serde::ser::SerializeMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

pub const MAX_EXPLICIT_IDENTITY_ENTRIES: usize = 32;
pub const MAX_EXPLICIT_IDENTITY_KEY_BYTES: usize = 128;
pub const MAX_EXPLICIT_IDENTITY_VALUE_BYTES: usize = 1024;

/// Routing group used when a request does not specify one.
pub const DEFAULT_ROUTING_GROUP: &str = "default";

const CACHE_SEMANTICS_DEFAULT_V1: &[u8] = b"dynamo/indexer-cache-semantics/default/v1";
const CACHE_SEMANTICS_EXPLICIT_V1: &[u8] = b"dynamo/indexer-cache-semantics/explicit/v1";
const ROUTING_SCOPE_DEFAULT_V1: &[u8] = b"dynamo/indexer-routing-scope/default/v1";
const ROUTING_SCOPE_EXPLICIT_V1: &[u8] = b"dynamo/indexer-routing-scope/explicit/v1";

#[cfg(any(
    feature = "standalone-indexer",
    feature = "standalone-selection",
    feature = "standalone-slot-tracker"
))]
pub(crate) fn default_routing_group() -> String {
    DEFAULT_ROUTING_GROUP.to_string()
}

/// Logical routing partition shared by selection, indexing, and active-sequence tracking.
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct RoutingPartitionId {
    pub model_name: String,
    pub routing_group: String,
}

impl RoutingPartitionId {
    pub fn new(model_name: impl Into<String>, routing_group: impl Into<String>) -> Self {
        Self {
            model_name: model_name.into(),
            routing_group: routing_group.into(),
        }
    }

    pub fn as_ref(&self) -> RoutingPartitionRef<'_> {
        RoutingPartitionRef::new(&self.model_name, &self.routing_group)
    }
}

impl fmt::Display for RoutingPartitionId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_ref().fmt(formatter)
    }
}

/// Borrowed view of a [`RoutingPartitionId`].
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct RoutingPartitionRef<'a> {
    pub model_name: &'a str,
    pub routing_group: &'a str,
}

impl<'a> RoutingPartitionRef<'a> {
    pub const fn new(model_name: &'a str, routing_group: &'a str) -> Self {
        Self {
            model_name,
            routing_group,
        }
    }

    pub fn into_owned(self) -> RoutingPartitionId {
        RoutingPartitionId::new(self.model_name, self.routing_group)
    }
}

impl fmt::Display for RoutingPartitionRef<'_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "model={} routing_group={}",
            self.model_name, self.routing_group
        )
    }
}

impl<'a> From<&'a RoutingPartitionId> for RoutingPartitionRef<'a> {
    fn from(partition: &'a RoutingPartitionId) -> Self {
        partition.as_ref()
    }
}

impl From<RoutingPartitionRef<'_>> for RoutingPartitionId {
    fn from(partition: RoutingPartitionRef<'_>) -> Self {
        partition.into_owned()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum IdentitySource {
    DefaultDerived,
    Explicit,
}

macro_rules! digest_identity {
    ($name:ident) => {
        #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
        pub struct $name {
            digest: [u8; 16],
            source: IdentitySource,
        }

        impl $name {
            pub const fn new(digest: [u8; 16], source: IdentitySource) -> Self {
                Self { digest, source }
            }

            pub const fn digest(self) -> [u8; 16] {
                self.digest
            }

            pub const fn source(self) -> IdentitySource {
                self.source
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                write_digest(formatter, &self.digest)
            }
        }

        impl fmt::Debug for $name {
            fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter
                    .debug_struct(stringify!($name))
                    .field("digest", &format_args!("{}", self))
                    .field("source", &self.source)
                    .finish()
            }
        }
    };
}

digest_identity!(CacheSemanticsId);
digest_identity!(RoutingScopeId);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct IndexerDomainId {
    cache_semantics: CacheSemanticsId,
    routing_scope: RoutingScopeId,
}

impl IndexerDomainId {
    pub const fn new(cache_semantics: CacheSemanticsId, routing_scope: RoutingScopeId) -> Self {
        Self {
            cache_semantics,
            routing_scope,
        }
    }

    pub const fn cache_semantics(self) -> CacheSemanticsId {
        self.cache_semantics
    }

    pub const fn routing_scope(self) -> RoutingScopeId {
        self.routing_scope
    }

    pub const fn relies_on_defaults(self) -> bool {
        matches!(
            self.cache_semantics.source(),
            IdentitySource::DefaultDerived
        ) || matches!(self.routing_scope.source(), IdentitySource::DefaultDerived)
    }
}

impl fmt::Display for IndexerDomainId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}:{}", self.cache_semantics, self.routing_scope)
    }
}

/// Control-plane-stable identity for one logical DC inside a routing federation.
///
/// NOTE: This value survives process restarts, scaling, endpoint replacement, and producer
/// generations. It is meaningful only as the DC dimension of [`PoolId`], not as a globally
/// unique identifier. Do not derive it from endpoint identity or fold it into routing scope.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct DcId(u64);

impl DcId {
    pub const fn new(value: u64) -> Self {
        Self(value)
    }

    pub const fn get(self) -> u64 {
        self.0
    }
}

impl fmt::Display for DcId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{:016x}", self.0)
    }
}

/// One DC-local producer/publication stream within an indexer domain.
///
/// NOTE: A global indexer has one domain and distinct pool lanes whose `dc_id` values differ.
/// Runtime endpoint resolution happens only after a query selects a pool lane.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct PoolId {
    indexer_domain: IndexerDomainId,
    dc_id: DcId,
}

impl PoolId {
    pub const fn new(indexer_domain: IndexerDomainId, dc_id: DcId) -> Self {
        Self {
            indexer_domain,
            dc_id,
        }
    }

    pub const fn indexer_domain(self) -> IndexerDomainId {
        self.indexer_domain
    }

    pub const fn dc_id(self) -> DcId {
        self.dc_id
    }
}

impl fmt::Display for PoolId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}/{}", self.indexer_domain, self.dc_id)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct IndexerIdentitySpec {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    semantics: Option<ExplicitIdentityMap>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    routing_scope: Option<ExplicitIdentityMap>,
}

impl IndexerIdentitySpec {
    pub fn new(
        semantics: Option<ExplicitIdentityMap>,
        routing_scope: Option<ExplicitIdentityMap>,
    ) -> Self {
        Self {
            semantics,
            routing_scope,
        }
    }

    pub fn semantics(&self) -> Option<&ExplicitIdentityMap> {
        self.semantics.as_ref()
    }

    pub fn routing_scope(&self) -> Option<&ExplicitIdentityMap> {
        self.routing_scope.as_ref()
    }

    pub const fn is_empty(&self) -> bool {
        self.semantics.is_none() && self.routing_scope.is_none()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExplicitIdentityMap {
    entries: BTreeMap<String, String>,
}

impl ExplicitIdentityMap {
    pub fn new(entries: BTreeMap<String, String>) -> Result<Self, IdentitySpecError> {
        validate_entries(&entries)?;
        Ok(Self { entries })
    }

    pub fn entries(&self) -> &BTreeMap<String, String> {
        &self.entries
    }
}

#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum IdentitySpecError {
    #[error("explicit identity map must contain at least one entry")]
    Empty,
    #[error("explicit identity map contains more than {MAX_EXPLICIT_IDENTITY_ENTRIES} entries")]
    TooManyEntries,
    #[error("explicit identity key must not be empty")]
    EmptyKey,
    #[error("explicit identity value for `{key}` must not be empty")]
    EmptyValue { key: String },
    #[error("explicit identity key exceeds {MAX_EXPLICIT_IDENTITY_KEY_BYTES} UTF-8 bytes: `{key}`")]
    KeyTooLong { key: String },
    #[error(
        "explicit identity value for `{key}` exceeds {MAX_EXPLICIT_IDENTITY_VALUE_BYTES} UTF-8 bytes"
    )]
    ValueTooLong { key: String },
}

impl Serialize for ExplicitIdentityMap {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut map = serializer.serialize_map(Some(self.entries.len()))?;
        for (key, value) in &self.entries {
            map.serialize_entry(key, value)?;
        }
        map.end()
    }
}

impl<'de> Deserialize<'de> for ExplicitIdentityMap {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct ExplicitIdentityMapVisitor;

        impl<'de> Visitor<'de> for ExplicitIdentityMapVisitor {
            type Value = ExplicitIdentityMap;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("a non-empty map of unique identity keys to string values")
            }

            fn visit_map<A>(self, mut access: A) -> Result<Self::Value, A::Error>
            where
                A: MapAccess<'de>,
            {
                let mut entries = BTreeMap::new();
                while let Some((key, value)) = access.next_entry::<String, String>()? {
                    if entries.insert(key.clone(), value).is_some() {
                        return Err(A::Error::custom(format!(
                            "duplicate explicit identity key `{key}`"
                        )));
                    }
                }
                ExplicitIdentityMap::new(entries).map_err(A::Error::custom)
            }
        }

        deserializer.deserialize_map(ExplicitIdentityMapVisitor)
    }
}

/// Canonical bytes hashed by a runtime or service layer that owns BLAKE3.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CanonicalIdentityMaterial {
    source: IdentitySource,
    bytes: Vec<u8>,
}

impl CanonicalIdentityMaterial {
    pub fn cache_semantics(
        defaults: &[&str],
        explicit: Option<&ExplicitIdentityMap>,
        kv_block_size: u32,
        event_hash_format: u16,
    ) -> Self {
        let (source, tag) = match explicit {
            Some(_) => (IdentitySource::Explicit, CACHE_SEMANTICS_EXPLICIT_V1),
            None => (IdentitySource::DefaultDerived, CACHE_SEMANTICS_DEFAULT_V1),
        };
        let mut bytes = Vec::new();
        append_framed(&mut bytes, tag);
        append_selected_material(&mut bytes, defaults, explicit);
        bytes.extend_from_slice(&kv_block_size.to_le_bytes());
        bytes.extend_from_slice(&event_hash_format.to_le_bytes());
        Self { source, bytes }
    }

    pub fn routing_scope(defaults: &[&str], explicit: Option<&ExplicitIdentityMap>) -> Self {
        let (source, tag) = match explicit {
            Some(_) => (IdentitySource::Explicit, ROUTING_SCOPE_EXPLICIT_V1),
            None => (IdentitySource::DefaultDerived, ROUTING_SCOPE_DEFAULT_V1),
        };
        let mut bytes = Vec::new();
        append_framed(&mut bytes, tag);
        append_selected_material(&mut bytes, defaults, explicit);
        Self { source, bytes }
    }

    pub const fn source(&self) -> IdentitySource {
        self.source
    }

    pub fn bytes(&self) -> &[u8] {
        &self.bytes
    }
}

fn append_selected_material(
    bytes: &mut Vec<u8>,
    defaults: &[&str],
    explicit: Option<&ExplicitIdentityMap>,
) {
    match explicit {
        Some(explicit) => {
            append_count(bytes, explicit.entries.len());
            for (key, value) in &explicit.entries {
                append_framed(bytes, key.as_bytes());
                append_framed(bytes, value.as_bytes());
            }
        }
        None => {
            append_count(bytes, defaults.len());
            for value in defaults {
                append_framed(bytes, value.as_bytes());
            }
        }
    }
}

fn append_count(bytes: &mut Vec<u8>, count: usize) {
    let count = u32::try_from(count).expect("identity input count is validated to fit u32");
    bytes.extend_from_slice(&count.to_le_bytes());
}

fn append_framed(bytes: &mut Vec<u8>, value: &[u8]) {
    let len = u32::try_from(value.len()).expect("identity input length is validated to fit u32");
    bytes.extend_from_slice(&len.to_le_bytes());
    bytes.extend_from_slice(value);
}

fn validate_entries(entries: &BTreeMap<String, String>) -> Result<(), IdentitySpecError> {
    if entries.is_empty() {
        return Err(IdentitySpecError::Empty);
    }
    if entries.len() > MAX_EXPLICIT_IDENTITY_ENTRIES {
        return Err(IdentitySpecError::TooManyEntries);
    }
    for (key, value) in entries {
        if key.is_empty() {
            return Err(IdentitySpecError::EmptyKey);
        }
        if key.len() > MAX_EXPLICIT_IDENTITY_KEY_BYTES {
            return Err(IdentitySpecError::KeyTooLong { key: key.clone() });
        }
        if value.is_empty() {
            return Err(IdentitySpecError::EmptyValue { key: key.clone() });
        }
        if value.len() > MAX_EXPLICIT_IDENTITY_VALUE_BYTES {
            return Err(IdentitySpecError::ValueTooLong { key: key.clone() });
        }
    }
    Ok(())
}

fn write_digest(formatter: &mut fmt::Formatter<'_>, digest: &[u8; 16]) -> fmt::Result {
    for byte in digest {
        write!(formatter, "{byte:02x}")?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use super::*;

    #[test]
    fn routing_partition_identity_matches_registry_key_semantics() {
        let partition = RoutingPartitionId::new("model-a", "group-a");
        let equivalent = RoutingPartitionId::new("model-a", "group-a");
        let other_group = RoutingPartitionId::new("model-a", "group-b");
        let other_model = RoutingPartitionId::new("model-b", "group-a");
        let mut registry_keys = HashSet::new();

        assert!(registry_keys.insert(partition.clone()));
        assert!(!registry_keys.insert(equivalent));
        assert!(registry_keys.insert(other_group));
        assert!(registry_keys.insert(other_model));
        assert_eq!(registry_keys.len(), 3);

        let borrowed = partition.as_ref();
        assert_eq!(borrowed, RoutingPartitionRef::new("model-a", "group-a"));
        assert_eq!(borrowed.into_owned(), partition);
    }

    #[test]
    fn explicit_identity_map_rejects_duplicate_json_keys() {
        let error = serde_json::from_str::<ExplicitIdentityMap>(r#"{"weights":"a","weights":"b"}"#)
            .unwrap_err();
        assert!(
            error
                .to_string()
                .contains("duplicate explicit identity key")
        );
    }

    #[test]
    fn explicit_identity_map_rejects_empty_and_oversized_inputs() {
        assert_eq!(
            ExplicitIdentityMap::new(BTreeMap::new()),
            Err(IdentitySpecError::Empty)
        );
        assert_eq!(
            ExplicitIdentityMap::new(BTreeMap::from([(String::new(), "value".to_string(),)])),
            Err(IdentitySpecError::EmptyKey)
        );
        assert!(matches!(
            ExplicitIdentityMap::new(BTreeMap::from([(
                "key".to_string(),
                "x".repeat(MAX_EXPLICIT_IDENTITY_VALUE_BYTES + 1),
            )])),
            Err(IdentitySpecError::ValueTooLong { .. })
        ));
    }

    #[test]
    fn explicit_material_replaces_defaults_and_is_order_independent() {
        let first = ExplicitIdentityMap::new(BTreeMap::from([
            ("weights".to_string(), "revision-a".to_string()),
            ("mapping".to_string(), "tp2".to_string()),
        ]))
        .unwrap();
        let second: ExplicitIdentityMap =
            serde_json::from_str(r#"{"mapping":"tp2","weights":"revision-a"}"#).unwrap();
        let a = CanonicalIdentityMaterial::cache_semantics(&["default-a"], Some(&first), 512, 1);
        let b = CanonicalIdentityMaterial::cache_semantics(&["default-b"], Some(&second), 512, 1);
        assert_eq!(a, b);
        assert_eq!(a.source(), IdentitySource::Explicit);
    }

    #[test]
    fn default_and_explicit_material_are_distinct() {
        let explicit = ExplicitIdentityMap::new(BTreeMap::from([(
            "model".to_string(),
            "same-text".to_string(),
        )]))
        .unwrap();
        let default = CanonicalIdentityMaterial::cache_semantics(&["same-text"], None, 512, 1);
        let explicit =
            CanonicalIdentityMaterial::cache_semantics(&["ignored"], Some(&explicit), 512, 1);
        assert_ne!(default.bytes(), explicit.bytes());
        assert_eq!(default.source(), IdentitySource::DefaultDerived);
        assert_eq!(explicit.source(), IdentitySource::Explicit);
    }

    #[test]
    fn length_framing_distinguishes_adjacent_inputs() {
        let first = CanonicalIdentityMaterial::routing_scope(&["ab", "c"], None);
        let second = CanonicalIdentityMaterial::routing_scope(&["a", "bc"], None);
        assert_ne!(first.bytes(), second.bytes());
    }

    #[test]
    fn identifiers_render_fixed_width_hex() {
        let semantics = CacheSemanticsId::new([0xab; 16], IdentitySource::Explicit);
        let routing = RoutingScopeId::new([0x01; 16], IdentitySource::DefaultDerived);
        assert_eq!(semantics.to_string(), "abababababababababababababababab");
        assert_eq!(routing.to_string(), "01010101010101010101010101010101");
        assert_eq!(DcId::new(1).to_string(), "0000000000000001");
    }
}