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
//! Transaction manager behavior: statement snapshots pinned in the global snapshot tracker
use std::sync::Arc;
use super::TransactionManager;
use crate::context::TransactionContext;
use crate::error::TransactionError;
use crate::types::*;
use linkrs_core::types::Timestamp;
impl TransactionManager {
/// Begin a statement in a transaction and refresh a READ COMMITTED
/// snapshot. Repeatable-read transactions keep their original snapshot.
///
/// Single entry point (no statement-kind flag): the statement kind is not
/// knowable here — the query is planned after the statement scope opens —
/// so read-only enforcement lives downstream instead of an intent
/// parameter: the query layer rejects write operators in read-only scopes
/// before execution (`reject_writes_outside_transaction_scope`),
/// `TransactionContext::record_mutation` rejects journal writes on
/// read-only transactions if they ever reach the journal, and commit
/// certification rejects them at commit time.
pub fn begin_statement(
&self,
txn_id: TransactionId,
) -> Result<(Arc<TransactionContext>, std::time::Instant), TransactionError> {
let context = self.get_context(txn_id)?;
if context.isolation_level == IsolationLevel::ReadCommitted {
let committed = self.version_manager.read_timestamp();
let snapshot = committed.max(context.timestamp());
context.set_refreshed_read_ts(snapshot);
let start = context.begin_statement()?;
self.pin_statement_snapshot(&context, snapshot)?;
self.stats.begin_statement();
Ok((context, start))
} else {
let start = context.begin_statement()?;
self.pin_statement_snapshot(&context, context.timestamp())?;
self.stats.begin_statement();
Ok((context, start))
}
}
/// Refresh a transaction's statement snapshot without opening a
/// materialized statement scope. This is used by lazy result streams.
///
/// Same single-entry contract as `begin_statement`: no statement-kind
/// flag, read-only enforcement lives in the query layer, the mutation
/// recorder, and commit certification.
pub fn refresh_statement_snapshot(
&self,
txn_id: TransactionId,
) -> Result<Arc<TransactionContext>, TransactionError> {
let context = self.get_context(txn_id)?;
context.can_execute()?;
context.check_timeouts()?;
if context.isolation_level == IsolationLevel::ReadCommitted {
let committed = self.version_manager.read_timestamp();
let snapshot = committed.max(context.timestamp());
context.set_refreshed_read_ts(snapshot);
self.pin_statement_snapshot(&context, snapshot)?;
} else {
self.pin_statement_snapshot(&context, context.timestamp())?;
}
Ok(context)
}
/// Finish a statement and record timeout failures as rollback-only.
pub fn finish_statement(
&self,
context: &TransactionContext,
statement_start: std::time::Instant,
) -> Result<(), TransactionError> {
let result = context.finish_statement(statement_start);
self.release_statement_snapshot_pin(context);
self.stats.end_statement();
if let Err(err) = &result {
use crate::error::TransactionErrorKind;
if matches!(
err.kind(),
TransactionErrorKind::TransactionTimeout
| TransactionErrorKind::TransactionExpired
| TransactionErrorKind::CheckpointTimeout
) {
self.stats.record_timeout();
if let Some(observable) = &self.observable {
observable.record_txn_timeout();
}
}
}
result
}
/// Pin a statement snapshot in the global snapshot tracker.
///
/// Registers `snapshot` and installs it as the context pin, releasing
/// any previous pin first so refresh-replace never leaks. GC derives
/// its cutoff from the tracker, so a pinned statement snapshot cannot
/// be reclaimed while the statement runs.
fn pin_statement_snapshot(
&self,
context: &Arc<TransactionContext>,
snapshot: Timestamp,
) -> Result<(), TransactionError> {
if let Err(error) = self
.version_manager
.snapshot_tracker()
.add_snapshot(snapshot)
{
return Err(TransactionError::internal(format!(
"Failed to pin statement snapshot {}: {}",
snapshot, error
)));
}
if let Some(previous) = context.set_statement_snapshot_pin(snapshot) {
let _ = self
.version_manager
.snapshot_tracker()
.release_snapshot(previous);
}
Ok(())
}
/// Release the statement snapshot pin held by `context`, if any.
///
/// Called on every terminal path (statement finish, commit, abort,
/// recovery re-drive) so pins never outlive their statement. Releasing
/// a missing entry is ignored: the pin may already have been replaced.
pub(crate) fn release_statement_snapshot_pin(&self, context: &TransactionContext) {
if let Some(pinned) = context.take_statement_snapshot_pin() {
if let Err(error) = self
.version_manager
.snapshot_tracker()
.release_snapshot(pinned)
{
log::debug!(
"Statement snapshot {} release skipped for txn={:?}: {}",
pinned,
context.id,
error
);
}
}
}
}