mirror of
https://github.com/penpot/penpot.git
synced 2026-11-26 21:00:32 +00:00
🐛 Count and label update-file validation refusals
AI-assisted-by: gpt-6-luna
This commit is contained in:
parent
28f63af16e
commit
dedddf466c
@ -85,6 +85,29 @@
|
||||
::mdef/buckets mtx/default-request-histogram-buckets
|
||||
::mdef/type :histogram}
|
||||
|
||||
:update-file-refused
|
||||
{::mdef/name "penpot_rpc_update_file_refused_total"
|
||||
::mdef/help "Saves update-file refused, or accepted only in part, by the step at which the save first failed, the error code, and whether every commit or only a tail of them was rejected. The code infrastructure marks a database, input-output, or interruption failure, which refuses the save without any validation failing. A save whose auto-repair gave up appears here too, as refused or partial, and in penpot_rpc_update_file_repair_total as unrepaired."
|
||||
::mdef/labels ["stage" "code" "outcome"]
|
||||
::mdef/type :counter}
|
||||
|
||||
:update-file-rejected-commits
|
||||
{::mdef/name "penpot_rpc_update_file_rejected_commits_total"
|
||||
::mdef/help "Commits update-file validation rejected, by the error code of the first rejected commit of their save."
|
||||
::mdef/labels ["code"]
|
||||
::mdef/type :counter}
|
||||
|
||||
:update-file-soft-failure
|
||||
{::mdef/name "penpot_rpc_update_file_soft_failure_total"
|
||||
::mdef/help "Soft file validation failures, by the check that failed and a bounded reason."
|
||||
::mdef/labels ["check" "reason"]
|
||||
::mdef/type :counter}
|
||||
|
||||
:update-file-lagged
|
||||
{::mdef/name "penpot_rpc_update_file_lagged_total"
|
||||
::mdef/help "Saves that passed the revision check with a revn behind the stored head."
|
||||
::mdef/type :counter}
|
||||
|
||||
:update-file-repair
|
||||
{::mdef/name "penpot_rpc_update_file_repair_total"
|
||||
::mdef/help "update-file auto-repair attempts by outcome (repaired or unrepaired)."
|
||||
|
||||
@ -50,6 +50,7 @@
|
||||
(declare ^:private process-changes-and-validate)
|
||||
(declare ^:private take-snapshot?)
|
||||
(declare ^:private invalidate-caches!)
|
||||
(declare ^:private infrastructure-error?)
|
||||
|
||||
;; PUBLIC API; intended to be used outside of this module
|
||||
(declare update-file!)
|
||||
@ -147,6 +148,110 @@
|
||||
(contains? token-change-types type)
|
||||
(contains? file-change-types type)))
|
||||
|
||||
;; --- METRICS
|
||||
|
||||
(defn- record-metric
|
||||
"Record a metric on the cfg's metrics instance. A cfg without one
|
||||
records nothing, so callers outside the service (scripts, tests)
|
||||
work unchanged."
|
||||
[cfg params]
|
||||
(when-let [metrics (::mtx/metrics cfg)]
|
||||
(when (mtx/metrics? metrics)
|
||||
(mtx/run! metrics params))))
|
||||
|
||||
(defn- data-code
|
||||
"Bounded label for the error described by the ex-data `data`: its
|
||||
`:code`, or its `:type` when it carries no code. An error without a
|
||||
`:type`, such as an exception without ex-data, is `unexpected-error`,
|
||||
the code `rejection` gives it on the commit path, so one failure is
|
||||
one series whatever the shape of the save.
|
||||
|
||||
Both are keywords written in this codebase, so the label set stays
|
||||
bounded by the source."
|
||||
[data]
|
||||
(let [type (:type data)
|
||||
code (or (:code data) type)]
|
||||
(if (and (keyword? type) (keyword? code))
|
||||
(name code)
|
||||
"unexpected-error")))
|
||||
|
||||
(defn- refusal-code
|
||||
"Bounded label for the error that refused a save: `infrastructure` for
|
||||
the failures `infrastructure-error?` matches (database, input-output,
|
||||
interruption, a file fragment that cannot be loaded), which say
|
||||
nothing about the save itself, and the label of `data-code` for any
|
||||
other."
|
||||
[cause]
|
||||
(if (infrastructure-error? cause)
|
||||
"infrastructure"
|
||||
(data-code (ex-data cause))))
|
||||
|
||||
(defn- step-stage
|
||||
"Bounded label for the furthest step whose timing `timings` holds.
|
||||
`apply-changes` and `validate-file` record a step's timing in a
|
||||
`finally` form, so the timing is present whether the step completed or
|
||||
raised: the furthest step present is the one that raised, or the last
|
||||
one before the failing step. A step that is off records nothing, so
|
||||
the label does not depend on which validation flags are on.
|
||||
|
||||
`changes` covers the change-application step, the change schema
|
||||
checks included. A failing file version check comes before it and
|
||||
reports `pre-changes`. A failure after validation, such as the write,
|
||||
reports the last validation step that ran."
|
||||
[timings]
|
||||
(cond
|
||||
(contains? timings :validate-schema-ms) "schema"
|
||||
(contains? timings :validate-affected-ms) "referential"
|
||||
(contains? timings :process-changes-ms) "changes"
|
||||
:else "pre-changes"))
|
||||
|
||||
(defn- record-failed-stage
|
||||
"Record, as `::failed-stage` of the step timings, the stage at which
|
||||
applying and validating all the changes at once failed. The repair and
|
||||
the bisection that follow run steps of their own (the repair times its
|
||||
schema check as `:validate-schema-ms`), and they would otherwise move
|
||||
`step-stage` past the step that refused the save."
|
||||
[cfg]
|
||||
(when-let [timings (::validation-timings cfg)]
|
||||
(swap! timings #(assoc % ::failed-stage (step-stage %)))))
|
||||
|
||||
(defn- refusal-stage
|
||||
"Bounded label for the stage a refused save, or a save whose tail was
|
||||
rejected, had reached: the stage `record-failed-stage` recorded at the
|
||||
first failure, or else the furthest step timed (`step-stage`). The
|
||||
retries of `apply-commits` record no timings, so a partial rejection
|
||||
reports the stage of the attempt over the whole change set."
|
||||
[timings]
|
||||
(or (::failed-stage timings)
|
||||
(step-stage timings)))
|
||||
|
||||
(defn- record-rejection
|
||||
"Record a save whose `rejected` commits, out of `total`, were not
|
||||
stored.
|
||||
|
||||
`apply-commits` keeps the valid prefix of the commits and rejects the
|
||||
rest without raising, so the refusal counter in the error handler of
|
||||
`update-file` never sees such a save. It is counted once the save is
|
||||
stored, in the same family as a whole refusal: the `outcome` label
|
||||
tells a save that stored nothing (`refused`, which is also what a save
|
||||
of a single rejected commit reports) from one that stored the commits
|
||||
before the first rejected one (`partial`).
|
||||
|
||||
The rejected commits are counted once each, all under the code of the
|
||||
first rejected commit, because the retries report only that error."
|
||||
[cfg rejected total]
|
||||
(let [code (data-code (:error rejected))
|
||||
stage (refusal-stage @(::validation-timings cfg))
|
||||
outcome (if (= (count (:commit-ids rejected)) total)
|
||||
"refused"
|
||||
"partial")]
|
||||
(record-metric cfg {:id :update-file-refused
|
||||
:labels [stage code outcome]
|
||||
:inc 1})
|
||||
(record-metric cfg {:id :update-file-rejected-commits
|
||||
:labels [code]
|
||||
:inc (count (:commit-ids rejected))})))
|
||||
|
||||
;; Not a real editor session: file-change messages with this id are
|
||||
;; delivered to every connected session, including the saver.
|
||||
(def repair-session-id
|
||||
@ -158,11 +263,9 @@
|
||||
|
||||
(defn- record-repair-outcome
|
||||
[cfg outcome]
|
||||
(when-let [metrics (::mtx/metrics cfg)]
|
||||
(when (mtx/metrics? metrics)
|
||||
(mtx/run! metrics {:id :update-file-repair
|
||||
:labels [(name outcome)]
|
||||
:inc 1}))))
|
||||
(record-metric cfg {:id :update-file-repair
|
||||
:labels [(name outcome)]
|
||||
:inc 1}))
|
||||
|
||||
(defn- error-codes
|
||||
[errors]
|
||||
@ -219,7 +322,9 @@
|
||||
(assoc :commits commits))
|
||||
|
||||
cfg (assoc cfg ::timestamp (ct/now)
|
||||
::validation-timings (atom {}))
|
||||
::validation-timings (atom {})
|
||||
::incoming-revn (:revn params)
|
||||
::stored-revn (:revn file))
|
||||
|
||||
tpoint (ct/tpoint)]
|
||||
|
||||
@ -239,6 +344,13 @@
|
||||
:context {:incoming-revn (:revn params)
|
||||
:stored-revn (:revn file)}))
|
||||
|
||||
;; The save passed the revision check with a revn behind the stored
|
||||
;; head: the server accepts it and folds it on top of changes its
|
||||
;; client had not received.
|
||||
(when (< (::incoming-revn cfg)
|
||||
(::stored-revn cfg))
|
||||
(record-metric cfg {:id :update-file-lagged :inc 1}))
|
||||
|
||||
;; When newly computed features does not match exactly with the
|
||||
;; features defined on team row, we update it
|
||||
(when-let [features (-> features
|
||||
@ -283,6 +395,13 @@
|
||||
;; (`handle-response`), so timings collected before a hard
|
||||
;; validation failure would be lost without this inline log.
|
||||
(let [timings @(::validation-timings cfg)]
|
||||
;; Nothing of a refused save is stored, whatever the number
|
||||
;; of commits it carried: the outcome is `refused`.
|
||||
(record-metric cfg {:id :update-file-refused
|
||||
:labels [(refusal-stage timings)
|
||||
(refusal-code cause)
|
||||
"refused"]
|
||||
:inc 1})
|
||||
(l/dbg :hint "update-file"
|
||||
:file-id (str (:id file))
|
||||
:project-id (str (:project-id file))
|
||||
@ -426,6 +545,13 @@
|
||||
(send-repair-notifications cfg params file repair))
|
||||
(send-notifications! cfg params file))
|
||||
|
||||
;; A save whose tail was rejected never raises, so the error
|
||||
;; handler of `update-file` does not see it. It is counted here,
|
||||
;; once stored: a step above that raises refuses the whole save,
|
||||
;; and that handler counts it then, once.
|
||||
(when (some? rejected)
|
||||
(record-rejection cfg rejected (count commits)))
|
||||
|
||||
(with-meta (cond-> {:revn revn :lagged (get-lagged-changes conn params)}
|
||||
(some? rejected)
|
||||
(assoc :rejected rejected))
|
||||
@ -556,30 +682,77 @@
|
||||
|
||||
(apply update-fn cfg file args)))
|
||||
|
||||
(defn- deepest-explain-error
|
||||
"The entry of a malli explain whose `:path` is the longest, or nil."
|
||||
[cause]
|
||||
(->> (-> cause ex-data ::sm/explain :errors)
|
||||
(filter #(seq (:path %)))
|
||||
(sort-by #(count (:path %)))
|
||||
last))
|
||||
|
||||
(defn- schema-failure-reason
|
||||
"Bounded label for a soft schema failure: the deepest keyword of the
|
||||
`:path` of the deepest explain entry, which is the schema key that
|
||||
failed (`type` for a shape whose type is invalid, `objects` for a nil
|
||||
object map). Indices and other non-keyword path elements are skipped
|
||||
so the label set stays small."
|
||||
[error]
|
||||
(let [key (->> (:path error) (filter keyword?) last)]
|
||||
(if (keyword? key) (name key) "unknown")))
|
||||
|
||||
(defn- referential-failure-code
|
||||
"The code of the first error `validate-file-affected!` reported, kept
|
||||
within its closed `error-codes` set, or `:unknown`."
|
||||
[cause]
|
||||
(let [code (-> cause ex-data :details first :code)]
|
||||
(if (contains? val/error-codes code) code :unknown)))
|
||||
|
||||
(defn- soft-validate-file-schema!
|
||||
[file changes]
|
||||
[cfg file changes]
|
||||
(try
|
||||
(val/validate-file-schema-affected! file changes)
|
||||
(catch Throwable cause
|
||||
(l/error :hint "file schema validation error" :cause cause))))
|
||||
(let [error (deepest-explain-error cause)]
|
||||
(record-metric cfg {:id :update-file-soft-failure
|
||||
:labels ["schema" (schema-failure-reason error)]
|
||||
:inc 1})
|
||||
(l/error :hint "file schema validation error"
|
||||
:file-id (str (:id file))
|
||||
:incoming-revn (::incoming-revn cfg)
|
||||
:stored-revn (::stored-revn cfg)
|
||||
:code (refusal-code cause)
|
||||
;; Schema path and instance path of the deepest explain
|
||||
;; entry: keywords, ids, and indices only.
|
||||
:path (some-> error :path vec)
|
||||
:in (some-> error :in vec)
|
||||
:cause cause)))))
|
||||
|
||||
(defn- record-validation-timing
|
||||
[cfg step-key step-label elapsed-ms]
|
||||
(when-let [metrics (::mtx/metrics cfg)]
|
||||
(when (mtx/metrics? metrics)
|
||||
(mtx/run! metrics {:id :update-file-validation-timing
|
||||
:labels [step-label]
|
||||
:val (double elapsed-ms)})))
|
||||
(record-metric cfg {:id :update-file-validation-timing
|
||||
:labels [step-label]
|
||||
:val (double elapsed-ms)})
|
||||
(when-let [timings (::validation-timings cfg)]
|
||||
(swap! timings assoc step-key elapsed-ms)))
|
||||
|
||||
(defn- soft-validate-file!
|
||||
[file libs changes]
|
||||
[cfg file libs changes]
|
||||
(try
|
||||
(val/validate-file-affected! file libs changes)
|
||||
(catch Throwable cause
|
||||
(l/error :hint "file validation error"
|
||||
:cause cause))))
|
||||
(let [code (referential-failure-code cause)
|
||||
error (-> cause ex-data :details first)]
|
||||
(record-metric cfg {:id :update-file-soft-failure
|
||||
:labels ["affected" (name code)]
|
||||
:inc 1})
|
||||
(l/error :hint "file validation error"
|
||||
:file-id (str (:id file))
|
||||
:incoming-revn (::incoming-revn cfg)
|
||||
:stored-revn (::stored-revn cfg)
|
||||
:code (name code)
|
||||
:page-id (some-> error :page-id str)
|
||||
:shape-id (some-> error :shape-id str)
|
||||
:cause cause)))))
|
||||
|
||||
(defmacro ^:private with-validation-timing
|
||||
"Runs `body` and records its time as the validation step `step-key`
|
||||
@ -598,11 +771,11 @@
|
||||
(binding [pmap/*tracked* nil]
|
||||
(when (contains? cf/flags :soft-file-validation)
|
||||
(with-validation-timing cfg :soft-validate-affected-ms "soft-validate-affected"
|
||||
(soft-validate-file! file libs changes)))
|
||||
(soft-validate-file! cfg file libs changes)))
|
||||
|
||||
(when (contains? cf/flags :soft-file-schema-validation)
|
||||
(with-validation-timing cfg :soft-validate-schema-ms "soft-validate-schema"
|
||||
(soft-validate-file-schema! file changes)))
|
||||
(soft-validate-file-schema! cfg file changes)))
|
||||
|
||||
(when (and (contains? cf/flags :file-validation)
|
||||
(not skip-validate))
|
||||
@ -947,6 +1120,7 @@
|
||||
(-> (apply-changes cfg file libs changes skip-validate)
|
||||
(assoc ::changes changes))
|
||||
(catch Throwable cause
|
||||
(record-failed-stage cfg)
|
||||
(repair-or-reject cfg original file libs changes commits
|
||||
skip-validate tracked cause))))]
|
||||
|
||||
|
||||
382
backend/test/backend_tests/files_update_refusal_test.clj
Normal file
382
backend/test/backend_tests/files_update_refusal_test.clj
Normal file
@ -0,0 +1,382 @@
|
||||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; Copyright (c) KALEIDOS SUBSIDIARY SL
|
||||
|
||||
(ns backend-tests.files-update-refusal-test
|
||||
"Counters that update-file records for refused saves, soft validation
|
||||
failures, and lagged saves, and the bounded labels they carry."
|
||||
(:require
|
||||
[app.binfile.common :as bfc]
|
||||
[app.common.features :as cfeat]
|
||||
[app.common.files.validate :as val]
|
||||
[app.common.types.shape :as cts]
|
||||
[app.common.uuid :as uuid]
|
||||
[app.config :as cf]
|
||||
[app.main :as main]
|
||||
[app.metrics :as mtx]
|
||||
[app.rpc :as-alias rpc]
|
||||
[app.rpc.commands.files-update :as files-update]
|
||||
[app.util.pointer-map :as pmap]
|
||||
[backend-tests.helpers :as th]
|
||||
[clojure.test :as t]
|
||||
[integrant.core :as ig])
|
||||
(:import
|
||||
io.prometheus.client.CollectorRegistry))
|
||||
|
||||
(t/use-fixtures :once th/state-init)
|
||||
(t/use-fixtures :each th/database-reset)
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Helpers
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
(defn- metrics
|
||||
[]
|
||||
(ig/init-key :app.metrics/metrics {:default main/default-metrics}))
|
||||
|
||||
(defn- counter
|
||||
"Value of a counter series, or 0 when the series does not exist yet."
|
||||
[metrics name label-names label-values]
|
||||
(or (.getSampleValue ^CollectorRegistry (mtx/get-registry metrics)
|
||||
^String name
|
||||
(into-array String label-names)
|
||||
(into-array String label-values))
|
||||
0.0))
|
||||
|
||||
(defn- refused
|
||||
"Value of the refused-saves series of the system's metrics with the
|
||||
labels `stage`, `code`, and `outcome`."
|
||||
[stage code outcome]
|
||||
(counter (:app.metrics/metrics th/*system*)
|
||||
"penpot_rpc_update_file_refused_total"
|
||||
["stage" "code" "outcome"]
|
||||
[stage code outcome]))
|
||||
|
||||
(defn- rejected-commits
|
||||
"Value of the rejected-commits series of the system's metrics with the
|
||||
label `code`."
|
||||
[code]
|
||||
(counter (:app.metrics/metrics th/*system*)
|
||||
"penpot_rpc_update_file_rejected_commits_total"
|
||||
["code"] [code]))
|
||||
|
||||
(defn- unrepaired
|
||||
[]
|
||||
(counter (:app.metrics/metrics th/*system*)
|
||||
"penpot_rpc_update_file_repair_total"
|
||||
["outcome"] ["unrepaired"]))
|
||||
|
||||
(defn- validation-cfg
|
||||
[metrics]
|
||||
{::mtx/metrics metrics
|
||||
::files-update/validation-timings (atom {})
|
||||
::files-update/incoming-revn 0
|
||||
::files-update/stored-revn 0})
|
||||
|
||||
(defn- setup-file
|
||||
"A stored file with one empty page, and the profile that owns it."
|
||||
[]
|
||||
(let [profile (th/create-profile* 1 {:is-active true})
|
||||
file (th/create-file* 1 {:profile-id (:id profile)
|
||||
:project-id (:default-project-id profile)})]
|
||||
{:profile-id (:id profile)
|
||||
:file file
|
||||
:page-id (first (:pages (:data file)))}))
|
||||
|
||||
(defn- save!
|
||||
"Calls update-file on the file of `ctx` with `params` over the defaults."
|
||||
[{:keys [profile-id file]} params]
|
||||
(th/command! (merge {::th/type :update-file
|
||||
::rpc/profile-id profile-id
|
||||
:id (:id file)
|
||||
:session-id (uuid/next)
|
||||
:revn 0
|
||||
:vern 0
|
||||
:features (cfeat/get-enabled-features cf/flags)}
|
||||
params)))
|
||||
|
||||
(defn- broken-schema-file
|
||||
"A file whose only defect is a shape with an invalid `:type`."
|
||||
[]
|
||||
(let [page-id (uuid/next)
|
||||
shape (-> (cts/setup-shape {:id (uuid/next)
|
||||
:type :rect
|
||||
:name "rect"
|
||||
:x 0 :y 0 :width 10 :height 10
|
||||
:parent-id uuid/zero
|
||||
:frame-id uuid/zero})
|
||||
(assoc :type :not-a-shape))]
|
||||
{:id (uuid/next)
|
||||
:revn 0
|
||||
:version 0
|
||||
:data {:pages [page-id]
|
||||
:pages-index {page-id {:id page-id
|
||||
:name "Page 1"
|
||||
:objects {(:id shape) shape}}}}}))
|
||||
|
||||
(defn- broken-referential-file
|
||||
"A file whose root lists a child that does not exist."
|
||||
[]
|
||||
(let [page-id (uuid/next)
|
||||
root (-> (cts/setup-shape {:id uuid/zero
|
||||
:type :frame
|
||||
:name "Root"
|
||||
:x 0 :y 0 :width 100 :height 100
|
||||
:parent-id uuid/zero
|
||||
:frame-id uuid/zero})
|
||||
(update :shapes (fnil conj []) (uuid/next)))]
|
||||
{:id (uuid/next)
|
||||
:revn 0
|
||||
:version 0
|
||||
:features #{"components/v2"}
|
||||
:data {:pages [page-id]
|
||||
:pages-index {page-id {:id page-id
|
||||
:name "Page 1"
|
||||
:objects {uuid/zero root}}}}}))
|
||||
|
||||
(defn- touch-first-page
|
||||
"A change that touches the first page of `file` and leaves it as it
|
||||
was: the schema check covers only the pages a save touches."
|
||||
[file]
|
||||
[{:type :mod-obj
|
||||
:page-id (first (:pages (:data file)))
|
||||
:id uuid/zero
|
||||
:operations []}])
|
||||
|
||||
(defn- set-attr
|
||||
[page-id shape-id attr val]
|
||||
{:type :mod-obj
|
||||
:page-id page-id
|
||||
:id shape-id
|
||||
:operations [{:type :set :attr attr :val val}]})
|
||||
|
||||
(defn- commit
|
||||
[& changes]
|
||||
{:commit-id (uuid/next) :changes (vec changes)})
|
||||
|
||||
(defn- invalid-type-commit
|
||||
"A commit group whose fold leaves a shape invalid. The operation passes
|
||||
the params schema (`:val` is untyped), so the fold itself refuses it."
|
||||
[page-id]
|
||||
(commit (set-attr page-id uuid/zero :type :not-a-shape)))
|
||||
|
||||
(defn- page-name-commit
|
||||
"A commit group that applies and validates."
|
||||
[page-id name]
|
||||
(commit {:type :mod-page :id page-id :name name}))
|
||||
|
||||
(defn- debug-fault
|
||||
"A change that asks the backend to fail with `fault` while applied."
|
||||
[fault]
|
||||
{:type :set-plugin-data
|
||||
:object-type :file
|
||||
:namespace :penpot-debug
|
||||
:key (str "save-fault:" fault)
|
||||
:value nil})
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Tests
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
(t/deftest soft-schema-failure-is-counted-with-the-failing-schema-key
|
||||
(let [metrics (metrics)
|
||||
cfg (validation-cfg metrics)
|
||||
file (broken-schema-file)
|
||||
before (counter metrics "penpot_rpc_update_file_soft_failure_total"
|
||||
["check" "reason"] ["schema" "type"])]
|
||||
(binding [cf/flags #{:soft-file-schema-validation}
|
||||
pmap/*tracked* (pmap/create-tracked)]
|
||||
(@#'files-update/process-changes-and-validate
|
||||
cfg file (touch-first-page file) nil nil))
|
||||
|
||||
(t/is (= (inc before)
|
||||
(counter metrics "penpot_rpc_update_file_soft_failure_total"
|
||||
["check" "reason"] ["schema" "type"])))))
|
||||
|
||||
(t/deftest soft-referential-failure-is-counted-with-the-reported-code
|
||||
(let [metrics (metrics)
|
||||
cfg (validation-cfg metrics)
|
||||
file (broken-referential-file)
|
||||
page-id (first (:pages (:data file)))
|
||||
before (counter metrics "penpot_rpc_update_file_soft_failure_total"
|
||||
["check" "reason"] ["affected" "child-not-found"])]
|
||||
(binding [cf/flags #{:soft-file-validation}
|
||||
pmap/*tracked* (pmap/create-tracked)]
|
||||
(with-redefs [bfc/get-resolved-file-libraries (fn [_ _] {})]
|
||||
(@#'files-update/process-changes-and-validate
|
||||
cfg file
|
||||
[{:type :mod-obj :page-id page-id :id uuid/zero :operations []}]
|
||||
nil nil)))
|
||||
|
||||
(t/is (= (inc before)
|
||||
(counter metrics "penpot_rpc_update_file_soft_failure_total"
|
||||
["check" "reason"] ["affected" "child-not-found"])))))
|
||||
|
||||
(t/deftest soft-failure-records-nothing-without-a-metrics-instance
|
||||
;; The bisection retries of `apply-commits` run without a metrics
|
||||
;; instance. A soft failure that raised there would reject a commit
|
||||
;; that is valid.
|
||||
(let [cfg {::files-update/validation-timings (atom {})
|
||||
::files-update/incoming-revn 0
|
||||
::files-update/stored-revn 0}
|
||||
file (broken-schema-file)]
|
||||
(binding [cf/flags #{:soft-file-schema-validation}
|
||||
pmap/*tracked* (pmap/create-tracked)]
|
||||
(t/is (map? (@#'files-update/process-changes-and-validate
|
||||
cfg file (touch-first-page file) nil nil))))))
|
||||
|
||||
(t/deftest refused-update-file-is-counted-with-stage-and-code
|
||||
(let [{:keys [page-id] :as ctx} (setup-file)
|
||||
before (refused "changes" "data-validation" "refused")
|
||||
out (save! ctx {:changes [(set-attr page-id uuid/zero :type :not-a-shape)]})]
|
||||
(t/is (some? (:error out)))
|
||||
(t/is (= (inc before)
|
||||
(refused "changes" "data-validation" "refused")))))
|
||||
|
||||
(t/deftest referential-refusal-reports-the-gate-stage-and-code
|
||||
;; The damage has to come from the changes. A file damaged before the
|
||||
;; save is repairable, and the save then goes through instead of
|
||||
;; refusing.
|
||||
(with-redefs [cf/flags (conj cf/flags :file-validation)]
|
||||
(let [{:keys [page-id] :as ctx} (setup-file)
|
||||
shape-id (uuid/next)
|
||||
_ (t/is (th/success? (save! ctx {:changes [(th/add-rect-change page-id shape-id)]})))
|
||||
before (refused "referential" "referential-integrity" "refused")
|
||||
out (save! ctx {:revn 1
|
||||
:changes [(set-attr page-id shape-id :parent-id (uuid/next))]})]
|
||||
(t/is (= :referential-integrity (:code (ex-data (:error out)))))
|
||||
(t/is (= (inc before)
|
||||
(refused "referential" "referential-integrity" "refused"))))))
|
||||
|
||||
(t/deftest failed-repair-reports-the-stage-of-the-first-failure
|
||||
;; The save fails the referential gate on damage the file already had.
|
||||
;; The repair fixes it and then fails its own schema check, which it
|
||||
;; times as `validate-schema`. The save was refused at the gate.
|
||||
(with-redefs [cf/flags (conj cf/flags :file-validation :file-schema-validation)]
|
||||
(let [{:keys [page-id] :as ctx} (setup-file)
|
||||
shape-a (uuid/next)
|
||||
shape-b (uuid/next)
|
||||
_ (t/is (th/success? (save! ctx {:changes [(th/add-rect-change page-id shape-a)
|
||||
(th/add-rect-change page-id shape-b)]})))
|
||||
_ (t/is (th/success? (save! ctx {:revn 1
|
||||
:skip-validate true
|
||||
:changes [(set-attr page-id shape-a :frame-id (uuid/next))]})))
|
||||
before (refused "referential" "referential-integrity" "refused")
|
||||
before-schema (refused "schema" "referential-integrity" "refused")
|
||||
before-unrepaired (unrepaired)
|
||||
out (with-redefs [val/validate-file-schema!
|
||||
(fn [_]
|
||||
(throw (ex-info "schema" {:type :validation
|
||||
:code :schema-validation})))]
|
||||
(save! ctx {:revn 2
|
||||
:changes [(set-attr page-id shape-b :name "renamed")]}))]
|
||||
(t/is (= :referential-integrity (:code (ex-data (:error out)))))
|
||||
(t/is (= (inc before-unrepaired) (unrepaired)))
|
||||
(t/is (= (inc before)
|
||||
(refused "referential" "referential-integrity" "refused")))
|
||||
(t/is (= before-schema
|
||||
(refused "schema" "referential-integrity" "refused"))))))
|
||||
|
||||
(t/deftest a-failure-without-a-type-is-one-series-on-both-request-shapes
|
||||
;; The debug fault is raised before the change step starts its timer,
|
||||
;; hence `pre-changes`.
|
||||
(with-redefs [cf/flags (conj cf/flags :debug-save-faults)]
|
||||
(let [ctx (setup-file)
|
||||
before (refused "pre-changes" "unexpected-error" "refused")
|
||||
whole (save! ctx {:changes [(debug-fault "runtime")]})
|
||||
_ (t/is (= (inc before)
|
||||
(refused "pre-changes" "unexpected-error" "refused")))
|
||||
by-commit (save! ctx {:changes-with-metadata [(commit (debug-fault "runtime"))]})]
|
||||
(t/is (some? (:error whole)))
|
||||
(t/is (= :unexpected-error (-> by-commit :result :rejected :error :code)))
|
||||
(t/is (= (+ 2 before)
|
||||
(refused "pre-changes" "unexpected-error" "refused"))))))
|
||||
|
||||
(t/deftest an-infrastructure-failure-is-not-counted-as-a-validation-code
|
||||
(with-redefs [cf/flags (conj cf/flags :debug-save-faults)]
|
||||
(let [ctx (setup-file)
|
||||
before (refused "pre-changes" "infrastructure" "refused")
|
||||
out (save! ctx {:changes [(debug-fault "sql")]})]
|
||||
(t/is (instance? java.sql.SQLException (:error out)))
|
||||
(t/is (= (inc before)
|
||||
(refused "pre-changes" "infrastructure" "refused"))))))
|
||||
|
||||
(t/deftest partial-rejection-is-counted-with-its-outcome-and-rejected-commits
|
||||
;; A save that carries commit groups keeps the commits before the first
|
||||
;; invalid one and answers normally, so nothing raises out of
|
||||
;; `update-file` and the refusal counter in its error handler never
|
||||
;; sees the rejection.
|
||||
(let [{:keys [page-id] :as ctx} (setup-file)
|
||||
commits [(page-name-commit page-id "a")
|
||||
(page-name-commit page-id "b")
|
||||
(invalid-type-commit page-id)
|
||||
(page-name-commit page-id "c")]
|
||||
before (refused "changes" "data-validation" "partial")
|
||||
before-refused (refused "changes" "data-validation" "refused")
|
||||
rejected-before (rejected-commits "data-validation")
|
||||
out (save! ctx {:changes-with-metadata commits})]
|
||||
;; The two commits before the invalid one apply and validate
|
||||
(t/is (= (mapv :commit-id (subvec commits 2))
|
||||
(-> out :result :rejected :commit-ids)))
|
||||
(t/is (= (inc before)
|
||||
(refused "changes" "data-validation" "partial")))
|
||||
(t/is (= before-refused
|
||||
(refused "changes" "data-validation" "refused")))
|
||||
(t/is (= (+ 2 rejected-before)
|
||||
(rejected-commits "data-validation")))))
|
||||
|
||||
(t/deftest a-partial-save-that-fails-to-store-is-counted-once
|
||||
;; The write fails after the commits were sorted: nothing is stored, so
|
||||
;; the save is one refusal and not also a partial one.
|
||||
(let [{:keys [page-id] :as ctx} (setup-file)
|
||||
commits [(page-name-commit page-id "a")
|
||||
(invalid-type-commit page-id)]
|
||||
before-partial (refused "changes" "data-validation" "partial")
|
||||
before (refused "changes" "infrastructure" "refused")
|
||||
out (with-redefs [files-update/persist-file!
|
||||
(fn [_ _]
|
||||
(throw (java.sql.SQLException. "write failed")))]
|
||||
(save! ctx {:changes-with-metadata commits}))]
|
||||
(t/is (instance? java.sql.SQLException (:error out)))
|
||||
(t/is (= before-partial
|
||||
(refused "changes" "data-validation" "partial")))
|
||||
(t/is (= (inc before)
|
||||
(refused "changes" "infrastructure" "refused")))))
|
||||
|
||||
(t/deftest a-save-that-rejects-every-commit-is-counted-as-a-refusal
|
||||
;; The request answers normally and stores nothing, so `refused` is
|
||||
;; the only outcome that separates it from a save that went through.
|
||||
(let [{:keys [page-id] :as ctx} (setup-file)
|
||||
commits [(invalid-type-commit page-id)]
|
||||
before (refused "changes" "data-validation" "refused")
|
||||
out (save! ctx {:changes-with-metadata commits})]
|
||||
(t/is (nil? (:error out)))
|
||||
(t/is (= (mapv :commit-id commits)
|
||||
(-> out :result :rejected :commit-ids)))
|
||||
(t/is (= (inc before)
|
||||
(refused "changes" "data-validation" "refused")))))
|
||||
|
||||
(t/deftest lagged-save-is-counted
|
||||
(let [{:keys [profile-id file page-id]} (setup-file)
|
||||
changes [{:type :mod-obj :page-id page-id :id uuid/zero :operations []}]
|
||||
metrics (:app.metrics/metrics th/*system*)
|
||||
before (counter metrics "penpot_rpc_update_file_lagged_total" [] [])]
|
||||
|
||||
;; The first save moves the stored revn to 1; it arrives at the head.
|
||||
(th/update-file! :profile-id profile-id
|
||||
:file-id (:id file)
|
||||
:revn 0
|
||||
:changes changes)
|
||||
(t/is (= before
|
||||
(counter metrics "penpot_rpc_update_file_lagged_total" [] [])))
|
||||
|
||||
;; The second one keeps the stale revn and the server accepts it,
|
||||
;; applying it on top of the change its client has not received.
|
||||
(th/update-file! :profile-id profile-id
|
||||
:file-id (:id file)
|
||||
:revn 0
|
||||
:changes changes)
|
||||
(t/is (= (inc before)
|
||||
(counter metrics "penpot_rpc_update_file_lagged_total" [] [])))))
|
||||
Loading…
x
Reference in New Issue
Block a user