Skip to content

Commit

Permalink
Merge pull request #3472 from vahonc/3453-elasticsuite-indices-detect…
Browse files Browse the repository at this point in the history
…-closed-indices-2.10-feature

[Indices] Feature #3453: Add detection for closed indices
  • Loading branch information
rbayet authored Dec 17, 2024
2 parents 86a57d7 + 974fd38 commit 690c5ea
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function getElasticSuiteIndices(): ?array
IndexStatus::GHOST_STATUS,
IndexStatus::EXTERNAL_STATUS,
IndexStatus::UNDEFINED_STATUS,
IndexStatus::CLOSED_STATUS,
];
$indices = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class IndexStatus extends AbstractRenderer
public const REBUILDING_STATUS = 'rebuilding';
public const GHOST_STATUS = 'ghost';
public const EXTERNAL_STATUS = 'external';
public const CLOSED_STATUS = 'closed';
public const UNDEFINED_STATUS = 'undefined';

/**
Expand All @@ -45,6 +46,7 @@ class IndexStatus extends AbstractRenderer
self::REBUILDING_STATUS => self::SEVERITY_MINOR,
self::GHOST_STATUS => self::SEVERITY_CRITICAL,
self::EXTERNAL_STATUS => self::SEVERITY_EXTERNAL,
self::CLOSED_STATUS => self::SEVERITY_MINOR,
self::UNDEFINED_STATUS => self::SEVERITY_UNDEFINED,
];

Expand Down
9 changes: 5 additions & 4 deletions src/module-elasticsuite-indices/Model/IndexStatsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ public function deleteIndex($indexName): void
public function indexStats($indexName, $alias): array
{
$data = [
'index_name' => $indexName,
'index_alias' => $alias,
'size' => 'undefined',
'index_name' => $indexName,
'index_alias' => $alias,
'number_of_documents' => 'undefined',
'size' => 'undefined',
];

try {
Expand All @@ -144,7 +145,7 @@ public function indexStats($indexName, $alias): array
sprintf('Error when loading/parsing statistics for index "%s"', $indexName),
['exception' => $e]
);
$data['index_status'] = IndexStatus::UNDEFINED_STATUS;
$data['index_status'] = IndexStatus::CLOSED_STATUS;
}

return $data;
Expand Down
60 changes: 58 additions & 2 deletions src/module-elasticsuite-indices/Model/IndexStatusProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
*/
namespace Smile\ElasticsuiteIndices\Model;

use Exception;
use Psr\Log\LoggerInterface;
use Magento\Framework\DataObject;
use Smile\ElasticsuiteCore\Api\Client\ClientInterface;
use Smile\ElasticsuiteCore\Helper\IndexSettings as IndexSettingsHelper;
use Smile\ElasticsuiteIndices\Block\Widget\Grid\Column\Renderer\IndexStatus;
use Smile\ElasticsuiteIndices\Model\ResourceModel\StoreIndices\CollectionFactory as StoreIndicesCollectionFactory;
Expand Down Expand Up @@ -45,6 +48,16 @@ class IndexStatusProvider
*/
private const SECONDS_IN_DAY = 86400;

/**
* @var ClientInterface
*/
private $client;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var IndexSettingsHelper
*/
Expand All @@ -63,22 +76,28 @@ class IndexStatusProvider
/**
* Constructor.
*
* @param ClientInterface $client ES client.
* @param IndexSettingsHelper $indexSettingsHelper Index settings helper.
* @param StoreIndicesCollectionFactory $storeIndicesFactory Store indices collection.
* @param WorkingIndexerCollectionFactory $indexerCollectionFactory Working indexers collection.
* @param LoggerInterface $logger Logger.
*/
public function __construct(
ClientInterface $client,
IndexSettingsHelper $indexSettingsHelper,
StoreIndicesCollectionFactory $storeIndicesFactory,
WorkingIndexerCollectionFactory $indexerCollectionFactory
WorkingIndexerCollectionFactory $indexerCollectionFactory,
LoggerInterface $logger
) {
$this->client = $client;
$this->indexSettingsHelper = $indexSettingsHelper;
$this->storeIndices = $storeIndicesFactory->create()->getItems();
$this->workingIndexers = $indexerCollectionFactory->create()->getItems();
$this->logger = $logger;
}

/**
* Get a index status.
* Get an index status.
*
* @param string $indexName Index name.
* @param string $alias Index alias.
Expand All @@ -93,6 +112,10 @@ public function getIndexStatus($indexName, $alias): string
return IndexStatus::EXTERNAL_STATUS;
}

if ($this->isClosed($indexName)) {
return IndexStatus::CLOSED_STATUS;
}

if ($this->isRebuilding($indexName, $indexDate)) {
return IndexStatus::REBUILDING_STATUS;
}
Expand Down Expand Up @@ -151,6 +174,39 @@ private function isExternal(string $indexName): bool
return true;
}

/**
* Returns if the index is closed.
*
* @param string $indexName Index name.
*
* @return bool
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
private function isClosed(string $indexName): bool
{
try {
// Ensure the index is NOT External before checking for Closed status.
if ($this->isExternal($indexName)) {
return false;
}

// Attempt to fetch index stats or metadata to check its status.
$indexStats = $this->client->indexStats($indexName);

// If we successfully retrieved index stats and no error occurs, the index is not closed.
return false;
} catch (Exception $e) {
// Log the error (optional for better diagnostics).
$this->logger->error(
sprintf('Error fetching index stats for "%s": %s', $indexName, $e->getMessage())
);

// If an error occurs, it's safer to assume the index could be closed, or the stats are unavailable.
// Returning true here means the index is likely closed or inaccessible.
return true;
}
}

/**
* Returns if index is ghost.
*
Expand Down

0 comments on commit 690c5ea

Please sign in to comment.