Skip to content

Commit

Permalink
add diagram
Browse files Browse the repository at this point in the history
  • Loading branch information
brianshih1 committed Jul 26, 2024
1 parent 22435b1 commit 43b94c2
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 14 deletions.
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"editor.formatOnSave": true
}
"editor.formatOnSave": true,
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer"
}
}
37 changes: 35 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ tokio = { version = "1.26.0", features = ["full"] }
rand = "0.8.5"
async-trait = "0.1.68"
tempfile = "3.7.0"
anyhow = "1.0.86"
tracing = "0.1.40"

[build-dependencies]
protobuf = "3.2.0"
Expand Down
6 changes: 5 additions & 1 deletion mdbook/src/chapter_5/life_of_a_query.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ The transactional layer of the toy database is composed of many entities. This p

This page is inspired by CRDB's [Life of a SQL Query](https://github.com/cockroachdb/cockroach/blob/530100fd39cc722bc324bfb3869a325622258fb3/docs/tech-notes/life_of_a_query.md) doc, which outlines the lifecycle of a single query through the different layers of CRDB. I found that doc extremely useful when learning how CRDB works.

From a higher level, the transactional layer of my toy database is composed of these entities:
Here is a diagram showing the higher level life cycle of a read/write request:

<img src="../images/life_of_a_query.png" width="55%">

The transactional layer of my toy database is composed of these entities:

- **Concurrency Manager**: Provides isolation to concurrent and conflicting requests by sequencing them. Once a request is sequenced, it is free to execute as no conflicting requests are running at the same time. The concurrency manager is made up of the latch manager, lock table, and transaction wait queue.
- **Latch Manager**: Serializes accesses for keys. Only one latch guard can be acquired for a key at any given time.
Expand Down
Binary file added mdbook/src/images/life_of_a_query.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/concurrency/concurrency_manager.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use anyhow::Result;
use tokio::sync::mpsc::Sender;

use crate::db::db::{TxnLink, TxnMap};
Expand All @@ -9,6 +10,7 @@ use crate::latch_manager::latch_manager::{LatchGuard, LatchManager};
use crate::lock_table::lock_table::{LockTable, LockTableGuardLink, UpdateLock, WaitForGuardError};
use crate::storage::mvcc::KVStore;
use crate::storage::Key;
use tracing::error;

pub struct ConcurrencyManager {
latch_manager: LatchManager<Key>,
Expand Down Expand Up @@ -83,7 +85,6 @@ impl ConcurrencyManager {
let txn = txn_link.read().unwrap();
let keys = txn.lock_spans.read().unwrap();
keys.clone()
// TODO: Remove clone
}

pub async fn update_txn_locks(&self, txn: TxnLink, update_lock: UpdateLock) {
Expand Down
4 changes: 1 addition & 3 deletions src/execute/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ impl Executor {
&self,
request: Request,
) -> ExecuteResult {
// TODO: This should return some form of Result<...>
loop {
let request_union = &request.request_union;
let guard = self.concr_manager.sequence_req(&request).await;
Expand Down Expand Up @@ -92,7 +91,6 @@ impl Executor {
Err(err) => match err {
ResponseError::WriteIntentError(err) => {
self.handle_write_intent_error(err.intent.clone());
println!("Handle write intent error");
}
ResponseError::ReadRefreshError => {
return Err(ExecuteError::ReadRefreshFailure);
Expand All @@ -112,10 +110,10 @@ impl Executor {
.get_transaction_record(txn_intent.txn_meta.txn_id);
if let Some(txn_record) = record {
if let TransactionStatus::ABORTED = txn_record.status {
// If the txn has aborted, remove the txn record
self.store.mvcc_delete(create_intent_key(&txn_intent.key));
}
}
// If the txn has aborted, remove the txn record
}

pub async fn execute_write_request(&self, request: &Request, _guard: &Guard) -> ResponseResult {
Expand Down
6 changes: 2 additions & 4 deletions src/latch_manager/latch_interval_btree.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::{
borrow::{Borrow},
borrow::Borrow,
sync::{Arc, Mutex, RwLock, Weak},
};

use tokio::{
sync::mpsc::{self, Receiver, Sender},
};
use tokio::sync::mpsc::{self, Receiver, Sender};

use crate::storage::{mvcc_key::MVCCKey, Key};

Expand Down
2 changes: 1 addition & 1 deletion src/latch_manager/latch_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<K: NodeKey> LatchManager<K> {
if let LatchKeyGuard::NotAcquired(mut wait_guard) = lg {
tokio::select! {
Some(_) = wait_guard.receiver.recv() => {
println!("retry acquire lock")
// wait for conflicting latch to release the guard
}
_ = &mut sleep, if !sleep.is_elapsed() => {
println!("operation timed out. Releasing and retrying");
Expand Down

0 comments on commit 43b94c2

Please sign in to comment.