feltdb 0.5.0

Embedded durable state store: an append-only log with atomic multi-record transactions, version-conditional writes, and idempotent retries
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
478
479
//! One logical durable authority across embedded and local multi-process use.
//!
//! The authority server is the sole process which opens the storage log. IPC
//! clients never mutate storage artifacts and every authoritative read crosses
//! the socket boundary.

use crate::{
    AtomicCommit, AtomicMutation, AtomicPrecondition, FeltDb, PayloadBinding, RecordPrecondition,
    StoredRow,
};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};
use std::{
    io,
    path::{Path, PathBuf},
};
use tokio::{
    io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
    net::{UnixListener, UnixStream},
};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ReadKind {
    Snapshot,
    Authoritative,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthorityRead {
    pub observed_revision: u64,
    pub authoritative_revision: u64,
    pub kind: ReadKind,
    pub content_hash: String,
    pub rows: Vec<StoredRow>,
}

impl AuthorityRead {
    fn current(db: &FeltDb, kind: ReadKind) -> crate::Result<Self> {
        let (revision, mut rows) = db.authority_state();
        rows.sort_by(|left, right| {
            (&left.capability, &left.key).cmp(&(&right.capability, &right.key))
        });
        let mut hasher = Sha256::new();
        hasher.update(revision.to_le_bytes());
        hasher.update(serde_json::to_vec(&rows)?);
        Ok(Self {
            observed_revision: revision,
            authoritative_revision: revision,
            kind,
            content_hash: format!("sha256:{:x}", hasher.finalize()),
            rows,
        })
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthorityTransaction {
    pub transaction_id: String,
    /// Explicit optimistic fence. `None` asks the authority to begin and
    /// commit an unconditional transaction at its current revision.
    #[serde(default)]
    pub base_revision: Option<u64>,
    #[serde(default)]
    pub preconditions: Vec<AtomicPrecondition>,
    #[serde(default)]
    pub record_preconditions: Vec<RecordPrecondition>,
    pub mutations: Vec<AtomicMutation>,
    #[serde(default)]
    pub audit: Option<Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthorityError {
    pub code: String,
    pub message: String,
    #[serde(default)]
    pub authoritative_revision: Option<u64>,
}

impl std::fmt::Display for AuthorityError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(formatter, "{}: {}", self.code, self.message)
    }
}

impl std::error::Error for AuthorityError {}

#[async_trait]
pub trait FeltDbAuthority: Send + Sync {
    async fn read_authoritative(&self) -> Result<AuthorityRead, AuthorityError>;
    async fn current_revision(&self) -> Result<u64, AuthorityError>;
    async fn commit(
        &self,
        transaction: AuthorityTransaction,
    ) -> Result<AtomicCommit, AuthorityError>;
}

#[derive(Clone)]
pub struct EmbeddedAuthority {
    db: FeltDb,
}

impl EmbeddedAuthority {
    pub fn open(path: impl AsRef<Path>) -> crate::Result<Self> {
        Ok(Self {
            db: FeltDb::open(path)?,
        })
    }

    pub fn from_db(db: FeltDb) -> Self {
        Self { db }
    }

    /// An explicitly historical value. It never claims freshness beyond its
    /// captured `observed_revision`.
    pub fn snapshot(&self) -> crate::Result<AuthorityRead> {
        AuthorityRead::current(&self.db, ReadKind::Snapshot)
    }
}

/// Names the construction of the payload digest the authority binds ids to.
///
/// The prefix is stored with the digest in the durable transaction record, so a
/// digest written by a different construction is recognizable as one this
/// build cannot judge, instead of being read as a mismatch.
const PAYLOAD_DIGEST_PREFIX: &str = "authority-tx1:sha256:";

/// Canonical text of a JSON value: object members sorted by name at every
/// depth, array order preserved. Written out here rather than left to
/// `serde_json`'s map ordering, which is a build feature that another crate in
/// the dependency graph can change.
fn canonical_json(value: &Value, out: &mut String) {
    match value {
        Value::Object(map) => {
            let mut keys: Vec<&String> = map.keys().collect();
            keys.sort();
            out.push('{');
            for (index, key) in keys.into_iter().enumerate() {
                if index > 0 {
                    out.push(',');
                }
                out.push_str(&Value::String(key.clone()).to_string());
                out.push(':');
                canonical_json(&map[key], out);
            }
            out.push('}');
        }
        Value::Array(items) => {
            out.push('[');
            for (index, item) in items.iter().enumerate() {
                if index > 0 {
                    out.push(',');
                }
                canonical_json(item, out);
            }
            out.push(']');
        }
        other => out.push_str(&other.to_string()),
    }
}

/// The digest an id is bound to: everything the commit acts on or records, and
/// not the id itself. `audit` is included because it is committed evidence the
/// caller supplied, not transport metadata; two requests that record different
/// audit facts under one id are different requests.
fn payload_digest(transaction: &AuthorityTransaction) -> Result<String, AuthorityError> {
    let semantic = serde_json::json!({
        "base_revision": transaction.base_revision,
        "preconditions": transaction.preconditions,
        "record_preconditions": transaction.record_preconditions,
        "mutations": transaction.mutations,
        "audit": transaction.audit,
    });
    let mut canonical = String::new();
    canonical_json(&semantic, &mut canonical);
    Ok(format!(
        "{PAYLOAD_DIGEST_PREFIX}{:x}",
        Sha256::digest(canonical.as_bytes())
    ))
}

fn authority_error(error: crate::FlowError) -> AuthorityError {
    match error {
        crate::FlowError::TransactionIdPayloadMismatch { transaction_id: id } => {
            AuthorityError {
                code: "IDEMPOTENCY_CONFLICT".into(),
                message: format!(
                    "transaction {id} was already committed with a different payload"
                ),
                authoritative_revision: None,
            }
        }
        crate::FlowError::RevisionConflict {
            expected: _,
            actual,
        } => AuthorityError {
            code: "REVISION_CONFLICT".into(),
            message: error.to_string(),
            authoritative_revision: Some(actual),
        },
        other => AuthorityError {
            code: "AUTHORITY_ERROR".into(),
            message: other.to_string(),
            authoritative_revision: None,
        },
    }
}

#[async_trait]
impl FeltDbAuthority for EmbeddedAuthority {
    async fn read_authoritative(&self) -> Result<AuthorityRead, AuthorityError> {
        AuthorityRead::current(&self.db, ReadKind::Authoritative).map_err(authority_error)
    }

    async fn current_revision(&self) -> Result<u64, AuthorityError> {
        self.db.current_revision().map_err(authority_error)
    }

    async fn commit(
        &self,
        transaction: AuthorityTransaction,
    ) -> Result<AtomicCommit, AuthorityError> {
        let digest = payload_digest(&transaction)?;
        // How the id is already known, if it is. A recorded hash never changes
        // and a missing one never appears, so this read cannot be invalidated
        // by a concurrent commit: the comparison that matters -- the id against
        // the digest of this request -- is made by the core, atomically with
        // the commit, under the lock that serializes every writer.
        let id = transaction.transaction_id.as_str();
        let applied = self.db.has_applied_transaction(id).map_err(authority_error)?;
        let recorded = if applied {
            self.db
                .applied_transaction_payload_hash(id)
                .map_err(authority_error)?
        } else {
            None
        };
        let (bound, legacy) = match (applied, recorded) {
            (false, _) => (Some(digest), false),
            // Committed before ids were bound: the id is all there is to compare.
            (true, None) => (None, true),
            (true, Some(hash)) if hash.starts_with(PAYLOAD_DIGEST_PREFIX) => (Some(digest), false),
            // Recorded by a construction this build does not know, or by another
            // writer's: neither a match nor a mismatch can be established.
            (true, Some(_)) => {
                return Err(AuthorityError {
                    code: "IDEMPOTENCY_UNVERIFIABLE".into(),
                    message: format!(
                        "transaction {id} exists, but its recorded payload identity cannot be verified by this build"
                    ),
                    authoritative_revision: None,
                })
            }
        };
        let mut commit = self
            .db
            .apply_atomic_transaction_guarded(
                id,
                bound.as_deref(),
                transaction.base_revision,
                &transaction.preconditions,
                &transaction.record_preconditions,
                &transaction.mutations,
                transaction.audit.clone(),
            )
            .map_err(authority_error)?;
        if legacy && commit.duplicate {
            commit.payload_binding = Some(PayloadBinding::LegacyUnverified);
        }
        Ok(commit)
    }
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "operation", rename_all = "snake_case")]
enum Request {
    Read,
    CurrentRevision,
    Commit { transaction: AuthorityTransaction },
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "result", content = "value", rename_all = "snake_case")]
enum Response {
    Read(AuthorityRead),
    Revision(u64),
    Commit(AtomicCommit),
    Error(AuthorityError),
}

