mvcc_cell code review

mvcc_cell code review

(Jin Qing's Column, Aug., 2024)

mvcc_cell: Software-transactional memory for Rust

Downloads: 1,475

Transactions are fully isolated and serialized:

  • Each transaction sees a fixed snapshot
  • Any concurrent commits will be prevented

Example:

rust 复制代码
let mvcc = Mvcc::new();

// Create a transactional slot
let slot = MvccCell::new(&mvcc, Box::new(0));

// Start concurrent transactions
let mut t1 = mvcc.begin();
let mut t2 = mvcc.begin();

// Uncommitted values are not visible outside the transaction
t1[&slot] = 42;
assert_eq!(t2[&slot], 0);

// First committer wins, regardless of actual modification order
t2[&slot] = 7;
assert!(t2.try_commit().is_ok());
assert!(t1.try_commit().is_err());

// Transactions always read the values that were current when
// begin() was called.
assert_eq!(mvcc.begin()[&slot], 7);

Features

  • The minimal implementation of MVCC
  • Uses 2 transaction IDs: begin ID and commit ID
    • 2 transactions can have the same begin ID
    • Commit will increase the global transaction ID
      • TxnId only increased on commit
    • Any overlap of 2 transactions is regarded as confliction
  • MvccCell stores a value with its history and pending writings
  • Snapshot isolation: reading and writing are within the snapshot
  • Non-blocking: no lock on reading and writing
  • Transaction
    • The ID of a transaction is private
    • Transaction is not Send nor Sync
  • Not two-phase commit: try_commit() will commit
  • Writing implies reading
    • Low Performance: any concurrent read or write to the same slot will fail
  • Consistency: Serializable
  • Vacuum can cleanup the history
相关推荐
已黑化的小白3 小时前
Rust 的所有权系统,是一场对“共享即混乱”的编程革命
开发语言·后端·rust
John_Rey4 小时前
Rust类型系统奇技淫巧:幽灵类型(PhantomData)——理解编译器与类型安全
前端·安全·rust
John_Rey14 小时前
Rust底层深度探究:自定义分配器(Allocators)——控制内存分配的精妙艺术
开发语言·后端·rust
勤奋的小小尘14 小时前
第三篇: Rust 结构体、Trait 和方法详解
rust
isyuah14 小时前
Rust Miko 框架系列(二):快速上手与基础示例
后端·rust
isyuah15 小时前
Rust Miko 框架系列(四):深入路由系统
后端·rust
星释17 小时前
Rust 练习册 10:多线程基础与并发安全
开发语言·后端·rust
2401_860319521 天前
【无标题】
开发语言·学习·rust
微小冷1 天前
Rust实战教程:做一个UDP聊天软件
rust·udp·egui·聊天软件·rust教程·用户图形界面
星释1 天前
Rust 练习册 :Rail Fence Cipher与栅栏密码
开发语言·算法·rust