quill-sql 0.3.0

An educational Rust relational database (RDBMS) inspired by CMU 15445
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
use std::collections::HashSet;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

use crate::error::{QuillSQLError, QuillSQLResult};
use crate::storage::record::RecordId;
use crate::transaction::{
    IsolationLevel, LockManager, LockMode, Transaction, TransactionId, TransactionSnapshot,
    TransactionState, TransactionStatus,
};
use crate::utils::table_ref::TableReference;
use dashmap::{DashMap, DashSet};
use serde::Serialize;
use sqlparser::ast::TransactionAccessMode;

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

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

impl Default for TransactionManager {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct TxnDebugEntry {
    pub txn_id: TransactionId,
    pub status: TransactionStatus,
    pub held_tables: usize,
    pub held_rows: usize,
}

#[derive(Debug, Clone, Serialize)]
pub struct TxnDebugSnapshot {
    pub active: Vec<TxnDebugEntry>,
    pub oldest_active: Option<TransactionId>,
    pub next_txn_id: TransactionId,
}

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

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

    pub fn lock_manager_arc(&self) -> Arc<LockManager> {
        self.lock_manager.clone()
    }

    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 txn = Transaction::new(txn_id, isolation_level, access_mode);
        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();
        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();
        Ok(())
    }

    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();
        while let Some(action) = txn.pop_undo_action() {
            action.undo(txn_id)?;
        }

        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();
        Ok(())
    }

    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 record_recovered_status(&self, txn_id: TransactionId, status: TransactionStatus) {
        if txn_id == 0 {
            return;
        }
        self.txn_statuses.insert(txn_id, status);
    }

    pub fn ensure_next_txn_id_at_least(&self, next_txn_id: TransactionId) {
        let mut current = self.next_txn_id.load(Ordering::SeqCst);
        while current < next_txn_id {
            match self.next_txn_id.compare_exchange(
                current,
                next_txn_id,
                Ordering::SeqCst,
                Ordering::SeqCst,
            ) {
                Ok(_) => break,
                Err(observed) => current = observed,
            }
        }
    }

    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)
    }

    pub fn debug_snapshot(&self) -> TxnDebugSnapshot {
        let active_ids = self.active_transactions();
        let mut active = Vec::with_capacity(active_ids.len());
        for txn_id in active_ids {
            let status = self.transaction_status(txn_id);
            let held = self.held_locks.get(&txn_id);
            let (held_tables, held_rows) = held
                .map(|locks| (locks.tables.len(), locks.rows.len()))
                .unwrap_or((0, 0));
            active.push(TxnDebugEntry {
                txn_id,
                status,
                held_tables,
                held_rows,
            });
        }
        TxnDebugSnapshot {
            active,
            oldest_active: self.oldest_active_txn(),
            next_txn_id: self.next_txn_id_hint(),
        }
    }

    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 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));
            }
        }
    }

    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, _) in held.tables.drain(..).rev() {
                let _ = self.lock_manager.unlock_table_raw(txn_id, table);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::record::TupleMeta;

    #[test]
    fn commit_marks_state_and_status() {
        let manager = TransactionManager::new();

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

        assert_eq!(txn.state(), TransactionState::Committed);
        assert_eq!(
            manager.transaction_status(txn.id()),
            TransactionStatus::Committed
        );
    }

    #[test]
    fn abort_marks_state_and_status() {
        let manager = TransactionManager::new();

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

        assert_eq!(txn.state(), TransactionState::Aborted);
        assert_eq!(
            manager.transaction_status(txn.id()),
            TransactionStatus::Aborted
        );
    }

    #[test]
    fn snapshot_excludes_running_insert_until_commit() {
        let manager = TransactionManager::new();

        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 manager = TransactionManager::new();

        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");
    }
}