pub struct AuthorityServer {
    socket_path: PathBuf,
    authority: EmbeddedAuthority,
}

impl AuthorityServer {
    pub fn open(
        socket_path: impl Into<PathBuf>,
        storage_path: impl AsRef<Path>,
    ) -> crate::Result<Self> {
        Ok(Self {
            socket_path: socket_path.into(),
            authority: EmbeddedAuthority::open(storage_path)?,
        })
    }

    pub async fn serve(self) -> io::Result<()> {
        if let Some(parent) = self.socket_path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        if self.socket_path.exists() {
            use std::os::unix::fs::FileTypeExt;
            if !std::fs::symlink_metadata(&self.socket_path)?
                .file_type()
                .is_socket()
            {
                return Err(io::Error::new(
                    io::ErrorKind::AlreadyExists,
                    "authority socket path is not a socket",
                ));
            }
            std::fs::remove_file(&self.socket_path)?;
        }
        let listener = UnixListener::bind(&self.socket_path)?;
        loop {
            let (stream, _) = listener.accept().await?;
            let authority = self.authority.clone();
            tokio::spawn(async move {
                let _ = handle_connection(stream, authority).await;
            });
        }
    }
}

async fn handle_connection(stream: UnixStream, authority: EmbeddedAuthority) -> io::Result<()> {
    let (reader, mut writer) = stream.into_split();
    let mut lines = BufReader::new(reader).lines();
    while let Some(line) = lines.next_line().await? {
        let response = match serde_json::from_str::<Request>(&line) {
            Ok(Request::Read) => authority.read_authoritative().await.map(Response::Read),
            Ok(Request::CurrentRevision) => {
                authority.current_revision().await.map(Response::Revision)
            }
            Ok(Request::Commit { transaction }) => {
                authority.commit(transaction).await.map(Response::Commit)
            }
            Err(error) => Err(AuthorityError {
                code: "INVALID_REQUEST".into(),
                message: error.to_string(),
                authoritative_revision: None,
            }),
        }
        .unwrap_or_else(Response::Error);
        writer
            .write_all(&serde_json::to_vec(&response).map_err(io::Error::other)?)
            .await?;
        writer.write_all(b"\n").await?;
    }
    Ok(())
}

