datum-agent 0.9.2

Embeddable Datum job registry and lifecycle supervisor
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
use std::{
    collections::HashMap,
    net::SocketAddr,
    sync::{
        Arc, Mutex,
        atomic::{AtomicU64, Ordering},
    },
};

use datum_net::quic::quinn;
use prost::Message as ProstMessage;
use tokio::{
    io::{AsyncRead, AsyncWrite, AsyncWriteExt},
    net::TcpStream,
    sync::{mpsc, oneshot},
    task::JoinHandle,
};

use crate::dcp::{
    DcpError, DcpResult,
    frame::{read_frame, write_frame},
    proto::{
        ClientKind, ConfigValue, DcpFrame, Event, GetConfig, Hello, JobStatus as WireJobStatus,
        JobStatusRequest, ListJobs, MetricFrame, MetricSample, PutConfig, Request, Response,
        ResponseStatus, StartJob, StopJob, SubscribeEvents, SubscribeMetrics, dcp_frame, request,
    },
    server::connect_quic_stream,
};

type PendingMap = HashMap<u64, oneshot::Sender<DcpResult<Response>>>;

#[derive(Clone)]
pub struct DcpClient {
    inner: Arc<ClientInner>,
    _reader_task: Arc<JoinHandle<()>>,
    _writer_task: Arc<JoinHandle<()>>,
    _quic_endpoint: Option<quinn::Endpoint>,
    _quic_connection: Option<quinn::Connection>,
}

struct ClientInner {
    outbound: mpsc::Sender<DcpFrame>,
    pending: Mutex<PendingMap>,
    event_subscriptions: Mutex<HashMap<u64, mpsc::Sender<Event>>>,
    metric_subscriptions: Mutex<HashMap<u64, mpsc::Sender<MetricSample>>>,
    next_request_id: AtomicU64,
}

pub struct EventSubscription {
    receiver: mpsc::Receiver<Event>,
}

impl EventSubscription {
    pub async fn recv(&mut self) -> Option<Event> {
        self.receiver.recv().await
    }
}

pub struct MetricSubscription {
    receiver: mpsc::Receiver<MetricSample>,
}

impl MetricSubscription {
    pub async fn recv(&mut self) -> Option<MetricSample> {
        self.receiver.recv().await
    }
}

impl DcpClient {
    pub async fn connect_tcp(addr: SocketAddr, hello: Hello) -> DcpResult<Self> {
        let stream = TcpStream::connect(addr).await?;
        stream.set_nodelay(true)?;
        let (reader, writer) = stream.into_split();
        let client = Self::from_parts(reader, writer, None, None);
        client.hello(hello).await?;
        Ok(client)
    }

    pub async fn connect_quic(
        addr: SocketAddr,
        server_name: &str,
        client_config: quinn::ClientConfig,
        hello: Hello,
    ) -> DcpResult<Self> {
        let (endpoint, connection, reader, writer) =
            connect_quic_stream(addr, server_name, client_config).await?;
        let client = Self::from_parts(reader, writer, Some(endpoint), Some(connection));
        client.hello(hello).await?;
        Ok(client)
    }

    fn from_parts<R, W>(
        reader: R,
        writer: W,
        quic_endpoint: Option<quinn::Endpoint>,
        quic_connection: Option<quinn::Connection>,
    ) -> Self
    where
        R: AsyncRead + Unpin + Send + 'static,
        W: AsyncWrite + Unpin + Send + 'static,
    {
        let (outbound, outbound_receiver) = mpsc::channel(256);
        let inner = Arc::new(ClientInner {
            outbound,
            pending: Mutex::new(HashMap::new()),
            event_subscriptions: Mutex::new(HashMap::new()),
            metric_subscriptions: Mutex::new(HashMap::new()),
            next_request_id: AtomicU64::new(1),
        });
        let reader_task = {
            let inner = Arc::clone(&inner);
            tokio::spawn(async move {
                read_loop(reader, inner).await;
            })
        };
        let writer_task = tokio::spawn(async move {
            let _ = write_loop(writer, outbound_receiver).await;
        });
        Self {
            inner,
            _reader_task: Arc::new(reader_task),
            _writer_task: Arc::new(writer_task),
            _quic_endpoint: quic_endpoint,
            _quic_connection: quic_connection,
        }
    }

