kv.rs 42.4 KB
Newer Older
1
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
3
// SPDX-License-Identifier: Apache-2.0

4
use pythonize::{depythonize, pythonize};
5
use std::collections::HashMap;
6
use std::ffi::OsString;
7
use std::sync::Arc;
8
use std::sync::atomic::AtomicU32;
9
use std::sync::mpsc;
10
use tokio_stream::StreamExt;
11

12
use super::*;
13
use crate::Endpoint;
14
15
#[cfg(feature = "kv-indexer")]
use clap::Parser;
16
17
18
use dynamo_kv_router::config::{KvRouterConfig, RouterConfigOverride};
use dynamo_kv_router::protocols::compute_block_hash_for_seq;
use dynamo_kv_router::protocols::*;
19
20
21
22
#[cfg(feature = "kv-indexer-runtime")]
use dynamo_kv_router::standalone_indexer::RuntimeConfig;
#[cfg(feature = "kv-indexer")]
use dynamo_kv_router::standalone_indexer::{self, IndexerConfig};
Yan Ru Pei's avatar
Yan Ru Pei committed
23
use rs::pipeline::{AsyncEngine, SingleIn};
24
use rs::protocols::annotated::Annotated as RsAnnotated;
25
use tracing;
26

27
use llm_rs::kv_router::KvPushRouter as RsKvPushRouter;
28
use llm_rs::kv_router::publisher::{KvEventSourceConfig, create_stored_blocks};
29
use llm_rs::protocols::common::timing::RequestTracker;
30
use llm_rs::protocols::common::{OutputOptions, SamplingOptions, StopConditions};
31
use serde_json::json;
32

33
34
35
use super::aic_callback::create_aic_prefill_load_estimator;
use super::entrypoint::AicPerfConfig;

36
37
38
39
fn depythonize_block_mm_infos(obj: &Bound<'_, PyAny>) -> PyResult<Vec<Option<BlockExtraInfo>>> {
    depythonize(obj).map_err(to_pyerr)
}

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
#[cfg(feature = "kv-indexer")]
#[derive(Parser)]
#[command(
    name = "python -m dynamo.indexer",
    about = "Standalone KV cache indexer"
)]
struct KvIndexerCli {
    /// KV cache block size for initial workers registered via --workers
    #[arg(long)]
    block_size: Option<u32>,

    /// HTTP server port
    #[arg(long, default_value_t = 8090)]
    port: u16,

    /// Number of indexer threads (1 = single-threaded KvIndexer, >1 = ThreadPoolIndexer)
    #[arg(long, default_value_t = 4)]
    threads: usize,

    /// Initial workers as "worker_id[:dp_rank]=zmq_address,..." (e.g. "1=tcp://host:5557,1:1=tcp://host:5558")
    #[arg(long)]
    workers: Option<String>,

    /// Model name for initial workers registered via --workers
    #[arg(long, default_value = "default")]
    model_name: String,

    /// Tenant ID for initial workers registered via --workers
    #[arg(long, default_value = "default")]
    tenant_id: String,

    /// Comma-separated peer URLs for P2P recovery (e.g. "http://host1:8090,http://host2:8091")
    #[arg(long)]
    peers: Option<String>,

    /// Enable Dynamo runtime integration (discovery, event plane, request plane).
    #[cfg(feature = "kv-indexer-runtime")]
    #[arg(long)]
    dynamo_runtime: bool,

    /// Dynamo namespace to register the indexer component under.
    #[cfg(feature = "kv-indexer-runtime")]
    #[arg(long, default_value = "default")]
    namespace: String,

    /// Component name for this indexer in the Dynamo runtime.
    #[cfg(feature = "kv-indexer-runtime")]
    #[arg(long, default_value = "kv-indexer")]
    component_name: String,

    /// Component name that workers register under.
    #[cfg(feature = "kv-indexer-runtime")]
    #[arg(long, default_value = "backend")]
    worker_component: String,
}

pub fn run_kv_indexer_cli<I, T>(args: I) -> anyhow::Result<()>
where
    I: IntoIterator<Item = T>,
    T: Into<OsString>,
{
    #[cfg(feature = "kv-indexer")]
    {
        let cli = KvIndexerCli::try_parse_from(
            std::iter::once(OsString::from("python -m dynamo.indexer"))
                .chain(args.into_iter().map(Into::into)),
        )?;

        #[cfg(feature = "kv-indexer-runtime")]
        if cli.dynamo_runtime {
            dynamo_runtime::logging::init();
            let worker = dynamo_runtime::Worker::from_settings()?;
            return worker.execute(move |runtime| {
                standalone_indexer::run_with_runtime(
                    runtime,
                    IndexerConfig {
                        block_size: cli.block_size,
                        port: cli.port,
                        threads: cli.threads,
                        workers: cli.workers,
                        model_name: cli.model_name,
                        tenant_id: cli.tenant_id,
                        peers: cli.peers,
                    },
                    RuntimeConfig {
                        namespace: cli.namespace,
                        component_name: cli.component_name,
                        worker_component: cli.worker_component,
                    },
                )
            });
        }

        init_standalone_logging();

        let rt = tokio::runtime::Runtime::new()?;
        rt.block_on(standalone_indexer::run_server(IndexerConfig {
            block_size: cli.block_size,
            port: cli.port,
            threads: cli.threads,
            workers: cli.workers,
            model_name: cli.model_name,
            tenant_id: cli.tenant_id,
            peers: cli.peers,
        }))
    }

    #[cfg(not(feature = "kv-indexer"))]
    {
        let _ = args;
        anyhow::bail!(
            "dynamo.indexer is not available in this build; reinstall with --features kv-indexer"
        )
    }
}

#[cfg(feature = "kv-indexer")]
fn init_standalone_logging() {
    let _ = tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
        )
        .try_init();
}

