Skip to content

Commit

Permalink
Don't set throughput on new indexes on pay-per-request tables
Browse files Browse the repository at this point in the history
  • Loading branch information
kipz committed Nov 18, 2019
1 parent 7cac7fe commit c3a4db3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 41 deletions.
80 changes: 40 additions & 40 deletions src/taoensso/faraday.clj
Original file line number Diff line number Diff line change
Expand Up @@ -743,49 +743,49 @@
;;;; Table updates

(defn- global-2nd-index-updates
"Implementation detail.
{:operation _ :name _ :hash-keydef _ :range-keydef _ :projection _ :throughput _}
index -> GlobalSecondaryIndexUpdate}"
[{index-name :name
:keys [hash-keydef range-keydef throughput projection operation]
:or {projection :all}
:as index}]

(case operation
:create
(do
(assert (and index-name hash-keydef projection throughput)
(str "Malformed global secondary index (GSI): " index))
"Validate new GSI configuration and create a GlobalSecondaryIndexUpdate"
[{:keys [gsindexes billing-mode] :as params}]
(let [{index-name :name
:keys [hash-keydef range-keydef throughput projection operation]
:or {projection :all}
:as index} gsindexes]
(case operation
:create
(do
(assert (and index-name hash-keydef projection (or (and throughput
(not= :pay-per-request billing-mode))
(and (not throughput)
(= :pay-per-request billing-mode))))
(str "Malformed global secondary index (GSI): " index))
(doto (GlobalSecondaryIndexUpdate.)
(.setCreate
(doto-cond [_ (CreateGlobalSecondaryIndexAction.)]
:always (.setIndexName (name index-name))
:always (.setKeySchema (key-schema hash-keydef range-keydef))
:always (.setProjection
(let [pr (Projection.)
ptype (if (coll?* projection) :include projection)]
(.setProjectionType pr (utils/enum ptype))
(when (= ptype :include)
(.setNonKeyAttributes pr (mapv name projection)))
pr))
throughput (.setProvisionedThroughput (provisioned-throughput throughput))))))

:update
(doto (GlobalSecondaryIndexUpdate.)
(.setCreate
(doto
(CreateGlobalSecondaryIndexAction.)
(.setIndexName (name index-name))
(.setKeySchema (key-schema hash-keydef range-keydef))
(.setProjection
(let [pr (Projection.)
ptype (if (coll?* projection) :include projection)]
(.setProjectionType pr (utils/enum ptype))
(when (= ptype :include)
(.setNonKeyAttributes pr (mapv name projection)))
pr))
(.setProvisionedThroughput (provisioned-throughput throughput))))))

:update
(doto (GlobalSecondaryIndexUpdate.)
(.setUpdate
(doto
(.setUpdate
(doto
(UpdateGlobalSecondaryIndexAction.)
(.setIndexName (name index-name))
(.setProvisionedThroughput (provisioned-throughput throughput)))))
(.setIndexName (name index-name))
(.setProvisionedThroughput (provisioned-throughput throughput)))))

:delete
(doto (GlobalSecondaryIndexUpdate.)
(.setDelete
(doto
:delete
(doto (GlobalSecondaryIndexUpdate.)
(.setDelete
(doto
(DeleteGlobalSecondaryIndexAction.)
(.setIndexName (name index-name)))))
nil))
(.setIndexName (name index-name)))))
nil)))

(defn- update-table-request "Implementation detail."
[table {:keys [throughput gsindexes stream-spec billing-mode] :as params}]
Expand All @@ -797,7 +797,7 @@
:always (.setTableName (name table))
throughput (.setProvisionedThroughput (provisioned-throughput throughput))
billing-mode (.setBillingMode (utils/enum billing-mode))
gsindexes (.setGlobalSecondaryIndexUpdates [(global-2nd-index-updates gsindexes)])
gsindexes (.setGlobalSecondaryIndexUpdates [(global-2nd-index-updates params)])
stream-spec (.setStreamSpecification (stream-specification stream-spec))
(seq attr-defs) (.setAttributeDefinitions attr-defs))))

Expand Down
24 changes: 23 additions & 1 deletion test/taoensso/faraday/tests/requests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,29 @@
(update-table-request
:update-table
{:throughput {:read 4 :write 2}
:billing-mode :pay-per-request})))))
:billing-mode :pay-per-request}))))

(testing "If parent table is pay-per-request, then can't specify throughput when adding a GSI"
(is (thrown? AssertionError
(update-table-request
:update-table
{:billing-mode :pay-per-request
:gsindexes {:name "new-global-secondary"
:operation :create
:hash-keydef [:id :s]
:projection :keys-only
:throughput {:read 10 :write 9}}}))) )

(testing "If parent table is pay-per-request, then no need to specify billing mode on new GSIs"
(let [req ^UpdateTableRequest
(update-table-request
:update-table
{:billing-mode :pay-per-request
:gsindexes {:name "new-global-secondary"
:operation :create
:hash-keydef [:id :s]
:projection :keys-only}})]
(is (= "update-table" (.getTableName req))))))

(deftest get-item-request-creation
(is (= "get-item"
Expand Down

0 comments on commit c3a4db3

Please sign in to comment.