Skip to content

[2.2] - Add command to view mview state and queue #12122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Indexer\Console\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Magento\Framework\Mview\View;
use Magento\Framework\Mview\View\CollectionFactory;
use Magento\Framework\Console\Cli;

/**
* Command for displaying status of mview indexers.
*/
class IndexerStatusMviewCommand extends Command
{
/** @var \Magento\Framework\Mview\View\CollectionInterface $mviewCollection */
private $mviewCollection;

public function __construct(
CollectionFactory $collectionFactory
) {
$this->mviewCollection = $collectionFactory->create();

parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('indexer:status:mview')
->setDescription('Shows status of Mview Indexers and their queue status');

parent::configure();
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$table = $this->getHelperSet()->get('table');
$table->setHeaders(['ID', 'Mode', 'Status', 'Updated', 'Version ID', 'Backlog']);

$rows = [];

/** @var \Magento\Framework\Mview\View $view */
foreach ($this->mviewCollection as $view) {
$state = $view->getState();
$changelog = $view->getChangelog();

try {
$currentVersionId = $changelog->getVersion();
} catch (View\ChangelogTableNotExistsException $e) {
continue;
}

$pendingCount = count($changelog->getList($state->getVersionId(), $currentVersionId));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that this code will have memory usage issue in case of huge number of item in log

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, it's defined as

    public function getList($fromVersionId, $toVersionId)
    {
        $changelogTableName = $this->resource->getTableName($this->getName());
        if (!$this->connection->isTableExists($changelogTableName)) {
            throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName]));
        }

        $select = $this->connection->select()->distinct(
            true
        )->from(
            $changelogTableName,
            [$this->getColumnName()]
        )->where(
            'version_id > ?',
            (int)$fromVersionId
        )->where(
            'version_id <= ?',
            (int)$toVersionId
        );

        return $this->connection->fetchCol($select);
    }

I'll address.


$pendingString = "<error>$pendingCount</error>";
if ($pendingCount <= 0) {
$pendingString = "<info>$pendingCount</info>";
}

$rows[] = [
$view->getId(),
$state->getMode(),
$state->getStatus(),
$state->getUpdated(),
$state->getVersionId(),
$pendingString,
];
}

usort($rows, function ($comp1, $comp2) {
return strcmp($comp1[0], $comp2[0]);
});

$table->addRows($rows);
$table->render($output);

return Cli::RETURN_SUCCESS;
} catch (\Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln($e->getTraceAsString());
}

return Cli::RETURN_FAILURE;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Indexer\Test\Unit\Console\Command;

