durable-actors 0.5.4

Standalone regional durable-actors control plane, host, and durability runtime
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
use crate::control_plane::admin::validate_component;
use anyhow::{Context, Result, ensure};
use serde::{Deserialize, Serialize};
use std::{
    sync::{
        Arc,
        atomic::{AtomicBool, AtomicU64, Ordering},
    },
    time::{Duration, Instant, SystemTime, UNIX_EPOCH},
};
use tokio::sync::{mpsc, watch};

pub(crate) mod history;
pub(crate) mod metrics;
pub(crate) mod persistence;
pub(crate) mod replay;
use persistence::{TracePersistence, sqlite::SqliteTracePersistence};
use replay::ReplayQuery;

pub(crate) const TRACE_CAPACITY: usize = 500;
pub(crate) const TRACE_BATCH_SIZE: usize = 64;
pub(crate) const TRACE_METADATA_LIMIT: usize = 4096;

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct RequestTrace {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub state_version: Option<u64>,
    pub project_id: String,
    pub request_id: String,
    pub actor_name: String,
    pub actor_id: String,
    pub kind: RequestKind,
    pub operation: String,
    pub connection_id: Option<String>,
    pub started_at_ms: u64,
    pub duration_ms: f64,
    pub queue_wait_ms: Option<f64>,
    pub outcome: RequestOutcome,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Value>,
}

impl RequestTrace {
    pub(crate) fn validate(&self) -> Result<()> {
        crate::actor::ActorKey {
            project_id: self.project_id.clone(),
            actor_name: self.actor_name.clone(),
            actor_id: self.actor_id.clone(),
        }
        .validate()?;
        ensure!(
            !self.request_id.is_empty() && self.request_id.len() <= 256,
            "invalid trace request ID"
        );
        ensure!(
            !self.operation.is_empty() && self.operation.len() <= 256,
            "invalid trace operation"
        );
        ensure!(
            self.connection_id.as_ref().is_none_or(|id| id.len() <= 256),
            "invalid trace connection ID"
        );
        ensure!(
            self.duration_ms.is_finite() && self.duration_ms >= 0.0,
            "invalid trace duration"
        );
        ensure!(
            self.queue_wait_ms
                .is_none_or(|ms| ms.is_finite() && ms >= 0.0 && ms <= self.duration_ms),
            "invalid trace queue wait"
        );
        ensure!(
            self.metadata
                .as_ref()
                .is_none_or(|metadata| metadata.to_string().len() <= TRACE_METADATA_LIMIT),
            "trace metadata exceeds {TRACE_METADATA_LIMIT} bytes"
        );
        Ok(())
    }
}

// Connection metadata beyond the limit is omitted rather than failing the trace.
fn bounded_metadata(metadata: Option<serde_json::Value>) -> Option<serde_json::Value> {
    metadata.filter(|value| value.to_string().len() <= TRACE_METADATA_LIMIT)
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum RequestKind {
    Method,
    Websocket,
}

impl RequestKind {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::Method => "method",
            Self::Websocket => "websocket",
        }
    }
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum RequestOutcome {
    Completed,
    Failed,
    Rejected,
    Rerouted,
    Interrupted,
}

impl RequestOutcome {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::Completed => "completed",
            Self::Failed => "failed",
            Self::Rejected => "rejected",
            Self::Rerouted => "rerouted",
            Self::Interrupted => "interrupted",
        }
    }
}

#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct TraceEvent {
    #[serde(default = "new_event_id")]
    pub event_id: String,
    pub host_id: String,
    pub session_id: String,
    #[serde(flatten)]
    pub trace: RequestTrace,
}

fn new_event_id() -> String {
    uuid::Uuid::new_v4().to_string()
}

#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct TraceRecord {
    pub sequence: u64,
    #[serde(flatten)]
    pub event: TraceEvent,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct TracePage {
    pub epoch: String,
    pub cursor: u64,
    pub capacity: usize,
    pub evicted: u64,
    pub dropped: u64,
    pub persistence_failed: bool,
    pub records: Vec<TraceRecord>,
    pub next_cursor: Option<String>,
    pub resume_cursor: String,
    pub reset: bool,
}

#[derive(Debug)]
pub(crate) struct InvalidTraceQuery;

impl std::fmt::Display for InvalidTraceQuery {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str("Invalid or incompatible request trace query")
    }
}
impl std::error::Error for InvalidTraceQuery {}

#[derive(Clone)]
pub(crate) struct TraceStore {
    writer: Arc<tokio::sync::Mutex<()>>,
    pending: Arc<tokio::sync::Semaphore>,
    persistence: Arc<dyn TracePersistence>,
    dropped: Arc<std::sync::Mutex<std::collections::HashMap<String, u64>>>,
    persistence_failed: Arc<AtomicBool>,
    pub changes: watch::Sender<()>,
}

