Skip to content
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

Prevent running again already running cron group #12497

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c959393
Add LockManager and Backend to Framework functionalities
paveq Nov 30, 2017
702c93a
Implement Locker in CronQueueObserver
paveq Nov 30, 2017
fcda71d
Do cleanup and generate of schedules under cron group lock
paveq Nov 30, 2017
016387c
Add scalar type hints, slight change of wording
paveq Nov 30, 2017
23cb078
Code style fix
paveq Nov 30, 2017
b69945b
Add static prefix for cron locks
paveq Dec 1, 2017
071df9f
Add doc block
paveq Dec 1, 2017
7a88a9a
Add unit and integration tests for locking
paveq Dec 1, 2017
2972e21
Fix failing tests
paveq Dec 1, 2017
4ad9c5d
Amend docblocks
paveq Dec 1, 2017
1e546c8
Add README for Lock library, fix issue with lock release, add a comment
paveq Dec 1, 2017
47759c8
rename setLock to acquireLock for consistency
paveq Dec 1, 2017
dd0b861
Add lockManagerMock for ProcessCronQueueObserverTest
paveq Dec 1, 2017
d1b56b2
Code style fix
paveq Dec 1, 2017
c2e5520
Fix ProcessCronQueueObserverTest
paveq Dec 1, 2017
7992e1e
Add DB name as lock prefix
paveq Dec 5, 2017
2a7099e
Fix failing unit test
paveq Dec 5, 2017
79310b9
Fix code style violations
paveq Dec 5, 2017
229476f
Add checks for pre-MySQL 5.7 locking, print out lock name if too long
paveq Feb 21, 2018
d12581e
Fix failing test case
paveq Feb 21, 2018
bf8409b
Fix failing static test
paveq Feb 21, 2018
209b2bd
Simplify method verbs in LockInterface
paveq Feb 22, 2018
c8be788
#12497 Prevent running again already running cron group
kandy Mar 24, 2018
83b17fe
Merge remote-tracking branch 'mainline/2.2-develop' into cron_group_l…
kandy Mar 25, 2018
79d60cb
#12497 Prevent running again already running cron group
kandy Mar 25, 2018
50dcc3d
magento/magento2#12497
Mar 28, 2018
175229e
magento/magento2#12497
Mar 28, 2018
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
39 changes: 36 additions & 3 deletions app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ class ProcessCronQueueObserver implements ObserverInterface
*/
const SECONDS_IN_MINUTE = 60;

/**
* How long to wait for cron group to become unlocked
*/
const LOCK_TIMEOUT = 5;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would not hard-code timeout. Some processes can take more than 5min based on the amount of the processed data. The easiest solution would be to make it configurable (either in Admin Panel or in config.php). Better solution could be to adjust automatically (e.g., make each process output something and if there is no output consider it hung and release the lock), but it might be a more complex solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is actually five seconds, after that we give up and continue to the next group.

Reasoning being: cron:run is triggered from system crontab every minute, so next check will happen within so small period, that it does not make sense to wait for a long time. I suppose this could be reduced to even one or zero seconds (skip group immediately if lock could not be taken).

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense. Thanks. I think it's ok to leave it as is (5s).


/**
* Static lock prefix for cron group locking
*/
const LOCK_PREFIX = 'CRON_GROUP_';

/**
* @var \Magento\Cron\Model\ResourceModel\Schedule\Collection
*/
Expand Down Expand Up @@ -116,6 +126,11 @@ class ProcessCronQueueObserver implements ObserverInterface
*/
private $state;

/**
* @var \Magento\Framework\Lock\LockManagerInterface
*/
private $lockManager;

