Skip to content

Commit db2297b

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

File tree

8 files changed

+131
-16
lines changed

8 files changed

+131
-16
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\CatalogSearch\Model\Indexer\Scope\State;
1111
use Magento\CatalogSearch\Model\Indexer\Scope\StateFactory;
1212
use Magento\CatalogSearch\Model\ResourceModel\Fulltext as FulltextResource;
13+
use Magento\Elasticsearch\Model\Indexer\EnhancedIndexerHandler;
1314
use Magento\Framework\App\ObjectManager;
1415
use Magento\Framework\Indexer\DimensionProviderInterface;
1516
use Magento\Framework\Indexer\SaveHandler\IndexerInterface;
@@ -40,6 +41,13 @@ class Fulltext implements
4041
*/
4142
private const BATCH_SIZE = 1000;
4243

44+
/**
45+
* Deployment config path
46+
*
47+
* @var string
48+
*/
49+
private const DEPLOYMENT_CONFIG_INDEXER_BATCHES = 'indexer/batch_size/';
50+
4351
/**
4452
* @var array index structure
4553
*/
@@ -94,13 +102,6 @@ class Fulltext implements
94102
*/
95103
private $deploymentConfig;
96104

97-
/**
98-
* Deployment config path
99-
*
100-
* @var string
101-
*/
102-
private const DEPLOYMENT_CONFIG_INDEXER_BATCHES = 'indexer/batch_size/';
103-
104105
/**
105106
* @param FullFactory $fullActionFactory
106107
* @param IndexerHandlerFactory $indexerHandlerFactory
@@ -112,6 +113,7 @@ class Fulltext implements
112113
* @param ProcessManager|null $processManager
113114
* @param int|null $batchSize
114115
* @param DeploymentConfig|null $deploymentConfig
116+
* @param EnhancedIndexerHandler|null $enhancedIndexerHandler
115117
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
116118
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
117119
*/
@@ -156,7 +158,7 @@ public function execute($entityIds)
156158
/**
157159
* @inheritdoc
158160
*
159-
* @throws \InvalidArgumentException
161+
* @throws \InvalidArgumentException|\Exception
160162
* @since 101.0.0
161163
*/
162164
public function executeByDimensions(array $dimensions, \Traversable $entityIds = null)
@@ -203,12 +205,13 @@ public function executeByDimensions(array $dimensions, \Traversable $entityIds =
203205
/**
204206
* Process batch
205207
*
206-
* @param IndexerInterface $saveHandler
208+
* @param EnhancedIndexerHandler $saveHandler
207209
* @param array $dimensions
208210
* @param array $entityIds
211+
* @throws \Exception
209212
*/
210213
private function processBatch(
211-
IndexerInterface $saveHandler,
214+
EnhancedIndexerHandler $saveHandler,
212215
array $dimensions,
213216
array $entityIds
214217
) : void {
@@ -217,8 +220,11 @@ private function processBatch(
217220
array_merge($entityIds, $this->fulltextResource->getRelationsByChild($entityIds))
218221
);
219222
if ($saveHandler->isAvailable($dimensions)) {
223+
$saveHandler->enableStackedActions();
220224
$saveHandler->deleteIndex($dimensions, new \ArrayIterator($productIds));
221225
$saveHandler->saveIndex($dimensions, $this->fullAction->rebuildStoreIndex($storeId, $productIds));
226+
$saveHandler->triggerStackedActions();
227+
$saveHandler->disableStackedActions();
222228
}
223229
}
224230

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

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ class Elasticsearch
124124
'elasticsearchMissing404' => Missing404Exception::class
125125
];
126126

