Skip to content

Commit

Permalink
We no longer need to do stepping when updating throughput
Browse files Browse the repository at this point in the history
See discussion on #78
  • Loading branch information
ricardojmendez committed Dec 15, 2015
1 parent 2fcf4d2 commit 1640651
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 39 deletions.
48 changes: 12 additions & 36 deletions src/taoensso/faraday.clj
Original file line number Diff line number Diff line change
Expand Up @@ -570,23 +570,6 @@
(when-not (describe-table client-opts table-name)
(create-table client-opts table-name hash-keydef opts)))

(defn throughput-steps
"Implementation detail.
Dec by any amount, inc by <= 2x current amount, Ref. http://goo.gl/Bj9TC.
x - start, x' - current, x* - goal."
[[r w] [r* w*]]
(let [step (fn [x* x'] (if (< x* x') x* (min x* (* 2 x'))))]
(loop [steps [] [r' w'] [r w]]
(if (= [r' w'] [r* w*])
steps
(let [[r' w' :as this-step] [(step r* r') (step w* w')]]
(recur (conj steps this-step) this-step))))))

(comment (throughput-steps [1 1] [1 1])
(throughput-steps [1 1] [12 12])
(throughput-steps [3 3] [27 27])
(throughput-steps [17 8] [3 22])
(throughput-steps [1 1] [300 300]))

(defn update-table-request "Implementation detail."
[table throughput]
Expand All @@ -595,11 +578,9 @@
(.setProvisionedThroughput (provisioned-throughput throughput))))

(defn update-table
"Updates a table. Allows automatic multi-step adjustments to conform to
update limits, Ref. http://goo.gl/Bj9TC.
Returns a promise to which the final resulting table description will be
delivered. Deref this promise to block until update (all steps) complete.
"Updates a table. Returns a promise to which the final resulting table
description will be delivered. Deref this promise to block until update
has completed.
Possible values to update:
Expand All @@ -611,28 +592,23 @@
{read* :read write* :write} throughput*
{:keys [status throughput]} (describe-table client-opts table)
{:keys [read write num-decreases-today]} throughput
read* (or read* read)
write* (or write* write)
{max-reqs :max} span-reqs
decreasing? (or (< read* read) (< write* write))
steps (throughput-steps [read write] [read* write*])
nsteps (count steps)]
read* (or read* read)
write* (or write* write)
decreasing? (or (< read* read) (< write* write))]
(cond (not= status :active)
(throw (Exception. (str "Invalid table status: " status)))
(and decreasing? (>= num-decreases-today 4)) ; API limit
(throw (Exception. (str "Max 4 decreases per 24hr period")))
(> nsteps max-reqs)
(throw (Exception. (str "`max-reqs` too low, needs reqs: " nsteps)))
(and decreasing? (>= num-decreases-today 4))
(throw (Exception. (str "API Limit - Max 4 decreases per 24hr period")))
:else
(letfn [(run1 [[r' w']]
(letfn [(async-update []
(as-map
(.updateTable (db-client client-opts)
(update-table-request table {:read r' :write w'})))
(.updateTable (db-client client-opts)
(update-table-request table {:read read* :write write*})))
;; Returns _new_ descr when ready:
@(table-status-watch client-opts table :updating))]

(let [p (promise)]
(future (deliver p (peek (mapv run1 steps))))
(future (deliver p (async-update)))
p)))))

(comment
Expand Down
15 changes: 12 additions & 3 deletions test/taoensso/faraday/tests/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -815,17 +815,26 @@
[:artist :s]
{:throughput {:read 1 :write 1}
:block? true})
updated @(far/update-table *client-opts* temp-table {:throughput {:read 3 :write 4}})
updated @(far/update-table *client-opts* temp-table {:throughput {:read 16 :write 16}})
again @(far/update-table *client-opts* temp-table {:throughput {:read 256 :write 256}})
]
; Both table descriptions are the same other than the throughput
(expect (dissoc created :throughput)
(dissoc updated :throughput))
(expect (dissoc updated :throughput)
(dissoc again :throughput))
; Throughput was updated
(expect
{:read 3 :write 4}
{:read 16 :write 16}
(-> updated
:throughput
(select-keys #{:read :write}))))
(select-keys #{:read :write})))
(expect
{:read 256 :write 256}
(-> again
:throughput
(select-keys #{:read :write})))
)


;;; Test `list-tables` lazy sequence
Expand Down

0 comments on commit 1640651

Please sign in to comment.