Skip to content

Commit

Permalink
Support some cases of blue/green deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
kandy committed Nov 18, 2021
1 parent 21e8e4e commit c241e11
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 205 deletions.
12 changes: 10 additions & 2 deletions app/code/Magento/Deploy/Model/Plugin/ConfigChangeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Magento\Deploy\Model\Plugin;

use Magento\Deploy\Model\DeploymentConfig\ChangeDetector;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\FrontControllerInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\LocalizedException;
Expand All @@ -20,19 +21,24 @@
*/
class ConfigChangeDetector
{
private const DEPLOYMENT_BLUE_GREEN_ENABLED = 'deployment/blue_green/enabled';

/**
* Configuration data changes detector.
*
* @var ChangeDetector
*/
private $changeDetector;

private DeploymentConfig $deploymentConfig;

/**
* @param ChangeDetector $changeDetector configuration data changes detector
*/
public function __construct(ChangeDetector $changeDetector)
public function __construct(ChangeDetector $changeDetector, DeploymentConfig $deploymentConfig)
{
$this->changeDetector = $changeDetector;
$this->deploymentConfig = $deploymentConfig;
}

/**
Expand All @@ -46,7 +52,9 @@ public function __construct(ChangeDetector $changeDetector)
*/
public function beforeDispatch(FrontControllerInterface $subject, RequestInterface $request)
{
if ($this->changeDetector->hasChanges()) {
if (!$this->deploymentConfig->get(self::DEPLOYMENT_BLUE_GREEN_ENABLED)
&& $this->changeDetector->hasChanges()
) {
throw new LocalizedException(
__(
'The configuration file has changed.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Deploy\Model\DeploymentConfig\ChangeDetector;
use Magento\Deploy\Model\Plugin\ConfigChangeDetector;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\FrontControllerInterface;
use Magento\Framework\App\RequestInterface;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -36,6 +37,11 @@ class ConfigChangeDetectorTest extends TestCase
*/
private $requestMock;

/**
* @var DeploymentConfig|mixed|MockObject
*/
private $deploymentConfig;

/**
* @return void
*/
Expand All @@ -48,8 +54,14 @@ protected function setUp(): void
->getMockForAbstractClass();
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
->getMockForAbstractClass();
$this->deploymentConfig =$this->getMockBuilder(DeploymentConfig::class)
->disableOriginalConstructor()
->getMock();

$this->configChangeDetectorPlugin = new ConfigChangeDetector($this->changeDetectorMock);
$this->configChangeDetectorPlugin = new ConfigChangeDetector(
$this->changeDetectorMock,
$this->deploymentConfig
);
}

/**
Expand All @@ -65,8 +77,6 @@ public function testBeforeDispatchWithoutException()

/**
* @return void
* @codingStandardsIgnoreStart
* @codingStandardsIgnoreEnd
*/
public function testBeforeDispatchWithException()
{
Expand All @@ -80,4 +90,13 @@ public function testBeforeDispatchWithException()
->willReturn(true);
$this->configChangeDetectorPlugin->beforeDispatch($this->frontControllerMock, $this->requestMock);
}

public function testBeforeDispatchWithBlueGreen()
{
$this->deploymentConfig->expects($this->atLeastOnce())
->method('get')
->with('deployment/blue_green/enabled')
->willReturn(1);
$this->configChangeDetectorPlugin->beforeDispatch($this->frontControllerMock, $this->requestMock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class RemoteSynchronizedCache extends \Zend_Cache_Backend implements \Zend_Cache
'local_backend_custom_naming' => true,
'local_backend_autoload' => true,
'use_stale_cache' => false,
'cleanup_percentage' => 95,
];

/**
Expand Down Expand Up @@ -264,7 +265,7 @@ public function save($data, $id, $tags = [], $specificLifetime = false)
*/
private function checkIfLocalCacheSpaceExceeded()
{
return $this->getFillingPercentage() >= 95;
return $this->getFillingPercentage() >= ($this->_options['cleanup_percentage'] ?? 95);
}

/**
Expand Down
18 changes: 16 additions & 2 deletions lib/internal/Magento/Framework/Module/Plugin/DbStatusValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Framework\Module\Plugin;

use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\Cache\FrontendInterface as FrontendCacheInterface;
use Magento\Framework\Module\DbVersionInfo;
use Magento\Framework\App\FrontController;
Expand All @@ -17,6 +18,8 @@
*/
class DbStatusValidator
{
private const DEPLOYMENT_BLUE_GREEN_ENABLED = 'deployment/blue_green/enabled';

/**
* @var FrontendCacheInterface
*/
Expand All @@ -27,14 +30,21 @@ class DbStatusValidator
*/
private $dbVersionInfo;

private DeploymentConfig $deploymentConfig;

/**
* @param FrontendCacheInterface $cache
* @param DbVersionInfo $dbVersionInfo
* @param DeploymentConfig $deploymentConfig
*/
public function __construct(FrontendCacheInterface $cache, DbVersionInfo $dbVersionInfo)
{
public function __construct(
FrontendCacheInterface $cache,
DbVersionInfo $dbVersionInfo,
DeploymentConfig $deploymentConfig
) {
$this->cache = $cache;
$this->dbVersionInfo = $dbVersionInfo;
$this->deploymentConfig = $deploymentConfig;
}

/**
Expand All @@ -49,6 +59,10 @@ public function __construct(FrontendCacheInterface $cache, DbVersionInfo $dbVers
*/
public function beforeDispatch(FrontController $subject, RequestInterface $request)
{
if ($this->deploymentConfig->get(self::DEPLOYMENT_BLUE_GREEN_ENABLED)) {
return;
}

if (!$this->cache->load('db_is_up_to_date')) {
list($versionTooLowErrors, $versionTooHighErrors) = array_values($this->getGroupedDbVersionErrors());
if ($versionTooHighErrors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\Framework\Module\Test\Unit\Plugin;

use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\FrontController;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Cache\FrontendInterface;
Expand Down Expand Up @@ -50,6 +51,11 @@ class DbStatusValidatorTest extends TestCase
*/
private $dbVersionInfoMock;

/**
* @var DeploymentConfig|mixed|MockObject
*/
private $deploymentConfig;

protected function setUp(): void
{
$this->_cacheMock = $this->getMockBuilder(FrontendInterface::class)
Expand All @@ -67,9 +73,15 @@ protected function setUp(): void
->disableOriginalConstructor()
->getMock();
$this->dbVersionInfoMock = $this->createMock(DbVersionInfo::class);

$this->deploymentConfig =$this->getMockBuilder(DeploymentConfig::class)
->disableOriginalConstructor()
->getMock();

$this->_model = new DbStatusValidator(
$this->_cacheMock,
$this->dbVersionInfoMock
$this->dbVersionInfoMock,
$this->deploymentConfig
);
}

Expand Down Expand Up @@ -188,4 +200,20 @@ public function aroundDispatchExceptionDataProvider()
],
];
}

public function testAroundDispatchBlueGreen()
{
$this->deploymentConfig->expects($this->atLeastOnce())
->method('get')
->with('deployment/blue_green/enabled')
->willReturn(1);

$this->_cacheMock->expects($this->never())
->method('load');

$this->dbVersionInfoMock->expects($this->never())
->method('getDbVersionErrors');

$this->_model->beforeDispatch($this->subjectMock, $this->requestMock);
}
}
Loading

0 comments on commit c241e11

Please sign in to comment.