127+
/**
128+
* @var bool
129+
*/
130+
private bool $isStackQueries = false;
131+
132+
/**
133+
* @var array
134+
*/
135+
private array $stackedQueries = [];
136+
127137
/**
128138
* @param ConnectionManager $connectionManager
129139
* @param FieldMapperInterface $fieldMapper
@@ -182,6 +192,59 @@ public function __construct(
182192
}
183193
}
184194

195+
/**
196+
* @return void
197+
*/
198+
public function disableStackQueriesMode(): void
199+
{
200+
$this->stackedQueries = [];
201+
$this->isStackQueries = false;
202+
}
203+
204+
/**
205+
* @return void
206+
*/
207+
public function enableStackQueriesMode(): void
208+
{
209+
$this->isStackQueries = true;
210+
}
211+
212+
/**
213+
* @return $this
214+
* @throws Exception
215+
*/
216+
public function triggerStackedQueries(): self
217+
{
218+
try {
219+
if (!empty($this->stackedQueries)) {
220+
$this->client->bulkQuery($this->stackedQueries);
221+
}
222+
} catch (Exception $e) {
223+
$this->logger->critical($e);
224+
throw $e;
225+
}
226+
227+
return $this;
228+
}
229+
230+
/**
231+
* @param array $queries
232+
* @return void
233+
* @throws LocalizedException
234+
*/
235+
protected function stackQueries(array $queries): void
236+
{
237+
if ($this->isStackQueries) {
238+
if (empty($this->stackedQueries)) {
239+
$this->stackedQueries = $queries;
240+
} else {
241+
$this->stackedQueries['body'] = array_merge($this->stackedQueries['body'], $queries['body']);
242+
}
243+
} else {
244+
throw new LocalizedException(__('Stacked indexer queries not enabled'));
245+
}
246+
}
247+
185248
/**
186249
* Retrieve Elasticsearch server status
187250
*
@@ -234,7 +297,11 @@ public function addDocs(array $documents, $storeId, $mappedIndexerId)
234297
try {
235298
$indexName = $this->indexNameResolver->getIndexName($storeId, $mappedIndexerId, $this->preparedIndex);
236299
$bulkIndexDocuments = $this->getDocsArrayInBulkIndexFormat($documents, $indexName);
237-
$this->client->bulkQuery($bulkIndexDocuments);
300+
if ($this->isStackQueries === false) {
301+
$this->client->bulkQuery($bulkIndexDocuments);
302+
} else {
303+
$this->stackQueries($bulkIndexDocuments);
304+
}
238305
} catch (Exception $e) {
239306
$this->logger->critical($e);
240307
throw $e;
@@ -309,7 +376,11 @@ public function deleteDocs(array $documentIds, $storeId, $mappedIndexerId)
309376
$indexName,
310377
self::BULK_ACTION_DELETE
311378
);
312-
$this->client->bulkQuery($bulkDeleteDocuments);
379+
if ($this->isStackQueries === false) {
380+
$this->client->bulkQuery($bulkDeleteDocuments);
381+
} else {
382+
$this->stackQueries($bulkDeleteDocuments);
383+
}
313384
} catch (Exception $e) {
314385
$this->logger->critical($e);
315386
throw $e;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Magento\Elasticsearch\Model\Indexer;
4+
5+
class EnhancedIndexerHandler extends IndexerHandler
6+
{
7+
/**
8+
* Disables stacked actions mode
9+
*
10+
* @return void
11+
*/
12+
public function disableStackedActions(): void
13+
{
14+
$this->adapter->disableStackQueriesMode();
15+
}
16+
17+
/**
18+
* Enables stacked actions mode
19+
*
20+
* @return void
21+
*/
22+
public function enableStackedActions(): void
23+
{
24+
$this->adapter->enableStackQueriesMode();
25+
}
26+
27+
/**
28+
* Runs stacked actions
29+
*
30+
* @return void
31+
* @throws \Exception
32+
*/
33+
public function triggerStackedActions(): void
34+
{
35+
$this->adapter->triggerStackedQueries();
36+
}
37+
}

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-
private $adapter;
40+
protected $adapter;
4141

4242
/**
4343
* @var IndexNameResolver

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

Lines changed: 1 addition & 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\IndexerHandler</item>
208+
<item name="elasticsearch5" xsi:type="string">Magento\Elasticsearch\Model\Indexer\EnhancedIndexerHandler</item>
209209
</argument>
210210
</arguments>
211211
</type>

app/code/Magento/Elasticsearch/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
"Test Connection","Test Connection"
1313
"Minimum Terms to Match","Minimum Terms to Match"
1414
"Created indexer handler must be instance of %1.", "Created indexer handler must be instance of %1."
15+
"Stacked indexer queries not enabled", "Stacked indexer queries not enabled"

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\IndexerHandler</item>
54+
<item name="elasticsearch7" xsi:type="string">Magento\Elasticsearch\Model\Indexer\EnhancedIndexerHandler</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\IndexerHandler</item>
93+
<item name="opensearch" xsi:type="string">Magento\Elasticsearch\Model\Indexer\EnhancedIndexerHandler</item>
9494
</argument>
9595
</arguments>
9696
</type>

0 commit comments

Comments
 (0)