Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate Replica Issue one set is standard and another is virtual #1453

Merged
merged 6 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions Helper/Entity/ProductHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ public function setSettings($indexName, $indexNameTmp, $storeId, $saveToTmpIndic

// Managing Virtual Replica
if ($this->configHelper->useVirtualReplica($storeId)) {
$replicas = $this->handleVirtualReplica($replicas, $indexName);
$replicas = $this->handleVirtualReplica($replicas);
}

// Merge current replicas with sorting replicas to not delete A/B testing replica indices
Expand Down Expand Up @@ -1534,7 +1534,7 @@ public function productIsInStock($product, $storeId)
* @param $replica
* @return array
*/
public function handleVirtualReplica($replicas, $indexName)
public function handleVirtualReplica($replicas)
{
$virtualReplicaArray = [];
foreach ($replicas as $replica) {
Expand All @@ -1558,17 +1558,19 @@ public function handlingReplica($indexName, $storeId, $sortingAttribute = false)
return $sortingIndex['name'];
}, $sortingIndices));
try {
$replicasFormated = $this->handleVirtualReplica($replicas);
$availableReplicaMatch = array_merge($replicasFormated, $replicas);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can cause an "Array to string conversion" failure when updating replica config. This must be tested across multiple scenarios.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I have verified it

if ($this->configHelper->useVirtualReplica($storeId)) {
$replicas = $this->handleVirtualReplica($replicas, $indexName);
$replicas = $replicasFormated;
}
$currentSettings = $this->algoliaHelper->getSettings($indexName);
if (is_array($currentSettings) && array_key_exists('replicas', $currentSettings)) {
$replicasRequired = array_values(array_diff_assoc($currentSettings['replicas'], $replicas));
$replicasRequired = array_values(array_diff($currentSettings['replicas'], $availableReplicaMatch));
$this->algoliaHelper->setSettings($indexName, ['replicas' => $replicasRequired]);
$setReplicasTaskId = $this->algoliaHelper->getLastTaskId();
$this->algoliaHelper->waitLastTask($indexName, $setReplicasTaskId);
if (count($replicas) > 0) {
foreach ($replicas as $replicaIndex) {
if (count($availableReplicaMatch) > 0) {
foreach ($availableReplicaMatch as $replicaIndex) {
$this->algoliaHelper->deleteIndex($replicaIndex);
}
}
Expand Down
4 changes: 3 additions & 1 deletion Model/Backend/Sorts.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ public function afterSave()
if ($this->isValueChanged()) {
try{
$oldValue = $this->serializer->unserialize($this->getOldValue());
$updatedValue = $this->serializer->unserialize($this->getValue());
$sortingAttributes = array_merge($oldValue, $updatedValue);
$storeIds = array_keys($this->storeManager->getStores());
foreach ($storeIds as $storeId) {
$indexName = $this->helper->getIndexName($this->productHelper->getIndexNameSuffix(), $storeId);
$this->productHelper->handlingReplica($indexName, $storeId, $oldValue);
$this->productHelper->handlingReplica($indexName, $storeId, $sortingAttributes);
}
} catch (AlgoliaException $e) {
if ($e->getCode() !== 404) {
Expand Down