forked from klauswuestefeld/prevayler-clj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.clj
206 lines (186 loc) · 7.22 KB
/
s3.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
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
105
106
107
108
109
110
111
112
113
114
115
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
(ns prevayler.s3
(:require [prevayler :as p]
[prevayler.locks :refer :all]
[clojure.java.io :as io])
(:import
(java.net URLEncoder)
(java.nio.charset StandardCharsets)
(java.time.format DateTimeFormatter)
(java.time LocalDateTime)
(java.util.concurrent.locks ReentrantLock)
(software.amazon.awssdk.core.sync RequestBody ResponseTransformer)
(software.amazon.awssdk.http.apache ApacheHttpClient)
(software.amazon.awssdk.services.s3 S3Client)
(software.amazon.awssdk.services.s3.model
CopyObjectRequest
DeleteObjectRequest
GetObjectRequest
ListObjectsRequest
PutObjectRequest
S3Object)
(java.io ByteArrayOutputStream FileOutputStream FileInputStream)
(org.apache.commons.io.output ThresholdingOutputStream TeeOutputStream)))
(def ^:private date-formatter
(DateTimeFormatter/ofPattern "uuuu-MM-dd'T'HH:mm:ss.SSS"))
(defn now-str []
(.format date-formatter (LocalDateTime/now)))
(defn- key-exists? [^S3Client s3 bucket key]
(let [r (-> (ListObjectsRequest/builder)
(.bucket bucket)
.build)
bucket-keys (->> r
(.listObjects s3)
.contents
(map (fn [^S3Object o] (.key o)))
set)]
(-> key bucket-keys boolean)))
(defn rename-key! [s3 bucket src dest]
(let [src-url (URLEncoder/encode (format "%s/%s" bucket src)
(str (StandardCharsets/UTF_8)))
copy-r (-> (CopyObjectRequest/builder)
(.copySource src-url)
(.destinationBucket bucket)
(.destinationKey dest)
.build)
delete-r (-> (DeleteObjectRequest/builder)
(.bucket bucket)
(.key src)
.build)]
(.copyObject s3 copy-r)
(.deleteObject s3 delete-r)
true))
(defn- produce-backup! [dbg s3 bucket key]
(let [backup (p/backup-name key)]
(if (key-exists? s3 bucket backup)
(do
(dbg (format "Using backup found at '%s'" backup))
backup)
(if (key-exists? s3 bucket key)
(do
(dbg (format "Renaming '%s' to '%s'" key backup))
(assert (rename-key! s3 bucket key backup))
backup)
(dbg (format "'%s' not found" key))))))
(defn- s3-client []
(-> (S3Client/builder)
(.httpClientBuilder (ApacheHttpClient/builder))
.build))
(defn download [s3 bucket key]
(let [get-r (-> (GetObjectRequest/builder)
(.bucket bucket)
(.key key)
.build)]
(.getObject s3 get-r (ResponseTransformer/toInputStream))))
(defn realize-msg [o] (if (fn? o) (o) o))
(defn- archive! [s3 bucket key]
(let [new-key (p/archive-name key)]
(assert (rename-key! s3 bucket key new-key))))
(defn- keep-uploading! [sleep-time
active?
needs-upload?
s3
bucket
key
backup-contents
die!
dbg]
(let [backup (produce-backup! dbg s3 bucket key)
tries (atom 0)
total-writes (atom 0)
dbg (fn [msg] (dbg #(format "[%d] %s" @total-writes (realize-msg msg))))
max-tries 5]
(when backup (archive! s3 bucket backup))
(dbg "Uploader started")
(while @active?
(when @needs-upload?
(dbg "Upload needed")
(let [upload-success? (atom false)]
(try
(swap! tries inc)
(let [bytes (with-global-access-lock
(do
(reset! needs-upload? false)
(.toByteArray backup-contents)))
put-r (-> (PutObjectRequest/builder)
(.bucket bucket)
(.key key)
.build)]
(dbg "Attempting upload")
(.putObject s3 put-r (RequestBody/fromBytes bytes))
(reset! upload-success? true)
(dbg (fn [] (format "Uploaded bytes: %d" (count bytes))))
(reset! tries 0))
(catch Throwable t
(println "FAILED to complete S3 upload")
(.printStackTrace t)
(if (not @upload-success?) (reset! needs-upload? true))
(when (or (>= @tries max-tries)
(-> t ex-data :die-immediately?))
(println "Giving up. Tries:" @tries)
(die!)))
(finally (swap! total-writes inc)))))
(Thread/sleep sleep-time))
(dbg "Uploader exiting")))
(defn backup-wrapper
[bucket &
{:keys [key debug? dbg-out sleep-time]
:or {key "journal"
sleep-time 1000
dbg-out
(fn [msg] (println (format "[%s] %s" (now-str) msg)))}}]
(fn [out-stream]
(let [s3 (s3-client)
dbg-out (or dbg-out
(fn [msg] (println (format "[%s] %s" (now-str) msg))))
dbg (if debug? (fn [o] (dbg-out (realize-msg o)))
identity)
s3-flush-active? (atom true)
needs-upload? (atom false)
backup-contents (ByteArrayOutputStream.)
die! (fn []
(if @s3-flush-active?
(let [tries (atom 0)]
(while (and @needs-upload? (< @tries 25))
(swap! tries inc)
(Thread/sleep 100))
(if @needs-upload?
(binding [*out* *err*]
(println "WARNING: S3 sync never finished")))
(with-global-access-lock
(dbg "Termination requested")
(reset! s3-flush-active? false)))
(dbg "Already terminated")))
cause-upload! (partial reset! needs-upload? true)
threshholding-stream
(proxy [ThresholdingOutputStream] [1]
(getStream [] backup-contents)
(thresholdReached []
(cause-upload!)
(proxy-super resetByteCount))
(close []
(proxy-super close)
;; In case we are wrapping a stream that writes extra bytes during
;; close -- thresholdReached isn't called during stream closing
(cause-upload!)
(die!)))]
(future (keep-uploading! sleep-time
s3-flush-active?
needs-upload?
s3
bucket
key
backup-contents
die!
dbg))
(TeeOutputStream. out-stream threshholding-stream))))
(comment
(with-open [fout (java.io.FileOutputStream. "test.txt")
wrapped-out ((backup-wrapper (System/getenv "PREVAYLER_BUCKET")
:debug? true
:key "test") fout)
writer (java.io.OutputStreamWriter. wrapped-out)
print-writer (java.io.PrintWriter. writer)]
(dorun (map (fn [n] (.println print-writer (format "hello %d" n)))
(range 100))))
*e
)