-
Notifications
You must be signed in to change notification settings - Fork 17
MCLOUD-6023: Investigate Redis scan keys eviction #31
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
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
afe31d6
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality df6c550
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality c91a6c3
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality 8840f8c
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality 4227ea4
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality f46406a
Merge branch 'develop' into MCLOUD-6023
shiftedreality 1bc6da8
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality 0dec115
Merge remote-tracking branch 'origin/MCLOUD-6023' into MCLOUD-6023
shiftedreality c2dd5b2
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality da2bfdc
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality fd30afd
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality ccee8c5
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality 081d40d
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality 1b78a6c
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality 2fcf856
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality e951d50
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality 98b2239
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality f7b609d
MCLOUD-6023: Investigate Redis scan keys eviction
shiftedreality File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
| /** | ||
| * Copyright © Magento, Inc. All rights reserved. | ||
| * See COPYING.txt for license details. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Magento\CloudComponents\Console\Command; | ||
|
|
||
| use Magento\CloudComponents\Model\Cache\Evictor; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| /** | ||
| * Performs force key eviction with a "scan" command. | ||
| */ | ||
| class CacheEvict extends Command | ||
| { | ||
| /** | ||
| * @var Evictor | ||
| */ | ||
| private $evictor; | ||
|
|
||
| /** | ||
| * @param Evictor $evictor | ||
| */ | ||
| public function __construct(Evictor $evictor) | ||
| { | ||
| $this->evictor = $evictor; | ||
|
|
||
| parent::__construct('cache:evict'); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| protected function configure() | ||
| { | ||
| $this->setDescription('Evicts unused keys by performing scan command'); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function execute(InputInterface $input, OutputInterface $output) | ||
| { | ||
| $output->writeln('Begin scanning of cache keys'); | ||
|
|
||
| $count = $this->evictor->evict(); | ||
|
|
||
| $output->writeln(sprintf( | ||
| 'Total scanned keys: %s', | ||
| $count | ||
| )); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <?php | ||
| /** | ||
| * Copyright © Magento, Inc. All rights reserved. | ||
| * See COPYING.txt for license details. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Magento\CloudComponents\Cron; | ||
|
|
||
| use Magento\CloudComponents\Model\Cache\Evictor; | ||
| use Magento\Framework\App\DeploymentConfig; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| /** | ||
| * The cron cprocess to evict keys. | ||
| */ | ||
| class Evict | ||
| { | ||
| /** | ||
| * @var Evictor | ||
| */ | ||
| private $evictor; | ||
|
|
||
| /** | ||
| * @var DeploymentConfig | ||
| */ | ||
| private $deploymentConfig; | ||
|
|
||
| /** | ||
| * @var LoggerInterface | ||
| */ | ||
| private $logger; | ||
|
|
||
| /** | ||
| * @param Evictor $evictor | ||
| * @param DeploymentConfig $deploymentConfig | ||
| * @param LoggerInterface $logger | ||
| */ | ||
| public function __construct(Evictor $evictor, DeploymentConfig $deploymentConfig, LoggerInterface $logger) | ||
| { | ||
| $this->evictor = $evictor; | ||
| $this->deploymentConfig = $deploymentConfig; | ||
| $this->logger = $logger; | ||
| } | ||
|
|
||
| /** | ||
| * Perform keys eviction. | ||
| */ | ||
| public function execute() | ||
| { | ||
| if (!$this->deploymentConfig->get(Evictor::CONFIG_PATH_ENABLED)) { | ||
| $this->logger->info('Keys eviction is disabled'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $this->evictor->evict(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| <?php | ||
| /** | ||
| * Copyright © Magento, Inc. All rights reserved. | ||
| * See COPYING.txt for license details. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Magento\CloudComponents\Model\Cache; | ||
|
|
||
| use Magento\Framework\App\Cache\Type\FrontendPool; | ||
| use Magento\Framework\App\DeploymentConfig; | ||
| use Psr\Log\LoggerInterface; | ||
| use Credis_Client as Client; | ||
| use Cm_Cache_Backend_Redis as Backend; | ||
|
|
||
| /** | ||
| * Performs force key eviction with a "scan" command. | ||
| */ | ||
| class Evictor | ||
| { | ||
| const DEFAULT_EVICTION_LIMIT = 10000; | ||
| const DEFAULT_SLEEP_TIMEOUT = 20000; | ||
| const CONFIG_PATH_ENABLED = 'cache_evict/enabled'; | ||
| const CONFIG_PATH_LIMIT = 'cache_evict/limit'; | ||
|
|
||
| /** | ||
| * @var DeploymentConfig | ||
| */ | ||
| private $deploymentConfig; | ||
|
|
||
| /** | ||
| * @var LoggerInterface | ||
| */ | ||
| private $logger; | ||
|
|
||
| /** | ||
| * @param DeploymentConfig $deploymentConfig | ||
| * @param LoggerInterface $logger | ||
| */ | ||
| public function __construct(DeploymentConfig $deploymentConfig, LoggerInterface $logger) | ||
| { | ||
| $this->deploymentConfig = $deploymentConfig; | ||
| $this->logger = $logger; | ||
| } | ||
|
|
||
| /** | ||
| * Evicts all keys using iterator. | ||
| * | ||
| * @return int | ||
| */ | ||
| public function evict(): int | ||
| { | ||
| $options = $this->deploymentConfig->getConfigData(FrontendPool::KEY_CACHE)[FrontendPool::KEY_FRONTEND_CACHE] | ||
| ?? []; | ||
| $evictedKeys = 0; | ||
|
|
||
| foreach ($options as $name => $cacheConfig) { | ||
| $this->logger->info(sprintf( | ||
| 'Scanning keys for "%s" database', | ||
| $name | ||
| )); | ||
|
|
||
| if (!isset( | ||
| $cacheConfig['backend_options']['server'], | ||
| $cacheConfig['backend_options']['port'], | ||
| $cacheConfig['backend_options']['database'] | ||
| )) { | ||
| $this->logger->debug(sprintf( | ||
| 'Cache config for database "%s" config is not valid', | ||
| $name | ||
| )); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| $dbKeys = $this->run( | ||
| $cacheConfig['backend_options']['server'], | ||
| $cacheConfig['backend_options']['port'], | ||
| $cacheConfig['backend_options']['database'] | ||
| ); | ||
| $evictedKeys += $dbKeys; | ||
|
|
||
| $this->logger->info(sprintf('Keys scanned: %s', $dbKeys)); | ||
| } | ||
|
|
||
| return $evictedKeys; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $host | ||
| * @param int $port | ||
| * @param int $db | ||
| * @return int | ||
| */ | ||
| private function run(string $host, int $port, int $db): int | ||
| { | ||
| $client = new Client($host, $port, null, '', $db); | ||
| $evictedKeys = 0; | ||
|
|
||
| do { | ||
| $keys = $client->scan( | ||
| $iterator, | ||
| Backend::PREFIX_KEY . '*', | ||
| (int)$this->deploymentConfig->get(self::CONFIG_PATH_LIMIT, self::DEFAULT_EVICTION_LIMIT) | ||
| ); | ||
|
|
||
| if ($keys === false) { | ||
| $this->logger->debug('Reached end'); | ||
| } else { | ||
| $keysCount = count($keys); | ||
| $evictedKeys += $keysCount; | ||
| } | ||
kandy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /* Give Redis some time to handle other requests */ | ||
| usleep(self::DEFAULT_SLEEP_TIMEOUT); | ||
| } while ($iterator > 0); | ||
|
|
||
| return $evictedKeys; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?xml version="1.0"?> | ||
| <!-- | ||
| /** | ||
| * Copyright © Magento, Inc. All rights reserved. | ||
| * See COPYING.txt for license details. | ||
| */ | ||
| --> | ||
| <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd"> | ||
| <group id="cache_evict"> | ||
| <job name="cache_evict_keys" instance="Magento\CloudComponents\Cron\Evict" method="execute"> | ||
| <schedule>0 */12 * * *</schedule> | ||
| </job> | ||
| </group> | ||
| </config> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like you should check if
$cacheConfigis an array and contains required keys?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added