Yan Ru Pei's avatar
Yan Ru Pei committed
166
#[pyfunction]
167
#[pyo3(name = "compute_block_hash_for_seq", signature = (tokens, kv_block_size, block_mm_infos=None, lora_name=None, is_eagle=None))]
168
169
170
171
172
pub fn compute_block_hash_for_seq_py(
    _py: Python,
    tokens: Vec<u32>,
    kv_block_size: usize,
    block_mm_infos: Option<Bound<PyAny>>,
173
    lora_name: Option<String>,
174
    is_eagle: Option<bool>,
175
) -> PyResult<Vec<u64>> {
Yan Ru Pei's avatar
Yan Ru Pei committed
176
    if kv_block_size == 0 {
177
178
179
        return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
            "kv_block_size cannot be 0",
        ));
Yan Ru Pei's avatar
Yan Ru Pei committed
180
181
    }

182
    let mm_infos = block_mm_infos
183
        .as_ref()
184
        .map(depythonize_block_mm_infos)
185
186
        .transpose()?;

187
188
189
    let hashes = compute_block_hash_for_seq(
        &tokens,
        kv_block_size as u32,
190
191
192
193
194
        BlockHashOptions {
            block_mm_infos: mm_infos.as_deref(),
            lora_name: lora_name.as_deref(),
            is_eagle,
        },
195
    );
196

Yan Ru Pei's avatar
Yan Ru Pei committed
197
198
199
    Ok(hashes.into_iter().map(|h| h.0).collect())
}

GuanLuo's avatar
GuanLuo committed
200
#[pyclass]
201
202
pub(crate) struct WorkerMetricsPublisher {
    inner: Arc<llm_rs::kv_router::publisher::WorkerMetricsPublisher>,
GuanLuo's avatar
GuanLuo committed
203
204
205
}

#[pymethods]
206
impl WorkerMetricsPublisher {
GuanLuo's avatar
GuanLuo committed
207
208
    #[new]
    fn new() -> PyResult<Self> {
209
210
        let inner =
            llm_rs::kv_router::publisher::WorkerMetricsPublisher::new().map_err(to_pyerr)?;
GuanLuo's avatar
GuanLuo committed
211
212
213
214
215
        Ok(Self {
            inner: inner.into(),
        })
    }

216
    #[pyo3(signature = (endpoint))]
Alec's avatar
Alec committed
217
    fn create_endpoint<'p>(
GuanLuo's avatar
GuanLuo committed
218
219
        &self,
        py: Python<'p>,
220
        endpoint: Endpoint,
GuanLuo's avatar
GuanLuo committed
221
222
    ) -> PyResult<Bound<'p, PyAny>> {
        let rs_publisher = self.inner.clone();
223
        let rs_component = endpoint.inner.component().clone();
GuanLuo's avatar
GuanLuo committed
224
        pyo3_async_runtimes::tokio::future_into_py(py, async move {
225
            rs_publisher
226
                .create_endpoint(rs_component)
GuanLuo's avatar
GuanLuo committed
227
228
229
230
231
232
                .await
                .map_err(to_pyerr)?;
            Ok(())
        })
    }

233
234
235
236
    /// Publish worker metrics for load monitoring.
    ///
    /// # Arguments
    /// * `dp_rank` - Data parallel rank of the worker (None defaults to 0)
237
238
239
240
241
242
243
244
245
    /// * `active_decode_blocks` - Scheduler-compatible active decode block count
    /// * `kv_used_blocks` - Authoritative total KV blocks currently in use
    #[pyo3(signature = (dp_rank=None, active_decode_blocks=None, kv_used_blocks=None))]
    fn publish(
        &self,
        dp_rank: Option<u32>,
        active_decode_blocks: Option<u64>,
        kv_used_blocks: Option<u64>,
    ) -> PyResult<()> {
GuanLuo's avatar
GuanLuo committed
246
        self.inner
247
            .publish(dp_rank, active_decode_blocks, kv_used_blocks)
GuanLuo's avatar
GuanLuo committed
248
249
250
            .map_err(to_pyerr)
    }
}
251

252
253
254
#[pyclass]
pub(crate) struct KvEventPublisher {
    inner: Arc<llm_rs::kv_router::publisher::KvEventPublisher>,
255
    kv_block_size: usize,
Yan Ru Pei's avatar
Yan Ru Pei committed
256
    dp_rank: DpRank,
257
    warning_count: Arc<AtomicU32>,
258
259
260
261
}

#[pymethods]
impl KvEventPublisher {
262
263
264
265
266
267
268
269
270
271
272
273
    /// Create a KV event publisher that batches raw engine events before forwarding
    /// them to NATS / the event plane.
    ///
    /// Args:
    ///     endpoint: The Dynamo component endpoint for this worker.
    ///     worker_id: Identifier of this worker (default 0).
    ///     kv_block_size: KV cache block size in tokens; must be > 0.
    ///     dp_rank: Data-parallel rank of this worker (default 0).
    ///     enable_local_indexer: When True, a local KV indexer is kept in-process
    ///         so that routers can recover events directly from this worker.
    ///     zmq_endpoint: Optional ZMQ SUB endpoint to read raw engine events from.
    ///     zmq_topic: ZMQ topic filter (default "").
274
    ///     batching_timeout_ms: Maximum time (in **milliseconds**) to accumulate
275
    ///         events into a single batch before flushing.
276
277
278
279
    ///         ``None`` disables batching: every event is published immediately.
    ///         ``50`` to enable batching with a 50 ms window.
    ///         ``0`` is treated as ``None`` (also disables batching).
    ///         Maximum allowed is 15_000 (15 seconds); larger values are capped.
280
    #[new]
281
    #[pyo3(signature = (endpoint, worker_id=0, kv_block_size=0, dp_rank=0, enable_local_indexer=false, zmq_endpoint=None, zmq_topic=None, batching_timeout_ms=llm_rs::kv_router::publisher::DEFAULT_BATCHING_TIMEOUT_MS))]
282
    #[allow(clippy::too_many_arguments)]
283
    fn new(
284
        endpoint: Endpoint,
285
286
287
        worker_id: WorkerId,
        kv_block_size: usize,
        dp_rank: DpRank,
288
        enable_local_indexer: bool,
289
290
        zmq_endpoint: Option<String>,
        zmq_topic: Option<String>,
291
        batching_timeout_ms: Option<u64>,
292
    ) -> PyResult<Self> {
293
294
        let _ = worker_id;

295
296
        let source_config = zmq_endpoint.map(|ep| KvEventSourceConfig::Zmq {
            endpoint: ep,
297
298
            topic: zmq_topic.unwrap_or_default(),
        });
299

Yan Ru Pei's avatar
Yan Ru Pei committed
300
301
302
303
        if kv_block_size == 0 {
            return Err(to_pyerr(anyhow::anyhow!("kv_block_size cannot be 0")));
        }

304
305
306
        // Extract component from endpoint
        let component = endpoint.inner.component().clone();

307
        let inner = llm_rs::kv_router::publisher::KvEventPublisher::new_with_local_indexer(
308
            component,
309
            kv_block_size as u32,
310
            source_config,
311
            enable_local_indexer,
312
            dp_rank,
313
            batching_timeout_ms,
314
315
        )
        .map_err(to_pyerr)?;
316

317
318
        Ok(Self {
            inner: inner.into(),
319
            kv_block_size,
Yan Ru Pei's avatar
Yan Ru Pei committed
320
            dp_rank,
321
            warning_count: Arc::new(AtomicU32::new(0)),
322
323
324
325
        })
    }

