acyclic-fs 0.1.5

Rust SDK for versioned workspaces and embedded native mounts
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//! Black-box conformance for canonical distributed adapters over public memory providers.
#![cfg(feature = "distributed")]

use acyclic_fs::{
    AppendOutcome, AsyncAuthorityStore, AsyncObjectStore, AuthorityId, AuthorityStoreError,
    CancellationToken, CreateAuthorityOutcome, Digest, DistributedFs, EmbeddedCapabilities, Epoch,
    FenceOutcome, ForkOptions, Fs, GenerationFork, GenerationForkSource, GenerationId, Head,
    IdempotencyKey, ObjectId, ObjectKind, OperationFailure, OperationId, ProposedCommit,
    ReplayLimit, Sequence, WorkBudget, object_digest,
};
use acyclic_fs::{ProviderObjectStore, StreamAuthorityStore};
use acyclic_objects::{MemoryObjects, ObjectsProvider};
use acyclic_stream::{
    AppendRequest, ChildrenRequest, MemoryStream, ReadRequest, StreamError, StreamPath,
    StreamProvider,
};
use bytes::Bytes;
use futures::StreamExt;
use std::sync::Arc;

#[tokio::test]
#[allow(
    clippy::too_many_lines,
    reason = "linear conformance walkthrough over one authority's lifecycle; each step is a \
              distinct sequential assertion building on the prior step's state, and splitting \
              it apart would only scatter the narrative across functions without clarifying it"
)]
async fn authority_lifecycle_is_native_stream_backed_and_exactly_idempotent()
-> Result<(), Box<dyn std::error::Error>> {
    let provider = Arc::new(MemoryStream::default());
    let store = StreamAuthorityStore::new(Arc::clone(&provider));
    let authority_id = AuthorityId::from_bytes([7; 16]);
    let cancellation = CancellationToken::new();
    assert_eq!(
        store
            .create_authority(
                authority_id,
                Epoch::GENESIS,
                WorkBudget::UNBOUNDED,
                &cancellation,
            )
            .await?
            .value,
        CreateAuthorityOutcome::Created(Head::genesis(Epoch::GENESIS))
    );
    assert_eq!(store.authorities(8).await?, vec![authority_id]);
    assert_eq!(
        provider
            .tail(StreamPath::new(format!(
                "fs/authorities/{}",
                hex::encode(authority_id.into_bytes())
            ))?)
            .await?,
        1
    );

    let proposal = ProposedCommit {
        operation_id: OperationId::from_bytes([8; 16]),
        fingerprint: Digest::from_bytes([9; 32]),
        payload: Bytes::from_static(b"generation"),
    };
    let committed = store
        .compare_and_append(
            authority_id,
            Epoch::GENESIS,
            Head::genesis(Epoch::GENESIS),
            proposal.clone(),
            WorkBudget::UNBOUNDED,
            &cancellation,
        )
        .await?
        .value;
    let AppendOutcome::Committed(commit) = committed else {
        return Err("first operation was not committed".into());
    };
    assert_eq!(
        store
            .compare_and_append(
                authority_id,
                Epoch::GENESIS,
                Head::genesis(Epoch::GENESIS),
                proposal.clone(),
                WorkBudget::UNBOUNDED,
                &cancellation,
            )
            .await?
            .value,
        AppendOutcome::AlreadyCommitted(commit.clone())
    );
    let second_authority = AuthorityId::from_bytes([17; 16]);
    store
        .create_authority(
            second_authority,
            Epoch::GENESIS,
            WorkBudget::UNBOUNDED,
            &cancellation,
        )
        .await?;
    assert!(matches!(
        store
            .compare_and_append(
                second_authority,
                Epoch::GENESIS,
                Head::genesis(Epoch::GENESIS),
                proposal.clone(),
                WorkBudget::UNBOUNDED,
                &cancellation,
            )
            .await?
            .value,
        AppendOutcome::Committed(_)
    ));
    let conflicting = ProposedCommit {
        fingerprint: Digest::from_bytes([10; 32]),
        ..proposal.clone()
    };
    assert!(matches!(
        store
            .compare_and_append(
                authority_id,
                Epoch::GENESIS,
                Head::genesis(Epoch::GENESIS),
                conflicting.clone(),
                WorkBudget::UNBOUNDED,
                &cancellation,
            )
            .await?
            .value,
        AppendOutcome::IdempotencyConflict { .. }
    ));
    let authority_path = StreamPath::new(format!(
        "fs/authorities/{}",
        hex::encode(authority_id.into_bytes())
    ))?;
    let children = provider
        .children(ChildrenRequest {
            parent: Some(authority_path),
            limit: 16,
        })
        .await?
        .collect::<Vec<_>>()
        .await
        .into_iter()
        .collect::<Result<Vec<_>, _>>()?;
    assert!(
        children
            .iter()
            .all(|child| !child.path.as_str().contains("/operations")),
        "native Stream idempotency must replace shadow operation paths"
    );
    assert_eq!(
        store
            .replay(
                authority_id,
                Sequence::GENESIS,
                ReplayLimit {
                    records: 4,
                    payload_bytes: 1024,
                },
                WorkBudget::UNBOUNDED,
                &cancellation,
            )
            .await?
            .value,
        vec![commit.clone()]
    );
    let head = Head {
        epoch: commit.epoch,
        sequence: commit.sequence,
        digest: commit.digest,
    };
    assert!(matches!(
        store
            .fence(authority_id, head, WorkBudget::UNBOUNDED, &cancellation,)
            .await?
            .value,
        FenceOutcome::Advanced(_)
    ));
    assert_eq!(
        store
            .compare_and_append(
                authority_id,
                Epoch::GENESIS,
                Head::genesis(Epoch::GENESIS),
                proposal.clone(),
                WorkBudget::UNBOUNDED,
                &cancellation,
            )
            .await?
            .value,
        AppendOutcome::AlreadyCommitted(commit)
    );
    assert!(matches!(
        store
            .compare_and_append(
                authority_id,
                Epoch::GENESIS,
                Head::genesis(Epoch::GENESIS),
                conflicting,
                WorkBudget::UNBOUNDED,
                &cancellation,
            )
            .await?
            .value,
        AppendOutcome::IdempotencyConflict { .. }
    ));
    assert!(matches!(
        store
            .compare_and_append(
                authority_id,
                Epoch::GENESIS,
                Head::genesis(Epoch::GENESIS),
                ProposedCommit {
                    operation_id: OperationId::from_bytes([18; 16]),
                    ..proposal
                },
                WorkBudget::UNBOUNDED,
                &cancellation,
            )
            .await?
            .value,
        AppendOutcome::Fenced { .. }
    ));
    store
        .create_authority(
            AuthorityId::from_bytes([11; 16]),
            Epoch::GENESIS,
            WorkBudget::UNBOUNDED,
            &cancellation,
        )
        .await?;
    assert!(store.authorities(1).await.is_err());
    Ok(())
}