use \Magento\Framework\Mview;
use Magento\Indexer\Console\Command\IndexerStatusMviewCommand;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\TableHelper;
use Magento\Store\Model\Website;
use Magento\Framework\Console\Cli;
use Magento\Framework\Mview\View\CollectionFactory;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class IndexerStatusMviewCommandTest extends \PHPUnit_Framework_TestCase
{
/**
* @var IndexerStatusMviewCommand
*/
private $command;

/**
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
*/
private $objectManager;

/**
* @var \Magento\Framework\Mview\View\Collection
*/
private $collection;

protected function setUp()
{
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

/** @var \Magento\Framework\Mview\View\Collection $collection */
$this->collection = $this->objectManager->getObject(Mview\View\Collection::class);

$reflectedCollection = new \ReflectionObject($this->collection);
$isLoadedProperty = $reflectedCollection->getProperty('_isCollectionLoaded');
$isLoadedProperty->setAccessible(true);
$isLoadedProperty->setValue($this->collection, true);

$collectionFactory = $this->getMockBuilder(CollectionFactory::class)
->disableOriginalConstructor()
->getMock();
$collectionFactory->method('create')
->willReturn($this->collection);

$this->command = $this->objectManager->getObject(
IndexerStatusMviewCommand::class,
['collectionFactory' => $collectionFactory]
);

/** @var HelperSet $helperSet */
$helperSet = $this->objectManager->getObject(
HelperSet::class,
['helpers' => [$this->objectManager->getObject(TableHelper::class)]]
);

//Inject table helper for output
$this->command->setHelperSet($helperSet);
}

public function testExecute()
{
$mviews = [
[
'view' => [
'view_id' => 'catalog_category_product',
],
'state' => [
'mode' => 'enabled',
'status' => 'idle',
'updated' => '2017-01-01 11:11:11',
'version_id' => 100,
],
'changelog' => [
'version_id' => 110
],
],
[
'view' => [
'view_id' => 'catalog_product_category',
],
'state' => [
'mode' => 'disabled',
'status' => 'idle',
'updated' => '2017-01-01 11:11:11',
'version_id' => 100,
],
'changelog' => [
'version_id' => 200
],
],
[
'view' => [
'view_id' => 'catalog_product_attribute',
],
'state' => [
'mode' => 'enabled',
'status' => 'idle',
'updated' => '2017-01-01 11:11:11',
'version_id' => 100,
],
'changelog' => [
'version_id' => 100
],
],
];

foreach ($mviews as $data) {
$this->collection->addItem($this->generateMviewStub($data['view'], $data['changelog'], $data['state']));
}
$this->collection->addItem($this->getNeverEnabledMviewIndexerWithNoTable());

$tester = new CommandTester($this->command);
$this->assertEquals(Cli::RETURN_SUCCESS, $tester->execute([]));

$linesOutput = array_filter(explode(PHP_EOL, $tester->getDisplay()));
$this->assertCount(7, $linesOutput, 'There should be 7 lines output. 3 Spacers, 1 header, 3 content.');
$this->assertEquals($linesOutput[0], $linesOutput[2], "Lines 0, 2, 7 should be spacer lines");
$this->assertEquals($linesOutput[2], $linesOutput[6], "Lines 0, 2, 6 should be spacer lines");

$headerValues = array_values(array_filter(explode('|', $linesOutput[1])));
$this->assertEquals('ID', trim($headerValues[0]));
$this->assertEquals('Mode', trim($headerValues[1]));
$this->assertEquals('Status', trim($headerValues[2]));
$this->assertEquals('Updated', trim($headerValues[3]));
$this->assertEquals('Version ID', trim($headerValues[4]));
$this->assertEquals('Backlog', trim($headerValues[5]));

$categoryProduct = array_values(array_filter(explode('|', $linesOutput[3])));
$this->assertEquals('catalog_category_product', trim($categoryProduct[0]));
$this->assertEquals('enabled', trim($categoryProduct[1]));
$this->assertEquals('idle', trim($categoryProduct[2]));
$this->assertEquals('2017-01-01 11:11:11', trim($categoryProduct[3]));
$this->assertEquals('100', trim($categoryProduct[4]));
$this->assertEquals('10', trim($categoryProduct[5]));
unset($categoryProduct);

$productAttribute = array_values(array_filter(explode('|', $linesOutput[4])));
$this->assertEquals('catalog_product_attribute', trim($productAttribute[0]));
$this->assertEquals('enabled', trim($productAttribute[1]));
$this->assertEquals('idle', trim($productAttribute[2]));
$this->assertEquals('2017-01-01 11:11:11', trim($productAttribute[3]));
$this->assertEquals('100', trim($productAttribute[4]));
$this->assertEquals('0', trim($productAttribute[5]));
unset($productAttribute);

$productCategory = array_values(array_filter(explode('|', $linesOutput[5])));
$this->assertEquals('catalog_product_category', trim($productCategory[0]));
$this->assertEquals('disabled', trim($productCategory[1]));
$this->assertEquals('idle', trim($productCategory[2]));
$this->assertEquals('2017-01-01 11:11:11', trim($productCategory[3]));
$this->assertEquals('100', trim($productCategory[4]));
$this->assertEquals('100', trim($productCategory[5]));
unset($productCategory);
}

/**
* @param array $viewData
* @param array $changelogData
* @param array $stateData
* @return Mview\View|Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject
*/
protected function generateMviewStub(array $viewData, array $changelogData, array $stateData)
{
/** @var Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject $stub */
$changelog = $this->getMockBuilder(\Magento\Framework\Mview\View\Changelog::class)
->disableOriginalConstructor()
->getMock();

$list = [];
if ($changelogData['version_id'] !== $stateData['version_id']) {
$list = range($stateData['version_id']+1, $changelogData['version_id']);
}

$changelog->expects($this->any())
->method('getList')
->willReturn($list);

$changelog->expects($this->any())
->method('getVersion')
->willReturn($changelogData['version_id']);

/** @var \Magento\Indexer\Model\Mview\View\State|\PHPUnit_Framework_MockObject_MockObject $stub */
$state = $this->getMockBuilder(\Magento\Indexer\Model\Mview\View\State::class)
->disableOriginalConstructor()
->setMethods(['loadByView'])
->getMock();

$state->setData($stateData);

/** @var Mview\View|\PHPUnit_Framework_MockObject_MockObject $stub */
$stub = $this->getMockBuilder(\Magento\Framework\Mview\View::class)
->disableOriginalConstructor()
->setMethods(['getChangelog', 'getState'])
->getMock();

$stub->expects($this->any())
->method('getChangelog')
->willReturn($changelog);

$stub->expects($this->any())
->method('getState')
->willReturn($state);

$stub->setData($viewData);

return $stub;
}

/**
* @return Mview\View|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getNeverEnabledMviewIndexerWithNoTable()
{
/** @var Mview\View\Changelog|\PHPUnit_Framework_MockObject_MockObject $stub */
$changelog = $this->getMockBuilder(\Magento\Framework\Mview\View\Changelog::class)
->disableOriginalConstructor()
->getMock();

$changelog->expects($this->any())
->method('getVersion')
->willThrowException(
new Mview\View\ChangelogTableNotExistsException(new \Magento\Framework\Phrase("Do not render"))
);

/** @var Mview\View|\PHPUnit_Framework_MockObject_MockObject $notInitiatedMview */
$notInitiatedMview = $this->getMockBuilder(\Magento\Framework\Mview\View::class)
->disableOriginalConstructor()
->getMock();

$notInitiatedMview->expects($this->any())
->method('getChangelog')
->willReturn($changelog);

return $notInitiatedMview;
}

