apkipa пре 6 дана
родитељ
комит
6cc3f75ec4
6 измењених фајлова са 272 додато и 10 уклоњено
  1. 246 0
      Cargo.lock
  2. 1 1
      README-lf.md
  3. 2 0
      demo/tsdbtest_lf.cpp
  4. 3 2
      package.sh
  5. 1 0
      rust/tsdb-sdk-rs/Cargo.toml
  6. 19 7
      rust/tsdb-sdk-rs/src/lf.rs

+ 246 - 0
Cargo.lock

@@ -17,6 +17,15 @@ version = "2.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
 
+[[package]]
+name = "aho-corasick"
+version = "1.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
+dependencies = [
+ "memchr",
+]
+
 [[package]]
 name = "android-tzdata"
 version = "0.1.1"
@@ -44,6 +53,17 @@ version = "1.7.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457"
 
+[[package]]
+name = "async-trait"
+version = "0.1.83"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.93",
+]
+
 [[package]]
 name = "atomic"
 version = "0.5.3"
@@ -129,10 +149,33 @@ dependencies = [
  "iana-time-zone",
  "js-sys",
  "num-traits",
+ "serde",
  "wasm-bindgen",
  "windows-targets",
 ]
 
+[[package]]
+name = "chrono-tz"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "93698b29de5e97ad0ae26447b344c482a7284c737d9ddc5f9e52b74a336671bb"
+dependencies = [
+ "chrono",
+ "chrono-tz-build",
+ "phf",
+]
+
+[[package]]
+name = "chrono-tz-build"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0c088aee841df9c3041febbb73934cfc39708749bf96dc827e3359cd39ef11b1"
+dependencies = [
+ "parse-zoneinfo",
+ "phf",
+ "phf_codegen",
+]
+
 [[package]]
 name = "cityhash-rs"
 version = "1.0.1"
@@ -191,6 +234,29 @@ dependencies = [
  "time",
 ]
 
+[[package]]
+name = "compiler-tools"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cc80bd305611ea6275a49dfa4b7525f5dbf628274e4ad92382e89231c2074e91"
+dependencies = [
+ "regex",
+ "serde",
+]
+
+[[package]]
+name = "compiler-tools-derive"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0748adeee3cbac61b70d26e701515182d22d227baabe596d190f2b4154895842"
+dependencies = [
+ "indexmap",
+ "proc-macro2",
+ "quote",
+ "regex",
+ "syn 2.0.93",
+]
+
 [[package]]
 name = "core-foundation-sys"
 version = "0.8.7"
@@ -701,6 +767,44 @@ dependencies = [
  "wasm-bindgen",
 ]
 
+[[package]]
+name = "klickhouse"
+version = "0.13.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "97059bf2db462de12af0ad96b68c69a66895f17d02c340726bb19e11492577d6"
+dependencies = [
+ "async-trait",
+ "chrono",
+ "chrono-tz",
+ "cityhash-rs",
+ "compiler-tools",
+ "compiler-tools-derive",
+ "futures-util",
+ "indexmap",
+ "klickhouse_derive",
+ "log",
+ "lz4",
+ "paste",
+ "rustc_version",
+ "serde",
+ "serde_json",
+ "thiserror",
+ "tokio",
+ "tokio-stream",
+ "uuid",
+]
+
+[[package]]
+name = "klickhouse_derive"
+version = "0.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "12cd36350e8478be1c63ee0c86f10db751f11eed836e6963dd3faae8e55e93d1"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.93",
+]
+
 [[package]]
 name = "libc"
 version = "0.2.169"
@@ -729,6 +833,25 @@ version = "0.4.22"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
 
+[[package]]
+name = "lz4"
+version = "1.28.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4d1febb2b4a79ddd1980eede06a8f7902197960aa0383ffcfdd62fe723036725"
+dependencies = [
+ "lz4-sys",
+]
+
+[[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]]
 name = "lz4_flex"
 version = "0.11.3"
@@ -823,6 +946,15 @@ version = "1.20.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
 
+[[package]]
+name = "parse-zoneinfo"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1f2a05b18d44e2957b88f96ba460715e295bc1d7510468a2f3d3b44535d26c24"
+dependencies = [
+ "regex",
+]
+
 [[package]]
 name = "paste"
 version = "1.0.15"
@@ -835,6 +967,44 @@ version = "2.3.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
 
