Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Much refactoring of cleanup processes and backing in test coverage |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
88cb69c20c6dc5acac2c54f18fa3d370 |
User & Date: | scstarkey 2025-03-14 12:43:14 |
Context
2025-03-14
| ||
12:48 | Cleanup check-in: 111ba750cb user: scstarkey tags: trunk | |
12:43 | Much refactoring of cleanup processes and backing in test coverage check-in: 88cb69c20c user: scstarkey tags: trunk | |
2025-03-13
| ||
13:56 | Make show-thread a bit more configurable check-in: effb2fd9bf user: scstarkey tags: trunk | |
Changes
Changes to src/streamful/aggregates.clj.
︙ | ︙ | |||
15 16 17 18 19 20 21 22 23 24 25 26 27 28 | ; License along with streamful. ; If not, see <https://www.gnu.org/licenses/#AGPL>. ; (ns streamful.aggregates (:require [clojure.walk :as walk] [sci.core :as sci] [streamful.stream :as stream] [taoensso.timbre :as log]) (:import (java.util.concurrent.locks ReentrantReadWriteLock))) (defprotocol AggregationSystem (stage-aggregator [_ params] "Prepare a new aggregate for introduction into the system. Compiles | > | 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | ; License along with streamful. ; If not, see <https://www.gnu.org/licenses/#AGPL>. ; (ns streamful.aggregates (:require [clojure.walk :as walk] [sci.core :as sci] [streamful.collections :as coll] [streamful.stream :as stream] [taoensso.timbre :as log]) (:import (java.util.concurrent.locks ReentrantReadWriteLock))) (defprotocol AggregationSystem (stage-aggregator [_ params] "Prepare a new aggregate for introduction into the system. Compiles |
︙ | ︙ | |||
37 38 39 40 41 42 43 44 45 46 47 48 49 50 | "For each pending aggregate, applies it to all stream messages and then activates it for application to all future messages. For each aggregate in cleanup, applies its cleanup function to all stream messages and then removes it completely.") (execute [_ msg] "Apply the active aggregates to the given message.")) (def ^:private agg-lock (ReentrantReadWriteLock.)) (def ^:private agg-allowed-symbols '[fn fn* -> ->> update conj and or inc dec assoc dissoc]) (def ^:private agg-opts {:allow agg-allowed-symbols :namespaces { ;;'clojure.core {'println println} | > > > > > | 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | "For each pending aggregate, applies it to all stream messages and then activates it for application to all future messages. For each aggregate in cleanup, applies its cleanup function to all stream messages and then removes it completely.") (execute [_ msg] "Apply the active aggregates to the given message.")) (def ^:dynamic *notify-inconsistent-aggregation-state* (fn [status t n agg] (log/error "Unexpected status -- you may want to redo!" {:status status, :t t, :n n, :agg agg}))) (def ^:private agg-lock (ReentrantReadWriteLock.)) (def ^:private agg-allowed-symbols '[fn fn* -> ->> update conj and or inc dec assoc dissoc]) (def ^:private agg-opts {:allow agg-allowed-symbols :namespaces { ;;'clojure.core {'println println} |
︙ | ︙ | |||
68 69 70 71 72 73 74 | (defmacro with-write! [& body] `(try (-> agg-lock .writeLock .lock) ~@body (finally (-> agg-lock .writeLock .unlock)))) | | | | | 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | (defmacro with-write! [& body] `(try (-> agg-lock .writeLock .lock) ~@body (finally (-> agg-lock .writeLock .unlock)))) (defn- apply-aggregator! ([stream-model {:keys [s a]} msg] (let [{:keys [id] {:keys [k m]} :m} (walk/keywordize-keys msg) msg (merge {:id id, :k k} m) [selected-id selected] (some->> msg s (stream/get-message-by-sha stream-model))] (when selected (let [selected-agg (selected "agg") selected-agg (if selected-agg (walk/keywordize-keys selected-agg) {}) agg-result (a selected-agg msg) agg-result (walk/stringify-keys agg-result)] (stream/put-aggregate! stream-model selected-id agg-result))))) ([stream-model agg-fns _ msg] (apply-aggregator! stream-model agg-fns msg))) (defn- apply-msg-cleanup! [stream-model {:keys [c]} msg-iid msg] (when-let [selected-agg (msg "agg")] (let [cleanup-result (-> selected-agg walk/keywordize-keys c walk/stringify-keys)] (stream/put-aggregate! stream-model msg-iid cleanup-result)))) (defn process-streams! [stream-model f] (doseq [sid (stream/list-streams-for-aggregation stream-model)] |
︙ | ︙ | |||
110 111 112 113 114 115 116 | last-id)] (doseq [[msg-iid msg] all-stream-messages] (reset! last-processed-id msg-iid) (when (= "put" (-> msg (get "m") (get "c"))) (f msg-iid msg))) (when @last-processed-id (recur @last-processed-id)))))) | | < | < < < < < < | | | | | | < | | | | | | | | | | | | | 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | last-id)] (doseq [[msg-iid msg] all-stream-messages] (reset! last-processed-id msg-iid) (when (= "put" (-> msg (get "m") (get "c"))) (f msg-iid msg))) (when @last-processed-id (recur @last-processed-id)))))) (defn- move-aggregator! [aggregators t n from to] (swap! aggregators coll/move-path n [from t] [to t])) (defn- commit! [stream-model aggregators] ;;todo this may be slow, so is likely a candidate for some kind of async ;;todo handling (with-write! (doseq [[n agg-fns] (get-in @aggregators [:pending "msg"])] (when-let [prev-agg-fns (get-in @aggregators [:active "msg" n])] (process-streams! stream-model (partial apply-msg-cleanup! stream-model prev-agg-fns))) (process-streams! stream-model (partial apply-aggregator! stream-model agg-fns)) (move-aggregator! aggregators "msg" n :pending :active) (stream/move-aggregator! stream-model "msg" n :pending :active)) (doseq [[n agg-fns] (get-in @aggregators [:cleanup "msg"])] (process-streams! stream-model (partial apply-msg-cleanup! stream-model agg-fns)) (swap! aggregators coll/dissoc-in [:cleanup "msg"] n) (stream/move-aggregator! stream-model "msg" n :cleanup nil)))) (defn- execute! [aggregators stream-model msg] (with-read! (doseq [[_ agg-fns] (get-in @aggregators [:active "msg"])] (apply-aggregator! stream-model agg-fns msg)))) (defn- load-aggregators! [stream-model aggregators] (log/info "Loading aggregators") (let [db-aggregators (stream/get-aggregators stream-model)] (doseq [status (keys db-aggregators) t (some-> db-aggregators status keys) [n {:keys [a s c]} :as agg] (get-in db-aggregators [status t])] (if (= :active status) (do (log/infof " %s %s" t n) (install-aggregator! aggregators :active t n s a c)) ;;todo at some point we should automatically repair this kind of thing ;;todo but for now we can just leave the error log (*notify-inconsistent-aggregation-state* status t n agg)))) (log/info "Aggregators loaded")) (defn base-aggregation [stream-model] (let [aggregators (atom {})] (load-aggregators! stream-model aggregators) (reify AggregationSystem (stage-aggregator [_ {:keys [t n s a c]}] (stage-aggregator! aggregators t n s a c)) (remove-aggregator [_ {:keys [t n]}] (move-aggregator! aggregators t n :active :cleanup)) (rollback [_] (with-write! (swap! aggregators dissoc :pending :cleanup))) (commit [_] (commit! stream-model aggregators)) (execute [_ msg] (execute! aggregators stream-model msg))))) |
Added src/streamful/collections.clj.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | ; ; This file is part of streamful. ; ; streamful is free software: you can redistribute it and/or modify it ; under the terms ofthe GNU Affero General Public License as published ; by the Free Software Foundation, either version 3 of the License, or ; (at your option) any later version. ; ; streamful is distributed in the hope that it will be useful, but ; WITHOUT ANY WARRANTY; without even the implied warranty of ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; GNU Affero General Public License for more details. ; ; You should have received a copy of the GNU Affero General Public ; License along with streamful. ; If not, see <https://www.gnu.org/licenses/#AGPL>. ; (ns streamful.collections) (defn dissoc-in "Given a map `m`, a key `k`, and a vector `from` which represents a path to a map, remove the key pointed at by `(conj from k)`. If the map at the end of `from` is empty, it is not removed" [m from k] (update-in m from dissoc k)) (defn move-path "Given a map `m`, a key `k`, a vector `from` which represents a path to a map, and a vector `to`, which represents a destination path to a map, take the value pointed at by `(conj from k)` and move it to the path pointed at by `(conj to k)`. If the map at the end of `from` is empty, it is not removed, since we rely on `remove-path`" [m k from to] (let [from-path (conj from k) v (get-in m from-path)] (-> m (dissoc-in from k) (assoc-in (conj to k) v)))) |
Changes to src/streamful/protocol.clj.
︙ | ︙ | |||
299 300 301 302 303 304 305 | (let [response (handle-message router req-state req)] [(count response) (ByteArrayInputStream. response)])]) (defn- send-error [mode reason] (case mode :bytes [:cmd | < | | 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | (let [response (handle-message router req-state req)] [(count response) (ByteArrayInputStream. response)])]) (defn- send-error [mode reason] (case mode :bytes [:cmd (let [encoded (transport/encode-msg reason)] [(count encoded) (ByteArrayInputStream. encoded)])] :cmd [:cmd reason])) (defn- handler [server-keys max-upload-bytes |
︙ | ︙ |
Changes to src/streamful/stream_datalevin.clj.
︙ | ︙ | |||
15 16 17 18 19 20 21 22 23 24 25 26 27 28 | ; License along with streamful. ; If not, see <https://www.gnu.org/licenses/#AGPL>. ; (ns streamful.stream-datalevin (:require [crypto.equality :as creq] [datalevin.core :as db] [streamful.crypto :as cr] [streamful.stream :as stream] [taoensso.timbre :as log])) (def ^:private stream-agg-table "stream-aggregates") (def ^:private stream-data-table "stream-data") (def ^:private stream-mapping-table "stream-mappings") | > | 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | ; License along with streamful. ; If not, see <https://www.gnu.org/licenses/#AGPL>. ; (ns streamful.stream-datalevin (:require [crypto.equality :as creq] [datalevin.core :as db] [streamful.collections :as coll] [streamful.crypto :as cr] [streamful.stream :as stream] [taoensso.timbre :as log])) (def ^:private stream-agg-table "stream-aggregates") (def ^:private stream-data-table "stream-data") (def ^:private stream-mapping-table "stream-mappings") |
︙ | ︙ | |||
269 270 271 272 273 274 275 | iid (assoc msg "agg" agg-value) :string :data]] (db/transact-kv tx [replacement])) (log/error "FAILED to replace aggregate"))))) | | | | | | | < | > | < < < < | 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | iid (assoc msg "agg" agg-value) :string :data]] (db/transact-kv tx [replacement])) (log/error "FAILED to replace aggregate"))))) (defn move-aggregator! [db t k from to] (db/with-transaction-kv [tx db] (db/transact-kv tx [[:put stream-agg-table :aggregates (let [m (get-db-aggregates tx)] (if to (coll/move-path m k [from t] [to t]) (coll/dissoc-in m [from t] k)))]]))) (defn build-stream-model [location] (let [db (db/open-kv location)] (db/open-dbi db stream-agg-table) (db/open-dbi db stream-data-table) (db/open-dbi db stream-mapping-table) (reify stream/StreamModel |
︙ | ︙ |
Changes to test/streamful/aggregates_test.clj.
︙ | ︙ | |||
19 20 21 22 23 24 25 26 27 28 29 | (ns streamful.aggregates-test (:require [clojure.test :refer :all] [streamful.aggregates :as agg] [streamful.client :as client] [streamful.crypto :as cr] [streamful.server.netty :as server] [streamful.server.netty] [streamful.test-aggregates :as tagg] [streamful.test-cfg :as tcfg] [streamful.test-protocol :as tp])) | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | (ns streamful.aggregates-test (:require [clojure.test :refer :all] [streamful.aggregates :as agg] [streamful.client :as client] [streamful.crypto :as cr] [streamful.server.netty :as server] [streamful.server.netty] [streamful.stream :as stream] [streamful.test-aggregates :as tagg] [streamful.test-cfg :as tcfg] [streamful.test-protocol :as tp])) (tcfg/def-configd-server-test commit!-test [out server/start-netty-server! {}] (let [{:keys [new-client]} out client-keys (cr/new-keys) client-pk (cr/b64-str (:public-signing-key client-keys)) client (new-client)] (tp/claimed client client-keys) (tp/is-registered "ok" client "root") (is (= {:response "ok"} (client/add-aggregate client {:t "msg" :n "replies" :s tagg/reply-aggregate-selector-fn :a tagg/reply-aggregator-fn :c tagg/cleanup-reply-fn}))) (is (= {:response "ok"} (client/add-aggregate client {:t "msg" :n "threads" :s tagg/thread-aggregate-selector-fn :a tagg/thread-aggregator-fn :c tagg/cleanup-thread-fn}))) (is (= {:response "ok"} (client/remove-aggregate client {:t "msg", :n "threads"}))) (let [msg-id1 (tp/is-sent tp/ok-put-ptn client client-keys "root" {:t "msg1"}) msg-id2 (tp/is-sent tp/ok-put-ptn client client-keys "root" {:t "reply1", :rid msg-id1, :tid msg-id1}) _ (is (= {:response "ok"} (client/add-aggregate client {:t "msg" :n "threads" :s tagg/thread-aggregate-selector-fn :a tagg/thread-aggregator-fn :c tagg/cleanup-thread-fn}))) expected [[{"ct" 7, "c" "put", "k" client-pk, "m" {"params" {"t" "msg1"}, "sid" "root"}} {"rc" 1, "rm" [msg-id2], "thc" 1, "thm" [msg-id2]}]] actual (tp/get-msg-with-agg client "root")] (is (= expected actual))))) (tcfg/def-configd-server-test reload-aggregators-test [out server/start-netty-server! {}] (let [{:keys [new-client restart-server!]} out client-keys (cr/new-keys) client-pk (cr/b64-str (:public-signing-key client-keys)) client (new-client)] |
︙ | ︙ | |||
67 68 69 70 71 72 73 | msg-id2 (tp/is-sent tp/ok-put-ptn client client-keys "root" | | > > > > | | | | | | | | | | | | | | | | | | | | | | | | | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | msg-id2 (tp/is-sent tp/ok-put-ptn client client-keys "root" {:t "reply1", :rid msg-id1}) consistency-errors (atom [])] (binding [agg/*notify-inconsistent-aggregation-state* (fn [status t n _agg] (swap! consistency-errors conj [status t n]))] (testing "reloads aggregates that were previously installed" (restart-server!) (client/close client) (let [msg-id3 (tp/is-sent tp/ok-put-ptn client client-keys "root" {:t "reply2", :rid msg-id2}) expected [[{"ct" 7, "c" "put", "k" client-pk, "m" {"params" {"t" "msg1"}, "sid" "root"}} {"rc" 1, "rm" [msg-id2]}] [{"ct" 8, "c" "put", "k" client-pk, "m" {"params" {"t" "reply1", "rid" msg-id1}, "sid" "root"}} {"rc" 1, "rm" [msg-id3]}]] actual (tp/get-msg-with-agg client "root")] (is (= expected actual)))) (testing "doesn't have any consistency errors" (is (= [] @consistency-errors))))))) (tcfg/def-configd-server-test consistency-errors-test [out server/start-netty-server! {}] (let [{:keys [stream-model-fn]} out {:keys [public-signing-key]} (cr/new-keys) stream-model (stream-model-fn) consistency-errors (atom [])] (binding [agg/*notify-inconsistent-aggregation-state* (fn [status t n _agg] (swap! consistency-errors conj [status t n]))] (is (= :ok (stream/claim! stream-model public-signing-key))) (is (= :ok (stream/register-stream! stream-model public-signing-key {"m" {"sid" "root"}} (cr/random-bytes 5)))) (is (= :ok (stream/configure-stream! stream-model public-signing-key {"m" {"sid" "root", "params" {"agg+" {"t" "msg" "n" "thing"}}}} (cr/random-bytes 5)))) (is (= :ok (stream/configure-stream! stream-model public-signing-key {"m" {"sid" "root", "params" {"agg-" {"t" "msg" "n" "another"}}}} (cr/random-bytes 5)))) (is (= :ok (stream/configure-stream! stream-model public-signing-key {"m" {"sid" "root", "params" {"agg+" {"t" "msg" "n" "good"}}}} (cr/random-bytes 5)))) (stream/move-aggregator! stream-model "msg" "good" :pending :active) (agg/base-aggregation stream-model) (testing "notifies of consistency errors" (is (= [[:pending "msg" "thing"] [:cleanup "msg" "another"]] @consistency-errors)))))) (tcfg/def-configd-server-test process-streams!-test [out server/start-netty-server! {}] (let [{:keys [new-client stream-model-fn]} out client-keys (cr/new-keys) |
︙ | ︙ |
Added test/streamful/collections_test.clj.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | ; ; This file is part of streamful. ; ; streamful is free software: you can redistribute it and/or modify it ; under the terms ofthe GNU Affero General Public License as published ; by the Free Software Foundation, either version 3 of the License, or ; (at your option) any later version. ; ; streamful is distributed in the hope that it will be useful, but ; WITHOUT ANY WARRANTY; without even the implied warranty of ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; GNU Affero General Public License for more details. ; ; You should have received a copy of the GNU Affero General Public ; License along with streamful. ; If not, see <https://www.gnu.org/licenses/#AGPL>. ; (ns streamful.collections-test (:require [clojure.test :refer :all] [streamful.collections :refer :all])) (deftest dissoc-in-test (testing "removes a value" (is (= {:a {:b "c"}} (dissoc-in {:a {:b "c", :d "e"}} [:a] :d))) (is (= {:a {:b {:c {}}}} (dissoc-in {:a {:b {:c {:d "e"}}}} [:a :b :c] :d)))) (testing "adds path if it doesn't exist" (is (= {:a {:b nil}} (dissoc-in {:a {}} [:a :b] :c))))) (deftest move-test (testing "moves a value" (is (= {:a {:b :c}, :d {}} (move-path {:a {}, :d {:b :c}} :b [:d] [:a]))) (is (= {:a {:b {:c :d, :e :f}}, :g {:h {}}} (move-path {:a {:b {:c :d}}, :g {:h {:e :f}}} :e [:g :h] [:a :b]))))) |