durable-actors 0.18.2

Standalone regional durable-actors control plane, host, and durability runtime
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
//! The connection an executor makes its database calls on, apart from its commands so that the
//! actor's thread can block on it.
//!
//! One call is answered at a time. A request is a `u32` size, that much JSON, then the values
//! the JSON counts under `bindings`: what a statement binds, or the JSON of each field the call
//! writes. The answer is a stream of records: `COLUMNS` and a `ROW` per row while a statement or
//! a field read runs, then `RESULT` or `FAILED`. Integers are little-endian, and a value is a
//! tag followed by its eight bytes, or by a `u32` size and that many bytes.

use std::{
    collections::{BTreeMap, HashMap},
    sync::{Arc, Mutex},
};

use anyhow::{Context, Result, ensure};
use serde::Deserialize;
use serde_json::value::RawValue;
use tokio::{
    io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader},
    net::{
        UnixStream,
        unix::{OwnedReadHalf, OwnedWriteHalf},
    },
    sync::mpsc,
};

use super::{ActorDatabase, DatabaseCall, DatabaseResult, RowSink, SqlCell, SqlValue};

const NULL: u8 = 0;
const INTEGER: u8 = 1;
const REAL: u8 = 2;
const TEXT: u8 = 3;
const BLOB: u8 = 4;

const COLUMNS: u8 = 0x10;
const ROW: u8 = 0x11;
const RESULT: u8 = 0x12;
const FAILED: u8 = 0x13;

const CHUNK_BYTES: usize = 64 * 1024;
const CHUNKS_IN_FLIGHT: usize = 4;
/// SQLite's own limit on the values one statement binds.
const MAX_BINDINGS: usize = 32_766;

/// The database each running command reaches, and the activation's for calls that name none.
#[derive(Default)]
pub(super) struct DatabaseScopes(Mutex<Scopes>);

#[derive(Default)]
struct Scopes {
    commands: HashMap<u64, Arc<dyn ActorDatabase>>,
    activation: Option<Arc<dyn ActorDatabase>>,
}

impl DatabaseScopes {
    pub(super) fn activate(&self, database: Option<Arc<dyn ActorDatabase>>) {
        self.0.lock().unwrap().activation = database;
    }

    pub(super) fn open(&self, message_id: u64, database: Arc<dyn ActorDatabase>) {
        self.0.lock().unwrap().commands.insert(message_id, database);
    }

    pub(super) fn close(&self, message_id: u64) {
        self.0.lock().unwrap().commands.remove(&message_id);
    }

    /// A call that names a command is refused once that command has been answered: it may come
    /// from an executor that was stopped while the command ran.
    fn resolve(&self, message_id: Option<u64>) -> Result<Arc<dyn ActorDatabase>> {
        let scopes = self.0.lock().unwrap();
        match message_id {
            Some(id) => scopes
                .commands
                .get(&id)
                .cloned()
                .context("the command this database call belongs to has ended"),
            None => scopes
                .activation
                .clone()
                .context("actor database is unavailable"),
        }
    }
}

pub(super) async fn serve(stream: UnixStream, scopes: Arc<DatabaseScopes>) -> Result<()> {
    let (reader, mut writer) = stream.into_split();
    let mut reader = BufReader::with_capacity(CHUNK_BYTES, reader);
    while let Some(request) = read_request(&mut reader).await? {
        answer(&mut writer, request, &scopes).await?;
    }
    Ok(())
}

struct Request {
    message_id: Option<u64>,
    call: DatabaseCall,
}

#[derive(Deserialize)]
struct RequestHead {
    #[serde(default)]
    message_id: Option<u64>,
    call: CallHead,
    #[serde(default)]
    bindings: usize,
}

/// A call without the values it carries. They follow the head, so a large one is never escaped
/// into the head's JSON or parsed out of it.
#[derive(Deserialize)]
#[serde(tag = "op", rename_all = "snake_case")]
enum CallHead {
    Exec {
        sql: String,
        #[serde(default)]
        immediate: bool,
    },
    Fields {
        #[serde(default)]
        names: Option<Vec<String>>,
    },
    InitializeFields {
        names: Vec<String>,
    },
    PersistField {
        name: String,
        #[serde(default)]
        immediate: bool,
    },
    DeleteField {
        name: String,
        #[serde(default)]
        immediate: bool,
    },
    Alarm,
    SetAlarm {
        timestamp_ms: i64,
        #[serde(default)]
        immediate: bool,
    },
    DeleteAlarm {
        #[serde(default)]
        immediate: bool,
    },
    Commit,
    Rollback,
}

