From 34fe702871ed88127a1e7a5c328d26be9e542f5b Mon Sep 17 00:00:00 2001 From: David Magoc Date: Mon, 2 Dec 2024 10:04:26 +0100 Subject: [PATCH 1/5] periodically check reindex status --- .../Worker/OpenSearch/AbstractOpenSearch.php | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php b/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php index d4c97a06..286b89b3 100644 --- a/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php +++ b/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php @@ -37,6 +37,7 @@ */ abstract class AbstractOpenSearch extends ProductCentricBatchProcessingWorker implements IndexRefreshInterface { + const REINDEX_TIMEOUT = 200; const STORE_TABLE_NAME = 'ecommerceframework_productindex_store_opensearch'; const RELATION_FIELD = 'parentchildrelation'; @@ -868,7 +869,7 @@ protected function getNextIndexVersion(): int * * @param string $sourceIndexName the name of the source index in ES. * @param string $targetIndexName the name of the target index in ES. If existing, will be deleted - * + * @throws \Exception */ protected function performReindex(string $sourceIndexName, string $targetIndexName): void { @@ -897,10 +898,34 @@ protected function performReindex(string $sourceIndexName, string $targetIndexNa 'body' => $body, ]); - $osClient->reindex([ + // in case of long running reindexing this might lead to Gateway Timeout of Opensearch. Due to that + // query without waiting for completion and check task status periodically + $result = $osClient->reindex([ 'body' => $body, + "wait_for_completion" => false, ]); + $taskId = $result['task']; + $taskResponse = null; + for ($checks = 1; $checks <= self::REINDEX_TIMEOUT; $checks++) { + sleep(15); + Logger::info('Waiting for reindex to finish. ' . $checks . '/' . self::REINDEX_TIMEOUT); + // query task status + $taskResponse = $osClient->tasks()->get(['task_id' => $taskId]); + // if task was completed delete it to not fill up index for tasks + if (isset($taskResponse['completed']) && $taskResponse['completed']) { + $osClient->delete([ + 'index' => '.tasks', + 'id' => $taskId, + ]); + break; + } + } + + if ($taskResponse === null || !isset($taskResponse['completed']) || $taskResponse['completed'] === false) { + throw new \Exception('reindex is not finished. Cleanup task index for task ' . $taskId); + } + Logger::info(sprintf('Completed re-index in %.02f seconds.', (time() - $startTime))); } From 6821a25c6bfe0981cc3812a0f6616237e5393203 Mon Sep 17 00:00:00 2001 From: AlternateIf Date: Mon, 2 Dec 2024 09:09:55 +0000 Subject: [PATCH 2/5] Apply php-cs-fixer changes --- src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php b/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php index 286b89b3..1b2d0da1 100644 --- a/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php +++ b/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php @@ -38,6 +38,7 @@ abstract class AbstractOpenSearch extends ProductCentricBatchProcessingWorker implements IndexRefreshInterface { const REINDEX_TIMEOUT = 200; + const STORE_TABLE_NAME = 'ecommerceframework_productindex_store_opensearch'; const RELATION_FIELD = 'parentchildrelation'; @@ -869,6 +870,7 @@ protected function getNextIndexVersion(): int * * @param string $sourceIndexName the name of the source index in ES. * @param string $targetIndexName the name of the target index in ES. If existing, will be deleted + * * @throws \Exception */ protected function performReindex(string $sourceIndexName, string $targetIndexName): void @@ -902,7 +904,7 @@ protected function performReindex(string $sourceIndexName, string $targetIndexNa // query without waiting for completion and check task status periodically $result = $osClient->reindex([ 'body' => $body, - "wait_for_completion" => false, + 'wait_for_completion' => false, ]); $taskId = $result['task']; @@ -918,6 +920,7 @@ protected function performReindex(string $sourceIndexName, string $targetIndexNa 'index' => '.tasks', 'id' => $taskId, ]); + break; } } From 41f1bb7d5c55e079cddbdf16084865201667072b Mon Sep 17 00:00:00 2001 From: David Magoc Date: Mon, 2 Dec 2024 10:20:05 +0100 Subject: [PATCH 3/5] use opensearch exception --- src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php b/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php index 1b2d0da1..18716c2e 100644 --- a/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php +++ b/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php @@ -871,7 +871,7 @@ protected function getNextIndexVersion(): int * @param string $sourceIndexName the name of the source index in ES. * @param string $targetIndexName the name of the target index in ES. If existing, will be deleted * - * @throws \Exception + * @throws \OpenSearch\Common\Exceptions\OpenSearchException */ protected function performReindex(string $sourceIndexName, string $targetIndexName): void { @@ -926,7 +926,7 @@ protected function performReindex(string $sourceIndexName, string $targetIndexNa } if ($taskResponse === null || !isset($taskResponse['completed']) || $taskResponse['completed'] === false) { - throw new \Exception('reindex is not finished. Cleanup task index for task ' . $taskId); + throw new \OpenSearch\Common\Exceptions\RequestTimeout408Exception('reindex is not finished. Cleanup task index for task ' . $taskId); } Logger::info(sprintf('Completed re-index in %.02f seconds.', (time() - $startTime))); From d44445fcfb1414fb18e6162f1b0a20ec682a0749 Mon Sep 17 00:00:00 2001 From: David Magoc Date: Mon, 2 Dec 2024 10:27:47 +0100 Subject: [PATCH 4/5] use opensearch exception --- src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php b/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php index 18716c2e..9bcd797c 100644 --- a/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php +++ b/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php @@ -18,6 +18,8 @@ use Doctrine\DBAL\Connection; use Exception; +use OpenSearch\Common\Exceptions\OpenSearchException; +use OpenSearch\Common\Exceptions\RequestTimeout408Exception; use OpenSearch\Client; use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Config\OpenSearch; use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Config\SearchConfigInterface; @@ -871,7 +873,7 @@ protected function getNextIndexVersion(): int * @param string $sourceIndexName the name of the source index in ES. * @param string $targetIndexName the name of the target index in ES. If existing, will be deleted * - * @throws \OpenSearch\Common\Exceptions\OpenSearchException + * @throws OpenSearchException */ protected function performReindex(string $sourceIndexName, string $targetIndexName): void { @@ -926,7 +928,7 @@ protected function performReindex(string $sourceIndexName, string $targetIndexNa } if ($taskResponse === null || !isset($taskResponse['completed']) || $taskResponse['completed'] === false) { - throw new \OpenSearch\Common\Exceptions\RequestTimeout408Exception('reindex is not finished. Cleanup task index for task ' . $taskId); + throw new RequestTimeout408Exception('reindex is not finished. Cleanup task index for task ' . $taskId); } Logger::info(sprintf('Completed re-index in %.02f seconds.', (time() - $startTime))); From 6a83db690dc2daf64f4a12c35b9dc90c2c53b460 Mon Sep 17 00:00:00 2001 From: AlternateIf Date: Mon, 2 Dec 2024 09:28:16 +0000 Subject: [PATCH 5/5] Apply php-cs-fixer changes --- src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php b/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php index 9bcd797c..6585faac 100644 --- a/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php +++ b/src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php @@ -18,9 +18,9 @@ use Doctrine\DBAL\Connection; use Exception; +use OpenSearch\Client; use OpenSearch\Common\Exceptions\OpenSearchException; use OpenSearch\Common\Exceptions\RequestTimeout408Exception; -use OpenSearch\Client; use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Config\OpenSearch; use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Config\SearchConfigInterface; use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Interpreter\RelationInterpreterInterface;