    #[allow(clippy::too_many_arguments)]
326
    #[pyo3(signature = (token_ids, num_block_tokens, block_hashes, parent_hash=None, block_mm_infos=None, lora_name=None, is_eagle=None))]
327
    fn publish_stored(
328
        &self,
329
        py: Python,
330
331
        token_ids: Vec<u32>,
        num_block_tokens: Vec<u64>,
332
333
        block_hashes: Vec<i64>,
        parent_hash: Option<i64>,
334
        block_mm_infos: Option<Bound<PyAny>>,
335
        lora_name: Option<String>,
336
        is_eagle: Option<bool>,
337
    ) -> PyResult<()> {
338
339
340
341
342
        let kv_block_size = self.kv_block_size as u32;
        let dp_rank = self.dp_rank;
        let warning_count = self.warning_count.clone();
        let inner = self.inner.clone();

343
344
        let event_id = inner.next_event_id();

345
        let mm_infos = block_mm_infos
346
            .as_ref()
347
            .map(depythonize_block_mm_infos)
348
349
            .transpose()?;

350
351
352
353
354
355
356
357
358
359
360
        py.allow_threads(|| {
            let block_hashes_u64: Vec<u64> = block_hashes.iter().map(|&h| h as u64).collect();
            let event = KvCacheEvent {
                event_id,
                data: KvCacheEventData::Stored(KvCacheStoreData {
                    parent_hash: parent_hash.map(ExternalSequenceBlockHash::from),
                    blocks: create_stored_blocks(
                        kv_block_size,
                        &token_ids,
                        &num_block_tokens,
                        &block_hashes_u64,
361
                        lora_name.as_deref(),
362
                        &warning_count,
363
                        mm_infos.as_deref(),
364
                        is_eagle,
365
366
367
368
369
370
371
                    ),
                }),
                dp_rank,
            };

            inner.publish(event).map_err(to_pyerr)
        })
372
373
    }

374
    fn publish_removed(&self, py: Python, block_hashes: Vec<i64>) -> PyResult<()> {
375
376
377
        let dp_rank = self.dp_rank;
        let inner = self.inner.clone();

378
379
380
        // Use shared monotonic event_id counter from the inner publisher
        let event_id = inner.next_event_id();

381
382
383
384
385
386
387
388
389
390
391
392
393
        py.allow_threads(|| {
            let block_hashes: Vec<ExternalSequenceBlockHash> = block_hashes
                .into_iter()
                .map(ExternalSequenceBlockHash::from)
                .collect();
            let event = KvCacheEvent {
                event_id,
                data: KvCacheEventData::Removed(KvCacheRemoveData { block_hashes }),
                dp_rank,
            };

            inner.publish(event).map_err(to_pyerr)
        })
394
    }
395
396
397
398
399
400
401
402

    fn shutdown(&mut self) {
        // If no other Arc clones exist, shut down eagerly.
        // Otherwise the Drop impl handles cleanup when the last reference is freed.
        if let Some(inner) = Arc::get_mut(&mut self.inner) {
            inner.shutdown();
        }
    }
403
404
}

405
406
407
#[pyclass]
#[derive(Clone)]
pub(crate) struct OverlapScores {
408
    inner: dynamo_kv_router::protocols::OverlapScores,
409
410
411
412
413
}

#[pymethods]
impl OverlapScores {
    #[getter]
414
    fn scores(&self) -> HashMap<(u64, u32), u32> {
Yan Ru Pei's avatar
Yan Ru Pei committed
415
416
417
418
419
420
        // Return scores with full WorkerWithDpRank granularity as (worker_id, dp_rank) tuples
        self.inner
            .scores
            .iter()
            .map(|(worker, score)| ((worker.worker_id, worker.dp_rank), *score))
            .collect()
421
422
423
424
425
426
427
428
    }

    #[getter]
    fn frequencies(&self) -> Vec<usize> {
        self.inner.frequencies.clone()
    }
}

429
430
431
#[derive(Debug)]
enum RadixTreeRequest {
    FindMatches {
432
        local_block_hashes: Vec<LocalBlockHash>,
433
        early_exit: bool,
434
        response_tx: mpsc::SyncSender<dynamo_kv_router::protocols::OverlapScores>,
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
    },
    ApplyEvent {
        worker_id: WorkerId,
        kv_cache_event_bytes: Vec<u8>,
        response_tx: mpsc::SyncSender<PyResult<()>>,
    },
    RemoveWorker {
        worker_id: WorkerId,
        response_tx: mpsc::SyncSender<()>,
    },
    ClearAllBlocks {
        worker_id: WorkerId,
        response_tx: mpsc::SyncSender<()>,
    },
    DumpTreeAsEvents {
450
        response_tx: mpsc::SyncSender<Vec<RouterEvent>>,
451
452
453
454
455
456
    },
    Shutdown,
}

