Skip to content

Commit e05e97d

Browse files
committed
ACP2E-1875: [Magento Cloud] Products incorrectly showed Out of Stock then all products show in stock
- stacked indexer actions
1 parent 9f2fa1e commit e05e97d

File tree

9 files changed

+134
-17
lines changed

9 files changed

+134
-17
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ public function __construct(
127127
array $data,
128128
ProcessManager $processManager = null,
129129
?int $batchSize = null,
130-
?DeploymentConfig $deploymentConfig = null
130+
?DeploymentConfig $deploymentConfig = null,
131+
?EnhancedIndexerHandler $enhancedIndexerHandler = null
131132
) {
132133
$this->fullAction = $fullActionFactory->create(['data' => $data]);
133134
$this->indexerHandlerFactory = $indexerHandlerFactory;
@@ -172,6 +173,12 @@ public function executeByDimensions(array $dimensions, \Traversable $entityIds =
172173
'data' => $this->data,
173174
]
174175
);
176+
$enhancedIndexerHandler = $this->indexerHandlerFactory->createSpecificHandler(
177+
[
178+
'data' => $this->data,
179+
],
180+
EnhancedIndexerHandler::class
181+
);
175182

176183
if (null === $entityIds) {
177184
$saveHandler->cleanIndex($dimensions);
@@ -191,13 +198,13 @@ public function executeByDimensions(array $dimensions, \Traversable $entityIds =
191198
foreach ($entityIds as $entityId) {
192199
$currentBatch[] = $entityId;
193200
if (++$i === $this->batchSize) {
194-
$this->processBatch($saveHandler, $dimensions, $currentBatch);
201+
$this->processBatch($enhancedIndexerHandler, $dimensions, $currentBatch);
195202
$i = 0;
196203
$currentBatch = [];
197204
}
198205
}
199206
if (!empty($currentBatch)) {
200-
$this->processBatch($saveHandler, $dimensions, $currentBatch);
207+
$this->processBatch($enhancedIndexerHandler, $dimensions, $currentBatch);
201208
}
202209
}
203210
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,35 @@ public function __construct(
5353
$this->engineResolver = $engineResolver;
5454
}
5555

56+
/**
57+
* Creates specific indexer handler
58+
*
59+
* @param array $data
60+
* @param string|null $handler
61+
* @return IndexerInterface
62+
*/
63+
public function createSpecificHandler(array $data = [], ?string $handler = null): IndexerInterface
64+
{
65+
if (!$handler) {
66+
return $this->create($data);
67+
}
68+
$indexer = $this->_objectManager->create($handler, $data);
69+
70+
$currentHandler = $this->engineResolver->getCurrentSearchEngine();
71+
if (!$indexer instanceof IndexerInterface) {
72+
throw new \InvalidArgumentException(
73+
$currentHandler . ' indexer handler doesn\'t implement ' . IndexerInterface::class
74+
);
75+
}
76+
77+
if ($indexer && !$indexer->isAvailable()) {
78+
throw new \LogicException(
79+
'Indexer handler is not available: ' . $currentHandler
80+
);
81+
}
82+
return $indexer;
83+
}
84+
5685
/**
5786
* Create indexer handler
5887
*

app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/FulltextTest.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\CatalogSearch\Model\Indexer\Fulltext;
1111
use Magento\CatalogSearch\Model\Indexer\Fulltext\Action\Full;
1212
use Magento\CatalogSearch\Model\Indexer\Fulltext\Action\FullFactory;
13+
use Magento\Elasticsearch\Model\Indexer\EnhancedIndexerHandler;
1314
use Magento\Framework\Indexer\SaveHandler\IndexerInterface;
1415
use Magento\CatalogSearch\Model\Indexer\IndexerHandlerFactory;
1516
use Magento\CatalogSearch\Model\Indexer\Scope\State;
@@ -41,6 +42,11 @@ class FulltextTest extends TestCase
4142
*/
4243
protected $saveHandler;
4344

45+
/**
46+
* @var IndexerInterface|MockObject
47+
*/
48+
protected $enhancedSaveHandler;
49+
4450
/**
4551
* @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext|MockObject
4652
*/
@@ -65,11 +71,13 @@ protected function setUp(): void
6571
);
6672
$fullActionFactory->expects($this->any())->method('create')->willReturn($this->fullAction);
6773
$this->saveHandler = $this->getClassMock(IndexerInterface::class);
74+
$this->enhancedSaveHandler = $this->getClassMock(EnhancedIndexerHandler::class);
6875
$indexerHandlerFactory = $this->createPartialMock(
6976
IndexerHandlerFactory::class,
70-
['create']
77+
['create', 'createSpecificHandler']
7178
);
7279
$indexerHandlerFactory->expects($this->any())->method('create')->willReturn($this->saveHandler);
80+
$indexerHandlerFactory->expects($this->any())->method('createSpecificHandler')->willReturn($this->enhancedSaveHandler);
7381

