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

periodically check reindex status #219

Open
wants to merge 5 commits into
base: 2.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/IndexService/Worker/OpenSearch/AbstractOpenSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Doctrine\DBAL\Connection;
use Exception;
use OpenSearch\Client;
use OpenSearch\Common\Exceptions\OpenSearchException;
use OpenSearch\Common\Exceptions\RequestTimeout408Exception;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Config\OpenSearch;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Config\SearchConfigInterface;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Interpreter\RelationInterpreterInterface;
Expand All @@ -37,6 +39,8 @@
*/
abstract class AbstractOpenSearch extends ProductCentricBatchProcessingWorker implements IndexRefreshInterface
{
const REINDEX_TIMEOUT = 200;

const STORE_TABLE_NAME = 'ecommerceframework_productindex_store_opensearch';

const RELATION_FIELD = 'parentchildrelation';
Expand Down Expand Up @@ -869,6 +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 OpenSearchException
*/
protected function performReindex(string $sourceIndexName, string $targetIndexName): void
{
Expand Down Expand Up @@ -897,10 +902,35 @@ 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 RequestTimeout408Exception('reindex is not finished. Cleanup task index for task ' . $taskId);
}

Logger::info(sprintf('Completed re-index in %.02f seconds.', (time() - $startTime)));
}

Expand Down