From 8824db9698bee5d15ea1bd2d5c2bd4871a7ae230 Mon Sep 17 00:00:00 2001 From: Sunny Bains Date: Tue, 13 Dec 2022 19:47:01 -0800 Subject: [PATCH 1/4] Issue#695 - Link liburing.a statically if present. Signed-off-by: Sunny Bains --- librocksdb_sys/build.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/librocksdb_sys/build.rs b/librocksdb_sys/build.rs index 22e9e4030..cff0c607e 100644 --- a/librocksdb_sys/build.rs +++ b/librocksdb_sys/build.rs @@ -15,7 +15,7 @@ extern crate bindgen; extern crate cc; extern crate cmake; -use cc::Build; +use cc::{Build, Tool}; use cmake::Config; use std::path::{Path, PathBuf}; use std::{env, str}; @@ -79,24 +79,29 @@ fn main() { build.flag("-std=c++11"); build.flag("-fno-rtti"); } - link_cpp(&mut build); - build.warnings(false).compile("libcrocksdb.a"); -} -fn link_cpp(build: &mut Build) { let tool = build.get_compiler(); - let stdlib = if tool.is_like_gnu() { - "libstdc++.a" + + if tool.is_like_gnu() { + link_lib("libstdc++.a", &tool); + build.cpp_link_stdlib(None); } else if tool.is_like_clang() { - "libc++.a" + link_lib("libc++.a", &tool); + build.cpp_link_stdlib(None); } else { // Don't link to c++ statically on windows. - return; }; + + link_lib("liburing.a", &tool); + + build.warnings(false).compile("libcrocksdb.a"); +} + +fn link_lib(lib: &str, tool: &Tool) { let output = tool .to_command() .arg("--print-file-name") - .arg(stdlib) + .arg(lib) .output() .unwrap(); if !output.status.success() || output.stdout.is_empty() { @@ -111,7 +116,7 @@ fn link_cpp(build: &mut Build) { return; } // remove lib prefix and .a postfix. - let libname = &stdlib[3..stdlib.len() - 2]; + let libname = &lib[3..lib.len() - 2]; // optional static linking if cfg!(feature = "static_libcpp") { println!("cargo:rustc-link-lib=static={}", &libname); @@ -122,7 +127,6 @@ fn link_cpp(build: &mut Build) { "cargo:rustc-link-search=native={}", path.parent().unwrap().display() ); - build.cpp_link_stdlib(None); } fn build_rocksdb() -> Build { From e16a09327385e66df7a512e3c96d6da97751543e Mon Sep 17 00:00:00 2001 From: Sunny Bains Date: Tue, 13 Dec 2022 19:51:11 -0800 Subject: [PATCH 2/4] Initial Signed-off-by: Sunny Bains --- librocksdb_sys/rust-titan/Cargo.toml | 28 +++++ librocksdb_sys/rust-titan/build.rs | 23 ++++ .../rust-titan/include/rust-titan.h | 7 ++ librocksdb_sys/rust-titan/src/lib.rs | 106 ++++++++++++++++++ librocksdb_sys/rust-titan/src/rust-titan.cc | 9 ++ 5 files changed, 173 insertions(+) create mode 100644 librocksdb_sys/rust-titan/Cargo.toml create mode 100644 librocksdb_sys/rust-titan/build.rs create mode 100644 librocksdb_sys/rust-titan/include/rust-titan.h create mode 100644 librocksdb_sys/rust-titan/src/lib.rs create mode 100644 librocksdb_sys/rust-titan/src/rust-titan.cc diff --git a/librocksdb_sys/rust-titan/Cargo.toml b/librocksdb_sys/rust-titan/Cargo.toml new file mode 100644 index 000000000..1dbdc07d8 --- /dev/null +++ b/librocksdb_sys/rust-titan/Cargo.toml @@ -0,0 +1,28 @@ +[package] +edition = "2021" +version = "0.1.0" +name = "rust-titan" +authors = ["Sunny Bains "] + +[lib] +name = "rust_titan" +path = "src/lib.rs" +crate-type = ["staticlib"] + +[dependencies] +rocksdb = { path = "../../.." } + +[dependencies.cxx] +version = "1" +cxx-build = "1" +features = ["c++20"] + +[build-dependencies] +cc = "1.0.3" +cmake = "0.1" +cxx-build = "1" +pkg-config = "0.3" + +[dependencies.snappy-sys] +git = "https://github.com/busyjay/rust-snappy.git" +branch = "static-link" diff --git a/librocksdb_sys/rust-titan/build.rs b/librocksdb_sys/rust-titan/build.rs new file mode 100644 index 000000000..e6d490b06 --- /dev/null +++ b/librocksdb_sys/rust-titan/build.rs @@ -0,0 +1,23 @@ +fn main() { + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=include/rust-titan.h"); + println!("cargo:rerun-if-changed=src/rust-titan.cc"); + println!("cargo:rustc-link-arg=-lcxxbridge1"); + + // FIXME: Use linklib() from top level build.rs + println!("cargo:rustc-link-arg=-lstdc++"); + println!("cargo:rerun-if-changed=src/rust-titan.cc"); + + println!("cargo:rustc-link-lib=static=rust-titan"); + + cxx_build::bridge("src/lib.rs") + .file("src/rust-titan.cc") + .flag("-DCXXASYNC_HAVE_COROUTINE_HEADER") + .flag("-fcoroutines") + .flag("-std=c++20") + .flag_if_supported("-Wall") + .include("include") + .include("../../rocksdb/include") + .include("../../crocksdb") + .compile("rust-titan"); +} diff --git a/librocksdb_sys/rust-titan/include/rust-titan.h b/librocksdb_sys/rust-titan/include/rust-titan.h new file mode 100644 index 000000000..d65c3808e --- /dev/null +++ b/librocksdb_sys/rust-titan/include/rust-titan.h @@ -0,0 +1,7 @@ +#pragma once + +#include "rocksdb/db.h" + +using ROCKSDB_NAMESPACE::DBOptions; + +std::shared_ptr new_db_options(); diff --git a/librocksdb_sys/rust-titan/src/lib.rs b/librocksdb_sys/rust-titan/src/lib.rs new file mode 100644 index 000000000..b03fc0d4f --- /dev/null +++ b/librocksdb_sys/rust-titan/src/lib.rs @@ -0,0 +1,106 @@ +use std::fmt; +use cxx::SharedPtr; +use rocksdb::rocksdb::*; + +#[cxx::bridge] +mod ffi { + unsafe extern "C++" { + include!("rust-titan.h"); + include!("rocksdb/db.h"); + + #[namespace = "rocksdb"] + type DBOptions; + + unsafe fn new_db_options() -> SharedPtr; + } +} + +#[derive(Copy, Clone, Debug)] +enum TitanBlobRunMode { + /// Titan process read/write as normal + Normal = 0, + + /// Titan stop writing value into blob log during flush + /// and compaction. Existing values in blob log is still + /// readable and garbage collected. + ReadOnly = 1, + + /// On flush and compaction, Titan will convert blob + /// index into real value, by reading from blob log, + /// and store the value in SST file. + Fallback = 2, +} + +struct TitanDBOptions { + /// The directory to store data specific to TitanDB alongside with + /// the base DB. + /// + /// Default: {dbname}/titandb + dirname: String, + + /// Disable background GC + /// + /// Default: false + disable_background_gc: bool, + + /// Max background GC thread + /// + /// Default: 1 + max_background_gc: i32, + + /// How often to schedule delete obsolete blob files periods. + /// If set zero, obsolete blob files won't be deleted. + /// + /// Default: 10 + purge_obsolete_files_period_sec: u32, + + /// If non-zero, dump titan internal stats to info log every + /// titan_stats_dump_period_sec. + /// + /// Default: 600 (10 min) + titan_stats_dump_period_sec: u32, + + /// In C++ we would inherit from this class + db_options: SharedPtr, +} + +impl fmt::Display for TitanDBOptions { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{} {} {} {} {}", + self.dirname, + self.disable_background_gc, + self.max_background_gc, + self.purge_obsolete_files_period_sec, + self.titan_stats_dump_period_sec, + ) + } +} + +impl TitanDBOptions { + pub fn new() -> Self { + let db_options_ptr = unsafe { ffi::new_db_options() }; + + Self { + dirname: "".to_string(), + disable_background_gc: false, + max_background_gc: 1, + purge_obsolete_files_period_sec: 10, + titan_stats_dump_period_sec: 600, + db_options: db_options_ptr, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn create_db_options() { + let titan_options = TitanDBOptions::new(); + + println!("{}", titan_options); + } +} diff --git a/librocksdb_sys/rust-titan/src/rust-titan.cc b/librocksdb_sys/rust-titan/src/rust-titan.cc new file mode 100644 index 000000000..43fb4a9d0 --- /dev/null +++ b/librocksdb_sys/rust-titan/src/rust-titan.cc @@ -0,0 +1,9 @@ +#include "rust-titan.h" +#include "rust-titan/src/lib.rs.h" + +using ROCKSDB_NAMESPACE::DBOptions; + +std::shared_ptr new_db_options() { + return std::make_shared(); +} + From fcaa130ee7991fc6ff459d5523f6c95e62a2d049 Mon Sep 17 00:00:00 2001 From: Sunny Bains Date: Wed, 14 Dec 2022 09:26:21 -0800 Subject: [PATCH 3/4] Fix path --- .cargo/config.toml | 3 +++ librocksdb_sys/rust-titan/Cargo.toml | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 000000000..6706980f5 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,3 @@ +[env] +CXX="g++" +CXXFLAGS="-std=c++20 -DCXXASYNC_HAVE_COROUTINE_HEADER" diff --git a/librocksdb_sys/rust-titan/Cargo.toml b/librocksdb_sys/rust-titan/Cargo.toml index 1dbdc07d8..930834a36 100644 --- a/librocksdb_sys/rust-titan/Cargo.toml +++ b/librocksdb_sys/rust-titan/Cargo.toml @@ -10,7 +10,7 @@ path = "src/lib.rs" crate-type = ["staticlib"] [dependencies] -rocksdb = { path = "../../.." } +rocksdb = { path = "../.." } [dependencies.cxx] version = "1" From 08e450347f1419f2afd523a2848da899f1f48772 Mon Sep 17 00:00:00 2001 From: subains Date: Wed, 14 Dec 2022 09:41:47 -0800 Subject: [PATCH 4/4] Fix path Signed-off-by: subains --- librocksdb_sys/rust-titan/build.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/librocksdb_sys/rust-titan/build.rs b/librocksdb_sys/rust-titan/build.rs index e6d490b06..3ae05744c 100644 --- a/librocksdb_sys/rust-titan/build.rs +++ b/librocksdb_sys/rust-titan/build.rs @@ -17,7 +17,7 @@ fn main() { .flag("-std=c++20") .flag_if_supported("-Wall") .include("include") - .include("../../rocksdb/include") - .include("../../crocksdb") + .include("../rocksdb/include") + .include("../crocksdb") .compile("rust-titan"); }