7482
$this->fulltextResource = $this->getClassMock(\Magento\CatalogSearch\Model\ResourceModel\Fulltext::class);
7583

@@ -116,9 +124,9 @@ public function testExecute()
116124
$this->fulltextResource->expects($this->exactly(2))
117125
->method('getRelationsByChild')
118126
->willReturn($ids);
119-
$this->saveHandler->expects($this->exactly(count($stores)))->method('deleteIndex');
120-
$this->saveHandler->expects($this->exactly(2))->method('saveIndex');
121-
$this->saveHandler->expects($this->exactly(2))->method('isAvailable')->willReturn(true);
127+
$this->enhancedSaveHandler->expects($this->exactly(count($stores)))->method('deleteIndex');
128+
$this->enhancedSaveHandler->expects($this->exactly(2))->method('saveIndex');
129+
$this->enhancedSaveHandler->expects($this->exactly(2))->method('isAvailable')->willReturn(true);
122130
$consecutiveStoreRebuildArguments = array_map(
123131
function ($store) use ($ids) {
124132
return [$store, $ids];
@@ -187,9 +195,9 @@ public function testExecuteList()
187195
$this->fulltextResource->expects($this->exactly(2))
188196
->method('getRelationsByChild')
189197
->willReturn($ids);
190-
$this->saveHandler->expects($this->exactly(count($stores)))->method('deleteIndex');
191-
$this->saveHandler->expects($this->exactly(2))->method('saveIndex');
192-
$this->saveHandler->expects($this->exactly(2))->method('isAvailable')->willReturn(true);
198+
$this->enhancedSaveHandler->expects($this->exactly(count($stores)))->method('deleteIndex');
199+
$this->enhancedSaveHandler->expects($this->exactly(2))->method('saveIndex');
200+
$this->enhancedSaveHandler->expects($this->exactly(2))->method('isAvailable')->willReturn(true);
193201
$this->fullAction->expects($this->exactly(2))
194202
->method('rebuildStoreIndex')
195203
->willReturn(new \ArrayObject([$indexData, $indexData]));
@@ -206,9 +214,9 @@ public function testExecuteRow()
206214
$this->fulltextResource->expects($this->exactly(2))
207215
->method('getRelationsByChild')
208216
->willReturn([$id]);
209-
$this->saveHandler->expects($this->exactly(count($stores)))->method('deleteIndex');
210-
$this->saveHandler->expects($this->exactly(2))->method('saveIndex');
211-
$this->saveHandler->expects($this->exactly(2))->method('isAvailable')->willReturn(true);
217+
$this->enhancedSaveHandler->expects($this->exactly(count($stores)))->method('deleteIndex');
218+
$this->enhancedSaveHandler->expects($this->exactly(2))->method('saveIndex');
219+
$this->enhancedSaveHandler->expects($this->exactly(2))->method('isAvailable')->willReturn(true);
212220
$this->fullAction->expects($this->exactly(2))
213221
->method('rebuildStoreIndex')
214222
->willReturn(new \ArrayObject([$indexData, $indexData]));

app/code/Magento/Elasticsearch/Model/Adapter/Elasticsearch.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ public function __construct(
193193
}
194194

195195
/**
196+
* Disable query stacking
197+
*
196198
* @return void
197199
*/
198200
public function disableStackQueriesMode(): void
@@ -202,6 +204,8 @@ public function disableStackQueriesMode(): void
202204
}
203205

204206
/**
207+
* Enable query stacking
208+
*
205209
* @return void
206210
*/
207211
public function enableStackQueriesMode(): void
@@ -210,6 +214,8 @@ public function enableStackQueriesMode(): void
210214
}
211215

212216
/**
217+
* Run the stacked queries
218+
*
213219
* @return $this
214220
* @throws Exception
215221
*/
@@ -228,6 +234,8 @@ public function triggerStackedQueries(): self
228234
}
229235

