create-feltdb 0.8.1 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/package-versions.js +1 -1
- package/dist/server-source/crates/feltdb/src/application.rs +19 -0
- package/dist/server-source/crates/feltdb/src/authority.rs +349 -0
- package/dist/server-source/crates/feltdb/src/bin/feltdb-authority-client.rs +32 -0
- package/dist/server-source/crates/feltdb/src/bin/feltdb-authority.rs +19 -0
- package/dist/server-source/crates/feltdb/src/lib.rs +444 -189
- package/dist/server-source/crates/feltdb/src/state_contract.rs +244 -51
- package/dist/server-source/crates/feltdb/tests/authority_process.rs +297 -0
- package/dist/server-source/crates/feltdb-server/src/auth.rs +41 -11
- package/dist/server-source/crates/feltdb-server/src/key_management.rs +8 -3
- package/dist/server-source/crates/feltdb-server/src/main.rs +1164 -225
- package/dist/server-source/crates/feltdb-server/src/tenancy.rs +134 -0
- package/package.json +1 -1
package/dist/package-versions.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// One release train keeps generated applications installable. The repository
|
|
2
2
|
// validation script checks these values against every workspace manifest.
|
|
3
|
-
export const FELTDB_PACKAGE_VERSION = '0.8.
|
|
3
|
+
export const FELTDB_PACKAGE_VERSION = '0.8.2';
|
|
4
4
|
export const feltdbPackageRange = `^${FELTDB_PACKAGE_VERSION}`;
|
|
@@ -1193,6 +1193,25 @@ impl ApplicationStore {
|
|
|
1193
1193
|
.map_err(|e| e.to_string())?;
|
|
1194
1194
|
fs::rename(tmp, &self.path).map_err(|e| e.to_string())
|
|
1195
1195
|
}
|
|
1196
|
+
|
|
1197
|
+
/// Remove revision material for one application after its tenancy record
|
|
1198
|
+
/// has passed the caller's guarded destruction policy. The append-only
|
|
1199
|
+
/// state and global audit logs remain separate.
|
|
1200
|
+
pub fn purge_application(&self, application_id: &str) -> Result<(), String> {
|
|
1201
|
+
let mut r = self
|
|
1202
|
+
.records
|
|
1203
|
+
.write()
|
|
1204
|
+
.map_err(|_| "application store lock poisoned")?;
|
|
1205
|
+
r.drafts.retain(|value| value.application_id != application_id);
|
|
1206
|
+
r.revisions.retain(|value| value.application_id != application_id);
|
|
1207
|
+
r.promotions.retain(|value| value.application_id != application_id);
|
|
1208
|
+
r.previews.retain(|value| value.application_id != application_id);
|
|
1209
|
+
r.environment_pointers.remove(application_id);
|
|
1210
|
+
r.audit.retain(|value| value.application_id != application_id);
|
|
1211
|
+
r.untrusted_revisions.remove(application_id);
|
|
1212
|
+
r.recoveries.retain(|value| value.application_id != application_id);
|
|
1213
|
+
self.persist(&r)
|
|
1214
|
+
}
|
|
1196
1215
|
fn event(
|
|
1197
1216
|
r: &mut RevisionRecords,
|
|
1198
1217
|
event: &str,
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
//! One logical durable authority across embedded and local multi-process use.
|
|
2
|
+
//!
|
|
3
|
+
//! The authority server is the sole process which opens the storage log. IPC
|
|
4
|
+
//! clients never mutate storage artifacts and every authoritative read crosses
|
|
5
|
+
//! the socket boundary.
|
|
6
|
+
|
|
7
|
+
use crate::{
|
|
8
|
+
AtomicCommit, AtomicMutation, AtomicPrecondition, FeltDb, RecordPrecondition, StoredRow,
|
|
9
|
+
};
|
|
10
|
+
use async_trait::async_trait;
|
|
11
|
+
use serde::{Deserialize, Serialize};
|
|
12
|
+
use serde_json::Value;
|
|
13
|
+
use sha2::{Digest, Sha256};
|
|
14
|
+
use std::{
|
|
15
|
+
io,
|
|
16
|
+
path::{Path, PathBuf},
|
|
17
|
+
};
|
|
18
|
+
use tokio::{
|
|
19
|
+
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
|
|
20
|
+
net::{UnixListener, UnixStream},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
24
|
+
#[serde(rename_all = "snake_case")]
|
|
25
|
+
pub enum ReadKind {
|
|
26
|
+
Snapshot,
|
|
27
|
+
Authoritative,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
31
|
+
pub struct AuthorityRead {
|
|
32
|
+
pub observed_revision: u64,
|
|
33
|
+
pub authoritative_revision: u64,
|
|
34
|
+
pub kind: ReadKind,
|
|
35
|
+
pub content_hash: String,
|
|
36
|
+
pub rows: Vec<StoredRow>,
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
impl AuthorityRead {
|
|
40
|
+
fn current(db: &FeltDb, kind: ReadKind) -> crate::Result<Self> {
|
|
41
|
+
let (revision, mut rows) = db.authority_state();
|
|
42
|
+
rows.sort_by(|left, right| {
|
|
43
|
+
(&left.capability, &left.key).cmp(&(&right.capability, &right.key))
|
|
44
|
+
});
|
|
45
|
+
let mut hasher = Sha256::new();
|
|
46
|
+
hasher.update(revision.to_le_bytes());
|
|
47
|
+
hasher.update(serde_json::to_vec(&rows)?);
|
|
48
|
+
Ok(Self {
|
|
49
|
+
observed_revision: revision,
|
|
50
|
+
authoritative_revision: revision,
|
|
51
|
+
kind,
|
|
52
|
+
content_hash: format!("sha256:{:x}", hasher.finalize()),
|
|
53
|
+
rows,
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
59
|
+
pub struct AuthorityTransaction {
|
|
60
|
+
pub transaction_id: String,
|
|
61
|
+
/// Explicit optimistic fence. `None` asks the authority to begin and
|
|
62
|
+
/// commit an unconditional transaction at its current revision.
|
|
63
|
+
#[serde(default)]
|
|
64
|
+
pub base_revision: Option<u64>,
|
|
65
|
+
#[serde(default)]
|
|
66
|
+
pub preconditions: Vec<AtomicPrecondition>,
|
|
67
|
+
#[serde(default)]
|
|
68
|
+
pub record_preconditions: Vec<RecordPrecondition>,
|
|
69
|
+
pub mutations: Vec<AtomicMutation>,
|
|
70
|
+
#[serde(default)]
|
|
71
|
+
pub audit: Option<Value>,
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
75
|
+
pub struct AuthorityError {
|
|
76
|
+
pub code: String,
|
|
77
|
+
pub message: String,
|
|
78
|
+
#[serde(default)]
|
|
79
|
+
pub authoritative_revision: Option<u64>,
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
impl std::fmt::Display for AuthorityError {
|
|
83
|
+
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
84
|
+
write!(formatter, "{}: {}", self.code, self.message)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
impl std::error::Error for AuthorityError {}
|
|
89
|
+
|
|
90
|
+
#[async_trait]
|
|
91
|
+
pub trait FeltDbAuthority: Send + Sync {
|
|
92
|
+
async fn read_authoritative(&self) -> Result<AuthorityRead, AuthorityError>;
|
|
93
|
+
async fn current_revision(&self) -> Result<u64, AuthorityError>;
|
|
94
|
+
async fn commit(
|
|
95
|
+
&self,
|
|
96
|
+
transaction: AuthorityTransaction,
|
|
97
|
+
) -> Result<AtomicCommit, AuthorityError>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
#[derive(Clone)]
|
|
101
|
+
pub struct EmbeddedAuthority {
|
|
102
|
+
db: FeltDb,
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
impl EmbeddedAuthority {
|
|
106
|
+
pub fn open(path: impl AsRef<Path>) -> crate::Result<Self> {
|
|
107
|
+
Ok(Self {
|
|
108
|
+
db: FeltDb::open(path)?,
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
pub fn from_db(db: FeltDb) -> Self {
|
|
113
|
+
Self { db }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/// An explicitly historical value. It never claims freshness beyond its
|
|
117
|
+
/// captured `observed_revision`.
|
|
118
|
+
pub fn snapshot(&self) -> crate::Result<AuthorityRead> {
|
|
119
|
+
AuthorityRead::current(&self.db, ReadKind::Snapshot)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
fn authority_error(error: crate::FlowError) -> AuthorityError {
|
|
124
|
+
match error {
|
|
125
|
+
crate::FlowError::RevisionConflict {
|
|
126
|
+
expected: _,
|
|
127
|
+
actual,
|
|
128
|
+
} => AuthorityError {
|
|
129
|
+
code: "REVISION_CONFLICT".into(),
|
|
130
|
+
message: error.to_string(),
|
|
131
|
+
authoritative_revision: Some(actual),
|
|
132
|
+
},
|
|
133
|
+
other => AuthorityError {
|
|
134
|
+
code: "AUTHORITY_ERROR".into(),
|
|
135
|
+
message: other.to_string(),
|
|
136
|
+
authoritative_revision: None,
|
|
137
|
+
},
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
#[async_trait]
|
|
142
|
+
impl FeltDbAuthority for EmbeddedAuthority {
|
|
143
|
+
async fn read_authoritative(&self) -> Result<AuthorityRead, AuthorityError> {
|
|
144
|
+
AuthorityRead::current(&self.db, ReadKind::Authoritative).map_err(authority_error)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async fn current_revision(&self) -> Result<u64, AuthorityError> {
|
|
148
|
+
self.db.current_revision().map_err(authority_error)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async fn commit(
|
|
152
|
+
&self,
|
|
153
|
+
transaction: AuthorityTransaction,
|
|
154
|
+
) -> Result<AtomicCommit, AuthorityError> {
|
|
155
|
+
self.db
|
|
156
|
+
.apply_atomic_transaction_guarded(
|
|
157
|
+
&transaction.transaction_id,
|
|
158
|
+
None,
|
|
159
|
+
transaction.base_revision,
|
|
160
|
+
&transaction.preconditions,
|
|
161
|
+
&transaction.record_preconditions,
|
|
162
|
+
&transaction.mutations,
|
|
163
|
+
transaction.audit,
|
|
164
|
+
)
|
|
165
|
+
.map_err(authority_error)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
#[derive(Debug, Serialize, Deserialize)]
|
|
170
|
+
#[serde(tag = "operation", rename_all = "snake_case")]
|
|
171
|
+
enum Request {
|
|
172
|
+
Read,
|
|
173
|
+
CurrentRevision,
|
|
174
|
+
Commit { transaction: AuthorityTransaction },
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
#[derive(Debug, Serialize, Deserialize)]
|
|
178
|
+
#[serde(tag = "result", content = "value", rename_all = "snake_case")]
|
|
179
|
+
enum Response {
|
|
180
|
+
Read(AuthorityRead),
|
|
181
|
+
Revision(u64),
|
|
182
|
+
Commit(AtomicCommit),
|
|
183
|
+
Error(AuthorityError),
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
pub struct AuthorityServer {
|
|
187
|
+
socket_path: PathBuf,
|
|
188
|
+
authority: EmbeddedAuthority,
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
impl AuthorityServer {
|
|
192
|
+
pub fn open(
|
|
193
|
+
socket_path: impl Into<PathBuf>,
|
|
194
|
+
storage_path: impl AsRef<Path>,
|
|
195
|
+
) -> crate::Result<Self> {
|
|
196
|
+
Ok(Self {
|
|
197
|
+
socket_path: socket_path.into(),
|
|
198
|
+
authority: EmbeddedAuthority::open(storage_path)?,
|
|
199
|
+
})
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
pub async fn serve(self) -> io::Result<()> {
|
|
203
|
+
if let Some(parent) = self.socket_path.parent() {
|
|
204
|
+
std::fs::create_dir_all(parent)?;
|
|
205
|
+
}
|
|
206
|
+
if self.socket_path.exists() {
|
|
207
|
+
use std::os::unix::fs::FileTypeExt;
|
|
208
|
+
if !std::fs::symlink_metadata(&self.socket_path)?
|
|
209
|
+
.file_type()
|
|
210
|
+
.is_socket()
|
|
211
|
+
{
|
|
212
|
+
return Err(io::Error::new(
|
|
213
|
+
io::ErrorKind::AlreadyExists,
|
|
214
|
+
"authority socket path is not a socket",
|
|
215
|
+
));
|
|
216
|
+
}
|
|
217
|
+
std::fs::remove_file(&self.socket_path)?;
|
|
218
|
+
}
|
|
219
|
+
let listener = UnixListener::bind(&self.socket_path)?;
|
|
220
|
+
loop {
|
|
221
|
+
let (stream, _) = listener.accept().await?;
|
|
222
|
+
let authority = self.authority.clone();
|
|
223
|
+
tokio::spawn(async move {
|
|
224
|
+
let _ = handle_connection(stream, authority).await;
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async fn handle_connection(stream: UnixStream, authority: EmbeddedAuthority) -> io::Result<()> {
|
|
231
|
+
let (reader, mut writer) = stream.into_split();
|
|
232
|
+
let mut lines = BufReader::new(reader).lines();
|
|
233
|
+
while let Some(line) = lines.next_line().await? {
|
|
234
|
+
let response = match serde_json::from_str::<Request>(&line) {
|
|
235
|
+
Ok(Request::Read) => authority.read_authoritative().await.map(Response::Read),
|
|
236
|
+
Ok(Request::CurrentRevision) => {
|
|
237
|
+
authority.current_revision().await.map(Response::Revision)
|
|
238
|
+
}
|
|
239
|
+
Ok(Request::Commit { transaction }) => {
|
|
240
|
+
authority.commit(transaction).await.map(Response::Commit)
|
|
241
|
+
}
|
|
242
|
+
Err(error) => Err(AuthorityError {
|
|
243
|
+
code: "INVALID_REQUEST".into(),
|
|
244
|
+
message: error.to_string(),
|
|
245
|
+
authoritative_revision: None,
|
|
246
|
+
}),
|
|
247
|
+
}
|
|
248
|
+
.unwrap_or_else(Response::Error);
|
|
249
|
+
writer
|
|
250
|
+
.write_all(&serde_json::to_vec(&response).map_err(io::Error::other)?)
|
|
251
|
+
.await?;
|
|
252
|
+
writer.write_all(b"\n").await?;
|
|
253
|
+
}
|
|
254
|
+
Ok(())
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
#[derive(Clone)]
|
|
258
|
+
pub struct AuthorityClient {
|
|
259
|
+
socket_path: PathBuf,
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
impl AuthorityClient {
|
|
263
|
+
pub fn connect(socket_path: impl Into<PathBuf>) -> Self {
|
|
264
|
+
Self {
|
|
265
|
+
socket_path: socket_path.into(),
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async fn request(&self, request: Request) -> Result<Response, AuthorityError> {
|
|
270
|
+
let mut stream = UnixStream::connect(&self.socket_path)
|
|
271
|
+
.await
|
|
272
|
+
.map_err(|error| AuthorityError {
|
|
273
|
+
code: "AUTHORITY_UNAVAILABLE".into(),
|
|
274
|
+
message: error.to_string(),
|
|
275
|
+
authoritative_revision: None,
|
|
276
|
+
})?;
|
|
277
|
+
stream
|
|
278
|
+
.write_all(
|
|
279
|
+
&serde_json::to_vec(&request).map_err(|error| AuthorityError {
|
|
280
|
+
code: "INVALID_REQUEST".into(),
|
|
281
|
+
message: error.to_string(),
|
|
282
|
+
authoritative_revision: None,
|
|
283
|
+
})?,
|
|
284
|
+
)
|
|
285
|
+
.await
|
|
286
|
+
.map_err(io_authority_error)?;
|
|
287
|
+
stream.write_all(b"\n").await.map_err(io_authority_error)?;
|
|
288
|
+
let mut response = String::new();
|
|
289
|
+
BufReader::new(stream)
|
|
290
|
+
.read_line(&mut response)
|
|
291
|
+
.await
|
|
292
|
+
.map_err(io_authority_error)?;
|
|
293
|
+
match serde_json::from_str(&response).map_err(|error| AuthorityError {
|
|
294
|
+
code: "INVALID_RESPONSE".into(),
|
|
295
|
+
message: error.to_string(),
|
|
296
|
+
authoritative_revision: None,
|
|
297
|
+
})? {
|
|
298
|
+
Response::Error(error) => Err(error),
|
|
299
|
+
response => Ok(response),
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
fn io_authority_error(error: io::Error) -> AuthorityError {
|
|
305
|
+
AuthorityError {
|
|
306
|
+
code: "AUTHORITY_UNAVAILABLE".into(),
|
|
307
|
+
message: error.to_string(),
|
|
308
|
+
authoritative_revision: None,
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
#[async_trait]
|
|
313
|
+
impl FeltDbAuthority for AuthorityClient {
|
|
314
|
+
async fn read_authoritative(&self) -> Result<AuthorityRead, AuthorityError> {
|
|
315
|
+
match self.request(Request::Read).await? {
|
|
316
|
+
Response::Read(read) => Ok(read),
|
|
317
|
+
_ => Err(AuthorityError {
|
|
318
|
+
code: "INVALID_RESPONSE".into(),
|
|
319
|
+
message: "expected authority read".into(),
|
|
320
|
+
authoritative_revision: None,
|
|
321
|
+
}),
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
async fn current_revision(&self) -> Result<u64, AuthorityError> {
|
|
326
|
+
match self.request(Request::CurrentRevision).await? {
|
|
327
|
+
Response::Revision(revision) => Ok(revision),
|
|
328
|
+
_ => Err(AuthorityError {
|
|
329
|
+
code: "INVALID_RESPONSE".into(),
|
|
330
|
+
message: "expected authority revision".into(),
|
|
331
|
+
authoritative_revision: None,
|
|
332
|
+
}),
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async fn commit(
|
|
337
|
+
&self,
|
|
338
|
+
transaction: AuthorityTransaction,
|
|
339
|
+
) -> Result<AtomicCommit, AuthorityError> {
|
|
340
|
+
match self.request(Request::Commit { transaction }).await? {
|
|
341
|
+
Response::Commit(commit) => Ok(commit),
|
|
342
|
+
_ => Err(AuthorityError {
|
|
343
|
+
code: "INVALID_RESPONSE".into(),
|
|
344
|
+
message: "expected authority commit".into(),
|
|
345
|
+
authoritative_revision: None,
|
|
346
|
+
}),
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
use feltdb::{
|
|
2
|
+
authority::{AuthorityClient, AuthorityTransaction, FeltDbAuthority},
|
|
3
|
+
AtomicMutation,
|
|
4
|
+
};
|
|
5
|
+
use serde_json::json;
|
|
6
|
+
|
|
7
|
+
#[tokio::main]
|
|
8
|
+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
9
|
+
let mut arguments = std::env::args().skip(1);
|
|
10
|
+
let socket = arguments
|
|
11
|
+
.next()
|
|
12
|
+
.ok_or("usage: feltdb-authority-client <socket> <id>")?;
|
|
13
|
+
let id = arguments
|
|
14
|
+
.next()
|
|
15
|
+
.ok_or("usage: feltdb-authority-client <socket> <id>")?;
|
|
16
|
+
let client = AuthorityClient::connect(socket);
|
|
17
|
+
client
|
|
18
|
+
.commit(AuthorityTransaction {
|
|
19
|
+
transaction_id: format!("writer-{id}"),
|
|
20
|
+
base_revision: None,
|
|
21
|
+
preconditions: vec![],
|
|
22
|
+
record_preconditions: vec![],
|
|
23
|
+
mutations: vec![AtomicMutation {
|
|
24
|
+
capability: "registrations".into(),
|
|
25
|
+
key: format!("registrations:{id}"),
|
|
26
|
+
value: Some(json!({ "id": id })),
|
|
27
|
+
}],
|
|
28
|
+
audit: None,
|
|
29
|
+
})
|
|
30
|
+
.await?;
|
|
31
|
+
Ok(())
|
|
32
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
use feltdb::authority::AuthorityServer;
|
|
2
|
+
|
|
3
|
+
#[tokio::main]
|
|
4
|
+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
5
|
+
let mut arguments = std::env::args_os().skip(1);
|
|
6
|
+
let socket = arguments
|
|
7
|
+
.next()
|
|
8
|
+
.ok_or("usage: feltdb-authority <socket> <storage>")?;
|
|
9
|
+
let storage = arguments
|
|
10
|
+
.next()
|
|
11
|
+
.ok_or("usage: feltdb-authority <socket> <storage>")?;
|
|
12
|
+
if arguments.next().is_some() {
|
|
13
|
+
return Err("usage: feltdb-authority <socket> <storage>".into());
|
|
14
|
+
}
|
|
15
|
+
AuthorityServer::open(socket, std::path::PathBuf::from(storage))?
|
|
16
|
+
.serve()
|
|
17
|
+
.await?;
|
|
18
|
+
Ok(())
|
|
19
|
+
}
|