/**
* @var array
*/
Expand All @@ -138,6 +153,7 @@ class ProcessCronQueueObserver implements ObserverInterface
* @param \Magento\Framework\Process\PhpExecutableFinderFactory $phpExecutableFinderFactory
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\App\State $state
* @param \Magento\Framework\Lock\LockManagerInterface $lockManager
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -151,7 +167,8 @@ public function __construct(
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
\Magento\Framework\Process\PhpExecutableFinderFactory $phpExecutableFinderFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\App\State $state
\Magento\Framework\App\State $state,
\Magento\Framework\Lock\LockManagerInterface $lockManager

Choose a reason for hiding this comment

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

Shouldn't BC be preserved?

Copy link
Contributor

Choose a reason for hiding this comment

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

@adrian-martinez-interactiv4, We treat Observers and Plugins as internal implementation and don't try to preserve BC for them

) {
$this->_objectManager = $objectManager;
$this->_scheduleFactory = $scheduleFactory;
Expand All @@ -164,6 +181,7 @@ public function __construct(
$this->phpExecutableFinder = $phpExecutableFinderFactory->create();
$this->logger = $logger;
$this->state = $state;
$this->lockManager = $lockManager;
}

/**
Expand All @@ -186,8 +204,6 @@ public function execute(\Magento\Framework\Event\Observer $observer)
$phpPath = $this->phpExecutableFinder->find() ?: 'php';

foreach ($jobGroupsRoot as $groupId => $jobsRoot) {
$this->_cleanup($groupId);
$this->_generate($groupId);
if ($this->_request->getParam('group') !== null
&& $this->_request->getParam('group') !== '\'' . ($groupId) . '\''
&& $this->_request->getParam('group') !== $groupId
Expand All @@ -211,6 +227,21 @@ public function execute(\Magento\Framework\Event\Observer $observer)
continue;
}

// Note: we acquire the lock here instead of above, as it should be taken by standalone (child) process,
// not by the parent process.
if (!$this->lockManager->acquireLock(self::LOCK_PREFIX . $groupId, self::LOCK_TIMEOUT)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Regarding the groups vs jobs discussion. Groups are just logical grouping for the user. There is no guarantee that different groups would not have dependent jobs. So locking mechanism should be implemented per job.

Dependent jobs is a good idea for the future cron evolution.

Copy link
Contributor

@ihor-sviziev ihor-sviziev Dec 22, 2017

Choose a reason for hiding this comment

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

Do we have any examples where one cron job has dependency to another one? Also currently they could be ran in parallel, so we already have this issue. Not sure we need to introduce fix for it as part of this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I previously commented that I would prefer cron groups being removed, but let's wait comments from Core team.

At this moment Groups are logical grouping, which would itself be ideal for this use. I can see scenario where example product import related jobs could be put into the same group, so that they do not run parallel, causing less stress on indexing and avoiding possible deadlocks.

Copy link
Contributor

Choose a reason for hiding this comment

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

For now, groups are used for grouping settings (cron run frequency) and it's not guaranteed that jobs from different groups can be run in parallel. As I mentioned before, I agree that it would be good to implement cron job dependencies, so some jobs could run in parallel. For now, jobs should run consequently, independently on the group.

Note: I represent core team, and discussed this question internally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@buskamuza I am not sure how to proceed with this PR after your comment. We can move locking mechanism outside "foreach" loop and have single global lock for cron runner, at least as a stop gap measure.

Yet I would like to see Magento being able to run at least some tasks parallel. Cron groups currently implement a toggle "run in a separate process". Would it be safe to assume those groups can run parallel to main process, otherwise what would be the purpose of this setting?

Copy link
Contributor

Choose a reason for hiding this comment

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

As cron already runs per groups, let's just leave your implementation of locking in scope of this PR.
In future, we may reconsider it and reimplement it based on job dependencies.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just for clarification: no further actions are needed here. Just review other comments in the PR. Thanks.

Copy link
Contributor Author

@paveq paveq Feb 21, 2018

Choose a reason for hiding this comment

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

After internal discussion with a development team that faced some issue with 3rd party module, we thought that maybe bit more strict path could be taken here. Implementation could be changed to use single global lock for all groups except for those that have "run in a separate process" enabled.

Thoughts on this? I would say it's safer way to go. However even my current solution is better than what we have now, as there's no limitation whatsoever.

$this->logger->warning(
sprintf(
"Could not acquire lock for cron group: %s, skipping run",
$groupId
)
);
continue;
}

$this->_cleanup($groupId);
$this->_generate($groupId);

/** @var \Magento\Cron\Model\Schedule $schedule */
foreach ($pendingJobs as $schedule) {
$jobConfig = isset($jobsRoot[$schedule->getJobCode()]) ? $jobsRoot[$schedule->getJobCode()] : null;
Expand Down Expand Up @@ -247,6 +278,8 @@ public function execute(\Magento\Framework\Event\Observer $observer)
}
$schedule->save();
}

