Skip to content

Commit

Permalink
wip2
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Oct 8, 2023
1 parent e94ed63 commit f5dd757
Show file tree
Hide file tree
Showing 19 changed files with 626 additions and 1,921 deletions.
13 changes: 3 additions & 10 deletions config/toydb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,10 @@ listen_raft: 0.0.0.0:9705
data_dir: data
compact_threshold: 0.2

# Whether to fsync writes. Fsyncing guarantees that committed data is persisted
# to disk, but has a high performance penalty. Disabling fsync and relying on
# cluster redundancy for data durability may be a reasonable trade-off, although
# this can compromise Raft linearizability guarantees in rare edge cases where
# committed entries lose majority.
sync: true

# Raft log storage engine
# - hybrid: (default) stores committed entries in an indexed append-only file, the rest in memory.
# - memory: stores all entries in memory.
storage_raft: hybrid
# - bitcask (default): an append-only log-structured store.
# - memory: an in-memory store using the Rust standard library's BTreeMap.
storage_raft: bitcask

# SQL key-value storage engine
# - bitcask (default): an append-only log-structured store.
Expand Down
29 changes: 16 additions & 13 deletions src/bin/toydb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ async fn main() -> Result<()> {
simplelog::SimpleLogger::init(loglevel, logconfig.build())?;

let path = std::path::Path::new(&cfg.data_dir);
let raft_store: Box<dyn storage::log::Store> = match cfg.storage_raft.as_str() {
"hybrid" | "" => Box::new(storage::log::Hybrid::new(path, cfg.sync)?),
"memory" => Box::new(storage::log::Memory::new()),
name => return Err(Error::Config(format!("Unknown Raft storage engine {}", name))),
};
let raft_state: Box<dyn raft::State> = match cfg.storage_sql.as_str() {
"bitcask" | "" => {
let engine =
Expand All @@ -52,13 +47,22 @@ async fn main() -> Result<()> {
}
name => return Err(Error::Config(format!("Unknown SQL storage engine {}", name))),
};
let raft_server = match cfg.storage_raft.as_str() {
"bitcask" | "" => {
let raft_log = raft::Log::new(storage::engine::BitCask::new_compact(
path.join("log"),
cfg.compact_threshold,
)?)?;
raft::Server::new(&cfg.id, cfg.peers, raft_log, raft_state).await?
}
"memory" => {
let raft_log = raft::Log::new(storage::engine::Memory::new())?;
raft::Server::new(&cfg.id, cfg.peers, raft_log, raft_state).await?
}
name => return Err(Error::Config(format!("Unknown Raft storage engine {}", name))),
};

Server::new(&cfg.id, cfg.peers, raft_store, raft_state)
.await?
.listen(&cfg.listen_sql, &cfg.listen_raft)
.await?
.serve()
.await
Server::new(raft_server).listen(&cfg.listen_sql, &cfg.listen_raft).await?.serve().await
}

#[derive(Debug, Deserialize)]
Expand All @@ -70,7 +74,6 @@ struct Config {
log_level: String,
data_dir: String,
compact_threshold: f64,
sync: bool,
storage_raft: String,
storage_sql: String,
}
Expand All @@ -86,7 +89,7 @@ impl Config {
.set_default("data_dir", "data")?
.set_default("compact_threshold", 0.2)?
.set_default("sync", true)?
.set_default("storage_raft", "hybrid")?
.set_default("storage_raft", "bitcask")?
.set_default("storage_sql", "bitcask")?
.add_source(config::File::with_name(file))
.add_source(config::Environment::with_prefix("TOYDB"))
Expand Down
Loading

0 comments on commit f5dd757

Please sign in to comment.