Skip to content

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

Make gate and indexer constants configurable (#62358)

lbrdnke2dc67831d9e
diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj
index ff95da2a6ae..a5418879566 100644
--- a/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj
+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj
@@ -64,7 +64,6 @@
(log/error e "Error executing semantic search")
(throw (ex-info "Error executing semantic search" {:type :semantic-search-error} e)))))
-;; TODO: tx-write
(defenterprise update-index!
"Enterprise implementation of semantic index updating."
:feature :semantic-search
diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/gate.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/gate.clj
index 7c5211b458b..1f322a0f609 100644
--- a/enterprise/backend/src/metabase_enterprise/semantic_search/gate.clj
+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/gate.clj
@@ -21,6 +21,7 @@
(:require
[buddy.core.hash :as buddy-hash]
[honey.sql :as sql]
+ [metabase-enterprise.semantic-search.settings :as semantic.settings]
[metabase.util :as u]
[metabase.util.json :as json]
[metabase.util.log :as log]
@@ -40,15 +41,13 @@
;; we should see tx timeout policies set accordingly e.g: SET LOCAL statement_timeout
;; we should use one transaction per write to limit opportunity for error here.
(def ^Duration gate-write-timeout
- "The time in which gate-documents! transactions should commit.
+ "[[semantic.settings/ee-search-gate-write-timeout]] wrapped in seconds duration.
+
+ The time in which gate-documents! transactions should commit.
Optimistically enforced by SET LOCAL statement_timeout.
This value is used to determine the lag-tolerance, and the degree to which we might
expect to see out of order commits (e.g. lag-tolerance = gate-write-timeout * 2)."
- (Duration/ofSeconds 5))
-
-(def max-batch-size
- "The maximum number of documents that can be sent to (gate-documents!) without causing an error"
- 512)
+ (Duration/ofSeconds (semantic.settings/ee-search-gate-write-timeout)))
(defn deleted-search-doc->gate-doc
"Converts a deleted document reference into a gate table record.
@@ -107,9 +106,7 @@
Returns a count of rows actually updated/inserted."
[pgvector index-metadata gate-document-batch]
{:pre [;; countable only
- (seqable? gate-document-batch)
- ;; too many documents and we might see buffer size limits get hit in the driver, TODO: bench/play with this - document expectations
- (<= (bounded-count (inc max-batch-size) gate-document-batch) max-batch-size)]}
+ (seqable? gate-document-batch)]}
;; We will fix the transaction policy here (rather than letting caller decide) to encapsulate timeout behaviour
;; and to ensure the READ COMMITTED :isolation level is set.
;; NOTE: we depend on the UPDATE behaviour described here: https://www.postgresql.org/docs/current/transaction-iso.html#XACT-READ-COMMITTED
@@ -172,10 +169,14 @@
- :lag-tolerance (Duration) (should be at least gate-write-timeout + some buffer for timeout variation)
Call (next-watermark) on the result to determine where to poll next."
- [pgvector index-metadata watermark & {:keys [^Duration lag-tolerance
- limit]
- :or {lag-tolerance (.multipliedBy gate-write-timeout 2) ; heuristic: still depends on postgres enforcing timeouts. We might still need a slow-pass over everything occasionally in the future.
- limit 1000}}]
+ [pgvector index-metadata watermark
+ & {:keys [^Duration lag-tolerance
+ limit]
+ :or {;; heuristic: still depends on postgres enforcing timeouts. We might still need a slow-pass over
+ ;; everything occasionally in the future.
+ lag-tolerance (.multipliedBy gate-write-timeout
+ (semantic.settings/ee-search-indexer-lag-tolerance-multiplier))
+ limit 1000}}]
{:pre [(pos? limit)]}
(let [;; IMPORTANT: these Instant conversion must only be used for heuristic / approximate conditions (such as confidence-time)
;; as their precision does not match the actual SQL clock_timestamp() value.
diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/indexer.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/indexer.clj
index 69ded2afe3c..c32a8f03892 100644
--- a/enterprise/backend/src/metabase_enterprise/semantic_search/indexer.clj
+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/indexer.clj
@@ -20,6 +20,7 @@
[metabase-enterprise.semantic-search.gate :as semantic.gate]
[metabase-enterprise.semantic-search.index :as semantic.index]
[metabase-enterprise.semantic-search.index-metadata :as semantic.index-metadata]
+ [metabase-enterprise.semantic-search.settings :as semantic.settings]
[metabase.util.log :as log]
[next.jdbc :as jdbc]
[next.jdbc.result-set :as jdbc.rs])
@@ -27,9 +28,9 @@
(set! *warn-on-reflection* true)
-(def ^:private poll-limit 1000)
-
-(def ^:private lag-tolerance (.multipliedBy semantic.gate/gate-write-timeout 2))
+(def ^:private lag-tolerance
+ (.multipliedBy semantic.gate/gate-write-timeout
+ (semantic.settings/ee-search-indexer-lag-tolerance-multiplier)))
(defn indexing-step
"Runs a single blocking step of the indexing loop."
@@ -37,7 +38,7 @@
(letfn [(poll []
(let [{:keys [watermark]} @indexing-state]
(semantic.gate/poll pgvector index-metadata watermark
- :limit poll-limit
+ :limit (semantic.settings/ee-search-indexer-poll-limit)
:lag-tolerance lag-tolerance)))
;; when polling within the lag-tolerance window we need to expect
;; to see the same gate entries when polling.
@@ -168,13 +169,13 @@
(on-indexing-interrupted)
(recur))))))
-(def default-max-run-duration
- "The default amount of time we expect to run the loop for before yielding to quartz."
- (Duration/ofMinutes 60))
+(def max-run-duration
+ "Duration minutes wrapper for [[semantic.settings/ee-search-indexer-max-run-duration]]."
+ (Duration/ofMinutes (semantic.settings/ee-search-indexer-max-run-duration)))
-(def default-exit-early-cold-duration
- "The default time we should wait to see new data before yielding back to quartz."
- (Duration/ofSeconds 30))
+(def exit-early-cold-duration
+ "Duration seconds wrapper for [[semantic.settings/ee-search-indexer-exit-early-cold-duration]]."
+ (Duration/ofSeconds (semantic.settings/ee-search-indexer-exit-early-cold-duration)))
(defn init-indexing-state
"Initialises the indexing state variable, contains the watermark position, statistics on
@@ -190,8 +191,8 @@
:last-seen-candidates (if last-seen #{last-seen} #{})
:last-indexed-count 0
:last-poll-count 0
- :max-run-duration default-max-run-duration
- :exit-early-cold-duration default-exit-early-cold-duration})))
+ :max-run-duration max-run-duration
+ :exit-early-cold-duration exit-early-cold-duration})))
(defn quartz-job-run!
"Quartz job (execute) implementation. Determines the active index before running
diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/pgvector_api.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/pgvector_api.clj
index eb366a7f8e4..ddeb26ee6a9 100644
--- a/enterprise/backend/src/metabase_enterprise/semantic_search/pgvector_api.clj
+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/pgvector_api.clj
@@ -13,6 +13,7 @@
[metabase-enterprise.semantic-search.gate :as semantic.gate]
[metabase-enterprise.semantic-search.index :as semantic.index]
[metabase-enterprise.semantic-search.index-metadata :as semantic.index-metadata]
+ [metabase-enterprise.semantic-search.settings :as semantic.settings]
[metabase.util :as u]
[metabase.util.log :as log])
(:import (java.time Instant)))
@@ -96,7 +97,7 @@
[pgvector index-metadata documents]
(let [now (Instant/now)]
(transduce
- (partition-all (min 512 semantic.gate/max-batch-size))
+ (partition-all (min 512 (semantic.settings/ee-search-gate-max-batch-size)))
(completing
(fn [acc documents]
(->> documents
@@ -114,7 +115,7 @@
[pgvector index-metadata model ids]
(let [now (Instant/now)]
(transduce
- (partition-all (min 512 semantic.gate/max-batch-size))
+ (partition-all (min 512 (semantic.settings/ee-search-gate-max-batch-size)))
(completing
(fn [acc ids]
(->> ids
diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/settings.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/settings.clj
index ba043df8d16..48d44f4164c 100644
--- a/enterprise/backend/src/metabase_enterprise/semantic_search/settings.clj
+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/settings.clj
@@ -87,3 +87,48 @@
:export? false
:visibility :internal
:doc "Number of threads to use for batched index updates, including embedding requests")
+
+(defsetting ee-search-gate-write-timeout
+ (str "Timeout of gate write statements in seconds. Used to determine lag tolerance of the indexer (see the "
+ "[[metabase-enterprise.semantic-search.gate/poll]]) in conjunction "
+ "with `ee-search-indexer-lag-tolerance-multiplier`.")
+ :type :integer
+ :default 5
+ :export? false
+ :visibility :internal)
+
+(defsetting ee-search-gate-max-batch-size
+ "The maximum number of documents that can be sent to `gate-documents!` without causing an error."
+ :type :integer
+ :default 512
+ :export? false
+ :visibility :internal)
+
+(defsetting ee-search-indexer-poll-limit
+ "Indexer poll limit."
+ :type :integer
+ :default 1000
+ :export? false
+ :visibility :internal)
+
+(defsetting ee-search-indexer-exit-early-cold-duration
+ "Number of seconds indexer should wait to see new data before yielding back to quartz."
+ :type :integer
+ :default 30
+ :export? false
+ :visibility :internal)
+
+(defsetting ee-search-indexer-max-run-duration
+ "Number of minutes we expect to run the indexer loop for before yielding to quartz."
+ :type :integer
+ :default 60
+ :export? false
+ :visibility :internal)
+
+(defsetting ee-search-indexer-lag-tolerance-multiplier
+ (str "Multiplier for computation of [[metabase-enterprise.semantic-search.indexer/lag-tolerance]]. The formula "
+ "is `ee-search-gate-write-timeout * ee-search-indexer-lag-tolerance-multiplier`.")
+ :type :integer
+ :default 2
+ :export? false
+ :visibility :internal)
diff --git a/enterprise/backend/test/metabase_enterprise/semantic_search/pgvector_api_test.clj b/enterprise/backend/test/metabase_enterprise/semantic_search/pgvector_api_test.clj
index 82706bbe302..b9adda427a4 100644
--- a/enterprise/backend/test/metabase_enterprise/semantic_search/pgvector_api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/semantic_search/pgvector_api_test.clj
@@ -6,6 +6,7 @@
[metabase-enterprise.semantic-search.index-metadata :as semantic.index-metadata]
[metabase-enterprise.semantic-search.indexer :as semantic.indexer]
[metabase-enterprise.semantic-search.pgvector-api :as semantic.pgvector-api]
+ [metabase-enterprise.semantic-search.settings :as semantic.settings]
[metabase-enterprise.semantic-search.test-util :as semantic.tu]
[metabase.search.ingestion :as search.ingestion]
[metabase.test :as mt]
@@ -236,7 +237,8 @@
(when-not (.join thread (Duration/ofSeconds 30))
(log/fatal "Indexing loop thread not exiting during test!")))))))]
(with-redefs [semantic.indexer/sleep (fn [_]) ; do not slow down
- semantic.indexer/poll-limit 4 ; important to test poll / paging (not many docs in test-data)
+ ; important to test poll / paging (not many docs in test-data)
+ semantic.settings/ee-search-indexer-poll-limit (constantly 4)
semantic.indexer/lag-tolerance Duration/ZERO] ; if too high will slow the test down significantly
(with-open [index-ref (open-semantic-search! pgvector index-metadata embedding-model)