Skip to content

Commit

Permalink
Merge pull request #6909 from magento-arcticfoxes/B2B-1832
Browse files Browse the repository at this point in the history
B2B-1832: Error when reindex Catalog Search index during the snapshot…
  • Loading branch information
rganin authored Jun 8, 2021
2 parents d8b6e12 + 3083997 commit 0a43509
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
29 changes: 24 additions & 5 deletions app/code/Magento/Elasticsearch/Model/Adapter/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,26 @@ public function cleanIndex($storeId, $mappedIndexerId)
// prepare new index name and increase version
$indexPattern = $this->indexNameResolver->getIndexPattern($storeId, $mappedIndexerId);
$version = (int)(str_replace($indexPattern, '', $indexName));
$newIndexName = $indexPattern . (++$version);

// remove index if already exists
if ($this->client->indexExists($newIndexName)) {
$this->client->deleteIndex($newIndexName);
// compatibility with snapshotting collision
$deleteQueue = [];
do {
$newIndexName = $indexPattern . (++$version);
if ($this->client->indexExists($newIndexName)) {
$deleteQueue[]= $newIndexName;
$indexExists = true;
} else {
$indexExists = false;
}
} while ($indexExists);

foreach ($deleteQueue as $indexToDelete) {
// remove index if already exists, wildcard deletion may cause collisions
try {
$this->client->deleteIndex($indexToDelete);
} catch (\Exception $e) {
$this->logger->critical($e);
}
}

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

// remove obsolete index
if ($oldIndex) {
$this->client->deleteIndex($oldIndex);
try {
$this->client->deleteIndex($oldIndex);
} catch (\Exception $e) {
$this->logger->critical($e);
}
unset($this->indexByCode[$mappedIndexerId . '_' . $storeId]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,23 @@ public function testCleanIndex()
{
$this->indexNameResolver->expects($this->any())
->method('getIndexName')
->with(1, 'product', [])
->willReturn('indexName_product_1_v');
->willReturnMap([[1, 'product', [1 => null], '_product_1_v0']]);

$this->client->expects($this->atLeastOnce())
->method('indexExists')
->willReturn(true);
$this->client->expects($this->once())
->willReturnMap(
[
['_product_1_v1', true],
['_product_1_v2', true],
['_product_1_v3', false],
]
);
$this->client->expects($this->exactly(2))
->method('deleteIndex')
->with('_product_1_v1');
->willReturnMap([
['_product_1_v1'],
['_product_1_v2'],
]);
$this->assertSame(
$this->model,
$this->model->cleanIndex(1, 'product')
Expand Down

0 comments on commit 0a43509

Please sign in to comment.