$this->lockManager->releaseLock(self::LOCK_PREFIX . $groupId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class ProcessCronQueueObserverTest extends \PHPUnit\Framework\TestCase
*/
protected $appStateMock;

/**
* @var \Magento\Framework\Lock\LockManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $lockManagerMock;

/**
* @var \Magento\Cron\Model\ResourceModel\Schedule|\PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -135,6 +140,12 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();

$this->lockManagerMock = $this->getMockBuilder(\Magento\Framework\Lock\LockManagerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->lockManagerMock->method('acquireLock')->willReturn(true);
$this->lockManagerMock->method('releaseLock')->willReturn(true);

$this->observer = $this->createMock(\Magento\Framework\Event\Observer::class);

$this->dateTimeMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
Expand Down Expand Up @@ -170,7 +181,8 @@ protected function setUp()
$this->dateTimeMock,
$phpExecutableFinderFactory,
$this->loggerMock,
$this->appStateMock
$this->appStateMock,
$this->lockManagerMock
);
}

Expand Down Expand Up @@ -813,22 +825,22 @@ public function testMissedJobsCleanedInTime()
$this->_config->expects($this->exactly(2))->method('getJobs')->will($this->returnValue($jobConfig));

$this->_scopeConfig->expects($this->at(0))->method('getValue')
->with($this->equalTo('system/cron/test_group/use_separate_process'))
->will($this->returnValue(0));
$this->_scopeConfig->expects($this->at(1))->method('getValue')
->with($this->equalTo('system/cron/test_group/history_cleanup_every'))
->will($this->returnValue(10));
$this->_scopeConfig->expects($this->at(1))->method('getValue')
$this->_scopeConfig->expects($this->at(2))->method('getValue')
->with($this->equalTo('system/cron/test_group/schedule_lifetime'))
->will($this->returnValue(2*24*60));
$this->_scopeConfig->expects($this->at(2))->method('getValue')
->with($this->equalTo('system/cron/test_group/history_success_lifetime'))
->will($this->returnValue(0));
$this->_scopeConfig->expects($this->at(3))->method('getValue')
->with($this->equalTo('system/cron/test_group/history_failure_lifetime'))
->with($this->equalTo('system/cron/test_group/history_success_lifetime'))
->will($this->returnValue(0));
$this->_scopeConfig->expects($this->at(4))->method('getValue')
->with($this->equalTo('system/cron/test_group/schedule_generate_every'))
->with($this->equalTo('system/cron/test_group/history_failure_lifetime'))
->will($this->returnValue(0));
$this->_scopeConfig->expects($this->at(5))->method('getValue')
->with($this->equalTo('system/cron/test_group/use_separate_process'))
->with($this->equalTo('system/cron/test_group/schedule_generate_every'))
->will($this->returnValue(0));

$this->_collection->expects($this->any())->method('addFieldToFilter')->will($this->returnSelf());
Expand Down
1 change: 1 addition & 0 deletions app/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<preference for="Magento\Framework\Locale\ListsInterface" type="Magento\Framework\Locale\TranslatedLists" />
<preference for="Magento\Framework\Locale\AvailableLocalesInterface" type="Magento\Framework\Locale\Deployed\Codes" />
<preference for="Magento\Framework\Locale\OptionInterface" type="Magento\Framework\Locale\Deployed\Options" />
<preference for="Magento\Framework\Lock\LockManagerInterface" type="Magento\Framework\Lock\Backend\Database" />
<preference for="Magento\Framework\Api\AttributeTypeResolverInterface" type="Magento\Framework\Reflection\AttributeTypeResolver" />
<preference for="Magento\Framework\Api\Search\SearchResultInterface" type="Magento\Framework\Api\Search\SearchResult" />
<preference for="Magento\Framework\Api\Search\SearchCriteriaInterface" type="Magento\Framework\Api\Search\SearchCriteria"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/**
* \Magento\Framework\Lock\Backend\Database test case
*/
namespace Magento\Framework\Lock\Backend;

class DatabaseTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\Lock\Backend\Database
*/
private $model;

/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;

protected function setUp()
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->model = $this->objectManager->create(\Magento\Framework\Lock\Backend\Database::class);
}

