Skip to content

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

Don't cache the app-db-not-ready token check failure past setup (#84690)

dpsuttondb64da92beb2
diff --git a/src/metabase/premium_features/token_check.clj b/src/metabase/premium_features/token_check.clj
index 59aa1a3962e..454866b28c3 100644
--- a/src/metabase/premium_features/token_check.clj
+++ b/src/metabase/premium_features/token_check.clj
@@ -536,14 +536,17 @@
(defn local-cached-token-checker
"Wraps a token checker with short-lived in-memory TTL memoization.
- Avoids hitting the DB on every request."
+ Avoids hitting the DB on every request. Passes through uncached until the app DB is set up, so the
+ app-db-not-ready failure is never cached past setup."
[token-checker {:keys [local-ttl]}]
(let [cached-check (memoize/ttl
(fn [token] (-check-token token-checker token))
:ttl/threshold (.toMillis ^java.time.Duration local-ttl))]
(reify TokenChecker
(-check-token [_ token]
- (cached-check token))
+ (if (app-db/db-is-set-up?)
+ (cached-check token)
+ (-check-token token-checker token)))
(-clear-cache! [_]
(memoize/memo-clear! cached-check)
(-clear-cache! token-checker))
@@ -594,8 +597,8 @@
:local-cache db-hash-local-cache})
;; inside the memoize so failure maps are memoized for local-ttl (negative cache; core.memoize
- ;; single-flights values but not thrown exceptions), outside db-hash so they never reach the
- ;; durable cache
+ ;; single-flights values but not thrown exceptions), except before app-DB setup, when the memo
+ ;; is skipped; outside db-hash so they never reach the durable cache
:always
(error-catching-token-checker)
diff --git a/test/metabase/premium_features/token_check_test.clj b/test/metabase/premium_features/token_check_test.clj
index c9a9291fd63..8c53a64330e 100644
--- a/test/metabase/premium_features/token_check_test.clj
+++ b/test/metabase/premium_features/token_check_test.clj
@@ -379,25 +379,25 @@
(reset! behavior :success)
(Thread/sleep 100) ;; wait for circuit breaker to open back up (also outlasts the 50ms negative cache)
(is (= good-response (token-check/-check-token checker token)))))
- (testing "App db not setup yet does not invoke the circuit breaker (#65294)"
+ (testing "App db not setup yet does not invoke the circuit breaker (#65294), and the refusal is not cached past setup"
(let [token (tu/random-token)
+ call-count (atom 0)
good-response {:valid true
:status "fake"
:features ["feature1" "feature2"]
:canonical? true}
- token-response (fn [_token]
- good-response)
checker (token-check/make-checker
{:base (reify token-check/TokenChecker
- (-check-token [_ token]
- (token-response token))
+ (-check-token [_ _token]
+ (swap! call-count inc)
+ good-response)
(-clear-cache! [_])
(-clear-local-cache! [_]))
:circuit-breaker {:failure-threshold-ratio-in-period [4 4 1000]
:delay-ms 5000
:success-threshold 1}
:timeout-ms 100
- :local-ttl (t/millis 50)
+ :local-ttl (t/seconds 10)
:soft-ttl (t/millis 200)
:hard-ttl (t/seconds 1)})]
(mt/with-dynamic-fn-redefs [mdb/db-is-set-up? (constantly false)]
@@ -412,10 +412,13 @@
:status "Unable to validate token"
:error-details "Metabase DB is not yet set up"}
(token-check/-check-token checker token))))
- (testing "When the db-is-set-up? we are not blocked by the circuit breaker"
- (Thread/sleep 60) ;; outlast the 50ms local-ttl — the not-set-up failure map is negative-cached
- (mt/with-dynamic-fn-redefs [mdb/db-is-set-up? (constantly true)]
- (is (= good-response (token-check/-check-token checker token))))))))
+ (testing "When the db-is-set-up? we are not blocked by the circuit breaker, and the first check reaches the store"
+ (try
+ (mt/with-dynamic-fn-redefs [mdb/db-is-set-up? (constantly true)]
+ (is (= good-response (token-check/-check-token checker token)))
+ (is (= 1 @call-count)))
+ (finally
+ (token-check/-clear-cache! checker)))))))
(defn- age-cache-entry!
"Rewrite the `updated_at` timestamp for `token` in the cache table to be `age-ms` milliseconds in the past.