Skip to content

Commit 0a43509

Browse files
authored
Merge pull request #6909 from magento-arcticfoxes/B2B-1832
B2B-1832: Error when reindex Catalog Search index during the snapshot…
2 parents d8b6e12 + 3083997 commit 0a43509

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,26 @@ public function cleanIndex($storeId, $mappedIndexerId)
239239
// prepare new index name and increase version
240240
$indexPattern = $this->indexNameResolver->getIndexPattern($storeId, $mappedIndexerId);
241241
$version = (int)(str_replace($indexPattern, '', $indexName));
242-
$newIndexName = $indexPattern . (++$version);
243242

244-
// remove index if already exists
245-
if ($this->client->indexExists($newIndexName)) {
246-
$this->client->deleteIndex($newIndexName);
243+
// compatibility with snapshotting collision
244+
$deleteQueue = [];
245+
do {
246+
$newIndexName = $indexPattern . (++$version);
247+
if ($this->client->indexExists($newIndexName)) {
248+
$deleteQueue[]= $newIndexName;
249+
$indexExists = true;
250+
} else {
251+
$indexExists = false;
252+
}
253+
} while ($indexExists);
254+
255+
foreach ($deleteQueue as $indexToDelete) {
256+
// remove index if already exists, wildcard deletion may cause collisions
257+
try {
258+
$this->client->deleteIndex($indexToDelete);
259+
} catch (\Exception $e) {
260+
$this->logger->critical($e);
261+
}
247262
}
248263

249264
// prepare new index
@@ -372,7 +387,11 @@ public function updateAlias($storeId, $mappedIndexerId)
372387

373388
// remove obsolete index
374389
if ($oldIndex) {
375-
$this->client->deleteIndex($oldIndex);
390+
try {
391+
$this->client->deleteIndex($oldIndex);
392+
} catch (\Exception $e) {
393+
$this->logger->critical($e);
394+
}
376395
unset($this->indexByCode[$mappedIndexerId . '_' . $storeId]);
377396
}
378397

app/code/Magento/Elasticsearch/Test/Unit/Model/Adapter/ElasticsearchTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,15 +336,23 @@ public function testCleanIndex()
336336
{
337337
$this->indexNameResolver->expects($this->any())
338338
->method('getIndexName')
339-
->with(1, 'product', [])
340-
->willReturn('indexName_product_1_v');
339+
->willReturnMap([[1, 'product', [1 => null], '_product_1_v0']]);
341340

342341
$this->client->expects($this->atLeastOnce())
343342
->method('indexExists')
344-
->willReturn(true);
345-
$this->client->expects($this->once())
343+
->willReturnMap(
344+
[
345+
['_product_1_v1', true],
346+
['_product_1_v2', true],
347+
['_product_1_v3', false],
348+
]
349+
);
350+
$this->client->expects($this->exactly(2))
346351
->method('deleteIndex')
347-
->with('_product_1_v1');
352+
->willReturnMap([
353+
['_product_1_v1'],
354+
['_product_1_v2'],
355+
]);
348356
$this->assertSame(
349357
$this->model,
350358
$this->model->cleanIndex(1, 'product')

0 commit comments

Comments
 (0)