Commit 7fa679cb authored by catch's avatar catch
Browse files

Issue #3006750 by phenaproxima, heddn, quietone, neclimdul, andypost,...

Issue #3006750 by phenaproxima, heddn, quietone, neclimdul, andypost, mikelutz, catch, benjifisher, xurizaemon, jofitz, godotislate: Remove memory management from MigrateExecutable

(cherry picked from commit abaa2c88)
parent 0bb58159
Loading
Loading
Loading
Loading
Loading
+0 −24
Original line number Diff line number Diff line
@@ -23610,12 +23610,6 @@
	'count' => 1,
	'path' => __DIR__ . '/modules/migrate/src/MigrateException.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\migrate\\\\MigrateExecutable\\:\\:checkStatus\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/modules/migrate/src/MigrateExecutable.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\migrate\\\\MigrateExecutable\\:\\:handleException\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
@@ -24423,24 +24417,6 @@
	'count' => 1,
	'path' => __DIR__ . '/modules/migrate/tests/src/Unit/TestMigrateExecutable.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Tests\\\\migrate\\\\Unit\\\\TestMigrateExecutable\\:\\:setMemoryLimit\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/modules/migrate/tests/src/Unit/TestMigrateExecutable.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Tests\\\\migrate\\\\Unit\\\\TestMigrateExecutable\\:\\:setMemoryThreshold\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/modules/migrate/tests/src/Unit/TestMigrateExecutable.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Tests\\\\migrate\\\\Unit\\\\TestMigrateExecutable\\:\\:setMemoryUsage\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/modules/migrate/tests/src/Unit/TestMigrateExecutable.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Tests\\\\migrate\\\\Unit\\\\TestMigrateExecutable\\:\\:setSource\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
+1 −137
Original line number Diff line number Diff line
@@ -2,8 +2,6 @@

namespace Drupal\migrate;