public function testExecuteExceptionNoVerbosity()
{
/** @var \Magento\Framework\Mview\View|\PHPUnit_Framework_MockObject_MockObject $stub */
$stub = $this->getMockBuilder(Mview\View::class)
->disableOriginalConstructor()
->getMock();

$stub->expects($this->any())
->method('getChangelog')
->willThrowException(new \Exception("Dummy test exception"));

$this->collection->addItem($stub);

$tester = new CommandTester($this->command);
$this->assertEquals(Cli::RETURN_FAILURE, $tester->execute([]));
$linesOutput = array_filter(explode(PHP_EOL, $tester->getDisplay()));
$this->assertEquals('Dummy test exception', $linesOutput[0]);
}
}
1 change: 1 addition & 0 deletions app/code/Magento/Indexer/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<item name="set-mode" xsi:type="object">Magento\Indexer\Console\Command\IndexerSetModeCommand</item>
<item name="show-mode" xsi:type="object">Magento\Indexer\Console\Command\IndexerShowModeCommand</item>
<item name="status" xsi:type="object">Magento\Indexer\Console\Command\IndexerStatusCommand</item>
<item name="status-mview" xsi:type="object">Magento\Indexer\Console\Command\IndexerStatusMviewCommand</item>
<item name="reset" xsi:type="object">Magento\Indexer\Console\Command\IndexerResetStateCommand</item>
</argument>
</arguments>
Expand Down