Skip to content

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

stabilize composed regex schemas (#61805)

bryan8d6e60df0fe2
diff --git a/src/metabase/util/malli/registry.cljc b/src/metabase/util/malli/registry.cljc
index d05195ba52f..d2c005ee1fe 100644
--- a/src/metabase/util/malli/registry.cljc
+++ b/src/metabase/util/malli/registry.cljc
@@ -5,6 +5,7 @@
#?@(:clj ([malli.experimental.time :as malli.time]
[metabase.util.malli.registry.validator-cache :as mr.validator-cache]
[net.cgrand.macrovich :as macros]))
+ [clojure.walk :as walk]
[malli.core :as mc]
[malli.registry]
[malli.util :as mut])
@@ -16,17 +17,22 @@
"Make schemas that aren't `=` to identical ones e.g.
[:re #\"\\d{4}\"]
+ [:or :int [:re #\"\\d{4}\"]]
work correctly as cache keys instead of creating new entries every time the code is evaluated."
[x]
- (if (and (vector? x)
- (= (first x) :re))
- (into (empty x)
- (map (fn [child]
- (cond-> child
- (instance? #?(:clj java.util.regex.Pattern :cljs js/RegExp) child) str)))
- x)
- x))
+ (walk/postwalk
+ (fn [form]
+ (cond-> form
+ (instance? #?(:clj java.util.regex.Pattern :cljs js/RegExp) form)
+ str))
+ x))
+
+(def ^:dynamic *cache-miss-hook*
+ "A hook that is called whenever there is a cache miss, for side effects.
+ This is used in tests or to monitor cache misses."
+ ;; (fn [_k _schema _value] nil)
+ nil)
(defn cached
"Get a cached value for `k` + `schema`. Cache is cleared whenever a schema is (re)defined
@@ -39,6 +45,8 @@
(let [schema-key (schema-cache-key schema)]
(or (get (get @cache k) schema-key) ; get-in is terribly inefficient
(let [v (value-thunk)]
+ (when *cache-miss-hook*
+ (*cache-miss-hook* k schema v))
(swap! cache assoc-in [k schema-key] v)
v))))
diff --git a/test/metabase/util/malli/registry_test.cljc b/test/metabase/util/malli/registry_test.cljc
index 01bc55c6eb4..3248224325d 100644
--- a/test/metabase/util/malli/registry_test.cljc
+++ b/test/metabase/util/malli/registry_test.cljc
@@ -5,18 +5,48 @@
[clojure.test :refer [are deftest is testing]]
[malli.core :as mc]
[malli.error :as me]
- [metabase.util.malli.registry :as mr]))
+ [metabase.util.malli.registry :as mr]
+ [metabase.util.malli.registry-test-macro :refer [with-returning-cache-miss-count]]))
(deftest ^:parallel cache-handle-regexes-test
(testing (str "For things that aren't ever equal when you re-evaluate them (like Regex literals) maybe sure we do"
" something smart to avoid creating infinite cache entries")
- (mr/validate [:re #"\d{4}"] "1234")
- (:validator @@#'mr/cache)
- (let [before-count (count (:validator @@#'mr/cache))]
- (mr/validate [:re #"\d{4}"] "1234")
- (mr/validate [:re #"\d{4}"] "1234")
- (is (= (count (:validator @@#'mr/cache))
- before-count)))))
+ (let [unique-schema [:re {:id (rand)} #"\d{4}"]]
+ (is (= 1 (with-returning-cache-miss-count
+ (mr/validate unique-schema "1234"))))
+ (is (= 0 (with-returning-cache-miss-count
+ (mr/validate unique-schema "1234")
+ (mr/validate unique-schema "1234")))
+ "Calling validate with a previously cached schema does not miss cache"))))
+
+(deftest ^:parallel cache-handle-composed-regexes-test
+ (testing (str "For things that aren't ever equal when you re-evaluate them (like Regex literals) maybe sure we do"
+ " something smart to avoid creating infinite cache entries")
+ (let [unique-id (rand)]
+ (is (= 1 (with-returning-cache-miss-count
+ (mr/validate [:and {:id unique-id} :string [:re #"\d{4}"]] "1234"))))
+ (let [validator-key-set (set (keys (:validator @@#'mr/cache)))]
+ (is (contains? validator-key-set
+ (#'mr/schema-cache-key [:and {:id unique-id} :string [:re #"\d{4}"]])))
+ (is (not (contains? validator-key-set
+ [:and {:id unique-id} :string [:re #"\d{4}"]]))))
+ (is (= 0
+ (with-returning-cache-miss-count
+ (mr/validate [:and {:id unique-id} :string [:re #"\d{4}"]] "1234")
+ (mr/validate [:and {:id unique-id} :string [:re #"\d{4}"]] "1234")))
+ "Calling validate with a previously cached schema does not miss cache")
+ (is (= 3
+ (with-returning-cache-miss-count
+ ;; one
+ (mr/validate [:and {:id unique-id} [:re #"\d{1}"] :string] "1234")
+ (mr/validate [:and {:id unique-id} [:re #"\d{1}"] :string] "1234")
+ ;; two
+ (mr/validate [:and {:id unique-id} [:re #"\d{2}"] :string] "1234")
+ (mr/validate [:and {:id unique-id} [:re #"\d{2}"] :string] "1234")
+ ;; three
+ (mr/validate [:and {:id unique-id} [:re #"\d{3}"] :string] "1234")
+ (mr/validate [:and {:id unique-id} [:re #"\d{3}"] :string] "1234")))
+ "Calling validate multiple times with the 'same' schema does not miss cache"))))
(mr/def ::int
:int)
diff --git a/test/metabase/util/malli/registry_test_macro.clj b/test/metabase/util/malli/registry_test_macro.clj
new file mode 100644
index 00000000000..fe344756f7d
--- /dev/null
+++ b/test/metabase/util/malli/registry_test_macro.clj
@@ -0,0 +1,16 @@
+(ns metabase.util.malli.registry-test-macro)
+
+(defmacro with-returning-cache-miss-count
+ "Returns the number of cache misses that occurred while executing `body`.
+ This is useful for testing that the cache is working as expected, especially
+ when you want to ensure that certain schemas are not cached.
+
+ Counting cache misses this way is reliable in a multi-threaded context. (`*cache-miss-hook*` will be thread-local,
+ so you only see the misses generated by your own `body`).
+
+ Counting them using the `mr/cache` atom is not! so use this to avoid flakes."
+ [& body]
+ `(let [cache-misses# (atom 0)]
+ (binding [mr/*cache-miss-hook* (fn [_# _# _#] (swap! cache-misses# inc))]
+ ~@body)
+ @cache-misses#))
diff --git a/test/metabase/util/malli/registry_test_macro.cljs b/test/metabase/util/malli/registry_test_macro.cljs
new file mode 100644
index 00000000000..1a9a63eb5ea
--- /dev/null
+++ b/test/metabase/util/malli/registry_test_macro.cljs
@@ -0,0 +1,4 @@
+(ns metabase.util.malli.registry-test-macro
+ (:require-macros [metabase.util.malli.registry-test-macro :refer [with-returning-cache-miss-count]]))
+
+(comment with-returning-cache-miss-count)