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
Remove the semantic search kill switch (#77570)
Chris Truterd65b0f5661bb
diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/core.cljindex ca49170a47d..8f6fa7028be 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj@@ -190,7 +190,7 @@(let [pgvector (semantic.env/get-pgvector-datasource!)index-metadata (semantic.env/get-index-metadata)](if-not (index-active? pgvector index-metadata)- ;; Semantic can become active at runtime (kill switch re-enabled, or added to additional-search-engines)+ ;; Semantic can become active at runtime (license applied, or added to additional-search-engines);; without init! ever having run; initializing here lets the periodic repair task backfill the index.(do(log/info "No active semantic index, initializing it instead of repairing")diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/settings.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/settings.cljindex 6ba4545bec4..34a90d8a90e 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/settings.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/settings.clj@@ -85,15 +85,6 @@:export? false:doc false)-(defsetting semantic-search-enabled- (deferred-tru "Enable the semantic search engine? Intended as a kill switch for the semantic search feature while dogfooding.")- :visibility :internal- :export? false- :encryption :no- :default true- :type :boolean- :doc false)-(defsetting openai-max-tokens-per-batch(deferred-tru "The maximum number of tokens sent in a single embedding API call."):type :integerdiff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/task/indexer.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/task/indexer.cljindex 09452dd8efe..2a331c5eaba 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/task/indexer.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/task/indexer.clj@@ -57,8 +57,8 @@(not (semantic.u/index-exists? pgvector hnsw-index)))(semantic.core/build-hnsw-index-async!)))(semantic-search.indexer/quartz-job-run! pgvector index-metadata))- ;; Engines can activate at runtime (license applied, kill switch re-enabled,- ;; additional-search-engines set on another node); initializing from the next tick heals+ ;; Engines can activate at runtime (license applied, additional-search-engines set on+ ;; another node); initializing from the next tick heals;; every such path within seconds. Initialize all active engines, not just semantic:;; activation may also have activated dependencies with no index.(dodiff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/util.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/util.cljindex 055c32d6a66..839790af25b 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/util.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/util.clj@@ -2,7 +2,6 @@(:require[clojure.string :as str][metabase-enterprise.semantic-search.db.datasource :as semantic.db.datasource]- [metabase-enterprise.semantic-search.settings :as semantic.settings][metabase.app-db.core :as mdb][metabase.premium-features.core :as premium-features][metabase.search.engine :as search.engine]@@ -73,33 +72,26 @@(defn semantic-search-configured?"Whether to schedule the semantic-search Quartz jobs at startup.True when the `:semantic-search` feature is present and a pgvector store might exist: a dedicated- MB_PGVECTOR_DB_URL, or a Postgres app DB that [[semantic-search-capable?]] can probe to answer for sure.+ MB_PGVECTOR_DB_URL, or a Postgres app DB that [[semantic-search-available?]] can probe to answer for sure.Cheap and infallible by contract -- it runs at boot and never queries the DB."[];; The license is in this boot gate, not only the per-execution gates, so an unlicensed instance's;; scheduler stays free of no-op jobs. The asymmetry is deliberate: removing the feature at runtime lets;; the scheduled jobs no-op via semantic-search-active?, but adding it needs a restart before they- ;; schedule. The kill switch and engine activity stay per-execution so they never need one.+ ;; schedule. Engine activity stays per-execution so it never needs one.(and (premium-features/has-feature? :semantic-search)(or (semantic.db.datasource/dedicated-url-configured?)(= :postgres (mdb/db-type)))))-(defn semantic-search-capable?+(defn semantic-search-available?"Does this instance have the infrastructure for semantic search: the premium feature and a pgvector DB.- Deliberately excludes the kill switch, which is checked per execution so it works at runtime."+ Engine selection and hygiene tasks key off this."[];; Feature first: the pgvector check may probe the app DB, and instances that can't use the answer;; must never probe.(and (premium-features/has-feature? :semantic-search)(semantic.db.datasource/pgvector-configured?)))-(defn semantic-search-available?- "Whether semantic search can run on this instance: capable and not disabled by the kill switch.- Engine selection, hygiene tasks, and metrics key off this."- []- (and (semantic-search-capable?)- (semantic.settings/semantic-search-enabled)))-(defn semantic-search-active?"Is the semantic index being maintained on this instance?Anything that writes to the index (or pays for embeddings) must gate on this, not on availability:diff --git a/src/metabase/search/core.clj b/src/metabase/search/core.cljindex 879be246b09..208308666a8 100644--- a/src/metabase/search/core.clj+++ b/src/metabase/search/core.clj@@ -1,6 +1,8 @@(ns metabase.search.core"NOT the API namespace for the search module!! See [[metabase.search]] instead."(:require+ [clojure.string :as str]+ [environ.core :as env][metabase.analytics-interface.core :as analytics][metabase.analytics.core :as analytics.core][metabase.lib-be.core :as lib-be]@@ -11,8 +13,10 @@[metabase.search.ingestion :as search.ingestion][metabase.search.spec :as search.spec][metabase.search.util :as search.util]+ [metabase.startup.core :as startup][metabase.tracing.core :as tracing][metabase.util :as u]+ [metabase.util.i18n :refer [trs]][metabase.util.log :as log][potemkin :as p]))@@ -94,6 +98,31 @@[](seq (search.engine/active-engines)))+(defn check-for-removed-env-vars!+ "Fail startup when the removed MB_SEMANTIC_SEARCH_ENABLED kill switch is still set, naming the exact+ MB_SEARCH_ENGINE value the switch would have fallen back to when semantic is now serving search."+ []+ ;; An empty value is "explicitly unset" per the usual env-var semantics, so only a non-blank value trips this.+ (when-not (str/blank? (env/env :mb-semantic-search-enabled))+ (let [engines (search.engine/supported-engines)+ semantic-serving? (= :search.engine/semantic (first engines))+ fallback (when semantic-serving? (second engines))]+ (throw (ex-info+ (cond+ fallback+ (trs "MB_SEMANTIC_SEARCH_ENABLED has been removed. Set MB_SEARCH_ENGINE={0} to keep semantic search off, then remove MB_SEMANTIC_SEARCH_ENABLED."+ (name fallback))++ semantic-serving?+ (trs "MB_SEMANTIC_SEARCH_ENABLED has been removed. Semantic search is the only supported engine and cannot be disabled; remove MB_SEMANTIC_SEARCH_ENABLED.")++ :else+ (trs "MB_SEMANTIC_SEARCH_ENABLED has been removed; remove it from your configuration."))+ {:env-var "MB_SEMANTIC_SEARCH_ENABLED"})))))++(defmethod startup/def-startup-validation! ::check-for-removed-env-vars [_]+ (check-for-removed-env-vars!))+(defn init-index!"Ensure there is an index ready to be populated."[& {:as opts}]diff --git a/src/metabase/startup/core.clj b/src/metabase/startup/core.cljindex 0d729a94572..2ed9a9a1d05 100644--- a/src/metabase/startup/core.clj+++ b/src/metabase/startup/core.clj@@ -1,9 +1,22 @@(ns metabase.startup.core- "Defines the `def-startup-logic!` and `def-shutdown-logic!` multimethods for server lifecycle hooks."+ "Defines the server lifecycle multimethods. `def-startup-validation!` and `def-startup-logic!` run at+ startup: validations first (a throw aborts startup), then initialization logic. `def-shutdown-logic!`+ runs at graceful shutdown."(:require[metabase.util :as u][metabase.util.log :as log]))+(defmulti def-startup-validation!+ "Registers a startup precondition: all run before any `def-startup-logic!`, and a throw from one aborts startup.+ Order among validations is unspecified.+ Use this over `def-startup-logic!` for checks that must fail the boot before initialization logic runs.+ The dispatch value is any unique keyword, used only for logging.++ (defmethod startup/def-startup-validation! ::ExampleCheck [_]+ (when (misconfigured?) (throw (ex-info \"Bad config\" {}))))"+ {:arglists '([validation-name])}+ keyword)+(defmulti def-startup-logic!"Runs initialization logic with a given name. All implementations of this method are called once and onlyonce when the server is starting up. Task namespaces (`metabase.*.task`) should add new@@ -31,19 +44,36 @@(defmethod startup-priority :default [_] 500)-(defn run-startup-logic!- "Call all implementations of `def-startup-logic!`. Called by metabase.core/init!- Methods run in ascending [[startup-priority]] order, with ties broken by dispatch-value name- for determinism."- []- (doseq [[k f] (sort-by (fn [[k _]] [(startup-priority k) (name k)])- (methods def-startup-logic!))]+(defn- run-impl!+ "Invoke startup impl `f` for dispatch value `k`.+ When `abort-on-error?` a throw propagates (aborting startup); otherwise it is logged and swallowed."+ [k f abort-on-error?]+ (if abort-on-error?+ (f k)(try- (log/infof "Running setup logic %s %s" (u/format-color 'green (name k)) (u/emoji "☑\uFE0F"))(f k)(catch Throwable e(log/errorf e "Error initializing startup logic %s" k)))))+(defn- run-startup-logic!*+ "Run `validation-impls` (each aborts the boot on a throw), then `setup-impls` (throws logged and skipped).+ Each is a seq of `[dispatch-value f]` pairs."+ [validation-impls setup-impls]+ (doseq [[k f] validation-impls]+ (log/infof "Running startup validation %s" (u/format-color 'green (name k)))+ (run-impl! k f true))+ (doseq [[k f] setup-impls]+ (log/infof "Running setup logic %s %s" (u/format-color 'green (name k)) (u/emoji "☑️"))+ (run-impl! k f false)))++(defn run-startup-logic!+ "Run all `def-startup-validation!` implementations (a throw aborts startup), then all `def-startup-logic!`+ implementations (errors logged and skipped). Called by metabase.core/init!+ Logic methods run in ascending [[startup-priority]] order, with ties broken by dispatch-value name."+ []+ (run-startup-logic!* (methods def-startup-validation!)+ (sort-by (fn [[k _]] [(startup-priority k) (name k)]) (methods def-startup-logic!))))+(defmulti def-shutdown-logic!"Runs shutdown logic with a given name. All implementations are called during graceful servershutdown. Counterpart to [[def-startup-logic!]].diff --git a/test/metabase/search/engine_test.clj b/test/metabase/search/engine_test.cljindex cc620a740cf..44657b9ede3 100644--- a/test/metabase/search/engine_test.clj+++ b/test/metabase/search/engine_test.clj@@ -1,11 +1,14 @@(ns metabase.search.engine-test(:require[clojure.test :refer :all]+ [environ.core :as env]+ [metabase.search.core :as search][metabase.search.engine :as search.engine];; Loaded for side effects: registers the engine implementations.[metabase.search.init][metabase.search.settings :as search.settings][metabase.search.task.search-index :as task.search-index]+ [metabase.startup.core :as startup][metabase.task.core :as task][metabase.test :as mt][metabase.test.util :as tu]))@@ -49,6 +52,43 @@(with-engines {:supported all-engines :configured :fulltext}(is (= :search.engine/appdb (search.engine/default-engine))))))+(deftest check-for-removed-env-vars-test+ (testing "the removed MB_SEMANTIC_SEARCH_ENABLED kill switch fails startup"+ (with-redefs [env/env {:mb-semantic-search-enabled "false"}]+ (testing "naming the exact fallback engine when semantic is serving search"+ (with-engines {:supported all-engines}+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Set MB_SEARCH_ENGINE=appdb to keep semantic search off"+ (search/check-for-removed-env-vars!))))+ (testing "the fallback follows precedence, not a hardcoded engine"+ (with-engines {:supported #{:search.engine/semantic :search.engine/in-place}}+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Set MB_SEARCH_ENGINE=in-place"+ (search/check-for-removed-env-vars!)))))+ (testing "when semantic is the only supported engine there is nothing to fall back to"+ (with-engines {:supported #{:search.engine/semantic}}+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"only supported engine and cannot be disabled"+ (search/check-for-removed-env-vars!))))))+ (testing "with a plain remove-it message when another engine already serves search"+ (with-engines {:supported #{:search.engine/appdb :search.engine/in-place}}+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"remove it from your configuration"+ (search/check-for-removed-env-vars!)))))+ (testing "the check runs as a startup validation so a throw aborts the boot"+ (is (contains? (methods startup/def-startup-validation!)+ :metabase.search.core/check-for-removed-env-vars)))))+ (testing "startup proceeds when the kill switch is absent"+ (with-redefs [env/env {}]+ (is (nil? (search/check-for-removed-env-vars!)))))+ (testing "an empty value counts as unset, not a leftover"+ (with-redefs [env/env {:mb-semantic-search-enabled ""}]+ (is (nil? (search/check-for-removed-env-vars!))))))+(deftest search-engine-setting-test(testing "the setting computes the resolved engine when no value is configured"(with-engines {:supported #{:search.engine/appdb :search.engine/in-place}}diff --git a/test/metabase/startup/core_test.clj b/test/metabase/startup/core_test.cljnew file mode 100644index 00000000000..d22762acb43--- /dev/null+++ b/test/metabase/startup/core_test.clj@@ -0,0 +1,18 @@+(ns metabase.startup.core-test+ (:require+ [clojure.test :refer :all]+ [metabase.startup.core :as startup]))++(deftest run-startup-logic-test+ (let [ran (atom [])+ rec (fn [k] [k (fn [_] (swap! ran conj k))])+ boom (fn [k] [k (fn [_] (throw (ex-info "boom" {})))])]+ (testing "a throwing validation aborts before any logic runs"+ (reset! ran [])+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"boom"+ (#'startup/run-startup-logic!* [(rec ::v1) (boom ::v2)] [(rec ::l1)])))+ (is (= [::v1] @ran) "the validation before the throw ran; no logic ran"))+ (testing "validations run before logic, and a throwing logic impl is swallowed"+ (reset! ran [])+ (is (nil? (#'startup/run-startup-logic!* [(rec ::v1)] [(rec ::l1) (boom ::l2) (rec ::l3)])))+ (is (= [::v1 ::l1 ::l3] @ran) "validation first, then logic; the throwing logic impl did not stop the rest"))))