impl CallHead {
    /// How many values follow the head. Only a statement leaves that to the request.
    fn values(&self) -> Option<usize> {
        match self {
            Self::Exec { .. } => None,
            Self::InitializeFields { names } => Some(names.len()),
            Self::PersistField { .. } => Some(1),
            _ => Some(0),
        }
    }
}

type Reader = BufReader<OwnedReadHalf>;

async fn read_request(reader: &mut Reader) -> Result<Option<Request>> {
    if reader.fill_buf().await?.is_empty() {
        return Ok(None);
    }
    let head: RequestHead =
        serde_json::from_slice(&read_sized(reader).await?).context("decode database call")?;
    ensure!(
        head.bindings <= MAX_BINDINGS,
        "database call binds too many values"
    );
    ensure!(
        head.call
            .values()
            .is_none_or(|values| values == head.bindings),
        "database call carries the wrong number of values"
    );
    Ok(Some(Request {
        message_id: head.message_id,
        call: read_call(reader, head.call, head.bindings).await?,
    }))
}

async fn read_call(reader: &mut Reader, head: CallHead, bindings: usize) -> Result<DatabaseCall> {
    Ok(match head {
        CallHead::Exec { sql, immediate } => DatabaseCall::Exec {
            sql,
            bindings: read_bindings(reader, bindings).await?,
            immediate,
        },
        CallHead::Fields { names } => DatabaseCall::Fields { names },
        CallHead::InitializeFields { names } => DatabaseCall::InitializeFields {
            fields: read_fields(reader, names).await?,
        },
        CallHead::PersistField { name, immediate } => DatabaseCall::PersistField {
            name,
            value: read_json(reader).await?,
            immediate,
        },
        CallHead::DeleteField { name, immediate } => DatabaseCall::DeleteField { name, immediate },
        CallHead::Alarm => DatabaseCall::Alarm,
        CallHead::SetAlarm {
            timestamp_ms,
            immediate,
        } => DatabaseCall::SetAlarm {
            timestamp_ms,
            immediate,
        },
        CallHead::DeleteAlarm { immediate } => DatabaseCall::DeleteAlarm { immediate },
        CallHead::Commit => DatabaseCall::Commit,
        CallHead::Rollback => DatabaseCall::Rollback,
    })
}

async fn read_bindings(reader: &mut Reader, count: usize) -> Result<Vec<SqlValue>> {
    let mut bindings = Vec::with_capacity(count);
    for _ in 0..count {
        bindings.push(read_value(reader).await?);
    }
    Ok(bindings)
}

async fn read_fields(
    reader: &mut Reader,
    names: Vec<String>,
) -> Result<BTreeMap<String, Box<RawValue>>> {
    let mut fields = BTreeMap::new();
    for name in names {
        fields.insert(name, read_json(reader).await?);
    }
    Ok(fields)
}

/// A field's JSON stays the text the executor wrote: it is checked, and never read into a tree.
async fn read_json(reader: &mut Reader) -> Result<Box<RawValue>> {
    ensure!(
        reader.read_u8().await? == TEXT,
        "a field's value is sent as text"
    );
    let text = String::from_utf8(read_sized(reader).await?).context("field value is not UTF-8")?;
    RawValue::from_string(text).context("field value is not JSON")
}

async fn read_value(reader: &mut Reader) -> Result<SqlValue> {
    Ok(match reader.read_u8().await? {
        NULL => SqlValue::Null,
        INTEGER => SqlValue::Integer(reader.read_i64_le().await?),
        REAL => SqlValue::Real(reader.read_f64_le().await?),
        TEXT => SqlValue::Text(String::from_utf8(read_sized(reader).await?)?),
        BLOB => SqlValue::Blob(read_sized(reader).await?),
        tag => anyhow::bail!("unknown database value type {tag}"),
    })
}

// The executor chooses the size, so memory is claimed only as its bytes arrive.
async fn read_sized(reader: &mut Reader) -> Result<Vec<u8>> {
    let size = reader.read_u32_le().await? as usize;
    let mut bytes = Vec::new();
    bytes
        .try_reserve_exact(size)
        .context("database call is too large")?;
    reader.take(size as u64).read_to_end(&mut bytes).await?;
    ensure!(
        bytes.len() == size,
        "executor disconnected inside a database call"
    );
    Ok(bytes)
}

