Skip to content

Commit dfcf068

Browse files
committed
MAGETWO-93319: Parallelization update
1 parent 79c6bbc commit dfcf068

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

app/code/Magento/Indexer/Model/ProcessManager.php

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,47 @@ class ProcessManager
1515
*/
1616
const THREADS_COUNT = 'MAGE_INDEXER_THREADS_COUNT';
1717

18+
/**
19+
* Configuration path for threads count
20+
*/
21+
const THREADS_COUNT_CONFIG_PATH = 'indexer/threads_count';
22+
1823
/** @var bool */
1924
private $failInChildProcess = false;
2025

2126
/** @var \Magento\Framework\App\ResourceConnection */
2227
private $resource;
2328

29+
/** @var \Magento\Framework\App\DeploymentConfig */
30+
private $deploymentConfig;
31+
32+
/** @var \Magento\Framework\Registry */
33+
private $registry;
34+
2435
/** @var int|null */
25-
private $threadsCount;
36+
private $threadsCountFromEnvVariable;
2637

2738
/**
2839
* @param \Magento\Framework\App\ResourceConnection $resource
40+
* @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
41+
* @param \Magento\Framework\Registry $registry
2942
* @param int|null $threadsCount
3043
*/
3144
public function __construct(
32-
\Magento\Framework\App\ResourceConnection $resource = null,
45+
\Magento\Framework\App\ResourceConnection $resource,
46+
\Magento\Framework\App\DeploymentConfig $deploymentConfig,
47+
\Magento\Framework\Registry $registry = null,
3348
int $threadsCount = null
3449
) {
35-
if (null === $resource) {
36-
$resource = \Magento\Framework\App\ObjectManager::getInstance()->get(
37-
\Magento\Framework\App\ResourceConnection::class
50+
$this->resource = $resource;
51+
$this->deploymentConfig = $deploymentConfig;
52+
if (null === $registry) {
53+
$registry = \Magento\Framework\App\ObjectManager::getInstance()->get(
54+
\Magento\Framework\Registry::class
3855
);
3956
}
40-
$this->resource = $resource;
41-
$this->threadsCount = (int)$threadsCount;
57+
$this->registry = $registry;
58+
$this->threadsCountFromEnvVariable = $threadsCount;
4259
}
4360

4461
/**
@@ -48,7 +65,7 @@ public function __construct(
4865
*/
4966
public function execute($userFunctions)
5067
{
51-
if ($this->threadsCount > 1 && $this->isCanBeParalleled()) {
68+
if ($this->getThreadsCount() > 1 && $this->isCanBeParalleled() && !$this->isSetupMode() && PHP_SAPI == 'cli') {
5269
$this->multiThreadsExecute($userFunctions);
5370
} else {
5471
$this->simpleThreadExecute($userFunctions);
@@ -106,6 +123,32 @@ private function isCanBeParalleled()
106123
return function_exists('pcntl_fork');
107124
}
108125

126+
/**
127+
* Get threads count
128+
*
129+
* @return bool
130+
*/
131+
private function getThreadsCount()
132+
{
133+
if ($this->threadsCountFromEnvVariable !== null) {
134+
return (int)$this->threadsCountFromEnvVariable;
135+
}
136+
if ($this->deploymentConfig->get(self::THREADS_COUNT_CONFIG_PATH) !== null) {
137+
return (int)$this->deploymentConfig->get(self::THREADS_COUNT_CONFIG_PATH);
138+
}
139+
return 1;
140+
}
141+
142+
/**
143+
* Is setup mode
144+
*
145+
* @return bool
146+
*/
147+
private function isSetupMode()
148+
{
149+
return $this->registry->registry('setup-mode-enabled') ?: false;
150+
}
151+
109152
/**
110153
* Start child process
111154
*
@@ -127,7 +170,7 @@ private function startChildProcess($userFunction)
127170
private function executeParentProcess(&$threadNumber)
128171
{
129172
$threadNumber++;
130-
if ($threadNumber >= $this->threadsCount) {
173+
if ($threadNumber >= $this->getThreadsCount()) {
131174
pcntl_wait($status);
132175
if (pcntl_wexitstatus($status) !== 0) {
133176
$this->failInChildProcess = true;

setup/src/Magento/Setup/Model/Installer.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,16 +755,24 @@ private function setupFlagTable(
755755
* Installs DB schema
756756
*
757757
* @return void
758+
* @throws \Exception
758759
*/
759760
public function installSchema()
760761
{
762+
/** @var \Magento\Framework\Registry $registry */
763+
$registry = $this->objectManagerProvider->get()->get(\Magento\Framework\Registry::class);
764+
//For backward compatibility in install and upgrade scripts with enabled parallelization.
765+
$registry->register('setup-mode-enabled', true);
766+
761767
$this->assertDbConfigExists();
762768
$this->assertDbAccessible();
763769
$setup = $this->setupFactory->create();
764770
$this->setupModuleRegistry($setup);
765771
$this->setupCoreTables($setup);
766772
$this->log->log('Schema creation/updates:');
767773
$this->handleDBSchemaData($setup, 'schema');
774+
775+
$registry->unregister('setup-mode-enabled');
768776
}
769777

770778
/**
@@ -775,12 +783,19 @@ public function installSchema()
775783
*/
776784
public function installDataFixtures()
777785
{
786+
/** @var \Magento\Framework\Registry $registry */
787+
$registry = $this->objectManagerProvider->get()->get(\Magento\Framework\Registry::class);
788+
//For backward compatibility in install and upgrade scripts with enabled parallelization.
789+
$registry->register('setup-mode-enabled', true);
790+
778791
$this->assertDbConfigExists();
779792
$this->assertDbAccessible();
780793
$setup = $this->dataSetupFactory->create();
781794
$this->checkFilePermissionsForDbUpgrade();
782795
$this->log->log('Data install/update:');
783796
$this->handleDBSchemaData($setup, 'data');
797+
798+
$registry->unregister('setup-mode-enabled');
784799
}
785800

786801
/**

0 commit comments

Comments
 (0)