Skip to content

Commit

Permalink
clean up ClientRequestToken
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Gaultney committed Jul 22, 2021
1 parent 50b16ee commit 0fbee27
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
15 changes: 8 additions & 7 deletions xoto3/dynamodb/write_versioned/ddb_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,13 @@ def boto3_transact_multiple_but_optimize_single(TransactItems: List[dict], **kwa
if len(TransactItems) == 0:
_log.debug("Nothing to transact - returning")
return
# ClientRequestToken, if provided, indicates a desire to use
# certain idempotency guarantees provided only by
# TransactWriteItems. I'm not sure if it's even relevant for a
# single-item operation, but it's an issue of expense, not
# correctness, to leave it in.
if len(TransactItems) == 1 and "ClientRequestToken" not in kwargs:
# attempt single put or delete to halve the cost
# attempt simple condition-checked put or delete to halve the cost
command = TransactItems[0]
item_args = tuple(command.values())[0] # first and only value is a dict of arguments
if set(command) == {"Put"}:
Expand All @@ -138,7 +143,7 @@ def boto3_transact_multiple_but_optimize_single(TransactItems: List[dict], **kwa
return
if set(command) == {"ConditionCheck"}:
_log.debug(
"A solitary ConditionCheck is meaningless - you already retrieved the item"
"Item was not modified and is solitary - no need to interact with the table"
)
return
# we don't (yet) support single write optimization for things other than Put or Delete
Expand Down Expand Up @@ -172,7 +177,6 @@ def _serialize_versioned_expr(expr: dict) -> dict:
def built_transaction_to_transact_write_items_args(
transaction: VersionedTransaction,
last_written_at_str: str,
ClientRequestToken: str = "",
item_version_attribute: str = "item_version",
last_written_attribute: str = "last_written_at",
) -> dict:
Expand Down Expand Up @@ -261,7 +265,4 @@ def item_remains_unmodified(
]
)

args: Dict[str, Any] = dict(TransactItems=transact_items)
if ClientRequestToken:
args["ClientRequestToken"] = ClientRequestToken
return args
return dict(TransactItems=transact_items)
6 changes: 5 additions & 1 deletion xoto3/dynamodb/write_versioned/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def versioned_transact_write_items(
moment of the successful transaction completion, including items
you did not need to modify.
An exception to the above rule is that if you interact with only
one item and you do not change it, we will not call
TransactWriteItems at all.
Returns the completed transaction, which contains the resulting
items as written to the table(s) at the completion of the
transaction.
Expand All @@ -71,6 +75,7 @@ def versioned_transact_write_items(
The implementation for transact_write_items will also optimize
your DynamoDB costs by reverting to a simple (but still versioned)
Put or Delete if you only operate on a single item.
"""
batch_get_item, transact_write_items = boto3_impl_defaults(
batch_get_item, transact_write_items,
Expand All @@ -90,7 +95,6 @@ def versioned_transact_write_items(
**built_transaction_to_transact_write_items_args(
built_transaction,
iso8601strict(datetime.utcnow()),
"",
item_version_attribute,
last_written_attribute,
)
Expand Down

0 comments on commit 0fbee27

Please sign in to comment.