Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Prevent missing search index tables from aborting caller transactions (#81573)

Chris Truter4f81af569127
diff --git a/src/metabase/search/appdb/index.clj b/src/metabase/search/appdb/index.clj
index 6cb2bcfc473..d9e742fac3d 100644
--- a/src/metabase/search/appdb/index.clj
+++ b/src/metabase/search/appdb/index.clj
@@ -300,6 +300,15 @@
:initial-exception-message (ex-message e-before)}
e-after))
+(defn- isolate-write!
+ "Run `f` directly unless already in a transaction, in which case use a savepoint so the caller can catch an error
+ without aborting the enclosing transaction."
+ [f]
+ (if (mdb/in-transaction?)
+ (t2/with-transaction [_conn]
+ (f))
+ (f)))
+
(defn- safe-batch-upsert!
"A version of batch-upsert! that no-ops for missing indexes, and handles stale index tracking metadata.
@@ -311,7 +320,9 @@
[table-type table-name-fn entries]
;; For convenience, no-op if we are not tracking any table.
(when-let [table-name (table-name-fn)]
- (let [upsert! (fn [t] (specialization/batch-upsert! t entries) t)]
+ (let [upsert! (fn [t]
+ (isolate-write! #(specialization/batch-upsert! t entries))
+ t)]
(try
(upsert! table-name)
(catch InterruptedException ie
@@ -322,7 +333,7 @@
(if (and (table-not-found-exception? e) (not (exists? table-name)))
(when-let [refreshed-table-name (do (sync-tracking-atoms!) (table-name-fn))]
(if (= table-name refreshed-table-name)
- (throw (ex-info "Currently tracked index does not exist" e {:table-name table-name}))
+ (throw (ex-info "Currently tracked index does not exist" {:table-name table-name} e))
(try
(upsert! refreshed-table-name)
(catch InterruptedException ie
@@ -411,8 +422,9 @@
(->> [(active-table) (pending-table)]
(keep (fn [table-name]
(when table-name
- {search-model (try (t2/delete! table-name :model search-model :model_id [:in (set ids)])
- ;; Race conditions with table being deleted, especially in tests.
+ ;; The table can disappear after we read its name, especially during tests.
+ {search-model (try (isolate-write!
+ #(t2/delete! table-name :model search-model :model_id [:in (set ids)]))
(catch Exception e (if (table-not-found-exception? e) 0 (throw e))))})))
(apply merge-with +)
(into {}))))
diff --git a/test/metabase/search/appdb/index_test.clj b/test/metabase/search/appdb/index_test.clj
index 71f58ddefed..ff42bf19dbb 100644
--- a/test/metabase/search/appdb/index_test.clj
+++ b/test/metabase/search/appdb/index_test.clj
@@ -788,3 +788,22 @@
(finally
(t2/delete! :model/SearchIndexMetadata :version "index-age-test")
(#'search.index/delete-obsolete-tables!))))))
+
+(deftest missing-index-table-does-not-abort-enclosing-transaction-test
+ (when (search/supports-index?)
+ (testing "Handling writes to a missing index table does not abort an enclosing transaction"
+ (search.tu/with-temp-index-table
+ (let [table-name (search.index/active-table)]
+ (#'search.index/drop-table! table-name)
+ (testing "delete!"
+ (t2/with-transaction [_conn]
+ (is (= {"card" 0} (search.engine/delete! :search.engine/appdb "card" [1])))
+ (testing "\nthe enclosing transaction remains usable"
+ (is (true? (t2/exists? :model/User))))))
+ (testing "upsert"
+ (t2/with-transaction [_conn]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Currently tracked index does not exist"
+ (#'search.index/safe-batch-upsert! :active (constantly table-name)
+ [{:model "card" :model_id "1"}])))
+ (testing "\nthe enclosing transaction remains usable"
+ (is (true? (t2/exists? :model/User)))))))))))