impl Default for TraceStore {
    fn default() -> Self {
        Self::new(Arc::new(SqliteTracePersistence::in_memory()))
    }
}

impl TraceStore {
    pub(crate) async fn open(persistence: Arc<dyn TracePersistence>) -> Result<Self> {
        persistence.initialize().await?;
        Ok(Self::new(persistence))
    }

    pub(crate) async fn record(
        &self,
        project_id: &str,
        host: &str,
        session: &str,
        traces: Vec<RequestTrace>,
        dropped: u64,
    ) -> Result<()> {
        validate_component("project ID", project_id, 64)?;
        ensure!(
            traces.iter().all(|trace| trace.project_id == project_id),
            "trace project does not match host"
        );
        if dropped > 0 {
            let mut counts = self.dropped.lock().unwrap();
            let count = counts.entry(project_id.to_owned()).or_default();
            *count = count.saturating_add(dropped);
        }
        let events = traces
            .into_iter()
            .map(|trace| TraceEvent {
                event_id: new_event_id(),
                host_id: host.into(),
                session_id: session.into(),
                trace,
            })
            .collect();
        self.persist_detached(events).await
    }

    pub(crate) async fn replay(&self, project_id: &str, query: &ReplayQuery) -> Result<TracePage> {
        validate_component("project ID", project_id, 64).context(InvalidTraceQuery)?;
        query.validate().context(InvalidTraceQuery)?;
        let mut page = self.persistence.replay(project_id, query).await?;
        page.dropped = self
            .dropped
            .lock()
            .unwrap()
            .get(project_id)
            .copied()
            .unwrap_or(0);
        page.persistence_failed = self.persistence_failed.load(Ordering::Relaxed);
        Ok(page)
    }

    pub(crate) async fn metrics(
        &self,
        project_id: &str,
        query: &metrics::TimeRange,
    ) -> Result<metrics::OverviewMetrics> {
        validate_component("project ID", project_id, 64).context(InvalidTraceQuery)?;
        query.validate().context(InvalidTraceQuery)?;
        self.persistence.metrics(project_id, query).await
    }

    pub(crate) async fn queue_waits(
        &self,
        project_id: &str,
        query: &metrics::QueueWaitQuery,
    ) -> Result<Vec<metrics::QueueWaitRow>> {
        validate_component("project ID", project_id, 64).context(InvalidTraceQuery)?;
        query.validate().context(InvalidTraceQuery)?;
        self.persistence.queue_waits(project_id, query).await
    }

    pub(crate) async fn websockets(
        &self,
        project_id: &str,
        query: &metrics::TimeRange,
    ) -> Result<Vec<metrics::SocketSession>> {
        validate_component("project ID", project_id, 64).context(InvalidTraceQuery)?;
        query.validate().context(InvalidTraceQuery)?;
        self.persistence.websockets(project_id, query).await
    }

    pub(crate) async fn history(
        &self,
        project_id: &str,
        query: &history::HistoryQuery,
    ) -> Result<TracePage> {
        validate_component("project ID", project_id, 64).context(InvalidTraceQuery)?;
        query.validate().context(InvalidTraceQuery)?;
        let mut page = self.persistence.history(project_id, query).await?;
        page.dropped = self
            .dropped
            .lock()
            .unwrap()
            .get(project_id)
            .copied()
            .unwrap_or(0);
        page.persistence_failed = self.persistence_failed.load(Ordering::Relaxed);
        Ok(page)
    }

    fn new(persistence: Arc<dyn TracePersistence>) -> Self {
        Self {
            writer: Arc::new(tokio::sync::Mutex::new(())),
            pending: Arc::new(tokio::sync::Semaphore::new(64)),
            persistence,
            dropped: Arc::new(std::sync::Mutex::new(std::collections::HashMap::new())),
            persistence_failed: Arc::new(AtomicBool::new(false)),
            changes: watch::channel(()).0,
        }
    }

    async fn persist_detached(&self, events: Vec<TraceEvent>) -> Result<()> {
        if events.is_empty() {
            self.changes.send_replace(());
            return Ok(());
        }
        let permit = match self.pending.clone().try_acquire_owned() {
            Ok(permit) => permit,
            Err(error) => {
                self.report_persistence_failure(error, events.len());
                return Ok(());
            }
        };
        let store = self.clone();
        // Finish the commit even if the reporting connection is cancelled.
        tokio::spawn(async move {
            let _permit = permit;
            let _writer = store.writer.lock().await;
            match store.persistence.append(&events).await {
                Ok(()) => {
                    store.changes.send_replace(());
                }
                Err(error) => store.report_persistence_failure(error, events.len()),
            }
        })
        .await?;
        Ok(())
    }

