Skip to content

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

Use min_duration_ms in cache-strategy (#79326)

metamben7171418bdb05
diff --git a/enterprise/backend/test/metabase_enterprise/cache/strategies_test.clj b/enterprise/backend/test/metabase_enterprise/cache/strategies_test.clj
index 2c35bb9731d..2d1ac9588c0 100644
--- a/enterprise/backend/test/metabase_enterprise/cache/strategies_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/cache/strategies_test.clj
@@ -33,7 +33,7 @@
(testing "strategy = ttl"
(let [query (assoc query :cache-strategy {:type :ttl
:multiplier 10
- :min-duration-ms 0
+ :min_duration_ms 0
:avg-execution-ms 500})]
(testing "Results are stored and available immediately"
(mt/with-clock #t "2024-02-13T10:00:00Z"
@@ -54,7 +54,7 @@
(let [query (assoc query :cache-strategy {:type :duration
:duration 1
:unit "minutes"
- :min-duration-ms 0})]
+ :min_duration_ms 0})]
(testing "Results are stored and available immediately"
(mt/with-clock #t "2024-02-13T10:00:00Z"
(is (=? (mkres nil)
diff --git a/enterprise/backend/test/metabase_enterprise/database_routing/e2e_test.clj b/enterprise/backend/test/metabase_enterprise/database_routing/e2e_test.clj
index 83448d20c08..8644364430f 100644
--- a/enterprise/backend/test/metabase_enterprise/database_routing/e2e_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/database_routing/e2e_test.clj
@@ -267,7 +267,7 @@
:cache-strategy {:type :ttl
:multiplier 60
:avg-execution-ms 1000
- :min-duration-ms 1}})
+ :min_duration_ms 1}})
:data
:rows))))
(mt/with-test-user :crowberto
@@ -279,7 +279,7 @@
:cache-strategy {:type :ttl
:multiplier 60
:avg-execution-ms 1000
- :min-duration-ms 1}}))))
+ :min_duration_ms 1}}))))
(mt/with-test-user :rasta
(is (=? {:data {:rows [["destination"]]}}
(qp/process-query {:database (u/the-id router-db)
@@ -288,7 +288,7 @@
:cache-strategy {:type :ttl
:multiplier 60
:avg-execution-ms 1000
- :min-duration-ms 1}})))
+ :min_duration_ms 1}})))
(is (=? {:cache/details {:cached true}
:data {:rows [["destination"]]}}
(qp/process-query {:database (u/the-id router-db)
@@ -297,7 +297,7 @@
:cache-strategy {:type :ttl
:multiplier 60
:avg-execution-ms 1000
- :min-duration-ms 1}})))))))))))
+ :min_duration_ms 1}})))))))))))
(deftest get-field-values-endpoint-works
(mt/with-premium-features #{:database-routing}
diff --git a/enterprise/backend/test/metabase_enterprise/impersonation/cache_test.clj b/enterprise/backend/test/metabase_enterprise/impersonation/cache_test.clj
index 24c9049efe7..219a11b7ae9 100644
--- a/enterprise/backend/test/metabase_enterprise/impersonation/cache_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/impersonation/cache_test.clj
@@ -14,7 +14,7 @@
{:type :ttl
:multiplier 60
:avg-execution-ms 1000
- :min-duration-ms 1})]
+ :min_duration_ms 1})]
(mt/with-premium-features #{:advanced-permissions}
(cache-test/with-mock-cache! [save-chan purge-chan]
(while (a/poll! save-chan))
diff --git a/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj b/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj
index ae98f95047c..e33cd00d928 100644
--- a/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sandbox/query_processor/middleware/sandboxing_test.clj
@@ -886,7 +886,7 @@
:cache-strategy {:type :ttl
:multiplier 60
:avg-execution-ms 10
- :min-duration-ms 0})))]
+ :min_duration_ms 0})))]
(testing "Run the query, should not be cached"
(let [result (run-query)]
(is (= nil
@@ -1256,7 +1256,7 @@
(let [results (qp/process-query (assoc query :cache-strategy {:type :ttl
:multiplier 60
:avg-execution-ms 10
- :min-duration-ms 0}))]
+ :min_duration_ms 0}))]
{:cached? (boolean (:cached (:cache/details results)))
:num-rows (count (mt/rows results))}))]
(testing "Make sure the underlying card for the GTAP returns cached results without sandboxing"
@@ -1537,7 +1537,7 @@
:cache-strategy {:type :ttl
:multiplier 60
:avg-execution-ms 10
- :min-duration-ms 0})))]
+ :min_duration_ms 0})))]
(testing "Run query with login_attributes"
(met/with-user-attributes! :rasta {"cat" 50}
(mt/with-test-user :rasta
diff --git a/src/metabase/query_processor/middleware/cache.clj b/src/metabase/query_processor/middleware/cache.clj
index b5e04124c66..542431a23a4 100644
--- a/src/metabase/query_processor/middleware/cache.clj
+++ b/src/metabase/query_processor/middleware/cache.clj
@@ -127,7 +127,7 @@
(m/dissoc-in result [:data :rows])
{}))
(let [duration-ms (/ (- (System/nanoTime) start-time-ns) 1e6)
- min-duration-ms (:min-duration-ms strategy 0)
+ min-duration-ms (:min_duration_ms strategy 0)
;; cache any query that ran long enough -- including ones that returned no rows, so a slow empty result
;; doesn't get re-run at full cost on every request
eligible? (> duration-ms min-duration-ms)]
diff --git a/test/metabase/query_processor/middleware/cache_test.clj b/test/metabase/query_processor/middleware/cache_test.clj
index eebbd7ef0eb..edd97162513 100644
--- a/test/metabase/query_processor/middleware/cache_test.clj
+++ b/test/metabase/query_processor/middleware/cache_test.clj
@@ -10,6 +10,7 @@
[java-time.api :as t]
[medley.core :as m]
[metabase.cache.core]
+ [metabase.cache.models.cache-config :as cache-config]
[metabase.driver :as driver]
[metabase.driver.settings :as driver.settings]
[metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
@@ -147,7 +148,7 @@
{:type :ttl
:multiplier 60
:avg-execution-ms 1000
- :min-duration-ms *query-caching-min-ttl*})
+ :min_duration_ms *query-caching-min-ttl*})
(defn- test-query [query-kvs]
(merge {:cache-strategy (ttl-strategy)
@@ -313,7 +314,7 @@
(run-query :cache-strategy strategy)))))))))
(deftest not-eligible-refresh-deletes-outdated-entry-test
- (testing "when the lease winner's rerun is no longer cache-eligible (ran under min-duration-ms), the expired entry
+ (testing "when the lease winner's rerun is no longer cache-eligible (ran under min_duration_ms), the expired entry
is deleted so other processes recompute instead of serving it stale for the rest of the lease window"
(with-mock-cache! [save-chan]
(run-query :cache-strategy (assoc (ttl-strategy) :multiplier 0.1))
@@ -498,6 +499,25 @@
(is (= :cached
(run-query)))))))
+(deftest min-duration-honored-from-stored-config-test
+ (testing (str "a fast query is not cached when a stored `:ttl` config sets `min_duration_ms` above its runtime "
+ "(regression for #78340)")
+ (mt/with-model-cleanup [:model/CacheConfig]
+ (cache-config/store! (mt/user->id :crowberto)
+ {:model "root"
+ :model_id 0
+ :strategy {:type :ttl :multiplier 60 :min_duration_ms 20000}})
+ (with-mock-cache! [save-chan]
+ (let [strategy (-> (cache-config/root-strategy)
+ cache-config/row->config
+ :strategy
+ (assoc :avg-execution-ms 1000))]
+ (run-query :cache-strategy strategy)
+ (is (= :metabase.test.util.async/timed-out
+ (mt/wait-for-result save-chan)))
+ (is (= :not-cached
+ (run-query :cache-strategy strategy))))))))
+
(deftest invalid-cache-entry-test
(testing "We should handle invalid cache entries gracefully"
(with-mock-cache! [save-chan]
diff --git a/test/metabase/query_processor/preprocess_test.clj b/test/metabase/query_processor/preprocess_test.clj
index d17a904d02a..58a5f17b7e3 100644
--- a/test/metabase/query_processor/preprocess_test.clj
+++ b/test/metabase/query_processor/preprocess_test.clj
@@ -33,7 +33,7 @@
:cache-strategy {:type :ttl
:multiplier 60
:avg-execution-ms 100
- :min-duration-ms 0})
+ :min_duration_ms 0})
run-query (fn []
(let [results (qp/process-query query)]
{:cached? (boolean (:cached (:cache/details results)))