Skip to content

Commit

Permalink
Make Solr instantiation more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-meyer committed Dec 21, 2020
1 parent 6786da5 commit 5c9d79a
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 26 deletions.
5 changes: 4 additions & 1 deletion Classes/Common/DocumentList.php
Expand Up @@ -565,7 +565,10 @@ protected function solrConnect()
// Get Solr instance.
if (!$this->solr) {
// Connect to Solr server.
if ($this->solr = Solr::getInstance($this->metadata['options']['core'])) {
$solr = Solr::getInstance($this->metadata['options']['core']);
if ($solr->ready) {
$this->solr = $solr;
// Get indexed fields.
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_metadata');

Expand Down
4 changes: 3 additions & 1 deletion Classes/Common/Indexer.php
Expand Up @@ -560,7 +560,9 @@ protected static function solrConnect($core, $pid = 0)
// Get Solr instance.
if (!self::$solr) {
// Connect to Solr server.
if (self::$solr = Solr::getInstance($core)) {
$solr = Solr::getInstance($core);
if ($solr->ready) {
self::$solr = $solr;
// Load indexing configuration if needed.
if ($pid) {
self::loadIndexConf($pid);
Expand Down
28 changes: 15 additions & 13 deletions Classes/Common/Solr.php
Expand Up @@ -274,9 +274,11 @@ public static function getInstance($core = null)
$core = Helper::getIndexNameFromUid($core, 'tx_dlf_solrcores');
}
// Check if core is set or null.
if (empty($core) && $core !== null) {
if (
empty($core)
&& $core !== null
) {
Helper::devLog('Invalid core UID or name given for Apache Solr', DEVLOG_SEVERITY_ERROR);
return;
}
if (!empty($core)) {
// Check if there is an instance in the registry already.
Expand All @@ -291,13 +293,10 @@ public static function getInstance($core = null)
// Create new instance...
$instance = new self($core);
// ...and save it to registry.
if (
$instance->ready
&& !empty($instance->core)
) {
self::$registry[$instance->core] = $instance;
} else {
if (!$instance->ready) {
Helper::devLog('Could not connect to Apache Solr service', DEVLOG_SEVERITY_ERROR);
} elseif (!empty($instance->core)) {
self::$registry[$instance->core] = $instance;
}
return $instance;
}
Expand Down Expand Up @@ -617,7 +616,7 @@ public function __set($var, $value)
*
* @return void
*/
protected function __construct($core = null)
protected function __construct($core)
{
// Get Solr connection parameters from configuration.
$this->loadSolrConnectionInfo();
Expand Down Expand Up @@ -649,26 +648,29 @@ protected function __construct($core = null)
// Check if connection is established.
$query = $this->service->createCoreAdmin();
$action = $query->createStatus();
if (!empty($core)) {
if ($core !== null) {
$action->setCore($core);
}
$query->setAction($action);
try {
$response = $this->service->coreAdmin($query);
if ($response->getWasSuccessful()) {
// Instantiation successful!
$this->ready = true;
// Solr is reachable, but is the core as well?
if (!empty($core)) {
if ($core !== null) {
$result = $response->getStatusResultByCoreName($core);
if (
$result instanceof \Solarium\QueryType\Server\CoreAdmin\Result\StatusResult
&& $result->getUptime() > 0
) {
// Set core name.
$this->core = $core;
} else {
// Core not available.
return;
}
}
// Instantiation successful!
$this->ready = true;
}
} catch (\Exception $e) {
// Nothing to do here.
Expand Down
6 changes: 4 additions & 2 deletions Classes/Hooks/DataHandler.php
Expand Up @@ -246,7 +246,8 @@ public function processDatamap_afterDatabaseOperations($status, $table, $id, &$f
$resArray = $allResults[0];
if ($resArray['hidden']) {
// Establish Solr connection.
if ($solr = Solr::getInstance($resArray['core'])) {
$solr = Solr::getInstance($resArray['core']);
if ($solr->ready) {
// Delete Solr document.
$updateQuery = $solr->service->createUpdate();
$updateQuery->addDeleteQuery('uid:' . $id);
Expand Down Expand Up @@ -325,7 +326,8 @@ public function processCmdmap_postProcess($command, $table, $id)
case 'move':
case 'delete':
// Establish Solr connection.
if ($solr = Solr::getInstance($resArray['core'])) {
$solr = Solr::getInstance($resArray['core']);
if ($solr->ready) {
// Delete Solr document.
$updateQuery = $solr->service->createUpdate();
$updateQuery->addDeleteQuery('uid:' . $id);
Expand Down
4 changes: 4 additions & 0 deletions Classes/Plugin/Collection.php
Expand Up @@ -142,6 +142,10 @@ protected function showCollectionList()
$this->showSingleCollection(intval($resArray['uid']));
}
$solr = Solr::getInstance($this->conf['solrcore']);
if (!$solr->ready) {
Helper::devLog('Apache Solr not available', DEVLOG_SEVERITY_ERROR);
return $content;
}
// We only care about the UID and partOf in the results and want them sorted
$params['fields'] = 'uid,partof';
$params['sort'] = ['uid' => 'asc'];
Expand Down
5 changes: 1 addition & 4 deletions Classes/Plugin/Eid/SearchInDocument.php
Expand Up @@ -53,10 +53,7 @@ public function main(ServerRequestInterface $request)
$core = Helper::decrypt($encrypted, $hashed);
// Perform Solr query.
$solr = Solr::getInstance($core);
if (
$solr->ready
&& $solr->core === $core
) {
if ($solr->ready) {
$query = $solr->service->createSelect();
$query->setFields(['id', 'uid', 'page']);
$query->setQuery('fulltext:(' . Solr::escapeQuery((string) $parameters['q']) . ') AND uid:' . intval($parameters['uid']));
Expand Down
5 changes: 1 addition & 4 deletions Classes/Plugin/Eid/SearchSuggest.php
Expand Up @@ -49,10 +49,7 @@ public function main(ServerRequestInterface $request)
$core = Helper::decrypt($encrypted, $hashed);
// Perform Solr query.
$solr = Solr::getInstance($core);
if (
$solr->ready
&& $solr->core === $core
) {
if ($solr->ready) {
$query = $solr->service->createSuggester();
$query->setCount(10);
$query->setDictionary('suggest');
Expand Down
4 changes: 4 additions & 0 deletions Classes/Plugin/OaiPmh.php
Expand Up @@ -923,6 +923,10 @@ protected function fetchDocumentUIDs()
$solr_query .= ' AND timestamp:[' . $from . ' TO ' . $until . ']';
$documentSet = [];
$solr = Solr::getInstance($this->conf['solrcore']);
if (!$solr->ready) {
Helper::devLog('Apache Solr not available', DEVLOG_SEVERITY_ERROR);
return $documentSet;
}
if (intval($this->conf['solr_limit']) > 0) {
$solr->limit = intval($this->conf['solr_limit']);
}
Expand Down
2 changes: 1 addition & 1 deletion class.ext_update.php
Expand Up @@ -141,7 +141,7 @@ protected function solariumSolrUpdateRequired(): bool
while ($resArray = $result->fetch()) {
// Instantiate search object.
$solr = Solr::getInstance($resArray['index_name']);
if ($solr->core !== $resArray['index_name']) {
if (!$solr->ready) {
return true;
}
}
Expand Down

0 comments on commit 5c9d79a

Please sign in to comment.