#[tokio::test]
async fn immutable_objects_use_the_exact_public_objects_provider()
-> Result<(), Box<dyn std::error::Error>> {
    let provider = Arc::new(MemoryObjects::default());
    let bucket = provider
        .create_bucket("adapter".to_owned(), Some("create-adapter".to_owned()))
        .await?
        .bucket
        .ok_or("bucket identity missing")?;
    let store = ProviderObjectStore::new(provider, bucket);
    let bytes = Bytes::from_static(b"authenticated");
    let object_id = ObjectId {
        kind: ObjectKind::BlobChunk,
        digest: object_digest(ObjectKind::BlobChunk, &bytes),
    };
    let cancellation = CancellationToken::new();
    store
        .put(
            object_id,
            bytes.clone(),
            WorkBudget::UNBOUNDED,
            &cancellation,
        )
        .await?;
    let read = store
        .read(object_id, 64, WorkBudget::UNBOUNDED, &cancellation)
        .await?;
    assert_eq!(read.value.bytes, bytes);
    assert!(
        store
            .contains(object_id, WorkBudget::UNBOUNDED, &cancellation)
            .await?
            .value
    );
    let wrong = ObjectId {
        kind: ObjectKind::GenerationRoot,
        digest: Digest::from_bytes([11; 32]),
    };
    assert!(
        store
            .put(
                wrong,
                Bytes::from_static(b"not-that-digest"),
                WorkBudget::UNBOUNDED,
                &cancellation,
            )
            .await
            .is_err()
    );
    Ok(())
}

