Skip to content

Commit 44408ea

Browse files
committed
MAGE-1251 Promote storeId to first class citizen in support of multi store where index names can be derived as needed
1 parent bb9de74 commit 44408ea

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

Service/Product/ReplicaManager.php

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ protected function hasReplicaConfigurationChanged(int $storeId): bool
8585
return false;
8686
case ReplicaState::REPLICA_STATE_UNKNOWN:
8787
default:
88-
$primaryIndexName = $this->indexNameFetcher->getProductIndexName($storeId);
89-
$old = $this->getMagentoReplicaConfigurationFromAlgolia($primaryIndexName, $storeId);
88+
$old = $this->getMagentoReplicaConfigurationFromAlgolia($storeId);
9089
$new = $this->sortingTransformer->transformSortingIndicesToReplicaSetting(
9190
$this->sortingTransformer->getSortingIndices($storeId, null, null, true)
9291
);
@@ -98,14 +97,19 @@ protected function hasReplicaConfigurationChanged(int $storeId): bool
9897

9998

10099
/**
101-
* @param $primaryIndexName
102-
* @param int|null $storeId
100+
* @param int $storeId
103101
* @param bool $refreshCache
102+
* @param string|null $primaryIndexName
104103
* @return string[]
105104
* @throws LocalizedException
105+
* @throws NoSuchEntityException
106106
*/
107-
protected function getReplicaConfigurationFromAlgolia($primaryIndexName, int $storeId = null, bool $refreshCache = false): array
107+
protected function getReplicaConfigurationFromAlgolia(
108+
int $storeId,
109+
bool $refreshCache = false,
110+
?string $primaryIndexName = null): array
108111
{
112+
$primaryIndexName ??= $this->indexNameFetcher->getProductIndexName($storeId);
109113
if ($refreshCache || !isset($this->_algoliaReplicaConfig[$primaryIndexName])) {
110114
try {
111115
$currentSettings = $this->algoliaHelper->getSettings($primaryIndexName, $storeId);
@@ -134,20 +138,19 @@ protected function clearAlgoliaReplicaSettingCache($primaryIndexName = null): vo
134138
* Obtain the replica configuration from Algolia but only those indices that are
135139
* relevant to the Magento integration
136140
*
137-
* @param string $primaryIndexName
138-
* @param int|null $storeId
141+
* @param int $storeId
139142
* @param bool $refreshCache
140143
* @return string[] Array of replica index names
141144
* @throws LocalizedException
142145
* @throws NoSuchEntityException
143146
*/
144147
protected function getMagentoReplicaConfigurationFromAlgolia(
145-
string $primaryIndexName,
146-
int $storeId = null,
148+
int $storeId,
147149
bool $refreshCache = false
148150
): array
149151
{
150-
$algoliaReplicas = $this->getReplicaConfigurationFromAlgolia($primaryIndexName, $storeId, $refreshCache);
152+
$primaryIndexName = $this->indexNameFetcher->getProductIndexName($storeId);
153+
$algoliaReplicas = $this->getReplicaConfigurationFromAlgolia($storeId, $refreshCache, $primaryIndexName);
151154
$magentoReplicas = $this->getMagentoReplicaSettings($primaryIndexName, $algoliaReplicas);
152155
return array_values(array_intersect($magentoReplicas, $algoliaReplicas));
153156
}
@@ -185,15 +188,15 @@ protected function isMagentoReplicaIndex(string $replicaIndexName, int|string $s
185188
}
186189

187190
/**
188-
* @param string $primaryIndexName
189-
* @param int|null $storeId
191+
* @param int $storeId
190192
* @return array
191193
* @throws LocalizedException
192194
* @throws NoSuchEntityException
193195
*/
194-
protected function getNonMagentoReplicaConfigurationFromAlgolia(string $primaryIndexName, int $storeId = null): array
196+
protected function getNonMagentoReplicaConfigurationFromAlgolia(int $storeId): array
195197
{
196-
$algoliaReplicas = $this->getReplicaConfigurationFromAlgolia($primaryIndexName, $storeId);
198+
$primaryIndexName = $this->indexNameFetcher->getProductIndexName($storeId);
199+
$algoliaReplicas = $this->getReplicaConfigurationFromAlgolia($storeId);
197200
$magentoReplicas = $this->getMagentoReplicaSettings($primaryIndexName, $algoliaReplicas);
198201
return array_diff($algoliaReplicas, $magentoReplicas);
199202
}
@@ -252,8 +255,8 @@ protected function setReplicasOnPrimaryIndex(int $storeId): array
252255
$indexName = $this->indexNameFetcher->getProductIndexName($storeId);
253256
$sortingIndices = $this->sortingTransformer->getSortingIndices($storeId);
254257
$newMagentoReplicasSetting = $this->sortingTransformer->transformSortingIndicesToReplicaSetting($sortingIndices);
255-
$oldMagentoReplicasSetting = $this->getMagentoReplicaConfigurationFromAlgolia($indexName, $storeId, true);
256-
$nonMagentoReplicasSetting = $this->getNonMagentoReplicaConfigurationFromAlgolia($indexName, $storeId);
258+
$oldMagentoReplicasSetting = $this->getMagentoReplicaConfigurationFromAlgolia($storeId, true);
259+
$nonMagentoReplicasSetting = $this->getNonMagentoReplicaConfigurationFromAlgolia($storeId);
257260
$oldMagentoReplicaIndices = $this->getBareIndexNamesFromReplicaSetting($oldMagentoReplicasSetting);
258261
$newMagentoReplicaIndices = $this->getBareIndexNamesFromReplicaSetting($newMagentoReplicasSetting);
259262

@@ -273,7 +276,7 @@ protected function setReplicasOnPrimaryIndex(int $storeId): array
273276
$setReplicasTaskId = $this->algoliaHelper->getLastTaskId($storeId);
274277
$this->algoliaHelper->waitLastTask($storeId, $indexName, $setReplicasTaskId);
275278
$this->clearAlgoliaReplicaSettingCache($indexName);
276-
$this->deleteReplicas($replicasToDelete, false, false, $storeId);
279+
$this->deleteReplicas($storeId, $replicasToDelete, false, false);
277280

278281
if (self::_DEBUG) {
279282
$this->logger->log(
@@ -369,23 +372,23 @@ protected function getBareIndexNameFromReplicaSetting(string $replicaSetting): s
369372
/**
370373
* Delete replica indices
371374
*
375+
* @param int $storeId
372376
* @param array $replicasToDelete - which replicas to delete
373377
* @param bool $waitLastTask - wait until deleting next replica (default: false)
374378
* @param bool $safeMode - ensure replica is not attached to a primary index before attempting to delete (default: false)
375-
* @param int|null $storeId
376379
* @return void
377380
* @throws AlgoliaException
378381
* @throws ExceededRetriesException
379382
* @throws NoSuchEntityException
380383
*/
381384
protected function deleteReplicas(
385+
int $storeId,
382386
array $replicasToDelete,
383387
bool $waitLastTask = false,
384-
bool $safeMode = false,
385-
?int $storeId = null): void
388+
bool $safeMode = false): void
386389
{
387390
foreach ($replicasToDelete as $deletedReplica) {
388-
$this->deleteReplica($deletedReplica, $safeMode, $storeId);
391+
$this->deleteReplica($storeId, $deletedReplica, $safeMode);
389392
if ($waitLastTask) {
390393
$this->algoliaHelper->waitLastTask($storeId, $deletedReplica);
391394
}
@@ -397,12 +400,12 @@ protected function deleteReplicas(
397400
* @throws ExceededRetriesException
398401
* @throws NoSuchEntityException
399402
*/
400-
protected function deleteReplica(string $replicaIndexName, bool $safeMode = false, ?int $storeId = null): void
403+
protected function deleteReplica(int $storeId, string $replicaIndexName, bool $safeMode = false): void
401404
{
402405
if ($safeMode) {
403406
$settings = $this->algoliaHelper->getSettings($replicaIndexName, $storeId);
404407
if (isset($settings[self::ALGOLIA_SETTINGS_KEY_PRIMARY])) {
405-
$this->detachReplica($settings[self::ALGOLIA_SETTINGS_KEY_PRIMARY], $replicaIndexName, $storeId);
408+
$this->detachReplica($storeId, $replicaIndexName, $settings[self::ALGOLIA_SETTINGS_KEY_PRIMARY]);
406409
}
407410
}
408411

@@ -416,8 +419,10 @@ protected function deleteReplica(string $replicaIndexName, bool $safeMode = fals
416419
* @throws AlgoliaException
417420
* @throws NoSuchEntityException
418421
*/
419-
protected function detachReplica(string $primaryIndexName, string $replicaIndexName, ?int $storeId = null): void
422+
protected function detachReplica(int $storeId, string $replicaIndexName, ?string $primaryIndexName = null): void
420423
{
424+
$primaryIndexName ??= $this->indexNameFetcher->getProductIndexName($storeId);
425+
421426
$settings = $this->algoliaHelper->getSettings($primaryIndexName, $storeId);
422427
if (!isset($settings[self::ALGOLIA_SETTINGS_KEY_REPLICAS])) {
423428
return;
@@ -522,8 +527,9 @@ public function getMaxVirtualReplicasPerIndex() : int
522527
/**
523528
* @throws AlgoliaException
524529
*/
525-
protected function clearReplicasSettingInAlgolia(string $primaryIndexName, int $storeId): void
530+
protected function clearReplicasSettingInAlgolia(int $storeId): void
526531
{
532+
$primaryIndexName = $this->indexNameFetcher->getProductIndexName($storeId);
527533
$this->algoliaHelper->setSettings(
528534
$primaryIndexName,
529535
[ self::ALGOLIA_SETTINGS_KEY_REPLICAS => []],
@@ -544,12 +550,11 @@ public function deleteReplicasFromAlgolia(int $storeId, bool $unused = false): v
544550
if ($unused) {
545551
$replicasToDelete = $this->getUnusedReplicaIndices($storeId);
546552
} else {
547-
$primaryIndexName = $this->indexNameFetcher->getProductIndexName($storeId);
548-
$replicasToDelete = $this->getMagentoReplicaIndicesFromAlgolia($primaryIndexName);
549-
$this->clearReplicasSettingInAlgolia($primaryIndexName, $storeId);
553+
$replicasToDelete = $this->getMagentoReplicaIndicesFromAlgolia($storeId);
554+
$this->clearReplicasSettingInAlgolia($storeId);
550555
}
551556

552-
$this->deleteReplicas($replicasToDelete, true, true, $storeId);
557+
$this->deleteReplicas($storeId, $replicasToDelete, true, true);
553558

554559
if ($unused) {
555560
$this->clearUnusedReplicaIndicesCache($storeId);
@@ -559,9 +564,9 @@ public function deleteReplicasFromAlgolia(int $storeId, bool $unused = false): v
559564
/**
560565
* @throws LocalizedException
561566
*/
562-
protected function getMagentoReplicaIndicesFromAlgolia(string $primaryIndexName, $storeId = null): array
567+
protected function getMagentoReplicaIndicesFromAlgolia(int $storeId): array
563568
{
564-
return $this->getBareIndexNamesFromReplicaSetting($this->getMagentoReplicaConfigurationFromAlgolia($primaryIndexName, $storeId));
569+
return $this->getBareIndexNamesFromReplicaSetting($this->getMagentoReplicaConfigurationFromAlgolia($storeId));
565570
}
566571

567572
/**
@@ -571,7 +576,7 @@ public function getUnusedReplicaIndices(int $storeId): array
571576
{
572577
$primaryIndexName = $this->indexNameFetcher->getProductIndexName($storeId);
573578
if (!isset($this->_unusedReplicaIndices[$storeId])) {
574-
$currentReplicas = $this->getMagentoReplicaIndicesFromAlgolia($primaryIndexName);
579+
$currentReplicas = $this->getMagentoReplicaIndicesFromAlgolia($storeId);
575580
$unusedReplicas = [];
576581
$allIndices = $this->algoliaHelper->listIndexes($storeId);
577582

@@ -613,10 +618,9 @@ protected function getAllReplicaIndices(): array
613618
$replicaIndices = [];
614619
$storeIds = array_keys($this->storeManager->getStores());
615620
foreach ($storeIds as $storeId) {
616-
$primaryIndexName = $this->indexNameFetcher->getProductIndexName($storeId);
617621
$replicaIndices = array_merge(
618622
$replicaIndices,
619-
$this->getMagentoReplicaIndicesFromAlgolia($primaryIndexName)
623+
$this->getMagentoReplicaIndicesFromAlgolia($storeId)
620624
);
621625
}
622626
return array_unique($replicaIndices);

Test/Integration/Indexing/Product/ReplicaIndexingTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ protected function getTroublesomePatchReplicaManager(array $replicas): ReplicaMa
312312
$mock
313313
->expects($this->any())
314314
->method('deleteReplicas')
315-
->willReturnCallback(function(array $replicasToDelete, ...$params) use ($mock, $replicas) {
315+
->willReturnCallback(function(int $storeId, array $replicasToDelete, ...$params) use ($mock, $replicas) {
316316
$originalMethod = new \ReflectionMethod(ReplicaManager::class, 'deleteReplicas');
317-
$originalMethod->invoke($mock, $replicasToDelete, false, false);
317+
$originalMethod->invoke($mock, $storeId, $replicasToDelete, false, false);
318318
if ($this->patchRetries) return;
319319
$this->runOnce(
320320
function() use ($replicas) {

0 commit comments

Comments
 (0)