    pub async fn hello(&self, hello: Hello) -> DcpResult<()> {
        let response = self
            .send_frame_with_response(0, DcpFrame::hello(hello))
            .await?;
        ensure_ok(response).map(|_| ())
    }

    pub async fn request(&self, request: Request) -> DcpResult<Response> {
        let response = self
            .send_frame_with_response(request.request_id, DcpFrame::request(request))
            .await?;
        ensure_ok(response)
    }

    pub async fn list_jobs(&self) -> DcpResult<Vec<WireJobStatus>> {
        let response = self
            .send_command(request::Command::ListJobs(ListJobs {}), 0)
            .await?;
        let list = crate::dcp::proto::JobList::decode(response.payload.as_slice())?;
        Ok(list.jobs)
    }

    pub async fn start_job(
        &self,
        factory_name: impl Into<String>,
        instance_name: impl Into<String>,
        params: HashMap<String, String>,
    ) -> DcpResult<WireJobStatus> {
        self.job_status_response(request::Command::StartJob(StartJob {
            factory_name: factory_name.into(),
            instance_name: instance_name.into(),
            params,
        }))
        .await
    }

    pub async fn drain_job(&self, name: impl Into<String>) -> DcpResult<WireJobStatus> {
        self.job_status_response(request::Command::DrainJob(crate::dcp::proto::DrainJob {
            name: name.into(),
        }))
        .await
    }

    pub async fn stop_job(&self, name: impl Into<String>) -> DcpResult<WireJobStatus> {
        self.job_status_response(request::Command::StopJob(StopJob { name: name.into() }))
            .await
    }

    pub async fn restart_job(&self, name: impl Into<String>) -> DcpResult<WireJobStatus> {
        self.job_status_response(request::Command::RestartJob(
            crate::dcp::proto::RestartJob { name: name.into() },
        ))
        .await
    }

    pub async fn job_status(&self, name: impl Into<String>) -> DcpResult<WireJobStatus> {
        self.job_status_response(request::Command::JobStatus(JobStatusRequest {
            name: name.into(),
        }))
        .await
    }

    pub async fn subscribe_events(&self) -> DcpResult<EventSubscription> {
        let request_id = self.next_request_id();
        let (sender, receiver) = mpsc::channel(256);
        self.inner
            .event_subscriptions
            .lock()
            .expect("DCP event subscriptions poisoned")
            .insert(request_id, sender);
        let request = Request {
            request_id,
            deadline_ms: 0,
            command: Some(request::Command::SubscribeEvents(SubscribeEvents {
                buffer: 0,
            })),
        };
        if let Err(error) = self.request(request).await {
            self.inner
                .event_subscriptions
                .lock()
                .expect("DCP event subscriptions poisoned")
                .remove(&request_id);
            return Err(error);
        }
        Ok(EventSubscription { receiver })
    }

    pub async fn subscribe_metrics(&self, interval_ms: u64) -> DcpResult<MetricSubscription> {
        let request_id = self.next_request_id();
        let (sender, receiver) = mpsc::channel(16);
        self.inner
            .metric_subscriptions
            .lock()
            .expect("DCP metric subscriptions poisoned")
            .insert(request_id, sender);
        let request = Request {
            request_id,
            deadline_ms: 0,
            command: Some(request::Command::SubscribeMetrics(SubscribeMetrics {
                interval_ms,
            })),
        };
        if let Err(error) = self.request(request).await {
            self.inner
                .metric_subscriptions
                .lock()
                .expect("DCP metric subscriptions poisoned")
                .remove(&request_id);
            return Err(error);
        }
        Ok(MetricSubscription { receiver })
    }

    pub async fn get_config(&self, key: impl Into<String>) -> DcpResult<ConfigValue> {
        let response = self
            .send_command(
                request::Command::GetConfig(GetConfig { key: key.into() }),
                0,
            )
            .await?;
        Ok(ConfigValue::decode(response.payload.as_slice())?)
    }

    pub async fn put_config(
        &self,
        key: impl Into<String>,
        value: impl Into<String>,
    ) -> DcpResult<ConfigValue> {
        let response = self
            .send_command(
                request::Command::PutConfig(PutConfig {
                    key: key.into(),
                    value: value.into(),
                }),
                0,
            )
            .await?;
        Ok(ConfigValue::decode(response.payload.as_slice())?)
    }

