Skip to content

Commit

Permalink
instrument: report errors to sentry when kvs are missing
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Dec 29, 2024
1 parent 6b8f1d8 commit bf1fe27
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/main/frontend/handler/worker.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
(defmethod handle :export-current-db [_]
(state/pub-event! [:db/export-sqlite]))

(defmethod handle :capture-error [_ _worker data]
(state/pub-event! [:capture-error data]))

(defmethod handle :default [_ _worker data]
(prn :debug "Worker data not handled: " data))

Expand Down
28 changes: 26 additions & 2 deletions src/main/frontend/worker/db_worker.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
[logseq.outliner.op :as outliner-op]
[me.tonsky.persistent-sorted-set :as set :refer [BTSet]]
[promesa.core :as p]
[shadow.cljs.modern :refer [defclass]]))
[shadow.cljs.modern :refer [defclass]]
[clojure.set]))

(defonce *sqlite worker-state/*sqlite)
(defonce *sqlite-conns worker-state/*sqlite-conns)
Expand Down Expand Up @@ -139,14 +140,35 @@
[addr (bean/->clj (js/JSON.parse addresses))])))
used-addresses (set (concat (mapcat second result)
[0 1 (:eavt schema) (:avet schema) (:aevt schema)]))
unused-addresses (set/difference (set (map first result)) used-addresses)]
unused-addresses (clojure.set/difference (set (map first result)) used-addresses)]
(when unused-addresses
(prn :debug :db-gc :unused-addresses unused-addresses)
(.transaction db (fn [tx]
(doseq [addr unused-addresses]
(.exec tx #js {:sql "Delete from kvs where addr = ?"
:bind #js [addr]}))))))))

(defn- find-missing-addresses
[^Object db]
(let [schema (some->> (.exec db #js {:sql "select content from kvs where addr = 0"
:rowMode "array"})
bean/->clj
ffirst
sqlite-util/transit-read)
result (->> (.exec db #js {:sql "select addr, addresses from kvs"
:rowMode "array"})
bean/->clj
(map (fn [[addr addresses]]
[addr (bean/->clj (js/JSON.parse addresses))])))
used-addresses (set (concat (mapcat second result)
[0 1 (:eavt schema) (:avet schema) (:aevt schema)]))
missing-addresses (clojure.set/difference used-addresses (set (map first result)))]
(when (seq missing-addresses)
(worker-util/post-message :capture-error
{:error "db-missing-addresses"
:payload {:missing-addresses missing-addresses}})
(prn :error :missing-addresses missing-addresses))))

(defn upsert-addr-content!
"Upsert addr+data-seq. Update sqlite-cli/upsert-addr-content! when making changes"
[repo data delete-addrs & {:keys [client-ops-db?] :or {client-ops-db? false}}]
Expand Down Expand Up @@ -331,7 +353,9 @@
(ldb/transact! conn (sqlite-create-graph/build-initial-views)))
(catch :default _e)))

(find-missing-addresses db)
;; (gc-kvs-table! db)

(try
(db-migrate/migrate conn search-db)
(catch :default _e
Expand Down

0 comments on commit bf1fe27

Please sign in to comment.