230236
/**
237+
* Combine query body request
238+
*
231239
* @param array $queries
232240
* @return void
233241
* @throws LocalizedException

app/code/Magento/Elasticsearch/Model/Indexer/EnhancedIndexerHandler.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,69 @@
11
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
27

38
namespace Magento\Elasticsearch\Model\Indexer;
49

10+
use Magento\CatalogSearch\Model\Indexer\Fulltext\Processor;
11+
use Magento\Elasticsearch\Model\Adapter\Elasticsearch as ElasticsearchAdapter;
12+
use Magento\Elasticsearch\Model\Adapter\Index\IndexNameResolver;
13+
use Magento\Framework\App\DeploymentConfig;
14+
use Magento\Framework\App\ScopeResolverInterface;
15+
use Magento\Framework\Indexer\CacheContext;
16+
use Magento\Framework\Indexer\IndexStructureInterface;
17+
use Magento\Framework\Indexer\SaveHandler\Batch;
18+
519
class EnhancedIndexerHandler extends IndexerHandler
620
{
21+
/**
22+
* @var ElasticsearchAdapter
23+
*/
24+
private ElasticsearchAdapter $adapter;
25+
26+
/**
27+
* IndexerHandler constructor.
28+
* @param IndexStructureInterface $indexStructure
29+
* @param ElasticsearchAdapter $adapter
30+
* @param IndexNameResolver $indexNameResolver
31+
* @param Batch $batch
32+
* @param ScopeResolverInterface $scopeResolver
33+
* @param array $data
34+
* @param int $batchSize
35+
* @param DeploymentConfig|null $deploymentConfig
36+
* @param CacheContext|null $cacheContext
37+
* @param Processor|null $processor
38+
*/
39+
public function __construct(
40+
IndexStructureInterface $indexStructure,
41+
ElasticsearchAdapter $adapter,
42+
IndexNameResolver $indexNameResolver,
43+
Batch $batch,
44+
ScopeResolverInterface $scopeResolver,
45+
array $data = [],
46+
int $batchSize = self::DEFAULT_BATCH_SIZE,
47+
?DeploymentConfig $deploymentConfig = null,
48+
?CacheContext $cacheContext = null,
49+
?Processor $processor = null
50+
) {
51+
$this->adapter = $adapter;
52+
53+
parent::__construct(
54+
$indexStructure,
55+
$this->adapter,
56+
$indexNameResolver,
57+
$batch,
58+
$scopeResolver,
59+
$data,
60+
$batchSize,
61+
$deploymentConfig,
62+
$cacheContext,
63+
$processor
64+
);
65+
}
66+
767
/**
868
* Disables stacked actions mode
969
*

app/code/Magento/Elasticsearch/Model/Indexer/IndexerHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class IndexerHandler implements IndexerInterface
3737
/**
3838
* @var ElasticsearchAdapter
3939
*/
40-
protected $adapter;
40+
private $adapter;
4141

4242
/**
4343
* @var IndexNameResolver

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@
205205
<type name="Magento\CatalogSearch\Model\Indexer\IndexerHandlerFactory">
206206
<arguments>
207207
<argument name="handlers" xsi:type="array">
208-
<item name="elasticsearch5" xsi:type="string">Magento\Elasticsearch\Model\Indexer\EnhancedIndexerHandler</item>
208+
<item name="elasticsearch5" xsi:type="string">Magento\Elasticsearch\Model\Indexer\IndexerHandler</item>
209209
</argument>
210210
</arguments>
211211
</type>
@@ -560,6 +560,11 @@
560560
<argument name="cacheContext" xsi:type="object">Magento\Framework\Indexer\CacheContext\Proxy</argument>
561561
</arguments>
562562
</type>
563+
<type name="Magento\Elasticsearch\Model\Indexer\EnhancedIndexerHandler">
564+
<arguments>
565+
<argument name="cacheContext" xsi:type="object">Magento\Framework\Indexer\CacheContext\Proxy</argument>
566+
</arguments>
567+
</type>
563568
<type name="Magento\Catalog\Model\Product\ReservedAttributeList">
564569
<arguments>
565570
<argument name="reservedAttributes" xsi:type="array">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<type name="Magento\CatalogSearch\Model\Indexer\IndexerHandlerFactory">
5252
<arguments>
5353
<argument name="handlers" xsi:type="array">
54-
<item name="elasticsearch7" xsi:type="string">Magento\Elasticsearch\Model\Indexer\EnhancedIndexerHandler</item>
54+
<item name="elasticsearch7" xsi:type="string">Magento\Elasticsearch\Model\Indexer\IndexerHandler</item>
5555
</argument>
5656
</arguments>
5757
</type>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
<type name="Magento\CatalogSearch\Model\Indexer\IndexerHandlerFactory">
9191
<arguments>
9292
<argument name="handlers" xsi:type="array">
93-
<item name="opensearch" xsi:type="string">Magento\Elasticsearch\Model\Indexer\EnhancedIndexerHandler</item>
93+
<item name="opensearch" xsi:type="string">Magento\Elasticsearch\Model\Indexer\IndexerHandler</item>
9494
</argument>
9595
</arguments>
9696
</type>

0 commit comments

Comments
 (0)