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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
//! The transactor as a process: multi-database state, durable naming,
//! lease acquisition/renewal, background indexing, tx-report fan-out, and
//! high-availability standby takeover.
use std::collections::{BTreeSet, HashMap};
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use corium_core::{Datom, IndexOrder, KeywordInterner, Schema};
use corium_db::{Db, Idents};
use corium_log::{LogError, TransactionLog, TxRecord};
use corium_protocol::codec::{self, CodecError};
use corium_protocol::pb;
use corium_protocol::schemaform::{SchemaFormError, schema_from_edn};
use corium_protocol::txforms::{TxFormError, tx_items_from_edn};
use corium_query::edn::Edn;
use corium_store::{
BlobId, BlobStore, RootStore, StoreError, decode_index_manifest, decode_segment_keys,
is_index_manifest, mark_and_sweep_retained, meta_root_name,
};
use thiserror::Error;
use tokio::sync::{broadcast, watch};
use tracing::Instrument;
use crate::backend::{LogBackend, NodeStore, StoreSpec};
use crate::lease::{self, Lease, LeaseError};
use crate::metrics::Metrics;
use crate::{DbRoot, EmbeddedTransactor, TransactError, db_root_name};
/// Expands user database-function invocations in boundary EDN transaction
/// forms before native conversion. Implemented by `corium-cljrs` (the
/// sandboxed Clojurust host, ADR-0008) and injected by the process wiring;
/// the transactor itself stays free of cljrs dependencies.
pub trait TxFnExpander: Send + Sync {
/// Rewrites `forms` with every `[:my/fn arg…]` invocation replaced by
/// the function's returned tx-data (recursively).
///
/// # Errors
/// Returns a display message when a function is missing, rejected by
/// the sandbox, fails, or exceeds its budget; the transaction aborts.
fn expand(&self, db: &Db, forms: Vec<Edn>) -> Result<Vec<Edn>, String>;
}
/// Node process configuration.
#[derive(Clone)]
pub struct NodeConfig {
/// Storage-service backend for blobs and roots (`mem`, `fs`, or Turso).
pub store: StoreSpec,
/// Data directory holding the filesystem blob/root store (for the `fs`
/// backend) and the transaction logs (for every non-`mem` backend).
pub data_dir: PathBuf,
/// Stable owner identity for lease records.
pub owner: String,
/// Lease time-to-live in milliseconds.
pub lease_ttl_ms: i64,
/// How long to wait for a held lease to expire before giving up.
pub lease_wait_ms: i64,
/// High-availability mode: when another owner holds a database's lease,
/// stand by and take over on expiry instead of failing startup, and on
/// depose return to standby instead of shutting the process down.
pub ha: bool,
/// Client endpoint advertised in the lease for peer lease-holder
/// rediscovery (e.g. `http://transactor-a:4334`).
pub advertise: Option<String>,
/// Interval between background index publications.
pub index_interval: Duration,
/// Minimum wait before the next index publication, as a multiple of the
/// previous publication's duration. Publications currently rewrite every
/// index in full, so this stretches the effective interval as the
/// database grows, bounding the share of time and storage bandwidth
/// spent republishing to at most `1/(1+n)`; 0 disables the backoff.
pub index_backoff: u32,
/// Pending log-tail growth (recorded datoms) below which a due
/// publication is deferred, so trickle writes coalesce instead of
/// rewriting every index; 0 publishes any pending work.
pub index_tail_threshold: u64,
/// Longest a pending below-threshold tail may defer publication.
pub index_tail_deadline: Duration,
/// Interval between heartbeats on subscription streams.
pub heartbeat_interval: Duration,
/// Interval between scheduled garbage-collection duties; `None` disables it.
pub gc_interval: Option<Duration>,
/// Minimum age of an unreachable blob before scheduled/manual online GC.
pub gc_retention: Duration,
/// Optional database-function expander (`:db/fn` support).
pub tx_fn_expander: Option<Arc<dyn TxFnExpander>>,
}
impl std::fmt::Debug for NodeConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("NodeConfig")
.field("store", &self.store)
.field("data_dir", &self.data_dir)
.field("owner", &self.owner)
.field("lease_ttl_ms", &self.lease_ttl_ms)
.field("lease_wait_ms", &self.lease_wait_ms)
.field("ha", &self.ha)
.field("advertise", &self.advertise)
.field("index_interval", &self.index_interval)
.field("index_backoff", &self.index_backoff)
.field("index_tail_threshold", &self.index_tail_threshold)
.field("index_tail_deadline", &self.index_tail_deadline)
.field("heartbeat_interval", &self.heartbeat_interval)
.field("gc_interval", &self.gc_interval)
.field("gc_retention", &self.gc_retention)
.field("tx_fn_expander", &self.tx_fn_expander.is_some())
.finish()
}
}
impl NodeConfig {
/// Sensible defaults for a data directory.
#[must_use]
pub fn new(data_dir: PathBuf) -> Self {
Self {
store: StoreSpec::Fs,
data_dir,
owner: format!(
"transactor-{}",
std::env::var("HOSTNAME").unwrap_or_else(|_| "local".into())
),
lease_ttl_ms: 5_000,
lease_wait_ms: 15_000,
ha: false,
advertise: None,
index_interval: Duration::from_secs(5),
index_backoff: 4,
index_tail_threshold: 0,
index_tail_deadline: Duration::from_secs(60),
heartbeat_interval: Duration::from_secs(10),
gc_interval: Some(Duration::from_secs(60 * 60)),
gc_retention: Duration::from_secs(72 * 60 * 60),
tx_fn_expander: None,
}
}
}
/// Pacing policy for one database's background indexing job.
///
/// A publication is due when the adaptive floor has elapsed — the base
/// interval stretched by a multiple of the previous publication's duration,
/// which bounds the indexing duty cycle as full republication gets slower —
/// and the pending log tail is either large enough to be worth rewriting
/// every index or old enough that deferring it further would leave cold
/// readers and backups too far behind.
///
/// Every database starts from the node's [`NodeConfig`] pacing fields; the
/// catalog `SetIndexPolicy` RPC (or
/// [`TransactorNode::set_index_policy`]) overrides it at runtime.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct IndexPolicy {
/// Base interval between publications ([`NodeConfig::index_interval`]).
pub interval: Duration,
/// Duty-cycle multiplier on the previous publication's duration
/// ([`NodeConfig::index_backoff`]).
pub backoff: u32,
/// Pending-datom count below which a due publication is deferred
/// ([`NodeConfig::index_tail_threshold`]).
pub tail_threshold: u64,
/// Longest a below-threshold tail may defer publication
/// ([`NodeConfig::index_tail_deadline`]).
pub tail_deadline: Duration,
}
/// Partial [`IndexPolicy`] override; `None` fields are left unchanged.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct IndexPolicyUpdate {
/// New base interval, when set.
pub interval: Option<Duration>,
/// New duty-cycle multiplier, when set.
pub backoff: Option<u32>,
/// New pending-datom threshold, when set.
pub tail_threshold: Option<u64>,
/// New deferral deadline, when set.
pub tail_deadline: Option<Duration>,
}
impl IndexPolicy {
fn from_config(config: &NodeConfig) -> Self {
Self {
interval: config.index_interval,
backoff: config.index_backoff,
tail_threshold: config.index_tail_threshold,
tail_deadline: config.index_tail_deadline,
}
}
fn apply(&mut self, update: IndexPolicyUpdate) {
if let Some(interval) = update.interval {
self.interval = interval;
}
if let Some(backoff) = update.backoff {
self.backoff = backoff;
}
if let Some(tail_threshold) = update.tail_threshold {
self.tail_threshold = tail_threshold;
}
if let Some(tail_deadline) = update.tail_deadline {
self.tail_deadline = tail_deadline;
}
}
/// Decides whether pending work should publish now. `since_publish` is
/// the time since the last publication finished (or the job started),
/// `last_duration` how long it took (zero before the first), and
/// `pending` the recorded datoms appended since it — `None` until a
/// publication in this process establishes a baseline, which publishes
/// at base pacing (covers restarting with an unindexed backlog).
fn due(&self, since_publish: Duration, last_duration: Duration, pending: Option<u64>) -> bool {
let floor = self
.interval
.max(last_duration.saturating_mul(self.backoff));
if since_publish < floor {
return false;
}
match pending {
Some(pending) if pending < self.tail_threshold => since_publish >= self.tail_deadline,
_ => true,
}
}
}
/// Node operation failure.
#[derive(Debug, Error)]
pub enum NodeError {
/// Named database does not exist.
#[error("unknown database {0:?}")]
UnknownDb(String),
/// Database name is not storable.
#[error("invalid database name {0:?}")]
InvalidName(String),
/// Database root uses a storage format newer than this binary.
#[error("storage format {found} is newer than supported format {supported}")]
UnsupportedFormat {
/// Version found in the root.
found: u32,
/// Newest version understood by this binary.
supported: u32,
},
/// This node no longer holds the write lease.
#[error("deposed: write lease for {0:?} is held elsewhere")]
Deposed(String),
/// This node is a warm standby for the database; the lease holder
/// serves it.
#[error("standby for {db:?}: lease held by {owner} at {endpoint:?}")]
Standby {
/// Database name.
db: String,
/// Current lease owner id (empty when unknown).
owner: String,
/// Owner's advertised client endpoint (empty when unadvertised).
endpoint: String,
},
/// Payload failed to decode.
#[error(transparent)]
Codec(#[from] CodecError),
/// Transaction forms failed to convert.
#[error(transparent)]
TxForm(#[from] TxFormError),
/// Schema forms failed to convert.
#[error(transparent)]
SchemaForm(#[from] SchemaFormError),
/// Transaction pipeline failure.
#[error(transparent)]
Transact(#[from] TransactError),
/// Store failure.
#[error(transparent)]
Store(#[from] StoreError),
/// Log failure.
#[error(transparent)]
Log(#[from] LogError),
/// Lease failure.
#[error(transparent)]
Lease(#[from] LeaseError),
/// Malformed request.
#[error("bad request: {0}")]
BadRequest(String),
}
struct Naming {
schema: Schema,
idents: Idents,
interner: KeywordInterner,
}
/// Per-database state hosted by a node.
pub struct DbState {
name: String,
transactor: EmbeddedTransactor,
log: Arc<dyn TransactionLog>,
naming: Mutex<Naming>,
commit: tokio::sync::Mutex<()>,
broadcast: broadcast::Sender<pb::subscribe_item::Item>,
basis: watch::Sender<u64>,
index_basis: AtomicU64,
index_policy: Mutex<IndexPolicy>,
held_lease: Mutex<Lease>,
deposed: AtomicBool,
}
impl DbState {
/// Database name.
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
/// Current database value.
#[must_use]
pub fn db(&self) -> Db {
self.transactor.db()
}
/// Watch channel following the commit basis.
#[must_use]
pub fn basis_watch(&self) -> watch::Receiver<u64> {
self.basis.subscribe()
}
/// Subscribes to live stream items (reports, index announcements,
/// heartbeats).
#[must_use]
pub fn stream_items(&self) -> broadcast::Receiver<pb::subscribe_item::Item> {
self.broadcast.subscribe()
}
/// Basis of the newest published index root.
#[must_use]
pub fn index_basis(&self) -> u64 {
self.index_basis.load(Ordering::Acquire)
}
/// The indexing pacing policy currently in effect for this database.
#[must_use]
pub fn index_policy(&self) -> IndexPolicy {
*self
.index_policy
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
/// Currently held lease record.
#[must_use]
pub fn lease(&self) -> Lease {
self.held_lease
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.clone()
}
/// Encoded schema/ident handshake payload plus a consistent basis and
/// interner snapshot for backfill encoding.
#[must_use]
pub fn handshake_snapshot(&self) -> (Vec<u8>, KeywordInterner) {
let naming = self
.naming
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
(
codec::encode_schema(&naming.schema, &naming.idents),
naming.interner.clone(),
)
}
/// Reads committed records in `[start, end)` from the durable log.
///
/// # Errors
/// Returns an error when the log cannot be read.
pub async fn tx_range(&self, start: u64, end: Option<u64>) -> Result<Vec<TxRecord>, NodeError> {
Ok(self.log.tx_range_async(start, end).await?)
}
/// Verifies this node still owns the write lease (identity check on
/// the root record; expiry changes from renewals do not matter).
async fn check_lease(&self, store: &dyn RootStore) -> Result<Lease, NodeError> {
if self.deposed.load(Ordering::Acquire) {
return Err(NodeError::Deposed(self.name.clone()));
}
let held = self.lease();
match lease::verify(store, &self.name, &held).await {
Ok(()) => Ok(held),
Err(LeaseError::Lost) => {
self.deposed.store(true, Ordering::Release);
Err(NodeError::Deposed(self.name.clone()))
}
Err(error) => Err(error.into()),
}
}
}
/// A running transactor node hosting every database under one data directory.
pub struct TransactorNode {
config: NodeConfig,
store: Arc<NodeStore>,
log_backend: LogBackend,
dbs: std::sync::RwLock<HashMap<String, Arc<DbState>>>,
/// Databases this node is standing by for (HA mode): the lease is held
/// elsewhere and the standby poller attempts takeover on expiry.
standby: std::sync::RwLock<BTreeSet<String>>,
gc_lock: tokio::sync::Mutex<()>,
/// Serializes forks: two forks to the same target must not interleave
/// appends into one target log.
fork_lock: tokio::sync::Mutex<()>,
metrics: Metrics,
shutdown: watch::Sender<Option<String>>,
}
fn now_unix_ms() -> i64 {
i64::try_from(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis(),
)
.unwrap_or(i64::MAX)
}
fn valid_db_name(name: &str) -> bool {
!name.is_empty()
&& name.len() <= 128
&& name
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || byte == b'-' || byte == b'_')
}
impl TransactorNode {
/// Opens a node over `config.data_dir`, recovering every database found
/// there (acquiring its lease, waiting out held leases up to the
/// configured bound).
///
/// # Errors
/// Returns an error when the store cannot be opened or a database cannot
/// be recovered.
pub async fn open(config: NodeConfig) -> Result<Arc<Self>, NodeError> {
let store = Arc::new(NodeStore::open(&config.store, &config.data_dir).await?);
let log_backend = LogBackend::for_spec(&config.store, &config.data_dir, Arc::clone(&store));
let node = Arc::new(Self {
config,
store,
log_backend,
dbs: std::sync::RwLock::new(HashMap::new()),
standby: std::sync::RwLock::new(BTreeSet::new()),
gc_lock: tokio::sync::Mutex::new(()),
fork_lock: tokio::sync::Mutex::new(()),
metrics: Metrics::default(),
shutdown: watch::channel(None).0,
});
let names: Vec<String> = node
.store
.list_roots("meta:")
.await?
.into_iter()
.filter_map(|root| root.strip_prefix("meta:").map(str::to_owned))
.collect();
for name in names {
match node.open_db(&name).await {
Ok(state) => {
node.dbs
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.insert(name, state);
}
Err(NodeError::Lease(LeaseError::Held { owner, .. })) if node.config.ha => {
tracing::info!(db = %name, %owner, "standing by; lease held elsewhere");
node.standby
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.insert(name);
}
Err(error) => return Err(error),
}
}
node.spawn_standby_poller();
node.spawn_scheduled_gc();
Ok(node)
}
/// The node's storage-service backend (blobs + roots).
#[must_use]
pub fn store(&self) -> &Arc<NodeStore> {
&self.store
}
/// Node configuration.
#[must_use]
pub fn config(&self) -> &NodeConfig {
&self.config
}
/// Process observability counters.
#[must_use]
pub const fn metrics(&self) -> &Metrics {
&self.metrics
}
fn spawn_scheduled_gc(self: &Arc<Self>) {
let Some(interval) = self.config.gc_interval else {
return;
};
let Ok(runtime) = tokio::runtime::Handle::try_current() else {
// Embedded callers may construct an empty catalog before they
// enter a runtime. Process wiring opens nodes inside Tokio.
return;
};
let node = Arc::clone(self);
runtime.spawn(async move {
let mut ticker = tokio::time::interval(interval);
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
// `interval` ticks immediately; scheduled duties should wait a full interval.
ticker.tick().await;
loop {
ticker.tick().await;
if let Err(error) = node.gc_deleted().await {
tracing::warn!(%error, "scheduled garbage collection failed");
}
}
});
}
/// Watch channel that reports a shutdown reason when the node deposes.
#[must_use]
pub fn shutdown_watch(&self) -> watch::Receiver<Option<String>> {
self.shutdown.subscribe()
}
/// Deposes a hosted database. In HA mode the database returns to
/// standby (the poller re-attempts takeover); otherwise the whole
/// process shuts down and a supervisor restart re-acquires or waits.
fn depose(&self, state: &DbState, reason: &str) {
state.deposed.store(true, Ordering::Release);
if self.config.ha {
tracing::warn!(db = %state.name, reason, "deposed; returning to standby");
self.dbs
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.remove(&state.name);
self.standby
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.insert(state.name.clone());
} else {
let _ = self
.shutdown
.send(Some(format!("database {:?}: {reason}", state.name)));
}
}
fn advertised(&self) -> &str {
self.config.advertise.as_deref().unwrap_or("")
}
/// Acquires the lease for `name`. In HA mode a held lease surfaces
/// immediately (the caller stands by); otherwise startup waits it out
/// up to the configured bound.
async fn acquire_lease(&self, name: &str) -> Result<Lease, NodeError> {
let deadline = now_unix_ms() + self.config.lease_wait_ms;
loop {
match lease::acquire(
self.store.as_ref(),
name,
&self.config.owner,
self.advertised(),
self.config.lease_ttl_ms,
now_unix_ms(),
)
.await
{
Ok(held) => return Ok(held),
Err(LeaseError::Held { .. }) if !self.config.ha && now_unix_ms() < deadline => {
tokio::time::sleep(Duration::from_millis(200)).await;
}
Err(error) => return Err(error.into()),
}
}
}
async fn open_db(self: &Arc<Self>, name: &str) -> Result<Arc<DbState>, NodeError> {
let meta = self
.store
.get_root(&meta_root_name(name))
.await?
.ok_or_else(|| NodeError::UnknownDb(name.to_owned()))?;
let (schema, idents, interner) = codec::decode_metadata(&meta)?;
let root_name = db_root_name(name);
let current = self
.store
.get_root(&root_name)
.await?
.as_deref()
.and_then(DbRoot::decode);
if let Some(root) = ¤t
&& root.format_version > corium_store::FORMAT_VERSION
{
return Err(NodeError::UnsupportedFormat {
found: root.format_version,
supported: corium_store::FORMAT_VERSION,
});
}
// Acquisition rewrites the root record under our lease version, so
// it doubles as the fence bump: a deposed writer's pending root CAS
// now has stale expected bytes and must fail. It also preserves the
// published snapshot's recovery hints, so the root we re-read below
// carries everything index-root recovery needs.
let held = self.acquire_lease(name).await?;
// The log tail replay below happens strictly after the fence, so it
// observes every record a previous owner could ever have acked.
let log = self.log_backend.open(name, held.version).await?;
let post_fence = self
.store
.get_root(&root_name)
.await?
.as_deref()
.and_then(DbRoot::decode);
let transactor = self
.recover_transactor(name, &schema, &idents, &interner, post_fence.as_ref(), &log)
.await?;
let basis_t = transactor.db().basis_t();
let index_basis = post_fence.map_or(0, |root| root.index_basis_t);
let state = Arc::new(DbState {
name: name.to_owned(),
transactor,
log,
naming: Mutex::new(Naming {
schema,
idents,
interner,
}),
commit: tokio::sync::Mutex::new(()),
broadcast: broadcast::channel(1024).0,
basis: watch::channel(basis_t).0,
index_basis: AtomicU64::new(index_basis),
index_policy: Mutex::new(IndexPolicy::from_config(&self.config)),
held_lease: Mutex::new(held),
deposed: AtomicBool::new(false),
});
self.spawn_maintenance(&state);
Ok(state)
}
/// Builds the recovered transactor for `open_db`.
///
/// When the post-fence root publishes a current snapshot with recovery
/// hints, recovers from the index root plus the log tail — open time
/// proportional to the tail, not the whole history. Any missing hint
/// (a pre-recovery root, or a bare fence bump with no snapshot) or a
/// failure materializing the snapshot falls back to full-log replay,
/// which is always correct because the log is the source of truth.
async fn recover_transactor(
&self,
name: &str,
schema: &Schema,
idents: &Idents,
interner: &KeywordInterner,
root: Option<&DbRoot>,
log: &Arc<dyn TransactionLog>,
) -> Result<EmbeddedTransactor, NodeError> {
// `next_entity_id == 0` is the "no hint" sentinel (see DbRoot); it and
// an absent snapshot both rule out the tail-only path.
if let Some(root) = root
&& let Some(roots) = &root.roots
&& root.next_entity_id != 0
{
match self
.load_current_snapshot(
root,
&roots[IndexOrder::Eavt as usize],
schema,
idents,
interner,
)
.await
{
Ok(snapshot) => {
return Ok(EmbeddedTransactor::recover_from_snapshot_async(
snapshot,
root.next_entity_id,
root.last_tx_instant,
Arc::clone(log),
)
.await?);
}
Err(error) => {
tracing::warn!(
db = %name,
%error,
"index-root recovery failed; falling back to full-log replay"
);
}
}
}
let base = Db::new(schema.clone()).with_naming(idents.clone(), interner.clone());
Ok(EmbeddedTransactor::recover_from_async(base, Arc::clone(log)).await?)
}
/// Materializes the current database value at a published index root from
/// its EAVT snapshot — the transactor-side counterpart of the peer's
/// bootstrap (`corium-peer`'s `load_current_snapshot`). Only current
/// facts are reconstructed; the log tail carries everything since.
async fn load_current_snapshot(
&self,
root: &DbRoot,
eavt: &BlobId,
schema: &Schema,
idents: &Idents,
interner: &KeywordInterner,
) -> Result<Db, StoreError> {
let datoms = self
.load_index_keys(eavt)
.await?
.into_iter()
.map(|key| Datom::from_key(IndexOrder::Eavt, &key))
.collect::<Result<Vec<_>, _>>()
.map_err(|error| StoreError::Io(std::io::Error::other(error.to_string())))?;
Ok(Db::from_current_snapshot(
root.index_basis_t,
schema.clone(),
idents.clone(),
interner.clone(),
datoms,
))
}
/// Reads one covering index's sorted key stream from the blob store: a
/// format-3 manifest's chunks in order, or a pre-format-3 flat blob.
async fn load_index_keys(&self, id: &BlobId) -> Result<Vec<Vec<u8>>, StoreError> {
let blob = self
.store
.get(id)
.await?
.ok_or_else(|| StoreError::MissingBlob(id.clone()))?;
if !is_index_manifest(&blob) {
return decode_segment_keys(&blob);
}
let mut keys = Vec::new();
for child in decode_index_manifest(&blob)? {
let chunk = self
.store
.get(&child)
.await?
.ok_or_else(|| StoreError::MissingBlob(child.clone()))?;
keys.extend(decode_segment_keys(&chunk)?);
}
Ok(keys)
}
/// HA standby duty: at the lease-renewal cadence, rediscover databases
/// (including ones created on the active after this process started)
/// and attempt takeover of any whose lease has lapsed. Takeover is
/// ordinary startup — acquire (which fences), replay the log tail,
/// serve — per the crash-only design.
fn spawn_standby_poller(self: &Arc<Self>) {
if !self.config.ha {
return;
}
let Ok(runtime) = tokio::runtime::Handle::try_current() else {
return;
};
let ttl = self.config.lease_ttl_ms;
let poll_every = Duration::from_millis(u64::try_from(ttl / 3).unwrap_or(1).max(50));
let node = Arc::clone(self);
runtime.spawn(async move {
let mut ticker = tokio::time::interval(poll_every);
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
loop {
ticker.tick().await;
if let Err(error) = node.standby_scan().await {
tracing::warn!(%error, "standby scan failed");
}
}
});
}
/// One standby pass: refresh the standby set from the catalog and try
/// to take over lapsed leases.
async fn standby_scan(self: &Arc<Self>) -> Result<(), NodeError> {
let names: Vec<String> = self
.store
.list_roots("meta:")
.await?
.into_iter()
.filter_map(|root| root.strip_prefix("meta:").map(str::to_owned))
.collect();
{
let mut standby = self
.standby
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner);
standby.retain(|name| names.contains(name));
}
for name in names {
if self
.dbs
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.contains_key(&name)
{
continue;
}
match self.open_db(&name).await {
Ok(state) => {
tracing::info!(db = %name, owner = %self.config.owner, "standby took over write lease");
self.standby
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.remove(&name);
self.dbs
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.insert(name, state);
}
Err(NodeError::Lease(LeaseError::Held { .. })) => {
self.standby
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.insert(name);
}
Err(error) => {
tracing::warn!(db = %name, %error, "standby takeover attempt failed");
}
}
}
Ok(())
}
fn spawn_maintenance(self: &Arc<Self>, state: &Arc<DbState>) {
let ttl = self.config.lease_ttl_ms;
let renew_every = Duration::from_millis(u64::try_from(ttl / 3).unwrap_or(1).max(50));
// Lease renewal.
let node = Arc::clone(self);
let db = Arc::clone(state);
tokio::spawn(async move {
let mut ticker = tokio::time::interval(renew_every);
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
loop {
ticker.tick().await;
if db.deposed.load(Ordering::Acquire) {
return;
}
// Serialize the root update and local held-lease update with
// transaction lease checks so they cannot observe different
// renewal generations and falsely depose this node.
let _commit = db.commit.lock().await;
let held = db.lease();
let name = db.name.clone();
let renewed =
lease::renew(node.store.as_ref(), &name, &held, ttl, now_unix_ms()).await;
match renewed {
Ok(renewed) => {
*db.held_lease
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner) = renewed;
}
Err(LeaseError::Lost) => {
node.depose(&db, "write lease lost");
return;
}
Err(_) => {}
}
}
});
self.spawn_indexing(state);
// Heartbeats.
let node = Arc::clone(self);
let db = Arc::clone(state);
tokio::spawn(async move {
let mut ticker = tokio::time::interval(node.config.heartbeat_interval);
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
loop {
ticker.tick().await;
if db.deposed.load(Ordering::Acquire) {
return;
}
let _ = db
.broadcast
.send(pb::subscribe_item::Item::Heartbeat(pb::Heartbeat {
basis_t: db.db().basis_t(),
}));
}
});
}
/// Spawns the background indexing job, paced by the database's
/// [`IndexPolicy`] (re-read every iteration so runtime overrides apply
/// within a poll interval).
fn spawn_indexing(self: &Arc<Self>, state: &Arc<DbState>) {
// How often the job re-checks work and policy when the configured
// interval is longer; runtime policy changes and pending work are
// never noticed later than this.
const POLICY_POLL: Duration = Duration::from_secs(1);
let node = Arc::clone(self);
let db = Arc::clone(state);
tokio::spawn(async move {
let mut published_at = Instant::now();
let mut last_duration = Duration::ZERO;
let mut published_len: Option<u64> = None;
loop {
let policy = db.index_policy();
tokio::time::sleep(policy.interval.min(POLICY_POLL)).await;
if db.deposed.load(Ordering::Acquire) {
return;
}
let snapshot = db.db();
if snapshot.basis_t() <= db.index_basis() {
continue;
}
let recorded_len = u64::try_from(snapshot.recorded_len()).unwrap_or(u64::MAX);
let pending = published_len.map(|len| recorded_len.saturating_sub(len));
if !policy.due(published_at.elapsed(), last_duration, pending) {
continue;
}
match node.publish_db_indexes(&db).await {
Ok((_, duration)) => {
last_duration = duration;
// publish_db_indexes snapshots after this loop did,
// so the covered length is at least recorded_len; the
// underestimate only makes the next tail look bigger.
published_len = Some(recorded_len);
}
Err(NodeError::Deposed(_)) => return,
Err(_) => {}
}
published_at = Instant::now();
}
});
}
/// Publishes `db`'s covering indexes now, returning the published index
/// basis and how long the publication took. Serialized with garbage
/// collection; deposes the database when the root is fenced by a newer
/// lease.
async fn publish_db_indexes(&self, db: &Arc<DbState>) -> Result<(u64, Duration), NodeError> {
let _gc = self.gc_lock.lock().await;
let version = db.lease().version;
let root_name = db_root_name(&db.name);
let started = Instant::now();
let published = db
.transactor
.publish_indexes(self.store.as_ref(), &root_name, version)
.await;
let duration = started.elapsed();
self.metrics.record_index(duration);
match published {
Ok(root) => {
tracing::debug!(db = %db.name, index_basis_t = root.index_basis_t, "published indexes");
db.index_basis.store(root.index_basis_t, Ordering::Release);
let _ = db
.broadcast
.send(pb::subscribe_item::Item::IndexBasis(pb::IndexBasis {
index_basis_t: root.index_basis_t,
}));
Ok((root.index_basis_t, duration))
}
Err(TransactError::Deposed { .. }) => {
self.depose(db, "database root fenced by a newer lease");
Err(NodeError::Deposed(db.name.clone()))
}
Err(error) => Err(error.into()),
}
}
/// Publishes indexes for `name` immediately, bypassing the pacing
/// policy (the catalog `RequestIndex` RPC). Returns the resulting index
/// basis; when the published indexes already cover every committed
/// transaction, returns the current index basis without publishing.
///
/// # Errors
/// Returns [`NodeError`] when the database is unknown, this node is
/// deposed or standing by, or publication fails.
pub async fn request_index(&self, name: &str) -> Result<u64, NodeError> {
let state = self.db_state(name).await?;
if state.db().basis_t() <= state.index_basis() {
return Ok(state.index_basis());
}
self.publish_db_indexes(&state)
.await
.map(|(index_basis_t, _)| index_basis_t)
}
/// Applies per-database indexing-policy overrides at runtime, returning
/// the policy now in effect. An empty update reads the current policy.
///
/// # Errors
/// Returns [`NodeError`] when the database is unknown or served
/// elsewhere.
pub async fn set_index_policy(
&self,
name: &str,
update: IndexPolicyUpdate,
) -> Result<IndexPolicy, NodeError> {
let state = self.db_state(name).await?;
let mut policy = state
.index_policy
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
policy.apply(update);
Ok(*policy)
}
/// Looks up a hosted database.
///
/// # Errors
/// Returns [`NodeError::Standby`] when this HA node is standing by for
/// the database, [`NodeError::UnknownDb`] when absent.
pub async fn db_state(&self, name: &str) -> Result<Arc<DbState>, NodeError> {
if let Some(state) = self
.dbs
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.get(name)
.cloned()
{
return Ok(state);
}
if self.config.ha
&& self
.standby
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.contains(name)
{
let root = self
.store
.get_root(&db_root_name(name))
.await?
.as_deref()
.and_then(DbRoot::decode);
return Err(NodeError::Standby {
db: name.to_owned(),
owner: root.as_ref().map(|r| r.owner.clone()).unwrap_or_default(),
endpoint: root.map(|r| r.owner_endpoint).unwrap_or_default(),
});
}
Err(NodeError::UnknownDb(name.to_owned()))
}
/// Databases this node currently stands by for (HA mode).
#[must_use]
pub fn standby_dbs(&self) -> Vec<String> {
self.standby
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.iter()
.cloned()
.collect()
}
/// Creates a database with the supplied EDN schema forms; returns
/// `false` when it already exists.
///
/// # Errors
/// Returns an error for invalid names/schema or store failures.
pub async fn create_db(
self: &Arc<Self>,
name: &str,
schema_edn: &[u8],
) -> Result<bool, NodeError> {
if !valid_db_name(name) {
return Err(NodeError::InvalidName(name.to_owned()));
}
if self
.dbs
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.contains_key(name)
{
return Ok(false);
}
let forms = match codec::decode_edn(schema_edn)? {
Edn::Vector(items) | Edn::List(items) => items,
Edn::Nil => Vec::new(),
other => {
return Err(NodeError::BadRequest(format!(
"schema must be a vector of attribute maps, got {other}"
)));
}
};
let (schema, idents) = schema_from_edn(&forms)?;
let meta = codec::encode_metadata(&schema, &idents, &KeywordInterner::default());
match self
.store
.cas_root(&meta_root_name(name), None, &meta)
.await
{
Ok(()) => {}
Err(StoreError::CasFailed { .. }) => return Ok(false),
Err(error) => return Err(error.into()),
}
let state = self.open_db(name).await?;
self.dbs
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.insert(name.to_owned(), state);
Ok(true)
}
/// Forks `source` into a new database `target` whose state duplicates
/// the source as of transaction `as_of_t` (`0` forks at the current
/// basis). Only the log prefix is copied; the target replays it and
/// publishes its own indexes, while blob segments dedupe by content
/// address. Returns the fork's basis, or `None` when `target` already
/// exists.
///
/// # Errors
/// Returns an error for an invalid target name, an unknown source, an
/// `as_of_t` ahead of the source's basis, or store/log failures.
pub async fn fork_db(
self: &Arc<Self>,
source: &str,
target: &str,
as_of_t: u64,
) -> Result<Option<u64>, NodeError> {
if !valid_db_name(target) {
return Err(NodeError::InvalidName(target.to_owned()));
}
if source == target {
return Err(NodeError::BadRequest(
"fork target must differ from the source".into(),
));
}
let state = self.db_state(source).await?;
let basis = state.db().basis_t();
let t = if as_of_t == 0 { basis } else { as_of_t };
if t > basis {
return Err(NodeError::BadRequest(format!(
"as-of t {t} is ahead of {source:?} basis {basis}"
)));
}
let _guard = self.fork_lock.lock().await;
if self
.dbs
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.contains_key(target)
|| self
.store
.get_root(&meta_root_name(target))
.await?
.is_some()
|| self.log_backend.exists(target).await
{
return Ok(None);
}
// Capture the records before the metadata: meta is made durable
// before any record that references it, so a meta read afterwards is
// always a sufficient decode dictionary for the captured prefix.
// Transaction numbers are contiguous from 1, so the prefix through
// `t` is exactly the source's state at that basis.
let records = state.log.tx_range_async(0, Some(t + 1)).await?;
let meta = self
.store
.get_root(&meta_root_name(source))
.await?
.ok_or_else(|| NodeError::UnknownDb(source.to_owned()))?;
// Write the log under version 0 so it sorts beneath the
// lease-versioned file the target's first open creates, and publish
// meta last — it is the catalog entry, so a crash mid-fork never
// catalogs a target without its log.
let log = self.log_backend.open(target, 0).await?;
for record in &records {
log.append_async(record).await?;
}
drop(log);
match self
.store
.cas_root(&meta_root_name(target), None, &meta)
.await
{
Ok(()) => {}
Err(StoreError::CasFailed { .. }) => {
// Another node claimed the name first; discard our log copy.
self.log_backend.delete_all(target).await?;
return Ok(None);
}
Err(error) => return Err(error.into()),
}
let state = self.open_db(target).await?;
self.dbs
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.insert(target.to_owned(), state);
Ok(Some(t))
}
/// Deletes a database: unhosts it, releases its lease, and removes its
/// roots and log. Blobs remain until [`Self::gc_deleted`].
///
/// # Errors
/// Returns an error when roots or the log cannot be removed.
pub async fn delete_db(&self, name: &str) -> Result<bool, NodeError> {
let Some(state) = self
.dbs
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.remove(name)
else {
return Ok(false);
};
state.deposed.store(true, Ordering::Release);
self.standby
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.remove(name);
self.store.delete_root(&db_root_name(name)).await?;
self.store.delete_root(&meta_root_name(name)).await?;
self.log_backend.delete_all(name).await?;
Ok(true)
}
/// Lists hosted databases.
#[must_use]
pub fn list_dbs(&self) -> Vec<String> {
let mut names: Vec<String> = self
.dbs
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.keys()
.cloned()
.collect();
names.sort();
names
}
/// Sweeps blobs unreachable from any live database root (including
/// everything left behind by deleted databases and superseded indexes).
///
/// # Errors
/// Returns an error when the store cannot be enumerated or swept.
pub async fn gc_deleted(&self) -> Result<u64, NodeError> {
self.gc_deleted_with_retention(self.config.gc_retention)
.await
}
/// Sweeps unreachable blobs older than the caller-supplied retention.
///
/// # Errors
/// Returns an error when the store cannot be enumerated or swept.
pub async fn gc_deleted_with_retention(&self, retention: Duration) -> Result<u64, NodeError> {
let _gc = self.gc_lock.lock().await;
let mut live = Vec::new();
for root_name in self.store.list_roots("db:").await? {
if let Some(root) = self
.store
.get_root(&root_name)
.await?
.as_deref()
.and_then(DbRoot::decode)
&& let Some(roots) = root.roots
{
live.extend(roots);
}
}
let report = mark_and_sweep_retained(
self.store.as_ref(),
live,
|_, bytes| corium_store::index_blob_children(bytes),
retention,
SystemTime::now(),
)
.await?;
self.metrics
.record_gc(report.swept as u64, report.retained as u64);
tracing::info!(
marked = report.marked,
swept = report.swept,
retained = report.retained,
"garbage collection completed"
);
Ok(report.swept as u64)
}
/// Validates, appends, applies, and reports one transaction supplied as
/// composite-encoded EDN transaction forms.
///
/// # Errors
/// Returns [`NodeError`] for decode/validation failures, lease loss, or
/// storage failures.
pub async fn transact(
&self,
name: &str,
tx_data: &[u8],
) -> Result<pb::TransactResponse, NodeError> {
let started = Instant::now();
let result = self
.transact_inner(name, tx_data)
.instrument(tracing::info_span!("transact", db = name))
.await;
self.metrics.record_tx(started.elapsed(), result.is_ok());
if let Err(error) = &result {
tracing::warn!(%error, "transaction failed");
}
result
}
async fn transact_inner(
&self,
name: &str,
tx_data: &[u8],
) -> Result<pb::TransactResponse, NodeError> {
let state = self.db_state(name).await?;
let decoded = codec::decode_edn(tx_data)?;
let forms = decoded
.as_seq()
.ok_or_else(|| NodeError::BadRequest("tx-data must be a vector".into()))?
.to_vec();
let queued = self.metrics.queue_waiter();
let commit = state.commit.lock().await;
drop(queued);
let _commit = commit;
state.check_lease(self.store.as_ref()).await?;
// Expand user database-function invocations against the
// db-in-transaction (the value under the commit lock) before native
// conversion. The expander blocks up to its budget deadline, so it
// runs off the async workers.
let forms = if let Some(expander) = &self.config.tx_fn_expander {
let expander = Arc::clone(expander);
let db = state.transactor.db();
tokio::task::spawn_blocking(move || expander.expand(&db, forms))
.await
.map_err(|error| NodeError::BadRequest(format!("expander task failed: {error}")))?
.map_err(NodeError::BadRequest)?
} else {
forms
};
// Convert forms, interning any new keyword values.
let (items, naming_changed, idents, interner, schema) = {
let mut naming = state
.naming
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let db = state.transactor.db();
let before = naming.interner.len();
let items = tx_items_from_edn(&db, &mut naming.interner, &forms)?;
(
items,
naming.interner.len() > before,
naming.idents.clone(),
naming.interner.clone(),
naming.schema.clone(),
)
};
if naming_changed {
// New keyword names must be durable before the datoms that
// reference them; recovery decodes the log against this meta.
let meta = codec::encode_metadata(&schema, &idents, &interner);
loop {
let current = self.store.get_root(&meta_root_name(name)).await?;
match self
.store
.cas_root(&meta_root_name(name), current.as_deref(), &meta)
.await
{
Ok(()) => break,
Err(StoreError::CasFailed { .. }) => {}
Err(error) => return Err(error.into()),
}
}
state.transactor.update_naming(idents, interner.clone());
}
let report = state.transactor.transact_async(items).await?;
// Post-append fence: acknowledge only if ownership was intact after
// the record became durable. A takeover that raced the append will
// have replayed the log *after* rewriting the root record, so a
// record we acked here is provably in the successor's replay, and a
// record we refuse here lands in our version's log file where the
// successor's cutoff discards it (see log-and-transactor.md).
if let Err(error) = state.check_lease(self.store.as_ref()).await {
if matches!(error, NodeError::Deposed(_)) {
self.depose(&state, "write lease lost after durable append");
}
// Either way the transaction is not acknowledged; a transient
// store failure here is ambiguous to the caller, exactly like a
// crash between append and reply.
return Err(error);
}
let t = report.db_after.basis_t();
let datoms = codec::encode_datoms(&report.tx.datoms, &interner)?;
let tempids = codec::encode_edn(&Edn::Map(
report
.tx
.tempids
.iter()
.map(|(tempid, eid)| {
(
Edn::Str(tempid.clone()),
Edn::Long(i64::try_from(eid.raw()).unwrap_or(i64::MAX)),
)
})
.collect(),
));
let _ = state
.broadcast
.send(pb::subscribe_item::Item::Report(pb::TxReport {
t,
tx_instant: report.tx_instant,
datoms: datoms.clone(),
}));
let _ = state.basis.send(t);
Ok(pb::TransactResponse {
basis_before: report.db_before.basis_t(),
basis_t: t,
tx_instant: report.tx_instant,
tempids,
tx_data: datoms,
})
}
/// Current status for a database.
///
/// # Errors
/// Returns [`NodeError::UnknownDb`] when absent.
pub async fn status(&self, name: &str) -> Result<pb::StatusResponse, NodeError> {
let state = self.db_state(name).await?;
let db = state.db();
let counts = db.stats();
let held = state.lease();
let metrics = self.metrics.snapshot();
Ok(pb::StatusResponse {
basis_t: db.basis_t(),
index_basis_t: state.index_basis(),
lease_owner: held.owner,
lease_version: held.version,
lease_expires_unix_ms: held.expires_unix_ms,
datom_count: counts.datoms as u64,
entity_count: counts.entities as u64,
attribute_count: counts.attributes as u64,
transaction_count: metrics.tx_total,
transaction_failure_count: metrics.tx_failed,
transaction_queue_depth: metrics.queue_depth,
index_lag: db.basis_t().saturating_sub(state.index_basis()),
indexing_runs: metrics.index_runs,
gc_runs: metrics.gc_runs,
gc_swept_blobs: metrics.gc_swept,
lease_owner_endpoint: held.endpoint,
})
}
/// Releases every held write lease (graceful shutdown): the record is
/// expired in place so a standby's next poll takes over immediately
/// instead of waiting out the TTL. Hosted databases stop accepting
/// work first, so nothing commits after its lease is gone.
pub async fn release_leases(&self) {
let states: Vec<Arc<DbState>> = self
.dbs
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.drain()
.map(|(_, state)| state)
.collect();
for state in states {
state.deposed.store(true, Ordering::Release);
if let Err(error) =
lease::release(self.store.as_ref(), &state.name, &state.lease()).await
{
tracing::warn!(db = %state.name, %error, "lease release failed at shutdown");
}
}
}
/// Waits until the database basis reaches `t`, returning the basis seen.
///
/// # Errors
/// Returns [`NodeError::UnknownDb`] when absent.
pub async fn sync(&self, name: &str, t: u64) -> Result<u64, NodeError> {
let state = self.db_state(name).await?;
let mut basis = state.basis_watch();
let target = if t == 0 { *basis.borrow() } else { t };
loop {
let current = *basis.borrow();
if current >= target {
return Ok(current);
}
if basis.changed().await.is_err() {
return Ok(*basis.borrow());
}
}
}
}
#[cfg(test)]
mod tests {
use super::IndexPolicy;
use std::time::Duration;
fn pacing(interval_ms: u64, backoff: u32, threshold: u64, deadline_ms: u64) -> IndexPolicy {
IndexPolicy {
interval: Duration::from_millis(interval_ms),
backoff,
tail_threshold: threshold,
tail_deadline: Duration::from_millis(deadline_ms),
}
}
#[test]
fn base_interval_gates_publication() {
let pacing = pacing(100, 4, 0, 60_000);
assert!(!pacing.due(Duration::from_millis(99), Duration::ZERO, None));
assert!(pacing.due(Duration::from_millis(100), Duration::ZERO, None));
}
#[test]
fn backoff_stretches_the_floor_past_the_interval() {
let pacing = pacing(100, 4, 0, 60_000);
let last = Duration::from_millis(300);
assert!(!pacing.due(Duration::from_millis(1_199), last, Some(10)));
assert!(pacing.due(Duration::from_millis(1_200), last, Some(10)));
}
#[test]
fn zero_backoff_keeps_the_base_interval() {
let pacing = pacing(100, 0, 0, 60_000);
assert!(pacing.due(
Duration::from_millis(100),
Duration::from_secs(30),
Some(10)
));
}
#[test]
fn fast_publications_leave_the_interval_untouched() {
let pacing = pacing(5_000, 4, 0, 60_000);
assert!(pacing.due(Duration::from_secs(5), Duration::from_millis(3), Some(1)));
}
#[test]
fn small_tail_defers_until_the_deadline() {
let pacing = pacing(100, 4, 1_000, 60_000);
assert!(!pacing.due(Duration::from_secs(30), Duration::ZERO, Some(999)));
assert!(pacing.due(Duration::from_secs(60), Duration::ZERO, Some(999)));
}
#[test]
fn tail_at_threshold_publishes_at_base_pacing() {
let pacing = pacing(100, 4, 1_000, 60_000);
assert!(pacing.due(Duration::from_millis(100), Duration::ZERO, Some(1_000)));
}
#[test]
fn unknown_tail_publishes_at_base_pacing() {
let pacing = pacing(100, 4, 1_000, 60_000);
assert!(pacing.due(Duration::from_millis(100), Duration::ZERO, None));
}
#[test]
fn deadline_never_overrides_the_backoff_floor() {
let pacing = pacing(100, 4, 1_000, 200);
let last = Duration::from_millis(300);
assert!(!pacing.due(Duration::from_millis(400), last, Some(1)));
assert!(pacing.due(Duration::from_millis(1_200), last, Some(1)));
}
}