quill-sql 0.2.0

A tiny yet serious SQL database in Rust with ARIES-style WAL, 2PL, and B+Tree indexes.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
use std::collections::HashSet;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;

use crate::error::{QuillSQLError, QuillSQLResult};
use crate::recovery::wal::codec::{ClrPayload, TransactionPayload, TransactionRecordKind};
use crate::recovery::wal_record::WalRecordPayload;
use crate::recovery::{Lsn, WalManager};
use crate::storage::page::RecordId;
use crate::transaction::{
    IsolationLevel, LockManager, LockMode, Transaction, TransactionId, TransactionSnapshot,
    TransactionState, TransactionStatus,
};
use crate::utils::table_ref::TableReference;
use dashmap::{DashMap, DashSet};
use sqlparser::ast::TransactionAccessMode;

#[derive(Debug, Default)]
struct HeldLocks {
    tables: Vec<(TableReference, LockMode)>,
    rows: Vec<(TableReference, RecordId, LockMode)>,
    row_keys: HashSet<(TableReference, RecordId)>,
    shared_rows: HashSet<(TableReference, RecordId)>,
}

pub struct TransactionManager {
    wal: Arc<WalManager>,
    next_txn_id: AtomicU64,
    synchronous_commit: AtomicBool,
    active_txns: DashSet<TransactionId>,
    lock_manager: Arc<LockManager>,
    held_locks: DashMap<TransactionId, HeldLocks>,
    txn_statuses: DashMap<TransactionId, TransactionStatus>,
}

impl TransactionManager {
    pub fn new(wal: Arc<WalManager>, synchronous_commit: bool) -> Self {
        Self {
            wal,
            next_txn_id: AtomicU64::new(1),
            synchronous_commit: AtomicBool::new(synchronous_commit),
            active_txns: DashSet::new(),
            lock_manager: Arc::new(LockManager::new()),
            held_locks: DashMap::new(),
            txn_statuses: DashMap::new(),
        }
    }

    pub fn with_lock_manager(
        wal: Arc<WalManager>,
        synchronous_commit: bool,
        lock_manager: Arc<LockManager>,
    ) -> Self {
        Self {
            wal,
            next_txn_id: AtomicU64::new(1),
            synchronous_commit: AtomicBool::new(synchronous_commit),
            active_txns: DashSet::new(),
            lock_manager,
            held_locks: DashMap::new(),
            txn_statuses: DashMap::new(),
        }
    }

    pub fn begin(
        &self,
        isolation_level: IsolationLevel,
        access_mode: TransactionAccessMode,
    ) -> QuillSQLResult<Transaction> {
        let txn_id = self.next_txn_id.fetch_add(1, Ordering::SeqCst);
        if txn_id == 0 {
            return Err(QuillSQLError::Internal(
                "Transaction ID wrapped around".to_string(),
            ));
        }
        let sync_commit = self.synchronous_commit.load(Ordering::Relaxed);
        let mut txn = Transaction::new(txn_id, isolation_level, access_mode, sync_commit);
        let append = self.wal.append_record_with(|_| {
            WalRecordPayload::Transaction(TransactionPayload {
                marker: TransactionRecordKind::Begin,
                txn_id,
            })
        })?;
        txn.set_begin_lsn(append.end_lsn);
        self.active_txns.insert(txn_id);
        self.txn_statuses
            .insert(txn_id, TransactionStatus::InProgress);
        self.held_locks.insert(txn_id, HeldLocks::default());
        Ok(txn)
    }

    pub fn acquire_table_lock(
        &self,
        txn: &Transaction,
        table: TableReference,
        mode: LockMode,
    ) -> QuillSQLResult<()> {
        if self.lock_manager.lock_table(txn, mode, table.clone()) {
            if let Some(mut entry) = self.held_locks.get_mut(&txn.id()) {
                entry.tables.push((table, mode));
            } else {
                let mut new_entry = HeldLocks::default();
                new_entry.tables.push((table, mode));
                self.held_locks.insert(txn.id(), new_entry);
            }
            Ok(())
        } else {
            Err(QuillSQLError::Internal(format!(
                "Failed to acquire table lock for txn {}",
                txn.id()
            )))
        }
    }