use Drupal\Component\Utility\Bytes;
use Drupal\Core\StringTranslation\ByteSizeMarkup;
use Drupal\Core\Utility\Error;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\migrate\Event\MigrateEvents;
@@ -40,20 +38,6 @@ class MigrateExecutable implements MigrateExecutableInterface {
   */
  protected $sourceRowStatus;

  /**
   * The ratio of the memory limit at which an operation will be interrupted.
   *
   * @var float
   */
  protected $memoryThreshold = 0.85;

  /**
   * The PHP memory_limit expressed in bytes.
   *
   * @var int
   */
  protected $memoryLimit;

  /**
   * The configuration values of the source.
   *
@@ -92,7 +76,7 @@ class MigrateExecutable implements MigrateExecutableInterface {
  public $message;

  /**
   * Constructs a MigrateExecutable and verifies and sets the memory limit.
   * Constructs a MigrateExecutable.
   *
   * @param \Drupal\migrate\Plugin\MigrationInterface $migration
   *   The migration to run.
@@ -106,14 +90,6 @@ public function __construct(MigrationInterface $migration, ?MigrateMessageInterf
    $this->message = $message ?: new MigrateMessage();
    $this->getIdMap()->setMessage($this->message);
    $this->eventDispatcher = $event_dispatcher;
    // Record the memory limit in bytes.
    $limit = trim(ini_get('memory_limit'));
    if ($limit == '-1') {
      $this->memoryLimit = PHP_INT_MAX;
    }
    else {
      $this->memoryLimit = Bytes::toNumber($limit);
    }
  }

  /**
@@ -278,11 +254,6 @@ public function import() {

        $this->sourceRowStatus = MigrateIdMapInterface::STATUS_IMPORTED;

        // Check for memory exhaustion.
        if (($return = $this->checkStatus()) != MigrationInterface::RESULT_COMPLETED) {
          break;
        }

        // If anyone has requested we stop, return the requested result.
        if ($this->migration->getStatus() == MigrationInterface::STATUS_STOPPING) {
          $return = $this->migration->getInterruptionResult();
@@ -357,11 +328,6 @@ public function rollback() {
      }
      $id_map->next();

      // Check for memory exhaustion.
      if (($return = $this->checkStatus()) != MigrationInterface::RESULT_COMPLETED) {
        break;
      }

      // If anyone has requested we stop, return the requested result.
      if ($this->migration->getStatus() == MigrationInterface::STATUS_STOPPING) {
        $return = $this->migration->getInterruptionResult();
@@ -518,106 +484,4 @@ protected function handleException(\Exception $exception, $save = TRUE) {
    $this->message->display($message, 'error');
  }

  /**
   * Checks for exceptional conditions, and display feedback.
   */
  protected function checkStatus() {
    if ($this->memoryExceeded()) {
      return MigrationInterface::RESULT_INCOMPLETE;
    }
    return MigrationInterface::RESULT_COMPLETED;
  }

  /**
   * Tests whether we've exceeded the desired memory threshold.
   *
   * If so, output a message.
   *
   * @return bool
   *   TRUE if the threshold is exceeded, otherwise FALSE.
   */
  protected function memoryExceeded() {
    $usage = $this->getMemoryUsage();
    $pct_memory = $usage / $this->memoryLimit;
    if (!$threshold = $this->memoryThreshold) {
      return FALSE;
    }
    if ($pct_memory > $threshold) {
      $this->message->display(
        $this->t(
          'Memory usage is @usage (@pct% of limit @limit), reclaiming memory.',
          [
            '@pct' => round($pct_memory * 100),
            '@usage' => ByteSizeMarkup::create($usage, NULL, $this->stringTranslation),
            '@limit' => ByteSizeMarkup::create($this->memoryLimit, NULL, $this->stringTranslation),
          ]
        ),
        'warning'
      );
      $usage = $this->attemptMemoryReclaim();
      $pct_memory = $usage / $this->memoryLimit;
      // Use a lower threshold - we don't want to be in a situation where we
      // keep coming back here and trimming a tiny amount.
      if ($pct_memory > (0.90 * $threshold)) {
        $this->message->display(
          $this->t(
            'Memory usage is now @usage (@pct% of limit @limit), not enough reclaimed, starting new batch',
            [
              '@pct' => round($pct_memory * 100),
              '@usage' => ByteSizeMarkup::create($usage, NULL, $this->stringTranslation),
              '@limit' => ByteSizeMarkup::create($this->memoryLimit, NULL, $this->stringTranslation),
            ]
          ),
          'warning'
        );
        return TRUE;
      }
      else {
        $this->message->display(
          $this->t(
            'Memory usage is now @usage (@pct% of limit @limit), reclaimed enough, continuing',
            [
              '@pct' => round($pct_memory * 100),
              '@usage' => ByteSizeMarkup::create($usage, NULL, $this->stringTranslation),
              '@limit' => ByteSizeMarkup::create($this->memoryLimit, NULL, $this->stringTranslation),
            ]
          ),
          'warning');
        return FALSE;
      }
    }
    else {
      return FALSE;
    }
  }

  /**
   * Returns the memory usage so far.
   *
   * @return int
   *   The memory usage.
   */
  protected function getMemoryUsage() {
    return memory_get_usage();
  }

  /**
   * Tries to reclaim memory.
   *
   * @return int
   *   The memory usage after reclaim.
   */
  protected function attemptMemoryReclaim() {
    // First, try resetting Drupal's static storage - this frequently releases
    // plenty of memory to continue.
    drupal_static_reset();

    // @todo Explore resetting the container.

    // Run garbage collector to further reduce memory.
    gc_collect_cycles();

    return memory_get_usage();
  }

}
+0 −120
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\migrate\Unit;

use PHPUnit\Framework\Attributes\Group;
use Prophecy\Argument;

/**
 * Tests the \Drupal\migrate\MigrateExecutable::memoryExceeded() method.
 */
#[Group('migrate')]
class MigrateExecutableMemoryExceededTest extends MigrateTestCase {

  /**
   * The mocked migration entity.
   *
   * @var \Drupal\migrate\Plugin\MigrationInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $migration;

  /**
   * The mocked migrate message.
   *
   * @var \Drupal\migrate\MigrateMessageInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $message;

  /**
   * The tested migrate executable.
   *
   * @var \Drupal\Tests\migrate\Unit\TestMigrateExecutable
   */
  protected $executable;

  /**
   * The migration configuration, initialized to set the ID to test.
   *
   * @var array
   */
  protected $migrationConfiguration = [
    'id' => 'test',
  ];

  /**
   * The php.ini memory_limit value.
   *
   * @var int
   */
  protected $memoryLimit = 10000000;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->migration = $this->getMigration();
    $this->message = $this->prophesize('Drupal\migrate\MigrateMessageInterface');