public function testLockAndRelease()
{
$name = 'test_lock';

$this->assertFalse($this->model->isLocked($name));

$this->assertTrue($this->model->acquireLock($name));
$this->assertTrue($this->model->isLocked($name));

$this->assertTrue($this->model->releaseLock($name));
$this->assertFalse($this->model->isLocked($name));
}

public function testReleaseLockWithoutExistingLock()
{
$name = 'test_lock';

$this->assertFalse($this->model->isLocked($name));
$this->assertFalse($this->model->releaseLock($name));
}
}
83 changes: 83 additions & 0 deletions lib/internal/Magento/Framework/Lock/Backend/Database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);
namespace Magento\Framework\Lock\Backend;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Phrase;

class Database implements \Magento\Framework\Lock\LockManagerInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be great to cover this class with unit and integration tests. Could you add them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'll work on implementing some test cases for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test cases added.

{
/** @var ResourceConnection */
private $resource;

public function __construct(
ResourceConnection $resource
) {
$this->resource = $resource;
}

/**
* Sets a lock for name
*
* @param string $name lock name
* @param int $timeout How long to wait lock acquisition in seconds, negative value means infinite timeout
* @return bool
* @throws InputException
*/
public function acquireLock(string $name, int $timeout = -1): bool
{
$this->checkLength($name);

return (bool)$this->resource->getConnection()->query("SELECT GET_LOCK(?, ?);", [(string)$name, (int)$timeout])
->fetchColumn();
}

/**
* Releases a lock for name
*
* @param string $name lock name
* @return bool
* @throws InputException
*/
public function releaseLock(string $name): bool
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add @throws to phpdoc block there?

{
$this->checkLength($name);

return (bool)$this->resource->getConnection()->query("SELECT RELEASE_LOCK(?);", [(string)$name])->fetchColumn();
}

/**
* Tests of lock is set for name
*
* @param string $name lock name
* @return bool
* @throws InputException
*/
public function isLocked(string $name): bool
{
$this->checkLength($name);

return (bool)$this->resource->getConnection()->query("SELECT IS_USED_LOCK(?);", [(string)$name])->fetchColumn();
}

/**
* Checks for max length of lock name
*
* Limited to 64 characters in MySQL.
*
* @param string $name
* @throws InputException
*/
private function checkLength(string $name)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add phpdoc block there?

{
if (strlen($name) > 64) {
throw new InputException(new Phrase('Lock name too long'));
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add full name (including prefix) to the exception message, so it's easy to understand what name caused the ussed.

}
}
}
44 changes: 44 additions & 0 deletions lib/internal/Magento/Framework/Lock/LockManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);
namespace Magento\Framework\Lock;

/**
* Interface of a lock manager
*
* @api
*/
interface LockManagerInterface
{
/**
* Sets a lock
*
* @param string $name lock name
* @param int $timeout How long to wait lock acquisition in seconds, negative value means infinite timeout
* @return bool
* @api
*/
public function acquireLock(string $name, int $timeout = -1): bool;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe better to have lock($name), unlock($name), tryLock($name, $timeout) methods?

Copy link
Contributor Author

@paveq paveq Feb 21, 2018

Choose a reason for hiding this comment

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

I can change to lock and unlock if there's consensus these are better action verbs. However I would avoid tryLock as ambiguous term, for example in Java is has different meaning than just testing the lock status. However testLock could be one alternative?

EDIT: lock and unlock seem to be common terms in eg. Linux kernel and also Java, changing this implementation to conform to those wise spread terms.


/**
* Releases a lock
*
* @param string $name lock name
* @return bool
* @api
*/
public function releaseLock(string $name): bool;

/**
* Tests if lock is set
*
* @param string $name lock name
* @return bool
* @api
*/
public function isLocked(string $name): bool;
}
8 changes: 8 additions & 0 deletions lib/internal/Magento/Framework/Lock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Lock library

Lock library provides mechanism to acquire Magento system-wide lock. Default implementation is based on MySQL locks, where any locks are automatically released on connection close.

The library provides interface *LockManagerInterface* which provides following methods:
* *acquireLock* - Acquires a named lock
* *releaseLock* - Releases a named lock
* *isLocked* - Tests if a named lock exists
Loading