I fold 20000 simulated tide readings into one atom from 8 futures. Every reading above the threshold also gets appended to outliers.log. The summary is fine: :n is always 20000, and :outliers is the same every run because the seed is fixed. But the log has more lines than the counter. Last run it was around 250 lines against a counter a little lower. Some ids appear twice. With --workers 1 they agree.
(defn- log-line [& parts]
(spit (:log-file config)
(str (str/join " " parts) "\n")
:append true))
(defn fold-reading [st r]
(let [lvl (:level r)]
(when (outlier? r)
(log-line "outlier" (:id r) (format "%.3f" lvl)))
(cond-> (-> st
(update :n inc)
(update :sum + lvl)
(update :sumsq + (* lvl lvl)))
(outlier? r) (update :outliers inc))))
(defn record! [r]
(swap! stats fold-reading r))
I thought spit :append from many threads double-writes, but then the counter would be wrong too. I also checked the partition-all chunks and they don't overlap, since :n is exactly 20000. I delete the log before each run. The line count is different again on the next run with the same seed.
How can the stats be exactly right while the log is not? I want exactly one line per outlier.