#[tokio::test]
async fn workspace_fork_uses_one_native_stream_prefix_and_independent_suffixes()
-> Result<(), Box<dyn std::error::Error>> {
    let streams = Arc::new(MemoryStream::default());
    let objects = Arc::new(MemoryObjects::default());
    let bucket = objects
        .create_bucket(
            "filesystem".to_owned(),
            Some("create-filesystem".to_owned()),
        )
        .await?
        .bucket
        .ok_or("bucket identity missing")?;
    let fs = Fs::new(
        StreamAuthorityStore::new(Arc::clone(&streams)),
        ProviderObjectStore::new(objects, bucket),
        EmbeddedCapabilities::MEMORY,
    );
    let source = fs.create_workspace("source").await?;
    source.write_text("/shared", "base").await?;
    let selected = source.head().await?;
    let child = source
        .fork(
            "child",
            ForkOptions::from_generation(selected.clone(), IdempotencyKey::from_bytes([0x51; 16])),
        )
        .await?;

    let lineage = |workspace: acyclic_fs::WorkspaceId| {
        StreamPath::new(format!(
            "fs/authorities/{}/lineage",
            hex::encode(workspace.into_bytes())
        ))
    };
    let source_lineage = lineage(source.id())?;
    let child_lineage = lineage(child.id())?;
    let inherited = streams.tail(source_lineage.clone()).await?;
    let mut source_records = streams
        .read(ReadRequest {
            path: source_lineage,
            from: 0,
            limit: u32::try_from(inherited)?,
        })
        .await?;
    let mut child_records = streams
        .read(ReadRequest {
            path: child_lineage,
            from: 0,
            limit: u32::try_from(inherited)?,
        })
        .await?;
    while let Some(source_record) = source_records.next().await {
        let source_record = source_record?;
        let child_record = child_records
            .next()
            .await
            .ok_or("native fork omitted an inherited record")??;
        assert_eq!(child_record, source_record);
    }
    assert!(child_records.next().await.is_none());
    assert_eq!(
        child.read("/shared", 16).await?,
        Bytes::from_static(b"base")
    );

    source.write_text("/source-only", "source").await?;
    let retry = source
        .fork(
            "child",
            ForkOptions::from_generation(selected, IdempotencyKey::from_bytes([0x51; 16])),
        )
        .await?;
    assert_eq!(retry.id(), child.id());
    child.write_text("/child-only", "child").await?;
    assert!(child.read("/source-only", 16).await.is_err());
    assert!(source.read("/child-only", 16).await.is_err());
    Ok(())
}

#[tokio::test]
async fn distributed_facade_leases_authorize_workspace_publication()
-> Result<(), Box<dyn std::error::Error>> {
    let streams = Arc::new(MemoryStream::default());
    let objects = Arc::new(MemoryObjects::default());
    let bucket = objects
        .create_bucket(
            "distributed-leases".to_owned(),
            Some("create-distributed-leases".to_owned()),
        )
        .await?
        .bucket
        .ok_or("bucket identity missing")?;
    let fs = Fs::new(
        StreamAuthorityStore::new(streams),
        ProviderObjectStore::new(objects, bucket),
        EmbeddedCapabilities::MEMORY,
    );
    let workspace = fs.create_workspace("leased").await?;
    let parent = workspace.head().await?;
    let distributed = DistributedFs::new(fs, ());
    let operations = distributed.operations();
    let lease = operations
        .begin(workspace.id(), parent.id(), "tool", 1, u64::MAX)
        .await?;
    let mut transaction = workspace
        .begin_transaction(IdempotencyKey::from_bytes([0x81; 16]))
        .await?;
    transaction
        .write_text("/published", "through lease")
        .await?;
    assert!(matches!(
        transaction
            .commit_with_permit(lease.publication_permit())
            .await?,
        acyclic_fs::TransactionCommit::Committed(_)
    ));
    assert_eq!(
        workspace.read("/published", 32).await?,
        Bytes::from_static(b"through lease")
    );
    Ok(())
}

