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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
//! Cross-process write serialisation per mem.
//!
//! Every mutation runs reload-before-operation, compares the caller's
//! `expected_hash`, then commits. Those three steps must be one atomic
//! unit across processes: if a sibling process commits between this
//! process's reload and its commit, the compare runs against a snapshot
//! that is already behind, and a stale write lands over the sibling's
//! (a lost update reported as success). The guard below holds an
//! exclusive OS file lock on the mem's [`MemBackend::write_lock_path`]
//! for the whole unit, so a second writer blocks until the first has
//! committed and then reloads, sees the new content, and gets its honest
//! `HASH_MISMATCH`.
//!
//! The lock is reentrant per thread ([`crate::storage::path_lock`]): a
//! mutation that calls another mutation entry point (a batch over
//! single-item paths, a merge over creates) takes the same lock again
//! without blocking on itself. Two threads, or two engines on two
//! threads, still exclude each other.
//!
//! The guard is also the one exit every mutation shares for its staged
//! writes. A mutation stages bytes into the backend and commits them
//! later; an error between the two (a refused sidecar write, a failed
//! commit, a `?` in any verb) used to leave the staged bytes in the
//! backend, where the next read served them as content that never
//! landed. When the outermost guard of a thread drops, it discards
//! whatever is still staged on the mems it locked. After a commit
//! nothing is staged and the discard is a no-op, so a call site has no
//! failure path to remember: holding the lock is the cleanup.
//!
//! [`MemBackend::write_lock_path`]: crate::backend::MemBackend::write_lock_path
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use crate::backend::{BackendError, MemBackend};
use crate::storage::path_lock;
use super::{Engine, EngineError};
/// Held for the duration of one mutation; dropping it discards the
/// writes still staged on the mems it covers and releases every lock
/// it took (both when the last nested holder drops).
#[must_use = "the write lock is released as soon as the guard drops"]
pub struct MemWriteGuard {
/// Each covered mem's backend with its lock-file path. A backend
/// with no shared on-disk state has no path and takes no OS lock,
/// but its staged writes are discarded like any other's.
held: Vec<Held>,
}
/// One mem a guard covers.
struct Held {
path: Option<PathBuf>,
backend: Arc<dyn MemBackend>,
/// The mount's record of a discard that failed
/// ([`super::MountedBackend::strays`]). The guard cannot reach the
/// engine when it drops, so it shares this flag with the mount.
strays: Arc<std::sync::atomic::AtomicBool>,
}
thread_local! {
/// How many guards of this thread cover a backend, by its address.
/// Only the outermost one discards: an inner guard dropping must
/// not empty a staged set the outer mutation is still building.
static COVERED: RefCell<HashMap<usize, usize>> = RefCell::new(HashMap::new());
}
fn backend_key(backend: &Arc<dyn MemBackend>) -> usize {
Arc::as_ptr(backend) as *const () as usize
}
impl MemWriteGuard {
fn empty() -> Self {
Self { held: Vec::new() }
}
fn cover(&mut self, held: Held) {
COVERED.with(|c| {
*c.borrow_mut()
.entry(backend_key(&held.backend))
.or_insert(0) += 1
});
self.held.push(held);
}
}
impl Drop for MemWriteGuard {
fn drop(&mut self) {
for Held {
path,
backend,
strays,
} in self.held.iter().rev()
{
let outermost = COVERED.with(|c| {
let mut covered = c.borrow_mut();
let key = backend_key(backend);
match covered.get_mut(&key) {
Some(count) if *count > 1 => {
*count -= 1;
false
}
_ => {
covered.remove(&key);
true
}
}
});
// Discard before the lock goes, so no other writer can
// stage between the two. A discard that fails leaves the
// staged writes where a read or the next commit would pick
// them up, so the backend is remembered and the engine
// discards again before its next operation.
if outermost {
strays.store(
backend.discard_pending().is_err(),
std::sync::atomic::Ordering::SeqCst,
);
}
if let Some(path) = path {
path_lock::release(path);
}
}
}
}
impl Engine {
/// Discard what a failed mutation left staged on `mem`'s backend,
/// if an earlier discard failed. Run before every operation, ahead
/// of anything that reads the backend: a staged write is served in
/// place of the committed bytes, and the next commit would land it.
///
/// When the discard fails again, the backend is replaced by a fresh
/// one over the same storage: what is staged lives in the backend
/// value and nowhere on disk, so a new value has none of it. Only a
/// mount whose storage is on disk is replaced (an in-memory mem is
/// its backend), and not while a mutation of this thread holds the
/// backend. An error means the staged writes are still there.
pub(super) fn discard_strays(&mut self, mem: &str) -> Result<(), BackendError> {
use std::sync::atomic::Ordering;
let Some(idx) = self.mounts.iter().position(|m| m.mount.mem == mem) else {
return Ok(());
};
let state = &self.mounts[idx];
if !state.strays.load(Ordering::SeqCst) {
return Ok(());
}
let failure = match state.backend.discard_pending() {
Ok(()) => {
state.strays.store(false, Ordering::SeqCst);
return Ok(());
}
Err(e) => e,
};
let on_disk = matches!(
state.mount.storage,
crate::workspace::MountStorage::Folder { .. }
| crate::workspace::MountStorage::GitBranch { .. }
);
let held_by_a_mutation = COVERED.with(|c| {
c.borrow()
.get(&backend_key(&state.backend))
.is_some_and(|count| *count > 0)
});
if on_disk
&& !held_by_a_mutation
&& let Ok(fresh) = (self.backend_factory)(&state.mount)
{
self.mounts[idx].backend = Arc::from(fresh);
self.mounts[idx].strays = Default::default();
return Ok(());
}
Err(failure)
}
/// The lock and the view for a verb that learns what it writes, or
/// what its guard reads, only by reading the workspace (a delete's
/// and a retype's referrer check, a rename's referrers). Such a verb
/// cannot name its mems up front, and a view of a mem taken before
/// that mem's lock is held can miss what a sibling wrote into it. So
/// it takes every writable mount's lock, in the one sorted order the
/// lock defines, after the roster is reconciled (a mem attached
/// since is locked too), and only then reloads every mem and loads
/// the ones whose loading was deferred. No lock is taken after
/// planning began, so there is nothing to restart.
///
/// A mem that cannot be reloaded refuses the verb with
/// `MEM_VIEW_STALE`: its view is from before the lock, and a guard
/// that reads every mem would be evaluated on it. So does a mem
/// that is out of service (quarantined), which is in no view.
///
/// `writes` false (a rehearsal) takes no lock and still brings the
/// view up to date.
pub fn lock_workspace_for_write_if(
&mut self,
writes: bool,
) -> Result<(MemWriteGuard, Vec<crate::ops::WarningHint>), EngineError> {
let mut warnings = self.roster_before_locking();
let (guard, reloaded) = self.lock_whole_workspace(writes)?;
warnings.extend(reloaded);
Ok((guard, warnings))
}
/// [`Self::lock_workspace_for_write_if`] for a verb that names the
/// entity it writes (a delete, a rename, a retype). When the id
/// names a mem that is mounted read-only the write is refused here,
/// before any lock is taken, so the refusal lays nothing where that
/// mem lives. A bare slug names no mem and is resolved under the
/// locks as before.
pub(in crate::engine) fn lock_workspace_for_entity_write(
&mut self,
writes: bool,
id: &crate::EntityId,
) -> Result<(MemWriteGuard, Vec<crate::ops::WarningHint>), EngineError> {
let mut warnings = self.roster_before_locking();
if writes {
let mem = id.mem();
if self.mounts.iter().any(|m| {
m.mount.mem == mem && m.mount.capability != crate::workspace::MountCapability::Write
}) {
return Err(EngineError::ReadOnlyMount(mem.to_string()));
}
}
let (guard, reloaded) = self.lock_whole_workspace(writes)?;
warnings.extend(reloaded);
Ok((guard, warnings))
}
/// [`Self::lock_workspace_for_write_if`] for an operation on the
/// mem `subject` as a whole (its deletion): `subject` itself may be
/// unreadable or out of service, which is often why it is being
/// removed, and that does not refuse the operation. Every other
/// mem must be readable as for any verb that reads the workspace.
pub fn lock_workspace_for_mem_operation(
&mut self,
subject: &str,
) -> Result<(MemWriteGuard, Vec<crate::ops::WarningHint>), EngineError> {
self.lock_workspace_for_mems_operation(&[subject])
}
/// [`Self::lock_workspace_for_mem_operation`] for an operation on
/// several mems as wholes (`pull --all` reconciles every one): each
/// subject may be unreadable or out of service; every other mem must
/// be readable.
pub fn lock_workspace_for_mems_operation(
&mut self,
subjects: &[&str],
) -> Result<(MemWriteGuard, Vec<crate::ops::WarningHint>), EngineError> {
let mut warnings = self.roster_before_locking();
let (guard, reloaded) = self.lock_whole_workspace_except(true, subjects, None)?;
warnings.extend(reloaded);
Ok((guard, warnings))
}
/// [`Self::lock_workspace_for_mem_operation`] for taking a
/// read-only mount of `subject` off the roster: every other mem's
/// lock is held and every mem is read under it, and the subject's
/// own lock is not taken. Nothing is written to the subject's
/// storage, which this workspace only ever read and may not be able
/// to write to, so no lock file is laid where it lives.
pub fn lock_workspace_beside_read_mount(
&mut self,
subject: &str,
) -> Result<(MemWriteGuard, Vec<crate::ops::WarningHint>), EngineError> {
let mut warnings = self.roster_before_locking();
let (guard, reloaded) =
self.lock_whole_workspace_except(true, &[subject], Some(subject))?;
warnings.extend(reloaded);
Ok((guard, warnings))
}
/// Whether `mem` is a read-only mount of a branch or a folder: the
/// mounts `memstead mem unregister` takes off the roster without
/// touching their storage.
pub fn is_removable_read_mount(&self, mem: &str) -> bool {
self.mounts.iter().any(|m| {
m.mount.mem == mem
&& m.mount.capability == crate::workspace::MountCapability::ReadOnly
&& matches!(
m.mount.storage,
crate::workspace::MountStorage::GitBranch { .. }
| crate::workspace::MountStorage::Folder { .. }
)
})
}
/// The roster as it stands, reconciled ahead of a decision that is
/// taken before any lock (which lock form a verb needs).
pub fn settle_roster_before_locking(&mut self) -> Vec<crate::ops::WarningHint> {
self.roster_before_locking()
}
fn lock_whole_workspace(
&mut self,
writes: bool,
) -> Result<(MemWriteGuard, Vec<crate::ops::WarningHint>), EngineError> {
self.lock_whole_workspace_except(writes, &[], None)
}
/// `tolerated` are mems that may be unreadable without refusing the
/// operation; `unlocked` is one whose own lock is not taken.
fn lock_whole_workspace_except(
&mut self,
writes: bool,
tolerated: &[&str],
unlocked: Option<&str>,
) -> Result<(MemWriteGuard, Vec<crate::ops::WarningHint>), EngineError> {
// Every mount that has a write lock, the ones this engine mounts
// read-only included: another process may mount the same
// storage writable, and a guard that reads it needs its writer
// to wait like any other.
let (guard, mut warnings) = self.lock_on_a_settled_roster(writes, |engine| {
engine
.mounts
.iter()
.filter(|m| {
m.mount.capability == crate::workspace::MountCapability::Write
|| m.backend.write_lock_path().is_some()
})
.filter(|m| Some(m.mount.mem.as_str()) != unlocked)
.map(|m| m.mount.mem.clone())
.collect()
})?;
warnings.extend(self.reload_if_stale_content_only(None));
if let Some((stale, failure)) = self
.mounts
.iter()
.filter(|m| !tolerated.contains(&m.mount.mem.as_str()))
.find_map(|m| m.reload_failure.as_ref().map(|f| (m, f)))
{
return Err(failure.refusal(&stale.mount.mem));
}
// A mem that is out of service is in no view at all: one whose
// deferred load just failed under this lock, or one that never
// attached. Its referrers are as unread as a stale mem's.
if let Some(out) = self
.quarantined
.iter()
.find(|q| !tolerated.contains(&q.mount.mem.as_str()))
{
return Err(EngineError::StaleView {
mem: out.mount.mem.clone(),
cause: super::StaleCause::OutOfService,
detail: format!("{}: {}", out.reason_code, out.reason_message),
});
}
Ok((guard, warnings))
}
/// Take the locks `choose` names, and hold them over a roster that
/// did not change while this process waited for them. The process
/// that held a lock may have changed the roster before it let go (a
/// mem rename takes the mem away under the lock a writer into it
/// is waiting on; a mem created meanwhile is one more writable
/// mount). So the roster is reconciled once more under the locks;
/// when that changes the mounts, the locks are given back and
/// chosen again. Nothing is planned before this returns.
fn lock_on_a_settled_roster(
&mut self,
writes: bool,
choose: impl Fn(&Self) -> Vec<String>,
) -> Result<(MemWriteGuard, Vec<crate::ops::WarningHint>), EngineError> {
let nested = COVERED.with(|c| !c.borrow().is_empty());
if nested || !writes {
return Ok((
self.lock_mems_for_write_if(writes, &choose(self))?,
Vec::new(),
));
}
let mut warnings = Vec::new();
// A roster that keeps changing is not waited out for ever, and
// no lock set is kept over a roster that changed under it: the
// call is refused, and sent again it starts from the roster as
// it is then.
for _ in 0..3 {
let guard = self.lock_mems_for_write(&choose(self))?;
// What counts is whether the mounts changed, not whether
// the reconcile had something to say: a mem that keeps
// failing to attach is reported every time and changes
// nothing.
let mounted = |engine: &Self| -> Vec<String> {
engine.mounts.iter().map(|m| m.mount.mem.clone()).collect()
};
let before = mounted(self);
let reported = self.roster_warnings();
if mounted(self) == before {
warnings.extend(reported);
return Ok((guard, warnings));
}
drop(guard);
warnings.extend(reported);
}
Err(EngineError::Backend(BackendError::Other(
"the workspace's mount roster changed each time this operation had taken its write \
locks; nothing was written, send the call again"
.to_string(),
)))
}
/// The lock and the view of a verb that names the mems it writes.
/// `locked` are the mems whose lock it takes, `written` the ones
/// it writes (reloaded, and loaded when deferred); every other
/// loaded mem is reloaded too ([`Self::level_for_write`]).
///
/// When `reads_workspace` says so, the verb is one that learns its
/// write set or its guard's read set by reading the workspace after
/// all, and gets [`Self::lock_workspace_for_write_if`] instead:
/// a call that names an entity by a bare slug (which mem carries
/// the slug is a fact about every mem), and a call that adds an
/// edge on an acyclic relationship (the cycle guard walks edges in
/// every mem, and two writers that each hold one mem's lock could
/// each add the edge that closes the other's cycle). The question
/// is asked after the roster is reconciled and before any lock is
/// taken.
///
/// A mutation running inside another one keeps the outer one's
/// locks: taking further locks there would take them out of order.
pub(in crate::engine) fn lock_for_verb<S: AsRef<str>>(
&mut self,
writes: bool,
locked: &[S],
written: &[S],
reads_workspace: impl Fn(&Self) -> bool,
) -> Result<(MemWriteGuard, Vec<crate::ops::WarningHint>), EngineError> {
let nested = COVERED.with(|c| !c.borrow().is_empty());
let mut warnings = self.roster_before_locking();
// A write into a mem mounted read-only is refused before any
// lock is taken: the refusal lays nothing where the mem lives,
// its lock file included.
if writes {
for mem in written {
if self.mounts.iter().any(|m| {
m.mount.mem == mem.as_ref()
&& m.mount.capability != crate::workspace::MountCapability::Write
}) {
return Err(EngineError::ReadOnlyMount(mem.as_ref().to_string()));
}
}
}
if !nested && reads_workspace(self) {
let (guard, reloaded) = self.lock_whole_workspace(writes)?;
warnings.extend(reloaded);
return Ok((guard, warnings));
}
let names: Vec<String> = locked.iter().map(|m| m.as_ref().to_string()).collect();
let (guard, settled) = self.lock_on_a_settled_roster(writes, |_| names.clone())?;
warnings.extend(settled);
warnings.extend(self.level_for_write(written));
// The question again, on the view taken under the locks: what
// it reads (a schema's relationships) may have changed since
// it was asked. A yes now gives the locks back and takes every
// mount, before anything is planned.
if writes && !nested && reads_workspace(self) {
drop(guard);
let (guard, reloaded) = self.lock_whole_workspace(writes)?;
warnings.extend(reloaded);
return Ok((guard, warnings));
}
Ok((guard, warnings))
}
/// [`Self::lock_for_verb`] for a verb whose references depend on
/// what is in storage: an update that appends to a section or
/// patches one adds whatever link the resulting body holds, and
/// that body is the stored text with the call's text worked in.
/// `refer` names the mems the verb's result refers to, from the
/// engine's view. It is asked before the locks are taken and again
/// under them, on the reloaded view; when the second answer names
/// a mem the first did not, the locks are given back and taken
/// again with it. Nothing is planned before the two agree.
pub(in crate::engine) fn lock_for_verb_referring(
&mut self,
writes: bool,
written: &[String],
refer: impl Fn(&Self) -> Vec<String>,
reads_workspace: impl Fn(&Self) -> bool,
) -> Result<(MemWriteGuard, Vec<crate::ops::WarningHint>), EngineError> {
let mut warnings = Vec::new();
let mut referred = refer(self);
for _ in 0..3 {
let mut locked = referred.clone();
locked.extend(written.iter().cloned());
let (guard, more) =
self.lock_for_verb(writes, &locked, written, |engine| reads_workspace(engine))?;
warnings.extend(more);
let under_lock = refer(self);
if !writes || under_lock.iter().all(|mem| referred.contains(mem)) {
return Ok((guard, warnings));
}
drop(guard);
for mem in under_lock {
if !referred.contains(&mem) {
referred.push(mem);
}
}
}
Err(EngineError::Backend(BackendError::Other(
"what this write refers to changed each time it had taken its write locks; nothing \
was written, send the call again"
.to_string(),
)))
}
/// Whether an edge of `rel_type` out of `mem` is guarded against
/// cycles: the relationship is acyclic, or it is in an acyclic set.
///
/// The name is judged in the form the verb will write it in: a
/// caller may send `depends_on`, and the lock is decided before the
/// verb normalises its arguments. A name that does not normalise is
/// refused by the verb, and no edge comes of it.
pub(in crate::engine) fn rel_type_is_acyclic(&self, mem: &str, rel_type: &str) -> bool {
let Ok(rel_type) = crate::entity::id::validate_rel_type(rel_type) else {
return false;
};
self.schemas.get(mem).is_some_and(|s| {
s.relationship_acyclic(&rel_type) || s.acyclic_set_containing(&rel_type).is_some()
})
}
/// The lock of a verb that names its mems: the roster is reconciled
/// first, so a mem attached to the workspace since this engine
/// started is mounted, and therefore locked, before the first write
/// into it. Returns what the reconcile reported; the caller takes
/// its view under the lock ([`Self::level_for_write`]) and looks
/// its mounts up after this call (a reconcile moves them).
pub fn lock_for_write<S: AsRef<str>>(
&mut self,
writes: bool,
mems: &[S],
) -> Result<(MemWriteGuard, Vec<crate::ops::WarningHint>), EngineError> {
let mut warnings = self.roster_before_locking();
let names: Vec<String> = mems.iter().map(|m| m.as_ref().to_string()).collect();
let (guard, settled) = self.lock_on_a_settled_roster(writes, |_| names.clone())?;
warnings.extend(settled);
Ok((guard, warnings))
}
/// The view a verb plans from, taken under its locks: the mems it
/// writes are reloaded (a deferred one is loaded: a write into a
/// mem needs the mem), and so is every other mem that is loaded.
/// The mems the write refers to are among its locks
/// ([`referred_mems`]), so what it checks of a relation target
/// (that it exists, what type it has) is read from a view taken
/// under the target's mem's lock and stays true until the commit. A
/// check that reads further than the mems a call names, the cycle
/// guard, does not run under this view: its verb takes the whole
/// workspace ([`Self::lock_for_verb`]). A deferred mem that is not
/// written stays unloaded; a target in it is checked against its
/// storage, under its lock.
pub(in crate::engine) fn level_for_write<S: AsRef<str>>(
&mut self,
written: &[S],
) -> Vec<crate::ops::WarningHint> {
let mut mems: Vec<String> = written.iter().map(|m| m.as_ref().to_string()).collect();
for m in &self.mounts {
if !m.deferred && !mems.contains(&m.mount.mem) {
mems.push(m.mount.mem.clone());
}
}
let mut warnings = Vec::new();
for mem in &mems {
warnings.extend(self.reload_if_stale_content_only(Some(mem)));
}
warnings
}
/// The mount a write into `mem` goes to. Looked up after
/// [`Self::lock_for_write`], whose roster reconcile can move the
/// mounts.
pub(in crate::engine) fn writable_mount_index(&self, mem: &str) -> Result<usize, EngineError> {
let idx = self
.mounts
.iter()
.position(|m| m.mount.mem == mem)
.ok_or_else(|| self.unknown_mem_error(mem))?;
if self.mounts[idx].mount.capability != crate::workspace::MountCapability::Write {
return Err(EngineError::ReadOnlyMount(mem.to_string()));
}
Ok(idx)
}
/// Reconcile the roster ahead of a lock. A mutation that runs
/// inside another one (a batch over the single-item path) holds its
/// locks already and has begun planning: the membership it planned
/// against stays as it is until the outer mutation is done.
fn roster_before_locking(&mut self) -> Vec<crate::ops::WarningHint> {
if COVERED.with(|c| c.borrow().is_empty()) {
self.roster_warnings()
} else {
Vec::new()
}
}
/// [`Self::lock_mems_for_write`] when `writes` is true; an empty
/// guard otherwise. A rehearsal (`dry_run`) commits nothing, so it
/// takes no lock and leaves the workspace byte-identical, lock files
/// included.
pub fn lock_mems_for_write_if<S: AsRef<str>>(
&mut self,
writes: bool,
mems: &[S],
) -> Result<MemWriteGuard, EngineError> {
if writes {
self.lock_mems_for_write(mems)
} else {
Ok(MemWriteGuard::empty())
}
}
/// Take the cross-process write lock of every named mem, in sorted
/// order (so two multi-mem writers can never deadlock), and hold it
/// until the returned guard drops. Call it before the
/// reload-before-operation probe of a mutation and keep the guard
/// alive past the commit. Mems that are not mounted are skipped:
/// the mutation's own lookup reports an unknown mem with its usual
/// error. A backend with no shared state takes no OS lock and is
/// still covered for the staged-write discard.
///
/// What an earlier mutation left staged on a mem when its discard
/// failed is discarded here, once the mem's lock is held and before
/// the guard covers its backend: a backend whose discard keeps
/// failing can be replaced by a fresh one only while no mutation
/// holds it, and a write sent again right after the failed one
/// would otherwise hold it already and be refused as stale.
pub fn lock_mems_for_write<S: AsRef<str>>(
&mut self,
mems: &[S],
) -> Result<MemWriteGuard, EngineError> {
let mut locks: Vec<(String, Option<PathBuf>, usize, Option<String>)> = mems
.iter()
.filter_map(|name| {
let m = self.mounts.iter().find(|m| m.mount.mem == name.as_ref())?;
// Named for the refusal below when it is mounted
// read-only.
let read_only = (m.mount.capability != crate::workspace::MountCapability::Write)
.then(|| m.mount.mem.clone());
Some((
m.mount.mem.clone(),
m.backend.write_lock_path(),
backend_key(&m.backend),
read_only,
))
})
.collect();
// Sorted by lock path so two multi-mem writers take their locks
// in one order; pathless backends take no lock and sort first.
locks.sort_by(|a, b| a.1.cmp(&b.1));
locks.dedup_by(|a, b| a.2 == b.2);
let mut guard = MemWriteGuard::empty();
// How long a write waits for one mem's lock. A holder that never
// lets go would otherwise park every later writer of that mem
// without bound; past the limit the write refuses, and the
// guard gives back whatever it had taken on its way here.
let limit = self.settings().mutations.write_lock_timeout();
for (mem, path, _, read_only) in locks {
if let Some(path) = &path {
let started = std::time::Instant::now();
match (path_lock::acquire_within(path, limit), read_only) {
(Ok(true), _) => {}
(Ok(false), _) => {
return Err(EngineError::WriteLockTimeout {
mem,
waited_ms: started.elapsed().as_millis() as u64,
});
}
// A mem mounted read-only may sit where this
// process cannot open its lock file. The operation
// asked for the lock because it reads the mem and
// needs it to hold still, and another process may
// write it; without the lock that cannot be had,
// so the operation is refused and names the mem.
(Err(e), Some(mem)) => {
return Err(EngineError::StaleView {
mem,
cause: super::StaleCause::LockUnavailable,
detail: format!(
"the mem's write lock cannot be taken here ({e}), so its content \
cannot be held still for an operation that reads it"
),
});
}
(Err(e), None) => return Err(EngineError::Backend(BackendError::Io(e))),
}
}
// A failure stays recorded on the mount, and the reload
// under this lock refuses the write naming the mem.
let _ = self.discard_strays(&mem);
let Some(m) = self.mounts.iter().find(|m| m.mount.mem == mem) else {
if let Some(path) = &path {
path_lock::release(path);
}
continue;
};
guard.cover(Held {
path,
backend: Arc::clone(&m.backend),
strays: Arc::clone(&m.strays),
});
}
Ok(guard)
}
}
/// The mems a create or an update refers to besides the one it writes:
/// the mems of its declared relation targets and of the wiki-links in
/// the text the call brings. The write takes their locks with its own.
/// A delete, a rename or a retype of a target holds the target's mem,
/// so a reference cannot be written while one is under way, whenever
/// the writing mem was attached; the write waits, reloads, and is
/// computed against the target as that verb left it.
pub(in crate::engine) fn referred_mems<'a>(
own: &str,
targets: impl IntoIterator<Item = &'a crate::entity::EntityId>,
texts: impl IntoIterator<Item = &'a str>,
) -> Vec<String> {
let mut mems: Vec<String> = targets.into_iter().map(|t| t.mem().to_string()).collect();
for text in texts {
// Cheap gate: a text without a wiki-link names no mem.
if text.contains("[[") {
mems.extend(
crate::entity::parser::extract_inline_links_lenient(text, own)
.iter()
.map(|t| t.mem().to_string()),
);
}
}
mems.retain(|m| !m.is_empty() && m != own);
mems.sort();
mems.dedup();
mems
}