    pub fn try_acquire_row_lock(
        &self,
        txn: &Transaction,
        table: TableReference,
        rid: RecordId,
        mode: LockMode,
    ) -> QuillSQLResult<bool> {
        let key = (table.clone(), rid);
        if let Some(entry) = self.held_locks.get(&txn.id()) {
            if entry.row_keys.contains(&key) {
                return Ok(true);
            }
        }
        if self.lock_manager.lock_row(txn, mode, table.clone(), rid) {
            self.record_row_lock(txn.id(), table, rid, mode);
            Ok(true)
        } else {
            Ok(false)
        }
    }

    pub fn acquire_row_lock(
        &self,
        txn: &Transaction,
        table: TableReference,
        rid: RecordId,
        mode: LockMode,
    ) -> QuillSQLResult<()> {
        if !self.try_acquire_row_lock(txn, table.clone(), rid, mode)? {
            return Err(QuillSQLError::Internal(format!(
                "Failed to acquire row lock for txn {}",
                txn.id()
            )));
        }
        Ok(())
    }

    pub fn commit(&self, txn: &mut Transaction) -> QuillSQLResult<()> {
        match txn.state() {
            TransactionState::Running | TransactionState::Tainted => {}
            TransactionState::Committed => {
                return Err(QuillSQLError::Internal(format!(
                    "Transaction {} already committed",
                    txn.id()
                )))
            }
            TransactionState::Aborted => {
                return Err(QuillSQLError::Internal(format!(
                    "Transaction {} already aborted",
                    txn.id()
                )))
            }
        }

        let txn_id = txn.id();
        let append = self.wal.append_record_with(|_| {
            WalRecordPayload::Transaction(TransactionPayload {
                marker: TransactionRecordKind::Commit,
                txn_id,
            })
        })?;
        txn.record_lsn(append.end_lsn);
        txn.set_state(TransactionState::Committed);

        self.active_txns.remove(&txn_id);
        self.txn_statuses
            .insert(txn_id, TransactionStatus::Committed);
        self.release_all_locks(txn_id);
        txn.clear_undo();
        txn.clear_snapshot();
        self.finish_commit(txn, append.end_lsn)
    }

    pub fn abort(&self, txn: &mut Transaction) -> QuillSQLResult<()> {
        match txn.state() {
            TransactionState::Committed => {
                return Err(QuillSQLError::Internal(format!(
                    "Transaction {} already committed",
                    txn.id()
                )))
            }
            TransactionState::Aborted => return Ok(()),
            TransactionState::Running | TransactionState::Tainted => {}
        }

        let txn_id = txn.id();
        let mut undo_next: Option<Lsn> = None;
        while let Some(action) = txn.pop_undo_action() {
            let payload = action.to_heap_payload()?;
            let clr_result = self.wal.append_record_with(|ctx| {
                txn.record_lsn(ctx.end_lsn);
                WalRecordPayload::Clr(ClrPayload {
                    txn_id,
                    undone_lsn: ctx.start_lsn,
                    undo_next_lsn: undo_next.unwrap_or(0),
                })
            })?;
            txn.record_lsn(clr_result.end_lsn);
            let heap_result = self.wal.append_record_with(|ctx| {
                txn.record_lsn(ctx.end_lsn);
                WalRecordPayload::Heap(payload.clone())
            })?;
            txn.record_lsn(heap_result.end_lsn);
            action.undo(txn_id)?;
            undo_next = Some(heap_result.start_lsn);
        }

        let append = self.wal.append_record_with(|_| {
            WalRecordPayload::Transaction(TransactionPayload {
                marker: TransactionRecordKind::Abort,
                txn_id,
            })
        })?;
        txn.record_lsn(append.end_lsn);
        txn.set_state(TransactionState::Aborted);

        self.active_txns.remove(&txn_id);
        self.txn_statuses.insert(txn_id, TransactionStatus::Aborted);
        self.release_all_locks(txn_id);
        txn.clear_undo();
        txn.clear_snapshot();
        self.finish_commit(txn, append.end_lsn)
    }

    pub fn synchronous_commit(&self) -> bool {
        self.synchronous_commit.load(Ordering::Relaxed)
    }

    pub fn set_synchronous_commit(&self, value: bool) {
        self.synchronous_commit.store(value, Ordering::Relaxed);
    }