    fn report_persistence_failure(&self, error: impl std::fmt::Display, count: usize) {
        tracing::error!(%error, count, "request trace persistence failed");
        self.persistence_failed.store(true, Ordering::Relaxed);
        self.changes.send_replace(());
    }
}

#[derive(Clone)]
pub(crate) struct TraceSender {
    sender: mpsc::Sender<RequestTrace>,
    dropped: Arc<AtomicU64>,
}

impl TraceSender {
    #[cfg(test)]
    pub(crate) fn channel(capacity: usize) -> (Self, mpsc::Receiver<RequestTrace>) {
        let (sender, receiver) = mpsc::channel(capacity);
        (
            Self {
                sender,
                dropped: Arc::new(AtomicU64::new(0)),
            },
            receiver,
        )
    }
    pub(crate) fn start(
        client: Arc<crate::control_plane::ControlPlaneClient>,
        stop: tokio_util::sync::CancellationToken,
    ) -> Self {
        let (sender, mut receiver) = mpsc::channel(1024);
        let dropped = Arc::new(AtomicU64::new(0));
        let lost = dropped.clone();
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(Duration::from_millis(200));
            loop {
                tokio::select! { _ = stop.cancelled() => break, _ = interval.tick() => {} }
                let mut batch = Vec::new();
                while batch.len() < TRACE_BATCH_SIZE {
                    match receiver.try_recv() {
                        Ok(trace) => batch.push(trace),
                        Err(_) => break,
                    }
                }
                let missing = lost.swap(0, Ordering::Relaxed);
                if batch.is_empty() && missing == 0 {
                    continue;
                }
                let count = batch.len() as u64;
                if !matches!(
                    tokio::time::timeout(
                        Duration::from_secs(3),
                        client.report_traces(batch, missing)
                    )
                    .await,
                    Ok(Ok(()))
                ) {
                    lost.fetch_add(count.saturating_add(missing), Ordering::Relaxed);
                }
            }
        });
        Self { sender, dropped }
    }

    fn send(&self, trace: RequestTrace) {
        if self.sender.try_send(trace).is_err() {
            self.dropped.fetch_add(1, Ordering::Relaxed);
        }
    }
}

pub(crate) struct RequestSpan {
    sender: TraceSender,
    started: Instant,
    trace: Option<RequestTrace>,
}

impl RequestSpan {
    pub(crate) fn new(
        sender: TraceSender,
        invocation: &crate::actor::ActorInvocation,
        kind: RequestKind,
        connection_id: Option<String>,
        metadata: Option<serde_json::Value>,
        started: Instant,
    ) -> Self {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis() as u64;
        Self {
            sender,
            started,
            trace: Some(RequestTrace {
                state_version: None,
                project_id: invocation.actor.project_id.clone(),
                request_id: invocation.request_id.clone(),
                actor_name: invocation.actor.actor_name.clone(),
                actor_id: invocation.actor.actor_id.clone(),
                kind,
                operation: invocation.method.clone(),
                connection_id,
                started_at_ms: now.saturating_sub(started.elapsed().as_millis() as u64),
                duration_ms: 0.0,
                queue_wait_ms: None,
                outcome: RequestOutcome::Interrupted,
                metadata: bounded_metadata(metadata),
            }),
        }
    }

    pub(crate) fn state_version(&mut self, version: Option<u64>) {
        if let Some(trace) = &mut self.trace {
            trace.state_version = version;
        }
    }

    pub(crate) fn admitted(&mut self) {
        self.trace.as_mut().unwrap().queue_wait_ms =
            Some(self.started.elapsed().as_secs_f64() * 1000.0);
    }

    pub(crate) fn finish(&mut self, outcome: RequestOutcome) {
        if let Some(mut trace) = self.trace.take() {
            trace.outcome = outcome;
            trace.duration_ms = self.started.elapsed().as_secs_f64() * 1000.0;
            self.sender.send(trace);
        }
    }

    pub(crate) fn complete(&mut self, result: &Result<crate::actor::ActorExecutionResult>) {
        use crate::actor::ActorExecutionResult as R;
        self.finish(match result {
            Ok(R::Completed { .. }) => RequestOutcome::Completed,
            Ok(R::HostUnavailable) => RequestOutcome::Rejected,
            Ok(R::Reroute) => RequestOutcome::Rerouted,
            _ => RequestOutcome::Failed,
        });
    }
}

impl Drop for RequestSpan {
    fn drop(&mut self) {
        self.finish(RequestOutcome::Interrupted);
    }
}

#[cfg(test)]
#[path = "../tests/unit/request_traces/tests.rs"]
mod tests;