Skip to content

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

15.4 KB
Raw Blame
(ns metabase-enterprise.search.semantic.task.index-cleanup
  "Task to clean up inactive and stale semantic search indexes."
  (:require
   [clojure.string :as str]
   [clojurewerkz.quartzite.jobs :as jobs]
   [clojurewerkz.quartzite.schedule.cron :as cron]
   [clojurewerkz.quartzite.triggers :as triggers]
   [honey.sql :as sql]
   [honey.sql.helpers :as sql.helpers]
   [java-time.api :as t]
   [metabase-enterprise.search.semantic.env :as semantic.env]
   [metabase-enterprise.search.semantic.index :as semantic.index]
   [metabase-enterprise.search.semantic.index-metadata :as semantic.index-metadata]
   [metabase-enterprise.search.semantic.settings :as semantic.settings]
   [metabase-enterprise.search.semantic.util :as semantic.u]
   [metabase.task.core :as task]
   [metabase.util.log :as log]
   [next.jdbc :as jdbc]
   [next.jdbc.result-set :as jdbc.rs]))

(set! *warn-on-reflection* true)

;; Both orphan sweeps feed table names to DROP, so in shared app-db mode the schema scoping keeps a LIKE
;; pattern from matching and dropping application tables.

(defn- scope-where-to-schema
  "Restrict an `information_schema.tables` WHERE (an aliased-`:t` `[:and ...]` vector) to the module schema.
  A no-op without a schema (dedicated mode)."
  [where schema]
  (cond-> where schema (conj [:= :t.table_schema [:inline schema]])))