#[tokio::test]
async fn generation_fork_rejects_an_authority_without_lineage_locators()
-> Result<(), Box<dyn std::error::Error>> {
    let streams = Arc::new(MemoryStream::default());
    let store = StreamAuthorityStore::new(streams);
    let source = AuthorityId::from_bytes([0x61; 16]);
    let destination = AuthorityId::from_bytes([0x62; 16]);
    let cancellation = CancellationToken::new();
    store
        .create_authority(source, Epoch::GENESIS, WorkBudget::UNBOUNDED, &cancellation)
        .await?;
    let Err(error) = store
        .fork_generation_authority(
            GenerationForkSource {
                authority: source,
                generation: GenerationId::new(Digest::from_bytes([0x63; 32])),
                lineage: GenerationFork::PublishedPrefix,
            },
            destination,
            OperationId::from_bytes([0x64; 16]),
            WorkBudget::UNBOUNDED,
            &cancellation,
        )
        .await
    else {
        return Err("missing lineage locator unexpectedly forked".into());
    };
    assert!(matches!(error.error, AuthorityStoreError::Rejected(_)));
    assert!(matches!(
        store
            .head(destination, WorkBudget::UNBOUNDED, &cancellation)
            .await,
        Err(OperationFailure {
            error: AuthorityStoreError::Missing,
            ..
        })
    ));
    Ok(())
}

#[tokio::test]
async fn failed_atomic_generation_fork_leaves_no_destination_lineage()
-> Result<(), Box<dyn std::error::Error>> {
    let streams = Arc::new(MemoryStream::default());
    let objects = Arc::new(MemoryObjects::default());
    let bucket = objects
        .create_bucket(
            "atomic-fork".to_owned(),
            Some("create-atomic-fork".to_owned()),
        )
        .await?
        .bucket
        .ok_or("bucket identity missing")?;
    let store = StreamAuthorityStore::new(Arc::clone(&streams));
    let fs = Fs::new(
        store.clone(),
        ProviderObjectStore::new(objects, bucket),
        EmbeddedCapabilities::MEMORY,
    );
    let source = fs.create_workspace("atomic-source").await?;
    source.write_text("/shared", "base").await?;
    let selected = source.head().await?;
    let destination_id = fs.workspace_id("blocked-destination")?;
    let destination_authority = AuthorityId::from_bytes(destination_id.into_bytes());
    let prefix = format!(
        "fs/authorities/{}",
        hex::encode(destination_authority.into_bytes())
    );
    streams
        .append(AppendRequest {
            path: StreamPath::new(format!("{prefix}/records"))?,
            records: vec![Bytes::from_static(b"conflict")],
            if_tail: Some(0),
            idempotency_key: None,
        })
        .await?;
    let source_authority = AuthorityId::from_bytes(source.id().into_bytes());
    assert!(
        store
            .fork_generation_authority(
                GenerationForkSource {
                    authority: source_authority,
                    generation: selected.id(),
                    lineage: GenerationFork::PublishedPrefix,
                },
                destination_authority,
                OperationId::from_bytes([0x65; 16]),
                WorkBudget::UNBOUNDED,
                &CancellationToken::new(),
            )
            .await
            .is_err()
    );
    assert_eq!(
        streams
            .tail(StreamPath::new(format!("{prefix}/lineage"))?)
            .await,
        Err(StreamError::NotFound)
    );
    Ok(())
}