// NOTE: RadixTree is now thread-safe with pure sync patterns
#[pyclass]
Yan Ru Pei's avatar
Yan Ru Pei committed
457
pub(crate) struct RadixTree {
458
    request_tx: mpsc::Sender<RadixTreeRequest>,
Yan Ru Pei's avatar
Yan Ru Pei committed
459
460
461
462
463
464
465
466
}

#[pymethods]
impl RadixTree {
    #[new]
    #[pyo3(signature = (expiration_duration_secs=None))]
    fn new(expiration_duration_secs: Option<f64>) -> PyResult<Self> {
        let expiration_duration = expiration_duration_secs.map(std::time::Duration::from_secs_f64);
467
468
469
470
471
472

        let (request_tx, request_rx) = mpsc::channel::<RadixTreeRequest>();

        // Spawn dedicated thread with simplified sync processing
        std::thread::spawn(move || {
            let mut radix_tree =
473
                dynamo_kv_router::indexer::RadixTree::new_with_frequency(expiration_duration);
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492

            loop {
                match request_rx.recv() {
                    Ok(RadixTreeRequest::Shutdown) => {
                        tracing::debug!("RadixTree thread received shutdown request");
                        break;
                    }
                    Ok(request) => {
                        Self::handle_request(&mut radix_tree, request);
                    }
                    Err(mpsc::RecvError) => {
                        tracing::debug!("RadixTree request channel disconnected");
                        break;
                    }
                }
            }
        });

        Ok(Self { request_tx })
Yan Ru Pei's avatar
Yan Ru Pei committed
493
494
495
496
497
    }

    #[pyo3(signature = (sequence, early_exit=false))]
    fn find_matches(
        &self,
498
        py: Python,
Yan Ru Pei's avatar
Yan Ru Pei committed
499
500
501
        sequence: Vec<u64>,
        early_exit: bool,
    ) -> PyResult<OverlapScores> {
502
503
        let (response_tx, response_rx) = mpsc::sync_channel(1);

504
505
        let local_block_hashes =
            py.allow_threads(|| sequence.into_iter().map(LocalBlockHash).collect());
506
507
508
509
510
511

        let request = RadixTreeRequest::FindMatches {
            local_block_hashes,
            early_exit,
            response_tx,
        };
Yan Ru Pei's avatar
Yan Ru Pei committed
512

513
514
515
516
517
518
519
520
521
522
523
524
525
526
        self.request_tx.send(request).map_err(|_| {
            PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
                "RadixTree background task has shut down",
            )
        })?;

        // Release GIL while waiting for response
        let result = py.allow_threads(move || {
            response_rx.recv().map_err(|_| {
                PyErr::new::<pyo3::exceptions::PyRuntimeError, _>("RadixTree request was cancelled")
            })
        })?;

        Ok(OverlapScores { inner: result })
Yan Ru Pei's avatar
Yan Ru Pei committed
527
528
529
    }

    fn apply_event(
530
531
        &self,
        py: Python,
Yan Ru Pei's avatar
Yan Ru Pei committed
532
        worker_id: WorkerId,
Yan Ru Pei's avatar
Yan Ru Pei committed
533
534
        kv_cache_event_bytes: &[u8],
    ) -> PyResult<()> {
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
        let (response_tx, response_rx) = mpsc::sync_channel(1);

        let request = RadixTreeRequest::ApplyEvent {
            worker_id,
            kv_cache_event_bytes: kv_cache_event_bytes.to_vec(),
            response_tx,
        };

        self.request_tx.send(request).map_err(|_| {
            PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
                "RadixTree background task has shut down",
            )
        })?;

        // Release GIL while waiting for response
        let result = py.allow_threads(move || response_rx.recv());

        result.map_err(|_| {
            PyErr::new::<pyo3::exceptions::PyRuntimeError, _>("RadixTree request was cancelled")
        })?
    }

    fn remove_worker(&self, py: Python, worker_id: WorkerId) -> PyResult<()> {
        let (response_tx, response_rx) = mpsc::sync_channel(1);

        let request = RadixTreeRequest::RemoveWorker {
            worker_id,
            response_tx,
        };

        self.request_tx.send(request).map_err(|_| {
            PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
                "RadixTree background task has shut down",
            )
        })?;

        // Release GIL while waiting for response
        py.allow_threads(move || {
            response_rx.recv().map_err(|_| {
                PyErr::new::<pyo3::exceptions::PyRuntimeError, _>("RadixTree request was cancelled")
            })
        })
    }

    fn clear_all_blocks(&self, py: Python, worker_id: WorkerId) -> PyResult<()> {
        let (response_tx, response_rx) = mpsc::sync_channel(1);

        let request = RadixTreeRequest::ClearAllBlocks {
            worker_id,
            response_tx,
        };

        self.request_tx.send(request).map_err(|_| {
            PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
                "RadixTree background task has shut down",
            )
        })?;

        // Release GIL while waiting for response
        py.allow_threads(move || {
            response_rx.recv().map_err(|_| {
                PyErr::new::<pyo3::exceptions::PyRuntimeError, _>("RadixTree request was cancelled")
            })
        })
    }
Yan Ru Pei's avatar
Yan Ru Pei committed
600

601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
    fn dump_tree_as_events(&self, py: Python) -> PyResult<Vec<String>> {
        let (response_tx, response_rx) = mpsc::sync_channel(1);

        let request = RadixTreeRequest::DumpTreeAsEvents { response_tx };

        self.request_tx.send(request).map_err(|_| {
            PyErr::new::<pyo3::exceptions::PyRuntimeError, _>("Failed to send dump tree request")
        })?;

        // Release GIL while waiting for response from dedicated thread
        let events = py.allow_threads(move || {
            response_rx.recv().map_err(|_| {
                PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
                    "Failed to receive dump tree response",
                )
            })
        })?;

        // Serialize RouterEvent structs to JSON strings with GIL released
        py.allow_threads(move || {
            events
                .into_iter()
                .map(|event| {
                    serde_json::to_string(&event).map_err(|e| {
                        PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
                            "Failed to serialize event to JSON: {}",
                            e
                        ))
                    })
                })
                .collect::<Result<Vec<String>, PyErr>>()
        })
Yan Ru Pei's avatar
Yan Ru Pei committed
633
    }
