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
|
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# https://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from boto3.dynamodb.table import BatchWriter
from tests import mock, unittest
class BaseTransformationTest(unittest.TestCase):
maxDiff = None
def setUp(self):
self.client = mock.Mock()
self.client.batch_write_item.return_value = {'UnprocessedItems': {}}
self.table_name = 'tablename'
self.flush_amount = 2
self.batch_writer = BatchWriter(
self.table_name, self.client, self.flush_amount
)
def assert_batch_write_calls_are(self, expected_batch_writes):
assert self.client.batch_write_item.call_count == len(
expected_batch_writes
)
batch_write_calls = [
args[1] for args in self.client.batch_write_item.call_args_list
]
assert batch_write_calls == expected_batch_writes
def test_batch_write_does_not_immediately_write(self):
self.batch_writer.put_item(Item={'Hash': 'foo'})
assert not self.client.batch_write_item.called
def test_batch_write_flushes_at_flush_amount(self):
self.batch_writer.put_item(Item={'Hash': 'foo1'})
self.batch_writer.put_item(Item={'Hash': 'foo2'})
expected = {
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo1'}}},
{'PutRequest': {'Item': {'Hash': 'foo2'}}},
]
}
}
self.assert_batch_write_calls_are([expected])
def test_multiple_flushes_reset_items_to_put(self):
self.batch_writer.put_item(Item={'Hash': 'foo1'})
self.batch_writer.put_item(Item={'Hash': 'foo2'})
self.batch_writer.put_item(Item={'Hash': 'foo3'})
self.batch_writer.put_item(Item={'Hash': 'foo4'})
# We should have two batch calls, one for foo1,foo2 and
# one for foo3,foo4.
first_batch = {
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo1'}}},
{'PutRequest': {'Item': {'Hash': 'foo2'}}},
]
}
}
second_batch = {
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo3'}}},
{'PutRequest': {'Item': {'Hash': 'foo4'}}},
]
}
}
self.assert_batch_write_calls_are([first_batch, second_batch])
def test_can_handle_puts_and_deletes(self):
self.batch_writer.put_item(Item={'Hash': 'foo1'})
self.batch_writer.delete_item(Key={'Hash': 'foo2'})
expected = {
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo1'}}},
{'DeleteRequest': {'Key': {'Hash': 'foo2'}}},
]
}
}
self.assert_batch_write_calls_are([expected])
def test_multiple_batch_calls_with_mixed_deletes(self):
self.batch_writer.put_item(Item={'Hash': 'foo1'})
self.batch_writer.delete_item(Key={'Hash': 'foo2'})
self.batch_writer.delete_item(Key={'Hash': 'foo3'})
self.batch_writer.put_item(Item={'Hash': 'foo4'})
first_batch = {
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo1'}}},
{'DeleteRequest': {'Key': {'Hash': 'foo2'}}},
]
}
}
second_batch = {
'RequestItems': {
self.table_name: [
{'DeleteRequest': {'Key': {'Hash': 'foo3'}}},
{'PutRequest': {'Item': {'Hash': 'foo4'}}},
]
}
}
self.assert_batch_write_calls_are([first_batch, second_batch])
def test_unprocessed_items_added_to_next_batch(self):
self.client.batch_write_item.side_effect = [
{
'UnprocessedItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo2'}}}
],
},
},
# Then the last response shows that everything went through
{'UnprocessedItems': {}},
]
self.batch_writer.put_item(Item={'Hash': 'foo1'})
self.batch_writer.put_item(Item={'Hash': 'foo2'})
self.batch_writer.put_item(Item={'Hash': 'foo3'})
# We should have sent two batch requests consisting of 2
# 2 requests. foo1,foo2 and foo2,foo3.
# foo2 is sent twice because the first response has it listed
# as an unprocessed item which means it needs to be part
# of the next batch.
first_batch = {
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo1'}}},
{'PutRequest': {'Item': {'Hash': 'foo2'}}},
]
}
}
second_batch = {
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo2'}}},
{'PutRequest': {'Item': {'Hash': 'foo3'}}},
]
}
}
self.assert_batch_write_calls_are([first_batch, second_batch])
def test_all_items_flushed_on_exit(self):
with self.batch_writer as b:
b.put_item(Item={'Hash': 'foo1'})
self.assert_batch_write_calls_are(
[
{
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo1'}}},
]
},
},
]
)
def test_never_send_more_than_max_batch_size(self):
# Suppose the server sends backs a response that indicates that
# all the items were unprocessed.
self.client.batch_write_item.side_effect = [
{
'UnprocessedItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo1'}}},
{'PutRequest': {'Item': {'Hash': 'foo2'}}},
],
},
},
{
'UnprocessedItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo2'}}},
],
},
},
{'UnprocessedItems': {}},
]
with BatchWriter(self.table_name, self.client, flush_amount=2) as b:
b.put_item(Item={'Hash': 'foo1'})
b.put_item(Item={'Hash': 'foo2'})
b.put_item(Item={'Hash': 'foo3'})
# Note how we're never sending more than flush_amount=2.
first_batch = {
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo1'}}},
{'PutRequest': {'Item': {'Hash': 'foo2'}}},
]
}
}
# Even when the server sends us unprocessed items of 2 elements,
# we'll still only send 2 at a time, in order.
second_batch = {
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo1'}}},
{'PutRequest': {'Item': {'Hash': 'foo2'}}},
]
}
}
# And then we still see one more unprocessed item so
# we need to send another batch.
third_batch = {
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo3'}}},
{'PutRequest': {'Item': {'Hash': 'foo2'}}},
]
}
}
self.assert_batch_write_calls_are(
[first_batch, second_batch, third_batch]
)
def test_repeated_flushing_on_exit(self):
# We're going to simulate unprocessed_items
# returning multiple unprocessed items across calls.
self.client.batch_write_item.side_effect = [
{
'UnprocessedItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo2'}}},
{'PutRequest': {'Item': {'Hash': 'foo3'}}},
],
},
},
{
'UnprocessedItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo3'}}},
],
},
},
{'UnprocessedItems': {}},
]
with BatchWriter(self.table_name, self.client, flush_amount=4) as b:
b.put_item(Item={'Hash': 'foo1'})
b.put_item(Item={'Hash': 'foo2'})
b.put_item(Item={'Hash': 'foo3'})
# So when we exit, we expect three calls.
# First we try the normal batch write with 3 items:
first_batch = {
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo1'}}},
{'PutRequest': {'Item': {'Hash': 'foo2'}}},
{'PutRequest': {'Item': {'Hash': 'foo3'}}},
]
}
}
# Then we see two unprocessed items so we send another batch.
second_batch = {
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo2'}}},
{'PutRequest': {'Item': {'Hash': 'foo3'}}},
]
}
}
# And then we still see one more unprocessed item so
# we need to send another batch.
third_batch = {
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo3'}}},
]
}
}
self.assert_batch_write_calls_are(
[first_batch, second_batch, third_batch]
)
def test_auto_dedup_for_dup_requests(self):
with BatchWriter(
self.table_name,
self.client,
flush_amount=5,
overwrite_by_pkeys=["pkey", "skey"],
) as b:
# dup 1
b.put_item(
Item={'pkey': 'foo1', 'skey': 'bar1', 'other': 'other1'}
)
b.put_item(
Item={'pkey': 'foo1', 'skey': 'bar1', 'other': 'other2'}
)
# dup 2
b.delete_item(
Key={
'pkey': 'foo1',
'skey': 'bar2',
}
)
b.put_item(
Item={'pkey': 'foo1', 'skey': 'bar2', 'other': 'other3'}
)
# dup 3
b.put_item(
Item={'pkey': 'foo2', 'skey': 'bar2', 'other': 'other3'}
)
b.delete_item(
Key={
'pkey': 'foo2',
'skey': 'bar2',
}
)
# dup 4
b.delete_item(
Key={
'pkey': 'foo2',
'skey': 'bar3',
}
)
b.delete_item(
Key={
'pkey': 'foo2',
'skey': 'bar3',
}
)
# 5
b.delete_item(
Key={
'pkey': 'foo3',
'skey': 'bar3',
}
)
# 2nd batch
b.put_item(
Item={'pkey': 'foo1', 'skey': 'bar1', 'other': 'other1'}
)
b.put_item(
Item={'pkey': 'foo1', 'skey': 'bar1', 'other': 'other2'}
)
first_batch = {
'RequestItems': {
self.table_name: [
{
'PutRequest': {
'Item': {
'pkey': 'foo1',
'skey': 'bar1',
'other': 'other2',
}
}
},
{
'PutRequest': {
'Item': {
'pkey': 'foo1',
'skey': 'bar2',
'other': 'other3',
}
}
},
{
'DeleteRequest': {
'Key': {
'pkey': 'foo2',
'skey': 'bar2',
}
}
},
{
'DeleteRequest': {
'Key': {
'pkey': 'foo2',
'skey': 'bar3',
}
}
},
{
'DeleteRequest': {
'Key': {
'pkey': 'foo3',
'skey': 'bar3',
}
}
},
]
}
}
second_batch = {
'RequestItems': {
self.table_name: [
{
'PutRequest': {
'Item': {
'pkey': 'foo1',
'skey': 'bar1',
'other': 'other2',
}
}
},
]
}
}
self.assert_batch_write_calls_are([first_batch, second_batch])
def test_added_unsent_request_not_flushed_put(self):
# If n requests that get sent fail to process where n = flush_amount
# and at least one more request gets created before the second attempt,
# then previously if n requests were successful on the next run and
# returned an empty dict, _item_buffer would be emptied before sending
# the next batch of n requests
self.client.batch_write_item.side_effect = [
{
'UnprocessedItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo1'}}},
{'PutRequest': {'Item': {'Hash': 'foo2'}}},
],
},
},
{
'UnprocessedItems': {},
},
{
'UnprocessedItems': {},
},
]
self.batch_writer.put_item({'Hash': 'foo1'})
self.batch_writer.put_item({'Hash': 'foo2'})
self.batch_writer.put_item({'Hash': 'foo3'})
self.assertIn(
{'PutRequest': {'Item': {'Hash': 'foo3'}}},
self.batch_writer._items_buffer,
)
batch = {
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo1'}}},
{'PutRequest': {'Item': {'Hash': 'foo2'}}},
]
}
}
final_batch = {
'RequestItems': {
self.table_name: [
{'PutRequest': {'Item': {'Hash': 'foo3'}}},
{'PutRequest': {'Item': {'Hash': 'foo4'}}},
]
}
}
# same batch sent twice since all failed on first try
# and flush_items = 2
self.assert_batch_write_calls_are([batch, batch])
# test that the next two items get sent
self.batch_writer.put_item({'Hash': 'foo4'})
self.assert_batch_write_calls_are([batch, batch, final_batch])
# the buffer should be empty now
self.assertEqual(self.batch_writer._items_buffer, [])
def test_added_unsent_request_not_flushed_delete(self):
# If n requests that get sent fail to process where n = flush_amount
# and at least one more request gets created before the second attempt,
# then previously if n requests were successful on the next run and
# returned an empty dict, _item_buffer would be emptied before sending
# the next batch of n requests
self.client.batch_write_item.side_effect = [
{
'UnprocessedItems': {
self.table_name: [
{'DeleteRequest': {'Key': {'Hash': 'foo1'}}},
{'DeleteRequest': {'Key': {'Hash': 'foo2'}}},
],
},
},
{
'UnprocessedItems': {},
},
{
'UnprocessedItems': {},
},
]
self.batch_writer.delete_item({'Hash': 'foo1'})
self.batch_writer.delete_item({'Hash': 'foo2'})
self.batch_writer.delete_item({'Hash': 'foo3'})
self.assertIn(
{'DeleteRequest': {'Key': {'Hash': 'foo3'}}},
self.batch_writer._items_buffer,
)
batch = {
'RequestItems': {
self.table_name: [
{'DeleteRequest': {'Key': {'Hash': 'foo1'}}},
{'DeleteRequest': {'Key': {'Hash': 'foo2'}}},
]
}
}
final_batch = {
'RequestItems': {
self.table_name: [
{'DeleteRequest': {'Key': {'Hash': 'foo3'}}},
{'DeleteRequest': {'Key': {'Hash': 'foo4'}}},
]
}
}
# same batch sent twice since all failed on first try
# and flush_items = 2
self.assert_batch_write_calls_are([batch, batch])
# test that the next two items get sent
self.batch_writer.delete_item({'Hash': 'foo4'})
self.assert_batch_write_calls_are([batch, batch, final_batch])
# the buffer should be empty now
self.assertEqual(self.batch_writer._items_buffer, [])
|