    async fn job_status_response(&self, command: request::Command) -> DcpResult<WireJobStatus> {
        let response = self.send_command(command, 0).await?;
        Ok(WireJobStatus::decode(response.payload.as_slice())?)
    }

    async fn send_command(
        &self,
        command: request::Command,
        deadline_ms: u64,
    ) -> DcpResult<Response> {
        let request_id = self.next_request_id();
        let request = Request {
            request_id,
            deadline_ms,
            command: Some(command),
        };
        self.request(request).await
    }

    async fn send_frame_with_response(
        &self,
        request_id: u64,
        frame: DcpFrame,
    ) -> DcpResult<Response> {
        let (sender, receiver) = oneshot::channel();
        self.inner
            .pending
            .lock()
            .expect("DCP pending map poisoned")
            .insert(request_id, sender);
        if self.inner.outbound.send(frame).await.is_err() {
            self.inner
                .pending
                .lock()
                .expect("DCP pending map poisoned")
                .remove(&request_id);
            return Err(DcpError::Closed);
        }
        receiver.await.map_err(|_| DcpError::Closed)?
    }

    fn next_request_id(&self) -> u64 {
        self.inner.next_request_id.fetch_add(1, Ordering::Relaxed)
    }
}

async fn read_loop<R>(mut reader: R, inner: Arc<ClientInner>)
where
    R: AsyncRead + Unpin,
{
    loop {
        match read_frame(&mut reader).await {
            Ok(Some(frame)) => handle_inbound_frame(frame, &inner).await,
            Ok(None) => {
                fail_pending(&inner, DcpError::Closed);
                break;
            }
            Err(error) => {
                fail_pending(&inner, error);
                break;
            }
        }
    }
}

async fn handle_inbound_frame(frame: DcpFrame, inner: &Arc<ClientInner>) {
    match frame.frame {
        Some(dcp_frame::Frame::Response(response)) => {
            if let Some(sender) = inner
                .pending
                .lock()
                .expect("DCP pending map poisoned")
                .remove(&response.request_id)
            {
                let _ = sender.send(Ok(response));
            }
        }
        Some(dcp_frame::Frame::Event(event_frame)) => {
            if let Some(event) = event_frame.event {
                let sender = inner
                    .event_subscriptions
                    .lock()
                    .expect("DCP event subscriptions poisoned")
                    .get(&event_frame.subscription_id)
                    .cloned();
                if let Some(sender) = sender {
                    let _ = sender.send(event).await;
                }
            }
        }
        Some(dcp_frame::Frame::Metric(MetricFrame {
            subscription_id,
            sample: Some(sample),
        })) => {
            let sender = inner
                .metric_subscriptions
                .lock()
                .expect("DCP metric subscriptions poisoned")
                .get(&subscription_id)
                .cloned();
            if let Some(sender) = sender {
                let _ = sender.send(sample).await;
            }
        }
        _ => {}
    }
}

fn fail_pending(inner: &ClientInner, error: DcpError) {
    let pending = std::mem::take(&mut *inner.pending.lock().expect("DCP pending map poisoned"));
    let message = error.to_string();
    for (_, sender) in pending {
        let _ = sender.send(Err(DcpError::Protocol(message.clone())));
    }
}

async fn write_loop<W>(mut writer: W, mut outbound: mpsc::Receiver<DcpFrame>) -> DcpResult<()>
where
    W: AsyncWrite + Unpin,
{
    while let Some(frame) = outbound.recv().await {
        write_frame(&mut writer, &frame).await?;
    }
    let _ = writer.shutdown().await;
    Ok(())
}

fn ensure_ok(response: Response) -> DcpResult<Response> {
    let status = response.response_status();
    if status == ResponseStatus::Ok {
        Ok(response)
    } else {
        Err(DcpError::response(status, response.message))
    }
}

impl Default for DcpClient {
    fn default() -> Self {
        panic!("DcpClient cannot be default-constructed; use connect_tcp or connect_quic")
    }
}

#[must_use]
pub fn default_hello(node_id: impl Into<String>, client_kind: ClientKind) -> Hello {
    Hello::new(node_id, client_kind)
}