Skip to content

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

Skip cache auto-refresh attempts for routed dbs (#70082)

John Swanson904a701e7168
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 1e993e18095..ef135f726d7 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -2736,6 +2736,7 @@
:model-imports #{:model/CacheConfig
:model/Card
:model/Dashboard
+ :model/DatabaseRouter
:model/Query
:model/QueryCache
:model/QueryExecution}}
diff --git a/enterprise/backend/src/metabase_enterprise/cache/task/refresh_cache_configs.clj b/enterprise/backend/src/metabase_enterprise/cache/task/refresh_cache_configs.clj
index 8627ce3ff6f..f59ab07e24a 100644
--- a/enterprise/backend/src/metabase_enterprise/cache/task/refresh_cache_configs.clj
+++ b/enterprise/backend/src/metabase_enterprise/cache/task/refresh_cache_configs.clj
@@ -57,25 +57,35 @@
definition contains a card-id, an optional dashboard-id, and a list of queries to rerun."
[refresh-defs]
(fn []
- (let [card-ids (into #{} (map :card-id refresh-defs))
- cards-by-id (t2/select-pk->fn identity :model/Card :id [:in card-ids])]
+ (let [card-ids (into #{} (map :card-id refresh-defs))
+ cards-by-id (t2/select-pk->fn identity :model/Card :id [:in card-ids])
+ all-db-ids (into #{} (keep (comp :database_id val)) cards-by-id)
+ router-db-ids (if (seq all-db-ids)
+ (t2/select-fn-set :database_id
+ :model/DatabaseRouter
+ :database_id [:in all-db-ids])
+ #{})]
(doseq [{:keys [card-id dashboard-id queries]} refresh-defs]
- ;; Annotate the query with its cache strategy in the format expected by the QP
- (let [cache-strategy (strategies/cache-strategy (get cards-by-id card-id) dashboard-id)]
- (doseq [query queries]
- (try
- (qp/process-query
- (qp/userland-query
- (-> query
- (assoc-in [:middleware :ignore-cached-results?] true)
- (assoc :cache-strategy cache-strategy))
- {:executed-by nil
- :context :cache-refresh
- :card-id card-id
- :dashboard-id dashboard-id})
- discarding-rff)
- (catch Exception e
- (log/debugf "Error refreshing cache for card %s: %s" card-id (ex-message e))))))))))
+ (let [card (get cards-by-id card-id)]
+ ;; Skip cards on router databases — cache refresh runs without a user context, so database
+ ;; routing will always fail for these cards (anonymous access is not allowed).
+ (when-not (contains? router-db-ids (:database_id card))
+ ;; Annotate the query with its cache strategy in the format expected by the QP
+ (let [cache-strategy (strategies/cache-strategy card dashboard-id)]
+ (doseq [query queries]
+ (try
+ (qp/process-query
+ (qp/userland-query
+ (-> query
+ (assoc-in [:middleware :ignore-cached-results?] true)
+ (assoc :cache-strategy cache-strategy))
+ {:executed-by nil
+ :context :cache-refresh
+ :card-id card-id
+ :dashboard-id dashboard-id})
+ discarding-rff)
+ (catch Exception e
+ (log/debugf "Error refreshing cache for card %s: %s" card-id (ex-message e))))))))))))
(defn- duration-ago
[{:keys [duration unit]}]
diff --git a/enterprise/backend/test/metabase_enterprise/cache/task/refresh_cache_configs_test.clj b/enterprise/backend/test/metabase_enterprise/cache/task/refresh_cache_configs_test.clj
index acedd1f5c68..ccdf1791d76 100644
--- a/enterprise/backend/test/metabase_enterprise/cache/task/refresh_cache_configs_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/cache/task/refresh_cache_configs_test.clj
@@ -663,6 +663,33 @@
(finally
(delete-cache-entry! original-cache-entry))))))))))))
+(deftest refresh-task-skips-router-database-cards-test
+ (mt/with-premium-features #{:cache-granular-controls :cache-preemptive}
+ (testing "Cards on router databases are skipped by refresh-task"
+ (binding [qp.util/*execute-async?* false
+ task.cache/*run-cache-refresh-async* false]
+ (let [query {:database (mt/id), :type :native, :native {:query "SELECT 1;"}}]
+ (mt/with-temp [:model/Card {card-id :id} {:name "Card on router DB"
+ :database_id (mt/id)
+ :dataset_query query}
+ :model/CacheConfig _ {:model "question"
+ :model_id card-id
+ :strategy :schedule
+ :refresh_automatically true
+ :next_run_at nil
+ :config {:schedule "0 0 * * * ?"}}]
+ ;; Run the card once to populate the Query + QueryExecution tables
+ (run-query-for-card-id card-id [])
+ (mt/with-temp [:model/DatabaseRouter _ {:database_id (mt/id)
+ :user_attribute "db_name"}]
+ (let [process-query-calls (atom 0)]
+ (mt/with-dynamic-fn-redefs [qp/process-query (fn [& _args]
+ (swap! process-query-calls inc)
+ {:status :failed})]
+ (@#'task.cache/refresh-cache-configs!)
+ (testing "process-query should not have been called for the router-DB card"
+ (is (= 0 @process-query-calls))))))))))))
+
(deftest cache-preemptive-feature-flag-test
(testing "Sanity check that we are correctly enforcing the :cache-preemptive feature flags"
(mt/with-temp [:model/Card {card-id :id} {:name "Cached card"