    pub fn active_transactions(&self) -> Vec<TransactionId> {
        self.active_txns.iter().map(|txn| *txn).collect()
    }

    pub fn snapshot(&self, txn_id: TransactionId) -> TransactionSnapshot {
        let active: Vec<TransactionId> = self
            .active_txns
            .iter()
            .map(|id| *id)
            .filter(|id| *id != txn_id)
            .collect();
        let xmax = self.next_txn_id.load(Ordering::SeqCst);
        let xmin = active.iter().copied().min().unwrap_or(xmax);
        TransactionSnapshot::new(txn_id, xmin, xmax, active)
    }

    pub fn transaction_status(&self, txn_id: TransactionId) -> TransactionStatus {
        if txn_id == 0 {
            return TransactionStatus::Committed;
        }
        self.txn_statuses
            .get(&txn_id)
            .map(|entry| *entry.value())
            .unwrap_or(TransactionStatus::Unknown)
    }

    pub fn oldest_active_txn(&self) -> Option<TransactionId> {
        self.active_txns.iter().map(|txn| *txn).min()
    }

    pub fn next_txn_id_hint(&self) -> TransactionId {
        self.next_txn_id.load(Ordering::SeqCst)
    }

    fn finish_commit(&self, txn: &Transaction, lsn: Lsn) -> QuillSQLResult<()> {
        if txn.synchronous_commit() {
            self.wal.wait_for_durable(lsn)?;
        } else {
            let _ = self.wal.flush_until(lsn)?;
        }
        Ok(())
    }

    pub fn record_row_lock(
        &self,
        txn_id: TransactionId,
        table: TableReference,
        rid: RecordId,
        mode: LockMode,
    ) {
        let mut entry = self.held_locks.entry(txn_id).or_default();
        if entry.row_keys.insert((table.clone(), rid)) {
            entry.rows.push((table, rid, mode));
        }
    }

    pub fn remove_row_key_marker(
        &self,
        txn_id: TransactionId,
        table: &TableReference,
        rid: RecordId,
    ) {
        if let Some(mut entry) = self.held_locks.get_mut(&txn_id) {
            entry.row_keys.remove(&(table.clone(), rid));
        }
    }

    pub fn record_shared_row_lock(
        &self,
        txn_id: TransactionId,
        table: TableReference,
        rid: RecordId,
    ) {
        let mut entry = self.held_locks.entry(txn_id).or_default();
        entry.shared_rows.insert((table, rid));
    }

    pub fn remove_shared_row_lock(
        &self,
        txn_id: TransactionId,
        table: &TableReference,
        rid: RecordId,
    ) {
        if let Some(mut entry) = self.held_locks.get_mut(&txn_id) {
            entry.shared_rows.remove(&(table.clone(), rid));
        }
    }

    pub fn try_unlock_shared_row(
        &self,
        txn_id: TransactionId,
        table: &TableReference,
        rid: RecordId,
    ) -> QuillSQLResult<()> {
        let _ = self.lock_manager.unlock_row_raw(txn_id, table.clone(), rid);
        self.remove_shared_row_lock(txn_id, table, rid);
        Ok(())
    }

    pub fn unlock_row(&self, txn_id: TransactionId, table: &TableReference, rid: RecordId) {
        if self.lock_manager.unlock_row_raw(txn_id, table.clone(), rid) {
            if let Some(mut entry) = self.held_locks.get_mut(&txn_id) {
                entry.row_keys.remove(&(table.clone(), rid));
                entry.rows.retain(|(t, r, _)| !(t == table && *r == rid));
                entry.shared_rows.remove(&(table.clone(), rid));
            }
        }
    }

