Explorar el Código

dev(rtc): refactor debug-log

- integrate `debug-log-db` into GC process
- update schema with `repo` column and `DATETIME`
- export `reset_debug_log_db`
rcmerci hace 1 semana
padre
commit
1a33a365fa

+ 11 - 5
src/main/frontend/worker/db_worker.cljs

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

+ 26 - 10
src/main/frontend/worker/rtc/debug_log.cljs

@@ -1,14 +1,30 @@
 (ns frontend.worker.rtc.debug-log
 (ns frontend.worker.rtc.debug-log
   "RTC debug logging stored in per-graph sqlite db."
   "RTC debug logging stored in per-graph sqlite db."
   (:require [frontend.worker.state :as worker-state]
   (:require [frontend.worker.state :as worker-state]
-            [lambdaisland.glogi :as log]
-            [logseq.common.util :as common-util]))
+            [lambdaisland.glogi :as log]))
 
 
 (defn create-tables!
 (defn create-tables!
   [^js db]
   [^js db]
   (when 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?
 (defn- enabled?
   []
   []
@@ -31,12 +47,12 @@
 
 
 (defn log-tx!
 (defn log-tx!
   [repo tx-data tx-meta]
   [repo tx-data tx-meta]
-  (when repo
+  (when (and (enabled?) repo)
     (when-let [db (worker-state/get-sqlite-conn repo :debug-log)]
     (when-let [db (worker-state/get-sqlite-conn repo :debug-log)]
       (insert! db
       (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!
 (defn log-ws-message!
   ([direction message]
   ([direction message]
@@ -45,5 +61,5 @@
    (when (and (enabled?) repo message)
    (when (and (enabled?) repo message)
      (when-let [db (worker-state/get-sqlite-conn repo :debug-log)]
      (when-let [db (worker-state/get-sqlite-conn repo :debug-log)]
        (insert! db
        (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)])))))

+ 1 - 0
src/main/logseq/api.cljs

@@ -84,6 +84,7 @@
 (def ^:export push_state api-app/push_state)
 (def ^:export push_state api-app/push_state)
 (def ^:export replace_state api-app/replace_state)
 (def ^:export replace_state api-app/replace_state)
 (def ^:export export_debug_log_db api-app/export_debug_log_db)
 (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
 ;; db
 (def ^:export q api-db/q)
 (def ^:export q api-db/q)

+ 5 - 0
src/main/logseq/api/app.cljs

@@ -178,3 +178,8 @@
   (fn []
   (fn []
     (when-let [repo (state/get-current-repo)]
     (when-let [repo (state/get-current-repo)]
       (export-handler/export-repo-as-debug-log-sqlite! 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))))