+[[package]]
+name = "phf"
+version = "0.11.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc"
+dependencies = [
+ "phf_shared",
+]
+
+[[package]]
+name = "phf_codegen"
+version = "0.11.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e8d39688d359e6b34654d328e262234662d16cc0f60ec8dcbe5e718709342a5a"
+dependencies = [
+ "phf_generator",
+ "phf_shared",
+]
+
+[[package]]
+name = "phf_generator"
+version = "0.11.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0"
+dependencies = [
+ "phf_shared",
+ "rand",
+]
+
+[[package]]
+name = "phf_shared"
+version = "0.11.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b"
+dependencies = [
+ "siphasher",
+]
+
 [[package]]
 name = "pin-project-lite"
 version = "0.2.15"
@@ -953,6 +1123,35 @@ dependencies = [
  "bitflags",
 ]
 
+[[package]]
+name = "regex"
+version = "1.11.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-automata",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-automata"
+version = "0.4.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
+
 [[package]]
 name = "replace_with"
 version = "0.1.7"
@@ -980,6 +1179,12 @@ version = "1.0.19"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4"
 
+[[package]]
+name = "ryu"
+version = "1.0.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
+
 [[package]]
 name = "safer-ffi"
 version = "0.1.13"
@@ -1066,6 +1271,18 @@ dependencies = [
  "syn 2.0.93",
 ]
 
+[[package]]
+name = "serde_json"
+version = "1.0.134"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d00f4175c42ee48b15416f6193a959ba3a0d67fc699a0db9ad12df9f83991c7d"
+dependencies = [
+ "itoa",
+ "memchr",
+ "ryu",
+ "serde",
+]
+
 [[package]]
 name = "sha2-const-stable"
 version = "0.1.0"
@@ -1078,6 +1295,12 @@ version = "1.3.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
 
+[[package]]
+name = "siphasher"
+version = "0.3.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d"
+
 [[package]]
 name = "slab"
 version = "0.4.9"