    $this->executable = new TestMigrateExecutable($this->migration, $this->message->reveal());
    $this->executable->setStringTranslation($this->getStringTranslationStub());
  }

  /**
   * Runs the actual test.
   *
   * @param string $message
   *   The second message to assert.
   * @param bool $memory_exceeded
   *   Whether to test the memory exceeded case.
   * @param int|null $memory_usage_first
   *   (optional) The first memory usage value. Defaults to NULL.
   * @param int|null $memory_usage_second
   *   (optional) The fake amount of memory usage reported after memory reclaim.
   *   Defaults to NULL.
   * @param int|null $memory_limit
   *   (optional) The memory limit. Defaults to NULL.
   */
  protected function runMemoryExceededTest($message, $memory_exceeded, $memory_usage_first = NULL, $memory_usage_second = NULL, $memory_limit = NULL): void {
    $this->executable->setMemoryLimit($memory_limit ?: $this->memoryLimit);
    $this->executable->setMemoryUsage($memory_usage_first ?: $this->memoryLimit, $memory_usage_second ?: $this->memoryLimit);
    $this->executable->setMemoryThreshold(0.85);
    if ($message) {
      $this->message->display(Argument::that(fn(string $subject) => str_contains($subject, 'reclaiming memory')), 'warning')
        ->shouldBeCalledOnce();
      $this->message->display(Argument::that(fn(string $subject) => str_contains($subject, $message)), 'warning')
        ->shouldBeCalledOnce();
    }
    else {
      $this->message->display(Argument::cetera())
        ->shouldNotBeCalled();
    }
    $result = $this->executable->memoryExceeded();
    $this->assertEquals($memory_exceeded, $result);
  }

  /**
   * Tests memoryExceeded method when a new batch is needed.
   */
  public function testMemoryExceededNewBatch(): void {
    // First case try reset and then start new batch.
    $this->runMemoryExceededTest('starting new batch', TRUE);
  }

  /**
   * Tests memoryExceeded method when enough is cleared.
   */
  public function testMemoryExceededClearedEnough(): void {
    $this->runMemoryExceededTest('reclaimed enough', FALSE, $this->memoryLimit, $this->memoryLimit * 0.75);
  }

  /**
   * Tests memoryExceeded when memory usage is not exceeded.
   */
  public function testMemoryNotExceeded(): void {
    $this->runMemoryExceededTest('', FALSE, floor($this->memoryLimit * 0.85) - 1);
  }

}
+0 −71
Original line number Diff line number Diff line
@@ -12,20 +12,6 @@
 */
class TestMigrateExecutable extends MigrateExecutable {

  /**
   * The fake memory usage in bytes.
   *
   * @var int
   */
  protected $memoryUsage;

  /**
   * The cleared memory usage.
   *
   * @var int
   */
  protected $clearedMemoryUsage;

  /**
   * Sets the string translation service.
   *
@@ -68,61 +54,4 @@ public function handleException(\Exception $exception, $save = TRUE) {
    $this->message->display($message);
  }

  /**
   * Allows access to the protected memoryExceeded method.
   *
   * @return bool
   *   The memoryExceeded value.
   */
  public function memoryExceeded() {
    return parent::memoryExceeded();
  }

  /**
   * {@inheritdoc}
   */
  protected function attemptMemoryReclaim() {
    return $this->clearedMemoryUsage;
  }

  /**
   * {@inheritdoc}
   */
  protected function getMemoryUsage() {
    return $this->memoryUsage;
  }

  /**
   * Sets the fake memory usage.
   *
   * @param int $memory_usage
   *   The fake memory usage value.
   * @param int $cleared_memory_usage
   *   (optional) The fake cleared memory value. Defaults to NULL.
   */
  public function setMemoryUsage($memory_usage, $cleared_memory_usage = NULL) {
    $this->memoryUsage = $memory_usage;
    $this->clearedMemoryUsage = $cleared_memory_usage;
  }

  /**
   * Sets the memory limit.
   *
   * @param int $memory_limit
   *   The memory limit.
   */
  public function setMemoryLimit($memory_limit) {
    $this->memoryLimit = $memory_limit;
  }

  /**
   * Sets the memory threshold.
   *
   * @param float $threshold
   *   The new threshold.
   */
  public function setMemoryThreshold($threshold) {
    $this->memoryThreshold = $threshold;
  }

}