async fn answer(
    writer: &mut OwnedWriteHalf,
    request: Request,
    scopes: &DatabaseScopes,
) -> Result<()> {
    let (rows, mut chunks) = mpsc::channel(CHUNKS_IN_FLIGHT);
    let (result, forwarded) = tokio::join!(
        call(request, scopes, RowStream::new(rows)),
        forward(&mut chunks, writer)
    );
    forwarded?;
    writer.write_all(&ending(result)?).await?;
    Ok(())
}

async fn call(
    request: Request,
    scopes: &DatabaseScopes,
    rows: RowStream,
) -> Result<DatabaseResult> {
    scopes
        .resolve(request.message_id)?
        .call(request.call, Box::new(rows))
        .await
}

/// Ends once the call has dropped its sink. Closing after a failed write releases a statement
/// that is waiting to send.
async fn forward(chunks: &mut mpsc::Receiver<Vec<u8>>, writer: &mut OwnedWriteHalf) -> Result<()> {
    while let Some(chunk) = chunks.recv().await {
        if let Err(error) = writer.write_all(&chunk).await {
            chunks.close();
            return Err(error.into());
        }
    }
    Ok(())
}

fn ending(result: Result<DatabaseResult>) -> Result<Vec<u8>> {
    let (kind, body) = match result {
        Ok(result) => (RESULT, serde_json::to_vec(&result)?),
        Err(error) => (FAILED, format!("{error:#}").into_bytes()),
    };
    let mut record = vec![kind];
    record.extend(u32::try_from(body.len())?.to_le_bytes());
    record.extend(body);
    Ok(record)
}

/// Sends rows in chunks of a fixed size, however large a row or a value is, and waits when the
/// executor reads slower than SQLite produces.
struct RowStream {
    chunks: mpsc::Sender<Vec<u8>>,
    chunk: Vec<u8>,
}

impl RowStream {
    fn new(chunks: mpsc::Sender<Vec<u8>>) -> Self {
        Self {
            chunks,
            chunk: Vec::new(),
        }
    }

    fn value(&mut self, cell: &SqlCell<'_>) -> Result<()> {
        match *cell {
            SqlCell::Null => self.put(&[NULL]),
            SqlCell::Integer(value) => {
                self.put(&[INTEGER])?;
                self.put(&value.to_le_bytes())
            }
            SqlCell::Real(value) => {
                self.put(&[REAL])?;
                self.put(&value.to_le_bytes())
            }
            SqlCell::Text(value) => {
                self.put(&[TEXT])?;
                self.sized(value)
            }
            SqlCell::Blob(value) => {
                self.put(&[BLOB])?;
                self.sized(value)
            }
        }
    }

    fn sized(&mut self, bytes: &[u8]) -> Result<()> {
        self.put(&u32::try_from(bytes.len())?.to_le_bytes())?;
        self.put(bytes)
    }

    fn put(&mut self, mut bytes: &[u8]) -> Result<()> {
        while !bytes.is_empty() {
            let room = CHUNK_BYTES - self.chunk.len();
            let (fitting, rest) = bytes.split_at(bytes.len().min(room));
            self.chunk.extend_from_slice(fitting);
            bytes = rest;
            if self.chunk.len() == CHUNK_BYTES {
                self.send()?;
            }
        }
        Ok(())
    }

    fn send(&mut self) -> Result<()> {
        if self.chunk.is_empty() {
            return Ok(());
        }
        let chunk = std::mem::replace(&mut self.chunk, Vec::with_capacity(CHUNK_BYTES));
        self.chunks
            .blocking_send(chunk)
            .map_err(|_| anyhow::anyhow!("executor stopped reading its database result"))
    }
}

impl RowSink for RowStream {
    fn columns(&mut self, names: &[&str]) -> Result<()> {
        self.put(&[COLUMNS])?;
        self.put(&u32::try_from(names.len())?.to_le_bytes())?;
        names
            .iter()
            .try_for_each(|name| self.sized(name.as_bytes()))
    }

    fn row(&mut self, cells: &[SqlCell<'_>]) -> Result<()> {
        self.put(&[ROW])?;
        cells.iter().try_for_each(|cell| self.value(cell))
    }

    fn finish(&mut self) -> Result<()> {
        self.send()
    }
}

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