(defn- requalify-table-names
  "Prefix bare `information_schema` table names with the module schema so they match how the metadata table
  stores them. A no-op in dedicated mode."
  [schema table-names]
  (map #(cond->> % schema (str schema ".")) table-names))

(defn- like-escape
  "Escape LIKE wildcards so `s` only matches itself (Postgres's default escape character is backslash)."
  [s]
  (str/replace s #"([\\%_])" "\\\\$1"))

(defn- orphan-index-tables
  "Returns a list of semantic search index tables which are not registered in the metadata table's
  table_name column. Not expected to occur (index tables are created in the same transaction that
  registers them), but if it does, these tables can be dropped.

  Candidacy is decided by table-name shape ([[semantic.index/index-table-name?]]); the control-plane
  tables (metadata/control/gate) share the index_ prefix but are excluded both by shape and by name.
  Matching is index-table-qualifier-aware, so a config with a distinguishing qualifier (e.g. test
  configs) only ever sees its own tables. Only base tables are candidates — never views.

  With a `:schema` (shared app-db mode) only tables inside the module's schema are considered, and the
  returned names are schema-qualified to match how the metadata table stores them. Without one
  (dedicated pgvector DB) the scan is limited to current_schema(), where the unqualified DROP that
  consumes these names resolves — tables in other schemas would be detected but never dropped."
  [pgvector {:keys [metadata-table-name control-table-name gate-table-name index-table-qualifier schema]}]
  (let [;; the qualifier may embed the app-db schema ("<schema>.%s") — information_schema reports bare
        ;; table names, so build the LIKE pattern and the qualifier stripping from its name part only
        [prefix suffix]     (str/split (semantic.u/table-name-part index-table-qualifier) #"%s" -1)
        like-pattern        (str (like-escape prefix) "index\\_%" (like-escape suffix))
        control-plane-names (into #{} (map semantic.u/table-name-part)
                                  [metadata-table-name control-table-name gate-table-name])
        index-shaped?       (fn [table-name]
                              ;; the LIKE prefilter guarantees the literal qualifier prefix/suffix, so
                              ;; strip them to recover the bare index_… name the naming code produced
                              (semantic.index/index-table-name?
                               (subs table-name (count prefix) (- (count table-name) (count suffix)))))
        ;; information_schema reports bare names; stored metadata names are qualified when we have a schema
        stored-name (if schema
                      [:|| [:inline (str schema ".")] :t.table_name]
                      :t.table_name)
        orphaned-tables-sql
        (-> {:select [:t.table_name]
             :from [[:information_schema.tables :t]]
             :left-join [[(keyword metadata-table-name) :meta]
                         [:= :meta.table_name stored-name]]
             :where [:and
                     [:like :t.table_name ^:allow-raw-sql [:inline like-pattern]]
                     [:= :meta.table_name nil]
                     [:= :t.table_type [:inline "BASE TABLE"]]
                     [:= :t.table_schema (if schema
                                           [:inline schema]
                                           [:current_schema])]]}
            (sql/format :quoted true))]
    (->> (jdbc/execute! pgvector orphaned-tables-sql {:builder-fn jdbc.rs/as-unqualified-lower-maps})
         (map :table_name)
         (remove control-plane-names)
         (filter index-shaped?)
         (requalify-table-names schema))))

(defn- parse-repair-table-timestamp
  "Extracts timestamp from repair table name. Returns nil if parsing fails."
  [table-name]
  (try
    (when-let [[_ timestamp-str] (re-find #"^repair_(\d+)_" table-name)]
      (t/instant (parse-long timestamp-str))) ; Convert milliseconds to instant
    (catch Exception _
      nil)))

(defn- orphan-repair-tables
  "Returns a list of repair tables that are older than the retention period.
  Repair tables are named: repair_<millis-since-epoch>_<short-id>

  Repair tables should not become orphaned under normal operation, since they should be deleted immeditaely after use.
  However, in case of a crash or failure, they may be left behind, so we clean them up after a retention period.
  With a `:schema` (shared app-db mode) only tables inside the module's schema are considered — a `repair_%`
  pattern must never match application tables — and the returned names are schema-qualified."
  [pgvector {:keys [schema]}]
  (let [retention-cutoff (t/minus (t/instant) (t/hours (semantic.settings/repair-table-retention-hours)))
        repair-tables-sql (-> {:select [:t.table_name]
                               :from [[:information_schema.tables :t]]
                               :where (scope-where-to-schema [:and [:like :t.table_name ^:allow-raw-sql [:inline "repair_%"]]] schema)}
                              (sql/format :quoted true))
        all-repair-tables (->> (jdbc/execute! pgvector repair-tables-sql {:builder-fn jdbc.rs/as-unqualified-lower-maps})
                               (map :table_name))
        old-tables (->> all-repair-tables
                        (filter (fn [table-name]
                                  (when-let [table-timestamp (parse-repair-table-timestamp table-name)]
                                    (t/before? table-timestamp retention-cutoff))))
                        (requalify-table-names schema))]
    (when (seq old-tables)
      (log/infof "Found %d orphaned repair tables older than %d hours"
                 (count old-tables)
                 (semantic.settings/repair-table-retention-hours)))
    old-tables))

(defn- cleanup-orphan-repair-tables!
  "Cleans up repair tables that are older than the retention period."
  [pgvector index-metadata]
  (let [orphan-tables (orphan-repair-tables pgvector index-metadata)]
    (when (seq orphan-tables)
      (let [tables-to-drop (map keyword orphan-tables)
            drop-table-sql (sql/format
                            (apply sql.helpers/drop-table :if-exists tables-to-drop)
                            :quoted true)]
        (log/infof "Dropping %d orphaned repair tables: %s"
                   (count tables-to-drop)
                   (str/join ", " orphan-tables))
        (jdbc/execute! pgvector drop-table-sql)))))

(defn- stale-index-tables
  "Returns a list of semantic search index tables that are considered stale and can be dropped.
   An index is considered stale if it is not the active index and has not been used or updated
   within the retention period defined in settings."
  [pgvector {:keys [metadata-table-name control-table-name]}]
  (let [retention-cutoff     (t/minus (t/offset-date-time) (t/hours (semantic.settings/stale-index-retention-hours)))
        stale-index-sql (-> {:select [:meta.table_name]
                             :from [[(keyword control-table-name) :control]]
                             :join [[(keyword metadata-table-name) :meta]
                                    [:!= :meta.id :control.active_id]]
                             :where [:and
                                     [:< :meta.index_created_at retention-cutoff]
                                     ;; If indexer_last_seen is set, we can use it as a proxy for when the index was
                                     ;; last used.
                                     [:or
                                      [:= :meta.indexer_last_seen nil]
                                      [:< :meta.indexer_last_seen retention-cutoff]]]}
                            (sql/format :quoted true))]
    (->> (jdbc/execute! pgvector stale-index-sql {:builder-fn jdbc.rs/as-unqualified-lower-maps})
         (map :table_name))))

(defn- indexer-ran-recently?
  "Check if the indexer has run within the specified number of hours."
  [metadata-row hours-ago]
  (when-let [last-poll-inst (t/instant (:indexer_last_poll metadata-row))]
    (t/after?
     last-poll-inst
     (t/minus (t/instant) (t/hours hours-ago)))))

(defn- get-active-index-metadata-row
  "Get the metadata row for the currently active index."
  [pgvector {:keys [metadata-table-name control-table-name]}]
  (jdbc/execute-one! pgvector
                     (-> {:select [:m.*]
                          :from [[(keyword control-table-name) :c]]
                          :join [[(keyword metadata-table-name) :m] [:= :m.id :c.active_id]]}
                         (sql/format :quoted true))
                     {:builder-fn jdbc.rs/as-unqualified-lower-maps}))

(defn- cleanup-old-gate-tombstones!
  "Cleans up old tombstone records from the gate table, if the indexer has run recently.
  Ensures that the gate table does not grow indefinitely with old tombstone records
  (where document and document_hash are null)."
  [pgvector index-metadata]
  (try
    (let [retention-hours     (semantic.settings/tombstone-retention-hours)
          active-metadata-row (get-active-index-metadata-row pgvector index-metadata)]
      (if-not (indexer-ran-recently? active-metadata-row retention-hours)
        (log/infof "Skipping tombstone cleanup: indexer has not run within the last %s hours"
                   retention-hours)
        (let [{:keys [gate-table-name]} index-metadata
              retention-cutoff (t/minus (t/offset-date-time)
                                        (t/hours retention-hours))
              tombstone-cleanup-sql (-> {:delete-from [(keyword gate-table-name)]
                                         :where [:and
                                                 [:= :document nil]
                                                 [:= :document_hash nil]
                                                 [:< :gated_at retention-cutoff]]}
                                        (sql/format :quoted true))
              deleted-count (::jdbc/update-count (jdbc/execute-one! pgvector tombstone-cleanup-sql))]
          (when (pos? deleted-count)
            (log/infof "Cleaned up %d old tombstone records from gate table" deleted-count)))))
    (catch Exception e
      (log/errorf "Failed to clean up tombstone records from gate table: %s" (ex-message e)))))

(defn- drop-index-table!
  "Drops one stale/orphaned index table, isolating failures: one undroppable table (e.g. one with a
  dependent view) must not abort the rest of the cleanup batch.
  Returns :dropped, :missing (referenced table no longer exists), or :failed."
  [pgvector kind table-name]
  (try
    (log/infof "Dropping %s semantic search index: %s" kind table-name)
    (jdbc/execute! pgvector (sql/format (sql.helpers/drop-table (keyword table-name)) :quoted true))
    :dropped
    ;; no IF EXISTS: a vacuous drop must not count as :dropped, so let 42P01 (undefined_table) tell us
    (catch java.sql.SQLException e
      (if (= "42P01" (.getSQLState e))
        (do (log/infof "Skipping %s semantic search index %s: table no longer exists" kind table-name)
            :missing)
        (do (log/warnf "Failed to drop %s semantic search index %s: %s" kind table-name (ex-message e))
            :failed)))
    (catch Exception e
      (log/warnf "Failed to drop %s semantic search index %s: %s" kind table-name (ex-message e))
      :failed)))

(defn- cleanup-stale-indexes!
  [pgvector index-metadata]
  (let [stale-table-names    (stale-index-tables pgvector index-metadata)
        orphaned-table-names (orphan-index-tables pgvector index-metadata)
        found                (+ (count stale-table-names) (count orphaned-table-names))]
    (when (pos? found)
      (log/infof "Found %d semantic search index tables to clean up" found)
      (let [outcomes (concat (mapv #(drop-index-table! pgvector "stale" %) stale-table-names)
                             (mapv #(drop-index-table! pgvector "orphaned" %) orphaned-table-names))
            dropped  (count (filter #{:dropped} outcomes))]
        (log/infof "Dropped %d of %d semantic search index tables" dropped found)))))

(defn- cleanup-stale-indexes-and-gate-tombstones!
  []
  (when (semantic.u/semantic-search-available?)
    (let [pgvector       (semantic.env/get-pgvector-datasource!)
          index-metadata (semantic.env/get-index-metadata)]
      ;; Available but never initialized is a steady state (semantic neither default nor additional,
      ;; or app-db mode with the engine disabled); the bookkeeping tables only exist after init, and
      ;; the cleanup queries would throw without them.
      ;; Still runs while merely available, so an opted-out instance's leftover indexes get cleaned.
      (when (semantic.index-metadata/control-and-metadata-tables-exist? pgvector index-metadata)
        (cleanup-stale-indexes! pgvector index-metadata)
        (cleanup-old-gate-tombstones! pgvector index-metadata))
      ;; Repair-table cleanup is information_schema-driven, so it needs no bookkeeping tables and runs
      ;; outside the guard above. Shared app-db mode scopes the sweep to the module schema; dedicated mode
      ;; owns the whole DB, so its `repair_%` sweep is DB-wide by design (a mispointed URL is the caller's).
      (cleanup-orphan-repair-tables! pgvector index-metadata))))

(def ^:private cleanup-job-key (jobs/key "metabase.task.semantic-index-cleanup.job"))
(def ^:private cleanup-trigger-key (triggers/key "metabase.task.semantic-index-cleanup.trigger"))

(task/defjob SemanticIndexCleanup
  "Clean up inactive semantic search index tables"
  {:saved-class "metabase_enterprise.semantic_search.task.index_cleanup.SemanticIndexCleanup"
   :concurrent? false}
  [_ctx]
  (cleanup-stale-indexes-and-gate-tombstones!))

(defmethod task/init! ::SemanticIndexCleanup [_]
  (when (semantic.u/semantic-search-configured?)
    (let [job (jobs/build
               (jobs/of-type SemanticIndexCleanup)
               (jobs/with-identity cleanup-job-key))
          trigger (triggers/build
                   (triggers/with-identity cleanup-trigger-key)
                   (triggers/start-now)
                   (triggers/with-schedule
                    ;; Run daily at 3 AM
                    (cron/cron-schedule "0 0 3 * * ? *")))]
      (task/schedule-task! job trigger))))

(comment
  (task/job-exists? cleanup-job-key)
  (task/trigger-now! cleanup-job-key))