Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
klauswuestefeld committed May 21, 2024
1 parent 5f8bd9f commit 197b7cf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{:deps {prevayler-clj/prevayler4 {:mvn/version "2020.11.14"}
{:deps {prevayler-clj/prevayler4 {:mvn/version "2024.03.18"}
com.cognitect.aws/api {:mvn/version "0.8.575"}
com.cognitect.aws/endpoints {:mvn/version "1.1.12.230"}
com.cognitect.aws/dynamodb {:mvn/version "821.2.1107.0"}
Expand Down
17 changes: 11 additions & 6 deletions src/prevayler_clj_aws/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,19 @@
snapshot-path "snapshot"
page-size 1000}} aws-opts
_ (println "Reading snapshot bucket...")
{state :state old-partkey :partkey} (read-snapshot s3-client s3-bucket snapshot-path)
{state :state previous-snapshot-index :partkey} (read-snapshot s3-client s3-bucket snapshot-path)
_ (println "Reading snapshot bucket done.")
state-atom (atom (or state initial-state))
new-partkey (inc old-partkey)
snapshot-index (inc previous-snapshot-index)
order-atom (atom 0)]

(println "Restoring events...")
(restore-events! dynamodb-client business-fn state-atom dynamodb-table old-partkey page-size)
(restore-events! dynamodb-client business-fn state-atom dynamodb-table previous-snapshot-index page-size)
(println "Restoring events done.")

; since s3 update is atomic, if saving snapshot fails next prevayler will pick the previous state
; and restore events from the previous partkey
(save-snapshot! s3-client s3-bucket snapshot-path {:state @state-atom :partkey new-partkey})
(save-snapshot! s3-client s3-bucket snapshot-path {:state @state-atom :partkey snapshot-index})
(println "Saving snapshot done.")

(reify
Expand All @@ -133,11 +133,16 @@
timestamp (timestamp-fn)
new-state (business-fn current-state event timestamp)] ; (C)onsistency: must be guaranteed by the handler. The event won't be journalled when the handler throws an exception.)
(when-not (identical? new-state current-state)
(write-event! dynamodb-client dynamodb-table new-partkey
(write-event! dynamodb-client dynamodb-table snapshot-index
(swap! order-atom inc) ; Skips an order number if there is an exception, but that's OK.
[timestamp event (hash new-state)]) ; (D)urability
(reset! state-atom new-state)) ; (A)tomicity
new-state)))
new-state)))

#_(snapshot! [this]
(locking this
(start-new-journal! journal-file data-out-atom @state-atom backup)))

(timestamp [_] (timestamp-fn))

IDeref (deref [_] @state-atom)
Expand Down
2 changes: 1 addition & 1 deletion test/prevayler_clj_aws/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(defonce localstack-port
(memoize
#(or (some-> (System/getenv "LOCALSTACK_PORT") (Integer/parseInt))
(-> (tc/create {:image-name "localstack/localstack"
(-> (tc/create {:image-name "localstack/localstack:1.0.4"
:exposed-ports [4566]})
(tc/start!)
:mapped-ports
Expand Down

0 comments on commit 197b7cf

Please sign in to comment.