    fn release_all_locks(&self, txn_id: TransactionId) {
        if let Some((_, mut held)) = self.held_locks.remove(&txn_id) {
            for (table, rid, _) in held.rows.drain(..).rev() {
                let _ = self.lock_manager.unlock_row_raw(txn_id, table, rid);
            }
            for (table, rid) in held.shared_rows.drain() {
                let _ = self.lock_manager.unlock_row_raw(txn_id, table, rid);
            }
            for (table, _) in held.tables.drain(..).rev() {
                let _ = self.lock_manager.unlock_table_raw(txn_id, table);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::WalConfig;
    use crate::recovery::WalManager;
    use crate::storage::page::TupleMeta;
    use tempfile::TempDir;

    fn build_wal(temp_dir: &TempDir) -> Arc<WalManager> {
        let wal_path = temp_dir.path().join("wal");
        let config = WalConfig {
            directory: wal_path,
            sync_on_flush: false,
            ..WalConfig::default()
        };
        Arc::new(WalManager::new(config, None, None).expect("wal manager"))
    }

    #[test]
    fn commit_waits_for_durable_when_sync() {
        let temp = TempDir::new().expect("tempdir");
        let wal = build_wal(&temp);
        let manager = TransactionManager::new(wal.clone(), true);

        let mut txn = manager
            .begin(
                IsolationLevel::ReadUncommitted,
                TransactionAccessMode::ReadWrite,
            )
            .expect("begin txn");
        manager.commit(&mut txn).expect("commit");

        assert_eq!(txn.state(), TransactionState::Committed);
        let lsn = txn.last_lsn().expect("commit lsn");
        assert!(wal.durable_lsn() >= lsn);
    }

    #[test]
    fn abort_records_wal_and_marks_state() {
        let temp = TempDir::new().expect("tempdir");
        let wal = build_wal(&temp);
        let manager = TransactionManager::new(wal.clone(), false);

        let mut txn = manager
            .begin(
                IsolationLevel::ReadUncommitted,
                TransactionAccessMode::ReadWrite,
            )
            .expect("begin txn");
        manager.abort(&mut txn).expect("abort");

        assert_eq!(txn.state(), TransactionState::Aborted);
        let lsn = txn.last_lsn().expect("abort lsn");
        // Async commit still triggers flush_until, so durable LSN should advance.
        assert!(wal.durable_lsn() >= lsn);
    }

    #[test]
    fn snapshot_excludes_running_insert_until_commit() {
        let temp = TempDir::new().expect("tempdir");
        let wal = build_wal(&temp);
        let manager = TransactionManager::new(wal, true);

        let mut writer = manager
            .begin(
                IsolationLevel::ReadCommitted,
                TransactionAccessMode::ReadWrite,
            )
            .expect("writer");
        let meta = TupleMeta::new(writer.id(), 0);

        let mut reader = manager
            .begin(
                IsolationLevel::ReadCommitted,
                TransactionAccessMode::ReadWrite,
            )
            .expect("reader");
        let snapshot = manager.snapshot(reader.id());
        assert!(
            !snapshot.is_visible(&meta, 0, |tid| manager.transaction_status(tid)),
            "running writer should not be visible",
        );

        manager.commit(&mut writer).expect("commit writer");
        let snapshot_after_commit = manager.snapshot(reader.id());
        assert!(snapshot_after_commit.is_visible(&meta, 0, |tid| manager.transaction_status(tid)));

        manager.abort(&mut reader).expect("abort reader");
    }

    #[test]
    fn snapshot_treats_committed_delete_as_invisible() {
        let temp = TempDir::new().expect("tempdir");
        let wal = build_wal(&temp);
        let manager = TransactionManager::new(wal, true);

        let mut inserter = manager
            .begin(
                IsolationLevel::ReadCommitted,
                TransactionAccessMode::ReadWrite,
            )
            .expect("insert txn");
        let mut meta = TupleMeta::new(inserter.id(), 0);
        manager.commit(&mut inserter).expect("commit insert");

        let mut deleter = manager
            .begin(
                IsolationLevel::ReadCommitted,
                TransactionAccessMode::ReadWrite,
            )
            .expect("delete txn");
        meta.mark_deleted(deleter.id(), 0);

        let mut reader = manager
            .begin(
                IsolationLevel::ReadCommitted,
                TransactionAccessMode::ReadWrite,
            )
            .expect("reader txn");

        let before_commit = manager.snapshot(reader.id());
        assert!(before_commit.is_visible(&meta, 0, |tid| manager.transaction_status(tid)));

        manager.commit(&mut deleter).expect("commit delete");
        let after_commit = manager.snapshot(reader.id());
        assert!(
            !after_commit.is_visible(&meta, 0, |tid| manager.transaction_status(tid)),
            "committed delete should hide tuple",
        );

        manager.abort(&mut reader).expect("abort reader");
    }
}