BatchSizeManagement.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Indexer;
  7. /**
  8. * Class set MEMORY table size for indexer processes according batch size and index row size.
  9. */
  10. class BatchSizeManagement implements \Magento\Framework\Indexer\BatchSizeManagementInterface
  11. {
  12. /**
  13. * @var \Magento\Framework\Indexer\IndexTableRowSizeEstimatorInterface
  14. */
  15. private $rowSizeEstimator;
  16. /**
  17. * @var \Psr\Log\LoggerInterface
  18. */
  19. private $logger;
  20. /**
  21. * CompositeProductBatchSizeCalculator constructor.
  22. * @param \Magento\Framework\Indexer\IndexTableRowSizeEstimatorInterface $rowSizeEstimator
  23. * @param \Psr\Log\LoggerInterface $logger
  24. */
  25. public function __construct(
  26. \Magento\Framework\Indexer\IndexTableRowSizeEstimatorInterface $rowSizeEstimator,
  27. \Psr\Log\LoggerInterface $logger
  28. ) {
  29. $this->rowSizeEstimator = $rowSizeEstimator;
  30. $this->logger = $logger;
  31. }
  32. /**
  33. * @inheritdoc
  34. */
  35. public function ensureBatchSize(\Magento\Framework\DB\Adapter\AdapterInterface $connection, $batchSize)
  36. {
  37. $rowMemory = $this->rowSizeEstimator->estimateRowSize();
  38. $maxHeapTableSize = $connection->fetchOne('SELECT @@max_heap_table_size;');
  39. $tmpTableSize = $connection->fetchOne('SELECT @@tmp_table_size;');
  40. $bufferPoolSize = $connection->fetchOne('SELECT @@innodb_buffer_pool_size;');
  41. $maxMemoryTableSize = min($maxHeapTableSize, $tmpTableSize);
  42. $size = (int) ($rowMemory * $batchSize);
  43. // Log warning if allocated memory for temp table greater than 20% of innodb_buffer_pool_size
  44. if ($size > $bufferPoolSize * .2) {
  45. $message = 'Memory size allocated for the temporary table is more than 20% of innodb_buffer_pool_size. ' .
  46. 'Please update innodb_buffer_pool_size or decrease batch size value '.
  47. '(which decreases memory usages for the temporary table). ' .
  48. 'Current batch size: %1; Allocated memory size: %2 bytes; InnoDB buffer pool size: %3 bytes.';
  49. $this->logger->warning(new \Magento\Framework\Phrase($message, [$batchSize, $size, $bufferPoolSize]));
  50. }
  51. if ($maxMemoryTableSize < $size) {
  52. $connection->query('SET SESSION tmp_table_size = ' . $size . ';');
  53. $connection->query('SET SESSION max_heap_table_size = ' . $size . ';');
  54. }
  55. }
  56. }