publisher.rs 4.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

16
use crate::kv_router::{indexer::RouterEvent, protocols::*, KV_EVENT_SUBJECT, KV_METRICS_ENDPOINT};
17
use async_trait::async_trait;
18
use dynamo_runtime::traits::{events::EventPublisher, DistributedRuntimeProvider};
Neelay Shah's avatar
Neelay Shah committed
19
use dynamo_runtime::{
20
21
22
23
24
25
    component::Component,
    pipeline::{
        network::Ingress, AsyncEngine, AsyncEngineContextProvider, ManyOut, ResponseStream,
        SingleIn,
    },
    protocols::annotated::Annotated,
26
    Error, Result,
27
};
28
29
30
use futures::stream;
use std::sync::Arc;
use tokio::sync::mpsc;
31

GuanLuo's avatar
GuanLuo committed
32
pub struct KvEventPublisher {
33
    tx: mpsc::UnboundedSender<KvCacheEvent>,
34
    kv_block_size: usize,
35
36
}

GuanLuo's avatar
GuanLuo committed
37
impl KvEventPublisher {
38
    pub fn new(component: Component, worker_id: i64, kv_block_size: usize) -> Result<Self> {
39
        let (tx, rx) = mpsc::unbounded_channel::<KvCacheEvent>();
40
        let p = KvEventPublisher { tx, kv_block_size };
41

42
        start_publish_task(component, worker_id, rx);
43
44
45
46
        Ok(p)
    }

    pub fn publish(&self, event: KvCacheEvent) -> Result<(), mpsc::error::SendError<KvCacheEvent>> {
47
        tracing::debug!("Publish event: {:?}", event);
48
49
        self.tx.send(event)
    }
50
51
52
53

    pub fn kv_block_size(&self) -> usize {
        self.kv_block_size
    }
54
55
56
}

fn start_publish_task(
57
    component: Component,
GuanLuo's avatar
GuanLuo committed
58
    worker_id: i64,
59
60
    mut rx: mpsc::UnboundedReceiver<KvCacheEvent>,
) {
61
    let component_clone = component.clone();
62
    tracing::info!("Publishing KV Events to subject: {}", KV_EVENT_SUBJECT);
63

64
    _ = component.drt().runtime().secondary().spawn(async move {
65
66
        while let Some(event) = rx.recv().await {
            let router_event = RouterEvent::new(worker_id, event);
67
68
            component_clone
                .publish(KV_EVENT_SUBJECT, &router_event)
69
70
71
72
73
                .await
                .unwrap();
        }
    });
}
GuanLuo's avatar
GuanLuo committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

pub struct KvMetricsPublisher {
    tx: tokio::sync::watch::Sender<Arc<ForwardPassMetrics>>,
    rx: tokio::sync::watch::Receiver<Arc<ForwardPassMetrics>>,
}

impl KvMetricsPublisher {
    pub fn new() -> Result<Self> {
        let (tx, rx) = tokio::sync::watch::channel(Arc::new(ForwardPassMetrics::default()));
        Ok(KvMetricsPublisher { tx, rx })
    }

    pub fn publish(
        &self,
        metrics: Arc<ForwardPassMetrics>,
    ) -> Result<(), tokio::sync::watch::error::SendError<Arc<ForwardPassMetrics>>> {
90
        tracing::trace!("Publish metrics: {metrics:?}");
GuanLuo's avatar
GuanLuo committed
91
92
93
        self.tx.send(metrics)
    }

94
    pub async fn create_endpoint(&self, component: Component) -> Result<()> {
GuanLuo's avatar
GuanLuo committed
95
        let mut metrics_rx = self.rx.clone();
96
97
98
        let handler = Arc::new(KvLoadEndpoingHander::new(metrics_rx.clone()));
        let handler = Ingress::for_engine(handler)?;

99
        component
100
            .endpoint(KV_METRICS_ENDPOINT)
101
102
            .endpoint_builder()
            .stats_handler(move |_| {
GuanLuo's avatar
GuanLuo committed
103
104
                let metrics = metrics_rx.borrow_and_update().clone();
                serde_json::to_value(&*metrics).unwrap()
105
106
            })
            .handler(handler)
107
108
            .start()
            .await
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
    }
}

struct KvLoadEndpoingHander {
    metrics_rx: tokio::sync::watch::Receiver<Arc<ForwardPassMetrics>>,
}

impl KvLoadEndpoingHander {
    pub fn new(metrics_rx: tokio::sync::watch::Receiver<Arc<ForwardPassMetrics>>) -> Self {
        Self { metrics_rx }
    }
}

#[async_trait]
impl AsyncEngine<SingleIn<()>, ManyOut<Annotated<ForwardPassMetrics>>, Error>
    for KvLoadEndpoingHander
{
    async fn generate(
        &self,
        request: SingleIn<()>,
    ) -> Result<ManyOut<Annotated<ForwardPassMetrics>>> {
        let context = request.context();
        let metrics = self.metrics_rx.borrow().clone();
        let metrics = (*metrics).clone();
        let stream = stream::iter(vec![Annotated::from_data(metrics)]);
        Ok(ResponseStream::new(Box::pin(stream), context))
GuanLuo's avatar
GuanLuo committed
135
136
    }
}