publisher.rs 4.41 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
31
use futures::stream;
use std::sync::Arc;
use tokio::sync::mpsc;
use tracing as log;
32

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

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

43
        start_publish_task(component, worker_id, rx);
44
45
46
47
48
49
50
        Ok(p)
    }

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

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

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

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

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>>> {
        log::debug!("Publish metrics: {:?}", metrics);
        self.tx.send(metrics)
    }

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

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

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
136
137
    }
}