-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddb_utils.clj
549 lines (499 loc) · 19.4 KB
/
ddb_utils.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
(ns deercreeklabs.ddb-utils
(:require
[clojure.core.async :as ca]
[clojure.string :as string]
[deercreeklabs.async-utils :as au]
[deercreeklabs.baracus :as ba]
[deercreeklabs.log-utils :as lu :refer [debugs]]
[schema.core :as s]
[taoensso.timbre :as timbre :refer [debugf errorf infof]])
(:import
(com.amazonaws ClientConfiguration)
(com.amazonaws.handlers AsyncHandler)
(com.amazonaws.services.dynamodbv2 AmazonDynamoDBAsyncClient
AmazonDynamoDBAsyncClientBuilder)
(com.amazonaws.services.dynamodbv2.model AttributeValue
AttributeValueUpdate
ConditionalCheckFailedException
GetItemRequest
GetItemResult
PutItemResult
QueryRequest
QueryResult
UpdateItemRequest
UpdateItemResult)
(java.nio ByteBuffer)
(java.security SecureRandom)))
(def LockClientOptions
{(s/optional-key :actor-name) s/Str
(s/optional-key :lease-length-ms) s/Int
(s/optional-key :lock-table-name) s/Str
(s/optional-key :refresh-ratio) s/Int})
(def default-lock-client-options
{:actor-name (str "actor-" (apply str (take 4 (repeatedly #(rand-int 10)))))
:lease-length-ms 4000
:lock-table-name "locks"
:refresh-ratio 4})
(defmacro sym-map
"Builds a map from symbols.
Symbol names are turned into keywords and become the map's keys.
Symbol values become the map's values.
(let [a 1
b 2]
(sym-map a b)) => {:a 1 :b 2}"
[& syms]
(zipmap (map keyword syms) syms))
(defn configure-logging
([] (configure-logging :debug))
([level]
(timbre/merge-config!
{:level level
:output-fn lu/short-log-output-fn
:appenders
{:println {:ns-blacklist []}}})))
(defn- parse-number
[^String num-str]
(if (.contains num-str ".")
(Double/parseDouble num-str)
(Long/parseLong num-str)))
(defn get-value [av]
(let [[type val] (some #(when-not (nil? (val %)) %)
(dissoc (bean av) :class))]
(case type
(:s :b :BOOL) val
(:SS :BS) (into #{} val)
:n (parse-number val)
:NS (into #{} (map parse-number val))
:l (into [] (map get-value val))
:m (into {} (map (fn [[k v]] [k (get-value v)]) val))
:NULL nil)))
(defn attr-map->clj-map [am]
(when am
(reduce-kv (fn [acc k av]
(assoc acc (keyword k)
(get-value av)))
{} (into {} am))))
(defn clj-key->str-key [k]
(name k))
(defn clj-value->attr-value [value]
(let [attr (AttributeValue.)]
(cond
(nil? value) (.setNULL attr true)
(number? value) (.setN attr (str value))
(string? value) (.setS attr value)
(instance? Boolean value) (.setBOOL attr value)
(sequential? value) (.setL attr
(map clj-value->attr-value value))
(ba/byte-array? value) (.setB attr (ByteBuffer/wrap value))
(instance? ByteBuffer value) (.setB attr value)
(set? value) (.setSS attr (map name value))
:else (throw (ex-info
(str "Value of type " (class value)
" is not supported")
(sym-map value))))
attr))
(defn clj-map->attr-map [m]
(reduce-kv (fn [acc k v]
(assoc acc (clj-key->str-key k)
(clj-value->attr-value v)))
{} m))
(defn make-success-cb [ret-ch]
(fn [result]
(if (nil? result)
(ca/close! ret-ch)
(ca/put! ret-ch result))))
(defn make-failure-cb [ret-ch op-name]
(fn [e]
(ca/put! ret-ch e)))
(defn make-cbs [ret-ch op-name]
[(make-success-cb ret-ch)
(make-failure-cb ret-ch op-name)])
(defn make-handler
([success-cb failure-cb op-name]
(make-handler success-cb failure-cb op-name identity identity))
([success-cb failure-cb op-name transform-result]
(make-handler success-cb failure-cb op-name transform-result identity))
([success-cb failure-cb op-name transform-result transform-error]
(reify AsyncHandler
(onSuccess [this req result]
(try
(success-cb (transform-result result))
(catch Exception e
(try
(failure-cb e)
(catch Exception e
(errorf "Error calling failure-cb for %s: \n%s" op-name
(lu/get-exception-msg-and-stacktrace e)))))))
(onError [this e]
(try
(failure-cb (transform-error e))
(catch Exception e
(errorf "Error calling failure-cb for %s: \n%s"
op-name (lu/get-exception-msg-and-stacktrace e))))))))
(defn make-boolean-handler [success-cb failure-cb op-name]
(make-handler success-cb failure-cb op-name (constantly true) identity))
(defn ddb-get
([client table-name key-map success-cb failure-cb]
(ddb-get client table-name key-map success-cb failure-cb true))
([^AmazonDynamoDBAsyncClient client table-name key-map
success-cb failure-cb consistent?]
(try
(let [am (clj-map->attr-map key-map)
transform-result (fn [result]
(-> (.getItem ^GetItemResult result)
(attr-map->clj-map)))
handler (make-handler success-cb failure-cb "ddb-get"
transform-result)]
(.getItemAsync client table-name am consistent? handler))
(catch Exception e
(try
(failure-cb e)
(catch Exception e
(errorf "Error calling failure-cb for ddb-get: %s"
(lu/get-exception-msg-and-stacktrace e))))))))
(defn <ddb-get
([client table-name key-map]
(<ddb-get client table-name key-map true))
([client table-name key-map consistent?]
(let [ret-ch (ca/chan)
[success-cb failure-cb] (make-cbs ret-ch "<ddb-get")]
(ddb-get client table-name key-map success-cb failure-cb consistent?)
ret-ch)))
(defn ddb-put
[^AmazonDynamoDBAsyncClient client ^String table-name m success-cb failure-cb]
(try
(let [^java.util.Map item (clj-map->attr-map m)
handler (make-boolean-handler success-cb failure-cb "ddb-put")]
(.putItemAsync client table-name item ^AsyncHandler handler))
(catch Exception e
(try
(failure-cb e)
(catch Exception e
(errorf "Error calling failure-cb for ddb-put: %s"
(lu/get-exception-msg-and-stacktrace e)))))))
(defn <ddb-put [client table-name m]
(let [ret-ch (ca/chan)
[success-cb failure-cb] (make-cbs ret-ch "<ddb-put")]
(ddb-put client table-name m success-cb failure-cb)
ret-ch))
(defn ddb-delete
[^AmazonDynamoDBAsyncClient client ^String table-name key-map
success-cb failure-cb]
(try
(let [^java.util.Map am (clj-map->attr-map key-map)
handler (make-boolean-handler success-cb failure-cb "ddb-delete")]
(.deleteItemAsync client table-name am ^AsyncHandler handler))
(catch Exception e
(try
(failure-cb e)
(catch Exception e
(errorf "Error calling failure-cb for ddb-delete: %s"
(lu/get-exception-msg-and-stacktrace e)))))))
(defn <ddb-delete [^AmazonDynamoDBAsyncClient client ^String table-name key-map]
(let [ret-ch (ca/chan)
[success-cb failure-cb] (make-cbs ret-ch "<ddb-delete")]
(ddb-delete client table-name key-map success-cb failure-cb)
ret-ch))
(defn op-type-dispatch [cond-expr & args]
(when cond-expr
(let [op (first cond-expr)]
(cond
(#{:= :not= :<> :< :> :<= :>=} op) :comparison
(#{:exists :not-exists} op) :existential
:else (throw (ex-info (str "Unknown operator: " op)
(sym-map op cond-expr)))))))
(defmulti cond-expr->string op-type-dispatch)
(defmulti cond-expr->attrs-and-values op-type-dispatch)
(defmethod cond-expr->string :comparison
[cond-expr attr->alias value->alias]
(let [[op attr v] cond-expr
op->str {:= "="
:not= "<>"
:<> "<>"
:< "<"
:> ">"
:<= "<="
:>= ">="}]
(string/join " " [(attr->alias (name attr))
(op->str op)
(value->alias v)])))
(defmethod cond-expr->string :existential
[cond-expr attr->alias value->alias]
(let [[op attr] cond-expr
op->str {:exists "attribute_exists"
:not-exists "attribute_not_exists"}]
(str (op->str op) "(" (attr->alias (name attr)) ")")))
(defmethod cond-expr->string nil
[cond-expr attr->alias value->alias]
nil)
(defmethod cond-expr->attrs-and-values :comparison
[cond-expr]
(let [[op attr v] cond-expr]
[[attr] [v]]))
(defmethod cond-expr->attrs-and-values :existential
[cond-expr]
(let [[op attr] cond-expr]
[[attr] []]))
(defmethod cond-expr->attrs-and-values nil
[cond-expr]
[[] []])
(defn attrs->attr-info [attrs]
(reduce (fn [acc [i attr]]
(let [a-alias (str "#attr" i)
attr (name attr)]
(-> acc
(update :attr->alias assoc attr a-alias)
(update :alias->attr assoc a-alias attr))))
{:attr->alias {}
:alias->attr {}}
(map-indexed vector attrs)))
(defn vals->val-info [vs]
(reduce (fn [acc [i value]]
(let [v-alias (str ":val" i)]
(-> acc
(update :value->alias assoc value v-alias)
(update :alias->value assoc v-alias value))))
{:value->alias {}
:alias->value {}}
(map-indexed vector vs)))
(defn make-update-expression-data [update-map cond-expr]
(let [[ce-attrs ce-vals] (cond-expr->attrs-and-values cond-expr)
um-attrs (keys update-map)
um-vals (vals update-map)
attrs (set (concat ce-attrs um-attrs))
vs (set (concat ce-vals um-vals))
{:keys [attr->alias alias->attr]} (attrs->attr-info attrs)
{:keys [value->alias alias->value]} (vals->val-info vs)
make-update-pair (fn [[attr value]]
(let [a-alias (attr->alias (name attr))
v-alias (value->alias value)]
(str a-alias " = " v-alias)))
uestr (when update-map
(->> update-map
(map make-update-pair)
(string/join ",")
(str "SET ")))
cestr (cond-expr->string cond-expr attr->alias value->alias)]
(sym-map alias->attr alias->value uestr cestr)))
(defn ddb-update
[^AmazonDynamoDBAsyncClient client table-name key-map
update-map cond-expr success-cb failure-cb]
(try
(let [akm (clj-map->attr-map key-map)
transform-result (constantly true)
transform-error (fn [e]
(if (instance? ConditionalCheckFailedException e)
false
e))
handler (make-handler success-cb failure-cb "ddb-update"
transform-result transform-error)
data (make-update-expression-data update-map cond-expr)
{:keys [alias->attr alias->value uestr cestr]} data
uir (doto (UpdateItemRequest.)
(.setTableName table-name)
(.setKey akm)
(.setExpressionAttributeValues (clj-map->attr-map
alias->value)))]
(when uestr
(.setUpdateExpression uir uestr))
(doseq [[alias attr-name] alias->attr]
(.addExpressionAttributeNamesEntry uir alias attr-name))
(when cestr
(.setConditionExpression uir cestr))
(.updateItemAsync client uir handler))
(catch Exception e
(try
(failure-cb e)
(catch Exception e
(errorf "Error calling failure-cb for ddb-update: %s"
(lu/get-exception-msg-and-stacktrace e)))))))
(defn <ddb-update
([client table-name key-map update-map]
(<ddb-update client table-name key-map update-map nil))
([client table-name key-map update-map cond-expr]
(let [ret-ch (ca/chan)
[success-cb failure-cb] (make-cbs ret-ch "<ddb-update")]
(ddb-update client table-name key-map update-map cond-expr
success-cb failure-cb)
ret-ch)))
(defn make-query-data [key-map]
(when (zero? (count key-map))
(throw
(ex-info (str "The key map of a query must contain at least the "
"partition key.")
(sym-map key-map))))
(when (> (count key-map) 2)
(throw
(ex-info (str "Only the patition key and (optionally) the sort key are "
"permitted in the key map of a query.")
(sym-map key-map))))
(let [attrs (keys key-map)
vs (vals key-map)
{:keys [attr->alias alias->attr]} (attrs->attr-info attrs)
{:keys [value->alias alias->value]} (vals->val-info vs)
make-eq-expr (fn [[attr value]]
(let [a-alias (attr->alias (name attr))
v-alias (value->alias value)]
(str a-alias " = " v-alias)))
kcstr (->> key-map
(map make-eq-expr)
(string/join " and "))]
(sym-map alias->attr alias->value kcstr)))
(defn ddb-query
[^AmazonDynamoDBAsyncClient client table-name key-map opts
success-cb failure-cb]
(try
(let [{:keys [limit reverse?]} opts
transform-result (fn [result]
(->> (.getItems ^QueryResult result)
(map attr-map->clj-map)))
handler (make-handler success-cb failure-cb "ddb-query"
transform-result)
data (make-query-data key-map)
{:keys [alias->attr alias->value kcstr]} data
qr (doto (QueryRequest.)
(.setTableName table-name)
(.setKeyConditionExpression kcstr)
(.setExpressionAttributeNames alias->attr)
(.setExpressionAttributeValues (clj-map->attr-map
alias->value)))]
(when limit
(when-not (integer? limit)
(throw (ex-info (str "Query limit must be an integer. Got: " limit)
(sym-map limit opts key-map table-name))))
(.setLimit qr (int limit)))
(when reverse?
(.setScanIndexForward qr false))
(.queryAsync client qr handler))
(catch Exception e
(try
(failure-cb e)
(catch Exception e
(errorf "Error calling failure-cb for ddb-query: %s"
(lu/get-exception-msg-and-stacktrace e)))))))
(defn <ddb-query
([client table-name key-map]
(<ddb-query client table-name key-map {}))
([client table-name key-map opts]
(let [ret-ch (ca/chan)
[success-cb failure-cb] (make-cbs ret-ch "<ddb-query")]
(ddb-query client table-name key-map opts success-cb failure-cb)
ret-ch)))
(defn ^AmazonDynamoDBAsyncClient make-ddb-client []
(AmazonDynamoDBAsyncClientBuilder/defaultClient))
(defn make-lease-id []
(let [rng (SecureRandom.)
bytes (byte-array 8)]
(.nextBytes rng bytes)
(ba/byte-array->b64 bytes)))
(defprotocol IDistributedLockClient
(acquired? [this])
(stop [this])
(release* [this])
(acquire* [this])
(<attempt-acquisition* [this prior-lease-id prior-lease-length-ms])
(<create-lock* [this])
(<refresh-lock* [this])
(start-aquire-loop* [this]))
(defrecord DistributedLockClient
[ddb-client lock-name on-acquire on-release actor-name lease-length-ms
lock-table-name refresh-ratio *acquired? *shutdown?]
IDistributedLockClient
(acquired? [this]
@*acquired?)
(stop [this]
(reset! *shutdown? true)
(release* this))
(release* [this]
(when (acquired? this)
(reset! *acquired? false)
(on-release)))
(acquire* [this]
(when (and (not @*shutdown?)
(not (acquired? this)))
(reset! *acquired? true)
(on-acquire)))
(<attempt-acquisition* [this prior-lease-id prior-lease-length-ms]
(au/go
(ca/<! (ca/timeout prior-lease-length-ms))
(when (not @*shutdown?)
(let [k (sym-map lock-name)
info (au/<? (<ddb-get ddb-client lock-table-name k))
{:keys [owner lease-length-ms lease-id]} info]
(when (and (= prior-lease-id lease-id)
(not @*shutdown?))
(let [update-map {:owner actor-name
:lease-id (make-lease-id)
:lease-length-ms lease-length-ms}
cond-expr [:= :lease-id prior-lease-id]
ret (au/<? (<ddb-update ddb-client lock-table-name k
update-map cond-expr))]
(if ret
(acquire* this)
(release* this))))))))
(<create-lock* [this]
(au/go
(let [owner actor-name
lease-id (make-lease-id)
k (sym-map lock-name)
update-map (sym-map owner lease-id lease-length-ms)
cond-expr [:not-exists :lock-name]
ret (au/<? (<ddb-update ddb-client lock-table-name k
update-map cond-expr))]
(if ret
(do
(acquire* this)
(merge k update-map))
(do
(release* this)
(au/<? (<ddb-get ddb-client lock-table-name k)))))))
(<refresh-lock* [this]
(au/go
(ca/<! (ca/timeout (/ lease-length-ms refresh-ratio)))
(when-not @*shutdown?
(let [k (sym-map lock-name)
update-map {:lease-id (make-lease-id)
:lease-length-ms lease-length-ms}
cond-expr [:= :owner actor-name]
ret (au/<? (<ddb-update ddb-client lock-table-name k
update-map cond-expr))]
(if ret
(acquire* this)
(release* this))))))
(start-aquire-loop* [this]
(ca/go
(try
(while (not @*shutdown?)
(let [k (sym-map lock-name)
info (or (au/<? (<ddb-get ddb-client lock-table-name k))
(au/<? (<create-lock* this)))
{:keys [owner lease-length-ms lease-id]} info]
(if (= actor-name owner)
(when-not @*shutdown?
(acquire* this)
(au/<? (<refresh-lock* this)))
(do
(release* this)
(when-not @*shutdown?
(au/<? (<attempt-acquisition* this lease-id
lease-length-ms)))))))
(catch Exception e
(errorf "Error in start-aquire-loop*: %s"
(lu/get-exception-msg-and-stacktrace e)))))))
(s/defn make-distributed-lock-client :- (s/protocol IDistributedLockClient)
[lock-name :- s/Str
on-acquire :- (s/=> s/Any)
on-release :- (s/=> s/Any)
options :- LockClientOptions]
(let [{:keys [actor-name lease-length-ms lock-table-name refresh-ratio]}
(merge default-lock-client-options options)
^AmazonDynamoDBAsyncClient ddb-client
(AmazonDynamoDBAsyncClientBuilder/defaultClient)
*acquired? (atom false)
*shutdown? (atom false)
client (->DistributedLockClient
ddb-client lock-name on-acquire on-release actor-name
lease-length-ms lock-table-name refresh-ratio
*acquired? *shutdown?)]
(start-aquire-loop* client)
client))