634
}
Yan Ru Pei's avatar
Yan Ru Pei committed
635

636
637
impl RadixTree {
    fn handle_request(
638
        radix_tree: &mut dynamo_kv_router::indexer::RadixTree,
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
        request: RadixTreeRequest,
    ) {
        match request {
            RadixTreeRequest::FindMatches {
                local_block_hashes,
                early_exit,
                response_tx,
            } => {
                let result = radix_tree.find_matches(local_block_hashes, early_exit);
                let _ = response_tx.send(result);
            }
            RadixTreeRequest::ApplyEvent {
                worker_id,
                kv_cache_event_bytes,
                response_tx,
            } => {
655
                let result = match serde_json::from_slice::<KvCacheEvent>(&kv_cache_event_bytes) {
656
                    Ok(kv_cache_event) => {
657
                        let router_event = RouterEvent::new(worker_id, kv_cache_event);
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
                        match radix_tree.apply_event(router_event) {
                            Ok(_) => Ok(()),
                            Err(e) => Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
                                format!("Failed to apply event: {}", e),
                            )),
                        }
                    }
                    Err(e) => Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
                        "Failed to deserialize KvCacheEvent: {}",
                        e
                    ))),
                };
                let _ = response_tx.send(result);
            }
            RadixTreeRequest::RemoveWorker {
                worker_id,
                response_tx,
            } => {
                radix_tree.remove_worker(worker_id);
                let _ = response_tx.send(());
            }
            RadixTreeRequest::ClearAllBlocks {
                worker_id,
                response_tx,
            } => {
                radix_tree.clear_all_blocks(worker_id);
                let _ = response_tx.send(());
            }
            RadixTreeRequest::DumpTreeAsEvents { response_tx } => {
                let events = radix_tree.dump_tree_as_events();
                let _ = response_tx.send(events);
            }
            RadixTreeRequest::Shutdown => {
                // This is handled in the main loop
            }
        }
Yan Ru Pei's avatar
Yan Ru Pei committed
694
    }
695
}
Yan Ru Pei's avatar
Yan Ru Pei committed
696

697
698
699
700
701
// Cleanup when RadixTree is dropped
impl Drop for RadixTree {
    fn drop(&mut self) {
        // Only need graceful shutdown via RadixTreeRequest::Shutdown
        let _ = self.request_tx.send(RadixTreeRequest::Shutdown);
Yan Ru Pei's avatar
Yan Ru Pei committed
702
703
704
    }
}

705
/// Helper function to create a KV router from an endpoint using the ModelManager
706
707
708
709
710
/// to ensure proper etcd registration.
/// Infers worker type using endpoint naming and router config:
/// - If endpoint name/component contains "prefill", treat as prefill
/// - If router_track_active_blocks is disabled, treat as prefill
/// - Otherwise, default to decode
711
712
713
async fn create_kv_router_from_endpoint(
    endpoint: &Endpoint,
    block_size: usize,
714
    kv_router_config: Option<KvRouterConfig>,
715
    prefill_load_estimator: Option<Arc<dyn dynamo_kv_router::PrefillLoadEstimator>>,
716
) -> Result<Arc<llm_rs::kv_router::KvRouter>, PyErr> {
717
    // Create ModelManager and use it to create KvRouter (ensures registration)
718
    let model_manager = Arc::new(llm_rs::discovery::ModelManager::new());
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
    let endpoint_id = endpoint.inner.id();
    let namespace = endpoint_id.namespace.to_lowercase();
    let component = endpoint_id.component.to_lowercase();
    let name = endpoint_id.name.to_lowercase();
    let endpoint_is_prefill =
        namespace.contains("prefill") || component.contains("prefill") || name.contains("prefill");
    let track_active_blocks = kv_router_config
        .as_ref()
        .map(|cfg| cfg.router_track_active_blocks)
        .unwrap_or(true);
    let worker_type = if endpoint_is_prefill || !track_active_blocks {
        llm_rs::discovery::WORKER_TYPE_PREFILL
    } else {
        llm_rs::discovery::WORKER_TYPE_DECODE
    };
734

735
736
    // Query discovery once so we can derive both model_name (for remote indexer)
    // and Eagle routing semantics from the model card.
737
738
739
740
    let needs_model_name = kv_router_config
        .as_ref()
        .map(|cfg| cfg.remote_indexer_component.is_some())
        .unwrap_or(false);
741
    let (model_name, enable_eagle) = {
742
743
744
745
746
747
748
749
750
751
        let discovery = endpoint.inner.component().drt().discovery();
        let instances = discovery
            .list(rs::discovery::DiscoveryQuery::EndpointModels {
                namespace: endpoint_id.namespace.clone(),
                component: endpoint_id.component.clone(),
                endpoint: endpoint_id.name.clone(),
            })
            .await
            .map_err(to_pyerr)?;

752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
        let maybe_card = instances.into_iter().find_map(|inst| {
            inst.deserialize_model::<llm_rs::model_card::ModelDeploymentCard>()
                .ok()
        });

        match maybe_card {
            Some(card) => {
                let model_name = needs_model_name.then(|| card.display_name.clone());
                (model_name, card.runtime_config.enable_eagle)
            }
            None => {
                tracing::warn!(
                    namespace = %endpoint_id.namespace,
                    component = %endpoint_id.component,
                    endpoint = %endpoint_id.name,
                    "No model card found in discovery; defaulting to non-Eagle routing semantics"
                );
                (None, false)
            }
        }
772
773
    };

774
    let kv_router = model_manager
775
776
777
778
        .kv_chooser_for(
            &endpoint.inner,
            block_size as u32,
            kv_router_config,
779
            prefill_load_estimator,
780
            worker_type,
781
            model_name,
782
            enable_eagle,
783
        )
784
785
786
787
788
789
        .await
        .map_err(to_pyerr)?;

    Ok(kv_router)
}

790
#[pyclass]
791
792
pub(crate) struct KvRouter {
    inner: Arc<RsKvPushRouter>,
Yan Ru Pei's avatar
Yan Ru Pei committed
793
794
}