@@ -1328,6 +1551,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551"
 dependencies = [
  "backtrace",
+ "bytes",
  "libc",
  "mio",
  "pin-project-lite",
@@ -1347,6 +1571,17 @@ dependencies = [
  "syn 2.0.93",
 ]
 
+[[package]]
+name = "tokio-stream"
+version = "0.1.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eca58d7bba4a75707817a2c44174253f9236b2d5fbd055602e9d5c07c139a047"
+dependencies = [
+ "futures-core",
+ "pin-project-lite",
+ "tokio",
+]
+
 [[package]]
 name = "toml_datetime"
 version = "0.6.8"
@@ -1414,6 +1649,7 @@ dependencies = [
  "anyhow",
  "clickhouse",
  "compile-time",
+ "klickhouse",
  "safer-ffi",
  "serde",
  "spdlog-rs",
@@ -1465,6 +1701,16 @@ version = "1.0.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
 
+[[package]]
+name = "uuid"
+version = "1.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a"
+dependencies = [
+ "getrandom",
+ "serde",
+]
+
 [[package]]
 name = "want"
 version = "0.3.1"

+ 1 - 1
README-lf.md

@@ -4,7 +4,7 @@
 
 编译命令:
 ```shell
-g++ -std=c++11 -Iinclude -Llib/x86-64 demo/tsdbtest_lf.cpp -o tsdbtest_lf -ltsdb-merged -lpthread
+g++ -std=c++11 -Iinclude -Llib/x86-64 demo/tsdbtest_lf.cpp -o tsdbtest_lf -ltsdb-merged -lpthread -ldl
 ```
 
 若需要日志输出,请在执行程序前配置如下环境变量:

+ 2 - 0
demo/tsdbtest_lf.cpp

@@ -7,6 +7,7 @@
 int main() {
     // 连接到时序数据库服务 (位于 localhost)
     tsdb_cpp::tsdb_entry db("localhost", 9000);
+    // tsdb_cpp::tsdb_entry db("172.31.48.206", 8123);
 
     // 选中数据表 (默认为 tsdb_cpp_dist)
     db.set_active_table("tsdb_cpp");
@@ -23,6 +24,7 @@ int main() {
         std::string pt_name = "many_points";
         for (int i = 0; i < 1000000; i++) {
             batch.add_point(pt_name, 4.2, i + 1000000);
+            // db.insert_point(pt_name, 4.2, i + 1000000);
         }
         // 批量插入点
         batch.close();

+ 3 - 2
package.sh

@@ -26,9 +26,10 @@ cp ./target/aarch64-unknown-linux-gnu/release/libtsdb_sdk_rs.a ./tmp/tsdb/lib/aa
 cp ./target/release/libtsdb_sdk_rs.a ./tmp/tsdb/lib/x86-64/libtsdb-merged.a
 
 # Verify x64 build
-g++ -std=c++11 -Iinclude -L./tmp/tsdb/lib/x86-64/ demo/tsdbtest_lf.cpp -o ./tmp/tsdbtest_lf -ltsdb-merged -O3
+g++ -std=c++11 -Iinclude -L./tmp/tsdb/lib/x86-64/ demo/tsdbtest_lf.cpp -o ./tmp/tsdbtest_lf -ltsdb-merged -ldl -O3
 if nc -zv 127.0.0.1 8123 2>&1 | grep -q 'succeeded'; then
-    (cd ./tmp && PATH="/usr/lib/linux-tools/6.8.0-51-generic/:$PATH" RUST_BACKTRACE=1 flamegraph -o ../target/tsdb_lf-flamegraph.svg -- ./tsdbtest_lf)
+    objdump -T ./tmp/tsdbtest_lf | grep GLIBC | sed 's/.*GLIBC_\([.0-9]*\).*/\1/g' | sort -Vu
+    (cd ./tmp && TSDBLOG_LEVEL="debug" PATH="/usr/lib/linux-tools/6.8.0-51-generic/:$PATH" RUST_BACKTRACE=1 flamegraph -o ../target/tsdb_lf-flamegraph.svg -- ./tsdbtest_lf)
 fi
 if [ -s ./tmp/tsdb.log.error.log ]; then
     echo "./tmp/tsdb.log.error.log exists and is not empty, please check the log"

+ 1 - 0
rust/tsdb-sdk-rs/Cargo.toml

@@ -14,6 +14,7 @@ required-features = ["headers"]
 anyhow = "1.0.95"
 clickhouse = { version = "0.13.1", features = ["inserter"] }
 compile-time = "0.2.0"
+klickhouse = "0.13.2"
 safer-ffi = "0.1.13"
 serde = { version = "1.0.217", features = ["derive"] }
 spdlog-rs = "0.4.0"

+ 19 - 7
rust/tsdb-sdk-rs/src/lf.rs

@@ -125,15 +125,19 @@ impl TsdbEntry {
             client
         });
 
-        Some(Self {
-            table_name: String::new(),
+        let table_name = "tsdb_cpp_dist".to_owned();
+        let mut this = Self {
+            table_name,
             metric_name: String::new(),
             async_buffer_size: 1,
             inserter_duration: std::time::Duration::from_secs(3),
             inserter: None,
             client,
             runtime: Arc::new(runtime),
-        })
+        };
+        this.inserter = this.create_inserter();
+
+        Some(this)
     }
 
     pub fn set_active_table(&mut self, table: &str) -> bool {
@@ -221,6 +225,7 @@ impl TsdbEntry {
 
     pub fn new_insert_batch(&self) -> Option<TsdbEntryInsertBatch> {
         self.runtime.block_on(async {
+            debug!("Creating new insert batch");
             let insert = logtry_opt!(self.client.insert(&self.table_name));
             Some(TsdbEntryInsertBatch {
                 metric_name: self.metric_name.clone(),
@@ -297,6 +302,7 @@ impl TsdbEntry {
 
             let point = f();
             if point.name.is_empty() {
+                count -= 1;
                 break;
             }
             let point = MyRowFull::new(
@@ -381,9 +387,11 @@ impl TsdbEntryInsertBatch {
     }
 
     pub async fn insert_point_async(&mut self, name: &str, value: f64, nanoseconds: i64) -> bool {
-        debug!(
+        trace!(
             "Inserting to batch point {} = {} with t={}ns",
-            name, value, nanoseconds
+            name,
+            value,
+            nanoseconds
         );
 
         let point = MyRowFull::new(name, &self.metric_name, value, nanoseconds);
@@ -397,9 +405,11 @@ impl TsdbEntryInsertBatch {
             return false;
         }
 
-        debug!(
+        trace!(
             "Inserted to batch point {} = {} with t={}ns",
-            name, value, nanoseconds
+            name,
+            value,
+            nanoseconds
         );
         self.insert = Some(insert);
         true
@@ -407,10 +417,12 @@ impl TsdbEntryInsertBatch {
 
     pub fn commit_and_close(&mut self) -> bool {
         if let Some(insert) = self.insert.take() {
+            debug!("Committing insert batch");
             if let Err(e) = self.runtime.block_on(insert.end()) {
                 error!("Failed to end insert batch: {}", e);
                 return false;
             }
+            debug!("Committed insert batch");
         }
         true
     }