Skip to content

Commit 813d575

Browse files
committed
MAGETWO-80789: [Indexer] Search Indexer is scoped & multi-threaded
1 parent 3ac38ee commit 813d575

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

app/code/Magento/CatalogSearch/Model/Indexer/Fulltext.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@ class Fulltext implements \Magento\Framework\Indexer\ActionInterface, \Magento\F
7777
*/
7878
private $processManager;
7979

80-
/**
81-
* @var int
82-
*/
83-
private $threadsCount;
84-
8580
/**
8681
* @param FullFactory $fullActionFactory
8782
* @param IndexerHandlerFactory $indexerHandlerFactory
@@ -93,7 +88,6 @@ class Fulltext implements \Magento\Framework\Indexer\ActionInterface, \Magento\F
9388
* @param IndexSwitcherInterface $indexSwitcher
9489
* @param Scope\State $indexScopeState
9590
* @param ProcessManager $processManager
96-
* @param int $threadsCount
9791
*/
9892
public function __construct(
9993
FullFactory $fullActionFactory,
@@ -105,8 +99,7 @@ public function __construct(
10599
array $data,
106100
IndexSwitcherInterface $indexSwitcher = null,
107101
State $indexScopeState = null,
108-
ProcessManager $processManager = null,
109-
int $threadsCount = 4 //Will be moved to the env.php
102+
ProcessManager $processManager = null
110103
) {
111104
$this->fullAction = $fullActionFactory->create(['data' => $data]);
112105
$this->indexerHandlerFactory = $indexerHandlerFactory;
@@ -127,7 +120,6 @@ public function __construct(
127120
$this->indexSwitcher = $indexSwitcher;
128121
$this->indexScopeState = $indexScopeState;
129122
$this->processManager = $processManager;
130-
$this->threadsCount = $threadsCount;
131123
}
132124

133125
/**
@@ -169,7 +161,7 @@ public function executeFull()
169161

170162
}
171163

172-
$this->processManager->execute($userFunctions, $this->threadsCount);
164+
$this->processManager->execute($userFunctions);
173165

174166
$this->fulltextResource->resetSearchResults();
175167
$this->searchRequestConfig->reset();

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,46 @@
1010
*/
1111
class ProcessManager
1212
{
13+
/**
14+
* Threads count environment variable name
15+
*/
16+
const THREADS_COUNT = 'MAGE_INDEXER_THREADS_COUNT';
17+
1318
/** @var bool */
1419
private $failInChildProcess = false;
1520

1621
/** @var \Magento\Framework\App\ResourceConnection */
1722
private $resource;
1823

24+
/** @var int|null */
25+
private $threadsCount;
26+
1927
/**
2028
* @param \Magento\Framework\App\ResourceConnection $resource
29+
* @param int|null $threadsCount
2130
*/
2231
public function __construct(
23-
\Magento\Framework\App\ResourceConnection $resource = null
32+
\Magento\Framework\App\ResourceConnection $resource = null,
33+
int $threadsCount = null
2434
) {
2535
if (null === $resource) {
2636
$resource = \Magento\Framework\App\ObjectManager::getInstance()->get(
2737
\Magento\Framework\App\ResourceConnection::class
2838
);
2939
}
3040
$this->resource = $resource;
41+
$this->threadsCount = (int)$threadsCount;
3142
}
3243

3344
/**
3445
* Execute user functions
3546
*
3647
* @param \Traversable $userFunctions
37-
* @param int $threadsCount
3848
*/
39-
public function execute($userFunctions, $threadsCount)
49+
public function execute($userFunctions)
4050
{
41-
if ($threadsCount > 1 && $this->isCanBeParalleled()) {
42-
$this->multiThreadsExecute($userFunctions, $threadsCount);
51+
if ($this->threadsCount > 1 && $this->isCanBeParalleled()) {
52+
$this->multiThreadsExecute($userFunctions);
4353
} else {
4454
$this->simpleThreadExecute($userFunctions);
4555
}
@@ -61,9 +71,8 @@ private function simpleThreadExecute($userFunctions)
6171
* Execute user functions in in multiThreads mode
6272
*
6373
* @param \Traversable $userFunctions
64-
* @param int $threadsCount
6574
*/
66-
private function multiThreadsExecute($userFunctions, $threadsCount)
75+
private function multiThreadsExecute($userFunctions)
6776
{
6877
$this->resource->closeConnection(null);
6978
$threadNumber = 0;
@@ -72,7 +81,7 @@ private function multiThreadsExecute($userFunctions, $threadsCount)
7281
if ($pid == -1) {
7382
throw new \RuntimeException('Unable to fork a new process');
7483
} elseif ($pid) {
75-
$this->executeParentProcess($threadNumber, $threadsCount);
84+
$this->executeParentProcess($threadNumber);
7685
} else {
7786
$this->startChildProcess($userFunction);
7887
}
@@ -111,12 +120,11 @@ private function startChildProcess($userFunction)
111120
* Execute parent process
112121
*
113122
* @param int $threadNumber
114-
* @param int $threadsCount
115123
*/
116-
private function executeParentProcess(&$threadNumber, $threadsCount)
124+
private function executeParentProcess(&$threadNumber)
117125
{
118126
$threadNumber++;
119-
if ($threadNumber >= $threadsCount) {
127+
if ($threadNumber >= $this->threadsCount) {
120128
pcntl_wait($status);
121129
if (pcntl_wexitstatus($status) !== 0) {
122130
$this->failInChildProcess = true;

app/code/Magento/Indexer/etc/di.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@
4242
<plugin name="page-cache-indexer-reindex-clean-cache"
4343
type="Magento\Indexer\Model\Processor\CleanCache" sortOrder="10"/>
4444
</type>
45-
45+
<type name="\Magento\Indexer\Model\ProcessManager">
46+
<arguments>
47+
<argument name="threadsCount" xsi:type="init_parameter">Magento\Indexer\Model\ProcessManager::THREADS_COUNT</argument>
48+
</arguments>
49+
</type>
4650
<type name="Magento\Framework\Console\CommandListInterface">
4751
<arguments>
4852
<argument name="commands" xsi:type="array">

0 commit comments

Comments
 (0)