WriteRequestBatch.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace Aws\DynamoDb;
  3. use Aws\CommandInterface;
  4. use Aws\CommandPool;
  5. use Aws\Exception\AwsException;
  6. use Aws\ResultInterface;
  7. /**
  8. * The WriteRequestBatch is an object that is capable of efficiently sending
  9. * DynamoDB BatchWriteItem requests from queued up put and delete item requests.
  10. * requests. The batch attempts to send the requests with the fewest requests
  11. * to DynamoDB as possible and also re-queues any unprocessed items to ensure
  12. * that all items are sent.
  13. */
  14. class WriteRequestBatch
  15. {
  16. /** @var DynamoDbClient DynamoDB client used to perform write operations. */
  17. private $client;
  18. /** @var array Configuration options for the batch. */
  19. private $config;
  20. /** @var array Queue of pending put/delete requests in the batch. */
  21. private $queue;
  22. /**
  23. * Creates a WriteRequestBatch object that is capable of efficiently sending
  24. * DynamoDB BatchWriteItem requests from queued up Put and Delete requests.
  25. *
  26. * @param DynamoDbClient $client DynamoDB client used to send batches.
  27. * @param array $config Batch configuration options.
  28. * - table: (string) DynamoDB table used by the batch, this can be
  29. * overridden for each individual put() or delete() call.
  30. * - batch_size: (int) The size of each batch (default: 25). The batch
  31. * size must be between 2 and 25. If you are sending batches of large
  32. * items, you may consider lowering the batch size, otherwise, you
  33. * should use 25.
  34. * - pool_size: (int) This number dictates how many BatchWriteItem
  35. * requests you would like to do in parallel. For example, if the
  36. * "batch_size" is 25, and "pool_size" is 3, then you would send 3
  37. * BatchWriteItem requests at a time, each with 25 items. Please keep
  38. * your throughput in mind when choosing the "pool_size" option.
  39. * - autoflush: (bool) This option allows the batch to automatically
  40. * flush once there are enough items (i.e., "batch_size" * "pool_size")
  41. * in the queue. This defaults to true, so you must set this to false
  42. * to stop autoflush.
  43. * - before: (callable) Executed before every BatchWriteItem operation.
  44. * It should accept an \Aws\CommandInterface object as its argument.
  45. * - error: Executed if an error was encountered executing a,
  46. * BatchWriteItem operation, otherwise errors are ignored. It should
  47. * accept an \Aws\Exception\AwsException as its argument.
  48. *
  49. * @throws \InvalidArgumentException if the batch size is not between 2 and 25.
  50. */
  51. public function __construct(DynamoDbClient $client, array $config = [])
  52. {
  53. // Apply defaults
  54. $config += [
  55. 'table' => null,
  56. 'batch_size' => 25,
  57. 'pool_size' => 1,
  58. 'autoflush' => true,
  59. 'before' => null,
  60. 'error' => null
  61. ];
  62. // Ensure the batch size is valid
  63. if ($config['batch_size'] > 25 || $config['batch_size'] < 2) {
  64. throw new \InvalidArgumentException('"batch_size" must be between 2 and 25.');
  65. }
  66. // Ensure the callbacks are valid
  67. if ($config['before'] && !is_callable($config['before'])) {
  68. throw new \InvalidArgumentException('"before" must be callable.');
  69. }
  70. if ($config['error'] && !is_callable($config['error'])) {
  71. throw new \InvalidArgumentException('"error" must be callable.');
  72. }
  73. // If autoflush is enabled, set the threshold
  74. if ($config['autoflush']) {
  75. $config['threshold'] = $config['batch_size'] * $config['pool_size'];
  76. }
  77. $this->client = $client;
  78. $this->config = $config;
  79. $this->queue = [];
  80. }
  81. /**
  82. * Adds a put item request to the batch.
  83. *
  84. * @param array $item Data for an item to put. Format:
  85. * [
  86. * 'attribute1' => ['type' => 'value'],
  87. * 'attribute2' => ['type' => 'value'],
  88. * ...
  89. * ]
  90. * @param string|null $table The name of the table. This must be specified
  91. * unless the "table" option was provided in the
  92. * config of the WriteRequestBatch.
  93. *
  94. * @return $this
  95. */
  96. public function put(array $item, $table = null)
  97. {
  98. $this->queue[] = [
  99. 'table' => $this->determineTable($table),
  100. 'data' => ['PutRequest' => ['Item' => $item]],
  101. ];
  102. $this->autoFlush();
  103. return $this;
  104. }
  105. /**
  106. * Adds a delete item request to the batch.
  107. *
  108. * @param array $key Key of an item to delete. Format:
  109. * [
  110. * 'key1' => ['type' => 'value'],
  111. * ...
  112. * ]
  113. * @param string|null $table The name of the table. This must be specified
  114. * unless the "table" option was provided in the
  115. * config of the WriteRequestBatch.
  116. *
  117. * @return $this
  118. */
  119. public function delete(array $key, $table = null)
  120. {
  121. $this->queue[] = [
  122. 'table' => $this->determineTable($table),
  123. 'data' => ['DeleteRequest' => ['Key' => $key]],
  124. ];
  125. $this->autoFlush();
  126. return $this;
  127. }
  128. /**
  129. * Flushes the batch by combining all the queued put and delete requests
  130. * into BatchWriteItem commands and executing them. Unprocessed items are
  131. * automatically re-queued.
  132. *
  133. * @param bool $untilEmpty If true, flushing will continue until the queue
  134. * is completely empty. This will make sure that
  135. * unprocessed items are all eventually sent.
  136. *
  137. * @return $this
  138. */
  139. public function flush($untilEmpty = true)
  140. {
  141. // Send BatchWriteItem requests until the queue is empty
  142. $keepFlushing = true;
  143. while ($this->queue && $keepFlushing) {
  144. $commands = $this->prepareCommands();
  145. $pool = new CommandPool($this->client, $commands, [
  146. 'before' => $this->config['before'],
  147. 'concurrency' => $this->config['pool_size'],
  148. 'fulfilled' => function (ResultInterface $result) {
  149. // Re-queue any unprocessed items
  150. if ($result->hasKey('UnprocessedItems')) {
  151. $this->retryUnprocessed($result['UnprocessedItems']);
  152. }
  153. },
  154. 'rejected' => function ($reason) {
  155. if ($reason instanceof AwsException) {
  156. $code = $reason->getAwsErrorCode();
  157. if ($code === 'ProvisionedThroughputExceededException') {
  158. $this->retryUnprocessed($reason->getCommand()['RequestItems']);
  159. } elseif (is_callable($this->config['error'])) {
  160. $this->config['error']($reason);
  161. }
  162. }
  163. }
  164. ]);
  165. $pool->promise()->wait();
  166. $keepFlushing = (bool) $untilEmpty;
  167. }
  168. return $this;
  169. }
  170. /**
  171. * Creates BatchWriteItem commands from the items in the queue.
  172. *
  173. * @return CommandInterface[]
  174. */
  175. private function prepareCommands()
  176. {
  177. // Chunk the queue into batches
  178. $batches = array_chunk($this->queue, $this->config['batch_size']);
  179. $this->queue = [];
  180. // Create BatchWriteItem commands for each batch
  181. $commands = [];
  182. foreach ($batches as $batch) {
  183. $requests = [];
  184. foreach ($batch as $item) {
  185. if (!isset($requests[$item['table']])) {
  186. $requests[$item['table']] = [];
  187. }
  188. $requests[$item['table']][] = $item['data'];
  189. }
  190. $commands[] = $this->client->getCommand(
  191. 'BatchWriteItem',
  192. ['RequestItems' => $requests]
  193. );
  194. }
  195. return $commands;
  196. }
  197. /**
  198. * Re-queues unprocessed results with the correct data.
  199. *
  200. * @param array $unprocessed Unprocessed items from a result.
  201. */
  202. private function retryUnprocessed(array $unprocessed)
  203. {
  204. foreach ($unprocessed as $table => $requests) {
  205. foreach ($requests as $request) {
  206. $this->queue[] = [
  207. 'table' => $table,
  208. 'data' => $request,
  209. ];
  210. }
  211. }
  212. }
  213. /**
  214. * If autoflush is enabled and the threshold is met, flush the batch
  215. */
  216. private function autoFlush()
  217. {
  218. if ($this->config['autoflush']
  219. && count($this->queue) >= $this->config['threshold']
  220. ) {
  221. // Flush only once. Unprocessed items are handled in a later flush.
  222. $this->flush(false);
  223. }
  224. }
  225. /**
  226. * Determine the table name by looking at what was provided and what the
  227. * WriteRequestBatch was originally configured with.
  228. *
  229. * @param string|null $table The table name.
  230. *
  231. * @return string
  232. * @throws \RuntimeException if there was no table specified.
  233. */
  234. private function determineTable($table)
  235. {
  236. $table = $table ?: $this->config['table'];
  237. if (!$table) {
  238. throw new \RuntimeException('There was no table specified.');
  239. }
  240. return $table;
  241. }
  242. }