#[derive(Clone)]
pub struct AuthorityClient {
    socket_path: PathBuf,
}

impl AuthorityClient {
    pub fn connect(socket_path: impl Into<PathBuf>) -> Self {
        Self {
            socket_path: socket_path.into(),
        }
    }

    async fn request(&self, request: Request) -> Result<Response, AuthorityError> {
        // Once a commit has been handed to the authority it may have been
        // applied whether or not an answer comes back, so every failure from
        // that point is an unknown outcome, not a failure.
        let is_commit = matches!(request, Request::Commit { .. });
        let unknown = |detail: String| AuthorityError {
            code: "COMMIT_OUTCOME_UNKNOWN".into(),
            message: format!(
                "the commit may or may not have been applied ({detail}); retry with the same transaction id and payload"
            ),
            authoritative_revision: None,
        };
        let mut stream = UnixStream::connect(&self.socket_path)
            .await
            .map_err(|error| AuthorityError {
                code: "AUTHORITY_UNAVAILABLE".into(),
                message: error.to_string(),
                authoritative_revision: None,
            })?;
        stream
            .write_all(
                &serde_json::to_vec(&request).map_err(|error| AuthorityError {
                    code: "INVALID_REQUEST".into(),
                    message: error.to_string(),
                    authoritative_revision: None,
                })?,
            )
            .await
            .map_err(|error| if is_commit { unknown(error.to_string()) } else { io_authority_error(error) })?;
        stream
            .write_all(b"\n")
            .await
            .map_err(|error| if is_commit { unknown(error.to_string()) } else { io_authority_error(error) })?;
        let mut response = String::new();
        BufReader::new(stream)
            .read_line(&mut response)
            .await
            .map_err(|error| if is_commit { unknown(error.to_string()) } else { io_authority_error(error) })?;
        match serde_json::from_str(&response).map_err(|error| {
            if is_commit {
                unknown(format!("unreadable response: {error}"))
            } else {
                AuthorityError {
                    code: "INVALID_RESPONSE".into(),
                    message: error.to_string(),
                    authoritative_revision: None,
                }
            }
        })? {
            Response::Error(error) => Err(error),
            response => Ok(response),
        }
    }
}

fn io_authority_error(error: io::Error) -> AuthorityError {
    AuthorityError {
        code: "AUTHORITY_UNAVAILABLE".into(),
        message: error.to_string(),
        authoritative_revision: None,
    }
}

#[async_trait]
impl FeltDbAuthority for AuthorityClient {
    async fn read_authoritative(&self) -> Result<AuthorityRead, AuthorityError> {
        match self.request(Request::Read).await? {
            Response::Read(read) => Ok(read),
            _ => Err(AuthorityError {
                code: "INVALID_RESPONSE".into(),
                message: "expected authority read".into(),
                authoritative_revision: None,
            }),
        }
    }

    async fn current_revision(&self) -> Result<u64, AuthorityError> {
        match self.request(Request::CurrentRevision).await? {
            Response::Revision(revision) => Ok(revision),
            _ => Err(AuthorityError {
                code: "INVALID_RESPONSE".into(),
                message: "expected authority revision".into(),
                authoritative_revision: None,
            }),
        }
    }

    async fn commit(
        &self,
        transaction: AuthorityTransaction,
    ) -> Result<AtomicCommit, AuthorityError> {
        match self.request(Request::Commit { transaction }).await? {
            Response::Commit(commit) => Ok(commit),
            _ => Err(AuthorityError {
                code: "COMMIT_OUTCOME_UNKNOWN".into(),
                message: "the authority answered a commit with something other than a commit; \
                          its outcome is unknown, retry with the same transaction id and payload"
                    .into(),
                authoritative_revision: None,
            }),
        }
    }
}