795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/// Inject worker_id info from tracker into response's disaggregated_params.
/// This is needed for Python bindings to expose worker routing info since
/// the raw LLMEngineOutput doesn't go through DeltaGenerator (which adds nvext).
fn inject_worker_id_from_tracker(
    data: &mut llm_rs::protocols::common::llm_backend::LLMEngineOutput,
    tracker: &RequestTracker,
) {
    let Some(worker_info) = tracker.get_worker_info() else {
        return;
    };

    let worker_id_json =
        serde_json::to_value(&worker_info).expect("WorkerIdInfo serialization should not fail");

    if let Some(obj) = data
        .disaggregated_params
        .as_mut()
        .and_then(|p| p.as_object_mut())
    {
        obj.insert("worker_id".to_string(), worker_id_json);
    } else {
        data.disaggregated_params = Some(json!({"worker_id": worker_id_json}));
    }
}

Yan Ru Pei's avatar
Yan Ru Pei committed
820
// TODO: can this reuse the stream conversion method in Client bindings?
821
impl KvRouter {
Yan Ru Pei's avatar
Yan Ru Pei committed
822
823
824
    /// Helper method to process a request and create a Python async generator
    fn process_request_to_stream<'p>(
        py: Python<'p>,
825
        inner: Arc<RsKvPushRouter>,
Yan Ru Pei's avatar
Yan Ru Pei committed
826
        request: llm_rs::protocols::common::preprocessor::PreprocessedRequest,
827
        tracker: Option<Arc<RequestTracker>>,
Yan Ru Pei's avatar
Yan Ru Pei committed
828
829
830
831
    ) -> PyResult<Bound<'p, PyAny>> {
        pyo3_async_runtimes::tokio::future_into_py(py, async move {
            let single_in = SingleIn::new(request);
            let stream = inner.generate(single_in).await.map_err(to_pyerr)?;
832
            let (tx, rx) = tokio::sync::mpsc::channel::<RsAnnotated<PyObject>>(100);
Yan Ru Pei's avatar
Yan Ru Pei committed
833
834
835

            tokio::spawn(async move {
                let mut stream = stream;
836
                let mut first_item = true;
837
                let mut first_token_gauges_observed = false;
838
839
840
841
842
843
844
845
846

                while let Some(mut response) = stream.next().await {
                    if first_item {
                        first_item = false;
                        if let (Some(tracker), Some(data)) = (&tracker, &mut response.data) {
                            inject_worker_id_from_tracker(data, tracker);
                        }
                    }

847
848
849
850
851
852
853
854
855
856
857
858
859
860
                    if !first_token_gauges_observed {
                        let has_tokens = response
                            .data
                            .as_ref()
                            .map(|d| !d.token_ids.is_empty())
                            .unwrap_or(false);
                        if has_tokens {
                            if let Some(ref tracker) = tracker {
                                tracker.observe_first_token_gauges();
                            }
                            first_token_gauges_observed = true;
                        }
                    }

Yan Ru Pei's avatar
Yan Ru Pei committed
861
862
863
864
865
866
867
868
                    let py_response = Python::with_gil(|py| {
                        pythonize(py, &response.data)
                            .map(|obj| obj.unbind())
                            .map_err(|e| e.to_string())
                    });

                    match py_response {
                        Ok(obj) => {
869
870
                            if tx.send(RsAnnotated::from_data(obj)).await.is_err() {
                                break;
Yan Ru Pei's avatar
Yan Ru Pei committed
871
872
873
874
875
876
877
878
                            }
                        }
                        Err(e) => {
                            tracing::error!("Failed to pythonize response: {}", e);
                            break;
                        }
                    }
                }
879
880
881
882

                if let Some(ref tracker) = tracker {
                    tracker.observe_finish_gauges();
                }
Yan Ru Pei's avatar
Yan Ru Pei committed
883
884
            });

885
            Ok(crate::AsyncResponseStream::new(rx, false))
Yan Ru Pei's avatar
Yan Ru Pei committed
886
887
        })
    }
888
889
890
}

#[pymethods]
891
892
impl KvRouter {
    /// Create a new KvRouter for KV-aware routing to workers.
893
894
895
896
897
898
899
900
    ///
    /// # Arguments
    /// * `endpoint` - The endpoint to route requests to
    /// * `block_size` - KV cache block size for routing decisions
    /// * `kv_router_config` - Configuration for the KV router
    ///
    /// Note: Worker type for Prometheus metrics is inferred from the endpoint name/component
    /// (contains "prefill") or by `router_track_active_blocks` being disabled.
901
    #[new]
902
    #[pyo3(signature = (endpoint, block_size, kv_router_config, aic_perf_config=None))]
903
904
905
906
    fn new(
        endpoint: &Endpoint,
        block_size: usize,
        kv_router_config: &super::entrypoint::KvRouterConfig,
907
        aic_perf_config: Option<&AicPerfConfig>,
908
    ) -> PyResult<Self> {
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
        let prefill_load_estimator = aic_perf_config
            .map(|config| {
                Python::with_gil(|py| {
                    create_aic_prefill_load_estimator(
                        py,
                        config.backend_name(),
                        config.system(),
                        config.model_path(),
                        config.tp_size(),
                        config.backend_version(),
                    )
                })
            })
            .transpose()
            .map_err(to_pyerr)?;

925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
        let runtime = pyo3_async_runtimes::tokio::get_runtime();
        runtime.block_on(async move {
            let client = endpoint.inner.client().await.map_err(to_pyerr)?;

            // Create PushRouter with KV router mode
            let push_router = rs::pipeline::PushRouter::<
                llm_rs::protocols::common::preprocessor::PreprocessedRequest,
                rs::protocols::annotated::Annotated<
                    llm_rs::protocols::common::llm_backend::LLMEngineOutput,
                >,
            >::from_client(
                client,
                rs::pipeline::network::egress::push_router::RouterMode::KV,
            )
            .await
            .map_err(to_pyerr)?;

942
943
944
945
946
            // Create KvRouter using helper function (ensures etcd registration)
            let kv_router = create_kv_router_from_endpoint(
                endpoint,
                block_size,
                Some(kv_router_config.inner()),
947
                prefill_load_estimator,
948
949
            )
            .await?;
950

951
            let kv_push_router = RsKvPushRouter::new(push_router, kv_router);
952
953
954
955
956
957
958
959

            Ok(Self {
                inner: Arc::new(kv_push_router),
            })
        })
    }

