Unverified Commit b7aadf76 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

[Turbopack] add rocksdb (#71688)

### What?

switches the database for persistent caching from lmdb to rocksdb.

### Why?

* No preallocated sparse file which cause problems
* Compression support
* Multiple files instead of a big single file
* Better performance
parent cd7b975e
...@@ -190,6 +190,7 @@ jobs: ...@@ -190,6 +190,7 @@ jobs:
- '--scenario=heavy-npm-deps-build --page=homepage' - '--scenario=heavy-npm-deps-build --page=homepage'
uses: ./.github/workflows/build_reusable.yml uses: ./.github/workflows/build_reusable.yml
with: with:
skipNativeBuild: 'yes'
afterBuild: pnpm install && ./node_modules/.bin/devlow-bench ./scripts/devlow-bench.mjs --datadog=ubuntu-latest-16-core ${{ matrix.mode }} ${{ matrix.selector }} afterBuild: pnpm install && ./node_modules/.bin/devlow-bench ./scripts/devlow-bench.mjs --datadog=ubuntu-latest-16-core ${{ matrix.mode }} ${{ matrix.selector }}
stepName: 'devlow-bench-${{ matrix.mode }}-${{ matrix.selector }}' stepName: 'devlow-bench-${{ matrix.mode }}-${{ matrix.selector }}'
secrets: inherit secrets: inherit
...@@ -295,6 +296,7 @@ jobs: ...@@ -295,6 +296,7 @@ jobs:
uses: ./.github/workflows/build_reusable.yml uses: ./.github/workflows/build_reusable.yml
with: with:
skipNativeBuild: 'yes'
afterBuild: rustup target add wasm32-unknown-unknown && curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh && node ./scripts/normalize-version-bump.js && turbo run build-wasm -- --target nodejs && git checkout . && mv crates/wasm/pkg crates/wasm/pkg-nodejs && node ./scripts/setup-wasm.mjs && NEXT_TEST_MODE=start TEST_WASM=true node run-tests.js test/production/pages-dir/production/test/index.test.ts test/e2e/streaming-ssr/index.test.ts afterBuild: rustup target add wasm32-unknown-unknown && curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh && node ./scripts/normalize-version-bump.js && turbo run build-wasm -- --target nodejs && git checkout . && mv crates/wasm/pkg crates/wasm/pkg-nodejs && node ./scripts/setup-wasm.mjs && NEXT_TEST_MODE=start TEST_WASM=true node run-tests.js test/production/pages-dir/production/test/index.test.ts test/e2e/streaming-ssr/index.test.ts
stepName: 'test-next-swc-wasm' stepName: 'test-next-swc-wasm'
secrets: inherit secrets: inherit
......
...@@ -713,6 +713,26 @@ dependencies = [ ...@@ -713,6 +713,26 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "bindgen"
version = "0.69.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088"
dependencies = [
"bitflags 2.5.0",
"cexpr",
"clang-sys",
"itertools 0.12.1",
"lazy_static",
"lazycell",
"proc-macro2",
"quote",
"regex",
"rustc-hash 1.1.0",
"shlex",
"syn 2.0.58",
]
[[package]] [[package]]
name = "binding_macros" name = "binding_macros"
version = "5.0.0" version = "5.0.0"
...@@ -920,6 +940,17 @@ dependencies = [ ...@@ -920,6 +940,17 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "bzip2-sys"
version = "0.1.11+1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "736a955f3fa7875102d57c82b8cac37ec45224a07fd32d58f9f7a186b6cd4cdc"
dependencies = [
"cc",
"libc",
"pkg-config",
]
[[package]] [[package]]
name = "camino" name = "camino"
version = "1.1.6" version = "1.1.6"
...@@ -978,12 +1009,13 @@ checksum = "a2698f953def977c68f935bb0dfa959375ad4638570e969e2f1e9f433cbf1af6" ...@@ -978,12 +1009,13 @@ checksum = "a2698f953def977c68f935bb0dfa959375ad4638570e969e2f1e9f433cbf1af6"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.83" version = "1.1.36"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" checksum = "baee610e9452a8f6f0a1b6194ec09ff9e2d85dea54432acdae41aa0761c95d70"
dependencies = [ dependencies = [
"jobserver", "jobserver",
"libc", "libc",
"shlex",
] ]
[[package]] [[package]]
...@@ -992,6 +1024,15 @@ version = "1.1.0" ...@@ -992,6 +1024,15 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c"
[[package]]
name = "cexpr"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
dependencies = [
"nom",
]
[[package]] [[package]]
name = "cfg-expr" name = "cfg-expr"
version = "0.15.4" version = "0.15.4"
...@@ -1121,6 +1162,17 @@ dependencies = [ ...@@ -1121,6 +1162,17 @@ dependencies = [
"half", "half",
] ]
[[package]]
name = "clang-sys"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4"
dependencies = [
"glob",
"libc",
"libloading",
]
[[package]] [[package]]
name = "clap" name = "clap"
version = "4.5.2" version = "4.5.2"
...@@ -3158,9 +3210,9 @@ checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" ...@@ -3158,9 +3210,9 @@ checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130"
[[package]] [[package]]
name = "jobserver" name = "jobserver"
version = "0.1.26" version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "936cfd212a0155903bcbc060e316fb6cc7cbf2e1907329391ebadc1fe0ce77c2" checksum = "48d1dbcbbeb6a7fec7e059840aa538bd62aaccf972c7346c4d9d2059312853d0"
dependencies = [ dependencies = [
"libc", "libc",
] ]
...@@ -3247,6 +3299,12 @@ version = "1.4.0" ...@@ -3247,6 +3299,12 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lazycell"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]] [[package]]
name = "leb128" name = "leb128"
version = "0.2.5" version = "0.2.5"
...@@ -3388,6 +3446,22 @@ dependencies = [ ...@@ -3388,6 +3446,22 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "librocksdb-sys"
version = "0.16.0+8.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce3d60bc059831dc1c83903fb45c103f75db65c5a7bf22272764d9cc683e348c"
dependencies = [
"bindgen",
"bzip2-sys",
"cc",
"glob",
"libc",
"libz-sys",
"lz4-sys",
"zstd-sys",
]
[[package]] [[package]]
name = "libz-sys" name = "libz-sys"
version = "1.1.8" version = "1.1.8"
...@@ -3587,6 +3661,16 @@ dependencies = [ ...@@ -3587,6 +3661,16 @@ dependencies = [
"url", "url",
] ]
[[package]]
name = "lz4-sys"
version = "1.11.1+lz4-1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bd8c0d6c6ed0cd30b3652886bb8711dc4bb01d637a68105a3d5158039b418e6"
dependencies = [
"cc",
"libc",
]
[[package]] [[package]]
name = "lz4_flex" name = "lz4_flex"
version = "0.11.3" version = "0.11.3"
...@@ -5391,6 +5475,16 @@ dependencies = [ ...@@ -5391,6 +5475,16 @@ dependencies = [
"syn 1.0.109", "syn 1.0.109",
] ]
[[package]]
name = "rocksdb"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bd13e55d6d7b8cd0ea569161127567cd587676c99f4472f779a0279aa60a7a7"
dependencies = [
"libc",
"librocksdb-sys",
]
[[package]] [[package]]
name = "rstest" name = "rstest"
version = "0.16.0" version = "0.16.0"
...@@ -5915,6 +6009,12 @@ dependencies = [ ...@@ -5915,6 +6009,12 @@ dependencies = [
"memmap2 0.6.2", "memmap2 0.6.2",
] ]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]] [[package]]
name = "signal-hook" name = "signal-hook"
version = "0.3.17" version = "0.3.17"
...@@ -8407,6 +8507,7 @@ dependencies = [ ...@@ -8407,6 +8507,7 @@ dependencies = [
"pot", "pot",
"rand", "rand",
"rayon", "rayon",
"rocksdb",
"rustc-hash 1.1.0", "rustc-hash 1.1.0",
"serde", "serde",
"serde_path_to_error", "serde_path_to_error",
......
...@@ -13,9 +13,11 @@ bench = false ...@@ -13,9 +13,11 @@ bench = false
workspace = true workspace = true
[features] [features]
default = [] default = ["rocksdb"]
verify_serialization = [] verify_serialization = []
trace_aggregation_update = [] trace_aggregation_update = []
lmdb = ["dep:lmdb-rkv"]
rocksdb = ["dep:rocksdb"]
[dependencies] [dependencies]
anyhow = { workspace = true } anyhow = { workspace = true }
...@@ -27,12 +29,13 @@ dashmap = { workspace = true, features = ["raw-api"]} ...@@ -27,12 +29,13 @@ dashmap = { workspace = true, features = ["raw-api"]}
either = { workspace = true } either = { workspace = true }
hashbrown = { workspace = true, features = ["raw"] } hashbrown = { workspace = true, features = ["raw"] }
indexmap = { workspace = true } indexmap = { workspace = true }
lmdb-rkv = "0.14.0" lmdb-rkv = { version = "0.14.0", optional = true }
once_cell = { workspace = true } once_cell = { workspace = true }
parking_lot = { workspace = true } parking_lot = { workspace = true }
pot = "3.0.0" pot = "3.0.0"
rand = { workspace = true } rand = { workspace = true }
rayon = { workspace = true } rayon = { workspace = true }
rocksdb = { version = "0.22.0", optional = true}
rustc-hash = { workspace = true } rustc-hash = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
serde_path_to_error = { workspace = true } serde_path_to_error = { workspace = true }
......
...@@ -2,14 +2,21 @@ mod by_key_space; ...@@ -2,14 +2,21 @@ mod by_key_space;
pub mod db_versioning; pub mod db_versioning;
pub mod fresh_db_optimization; pub mod fresh_db_optimization;
pub mod key_value_database; pub mod key_value_database;
#[cfg(feature = "lmdb")]
pub mod lmdb; pub mod lmdb;
pub mod noop_kv; pub mod noop_kv;
pub mod read_transaction_cache; pub mod read_transaction_cache;
#[cfg(feature = "rocksdb")]
pub mod rocksdb;
mod startup_cache; mod startup_cache;
pub use db_versioning::handle_db_versioning; pub use db_versioning::handle_db_versioning;
pub use fresh_db_optimization::{is_fresh, FreshDbOptimization}; pub use fresh_db_optimization::{is_fresh, FreshDbOptimization};
#[cfg(feature = "lmdb")]
pub use lmdb::LmbdKeyValueDatabase;
#[allow(unused_imports)] #[allow(unused_imports)]
pub use noop_kv::NoopKvDb; pub use noop_kv::NoopKvDb;
pub use read_transaction_cache::ReadTransactionCache; pub use read_transaction_cache::ReadTransactionCache;
#[cfg(feature = "rocksdb")]
pub use rocksdb::RocksDbKeyValueDatabase;
pub use startup_cache::StartupCacheLayer; pub use startup_cache::StartupCacheLayer;
use std::{
hash::{BuildHasher, BuildHasherDefault},
iter::once,
path::Path,
thread::{available_parallelism, scope},
time::Instant,
};
use anyhow::{Context, Result};
use rocksdb::{ColumnFamily, Env, SliceTransform, WriteBatch as RdbWriteBack, WriteOptions, DB};
use rustc_hash::FxHasher;
use crate::database::key_value_database::{KeySpace, KeyValueDatabase, WriteBatch};
macro_rules! make_names {
($name: ident, $prefix: literal) => {
make_names!($name, $prefix, 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15);
};
($name: ident, $prefix: literal, $($i: expr)*) => {
static $name: [&str; SHARDS] = [
$(concat!($prefix, $i),)*
];
};
}
// Atomic flush doesn't support parallel flushing of the column families so we can't use it
const USE_ATOMIC_FLUSH: bool = false;
const SHARD_BITS: usize = 4;
const SHARDS: usize = 1 << SHARD_BITS;
make_names!(TASK_DATA, "task-data-");
make_names!(TASK_META, "task-meta-");
make_names!(FORWARD_TASK_CACHE, "forward-task-cache-");
make_names!(REVERSE_TASK_CACHE, "reverse-task-cache-");
pub struct RocksDbKeyValueDatabase {
db: DB,
}
impl RocksDbKeyValueDatabase {
pub fn new(path: &Path) -> Result<Self> {
let parallelism = available_parallelism().map_or(4, |p| p.get() as i32);
let mut env = Env::new()?;
env.set_background_threads(parallelism);
env.set_high_priority_background_threads(parallelism);
env.set_bottom_priority_background_threads(parallelism);
env.set_low_priority_background_threads(parallelism);
let mut options = rocksdb::Options::default();
options.set_env(&env);
options.create_if_missing(true);
options.create_missing_column_families(true);
if USE_ATOMIC_FLUSH {
options.set_atomic_flush(true);
}
options.set_allow_concurrent_memtable_write(false);
options.set_allow_mmap_reads(true);
options.set_allow_mmap_writes(true);
#[allow(deprecated)]
options.set_max_background_flushes(parallelism);
#[allow(deprecated)]
options.set_max_background_compactions(parallelism);
options.set_enable_blob_files(true);
options.set_enable_blob_gc(true);
options.set_min_blob_size(1024 * 1024);
let mut cf_options = rocksdb::Options::default();
cf_options.set_compaction_style(rocksdb::DBCompactionStyle::Universal);
cf_options.set_level_compaction_dynamic_level_bytes(false);
cf_options.set_allow_concurrent_memtable_write(false);
cf_options.set_prefix_extractor(SliceTransform::create_noop());
cf_options.set_memtable_factory(rocksdb::MemtableFactory::Vector);
cf_options.set_compression_type(rocksdb::DBCompressionType::Lz4);
cf_options.set_compression_options(-14, 32767, 0, 1024);
cf_options.set_bottommost_compression_type(rocksdb::DBCompressionType::Zstd);
cf_options.set_bottommost_zstd_max_train_bytes(1024, true);
cf_options.optimize_for_point_lookup(8);
cf_options.set_min_blob_size(1024 * 1024);
cf_options.set_enable_blob_files(true);
cf_options.set_blob_compression_type(rocksdb::DBCompressionType::Zstd);
let cfs = Self::all_cf_names()
.map(|name| (name, cf_options.clone()))
.collect::<Vec<_>>();
let db = DB::open_cf_with_opts(&options, path, cfs)?;
Ok(Self { db })
}
fn all_cf_names() -> impl Iterator<Item = &'static str> {
once("default")
.chain(TASK_DATA.iter().copied())
.chain(TASK_META.iter().copied())
.chain(FORWARD_TASK_CACHE.iter().copied())
.chain(REVERSE_TASK_CACHE.iter().copied())
}
fn cf_handle(&self, key_space: KeySpace, key: &[u8]) -> Result<&ColumnFamily> {
let shard = if key.len() <= 4 {
// It's a TaskId in little-endian, we can take the first byte
(key[0] & (SHARDS as u8 - 1)) as usize
} else {
let hash = BuildHasherDefault::<FxHasher>::default().hash_one(key);
(hash as u8 & (SHARDS as u8 - 1)) as usize
};
self.db
.cf_handle(match key_space {
KeySpace::Infra => "default",
KeySpace::TaskMeta => TASK_META[shard],
KeySpace::TaskData => TASK_DATA[shard],
KeySpace::ForwardTaskCache => FORWARD_TASK_CACHE[shard],
KeySpace::ReverseTaskCache => REVERSE_TASK_CACHE[shard],
})
.context("Failed to get column family")
}
}
impl KeyValueDatabase for RocksDbKeyValueDatabase {
type ReadTransaction<'l>
= ()
where
Self: 'l;
fn lower_read_transaction<'l: 'i + 'r, 'i: 'r, 'r>(
tx: &'r Self::ReadTransaction<'l>,
) -> &'r Self::ReadTransaction<'i> {
tx
}
fn begin_read_transaction(&self) -> Result<Self::ReadTransaction<'_>> {
Ok(())
}
type ValueBuffer<'l>
= Vec<u8>
where
Self: 'l;
fn get<'l, 'db: 'l>(
&'l self,
_transaction: &'l Self::ReadTransaction<'db>,
key_space: super::key_value_database::KeySpace,
key: &[u8],
) -> Result<Option<Self::ValueBuffer<'l>>> {
let cf = self.cf_handle(key_space, key)?;
Ok(self.db.get_cf(cf, key)?)
}
type WriteBatch<'l>
= RocksDbWriteBatch<'l>
where
Self: 'l;
fn write_batch(&self) -> Result<Self::WriteBatch<'_>> {
Ok(RocksDbWriteBatch {
batch: RdbWriteBack::default(),
this: self,
})
}
}
pub struct RocksDbWriteBatch<'a> {
batch: RdbWriteBack,
this: &'a RocksDbKeyValueDatabase,
}
impl<'a> WriteBatch<'a> for RocksDbWriteBatch<'a> {
type ValueBuffer<'l>
= Vec<u8>
where
Self: 'l,
'a: 'l;
fn get<'l>(&'l self, key_space: KeySpace, key: &[u8]) -> Result<Option<Self::ValueBuffer<'l>>>
where
'a: 'l,
{
self.this.get(&(), key_space, key)
}
fn put(
&mut self,
key_space: KeySpace,
key: std::borrow::Cow<[u8]>,
value: std::borrow::Cow<[u8]>,
) -> Result<()> {
let cf = self.this.cf_handle(key_space, &key)?;
self.batch.put_cf(cf, key, value);
Ok(())
}
fn delete(&mut self, key_space: KeySpace, key: std::borrow::Cow<[u8]>) -> Result<()> {
let cf = self.this.cf_handle(key_space, &key)?;
self.batch.delete_cf(cf, key);
Ok(())
}
fn commit(self) -> Result<()> {
let start = Instant::now();
let mut write_options = WriteOptions::default();
if USE_ATOMIC_FLUSH {
write_options.disable_wal(true);
}
self.this.db.write_opt(self.batch, &write_options)?;
println!("Write took {:?}", start.elapsed());
let start = Instant::now();
if !USE_ATOMIC_FLUSH {
scope(|s| {
let threads = RocksDbKeyValueDatabase::all_cf_names()
.map(|name| {
s.spawn(move || {
let cf = self
.this
.db
.cf_handle(name)
.context("Failed to get column family")?;
self.this.db.flush_cf(cf)?;
anyhow::Ok(())
})
})
.collect::<Vec<_>>();
for thread in threads {
thread.join().unwrap()?;
}
anyhow::Ok(())
})?;
}
let mut wait_for_compact_options = rocksdb::WaitForCompactOptions::default();
wait_for_compact_options.set_flush(USE_ATOMIC_FLUSH);
self.this.db.wait_for_compact(&wait_for_compact_options)?;
println!("Flush took {:?}", start.elapsed());
Ok(())
}
}
...@@ -26,7 +26,7 @@ struct IntKey([u8; 4]); ...@@ -26,7 +26,7 @@ struct IntKey([u8; 4]);
impl IntKey { impl IntKey {
fn new(value: u32) -> Self { fn new(value: u32) -> Self {
Self(value.to_be_bytes()) Self(value.to_le_bytes())
} }
} }
...@@ -37,7 +37,7 @@ impl AsRef<[u8]> for IntKey { ...@@ -37,7 +37,7 @@ impl AsRef<[u8]> for IntKey {
} }
fn as_u32(bytes: impl Borrow<[u8]>) -> Result<u32> { fn as_u32(bytes: impl Borrow<[u8]>) -> Result<u32> {
let n = u32::from_be_bytes(bytes.borrow().try_into()?); let n = u32::from_le_bytes(bytes.borrow().try_into()?);
Ok(n) Ok(n)
} }
...@@ -144,7 +144,7 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorage ...@@ -144,7 +144,7 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorage
.put( .put(
KeySpace::Infra, KeySpace::Infra,
Cow::Borrowed(IntKey::new(META_KEY_SESSION_ID).as_ref()), Cow::Borrowed(IntKey::new(META_KEY_SESSION_ID).as_ref()),
Cow::Borrowed(&session_id.to_be_bytes()), Cow::Borrowed(&session_id.to_le_bytes()),
) )
.with_context(|| anyhow!("Unable to write next session id"))?; .with_context(|| anyhow!("Unable to write next session id"))?;
} }
...@@ -153,7 +153,7 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorage ...@@ -153,7 +153,7 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorage
KeySpace::Infra, KeySpace::Infra,
IntKey::new(META_KEY_NEXT_FREE_TASK_ID).as_ref(), IntKey::new(META_KEY_NEXT_FREE_TASK_ID).as_ref(),
)? { )? {
Some(bytes) => u32::from_be_bytes(bytes.borrow().try_into()?), Some(bytes) => u32::from_le_bytes(bytes.borrow().try_into()?),
None => 1, None => 1,
}; };
{ {
...@@ -187,7 +187,7 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorage ...@@ -187,7 +187,7 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorage
.put( .put(
KeySpace::ForwardTaskCache, KeySpace::ForwardTaskCache,
Cow::Borrowed(&task_type_bytes), Cow::Borrowed(&task_type_bytes),
Cow::Borrowed(&task_id.to_be_bytes()), Cow::Borrowed(&task_id.to_le_bytes()),
) )
.with_context(|| { .with_context(|| {
anyhow!("Unable to write task cache {task_type:?} => {task_id}") anyhow!("Unable to write task cache {task_type:?} => {task_id}")
...@@ -208,7 +208,7 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorage ...@@ -208,7 +208,7 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorage
.put( .put(
KeySpace::Infra, KeySpace::Infra,
Cow::Borrowed(IntKey::new(META_KEY_NEXT_FREE_TASK_ID).as_ref()), Cow::Borrowed(IntKey::new(META_KEY_NEXT_FREE_TASK_ID).as_ref()),
Cow::Borrowed(&next_task_id.to_be_bytes()), Cow::Borrowed(&next_task_id.to_le_bytes()),
) )
.with_context(|| anyhow!("Unable to write next free task id"))?; .with_context(|| anyhow!("Unable to write next free task id"))?;
} }
...@@ -279,7 +279,7 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorage ...@@ -279,7 +279,7 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorage
return Ok(None); return Ok(None);
}; };
let bytes = bytes.borrow().try_into()?; let bytes = bytes.borrow().try_into()?;
let id = TaskId::from(u32::from_be_bytes(bytes)); let id = TaskId::from(u32::from_le_bytes(bytes));
Ok(Some(id)) Ok(Some(id))
} }
let id = self let id = self
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
mod backend; mod backend;
mod backing_storage; mod backing_storage;
mod data; mod data;
mod database; pub mod database;
mod kv_backing_storage; mod kv_backing_storage;
mod utils; mod utils;
...@@ -12,22 +12,37 @@ use std::path::Path; ...@@ -12,22 +12,37 @@ use std::path::Path;
use anyhow::Result; use anyhow::Result;
pub use self::{backend::TurboTasksBackend, kv_backing_storage::KeyValueDatabaseBackingStorage}; pub use self::{backend::TurboTasksBackend, kv_backing_storage::KeyValueDatabaseBackingStorage};
use crate::database::{ use crate::database::NoopKvDb;
handle_db_versioning, is_fresh, lmdb::LmbdKeyValueDatabase, FreshDbOptimization, NoopKvDb,
ReadTransactionCache, StartupCacheLayer,
};
#[cfg(feature = "lmdb")]
pub type LmdbBackingStorage = KeyValueDatabaseBackingStorage< pub type LmdbBackingStorage = KeyValueDatabaseBackingStorage<
ReadTransactionCache<StartupCacheLayer<FreshDbOptimization<LmbdKeyValueDatabase>>>, crate::database::ReadTransactionCache<
crate::database::StartupCacheLayer<
crate::database::FreshDbOptimization<crate::database::LmbdKeyValueDatabase>,
>,
>,
>; >;
#[cfg(feature = "lmdb")]
pub fn lmdb_backing_storage(path: &Path) -> Result<LmdbBackingStorage> { pub fn lmdb_backing_storage(path: &Path) -> Result<LmdbBackingStorage> {
let path = handle_db_versioning(path)?; let path = crate::database::handle_db_versioning(path)?;
let fresh_db = is_fresh(&path); let fresh_db = crate::database::is_fresh(&path);
let database = LmbdKeyValueDatabase::new(&path)?; let database = crate::database::LmbdKeyValueDatabase::new(&path)?;
let database = FreshDbOptimization::new(database, fresh_db); let database = crate::database::FreshDbOptimization::new(database, fresh_db);
let database = StartupCacheLayer::new(database, path.join("startup.cache"), fresh_db)?; let database =
let database = ReadTransactionCache::new(database); crate::database::StartupCacheLayer::new(database, path.join("startup.cache"), fresh_db)?;
let database = crate::database::ReadTransactionCache::new(database);
Ok(KeyValueDatabaseBackingStorage::new(database))
}
#[cfg(feature = "rocksdb")]
pub type RocksDBBackingStorage =
KeyValueDatabaseBackingStorage<crate::database::RocksDbKeyValueDatabase>;
#[cfg(feature = "rocksdb")]
pub fn rocksdb_backing_storage(path: &Path) -> Result<RocksDBBackingStorage> {
let path = crate::database::handle_db_versioning(path)?;
let database = crate::database::RocksDbKeyValueDatabase::new(&path)?;
Ok(KeyValueDatabaseBackingStorage::new(database)) Ok(KeyValueDatabaseBackingStorage::new(database))
} }
...@@ -37,8 +52,18 @@ pub fn noop_backing_storage(_path: &Path) -> Result<NoopBackingStorage> { ...@@ -37,8 +52,18 @@ pub fn noop_backing_storage(_path: &Path) -> Result<NoopBackingStorage> {
Ok(KeyValueDatabaseBackingStorage::new(NoopKvDb)) Ok(KeyValueDatabaseBackingStorage::new(NoopKvDb))
} }
#[cfg(feature = "rocksdb")]
pub type DefaultBackingStorage = RocksDBBackingStorage;
#[cfg(feature = "rocksdb")]
pub fn default_backing_storage(path: &Path) -> Result<DefaultBackingStorage> {
rocksdb_backing_storage(path)
}
#[cfg(all(not(feature = "rocksdb"), feature = "lmdb"))]
pub type DefaultBackingStorage = LmdbBackingStorage; pub type DefaultBackingStorage = LmdbBackingStorage;
#[cfg(all(not(feature = "rocksdb"), feature = "lmdb"))]
pub fn default_backing_storage(path: &Path) -> Result<DefaultBackingStorage> { pub fn default_backing_storage(path: &Path) -> Result<DefaultBackingStorage> {
lmdb_backing_storage(path) rocksdb_backing_storage(path)
} }
...@@ -79,6 +79,7 @@ where ...@@ -79,6 +79,7 @@ where
fn closure_to_name<T>(value: &T) -> String { fn closure_to_name<T>(value: &T) -> String {
let name = std::any::type_name_of_val(value); let name = std::any::type_name_of_val(value);
println!("Closure name: {}", name);
name.replace("::{{closure}}", "").replace("::", "_") name.replace("::{{closure}}", "").replace("::", "_")
} }
...@@ -90,20 +91,33 @@ where ...@@ -90,20 +91,33 @@ where
F: Future<Output = Result<T>> + Send + 'static, F: Future<Output = Result<T>> + Send + 'static,
T: Debug + PartialEq + Eq + TraceRawVcs + Send + 'static, T: Debug + PartialEq + Eq + TraceRawVcs + Send + 'static,
{ {
run_with_tt(registration, move |tt| run_once(tt, fut())).await let name = closure_to_name(&fut);
run_internal(registration, name, move |tt| run_once(tt, fut())).await
} }
pub async fn run_with_tt<T, F>( pub async fn run_with_tt<T, F>(
registration: &Registration, registration: &Registration,
fut: impl Fn(Arc<dyn TurboTasksApi>) -> F + Send + 'static, fut: impl Fn(Arc<dyn TurboTasksApi>) -> F + Send + 'static,
) -> Result<()> ) -> Result<()>
where
F: Future<Output = Result<T>> + Send + 'static,
T: Debug + PartialEq + Eq + TraceRawVcs + Send + 'static,
{
let name = closure_to_name(&fut);
run_internal(registration, name, fut).await
}
pub async fn run_internal<T, F>(
registration: &Registration,
name: String,
fut: impl Fn(Arc<dyn TurboTasksApi>) -> F + Send + 'static,
) -> Result<()>
where where
F: Future<Output = Result<T>> + Send + 'static, F: Future<Output = Result<T>> + Send + 'static,
T: Debug + PartialEq + Eq + TraceRawVcs + Send + 'static, T: Debug + PartialEq + Eq + TraceRawVcs + Send + 'static,
{ {
registration.ensure_registered(); registration.ensure_registered();
let name = closure_to_name(&fut);
let tt = registration.create_turbo_tasks(&name, true); let tt = registration.create_turbo_tasks(&name, true);
println!("Run #1 (without cache)"); println!("Run #1 (without cache)");
let start = std::time::Instant::now(); let start = std::time::Instant::now();
...@@ -117,6 +131,11 @@ where ...@@ -117,6 +131,11 @@ where
let start = std::time::Instant::now(); let start = std::time::Instant::now();
tt.stop_and_wait().await; tt.stop_and_wait().await;
println!("Stopping TurboTasks took {:?}", start.elapsed()); println!("Stopping TurboTasks took {:?}", start.elapsed());
// TODO enable that when turbo-tasks-memory is removed
// assert_eq!(Arc::strong_count(&tt), 1);
let start = std::time::Instant::now();
drop(tt);
println!("Dropping TurboTasks took {:?}", start.elapsed());
let tt = registration.create_turbo_tasks(&name, false); let tt = registration.create_turbo_tasks(&name, false);
println!("Run #3 (with persistent cache if available, new TurboTasks instance)"); println!("Run #3 (with persistent cache if available, new TurboTasks instance)");
let start = std::time::Instant::now(); let start = std::time::Instant::now();
...@@ -125,6 +144,11 @@ where ...@@ -125,6 +144,11 @@ where
let start = std::time::Instant::now(); let start = std::time::Instant::now();
tt.stop_and_wait().await; tt.stop_and_wait().await;
println!("Stopping TurboTasks took {:?}", start.elapsed()); println!("Stopping TurboTasks took {:?}", start.elapsed());
// TODO enable that when turbo-tasks-memory is removed
// assert_eq!(Arc::strong_count(&tt), 1);
let start = std::time::Instant::now();
drop(tt);
println!("Dropping TurboTasks took {:?}", start.elapsed());
assert_eq!(first, third); assert_eq!(first, third);
Ok(()) Ok(())
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment