Skip to content

Commit 95f07b2

Browse files
Merge pull request #11 from nubank/snapshot-streams2
Snapshot streams2
2 parents 17fffe8 + 8bcf26f commit 95f07b2

File tree

2 files changed

+10
-33
lines changed

2 files changed

+10
-33
lines changed

src/prevayler_clj_aws/core.clj

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,12 @@
4646
unmarshal-fn))
4747

4848
(defn- read-snapshot [s3-cli s3-sdk-cli bucket snapshot-path]
49-
(if (snapshot-exists? s3-cli bucket snapshot-path)
50-
(let [v2-path (snapshot-v2-path snapshot-path)
51-
snap1 (read-object s3-sdk-cli bucket snapshot-path unmarshal)]
52-
(try
53-
(if (snapshot-exists? s3-cli bucket v2-path)
54-
(let [snap2 (read-object s3-sdk-cli bucket v2-path unmarshal-from-in)]
55-
(println "Snapshot v1" (if (= snap1 snap2) "IS" "IS NOT") "equal to v2"))
56-
(println v2-path "object not found in bucket."))
57-
(catch Exception e
58-
(.printStackTrace e)))
59-
snap1)
60-
{:partkey 0}))
61-
62-
(defn- save-snapshot! [s3-cli s3-sdk-cli bucket snapshot-path snapshot]
63-
(util/aws-invoke s3-cli {:op :PutObject
64-
:request {:Bucket bucket
65-
:Key snapshot-path
66-
:Body (marshal snapshot)}})
49+
(let [v2-path (snapshot-v2-path snapshot-path)]
50+
(if (snapshot-exists? s3-cli bucket v2-path)
51+
(read-object s3-sdk-cli bucket v2-path unmarshal-from-in)
52+
{:partkey 0})))
53+
54+
(defn- save-snapshot! [s3-sdk-cli bucket snapshot-path snapshot]
6755
(try
6856
(let [v2-path (snapshot-v2-path snapshot-path)
6957
temp-file (java.io.File/createTempFile "snapshot" "")] ; We use an intermediary file to easily determine the length of the stream. Otherwise, to determine its length, Amazon's SDK would buffer the entire stream in RAM, defeating our purpose.
@@ -160,7 +148,7 @@
160148
(println "Saving snapshot to bucket...")
161149
; Since s3 update is atomic, if saving snapshot fails next prevayler will pick the previous state
162150
; and restore events from the previous partkey
163-
(save-snapshot! s3-client s3-sdk-cli s3-bucket snapshot-path {:state @state-atom
151+
(save-snapshot! s3-sdk-cli s3-bucket snapshot-path {:state @state-atom
164152
:partkey (inc @snapshot-index-atom)})
165153
(println "Snapshot done.")
166154
(swap! snapshot-index-atom inc)

test/prevayler_clj_aws/core_test.clj

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@
8080
(testing "snapshot-v2 is the default snapshot file name"
8181
(let [{{:keys [s3-client s3-bucket]} :aws-opts :as opts} (gen-opts)
8282
_ (prev! opts)]
83-
(is (match? [{:Key "snapshot"} {:Key "snapshot-v2"}] (list-objects s3-client s3-bucket)))))
83+
(is (match? [{:Key "snapshot-v2"}] (list-objects s3-client s3-bucket)))))
8484

8585
(testing "can override snapshot file name"
8686
(let [{{:keys [s3-client s3-bucket]} :aws-opts :as opts} (gen-opts :aws-opts {:snapshot-path "my-path"})
8787
_ (prev! opts)]
88-
(is (match? [{:Key "my-path"} {:Key "my-path-v2"}] (list-objects s3-client s3-bucket)))))
88+
(is (match? [{:Key "my-path-v2"}] (list-objects s3-client s3-bucket)))))
8989

9090
(testing "default initial state is empty map"
9191
(let [prevayler (prev! (gen-opts))]
@@ -154,15 +154,4 @@
154154
(prevayler/snapshot! prev1)
155155
(prevayler/snapshot! prev1)
156156
(let [prev2 (prev! (assoc opts :business-fn (constantly "rubbish")))]
157-
(is (= ["A" "B" "C" "D"] @prev2)))))
158-
159-
(testing "it generates snapshot v2"
160-
(let [{{:keys [s3-client s3-sdk-cli s3-bucket]} :aws-opts :as opts} (gen-opts)
161-
_ (util/aws-invoke s3-client {:op :PutObject
162-
:request {:Bucket s3-bucket
163-
:Key "snapshot"
164-
:Body (#'core/marshal {:partkey 0
165-
:state :state})}})
166-
prev (prev! opts)] ;; saves snapshot-v2
167-
(is (= :state @prev))
168-
(is (= {:state :state :partkey 1} (#'core/read-object s3-sdk-cli s3-bucket "snapshot-v2" #'core/unmarshal-from-in))))))
157+
(is (= ["A" "B" "C" "D"] @prev2))))))

0 commit comments

Comments
 (0)