    #[allow(clippy::too_many_arguments)]
960
    #[pyo3(signature = (token_ids, model, stop_conditions=None, sampling_options=None, output_options=None, router_config_override=None, worker_id=None, dp_rank=None, extra_args=None, block_mm_infos=None, multi_modal_data=None, mm_routing_info=None))]
961
962
963
964
965
966
967
968
969
    fn generate<'p>(
        &self,
        py: Python<'p>,
        token_ids: Vec<u32>,
        model: String,
        stop_conditions: Option<PyObject>,
        sampling_options: Option<PyObject>,
        output_options: Option<PyObject>,
        router_config_override: Option<PyObject>,
Yan Ru Pei's avatar
Yan Ru Pei committed
970
971
        worker_id: Option<WorkerId>,
        dp_rank: Option<DpRank>,
972
        extra_args: Option<PyObject>,
973
974
975
        block_mm_infos: Option<PyObject>,
        multi_modal_data: Option<PyObject>,
        mm_routing_info: Option<PyObject>,
976
977
    ) -> PyResult<Bound<'p, PyAny>> {
        // Depythonize the options with defaults
978
979
980
981
982
        let stop_conditions: StopConditions = if let Some(obj) = stop_conditions {
            depythonize(obj.bind(py)).map_err(to_pyerr)?
        } else {
            StopConditions::default()
        };
983

984
985
986
987
988
        let sampling_options: SamplingOptions = if let Some(obj) = sampling_options {
            depythonize(obj.bind(py)).map_err(to_pyerr)?
        } else {
            SamplingOptions::default()
        };
989

990
991
992
993
994
        let output_options: OutputOptions = if let Some(obj) = output_options {
            depythonize(obj.bind(py)).map_err(to_pyerr)?
        } else {
            OutputOptions::default()
        };
995

996
        let router_config_override: Option<RouterConfigOverride> =
997
998
999
1000
1001
            if let Some(obj) = router_config_override {
                Some(depythonize(obj.bind(py)).map_err(to_pyerr)?)
            } else {
                None
            };
1002

1003
1004
1005
1006
1007
        let extra_args: Option<serde_json::Value> = if let Some(obj) = extra_args {
            Some(depythonize(obj.bind(py)).map_err(to_pyerr)?)
        } else {
            None
        };
1008

1009
1010
1011
        let block_mm_infos = block_mm_infos
            .map(|obj| depythonize_block_mm_infos(obj.bind(py)))
            .transpose()?;
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031

        let multi_modal_data: Option<llm_rs::protocols::common::preprocessor::MultimodalDataMap> =
            if let Some(obj) = multi_modal_data {
                Some(depythonize(obj.bind(py)).map_err(to_pyerr)?)
            } else {
                None
            };

        let mm_routing_info: Option<llm_rs::protocols::common::preprocessor::MmRoutingInfo> =
            if let Some(obj) = mm_routing_info {
                Some(depythonize(obj.bind(py)).map_err(to_pyerr)?)
            } else {
                block_mm_infos.map(
                    |infos| llm_rs::protocols::common::preprocessor::MmRoutingInfo {
                        routing_token_ids: token_ids.clone(),
                        block_mm_infos: infos,
                    },
                )
            };

1032
1033
1034
        // Create tracker to capture worker routing info from KvRouter
        let tracker = Arc::new(RequestTracker::new());

1035
        // Build the PreprocessedRequest
1036
1037
1038
        let mut request_builder =
            llm_rs::protocols::common::preprocessor::PreprocessedRequest::builder();
        request_builder
1039
1040
1041
1042
1043
            .model(model)
            .token_ids(token_ids)
            .stop_conditions(stop_conditions)
            .sampling_options(sampling_options)
            .output_options(output_options)
1044
            .router_config_override(router_config_override)
1045
1046
            .multi_modal_data(multi_modal_data)
            .mm_routing_info(mm_routing_info)
1047
1048
            .extra_args(extra_args)
            .tracker(Some(tracker.clone()));
1049

1050
1051
1052
1053
1054
1055
1056
1057
        // Set routing hints if worker_id or dp_rank is provided
        if worker_id.is_some() || dp_rank.is_some() {
            let routing = llm_rs::protocols::common::preprocessor::RoutingHints {
                backend_instance_id: worker_id,
                dp_rank,
                ..Default::default()
            };
            request_builder.routing(Some(routing));
1058
1059
1060
        }

        let request = request_builder.build().map_err(to_pyerr)?;
1061

Yan Ru Pei's avatar
Yan Ru Pei committed
1062
        // Use the helper method to process the request
1063
        Self::process_request_to_stream(py, self.inner.clone(), request, Some(tracker))
Yan Ru Pei's avatar
Yan Ru Pei committed
1064
    }
1065

Yan Ru Pei's avatar
Yan Ru Pei committed
1066
1067
1068
1069
1070
1071
    fn generate_from_request<'p>(
        &self,
        py: Python<'p>,
        request: PyObject,
    ) -> PyResult<Bound<'p, PyAny>> {
        // Depythonize the request directly into PreprocessedRequest
1072
        let mut request: llm_rs::protocols::common::preprocessor::PreprocessedRequest =
1073
            depythonize(request.bind(py)).map_err(to_pyerr)?;
1074

1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
        // Create tracker if not already set, to capture worker routing info
        let tracker = match request.tracker {
            Some(ref t) => t.clone(),
            None => {
                let t = Arc::new(RequestTracker::new());
                request.tracker = Some(t.clone());
                t
            }
        };

Yan Ru Pei's avatar
Yan Ru Pei committed
1085
        // Use the helper method to process the request
1086
        Self::process_request_to_stream(py, self.inner.clone(), request, Some(tracker))
1087
    }
1088

1089
1090
    #[allow(clippy::too_many_arguments)]
    #[pyo3(signature = (token_ids, router_config_override=None, request_id=None, update_indexer=false, block_mm_infos=None, lora_name=None))]
