The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
[GDGT-376] [GDGT-2623] fix serdes export oom and n+1 table lookups (#75595)
YB58d6210131db
diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/analytics_dev.clj b/enterprise/backend/src/metabase_enterprise/audit_app/analytics_dev.cljindex a942c1132c3..bf70dcb28de 100644--- a/enterprise/backend/src/metabase_enterprise/audit_app/analytics_dev.clj+++ b/enterprise/backend/src/metabase_enterprise/audit_app/analytics_dev.clj@@ -199,7 +199,7 @@:include-field-values true}report (serdes/with-cache (serialization/store! (serialization/extract opts)(serialization/file-writer (.getPath temp-path))))]- (log/info "Export complete:" (count (:seen report)) "entities exported")+ (log/info "Export complete:" (reduce + 0 (vals (:entity-counts report))) "entities exported")(when (seq (:errors report))(log/warn "Export had errors:" (:errors report))){:report reportdiff --git a/enterprise/backend/src/metabase_enterprise/serialization/api.clj b/enterprise/backend/src/metabase_enterprise/serialization/api.cljindex bf70eb023e0..1c0184dc95d 100644--- a/enterprise/backend/src/metabase_enterprise/serialization/api.clj+++ b/enterprise/backend/src/metabase_enterprise/serialization/api.clj@@ -188,7 +188,7 @@:direction "export":source "api":duration_ms (int (/ (- (System/nanoTime) start) 1e6))- :count (count (:seen report))+ :count (reduce + 0 (vals (:entity-counts report))):error_count (count (:errors report)):collection (str/join "," (map str collection)):all_collections (and (empty? collection)diff --git a/enterprise/backend/src/metabase_enterprise/serialization/cmd.clj b/enterprise/backend/src/metabase_enterprise/serialization/cmd.cljindex 145662b1b2d..10f9f2c139c 100644--- a/enterprise/backend/src/metabase_enterprise/serialization/cmd.clj+++ b/enterprise/backend/src/metabase_enterprise/serialization/cmd.clj@@ -123,7 +123,7 @@:direction "export":source "cli":duration_ms (int (/ (- (System/nanoTime) start) 1e6))- :count (count (:seen report))+ :count (reduce + 0 (vals (:entity-counts report))):error_count (count (:errors report)):collection (str/join "," collection-ids):all_collections (and (empty? collection-ids)diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/storage.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/storage.cljindex eee0dae34b3..3a01abd6517 100644--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/storage.clj+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/storage.clj@@ -1,18 +1,20 @@(ns metabase-enterprise.serialization.v2.storage(:require- [metabase-enterprise.serialization.v2.protocols :as protocols]))+ [metabase-enterprise.serialization.v2.protocols :as protocols]+ [metabase.util :as u]))(set! *warn-on-reflection* true)(defn store!"Consume the entity `stream` and store each entity via the given `writer`.- Returns a report map with `:seen` and `:errors` vectors.+ Returns a report map with `:entity-counts`, a map of model name to the count of stored+ entities, and an `:errors` vector.Uses `run!` to make elements reach the writer as they are produced andnot materialize the entire upstream before the loop body."[stream writer](let [settings (atom [])- report (atom {:seen [] :errors []})]+ report (atom {:entity-counts {} :errors []})](run! (fn [entity](cond(instance? Exception entity)@@ -22,9 +24,10 @@(swap! settings conj entity):else- (swap! report update :seen conj (protocols/store-entity! writer entity))))+ (let [path (protocols/store-entity! writer entity)]+ (swap! report update-in [:entity-counts (-> path last :model)] u/safe-inc))))stream)(when (seq @settings)(protocols/store-settings! writer @settings)- (swap! report update :seen conj [{:model "Setting"}]))+ (swap! report update-in [:entity-counts "Setting"] u/safe-inc))@report))diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/storage_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/storage_test.cljindex 1c4240c51ca..596490af388 100644--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/storage_test.clj+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/storage_test.clj@@ -146,6 +146,21 @@(update :visibility_type keyword)(update :base_type keyword))))))))))+(deftest entity-counts-report-test+ (ts/with-random-dump-dir [dump-dir "serdesv2-"]+ (mt/with-empty-h2-app-db!+ (ts/with-temp-dpc [:model/Collection coll {:name "Some Collection"}+ :model/Card _ {:name "Some Card" :collection_id (:id coll)}]+ (let [export (into [] (extract/extract {:no-data-model true :no-transforms true}))+ models (map #(-> % :serdes/meta last :model) export)+ expected (cond-> (frequencies (remove #{"Setting"} models))+ (some #{"Setting"} models) (assoc "Setting" 1))+ report (storage/store! export (storage.files/file-writer dump-dir))]+ (testing ":entity-counts is a {model count} map, with all settings tallied as a single entry"+ (is (= expected (:entity-counts report)))+ (is (pos? (get-in report [:entity-counts "Setting"] 0))+ "settings should be part of this export, to exercise the Setting tally")))))))+(deftest yaml-sorted-test(ts/with-random-dump-dir [dump-dir "serdesv2-"](mt/with-empty-h2-app-db!diff --git a/src/metabase/models/serialization.clj b/src/metabase/models/serialization.cljindex 3ee45872063..01db076aed0 100644--- a/src/metabase/models/serialization.clj+++ b/src/metabase/models/serialization.clj@@ -1084,7 +1084,7 @@[[*import-database-fk*]] is the inverse."[id](when id- (t2/select-one-fn :name [:model/Database :id :name] :id id)))+ (resolve/export-fk-keyed (export-resolver) id :model/Database :name)))(defn ^:dynamic *import-database-fk*"Given a portable database name, resolve it back to a numeric ID.@@ -1101,9 +1101,7 @@[[*import-table-fk*]] is the inverse."[table-id :- [:maybe ::lib.schema.id/table]](when table-id- (let [{:keys [db_id name schema]} (t2/select-one [:model/Table :id :db_id :name :schema] :id table-id)- db-name (*export-database-fk* db_id)]- [db-name schema name])))+ (resolve/export-table-fk (export-resolver) table-id)))(mu/defn ^:dynamic *import-table-fk*"Given a `table_id` as exported by [[*export-table-fk*]], resolve it back into a numeric `table_id`.@@ -1176,6 +1174,13 @@[:= :name field][:= :parent_id (recursively-find-field-q table-id rest)]]}))+;; NOTE: field lookups are intentionally NOT routed through the cached resolver, unlike the+;; database and table exporters above. Fields are unbounded in number (millions on large+;; instances), and the dominant traffic — each field's own path during a data-model export —+;; has no reuse, so a cache would retain an O(field-count) map for near-zero hits and can OOM+;; the export. Export order can't be arranged around field-fk reuse either, so even a bounded+;; cache has no reliable hit rate. If caching is ever added here (e.g. for the reuse-heavy+;; FK-target refs), it MUST be bounded so no O(field-count) structure can blow up memory.(mu/defn ^:dynamic *export-field-fk*"Given a numeric `field_id`, return a portable field reference.That has the form `[db-name schema table-name field-name]`, where the `schema` might be nil.diff --git a/src/metabase/models/serialization/resolve/db.clj b/src/metabase/models/serialization/resolve/db.cljindex 9908dd9328d..32fcf09d57c 100644--- a/src/metabase/models/serialization/resolve/db.clj+++ b/src/metabase/models/serialization/resolve/db.clj@@ -44,8 +44,8 @@"Given a numeric table_id, return a portable table reference [db-name schema table-name]."[table-id](when table-id- (let [{:keys [db_id name schema]} (t2/select-one :model/Table :id table-id)- db-name (t2/select-one-fn :name :model/Database :id db_id)]+ (let [{:keys [db_id name schema]} (t2/select-one [:model/Table :id :db_id :name :schema] :id table-id)+ db-name (t2/select-one-fn :name [:model/Database :id :name] :id db_id)][db-name schema name])))(defn export-field-fkdiff --git a/test/metabase/models/serialization/resolve/db_test.clj b/test/metabase/models/serialization/resolve/db_test.cljindex 9f278c4ea4a..331e1b2c4d4 100644--- a/test/metabase/models/serialization/resolve/db_test.clj+++ b/test/metabase/models/serialization/resolve/db_test.clj@@ -75,6 +75,36 @@(is (=? {:name "middle" :active false :parent_id (:id outer)} middle))(is (=? {:name "outer" :active false :parent_id nil :table_id table-id} outer)))))))+(deftest export-database-fk-test+ (testing "returns the database name for an existing id"+ (mt/with-temp [:model/Database {db-id :id} {:name "test-db"}]+ (is (= "test-db" (serdes/*export-database-fk* db-id)))))+ (testing "nil id returns nil"+ (is (nil? (serdes/*export-database-fk* nil))))+ (testing "unknown id returns nil"+ (is (nil? (serdes/*export-database-fk* Integer/MAX_VALUE)))))++(deftest export-table-fk-test+ (testing "returns [db-name schema table-name]"+ (mt/with-temp [:model/Database {db-id :id} {:name "test-db"}+ :model/Table {table-id :id} {:db_id db-id :name "users" :schema "public"}]+ (is (= ["test-db" "public" "users"] (serdes/*export-table-fk* table-id)))))+ (testing "nil schema is preserved"+ (mt/with-temp [:model/Database {db-id :id} {:name "test-db"}+ :model/Table {table-id :id} {:db_id db-id :name "schemaless" :schema nil}]+ (is (= ["test-db" nil "schemaless"] (serdes/*export-table-fk* table-id)))))+ (testing "nil id returns nil"+ (is (nil? (serdes/*export-table-fk* nil)))))++(deftest cached-export-database-fk-test+ (mt/with-temp [:model/Database {db-id :id} {:name "test-db"}]+ (serdes/with-cache+ (t2/with-call-count [call-count]+ (is (= "test-db" (serdes/*export-database-fk* db-id)))+ (is (= "test-db" (serdes/*export-database-fk* db-id)))+ (testing "repeat lookups inside with-cache are served from the memoized resolver"+ (is (= 1 (call-count))))))))+(deftest import-field-fk-reuses-existing-parent-test(testing "uses an existing parent field rather than creating a duplicate"(mt/with-model-cleanup [:model/Field]