dev(rtc): refactor debug-log

- integrate `debug-log-db` into GC process
- update schema with `repo` column and `DATETIME`
- export `reset_debug_log_db`
This commit is contained in:
rcmerci
2026-01-06 22:52:52 +08:00
parent 27016657fd
commit 1a33a365fa
4 changed files with 43 additions and 15 deletions

View File

@@ -214,16 +214,17 @@
(defn- gc-sqlite-dbs!
"Gc main db weekly and rtc ops db each time when opening it"
[sqlite-db client-ops-db datascript-conn {:keys [full-gc?]}]
[sqlite-db client-ops-db debug-log-db datascript-conn {:keys [full-gc?]}]
(let [last-gc-at (:kv/value (d/entity @datascript-conn :logseq.kv/graph-last-gc-at))]
(when (or full-gc?
(nil? last-gc-at)
(not (number? last-gc-at))
(> (- (common-util/time-ms) last-gc-at) (* 3 24 3600 1000))) ; 3 days ago
(println :debug "gc current graph")
(log/info :gc-sqlite-dbs "gc current graph")
(doseq [db (if @*publishing? [sqlite-db] [sqlite-db client-ops-db])]
(sqlite-gc/gc-kvs-table! db {:full-gc? full-gc?})
(.exec db "VACUUM"))
(rtc-debug-log/gc! debug-log-db)
(ldb/transact! datascript-conn [{:db/ident :logseq.kv/graph-last-gc-at
:kv/value (common-util/time-ms)}]))))
@@ -282,7 +283,7 @@
config (select-keys opts [:import-type :graph-git-sha]))]
(ldb/transact! conn initial-data {:initial-db? true})))
(gc-sqlite-dbs! db client-ops-db conn {})
(gc-sqlite-dbs! db client-ops-db debug-log-db conn {})
(let [migration-result (db-migrate/migrate conn)]
(when (client-op/rtc-db-graph? repo)
@@ -584,6 +585,11 @@
(p/catch (fn [error]
(throw error)))))
(def-thread-api :thread-api/reset-debug-log-db
[repo]
(when-let [^js db (worker-state/get-sqlite-conn repo :debug-log)]
(rtc-debug-log/reset-tables! db)))
(def-thread-api :thread-api/import-db
[repo data]
(when-not (string/blank? repo)
@@ -725,10 +731,10 @@
(def-thread-api :thread-api/gc-graph
[repo]
(let [{:keys [db client-ops]} (get @*sqlite-conns repo)
(let [{:keys [db client-ops debug-log]} (get @*sqlite-conns repo)
conn (get @*datascript-conns repo)]
(when (and db conn)
(gc-sqlite-dbs! db client-ops conn {:full-gc? true})
(gc-sqlite-dbs! db client-ops debug-log conn {:full-gc? true})
nil)))
(def-thread-api :thread-api/vec-search-embedding-model-info

View File

@@ -1,14 +1,30 @@
(ns frontend.worker.rtc.debug-log
"RTC debug logging stored in per-graph sqlite db."
(:require [frontend.worker.state :as worker-state]
[lambdaisland.glogi :as log]
[logseq.common.util :as common-util]))
[lambdaisland.glogi :as log]))
(defn create-tables!
[^js db]
(when db
(.exec db "CREATE TABLE IF NOT EXISTS tx_log (id INTEGER PRIMARY KEY AUTOINCREMENT, created_at INTEGER NOT NULL, tx_data TEXT, tx_meta TEXT)")
(.exec db "CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT, created_at INTEGER NOT NULL, direction TEXT NOT NULL, message TEXT NOT NULL)")))
(.exec db "CREATE TABLE IF NOT EXISTS tx_log (id INTEGER PRIMARY KEY AUTOINCREMENT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, repo TEXT, tx_data TEXT, tx_meta TEXT)")
(.exec db "CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, repo TEXT, direction TEXT NOT NULL, message TEXT NOT NULL)")))
(defn reset-tables!
[^js db]
(when db
(.exec db "DROP TABLE IF EXISTS tx_log")
(.exec db "DROP TABLE IF EXISTS messages"))
(create-tables! db))
(defn gc!
[^js db]
(when db
(doseq [table ["tx_log" "messages"]]
(try
(.exec db (str "DELETE FROM " table " WHERE id <= (SELECT id FROM " table " ORDER BY id DESC LIMIT 1 OFFSET 10000)"))
(catch :default e
(log/error :rtc-debug-log-gc-failed {:table table :error e}))))
(.exec db "VACUUM")))
(defn- enabled?
[]
@@ -31,12 +47,12 @@
(defn log-tx!
[repo tx-data tx-meta]
(when repo
(when (and (enabled?) repo)
(when-let [db (worker-state/get-sqlite-conn repo :debug-log)]
(insert! db
"INSERT INTO tx_log (created_at, tx_data, tx_meta) VALUES (?1, ?2, ?3)"
[(common-util/time-ms) (safe-str tx-data) (safe-str tx-meta)])
(prn :debug :log-tx tx-meta))))
"INSERT INTO tx_log (repo, tx_data, tx_meta) VALUES (?1, ?2, ?3)"
[repo (safe-str tx-data) (safe-str tx-meta)])
(log/debug :log-tx tx-meta))))
(defn log-ws-message!
([direction message]
@@ -45,5 +61,5 @@
(when (and (enabled?) repo message)
(when-let [db (worker-state/get-sqlite-conn repo :debug-log)]
(insert! db
"INSERT INTO messages (created_at, direction, message) VALUES (?1, ?2, ?3)"
[(common-util/time-ms) (name direction) (str message)])))))
"INSERT INTO messages (repo, direction, message) VALUES (?1, ?2, ?3)"
[repo (name direction) (str message)])))))

View File

@@ -84,6 +84,7 @@
(def ^:export push_state api-app/push_state)
(def ^:export replace_state api-app/replace_state)
(def ^:export export_debug_log_db api-app/export_debug_log_db)
(def ^:export reset_debug_log_db api-app/reset_debug_log_db)
;; db
(def ^:export q api-db/q)

View File

@@ -178,3 +178,8 @@
(fn []
(when-let [repo (state/get-current-repo)]
(export-handler/export-repo-as-debug-log-sqlite! repo))))
(def reset_debug_log_db
(fn []
(when-let [repo (state/get-current-repo)]
(state/<invoke-db-worker-direct-pass :thread-api/reset-debug-log-db repo))))