Yan Ru Pei's avatar
Yan Ru Pei committed
1091
1092
1093
1094
1095
1096
    fn best_worker<'p>(
        &self,
        py: Python<'p>,
        token_ids: Vec<u32>,
        router_config_override: Option<PyObject>,
        request_id: Option<String>,
1097
        update_indexer: bool,
1098
        block_mm_infos: Option<PyObject>,
1099
        lora_name: Option<String>,
Yan Ru Pei's avatar
Yan Ru Pei committed
1100
1101
    ) -> PyResult<Bound<'p, PyAny>> {
        let router_config_override = if let Some(obj) = router_config_override {
1102
            let override_config: RouterConfigOverride =
1103
1104
                depythonize(obj.bind(py)).map_err(to_pyerr)?;
            Some(override_config)
Yan Ru Pei's avatar
Yan Ru Pei committed
1105
1106
1107
1108
        } else {
            None
        };

1109
1110
1111
        let block_mm_infos = block_mm_infos
            .map(|obj| depythonize_block_mm_infos(obj.bind(py)))
            .transpose()?;
1112

Yan Ru Pei's avatar
Yan Ru Pei committed
1113
1114
1115
1116
1117
1118
1119
1120
        let chooser = self.inner.chooser.clone();
        let update_states = request_id.is_some();

        pyo3_async_runtimes::tokio::future_into_py(py, async move {
            let (best_worker, overlap_blocks) = chooser
                .find_best_match(
                    request_id.as_deref(),
                    &token_ids,
1121
                    block_mm_infos.as_deref(),
Yan Ru Pei's avatar
Yan Ru Pei committed
1122
1123
                    router_config_override.as_ref(),
                    update_states,
1124
                    lora_name.clone(),
1125
                    0.0,
1126
                    None,
1127
                    None, // allowed_worker_ids: pass via RoutingHints in PreprocessedRequest path
Yan Ru Pei's avatar
Yan Ru Pei committed
1128
1129
1130
1131
                )
                .await
                .map_err(to_pyerr)?;

1132
            if update_indexer && !chooser.kv_router_config().use_kv_events {
1133
1134
1135
1136
1137
1138
1139
1140
1141
                let mut tokens_with_hashes =
                    TokensWithHashes::new(token_ids.clone(), chooser.block_size())
                        .with_is_eagle(chooser.is_eagle());
                if let Some(infos) = block_mm_infos.as_ref() {
                    tokens_with_hashes = tokens_with_hashes.with_mm_infos(infos.clone());
                }
                if let Some(lora_name) = lora_name.as_ref() {
                    tokens_with_hashes = tokens_with_hashes.with_lora_name(lora_name.clone());
                }
1142
                chooser
1143
                    .record_routing_decision(tokens_with_hashes, best_worker)
1144
1145
1146
1147
                    .await
                    .map_err(to_pyerr)?;
            }

Yan Ru Pei's avatar
Yan Ru Pei committed
1148
1149
1150
1151
            Ok((best_worker.worker_id, best_worker.dp_rank, overlap_blocks))
        })
    }

1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
    /// Mark prefill as completed for a request
    fn mark_prefill_complete<'p>(
        &self,
        py: Python<'p>,
        request_id: String,
    ) -> PyResult<Bound<'p, PyAny>> {
        let chooser = self.inner.chooser.clone();

        pyo3_async_runtimes::tokio::future_into_py(py, async move {
            chooser
                .mark_prefill_completed(&request_id)
                .await
                .map_err(to_pyerr)?;
            Ok(())
        })
    }

    /// Free a request by its ID, signaling the router to release resources
    fn free<'p>(&self, py: Python<'p>, request_id: String) -> PyResult<Bound<'p, PyAny>> {
        let chooser = self.inner.chooser.clone();

        pyo3_async_runtimes::tokio::future_into_py(py, async move {
            chooser.free(&request_id).await.map_err(to_pyerr)?;
            Ok(())
        })
    }

1179
    #[pyo3(signature = (token_ids, block_mm_infos=None, lora_name=None))]
1180
1181
1182
1183
    fn get_potential_loads<'p>(
        &self,
        py: Python<'p>,
        token_ids: Vec<u32>,
1184
        block_mm_infos: Option<PyObject>,
1185
        lora_name: Option<String>,
1186
    ) -> PyResult<Bound<'p, PyAny>> {
1187
1188
1189
        let block_mm_infos = block_mm_infos
            .map(|obj| depythonize_block_mm_infos(obj.bind(py)))
            .transpose()?;
1190
        let chooser = self.inner.chooser.clone();
1191
1192

        pyo3_async_runtimes::tokio::future_into_py(py, async move {
1193
            let loads = chooser
1194
1195
1196
1197
1198
1199
                .get_potential_loads(
                    &token_ids,
                    None,
                    block_mm_infos.as_deref(),
                    lora_name.as_deref(),
                )
1200
1201
1202
                .await
                .map_err(to_pyerr)?;

Yan Ru Pei's avatar
Yan Ru Pei committed
1203
            // Return loads without aggregation - each (worker_id, dp_rank) pair is a separate entry
1204
1205
1206
1207
1208
1209
1210
1211
1212
            // Use pythonize to convert Vec<PotentialLoad> to Python list of dicts
            Python::with_gil(|py| {
                pythonize(py, &loads)
                    .map(|obj| obj.unbind())
                    .map_err(to_pyerr)
            })
        })
    }

1213
1214
    /// Dump all events from the KV router's indexer as a JSON string
    fn dump_events<'p>(&self, py: Python<'p>) -> PyResult<Bound<'p, PyAny>> {
1215
        let chooser = self.inner.chooser.clone();
1216
1217

        pyo3_async_runtimes::tokio::future_into_py(py, async move {
1218
            let events = chooser.dump_events().await.map_err(to_pyerr)?;
1219
1220
1221
1222
1223
1224
            // Serialize to JSON string
            let json_str = serde_json::to_string(&events).map_err(to_pyerr)?;
            Ok(json_str)
        })
    }
}