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
//! Optimistic certification pre-check: lock-free fast paths, active-transaction
//! scan, committed write-set index probes, phantom checks and SSI
//! dangerous-structure detection.
use std::collections::BTreeMap;
use std::ops::Bound::{Excluded, Unbounded};
use std::sync::Arc;
use dashmap::DashMap;
use linkrs_core::types::Timestamp;
use super::super::context::TransactionContext;
use super::super::error::TransactionError;
use super::super::types::*;
use super::conflict_kind::ConflictType;
use super::Certifier;
impl Certifier {
/// Check for write-set based conflicts with active transactions.
///
/// This method checks if a transaction's write set conflicts with any other
/// write transactions that have already passed validation.
/// After a successful check, the transaction is marked as validated.
///
/// Fast paths (lock-free, no commit-lock acquisition):
/// - Read-only transactions never conflict.
/// - SingleWriter mode bypasses certification (exclusive write lease).
/// - Empty write sets (and empty read sets for Serializable) bypass certification.
///
/// Only transactions that need certification acquire the global
/// commit lock. Returns Ok(()) if no conflicts, or Err if conflicts
/// are detected.
pub fn check_write_set_conflict(
&self,
txn_id: TransactionId,
active_transactions: &DashMap<TransactionId, Arc<TransactionContext>>,
stats: &TransactionStats,
) -> Result<(), TransactionError> {
// Lock-free fast path: cheap checks before acquiring the global lock.
let ctx = active_transactions
.get(&txn_id)
.ok_or_else(|| TransactionError::transaction_not_found(txn_id))?;
let txn_write_set = ctx.get_write_set();
let txn_read_set = ctx.get_read_set();
let serializable = ctx.isolation_level == IsolationLevel::Serializable;
// Read-only fast path: only Serializable with a non-empty read set
// needs certification (phantom / schema / index generation checks).
if ctx.read_only && (!serializable || txn_read_set.is_empty()) {
return Ok(());
}
if ctx.get_concurrency_mode() == ConcurrencyMode::SingleWriter {
ctx.mark_write_validated();
return Ok(());
}
if txn_write_set.is_empty() && (!serializable || txn_read_set.is_empty()) {
// Read-only Serializable already handled above; this is the empty
// write+read case for write transactions.
if !ctx.read_only {
return Ok(());
}
}
// Only acquire the global commit lock for transactions that require
// certification. The check and the later publish both run under this
// same lock, which closes the race where two conflicting
// transactions each observed the other as not-yet-validated and both
// passed.
let _certification_guard = self.commit_lock.lock();
// Re-fetch context after acquiring lock to ensure consistency (context may have been removed)
let ctx = active_transactions
.get(&txn_id)
.ok_or_else(|| TransactionError::transaction_not_found(txn_id))?;
// SSI: register read locks for all entities in the read set.
if serializable {
self.ssi_tracker
.register_txn_reads(txn_id, ctx.start_timestamp, &txn_read_set);
}
self.scan_active_transactions(
txn_id,
&ctx,
serializable,
&txn_read_set,
active_transactions,
stats,
)?;
let committed = self.committed_write_sets.lock();
self.probe_committed_writes(txn_id, ctx.start_timestamp, &txn_write_set, stats)?;
if serializable {
self.probe_committed_reads(txn_id, ctx.start_timestamp, &txn_read_set, stats)?;
self.check_range_phantoms_and_full_scan(&ctx, &txn_read_set, &committed, stats)?;
// Dangerous-structure detection: T_current writes R, T_other read
// R, AND T_current read something T_other writes (O(W × K) where
// W = write set size and K = max readers per resource).
self.detect_dangerous_structures(
txn_id,
&ctx,
&txn_write_set,
active_transactions,
stats,
)?;
}
drop(committed);
ctx.mark_write_validated();
Ok(())
}
/// Scan active validated writers for direct write-write conflicts and,
/// for Serializable, read-set vs write-set conflicts.
fn scan_active_transactions(
&self,
txn_id: TransactionId,
ctx: &Arc<TransactionContext>,
serializable: bool,
txn_read_set: &WriteSet,
active_transactions: &DashMap<TransactionId, Arc<TransactionContext>>,
stats: &TransactionStats,
) -> Result<(), TransactionError> {
for entry in active_transactions.iter() {
let (other_id, other_ctx) = entry.pair();
if other_id == &txn_id {
continue;
}
if other_ctx.read_only {
continue;
}
if !other_ctx.is_write_validated() {
continue;
}
if ctx.has_write_conflict_with(other_ctx) {
stats.record_txn_conflict_with_type(ConflictType::WriteWrite);
log::warn!(
"certification conflict txn={} other={} type={}",
txn_id,
other_id,
ConflictType::WriteWrite
);
return Err(TransactionError::serialization_failed(format!(
"conflict {} write-write with {}",
ConflictType::WriteWrite,
other_id
)));
}
if serializable && txn_read_set.has_conflict_with(&other_ctx.get_write_set()) {
stats.record_txn_conflict_with_type(ConflictType::ReadWrite);
log::warn!(
"certification conflict txn={} other={} type={}",
txn_id,
other_id,
ConflictType::ReadWrite
);
return Err(TransactionError::serialization_failed(format!(
"conflict {} read-write with {}",
ConflictType::ReadWrite,
other_id
)));
}
}
Ok(())
}
/// O(1) write-set conflict lookup against the committed spatial indices.
fn probe_committed_writes(
&self,
txn_id: TransactionId,
start_timestamp: Timestamp,
txn_write_set: &WriteSet,
stats: &TransactionStats,
) -> Result<(), TransactionError> {
for vid in txn_write_set.vertices.iter() {
if self.vertex_writes.conflicts_after(vid, start_timestamp) {
stats.record_txn_conflict_with_type(ConflictType::WriteWrite);
log::warn!(
"certification conflict txn={} type={} resource=vertex:{:?}",
txn_id,
ConflictType::WriteWrite,
vid
);
return Err(TransactionError::serialization_failed(format!(
"conflict {} write-write vertex {:?}",
ConflictType::WriteWrite,
vid
)));
}
}
for edge in txn_write_set.edges.iter() {
if self.edge_writes.conflicts_after(edge, start_timestamp) {
stats.record_txn_conflict_with_type(ConflictType::WriteWrite);
log::warn!(
"certification conflict txn={} type={} resource=edge:{:?}",
txn_id,
ConflictType::WriteWrite,
edge
);
return Err(TransactionError::serialization_failed(format!(
"conflict {} write-write edge {:?}",
ConflictType::WriteWrite,
edge
)));
}
}
for resource in txn_write_set.schema_resources.iter() {
if self
.schema_writes
.conflicts_after(resource, start_timestamp)
{
stats.record_txn_conflict_with_type(ConflictType::SchemaGeneration);
log::warn!(
"certification conflict txn={} type={} resource=schema:{}",
txn_id,
ConflictType::SchemaGeneration,
resource
);
return Err(TransactionError::serialization_failed(format!(
"conflict {} schema {}",
ConflictType::SchemaGeneration,
resource
)));
}
}
for resource in txn_write_set.index_resources.iter() {
if self.index_writes.conflicts_after(resource, start_timestamp) {
stats.record_txn_conflict_with_type(ConflictType::IndexGeneration);
log::warn!(
"certification conflict txn={} type={} resource=index:{}",
txn_id,
ConflictType::IndexGeneration,
resource
);
return Err(TransactionError::serialization_failed(format!(
"conflict {} index {}",
ConflictType::IndexGeneration,
resource
)));
}
}
Ok(())
}
/// O(1) read-set conflict lookup via committed write indices. The
/// committed write-set scan for phantoms is handled separately by
/// [`Self::check_range_phantoms_and_full_scan`].
fn probe_committed_reads(
&self,
txn_id: TransactionId,
start_timestamp: Timestamp,
txn_read_set: &WriteSet,
stats: &TransactionStats,
) -> Result<(), TransactionError> {
for vid in txn_read_set.vertices.iter() {
if self.vertex_writes.conflicts_after(vid, start_timestamp) {
stats.record_txn_conflict_with_type(ConflictType::ReadWrite);
log::warn!(
"certification conflict txn={} type={} resource=vertex:{:?}",
txn_id,
ConflictType::ReadWrite,
vid
);
return Err(TransactionError::serialization_failed(format!(
"conflict {} read-write vertex {:?}",
ConflictType::ReadWrite,
vid
)));
}
}
for edge in txn_read_set.edges.iter() {
if self.edge_writes.conflicts_after(edge, start_timestamp) {
stats.record_txn_conflict_with_type(ConflictType::ReadWrite);
log::warn!(
"certification conflict txn={} type={} resource=edge:{:?}",
txn_id,
ConflictType::ReadWrite,
edge
);
return Err(TransactionError::serialization_failed(format!(
"conflict {} read-write edge {:?}",
ConflictType::ReadWrite,
edge
)));
}
}
for resource in txn_read_set.schema_resources.iter() {
if self
.schema_writes
.conflicts_after(resource, start_timestamp)
{
stats.record_txn_conflict_with_type(ConflictType::SchemaGeneration);
return Err(TransactionError::serialization_failed(format!(
"schema-generation conflict: {} {}",
ConflictType::SchemaGeneration,
resource
)));
}
}
for resource in txn_read_set.index_resources.iter() {
if self.index_writes.conflicts_after(resource, start_timestamp) {
stats.record_txn_conflict_with_type(ConflictType::IndexGeneration);
return Err(TransactionError::serialization_failed(format!(
"index-generation conflict: {} {}",
ConflictType::IndexGeneration,
resource
)));
}
}
Ok(())
}
/// Predicate range phantom detection and the full-scan threshold check
/// against the committed write-set list.
fn check_range_phantoms_and_full_scan(
&self,
ctx: &Arc<TransactionContext>,
txn_read_set: &WriteSet,
committed: &BTreeMap<Timestamp, Vec<WriteSet>>,
stats: &TransactionStats,
) -> Result<(), TransactionError> {
// A concurrent committed write whose vertex falls inside a read range
// committed after our start indicates a phantom. The ordered map
// narrows the scan to commits newer than the start timestamp.
if !txn_read_set.read_ranges.is_empty() {
for sets in committed
.range((Excluded(ctx.start_timestamp), Unbounded))
.map(|(_, sets)| sets)
{
for ws in sets {
if txn_read_set.has_read_range_conflict_with(ws) {
stats.record_txn_conflict_with_type(ConflictType::Phantom);
return Err(TransactionError::serialization_failed(format!(
"phantom conflict: {} on range",
ConflictType::Phantom
)));
}
}
}
}
// Full-scan certification when the read set is large: the
// per-resource probes above already passed, so abort only on a
// newer commit that actually touches the tracked footprint.
// Unrelated concurrent commits no longer abort the scan.
if let Some(threshold) = ctx.serializable_full_scan_threshold() {
let read_size = txn_read_set.size() + txn_read_set.read_ranges.len();
if read_size >= threshold {
let has_conflicting_commit = committed
.range((Excluded(ctx.start_timestamp), Unbounded))
.any(|(_, sets)| {
sets.iter()
.any(|ws| txn_read_set.has_read_conflict_with(ws))
});
if has_conflicting_commit {
stats.record_txn_conflict_with_type(ConflictType::Phantom);
return Err(TransactionError::serialization_failed(
"full-scan read set exceeds threshold with overlapping commit",
));
}
}
}
Ok(())
}
/// SSI dangerous-structure detection against active transactions.
fn detect_dangerous_structures(
&self,
txn_id: TransactionId,
ctx: &Arc<TransactionContext>,
txn_write_set: &WriteSet,
active_transactions: &DashMap<TransactionId, Arc<TransactionContext>>,
stats: &TransactionStats,
) -> Result<(), TransactionError> {
let write_resources = txn_write_set.ssi_resources();
let read_resources = ctx.get_ssi_read_resources();
for resource in &write_resources {
// rw-dependency: T_other →rw T_current
for &(reader_id, reader_start_ts) in &self.ssi_tracker.readers(resource) {
if reader_id == txn_id {
continue;
}
if reader_start_ts >= ctx.start_timestamp {
continue;
}
// Check if T_current also reads something T_other writes
// (rw-dependency: T_current →rw T_other → potential cycle)
if let Some(reader_ctx) = active_transactions.get(&reader_id) {
if !reader_ctx.read_only
&& reader_ctx.is_write_validated()
&& read_resources
.iter()
.any(|r| reader_ctx.get_write_set().ssi_resources().contains(r))
{
stats.record_txn_conflict_with_type(ConflictType::ReadWrite);
return Err(TransactionError::serialization_failed(
"SSI dangerous structure detected: read-write cycle",
));
}
}
}
}
Ok(())
}
}