Skip to content

Magento#25669: fixed issue "health_check.php fails if any database cache engine configured" #25722

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 4 commits into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 33 additions & 11 deletions lib/internal/Magento/Framework/Cache/Backend/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Database extends \Zend_Cache_Backend implements \Zend_Cache_Backend_Extend
* Constructor
*
* @param array $options associative array of options
* @throws \Zend_Cache_Exception
*/
public function __construct($options = [])
{
Expand All @@ -82,6 +83,7 @@ public function __construct($options = [])
* Get DB adapter
*
* @return \Magento\Framework\DB\Adapter\AdapterInterface
* @throws \Zend_Cache_Exception
*/
protected function _getConnection()
{
Expand All @@ -106,6 +108,7 @@ protected function _getConnection()
* Get table name where data is stored
*
* @return string
* @throws \Zend_Cache_Exception
*/
protected function _getDataTable()
{
Expand All @@ -122,6 +125,7 @@ protected function _getDataTable()
* Get table name where tags are stored
*
* @return string
* @throws \Zend_Cache_Exception
*/
protected function _getTagsTable()
{
Expand All @@ -139,9 +143,10 @@ protected function _getTagsTable()
*
* Note : return value is always "string" (unserialization is done by the core not by the backend)
*
* @param string $id Cache id
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
* @param string $id Cache id
* @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
* @return string|false cached datas
* @throws \Zend_Cache_Exception
*/
public function load($id, $doNotTestCacheValidity = false)
{
Expand All @@ -166,8 +171,9 @@ public function load($id, $doNotTestCacheValidity = false)
/**
* Test if a cache is available or not (for the given id)
*
* @param string $id cache id
* @param string $id cache id
* @return mixed|false (a cache is not available) or "last modified" timestamp (int) of the available cache record
* @throws \Zend_Cache_Exception
*/
public function test($id)
{
Expand Down Expand Up @@ -196,11 +202,13 @@ public function test($id)
* Note : $data is always "string" (serialization is done by the
* core not by the backend)
*
* @param string $data Datas to cache
* @param string $id Cache id
* @param string[] $tags Array of strings, the cache record will be tagged by each string entry
* @param int|bool $specificLifetime Integer to set a specific lifetime or null for infinite lifetime
* @param string $data Datas to cache
* @param string $id Cache id
* @param string[] $tags Array of strings, the cache record will be tagged by each string entry
* @param int|bool $specificLifetime Integer to set a specific lifetime or null for infinite lifetime
* @return bool true if no problem
* @throws \Zend_Db_Statement_Exception
* @throws \Zend_Cache_Exception
*/
public function save($data, $id, $tags = [], $specificLifetime = false)
{
Expand Down Expand Up @@ -239,8 +247,9 @@ public function save($data, $id, $tags = [], $specificLifetime = false)
/**
* Remove a cache record
*
* @param string $id Cache id
* @return boolean True if no problem
* @param string $id Cache id
* @return int|boolean Number of affected rows or false on failure
* @throws \Zend_Cache_Exception
*/
public function remove($id)
{
Expand All @@ -266,9 +275,10 @@ public function remove($id)
* \Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
* ($tags can be an array of strings or a single string)
*
* @param string $mode Clean mode
* @param string[] $tags Array of tags
* @param string $mode Clean mode
* @param string[] $tags Array of tags
* @return boolean true if no problem
* @throws \Zend_Cache_Exception
*/
public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, $tags = [])
{
Expand Down Expand Up @@ -301,6 +311,7 @@ public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, $tags = [])
* Return an array of stored cache ids
*
* @return string[] array of stored cache ids (string)
* @throws \Zend_Cache_Exception
*/
public function getIds()
{
Expand All @@ -316,6 +327,7 @@ public function getIds()
* Return an array of stored tags
*
* @return string[] array of stored tags (string)
* @throws \Zend_Cache_Exception
*/
public function getTags()
{
Expand All @@ -330,6 +342,7 @@ public function getTags()
*
* @param string[] $tags array of tags
* @return string[] array of matching cache ids (string)
* @throws \Zend_Cache_Exception
*/
public function getIdsMatchingTags($tags = [])
{
Expand All @@ -356,6 +369,7 @@ public function getIdsMatchingTags($tags = [])
*
* @param string[] $tags array of tags
* @return string[] array of not matching cache ids (string)
* @throws \Zend_Cache_Exception
*/
public function getIdsNotMatchingTags($tags = [])
{
Expand All @@ -369,6 +383,7 @@ public function getIdsNotMatchingTags($tags = [])
*
* @param string[] $tags array of tags
* @return string[] array of any matching cache ids (string)
* @throws \Zend_Cache_Exception
*/
public function getIdsMatchingAnyTags($tags = [])
{
Expand Down Expand Up @@ -404,6 +419,7 @@ public function getFillingPercentage()
*
* @param string $id cache id
* @return array|false array of metadatas (false if the cache id is not found)
* @throws \Zend_Cache_Exception
*/
public function getMetadatas($id)
{
Expand All @@ -425,6 +441,7 @@ public function getMetadatas($id)
* @param string $id cache id
* @param int $extraLifetime
* @return boolean true if ok
* @throws \Zend_Cache_Exception
*/
public function touch($id, $extraLifetime)
{
Expand Down Expand Up @@ -471,6 +488,7 @@ public function getCapabilities()
* @param string $id
* @param string[] $tags
* @return bool
* @throws \Zend_Cache_Exception
*/
protected function _saveTags($id, $tags)
{
Expand Down Expand Up @@ -509,6 +527,8 @@ protected function _saveTags($id, $tags)
* @param string $mode
* @param string[] $tags
* @return bool
* @throws \Zend_Cache_Exception
* @throws \Zend_Db_Statement_Exception
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function _cleanByTags($mode, $tags)
Expand Down Expand Up @@ -558,6 +578,7 @@ protected function _cleanByTags($mode, $tags)
*
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
* @return bool
* @throws \Zend_Cache_Exception
*/
private function cleanAll(\Magento\Framework\DB\Adapter\AdapterInterface $connection)
{
Expand All @@ -575,6 +596,7 @@ private function cleanAll(\Magento\Framework\DB\Adapter\AdapterInterface $connec
*
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
* @return bool
* @throws \Zend_Cache_Exception
*/
private function cleanOld(\Magento\Framework\DB\Adapter\AdapterInterface $connection)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/Magento/Framework/Lock/Backend/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function lock(string $name, int $timeout = -1): bool
*/
public function unlock(string $name): bool
{
return $this->cache->remove($this->getIdentifier($name));
return (bool)$this->cache->remove($this->getIdentifier($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 I kindly ask you to cover this part using a simple unit test? Just mock a behavior that caused an error previously for $this->cache->remove($this->getIdentifier($name)) and make sure that there's no error after the proposed change.

Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello @rogyar
Tests have already created.
Thank you @engcom-Echo

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\Lock\Test\Unit\Backend;

use Magento\Framework\Cache\FrontendInterface;
use Magento\Framework\Lock\Backend\Cache;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class CacheTest extends TestCase
{
const LOCK_PREFIX = 'LOCKED_RECORD_INFO_';

/**
* @var FrontendInterface|MockObject
*/
private $frontendCacheMock;

/**
* @var Cache
*/
private $cache;

/**
* @inheritDoc
*/
public function setUp()
{
$this->frontendCacheMock = $this->createMock(FrontendInterface::class);

$objectManager = new ObjectManagerHelper($this);

$this->cache = $objectManager->getObject(
Cache::class,
[
'cache' => $this->frontendCacheMock
]
);
}

/**
* Verify released a lock.
*
* @return void
*/
public function testUnlock(): void
{
$identifier = 'lock_name';

$this->frontendCacheMock
->expects($this->once())
->method('remove')
->with(self::LOCK_PREFIX . $identifier)
->willReturn(true);

$this->assertEquals(true, $this->cache->unlock($identifier));
}
}
4 changes: 3 additions & 1 deletion pub/health_check.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@
}
$cacheBackendClass = $cacheConfig[ConfigOptionsListConstants::CONFIG_PATH_BACKEND];
try {
/** @var \Magento\Framework\App\Cache\Frontend\Factory $cacheFrontendFactory */
$cacheFrontendFactory = $objectManager->get(Magento\Framework\App\Cache\Frontend\Factory::class);
/** @var \Zend_Cache_Backend_Interface $backend */
$backend = new $cacheBackendClass($cacheConfig[ConfigOptionsListConstants::CONFIG_PATH_BACKEND_OPTIONS]);
$backend = $cacheFrontendFactory->create($cacheConfig);
$backend->test('test_cache_id');
} catch (\Exception $e) {
http_response_code(500);
Expand Down