Skip to content

Commit

Permalink
[!!!][TASK] Split SolrService into SolrAdminService, SolrReadService …
Browse files Browse the repository at this point in the history
…and SolrWriteService

This pr:

* Extracts the SolrAdminService, SolrReadService and SolrWriteService from the SolrService.
* Changes the SolrConnectionManager to return a SolrConnection that could be used to access these services.
* Allows as followup to differnciate between read and write connections an to send them to different solr servers (master/slave)

Impact:

The ConnectionManager now return a SolrConnection that has the methods "getReadService", "getWriteService" and get "getAdminService".
These Services contain the corresponding method from the previous SolrService.

Fixes: #1745
  • Loading branch information
timohund committed Nov 24, 2017
1 parent 4a07368 commit eef6386
Show file tree
Hide file tree
Showing 35 changed files with 2,372 additions and 1,284 deletions.
27 changes: 13 additions & 14 deletions Classes/ConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use ApacheSolrForTypo3\Solr\System\Page\Rootline;
use ApacheSolrForTypo3\Solr\System\Records\Pages\PagesRepository as PagesRepositoryAtExtSolr;
use ApacheSolrForTypo3\Solr\System\Records\SystemLanguage\SystemLanguageRepository;
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Toolbar\ClearCacheActionsHookInterface;
use TYPO3\CMS\Core\Registry;
Expand Down Expand Up @@ -93,7 +94,7 @@ public function __construct(SystemLanguageRepository $systemLanguageRepository =
* @param string $scheme Solr scheme, defaults to http, can be https (optional)
* @param string $username Solr user name (optional)
* @param string $password Solr password (optional)
* @return SolrService A solr connection.
* @return SolrConnection A solr connection.
*/
public function getConnection($host = '', $port = 8983, $path = '/solr/', $scheme = 'http', $username = '', $password = '')
{
Expand All @@ -114,11 +115,7 @@ public function getConnection($host = '', $port = 8983, $path = '/solr/', $schem

$connectionHash = md5($scheme . '://' . $host . $port . $path . $username . $password);
if (!isset(self::$connections[$connectionHash])) {
$connection = $this->buildSolrService($host, $port, $path, $scheme);
if (trim($username) !== '') {
$connection->setAuthenticationCredentials($username, $password);
}

$connection = $this->buildSolrConnection($host, $port, $path, $scheme, $username, $password);
self::$connections[$connectionHash] = $connection;
}

Expand All @@ -132,18 +129,20 @@ public function getConnection($host = '', $port = 8983, $path = '/solr/', $schem
* @param int $port
* @param string $path
* @param string $scheme
* @return SolrService|object
* @param string $username
* @param string $password
* @return SolrConnection|object
*/
protected function buildSolrService($host, $port, $path, $scheme)
protected function buildSolrConnection($host, $port, $path, $scheme, $username = '', $password = '')
{
return GeneralUtility::makeInstance(SolrService::class, $host, $port, $path, $scheme);
return GeneralUtility::makeInstance(SolrConnection::class, $host, $port, $path, $scheme, $username, $password);
}

/**
* Creates a solr configuration from the configuration array and returns it.
*
* @param array $config The solr configuration array
* @return SolrService
* @return SolrConnection
*/
protected function getConnectionFromConfiguration(array $config)
{
Expand Down Expand Up @@ -198,7 +197,7 @@ public function getConfigurationByPageId($pageId, $language = 0, $mount = '')
* @param int $pageId A page ID.
* @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0.
* @param string $mount Comma list of MountPoint parameters
* @return SolrService A solr connection.
* @return SolrConnection A solr connection.
* @throws NoSolrConnectionFoundException
*/
public function getConnectionByPageId($pageId, $language = 0, $mount = '')
Expand Down Expand Up @@ -245,7 +244,7 @@ public function getConfigurationByRootPageId($pageId, $language = 0)
*
* @param int $pageId A root page ID.
* @param int $language The language ID to get the connection for as the path may differ. Optional, defaults to 0.
* @return SolrService A solr connection.
* @return SolrConnection A solr connection.
* @throws NoSolrConnectionFoundException
*/
public function getConnectionByRootPageId($pageId, $language = 0)
Expand Down Expand Up @@ -285,7 +284,7 @@ protected function setAllConfigurations(array $solrConfigurations)
/**
* Gets all connections found.
*
* @return SolrService[] An array of initialized ApacheSolrForTypo3\Solr\SolrService connections
* @return SolrConnection[] An array of initialized ApacheSolrForTypo3\Solr\System\Solr\SolrConnection connections
*/
public function getAllConnections()
{
Expand Down Expand Up @@ -323,7 +322,7 @@ public function getConfigurationsBySite(Site $site)
* Gets all connections configured for a given site.
*
* @param Site $site A TYPO3 site
* @return SolrService[] An array of Solr connection objects (ApacheSolrForTypo3\Solr\SolrService)
* @return SolrConnection[] An array of Solr connection objects (ApacheSolrForTypo3\Solr\System\Solr\SolrConnection)
*/
public function getConnectionsBySite(Site $site)
{
Expand Down
15 changes: 8 additions & 7 deletions Classes/Controller/Backend/Search/AbstractModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use ApacheSolrForTypo3\Solr\ConnectionManager;
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
use ApacheSolrForTypo3\Solr\Site;
use ApacheSolrForTypo3\Solr\SolrService as SolrCoreConnection;
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection as SolrCoreConnection;
use ApacheSolrForTypo3\Solr\System\Mvc\Backend\Component\Exception\InvalidViewObjectNameException;
use ApacheSolrForTypo3\Solr\System\Mvc\Backend\Service\ModuleDataStorageService;
use TYPO3\CMS\Backend\Template\Components\Menu\Menu;
Expand Down Expand Up @@ -199,17 +199,18 @@ protected function generateCoreSelectorMenu(Site $site, string $uriToRedirectTo
$this->initializeSelectedSolrCoreConnection();
$cores = $this->solrConnectionManager->getConnectionsBySite($site);
foreach ($cores as $core) {
$coreAdmin = $core->getAdminService();
$menuItem = $this->coreSelectorMenu->makeMenuItem();
$menuItem->setTitle($core->getPath());
$menuItem->setTitle($coreAdmin->getPath());
$uri = $this->uriBuilder->reset()->uriFor('switchCore',
[
'corePath' => $core->getPath(),
'corePath' => $coreAdmin->getPath(),
'uriToRedirectTo' => $uriToRedirectTo
]
);
$menuItem->setHref($uri);

if ($core->getPath() == $this->selectedSolrCoreConnection->getPath()) {
if ($coreAdmin->getPath() == $this->selectedSolrCoreConnection->getAdminService()->getPath()) {
$menuItem->setActive(true);
}
$this->coreSelectorMenu->addMenuItem($menuItem);
Expand Down Expand Up @@ -253,13 +254,13 @@ private function initializeSelectedSolrCoreConnection()
return;
}
foreach ($solrCoreConnections as $solrCoreConnection) {
if ($solrCoreConnection->getPath() == $currentSolrCorePath) {
if ($solrCoreConnection->getAdminService()->getPath() == $currentSolrCorePath) {
$this->selectedSolrCoreConnection = $solrCoreConnection;
}
}
if (!$this->selectedSolrCoreConnection instanceof SolrCoreConnection && count($solrCoreConnections) > 0) {
$this->initializeFirstAvailableSolrCoreConnection($solrCoreConnections, $moduleData);
$message = LocalizationUtility::translate('coreselector_switched_to_default_core', 'solr', [$currentSolrCorePath, $this->selectedSite->getLabel(), $this->selectedSolrCoreConnection->getPath()]);
$message = LocalizationUtility::translate('coreselector_switched_to_default_core', 'solr', [$currentSolrCorePath, $this->selectedSite->getLabel(), $this->selectedSolrCoreConnection->getAdminService()->getPath()]);
$this->addFlashMessage($message, '', AbstractMessage::NOTICE);
}
}
Expand All @@ -273,7 +274,7 @@ private function initializeFirstAvailableSolrCoreConnection(array $solrCoreConne
return;
}
$this->selectedSolrCoreConnection = $solrCoreConnections[0];
$moduleData->setCore($this->selectedSolrCoreConnection->getPath());
$moduleData->setCore($this->selectedSolrCoreConnection->getAdminService()->getPath());
$this->moduleDataStorageService->persistModuleData($moduleData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ public function indexAction()
}

$synonyms = [];
$rawSynonyms = $this->selectedSolrCoreConnection->getSynonyms();
$coreAdmin = $this->selectedSolrCoreConnection->getAdminService();
$rawSynonyms = $coreAdmin->getSynonyms();
foreach ($rawSynonyms as $baseWord => $synonymList) {
$synonyms[$baseWord] = implode(', ', $synonymList);
}

$stopWords = $this->selectedSolrCoreConnection->getStopWords();
$stopWords = $coreAdmin->getStopWords();
$this->view->assignMultiple([
'synonyms' => $synonyms,
'stopWords' => implode(PHP_EOL, $stopWords),
Expand Down Expand Up @@ -99,14 +100,12 @@ public function addSynonymsAction(string $baseWord, string $synonyms, $overrideE
$baseWord = mb_strtolower($baseWord);
$synonyms = mb_strtolower($synonyms);

if ($overrideExisting && $this->selectedSolrCoreConnection->getSynonyms($baseWord)) {
$this->selectedSolrCoreConnection->deleteSynonym($baseWord);
$coreAdmin = $this->selectedSolrCoreConnection->getAdminService();
if ($overrideExisting && $coreAdmin->getSynonyms($baseWord)) {
$coreAdmin->deleteSynonym($baseWord);
}
$this->selectedSolrCoreConnection->addSynonym(
$baseWord,
GeneralUtility::trimExplode(',', $synonyms, true)
);
$this->selectedSolrCoreConnection->reloadCore();
$coreAdmin->addSynonym($baseWord, GeneralUtility::trimExplode(',', $synonyms, true));
$coreAdmin->reloadCore();

$this->addFlashMessage(
'"' . $synonyms . '" added as synonyms for base word "' . $baseWord . '"'
Expand All @@ -122,8 +121,9 @@ public function addSynonymsAction(string $baseWord, string $synonyms, $overrideE
*/
public function exportStopWordsAction($fileFormat = 'txt')
{
$coreAdmin = $this->selectedSolrCoreConnection->getAdminService();
$this->exportFile(
implode(PHP_EOL, $this->selectedSolrCoreConnection->getStopWords()),
implode(PHP_EOL, $coreAdmin->getStopWords()),
'stopwords',
$fileFormat
);
Expand All @@ -137,7 +137,8 @@ public function exportStopWordsAction($fileFormat = 'txt')
*/
public function exportSynonymsAction($fileFormat = 'txt')
{
$synonyms = $this->selectedSolrCoreConnection->getSynonyms();
$coreAdmin = $this->selectedSolrCoreConnection->getAdminService();
$synonyms = $coreAdmin->getSynonyms();
return $this->exportFile(ManagedResourcesUtility::exportSynonymsToTxt($synonyms), 'synonyms', $fileFormat);
}

Expand All @@ -155,19 +156,18 @@ public function importSynonymListAction(array $synonymFileUpload, $overrideExist

$fileLines = ManagedResourcesUtility::importSynonymsFromPlainTextContents($synonymFileUpload);
$synonymCount = 0;

$coreAdmin = $this->selectedSolrCoreConnection->getAdminService();
foreach ($fileLines as $baseWord => $synonyms) {
if (!isset($baseWord) || empty($synonyms)) {
continue;
}
$this->deleteExistingSynonym($overrideExisting, $deleteSynonymsBefore, $baseWord);
$this->selectedSolrCoreConnection->addSynonym(
$baseWord,
$synonyms
);
$coreAdmin->addSynonym($baseWord, $synonyms);
$synonymCount++;
}

$this->selectedSolrCoreConnection->reloadCore();
$coreAdmin->reloadCore();
$this->addFlashMessage(
$synonymCount . ' synonyms imported.'
);
Expand Down Expand Up @@ -197,7 +197,8 @@ public function deleteAllSynonymsAction()
{
$allSynonymsCouldBeDeleted = $this->deleteAllSynonyms();

$reloadResponse = $this->selectedSolrCoreConnection->reloadCore();
$coreAdmin = $this->selectedSolrCoreConnection->getAdminService();
$reloadResponse = $coreAdmin->reloadCore();

if ($allSynonymsCouldBeDeleted
&& $reloadResponse->getHttpStatus() == 200
Expand All @@ -222,8 +223,9 @@ public function deleteAllSynonymsAction()
*/
public function deleteSynonymsAction($baseWord)
{
$deleteResponse = $this->selectedSolrCoreConnection->deleteSynonym($baseWord);
$reloadResponse = $this->selectedSolrCoreConnection->reloadCore();
$coreAdmin = $this->selectedSolrCoreConnection->getAdminService();
$deleteResponse = $coreAdmin->deleteSynonym($baseWord);
$reloadResponse = $coreAdmin->reloadCore();

if ($deleteResponse->getHttpStatus() == 200
&& $reloadResponse->getHttpStatus() == 200
Expand Down Expand Up @@ -254,7 +256,9 @@ public function saveStopWordsAction(string $stopWords, $replaceStopwords = true)
// lowercase stopword before saving because terms get lowercased before stopword filtering
$newStopWords = mb_strtolower($stopWords);
$newStopWords = GeneralUtility::trimExplode("\n", $newStopWords, true);
$oldStopWords = $this->selectedSolrCoreConnection->getStopWords();

$coreAdmin = $this->selectedSolrCoreConnection->getAdminService();
$oldStopWords = $coreAdmin->getStopWords();

if ($replaceStopwords) {
$removedStopWords = array_diff($oldStopWords, $newStopWords);
Expand All @@ -266,11 +270,11 @@ public function saveStopWordsAction(string $stopWords, $replaceStopwords = true)
$wordsAdded = true;
$addedStopWords = array_diff($newStopWords, $oldStopWords);
if (!empty($addedStopWords)) {
$wordsAddedResponse = $this->selectedSolrCoreConnection->addStopWords($addedStopWords);
$wordsAddedResponse = $coreAdmin->addStopWords($addedStopWords);
$wordsAdded = ($wordsAddedResponse->getHttpStatus() == 200);
}

$reloadResponse = $this->selectedSolrCoreConnection->reloadCore();
$reloadResponse = $coreAdmin->reloadCore();
if ($wordsRemoved && $wordsAdded && $reloadResponse->getHttpStatus() == 200) {
$this->addFlashMessage(
'Stop Words Updated.'
Expand All @@ -288,13 +292,15 @@ public function saveStopWordsAction(string $stopWords, $replaceStopwords = true)
*/
protected function exportFile($content, $type = 'synonyms', $fileExtension = 'txt') : string
{
$coreAdmin = $this->selectedSolrCoreConnection->getAdminService();

$this->response->setHeader('Content-type', 'text/plain', true);
$this->response->setHeader('Cache-control', 'public', true);
$this->response->setHeader('Content-Description', 'File transfer', true);
$this->response->setHeader(
'Content-disposition',
'attachment; filename =' . $type . '_' .
$this->selectedSolrCoreConnection->getCoreName() . '.' . $fileExtension,
$coreAdmin->getCoreName() . '.' . $fileExtension,
true
);
return $content;
Expand All @@ -307,11 +313,12 @@ protected function exportFile($content, $type = 'synonyms', $fileExtension = 'tx
*/
protected function deleteAllSynonyms() : bool
{
$synonyms = $this->selectedSolrCoreConnection->getSynonyms();
$coreAdmin = $this->selectedSolrCoreConnection->getAdminService();
$synonyms = $coreAdmin->getSynonyms();
$allSynonymsCouldBeDeleted = true;

foreach ($synonyms as $baseWord => $synonym) {
$deleteResponse = $this->selectedSolrCoreConnection->deleteSynonym($baseWord);
$deleteResponse = $coreAdmin->deleteSynonym($baseWord);
$allSynonymsCouldBeDeleted = $allSynonymsCouldBeDeleted && $deleteResponse->getHttpStatus() == 200;
}

Expand All @@ -325,8 +332,10 @@ protected function deleteAllSynonyms() : bool
protected function removeStopsWordsFromIndex($stopwordsToRemove) : bool
{
$wordsRemoved = true;
$coreAdmin = $this->selectedSolrCoreConnection->getAdminService();

foreach ($stopwordsToRemove as $word) {
$response = $this->selectedSolrCoreConnection->deleteStopWord($word);
$response = $coreAdmin->deleteStopWord($word);
if ($response->getHttpStatus() != 200) {
$wordsRemoved = false;
$this->addFlashMessage(
Expand All @@ -349,11 +358,13 @@ protected function removeStopsWordsFromIndex($stopwordsToRemove) : bool
*/
protected function deleteExistingSynonym($overrideExisting, $deleteSynonymsBefore, $baseWord)
{
$coreAdmin = $this->selectedSolrCoreConnection->getAdminService();

if (!$deleteSynonymsBefore &&
$overrideExisting &&
$this->selectedSolrCoreConnection->getSynonyms($baseWord)
$coreAdmin->getSynonyms($baseWord)
) {
$this->selectedSolrCoreConnection->deleteSynonym($baseWord);
$coreAdmin->deleteSynonym($baseWord);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

use ApacheSolrForTypo3\Solr\ConnectionManager;
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
use ApacheSolrForTypo3\Solr\SolrService;
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
use TYPO3\CMS\Backend\Routing\UriBuilder as BackendUriBuilder;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand Down Expand Up @@ -86,10 +86,11 @@ public function emptyIndexAction()
$affectedCores = [];
$solrServers = $this->solrConnectionManager->getConnectionsBySite($this->selectedSite);
foreach ($solrServers as $solrServer) {
/* @var $solrServer SolrService */
$solrServer->deleteByQuery('siteHash:' . $siteHash);
$solrServer->commit(false, false, false);
$affectedCores[] = $solrServer->getCoreName();
$writeService = $solrServer->getWriteService();
/* @var $solrServer SolrConnection */
$writeService->deleteByQuery('siteHash:' . $siteHash);
$writeService->commit(false, false, false);
$affectedCores[] = $writeService->getCoreName();
}
$this->addFlashMessage(LocalizationUtility::translate('solr.backend.index_administration.index_emptied_all', 'Solr', [$this->selectedSite->getLabel(), implode(', ', $affectedCores)]));
} catch (\Exception $e) {
Expand Down Expand Up @@ -126,9 +127,10 @@ public function reloadIndexConfigurationAction()
$solrServers = $this->solrConnectionManager->getConnectionsBySite($this->selectedSite);

foreach ($solrServers as $solrServer) {
/* @var $solrServer SolrService */
$coreReloaded = $solrServer->reloadCore()->getHttpStatus() === 200;
$coreName = $solrServer->getCoreName();
/* @var $solrServer SolrConnection */
$coreAdmin = $solrServer->getAdminService();
$coreReloaded = $coreAdmin->reloadCore()->getHttpStatus() === 200;
$coreName = $coreAdmin->getCoreName();

if (!$coreReloaded) {
$coresReloaded = false;
Expand Down
Loading

0 comments on commit eef6386

Please sign in to comment.