Reedy has uploaded a new change for review. https://gerrit.wikimedia.org/r/255934
Change subject: refactor out connection singleton ...................................................................... refactor out connection singleton Requires complementary patch in Elastica extension to work: Ib0d8429 Bug: T86781 Change-Id: Ib6132f87641c4df83b476deb77f4926cc21b0b14 (cherry picked from commit 8d21229c43688eba694d8720fef3f755d3f83203) --- M autoload.php A includes/Api/ApiBase.php M includes/Api/ConfigDump.php M includes/Api/FreezeWritesToCluster.php M includes/Api/MappingDump.php M includes/Api/SettingsDump.php M includes/Api/Suggest.php M includes/Api/SuggestIndex.php M includes/BuildDocument/RedirectsAndIncomingLinks.php M includes/CirrusSearch.php M includes/Connection.php M includes/DataSender.php M includes/Dump.php M includes/ElasticsearchIntermediary.php M includes/Hooks.php M includes/Job/DeletePages.php M includes/Job/ElasticaWrite.php M includes/Job/IncomingLinkCount.php M includes/Job/Job.php M includes/Job/LinksUpdate.php M includes/Job/MassIndex.php M includes/Job/OtherIndex.php M includes/Maintenance/Maintenance.php D includes/Maintenance/ReindexForkController.php M includes/Maintenance/Reindexer.php M includes/Maintenance/Validators/CacheWarmersValidator.php M includes/Maintenance/Validators/Validator.php M includes/OtherIndexes.php M includes/Sanity/Checker.php M includes/Searcher.php M includes/Updater.php M includes/Version.php M maintenance/checkIndexes.php M maintenance/cirrusNeedsToBeBuilt.php M maintenance/dumpIndex.php M maintenance/forceSearchIndex.php M maintenance/freezeWritesToCluster.php M maintenance/indexNamespaces.php M maintenance/runSearch.php M maintenance/saneitize.php M maintenance/updateOneSearchIndexConfig.php M maintenance/updateSearchIndexConfig.php M maintenance/updateSuggesterIndex.php M maintenance/updateVersionIndex.php M tests/jenkins/cleanSetup.php M tests/jenkins/nukeAllIndexes.php M tests/unit/ConnectionTest.php 47 files changed, 365 insertions(+), 306 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CirrusSearch refs/changes/34/255934/1 diff --git a/autoload.php b/autoload.php index 06095ec..fae45b7 100644 --- a/autoload.php +++ b/autoload.php @@ -5,6 +5,7 @@ $wgAutoloadClasses += array( 'CirrusSearch' => __DIR__ . '/includes/CirrusSearch.php', + 'CirrusSearch\\Api\\ApiBase' => __DIR__ . '/includes/Api/ApiBase.php', 'CirrusSearch\\Api\\ConfigDump' => __DIR__ . '/includes/Api/ConfigDump.php', 'CirrusSearch\\Api\\FreezeWritesToCluster' => __DIR__ . '/includes/Api/FreezeWritesToCluster.php', 'CirrusSearch\\Api\\MappingDump' => __DIR__ . '/includes/Api/MappingDump.php', @@ -49,7 +50,6 @@ 'CirrusSearch\\Maintenance\\IndexNamespaces' => __DIR__ . '/maintenance/indexNamespaces.php', 'CirrusSearch\\Maintenance\\Maintenance' => __DIR__ . '/includes/Maintenance/Maintenance.php', 'CirrusSearch\\Maintenance\\MappingConfigBuilder' => __DIR__ . '/includes/Maintenance/MappingConfigBuilder.php', - 'CirrusSearch\\Maintenance\\ReindexForkController' => __DIR__ . '/includes/Maintenance/ReindexForkController.php', 'CirrusSearch\\Maintenance\\Reindexer' => __DIR__ . '/includes/Maintenance/Reindexer.php', 'CirrusSearch\\Maintenance\\RunSearch' => __DIR__ . '/maintenance/runSearch.php', 'CirrusSearch\\Maintenance\\StreamingForkController' => __DIR__ . '/includes/Maintenance/StreamingForkController.php', @@ -79,6 +79,7 @@ 'CirrusSearch\\Sanity\\PrintingRemediator' => __DIR__ . '/includes/Sanity/Remediator.php', 'CirrusSearch\\Sanity\\QueueingRemediator' => __DIR__ . '/includes/Sanity/QueueingRemediator.php', 'CirrusSearch\\Sanity\\Remediator' => __DIR__ . '/includes/Sanity/Remediator.php', + 'CirrusSearch\\SearchConfig' => __DIR__ . '/includes/SearchConfig.php', 'CirrusSearch\\Search\\Escaper' => __DIR__ . '/includes/Search/Escaper.php', 'CirrusSearch\\Search\\FancyTitleResultsType' => __DIR__ . '/includes/Search/ResultsType.php', 'CirrusSearch\\Search\\Filters' => __DIR__ . '/includes/Search/Filters.php', @@ -89,7 +90,6 @@ 'CirrusSearch\\Search\\ResultSet' => __DIR__ . '/includes/Search/ResultSet.php', 'CirrusSearch\\Search\\ResultsType' => __DIR__ . '/includes/Search/ResultsType.php', 'CirrusSearch\\Search\\TitleResultsType' => __DIR__ . '/includes/Search/ResultsType.php', - 'CirrusSearch\\SearchConfig' => __DIR__ . '/includes/SearchConfig.php', 'CirrusSearch\\Searcher' => __DIR__ . '/includes/Searcher.php', 'CirrusSearch\\Updater' => __DIR__ . '/includes/Updater.php', 'CirrusSearch\\UserTesting' => __DIR__ . '/includes/UserTesting.php', diff --git a/includes/Api/ApiBase.php b/includes/Api/ApiBase.php new file mode 100644 index 0000000..14422d6 --- /dev/null +++ b/includes/Api/ApiBase.php @@ -0,0 +1,19 @@ +<?php + +namespace CirrusSearch\Api; + +use ApiBase as CoreApiBase; +use CirrusSearch\Connection; +use ConfigFactory; + +abstract class ApiBase extends CoreApiBase { + private $connection; + + public function getCirrusConnection() { + if ($this->connection === null) { + $config = ConfigFactory::getDefaultInstance()->makeConfig( 'CirrusSearch' ); + $this->connection = new Connection( $config ); + } + return $this->connection; + } +} diff --git a/includes/Api/ConfigDump.php b/includes/Api/ConfigDump.php index 02f568f..cf99198 100644 --- a/includes/Api/ConfigDump.php +++ b/includes/Api/ConfigDump.php @@ -1,8 +1,6 @@ <?php - namespace CirrusSearch\Api; -use \ApiBase; /** * Dumps CirrusSearch configuration for easy viewing. diff --git a/includes/Api/FreezeWritesToCluster.php b/includes/Api/FreezeWritesToCluster.php index 8284c21..29ef9d6 100644 --- a/includes/Api/FreezeWritesToCluster.php +++ b/includes/Api/FreezeWritesToCluster.php @@ -1,8 +1,7 @@ <?php - namespace CirrusSearch\Api; -use ApiBase; + use CirrusSearch\DataSender; /** @@ -26,7 +25,8 @@ */ class FreezeWritesToCluster extends ApiBase { public function execute() { - $sender = new DataSender(); + $sender = new DataSender( $this->getCirrusConnection() ); + if ( $this->getParameter( 'thaw' ) ) { $sender->thawIndexes(); } else { diff --git a/includes/Api/MappingDump.php b/includes/Api/MappingDump.php index 453a3c9..4877f6c 100644 --- a/includes/Api/MappingDump.php +++ b/includes/Api/MappingDump.php @@ -1,9 +1,6 @@ <?php - namespace CirrusSearch\Api; -use \ApiBase; -use \CirrusSearch\Connection; /** * Dumps CirrusSearch mappings for easy viewing. @@ -25,9 +22,10 @@ */ class MappingDump extends ApiBase { public function execute() { - foreach( Connection::getAllIndexTypes() as $index ) { - $this->getResult()->addValue( null, $index, - Connection::getPageType( wfWikiId(), $index )->getMapping() ); + $conn = $this->getCirrusConnection(); + foreach( $conn->getAllIndexTypes() as $index ) { + $mapping = $conn->getPageType( wfWikiId(), $index )->getMapping(); + $this->getResult()->addValue( null, $index, $mapping ); $this->getResult()->addPreserveKeysList( array( $index, 'page' ), '_all' ); } } diff --git a/includes/Api/SettingsDump.php b/includes/Api/SettingsDump.php index 5977b81..856c07a 100644 --- a/includes/Api/SettingsDump.php +++ b/includes/Api/SettingsDump.php @@ -1,9 +1,6 @@ <?php - namespace CirrusSearch\Api; -use \ApiBase; -use \CirrusSearch\Connection; /** * Dumps CirrusSearch mappings for easy viewing. @@ -25,9 +22,10 @@ */ class SettingsDump extends ApiBase { public function execute() { - foreach( Connection::getAllIndexTypes() as $index ) { + $conn = $this->getCirrusConnection(); + foreach( $conn->getAllIndexTypes() as $index ) { $this->getResult()->addValue( array( $index, 'page' ), 'index', - Connection::getIndex( wfWikiId(), $index )->getSettings()->get() ); + $conn->getIndex( wfWikiId(), $index )->getSettings()->get() ); } } diff --git a/includes/Api/Suggest.php b/includes/Api/Suggest.php index 2869b95..7bca856 100644 --- a/includes/Api/Suggest.php +++ b/includes/Api/Suggest.php @@ -1,7 +1,6 @@ <?php namespace CirrusSearch\Api; -use ApiBase; use CirrusSearch\Searcher; use RequestContext; @@ -28,7 +27,9 @@ public function execute() { $context = RequestContext::getMain(); $user = $context->getUser(); - $searcher = new Searcher( 0, $this->getParameter( 'limit' ), null, null, $user ); + $conn = $this->getCirrusConnection(); + $searcher = new Searcher( $conn, 0, $this->getParameter( 'limit' ), null, null, $user ); + $queryText = $this->getParameter( 'text' ); if ( !$queryText ) { diff --git a/includes/Api/SuggestIndex.php b/includes/Api/SuggestIndex.php index 7e84891..8bd005f 100644 --- a/includes/Api/SuggestIndex.php +++ b/includes/Api/SuggestIndex.php @@ -1,8 +1,6 @@ <?php namespace CirrusSearch\Api; -use ApiBase; - /** * Update ElasticSearch suggestion index * @@ -31,4 +29,4 @@ wfShellExecWithStderr( "unset REQUEST_METHOD; /usr/local/bin/mwscript $updaterScript --wiki " . wfWikiID() ) ); } -} \ No newline at end of file +} diff --git a/includes/BuildDocument/RedirectsAndIncomingLinks.php b/includes/BuildDocument/RedirectsAndIncomingLinks.php index c673ab3..db8d57d 100644 --- a/includes/BuildDocument/RedirectsAndIncomingLinks.php +++ b/includes/BuildDocument/RedirectsAndIncomingLinks.php @@ -1,6 +1,7 @@ <?php namespace CirrusSearch\BuildDocument; + use CirrusSearch\Connection; use CirrusSearch\ElasticsearchIntermediary; use Elastica\Filter\Terms; @@ -8,6 +9,7 @@ use Elastica\Query\Filtered; use Elastica\Query\MatchAll; use MediaWiki\Logger\LoggerFactory; +use SplObjectStorage; /** * Adds redirects and incoming links to the documents. These are done together @@ -37,11 +39,14 @@ private $linkCountMultiSearch = null; private $linkCountClosures = null; - public static function buildDocument( $doc, $title ) { + public static function buildDocument( $doc, $title, Connection $conn ) { if ( self::$externalLinks === null ) { - self::$externalLinks = new self(); + self::$externalLinks = new SplObjectStorage; } - self::$externalLinks->realBuildDocument( $doc, $title ); + if ( !isset( self::$externalLinks[$conn] ) ) { + self::$externalLinks[$conn] = new self( $conn ); + } + self::$externalLinks[$conn]->realBuildDocument( $doc, $title ); return true; } @@ -50,14 +55,16 @@ // Nothing to do as we haven't set up any actions during the buildDocument phase return; } - self::$externalLinks->realFinishBatch( $pages ); + foreach ( self::$externalLinks as $conn ) { + self::$externalLinks[$conn]->realFinishBatch( $pages ); + } self::$externalLinks = null; return true; } - protected function __construct() { - parent::__construct( null, null ); - $this->linkCountMultiSearch = new \Elastica\Multi\Search( Connection::getClient() ); + protected function __construct( Connection $conn ) { + parent::__construct( $conn, null, null ); + $this->linkCountMultiSearch = new \Elastica\Multi\Search( $conn->getClient() ); $this->linkCountClosures = array(); } @@ -132,7 +139,7 @@ private function buildCount( $titles ) { $filter = new Terms( 'outgoing_link', $titles ); $filter->setCached( false ); // We're not going to be redoing this any time soon. - $type = Connection::getPageType( wfWikiId() ); + $type = $this->connection->getPageType( wfWikiId() ); $search = new Search( $type->getIndex()->getClient() ); $search->addIndex( $type->getIndex() ); $search->addType( $type ); diff --git a/includes/CirrusSearch.php b/includes/CirrusSearch.php index a1c1202..ee8d933 100644 --- a/includes/CirrusSearch.php +++ b/includes/CirrusSearch.php @@ -1,5 +1,6 @@ <?php +use CirrusSearch\Connection; use CirrusSearch\InterwikiSearcher; use CirrusSearch\Search\FullTextResultsType; use CirrusSearch\Searcher; @@ -47,8 +48,19 @@ */ private $indexBaseName; + /** + * @var Connection + */ + private $connection; + public function __construct( $baseName = null ) { $this->indexBaseName = $baseName === null ? wfWikiId() : $baseName; + $config = ConfigFactory::getDefaultInstance()->makeConfig( 'CirrusSearch' ); + $this->connection = new Connection( $config ); + } + + public function setConnection( Connection $connection ) { + $this->connection = $connection; } /** @@ -185,7 +197,7 @@ $this->indexBaseName = $config->getWikiId(); } - $searcher = new Searcher( $this->offset, $this->limit, $config, $this->namespaces, $user, $this->indexBaseName ); + $searcher = new Searcher( $this->connection, $this->offset, $this->limit, $config, $this->namespaces, $user, $this->indexBaseName ); // Ignore leading ~ because it is used to force displaying search results but not to effect them if ( substr( $term, 0, 1 ) === '~' ) { @@ -268,6 +280,7 @@ // wrote the code but Searcher needs some refactoring first. foreach ( $wgCirrusSearchInterwikiSources as $interwiki => $index ) { $iwSearch = new InterwikiSearcher( $this->namespaces, $user, $index, $interwiki ); + $iwSearch->setConnection( $this->connection ); $interwikiResult = $iwSearch->getInterwikiResults( $term ); if ( $interwikiResult ) { $status->getValue()->addInterwikiResults( $interwikiResult ); diff --git a/includes/Connection.php b/includes/Connection.php index 730f92d..7fa263e 100644 --- a/includes/Connection.php +++ b/includes/Connection.php @@ -61,11 +61,19 @@ const TITLE_SUGGEST_TYPE_NAME = 'titlesuggest'; /** + * @var SearchConfig + */ + protected $config; + + public function __construct( SearchConfig $config ) { + $this->config = $config; + } + + /** * @return array(string) */ public function getServerList() { - global $wgCirrusSearchServers; - return $wgCirrusSearchServers; + return $this->config->get( 'CirrusSearchServers' ); } /** @@ -74,8 +82,7 @@ * @return int */ public function getMaxConnectionAttempts() { - global $wgCirrusSearchConnectionAttempts; - return $wgCirrusSearchConnectionAttempts; + return $this->config->get( 'CirrusSearchConnectionAttempts' ); } /** @@ -83,8 +90,8 @@ * frozen indexes that should not be written to. * @return \Elastica\Index */ - public static function getFrozenIndex() { - $index = self::getIndex( 'mediawiki_cirrussearch_frozen_indexes' ); + public function getFrozenIndex() { + $index = $this->getIndex( 'mediawiki_cirrussearch_frozen_indexes' ); if ( !$index->exists() ) { $options = array( 'number_of_shards' => 1, @@ -98,8 +105,8 @@ /** * @return \Elastica\Type */ - public static function getFrozenIndexNameType() { - return self::getFrozenIndex()->getType( 'name' ); + public function getFrozenIndexNameType() { + return $this->getFrozenIndex()->getType( 'name' ); } /** @@ -108,8 +115,8 @@ * @param mixed $type type of index (content or general or false to get all) * @return \Elastica\Type */ - public static function getPageType( $name, $type = false ) { - return self::getIndex( $name, $type )->getType( self::PAGE_TYPE_NAME ); + public function getPageType( $name, $type = false ) { + return $this->getIndex( $name, $type )->getType( self::PAGE_TYPE_NAME ); } /** @@ -117,9 +124,9 @@ * @param mixed $name basename of index * @return \Elastica\Type */ - public static function getNamespaceType( $name ) { + public function getNamespaceType( $name ) { $type = 'general'; // Namespaces are always stored in the 'general' index. - return self::getIndex( $name, $type )->getType( self::NAMESPACE_TYPE_NAME ); + return $this->getIndex( $name, $type )->getType( self::NAMESPACE_TYPE_NAME ); } /** @@ -127,9 +134,8 @@ * * @return array(string) */ - public static function getAllIndexTypes() { - global $wgCirrusSearchNamespaceMappings; - return array_merge( array_values( $wgCirrusSearchNamespaceMappings ), + public function getAllIndexTypes() { + return array_merge( array_values( $this->config->get( 'CirrusSearchNamespaceMappings' ) ), array( self::CONTENT_INDEX_TYPE, self::GENERAL_INDEX_TYPE ) ); } @@ -141,8 +147,8 @@ * @param \Elastica\Index[] $indexes * @return string[] */ - public static function indexToIndexTypes( array $indexes ) { - $allowed = self::getAllIndexTypes(); + public function indexToIndexTypes( array $indexes ) { + $allowed = $this->getAllIndexTypes(); return array_map( function( $type ) use ( $allowed ) { $fullName = $type->getIndex()->getName(); $parts = explode( '_', $fullName ); @@ -176,10 +182,10 @@ * @param int $namespace A namespace id * @return string */ - public static function getIndexSuffixForNamespace( $namespace ) { - global $wgCirrusSearchNamespaceMappings; - if ( isset( $wgCirrusSearchNamespaceMappings[$namespace] ) ) { - return $wgCirrusSearchNamespaceMappings[$namespace]; + public function getIndexSuffixForNamespace( $namespace ) { + $mappings = $this->config->get( 'CirrusSearchNamespaceMappings' ); + if ( isset( $mappings[$namespace] ) ) { + return $mappings[$namespace]; } return MWNamespace::isContent( $namespace ) ? @@ -191,19 +197,18 @@ * @var string $indexType an index type * @return false|integer false if the number of indexes is unknown, an integer if it is known */ - public static function namespacesInIndexType( $indexType ) { - global $wgCirrusSearchNamespaceMappings, - $wgContentNamespaces; - + public function namespacesInIndexType( $indexType ) { if ( $indexType === self::GENERAL_INDEX_TYPE ) { return false; } - $count = count( array_keys( $wgCirrusSearchNamespaceMappings, $indexType ) ); + $mappings = $this->config->get( 'CirrusSearchNamespaceMappings' ); + $count = count( array_keys( $mappings, $indexType ) ); if ( $indexType === self::CONTENT_INDEX_TYPE ) { // The content namespace includes everything set in the mappings to content (count right now) // Plus everything in wgContentNamespaces that isn't already in namespace mappings - $count += count( array_diff( $wgContentNamespaces, array_keys( $wgCirrusSearchNamespaceMappings ) ) ); + $contentNamespaces = $this->config->get( 'ContentNamespaces' ); + $count += count( array_diff( $contentNamespaces, array_keys( $mappings ) ) ); } return $count; } diff --git a/includes/DataSender.php b/includes/DataSender.php index de88333..43c24a5 100644 --- a/includes/DataSender.php +++ b/includes/DataSender.php @@ -29,8 +29,11 @@ class DataSender extends ElasticsearchIntermediary { const ALL_INDEXES_FROZEN_NAME = 'freeze_everything'; - public function __construct() { - parent::__construct( null, null ); + /** + * @var Connection + */ + public function __construct( Connection $conn ) { + parent::__construct( $conn, null, null ); $this->log = LoggerFactory::getInstance( 'CirrusSearch' ); $this->failedLog = LoggerFactory::getInstance( 'CirrusSearchChangeFailed' ); } @@ -49,7 +52,7 @@ } elseif ( count( $indexes ) === 0 ) { return; } else { - $names = self::indexesToIndexNames( $indexes ); + $names = $this->indexesToIndexNames( $indexes ); } $this->log->info( "Freezing writes to: " . implode( ',', $names ) ); @@ -64,8 +67,8 @@ $documents[] = $doc; } - $client = Connection::getClient(); - $type = Connection::getFrozenIndexNameType(); + $client = $this->connection->getClient(); + $type = $this->connection->getFrozenIndexNameType(); // Elasticsearch has a queue capacity of 50 so if $data // contains 50 documents it could bump up against the max. So // we chunk it and do them sequentially. @@ -94,12 +97,12 @@ } elseif ( count( $indexes ) === 0 ) { return; } else { - $names = self::indexesToIndexNames( $indexes ); + $names = $this->indexesToIndexNames( $indexes ); } $this->log->info( "Thawing writes to " . implode( ',', $names ) ); - Connection::getFrozenIndexNameType()->deleteIds( $names ); + $this->connection->getFrozenIndexNameType()->deleteIds( $names ); } /** @@ -117,12 +120,12 @@ return true; } if ( !$areIndexesFullyQualified ) { - $indexes = self::indexesToIndexNames( $indexes ); + $indexes = $this->indexesToIndexNames( $indexes ); } $ids = new \Elastica\Query\Ids( null, $indexes ); $ids->addId( self::ALL_INDEXES_FROZEN_NAME ); - $resp = Connection::getFrozenIndexNameType()->search( $ids ); + $resp = $this->connection->getFrozenIndexNameType()->search( $ids ); if ( $resp->count() === 0 ) { $this->log->debug( "Allowed writes to " . implode( ',', $indexes ) ); @@ -154,12 +157,12 @@ $exception = null; try { - $pageType = Connection::getPageType( wfWikiId(), $indexType ); + $pageType = $this->connection->getPageType( wfWikiId(), $indexType ); $this->start( "sending {numBulk} documents to the {indexType} index", array( 'numBulk' => $documentCount, 'indexType' => $indexType, ) ); - $bulk = new \Elastica\Bulk( Connection::getClient() ); + $bulk = new \Elastica\Bulk( $this->connection->getClient() ); if ( $shardTimeout ) { $bulk->setShardTimeout( $shardTimeout ); } @@ -207,7 +210,7 @@ */ public function sendDeletes( $ids, $indexType = null ) { if ( $indexType === null ) { - $indexes = Connection::getAllIndexTypes(); + $indexes = $this->connection->getAllIndexTypes(); } else { $indexes = array( $indexType ); } @@ -224,7 +227,7 @@ 'numIds' => $idCount, 'indexType' => $indexType, ) ); - Connection::getPageType( wfWikiId(), $indexType )->deleteIds( $ids ); + $this->connection->getPageType( wfWikiId(), $indexType )->deleteIds( $ids ); $this->success(); } } catch ( \Elastica\Exception\ExceptionInterface $e ) { @@ -251,7 +254,7 @@ return Status::newFatal( 'cirrussearch-indexes-frozen' ); } - $client = Connection::getClient(); + $client = $this->connection->getClient(); $status = Status::newGood(); foreach ( array_chunk( $otherActions, 30 ) as $updates ) { $bulk = new \Elastica\Bulk( $client ); @@ -353,11 +356,11 @@ return $justDocumentMissing; } - public static function indexesToIndexNames( array $indexes ) { + public function indexesToIndexNames( array $indexes ) { $names = array(); $wikiId = wfWikiId(); foreach ( $indexes as $indexType ) { - $names[] = Connection::getIndexName( $wikiId, $indexType ); + $names[] = $this->connection->getIndexName( $wikiId, $indexType ); } return $names; } diff --git a/includes/Dump.php b/includes/Dump.php index 55b1a61..10ea654 100644 --- a/includes/Dump.php +++ b/includes/Dump.php @@ -2,6 +2,7 @@ namespace CirrusSearch; +use ConfigFactory; use FormlessAction; /** @@ -31,7 +32,10 @@ $response = $this->getRequest()->response(); $response->header( 'Content-type: application/json; charset=UTF-8' ); - $searcher = new Searcher( 0, 0, null, array(), $this->getUser() ); + $config = ConfigFactory::getDefaultInstance()->makeConfig( 'CirrusSearch' ); + $conn = new Connection( $config ); + $searcher = new Searcher( $conn, 0, 0, null, array(), $this->getUser() ); + $id = $this->getTitle()->getArticleID(); $esSources = $searcher->get( array( $id ), true ); if ( !$esSources->isOk() ) { diff --git a/includes/ElasticsearchIntermediary.php b/includes/ElasticsearchIntermediary.php index 7b62813..eb11b25 100644 --- a/includes/ElasticsearchIntermediary.php +++ b/includes/ElasticsearchIntermediary.php @@ -2,6 +2,7 @@ namespace CirrusSearch; +use ConfigFactory; use DeferredUpdates; use Elastica\Exception\PartialShardFailureException; use Elastica\Exception\ResponseException; @@ -30,6 +31,10 @@ * http://www.gnu.org/copyleft/gpl.html */ class ElasticsearchIntermediary { + /** + * @var Connection + */ + protected $connection; /** * @var User|null user for which we're performing this search or null in the case of * requests kicked off by jobs @@ -83,10 +88,12 @@ * @param float $slowSeconds how many seconds a request through this intermediary needs to take before it counts as * slow. 0 means none count as slow. */ - protected function __construct( User $user = null, $slowSeconds ) { + protected function __construct( Connection $connection, User $user = null, $slowSeconds ) { + $this->connection = $connection; $this->user = $user; $this->slowMillis = round( 1000 * $slowSeconds ); $this->ut = UserTesting::getInstance(); + $config = ConfigFactory::getDefaultInstance()->makeConfig( 'CirrusSearch' ); } /** @@ -369,7 +376,7 @@ * @return array */ private function buildLogContext( $took ) { - $client = Connection::getClient(); + $client = $this->connection->getClient(); $query = $client->getLastRequest(); $result = $client->getLastResponse(); diff --git a/includes/Hooks.php b/includes/Hooks.php index 716f266..83eca40 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -2,28 +2,29 @@ namespace CirrusSearch; -use \ApiMain; -use \BetaFeatures; -use \CirrusSearch; -use \CirrusSearch\Search\FancyTitleResultsType; -use \CirrusSearch\Search\TitleResultsType; -use \DeferredUpdates; -use \JobQueueGroup; -use \LinksUpdate; -use \OutputPage; -use \ResourceLoader; -use \Skin; -use \SpecialSearch; -use \Title; -use \RecursiveDirectoryIterator; -use \RecursiveIteratorIterator; -use \RequestContext; -use \UsageException; -use \User; -use \WebRequest; -use \WikiPage; -use \Xml; -use \Html; +use ApiMain; +use BetaFeatures; +use CirrusSearch; +use CirrusSearch\Search\FancyTitleResultsType; +use CirrusSearch\Search\TitleResultsType; +use ConfigFactory; +use DeferredUpdates; +use JobQueueGroup; +use LinksUpdate; +use OutputPage; +use ResourceLoader; +use Skin; +use SpecialSearch; +use Title; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; +use RequestContext; +use UsageException; +use User; +use WebRequest; +use WikiPage; +use Xml; +use Html; /** * All CirrusSearch's external hooks. @@ -556,7 +557,7 @@ * @return bool */ public static function onSoftwareInfo( &$software ) { - $version = new Version; + $version = new Version( self::getConnection() ); $status = $version->get(); if ( $status->isOk() ) { // We've already logged if this isn't ok and there is no need to warn the user on this page. @@ -668,7 +669,7 @@ public static function prefixSearchExtractNamespace( &$namespaces, &$search ) { $user = RequestContext::getMain()->getUser(); - $searcher = new Searcher( 0, 1, null, $namespaces, $user ); + $searcher = new Searcher( self::getConnection(), 0, 1, null, $namespaces, $user ); $searcher->updateNamespacesFromQuery( $search ); $namespaces = $searcher->getNamespaces(); return false; @@ -685,7 +686,7 @@ */ public static function prefixSearch( $namespaces, $search, $limit, &$results, $offset = 0 ) { $user = RequestContext::getMain()->getUser(); - $searcher = new Searcher( $offset, $limit, null, $namespaces, $user ); + $searcher = new Searcher( self::getConnection(), $offset, $limit, null, $namespaces, $user ); if ( $search ) { $searcher->setResultsType( new FancyTitleResultsType( 'prefix' ) ); } else { @@ -739,7 +740,7 @@ $user = RequestContext::getMain()->getUser(); // Ask for the first 50 results we see. If there are more than that too bad. - $searcher = new Searcher( 0, 50, null, array( $title->getNamespace() ), $user ); + $searcher = new Searcher( self::getConnection(), 0, 50, null, array( $title->getNamespace() ), $user ); if ( $title->getNamespace() === NS_MAIN ) { $searcher->updateNamespacesFromQuery( $term ); } else { @@ -797,7 +798,8 @@ // almost everything. The only thing they miss is if a page moves from one // index to another. That only happens if it switches namespace. if ( $title->getNamespace() !== $newTitle->getNamespace() ) { - $oldIndexType = Connection::getIndexSuffixForNamespace( $title->getNamespace() ); + $conn = self::getConnection(); + $oldIndexType = $conn->getIndexSuffixForNamespace( $title->getNamespace() ); JobQueueGroup::singleton()->push( new Job\DeletePages( $title, array( 'indexType' => $oldIndexType, 'id' => $oldId @@ -939,4 +941,9 @@ return true; } + + private static function getConnection() { + $config = ConfigFactory::getDefaultInstance()->makeConfig( 'CirrusSearch' ); + return new Connection( $config ); + } } diff --git a/includes/Job/DeletePages.php b/includes/Job/DeletePages.php index 40c9c64..5b3ecf7 100644 --- a/includes/Job/DeletePages.php +++ b/includes/Job/DeletePages.php @@ -35,7 +35,7 @@ protected function doJob() { global $wgCirrusSearchClientSideUpdateTimeout; - $updater = new Updater(); + $updater = new Updater( $this->connection ); $indexType = isset( $this->params[ 'indexType' ] ) ? $this->params[ 'indexType' ] : null; return $updater->deletePages( array( $this->title ), array( $this->params[ 'id' ] ), $wgCirrusSearchClientSideUpdateTimeout, $indexType ); diff --git a/includes/Job/ElasticaWrite.php b/includes/Job/ElasticaWrite.php index b38c17c..df3f5ee 100644 --- a/includes/Job/ElasticaWrite.php +++ b/includes/Job/ElasticaWrite.php @@ -4,7 +4,8 @@ use CirrusSearch\Connection; use CirrusSearch\DataSender; -use \JobQueueGroup; +use ConfigFactory; +use JobQueueGroup; use MediaWiki\Logger\LoggerFactory; /** @@ -38,13 +39,14 @@ global $wgCirrusSearchDropDelayedJobsAfter; if ( $this->params['clientSideTimeout'] ) { - Connection::setTimeout( $this->params['clientSideTimeout'] ); + $this->connection->setTimeout( $this->params['clientSideTimeout'] ); } LoggerFactory::getInstance( 'CirrusSearch' )->debug( "Running {$this->params['method']} for " . json_encode( $this->params['arguments'] ) ); - $sender = new DataSender(); + $sender = new DataSender( $this->connection ); + $status = call_user_func_array( array( $sender, $this->params['method'] ), $this->params['arguments'] diff --git a/includes/Job/IncomingLinkCount.php b/includes/Job/IncomingLinkCount.php index e9e4570..7d42d3f 100644 --- a/includes/Job/IncomingLinkCount.php +++ b/includes/Job/IncomingLinkCount.php @@ -28,7 +28,7 @@ protected function doJob() { // Load the titles and filter out any that no longer exist. - $updater = new Updater(); + $updater = new Updater( $this->connection ); // We're intentionally throwing out whether or not this job succeeds. // We're loggging it but we're not retrying. $updater->updateLinkedArticles( array( $this->getTitle() ) ); diff --git a/includes/Job/Job.php b/includes/Job/Job.php index 9e2cc08..c16c8d9 100644 --- a/includes/Job/Job.php +++ b/includes/Job/Job.php @@ -2,8 +2,10 @@ namespace CirrusSearch\Job; -use \Job as MWJob; -use \JobQueueGroup; +use CirrusSearch\Connection; +use ConfigFactory; +use Job as MWJob; +use JobQueueGroup; /** * Abstract job class used by all CirrusSearch*Job classes @@ -24,6 +26,11 @@ * http://www.gnu.org/copyleft/gpl.html */ abstract class Job extends MWJob { + /** + * @var Connection + */ + protected $connection; + public function __construct( $title, $params ) { // eg: DeletePages -> cirrusSearchDeletePages $jobName = 'cirrusSearch' . str_replace( 'CirrusSearch\\Job\\', '', get_class( $this ) ); @@ -34,6 +41,13 @@ // it can't be deduplicated or else the search index will end up with out of date // data. Luckily, this is how the JobQueue implementations work. $this->removeDuplicates = true; + + $config = ConfigFactory::getDefaultInstance()->makeConfig( 'CirrusSearch' ); + $this->connection = new Connection( $config ); + } + + public function setConnection( Connection $connection ) { + $this->connection = $connection; } /** diff --git a/includes/Job/LinksUpdate.php b/includes/Job/LinksUpdate.php index 92c639c..f750f4d 100644 --- a/includes/Job/LinksUpdate.php +++ b/includes/Job/LinksUpdate.php @@ -45,7 +45,7 @@ protected function doJob() { global $wgCirrusSearchRefreshInterval; - $updater = new Updater(); + $updater = new Updater( $this->connection ); $res = $updater->updateFromTitle( $this->title ); if ( $res === false ) { // Couldn't update. Bail early and retry rather than adding an diff --git a/includes/Job/MassIndex.php b/includes/Job/MassIndex.php index c872d3f..7891f9b 100644 --- a/includes/Job/MassIndex.php +++ b/includes/Job/MassIndex.php @@ -57,7 +57,7 @@ $pageData[] = WikiPage::factory( $title ); } // Now invoke the updater! - $updater = new Updater(); + $updater = new Updater( $this->connection ); $count = $updater->updatePages( $pageData, null, null, $this->params[ 'updateFlags' ] ); return $count >= 0; } diff --git a/includes/Job/OtherIndex.php b/includes/Job/OtherIndex.php index 1da65f0..9753d25 100644 --- a/includes/Job/OtherIndex.php +++ b/includes/Job/OtherIndex.php @@ -54,7 +54,7 @@ $titles[] = Title::makeTitle( $namespace, $title ); } $otherIdx = new OtherIndexes( wfWikiId() ); - + $otherIdx->setConnection( $this->connection ); $otherIdx->updateOtherIndex( $titles ); } } diff --git a/includes/Maintenance/Maintenance.php b/includes/Maintenance/Maintenance.php index 500a342..1a1db25 100644 --- a/includes/Maintenance/Maintenance.php +++ b/includes/Maintenance/Maintenance.php @@ -2,6 +2,9 @@ namespace CirrusSearch\Maintenance; +use CirrusSearch\Connection; +use ConfigFactory; + /** * Cirrus helpful extensions to Maintenance. * @@ -25,6 +28,19 @@ protected static $indent = null; /** + * @var Connection|null + */ + private $connection; + + public function getConnection() { + if ( $this->connection === null ) { + $config = ConfigFactory::getDefaultInstance()->makeConfig( 'CirrusSearch' ); + $this->connection = new Connection( $config ); + } + return $this->connection; + } + + /** * Execute a callback function at the end of initialisation */ public function loadSpecialVars() { diff --git a/includes/Maintenance/ReindexForkController.php b/includes/Maintenance/ReindexForkController.php deleted file mode 100644 index cb8cd92..0000000 --- a/includes/Maintenance/ReindexForkController.php +++ /dev/null @@ -1,30 +0,0 @@ -<?php - -namespace CirrusSearch\Maintenance; -use \ForkController; - -/** - * Extensions to ForkController to prepare Elastica and to tell the child - * process which one it is. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/gpl.html - */ -class ReindexForkController extends ForkController { - protected function prepareEnvironment() { - parent::prepareEnvironment(); - \CirrusSearch\Connection::destroySingleton(); - } -} diff --git a/includes/Maintenance/Reindexer.php b/includes/Maintenance/Reindexer.php index ff5e9dc..899446d 100644 --- a/includes/Maintenance/Reindexer.php +++ b/includes/Maintenance/Reindexer.php @@ -10,6 +10,7 @@ use Elastica\Index; use Elastica\Query; use Elastica\Type; +use ForkController; use MediaWiki\Logger\LoggerFactory; /** @@ -142,8 +143,8 @@ 'routing.allocation.total_shards_per_node' => $maxShardsPerNode, ) ); - $sender = new DataSender(); - $frozenIndexes = Connection::indexToIndexTypes( $this->types ); + $sender = new DataSender( $this->connection ); + $frozenIndexes = $conn->indexToIndexTypes( $this->types ); $sender->freezeIndexes( $frozenIndexes ); if ( $processes > 1 ) { if ( !isset( $wgCirrusSearchWikimediaExtraPlugin[ 'id_hash_mod_filter' ] ) || @@ -151,9 +152,11 @@ $this->error( "Can't use multiple processes without \$wgCirrusSearchWikimediaExtraPlugin[ 'id_hash_mod_filter' ] = true", 1 ); } - $fork = new ReindexForkController( $processes ); + $fork = new ForkController( $processes ); $forkResult = $fork->start(); - // Forking clears the timeout so we have to reinstate it. + // we don't want to share sockets between forks, so destroy the client. + $this->connection->destroyClient(); + // destroying the client resets the timeout so we have to reinstate it. $this->setConnectionTimeout(); switch ( $forkResult ) { @@ -417,7 +420,7 @@ } private function setConnectionTimeout() { - $this->connection->setTimeout2( $this->connectionTimeout ); + $this->connection->setTimeout( $this->connectionTimeout ); } private function destroySingleton() { diff --git a/includes/Maintenance/Validators/CacheWarmersValidator.php b/includes/Maintenance/Validators/CacheWarmersValidator.php index b9410df..d809051 100644 --- a/includes/Maintenance/Validators/CacheWarmersValidator.php +++ b/includes/Maintenance/Validators/CacheWarmersValidator.php @@ -74,6 +74,7 @@ private function buildWarmer( $search ) { // This has a couple of compromises: $searcher = new Searcher( + $this->maint->getConnection(), 0, 50, // 0 offset 50 limit is the default for searching so we try it too. null, diff --git a/includes/Maintenance/Validators/Validator.php b/includes/Maintenance/Validators/Validator.php index 671b85c..8af2f17 100644 --- a/includes/Maintenance/Validators/Validator.php +++ b/includes/Maintenance/Validators/Validator.php @@ -8,7 +8,7 @@ /** * @var Maintenance */ - protected $out; + protected $maint; /** * @var bool @@ -16,10 +16,10 @@ protected $printDebugCheckConfig = false; /** - * @param Maintenance $out Maintenance object, to relay output to. + * @param Maintenance $maint Maintenance object, to relay output to. */ - public function __construct( Maintenance $out = null ) { - $this->out = $out; + public function __construct( Maintenance $maint ) { + $this->maint = $maint; } /** @@ -109,8 +109,8 @@ * @param mixed $channel */ protected function output( $message, $channel = null ) { - if ( $this->out ) { - $this->out->output( $message, $channel ); + if ( $this->maint ) { + $this->maint->output( $message, $channel ); } } @@ -118,8 +118,8 @@ * @param string $message */ protected function outputIndented( $message ) { - if ( $this->out ) { - $this->out->outputIndented( $message ); + if ( $this->maint ) { + $this->maint->outputIndented( $message ); } } } diff --git a/includes/OtherIndexes.php b/includes/OtherIndexes.php index 12a5bd4..8f0ad63 100644 --- a/includes/OtherIndexes.php +++ b/includes/OtherIndexes.php @@ -81,14 +81,14 @@ $updates = array(); // Build multisearch to find ids to update - $findIdsMultiSearch = new \Elastica\Multi\Search( Connection::getClient() ); + $findIdsMultiSearch = new \Elastica\Multi\Search( $this->getConnection()->getClient() ); $findIdsClosures = array(); foreach ( $titles as $title ) { foreach ( OtherIndexes::getExternalIndexes( $title ) as $otherIndex ) { if ( $otherIndex === null ) { continue; } - $type = Connection::getPageType( $otherIndex ); + $type = $this->getConnection()->getPageType( $otherIndex ); $bool = new \Elastica\Filter\Bool(); // Note that we need to use the keyword indexing of title so the analyzer gets out of the way. $bool->addMust( new \Elastica\Filter\Term( array( 'title.keyword' => $title->getText() ) ) ); @@ -147,6 +147,7 @@ 'method' => 'sendOtherIndexUpdates', 'arguments' => array( $this->localSite, $indexName, $actions ), ) ); + $job->setConnection( $this->getConnection() ); $job->run(); } } diff --git a/includes/Sanity/Checker.php b/includes/Sanity/Checker.php index 00ee595..72d974e 100644 --- a/includes/Sanity/Checker.php +++ b/includes/Sanity/Checker.php @@ -1,11 +1,13 @@ <?php namespace CirrusSearch\Sanity; -use \CirrusSearch\Connection; -use \CirrusSearch\Searcher; -use \Status; -use \Title; -use \WikiPage; + +use CirrusSearch\Connection; +use CirrusSearch\Searcher; +use ConfigFactory; +use Status; +use Title; +use WikiPage; /** * Checks if a WikiPage's representation in search index is sane. @@ -27,6 +29,7 @@ */ class Checker { + private $connection; private $searcher; private $remediator; private $logSane; @@ -38,7 +41,8 @@ * @param Searcher $searcher searcher to use for fetches * @param boolean $logSane should we log sane ids */ - public function __construct( $remediator, $searcher, $logSane ) { + public function __construct( Connection $connection, $remediator, $searcher, $logSane ) { + $this->connection = $connection; $this->remediator = $remediator; $this->searcher = $searcher; $this->logSane = $logSane; @@ -69,7 +73,7 @@ } else { if ( $inIndex ) { $foundInsanityInIndex = false; - $expectedType = Connection::getIndexSuffixForNamespace( $page->getTitle()->getNamespace() ); + $expectedType = $this->connection->getIndexSuffixForNamespace( $page->getTitle()->getNamespace() ); foreach ( $fromIndex as $indexInfo ) { $matches = array(); if ( !preg_match( '/_(.+)_.+$/', $indexInfo->getIndex(), $matches ) ) { diff --git a/includes/Searcher.php b/includes/Searcher.php index 10a1e3a..b98e8e3 100644 --- a/includes/Searcher.php +++ b/includes/Searcher.php @@ -227,14 +227,16 @@ * @param User|null $user user for which this search is being performed. Attached to slow request logs. * @param string|boolean $index Base name for index to search from, defaults to wfWikiId() */ - public function __construct( $offset, $limit, SearchConfig $config = null, array $namespaces = null, + public function __construct( Connection $conn, $offset, $limit, SearchConfig $config = null, array $namespaces = null, User $user = null, $index = false ) { if ( is_null( $config ) ) { + // @todo connection has an embeded config ... reuse that? somehow should + // at least ensure they are the same. $config = ConfigFactory::getDefaultInstance()->makeConfig( 'CirrusSearch' ); } - parent::__construct( $user, $config->get( 'CirrusSearchSlowSearch' ) ); + parent::__construct( $conn, $user, $config->get( 'CirrusSearchSlowSearch' ) ); $this->config = $config; $this->offset = min( $offset, self::MAX_OFFSET ); $this->limit = $limit; @@ -386,6 +388,7 @@ $this->suggestSuffixes[] = ' prefix:' . $value; // Suck namespaces out of $value $cirrusSearchEngine = new CirrusSearch(); + $cirrusSearchEngine->setConnection( $this->connection ); $value = trim( $cirrusSearchEngine->replacePrefixes( $value ) ); $this->namespaces = $cirrusSearchEngine->namespaces; // If the namespace prefix wasn't the entire prefix filter then add a filter for the title @@ -814,9 +817,9 @@ $queryOptions = array(); $queryOptions[ 'timeout' ] = $this->config->getElement( 'CirrusSearchSearchShardTimeout', 'default' ); - Connection::setTimeout( $queryOptions[ 'timeout' ] ); + $this->connection->setTimeout( $queryOptions[ 'timeout' ] ); - $index = Connection::getIndex( $this->indexBaseName, Connection::TITLE_SUGGEST_TYPE ); + $index = $this->connection->getIndex( $this->indexBaseName, Connection::TITLE_SUGGEST_TYPE ); $logContext = array( 'query' => $text, 'queryType' => 'comp_suggest' @@ -955,7 +958,7 @@ // very big index... // XXX: we support only the content index - $type = Connection::getPageType( $this->indexBaseName, Connection::CONTENT_INDEX_TYPE ); + $type = $this->connection->getPageType( $this->indexBaseName, Connection::CONTENT_INDEX_TYPE ); // NOTE: we are already in a poolCounterWork // Multi get is not supported by elastica $redirResponse = null; @@ -1138,18 +1141,19 @@ $indexType = $this->pickIndexTypeFromNamespaces(); $searcher = $this; $indexBaseName = $this->indexBaseName; + $conn = $this->connection; return Util::doPoolCounterWork( 'CirrusSearch-Search', $this->user, - function() use ( $searcher, $pageIds, $sourceFiltering, $indexType, $indexBaseName ) { + function() use ( $searcher, $pageIds, $sourceFiltering, $indexType, $indexBaseName, $conn ) { try { $searcher->start( "get of {indexType}.{pageIds}", array( 'indexType' => $indexType, 'pageIds' => array_map( 'intval', $pageIds ), ) ); // Shard timeout not supported on get requests so we just use the client side timeout - Connection::setTimeout( $this->config->getElement( 'CirrusSearchClientSideSearchTimeout', 'default' ) ); - $pageType = Connection::getPageType( $indexBaseName, $indexType ); + $conn->setTimeout( $this->config->getElement( 'CirrusSearchClientSideSearchTimeout', 'default' ) ); + $pageType = $conn->getPageType( $indexBaseName, $indexType ); $query = new \Elastica\Query( new \Elastica\Query\Ids( null, $pageIds ) ); $query->setParam( '_source', $sourceFiltering ); $query->addParam( 'stats', 'get' ); @@ -1168,15 +1172,16 @@ public function findNamespace( $name ) { $searcher = $this; $indexBaseName = $this->indexBaseName; + $conn = $this->connection; return Util::doPoolCounterWork( 'CirrusSearch-NamespaceLookup', $this->user, - function() use ( $searcher, $name, $indexBaseName ) { + function() use ( $searcher, $name, $indexBaseName, $conn ) { try { $searcher->start( "lookup namespace for {namespaceName}", array( 'namespaceName' => $name, ) ); - $pageType = Connection::getNamespaceType( $indexBaseName ); + $pageType = $conn->getNamespaceType( $indexBaseName ); $match = new \Elastica\Query\Match(); $match->setField( 'name', $name ); $query = new \Elastica\Query( $match ); @@ -1409,10 +1414,10 @@ $poolCounterType = 'CirrusSearch-Search'; $queryOptions[ 'timeout' ] = $this->config->getElement( 'CirrusSearchSearchShardTimeout', 'default' ); } - Connection::setTimeout( $queryOptions[ 'timeout' ] ); + $this->connection->setTimeout( $queryOptions[ 'timeout' ] ); // Setup the search - $pageType = Connection::getPageType( $this->indexBaseName, $indexType ); + $pageType = $this->connection->getPageType( $this->indexBaseName, $indexType ); $search = $pageType->createSearch( $query, $queryOptions ); foreach ( $extraIndexes as $i ) { $search->addIndex( $i ); @@ -1527,7 +1532,7 @@ // We're searching less than everything but we're going across indexes. Back to the defensive. return true; } - $namespacesInIndexType = Connection::namespacesInIndexType( $indexType ); + $namespacesInIndexType = $this->connection->namespacesInIndexType( $indexType ); return $nsCount !== $namespacesInIndexType; } @@ -1753,7 +1758,7 @@ $indexTypes = array(); foreach ( $this->namespaces as $namespace ) { $indexTypes[] = - Connection::getIndexSuffixForNamespace( $namespace ); + $this->connection->getIndexSuffixForNamespace( $namespace ); } $indexTypes = array_unique( $indexTypes ); return count( $indexTypes ) > 1 ? false : $indexTypes[0]; diff --git a/includes/Updater.php b/includes/Updater.php index 5b672d3..1c63a11 100644 --- a/includes/Updater.php +++ b/includes/Updater.php @@ -49,8 +49,8 @@ */ private $updated = array(); - public function __construct() { - parent::__construct( null, null ); + public function __construct( Connection $conn ) { + parent::__construct( $conn, null, null ); } /** @@ -190,9 +190,9 @@ $titles = $this->pagesToTitles( $pages ); Job\OtherIndex::queueIfRequired( $titles ); - $allData = array_fill_keys( Connection::getAllIndexTypes(), array() ); + $allData = array_fill_keys( $this->connection->getAllIndexTypes(), array() ); foreach ( $this->buildDocumentsForPages( $pages, $flags ) as $document ) { - $suffix = Connection::getIndexSuffixForNamespace( $document->get( 'namespace' ) ); + $suffix = $this->connection->getIndexSuffixForNamespace( $document->get( 'namespace' ) ); if ( isset( $wgCirrusSearchWikimediaExtraPlugin[ 'super_detect_noop' ] ) && $wgCirrusSearchWikimediaExtraPlugin[ 'super_detect_noop' ] ) { $document = $this->docToSuperDetectNoopScript( $document ); @@ -306,11 +306,11 @@ } // Then let hooks have a go - MWHooks::run( 'CirrusSearchBuildDocumentParse', array( $doc, $title, $content, $parserOutput ) ); + MWHooks::run( 'CirrusSearchBuildDocumentParse', array( $doc, $title, $content, $parserOutput, $this->connection ) ); } if ( !$skipLinks ) { - MWHooks::run( 'CirrusSearchBuildDocumentLinks', array( $doc, $title ) ); + MWHooks::run( 'CirrusSearchBuildDocumentLinks', array( $doc, $title, $this->connection) ); } $documents[] = $doc; diff --git a/includes/Version.php b/includes/Version.php index fe57bcb..5c8b525 100644 --- a/includes/Version.php +++ b/includes/Version.php @@ -25,8 +25,8 @@ /** * Constructor */ - public function __construct() { - parent::__construct( null, 0 ); + public function __construct( Connection $conn ) { + parent::__construct( $conn, null, 0 ); } /** @@ -44,8 +44,8 @@ $this->start( 'fetching elasticsearch version' ); // If this times out the cluster is in really bad shape but we should still // check it. - Connection::setTimeout( $wgCirrusSearchClientSideSearchTimeout[ 'default' ] ); - $result = Connection::getClient()->request( '' ); + $this->connection->setTimeout( $wgCirrusSearchClientSideSearchTimeout[ 'default' ] ); + $result = $this->connection->getClient()->request( '' ); $this->success(); } catch ( \Elastica\Exception\ExceptionInterface $e ) { return $this->failure( $e ); diff --git a/maintenance/checkIndexes.php b/maintenance/checkIndexes.php index d85df37..7d91708 100644 --- a/maintenance/checkIndexes.php +++ b/maintenance/checkIndexes.php @@ -1,7 +1,8 @@ <?php namespace CirrusSearch; -use \Maintenance; + +use CirrusSearch\Maintenance\Maintenance; /** * Check that all Cirrus indexes report OK. @@ -27,6 +28,7 @@ $IP = __DIR__ . '/../../..'; } require_once( "$IP/maintenance/Maintenance.php" ); +require_once( __DIR__ . '/../includes/Maintenance/Maintenance.php' ); class CheckIndexes extends Maintenance { private $errors = array(); @@ -172,14 +174,15 @@ private function ensureClusterStateFetched() { if ( $this->clusterState === null ) { - $this->clusterState = Connection::getClient()->request( '_cluster/state' )->getData(); + $this->clusterState = $this->getConnection()->getClient() + ->request( '_cluster/state' )->getData(); } } private function ensureCirrusInfoFetched() { if ( $this->cirrusInfo === null ) { $query = new \Elastica\Query(); $query->setSize( 5000 ); - $res = Connection::getIndex( 'mw_cirrus_versions' )->getType( 'version' ) + $res = $this->getConnection()->getIndex( 'mw_cirrus_versions' )->getType( 'version' ) ->getIndex()->search( $query ); $this->cirrusInfo = array(); foreach( $res as $r ) { diff --git a/maintenance/cirrusNeedsToBeBuilt.php b/maintenance/cirrusNeedsToBeBuilt.php index 59a98b3..24848c7 100644 --- a/maintenance/cirrusNeedsToBeBuilt.php +++ b/maintenance/cirrusNeedsToBeBuilt.php @@ -1,8 +1,9 @@ <?php namespace CirrusSearch; + +use CirrusSearch\Maintenance\Maintenance; use Elastica; -use \Maintenance; /** * Returns zero status if a Cirrus index needs to be built for this wiki. If @@ -29,6 +30,7 @@ $IP = __DIR__ . '/../../..'; } require_once( "$IP/maintenance/Maintenance.php" ); +require_once( __DIR__ . '/../includes/Maintenance/Maintenance.php' ); class CirrusIsSetup extends Maintenance { public function __construct() { @@ -40,7 +42,7 @@ $end = microtime( true ) + 60; while ( true ) { try { - $health = new \Elastica\Cluster\Health ( Connection::getClient() ); + $health = new \Elastica\Cluster\Health ( $this->getConnection()->getClient() ); $status = $health->getStatus(); $this->output( "Elasticsearch status: $status\n" ); if ( $status === 'green' ) { @@ -49,7 +51,7 @@ } catch ( \Elastica\Exception\Connection\HttpException $e ) { if ( $e->getError() === CURLE_COULDNT_CONNECT ) { $this->output( "Elasticsearch not up.\n" ); - Connection::destroySingleton(); + $this->getConnection()->destroyClient(); } else { // The two exit code here makes puppet fail with an error. $this->error( 'Connection error: ' . $e->getMessage(), 2 ); @@ -61,9 +63,9 @@ sleep( 1 ); } - foreach ( Connection::getAllIndexTypes() as $indexType ) { + foreach ( $this->getConnection()->getAllIndexTypes() as $indexType ) { try { - $count = Connection::getPageType( wfWikiId(), $indexType )->count(); + $count = $this->getConnection()->getPageType( wfWikiId(), $indexType )->count(); } catch ( \Elastica\Exception\ResponseException $e ) { $this->output( "$indexType doesn't exist.\n" ); $this->error( "true" ); diff --git a/maintenance/dumpIndex.php b/maintenance/dumpIndex.php index d0caa05..d86d7e3 100644 --- a/maintenance/dumpIndex.php +++ b/maintenance/dumpIndex.php @@ -2,10 +2,8 @@ namespace CirrusSearch\Maintenance; -use \CirrusSearch\Connection; -use \CirrusSearch\ElasticsearchIntermediary; -use \CirrusSearch\Util; - +use CirrusSearch\ElasticsearchIntermediary; +use CirrusSearch\Util; use Elastica; use Elastica\Filter; use Elastica\Index; @@ -102,13 +100,13 @@ $this->indexType = $this->getOption( 'indexType' ); $this->indexBaseName = $this->getOption( 'baseName', wfWikiId() ); - $indexTypes = $this->getAllIndexTypes(); + $indexTypes = $this->getConnection()->getAllIndexTypes(); if ( !in_array( $this->indexType, $indexTypes ) ) { $this->error( 'indexType option must be one of ' . implode( ', ', $indexTypes ), 1 ); } - $utils = new ConfigUtils( $this->getClient(), $this ); + $utils = new ConfigUtils( $this->getConnection()->getClient(), $this ); $this->indexIdentifier = $this->getOption( 'indexIdentifier' ); @@ -163,7 +161,7 @@ private function setConnectionTimeout() { global $wgCirrusSearchMaintenanceTimeout; - Connection::setTimeout( $wgCirrusSearchMaintenanceTimeout ); + $this->getConnection()->setTimeout( $wgCirrusSearchMaintenanceTimeout ); } public function write( array $document ) { @@ -186,28 +184,14 @@ } /** - * @return Elastica\Client - */ - private function getClient() { - return Connection::getClient(); - } - - /** * @return Elastica\Index being updated */ private function getIndex() { if ( $this->indexIdentifier ) { - return Connection::getIndex( $this->indexBaseName, $this->indexType, $this->indexIdentifier ); + return $this->getConnection()->getIndex( $this->indexBaseName, $this->indexType, $this->indexIdentifier ); } else { - return Connection::getIndex( $this->indexBaseName, $this->indexType ); + return $this->getConnection()->getIndex( $this->indexBaseName, $this->indexType ); } - } - - /** - * @return array - */ - private function getAllIndexTypes() { - return Connection::getAllIndexTypes(); } public function outputIndented( $message ) { diff --git a/maintenance/forceSearchIndex.php b/maintenance/forceSearchIndex.php index 7640cd3..e44c1be 100644 --- a/maintenance/forceSearchIndex.php +++ b/maintenance/forceSearchIndex.php @@ -1,10 +1,11 @@ <?php namespace CirrusSearch; + use CirrusSearch; +use CirrusSearch\Maintenance\Maintenance; use JobQueueGroup; use LinkCache; -use Maintenance; use MediaWiki\Logger\LoggerFactory; use MWException; use MWTimestamp; @@ -35,6 +36,7 @@ $IP = __DIR__ . '/../../..'; } require_once( "$IP/maintenance/Maintenance.php" ); +require_once( __DIR__ . '/../includes/Maintenance/Maintenance.php' ); class ForceSearchIndex extends Maintenance { const SECONDS_BETWEEN_JOB_QUEUE_LENGTH_CHECKS = 3; @@ -90,7 +92,7 @@ $wiki = sprintf( "[%20s]", wfWikiId() ); // Set the timeout for maintenance actions - Connection::setTimeout( $wgCirrusSearchMaintenanceTimeout ); + $this->getConnection()->setTimeout( $wgCirrusSearchMaintenanceTimeout ); // Make sure we've actually got indicies to populate if ( !$this->simpleCheckIndexes() ) { @@ -201,7 +203,7 @@ JobQueueGroup::singleton()->push( Job\MassIndex::build( $pages, $updateFlags ) ); } else { // Update size with the actual number of updated documents. - $updater = new Updater(); + $updater = new Updater( $this->getConnection() ); $size = $updater->updatePages( $pages, null, null, $updateFlags ); } } else { @@ -215,7 +217,7 @@ $minUpdate = $lastDelete[ 'timestamp' ]; $minNamespace = $lastDelete[ 'title' ]->getNamespace(); $minTitle = $lastDelete[ 'title' ]->getText(); - $updater = new Updater(); + $updater = new Updater( $this->getConnection() ); $updater->deletePages( $titlesToDelete, $idsToDelete ); } $completed += $size; @@ -264,17 +266,17 @@ */ private function simpleCheckIndexes() { $wiki = wfWikiId(); - $status = Connection::getClient()->getStatus(); + $status = $this->getConnection()->getClient()->getStatus(); // Top-level alias needs to exist - if ( !Connection::getIndex( $wiki )->exists() ) { + if ( !$this->getConnection()->getIndex( $wiki )->exists() ) { return false; } // Now check all index types to see if they exist - foreach ( Connection::getAllIndexTypes() as $indexType ) { + foreach ( $this->getConnection()->getAllIndexTypes() as $indexType ) { // If the alias for this type doesn't exist, fail - if ( !Connection::getIndex( $wiki, $indexType )->exists() ) { + if ( !$this->getConnection()->getIndex( $wiki, $indexType )->exists() ) { return false; } } @@ -349,7 +351,7 @@ $result = array(); // Build the updater outside the loop because it stores the redirects it hits. Don't build it at the top // level so those are stored when it is freed. - $updater = new Updater(); + $updater = new Updater( $this->getConnection() ); foreach ( $res as $row ) { // No need to call Updater::traceRedirects here because we know this is a valid page because diff --git a/maintenance/freezeWritesToCluster.php b/maintenance/freezeWritesToCluster.php index 51a4240..d36df67 100644 --- a/maintenance/freezeWritesToCluster.php +++ b/maintenance/freezeWritesToCluster.php @@ -3,7 +3,6 @@ namespace CirrusSearch\Maintenance; use CirrusSearch\DataSender; -use \Maintenance; /** * Freeze/thaw writes to the elasticsearch cluster. This effects all wikis in a @@ -30,6 +29,7 @@ $IP = __DIR__ . '/../../..'; } require_once( "$IP/maintenance/Maintenance.php" ); +require_once( __DIR__ . '/../includes/Maintenance/Maintenance.php' ); class FreezeWritesToCluster extends Maintenance { public function __construct() { @@ -40,7 +40,7 @@ } public function execute() { - $sender = new DataSender; + $sender = new DataSender( $this->getConnection() ); if ( $this->hasOption( 'thaw' ) ) { $sender->thawIndexes(); $this->output( "Thawed any existing cluster-wide freeze\n\n" ); diff --git a/maintenance/indexNamespaces.php b/maintenance/indexNamespaces.php index 5b73c29..192d110 100644 --- a/maintenance/indexNamespaces.php +++ b/maintenance/indexNamespaces.php @@ -2,9 +2,8 @@ namespace CirrusSearch\Maintenance; -use \CirrusSearch\Connection; -use \Elastica\Document; -use \Elastica\Query\MatchAll; +use Elastica\Document; +use Elastica\Query\MatchAll; /** * Index all namespaces for quick lookup. @@ -36,7 +35,7 @@ public function execute() { global $wgContLang; - $type = Connection::getNamespaceType( wfWikiId() ); + $type = $this->getConnection()->getNamespaceType( wfWikiId() ); $this->outputIndented( "Deleting namespaces..." ); $type->deleteByQuery( new MatchAll() ); diff --git a/maintenance/runSearch.php b/maintenance/runSearch.php index 10a35be..7ad4c55 100644 --- a/maintenance/runSearch.php +++ b/maintenance/runSearch.php @@ -4,8 +4,8 @@ use CirrusSearch; use CirrusSearch\Searcher; -use Status; use CirrusSearch\Search\ResultSet; +use Status; /** * Run search queries provided on stdin @@ -124,7 +124,9 @@ $searchType = $this->getOption( 'type', 'full_text' ); switch ( $searchType ) { case 'full_text': + // @todo pass through $this->getConnection() ? $engine = new CirrusSearch( $this->indexBaseName ); + $engine->setConnection( $this->getConnection() ); $result = $engine->searchText( $query ); if ( $result instanceof Status ) { return $result; @@ -133,11 +135,11 @@ } case 'prefix': - $searcher = new Searcher( 0, 10, null, null, null, $this->indexBaseName ); + $searcher = new Searcher( $this->getConnection(), 0, 10, null, null, null, $this->indexBaseName ); return $searcher->prefixSearch( $query ); case 'suggest': - $searcher = new Searcher( 0, 10, null, null, null, $this->indexBaseName ); + $searcher = new Searcher( $this->getConnection(), 0, 10, null, null, null, $this->indexBaseName ); $result = $searcher->suggest( $query ); if ( $result instanceof Status ) { return $result; diff --git a/maintenance/saneitize.php b/maintenance/saneitize.php index b36a45d..2e10c71 100644 --- a/maintenance/saneitize.php +++ b/maintenance/saneitize.php @@ -1,11 +1,12 @@ <?php namespace CirrusSearch; -use \CirrusSearch\Sanity\Checker; -use \CirrusSearch\Sanity\NoopRemediator; -use \CirrusSearch\Sanity\PrintingRemediator; -use \CirrusSearch\Sanity\QueueingRemediator; -use \Maintenance; + +use CirrusSearch\Maintenance\Maintenance; +use CirrusSearch\Sanity\Checker; +use CirrusSearch\Sanity\NoopRemediator; +use CirrusSearch\Sanity\PrintingRemediator; +use CirrusSearch\Sanity\QueueingRemediator; /** * Make sure the index for the wiki is sane. @@ -31,6 +32,7 @@ $IP = __DIR__ . '/../../..'; } require_once( "$IP/maintenance/Maintenance.php" ); +require_once( __DIR__ . '/../includes/Maintenance/Maintenance.php' ); class Saneitize extends Maintenance { private $fromId; @@ -55,7 +57,7 @@ $wgCirrusSearchClientSideUpdateTimeout; // Set the timeout for maintenance actions - Connection::setTimeout( $wgCirrusSearchMaintenanceTimeout ); + $this->getConnection()->setTimeout( $wgCirrusSearchMaintenanceTimeout ); $wgCirrusSearchClientSideUpdateTimeout = $wgCirrusSearchMaintenanceTimeout; // Make sure we don't flood the pool counter @@ -116,8 +118,8 @@ $this->remediator = new PrintingRemediator( $this->remediator ); } // This searcher searches all indexes for the current wiki. - $searcher = new Searcher( 0, 0, null, array(), null ); - $this->checker = new Checker( $this->remediator, $searcher, $this->getOption( 'logSane' ) ); + $searcher = new Searcher( $this->getConnection(), 0, 0, null, array(), null ); + $this->checker = new Checker( $this->getConnection(), $this->remediator, $searcher, $this->getOption( 'logSane' ) ); } } diff --git a/maintenance/updateOneSearchIndexConfig.php b/maintenance/updateOneSearchIndexConfig.php index 6d297d3..c64ce05 100644 --- a/maintenance/updateOneSearchIndexConfig.php +++ b/maintenance/updateOneSearchIndexConfig.php @@ -2,9 +2,10 @@ namespace CirrusSearch\Maintenance; -use \CirrusSearch\Connection; -use \CirrusSearch\ElasticsearchIntermediary; -use \CirrusSearch\Util; +use CirrusSearch\Connection; +use CirrusSearch\ElasticsearchIntermediary; +use CirrusSearch\Util; +use ConfigFactory; use Elastica; /** @@ -176,10 +177,11 @@ // Make sure we don't flood the pool counter unset( $wgPoolCounterConf['CirrusSearch-Search'] ); + // Set the timeout for maintenance actions $this->setConnectionTimeout(); - $utils = new ConfigUtils( $this->getClient(), $this ); + $utils = new ConfigUtils( $this->getConnection()->getClient(), $this ); $this->indexType = $this->getOption( 'indexType' ); $this->startOver = $this->getOption( 'startOver', false ); @@ -200,7 +202,7 @@ $this->refreshInterval = $wgCirrusSearchRefreshInterval; try{ - $indexTypes = $this->getAllIndexTypes(); + $indexTypes = $this->getConnection()->getAllIndexTypes(); if ( !in_array( $this->indexType, $indexTypes ) ) { $this->error( 'indexType option must be one of ' . implode( ', ', $indexTypes ), 1 ); @@ -351,7 +353,7 @@ $reindexer = new Reindexer( $this->getIndex(), - Connection::getSingleton(), + $this->getConnection(), array( $this->getPageType() ), array( $this->getOldPageType() ), $this->getShardCount(), @@ -363,7 +365,7 @@ ); $validator = new \CirrusSearch\Maintenance\Validators\SpecificAliasValidator( - $this->getClient(), + $this->getConnection()->getClient(), $this->getIndexTypeName(), $this->getSpecificIndexName(), $this->startOver, @@ -381,7 +383,7 @@ } public function validateAllAlias() { - $validator = new \CirrusSearch\Maintenance\Validators\IndexAllAliasValidator( $this->getClient(), + $validator = new \CirrusSearch\Maintenance\Validators\IndexAllAliasValidator( $this->getConnection()->getClient(), $this->getIndexName(), $this->getSpecificIndexName(), $this->startOver, $this->getIndexTypeName(), $this ); $status = $validator->validate(); if ( !$status->isOK() ) { @@ -455,42 +457,28 @@ * @return \Elastica\Index being updated */ public function getIndex() { - return Connection::getIndex( $this->indexBaseName, $this->indexType, $this->indexIdentifier ); + return $this->getConnection()->getIndex( $this->indexBaseName, $this->indexType, $this->indexIdentifier ); } /** * @return string name of the index being updated */ protected function getSpecificIndexName() { - return Connection::getIndexName( $this->indexBaseName, $this->indexType, $this->indexIdentifier ); + return $this->getConnection()->getIndexName( $this->indexBaseName, $this->indexType, $this->indexIdentifier ); } /** * @return string name of the index type being updated */ protected function getIndexTypeName() { - return Connection::getIndexName( $this->indexBaseName, $this->indexType ); + return $this->getConnection()->getIndexName( $this->indexBaseName, $this->indexType ); } /** * @return string */ protected function getIndexName() { - return Connection::getIndexName( $this->indexBaseName ); - } - - /** - * @return Elastica\Client - */ - protected function getClient() { - return Connection::getClient(); - } - - /** - * @return array - */ - protected function getAllIndexTypes() { - return Connection::getAllIndexTypes(); + return $this->getConnection()->getIndexName( $this->indexBaseName ); } /** @@ -515,12 +503,12 @@ * @return Elastica\Type */ protected function getOldPageType() { - return Connection::getPageType( $this->indexBaseName, $this->indexType ); + return $this->getConnection()->getPageType( $this->indexBaseName, $this->indexType ); } protected function setConnectionTimeout() { global $wgCirrusSearchMaintenanceTimeout; - Connection::setTimeout( $wgCirrusSearchMaintenanceTimeout ); + $this->getConnection()->setTimeout( $wgCirrusSearchMaintenanceTimeout ); } /** diff --git a/maintenance/updateSearchIndexConfig.php b/maintenance/updateSearchIndexConfig.php index 000480c..7d362da 100644 --- a/maintenance/updateSearchIndexConfig.php +++ b/maintenance/updateSearchIndexConfig.php @@ -2,8 +2,6 @@ namespace CirrusSearch\Maintenance; -use \CirrusSearch\Connection; - /** * Update the search configuration on the search backend. * @@ -44,7 +42,7 @@ } public function execute() { - foreach ( Connection::getAllIndexTypes() as $indexType ) { + foreach ( $this->getConnection()->getAllIndexTypes() as $indexType ) { $this->outputIndented( "$indexType index...\n"); $child = $this->runChild( 'CirrusSearch\Maintenance\UpdateOneSearchIndexConfig' ); $child->mOptions[ 'indexType' ] = $indexType; diff --git a/maintenance/updateSuggesterIndex.php b/maintenance/updateSuggesterIndex.php index a186de1..8089931 100644 --- a/maintenance/updateSuggesterIndex.php +++ b/maintenance/updateSuggesterIndex.php @@ -2,11 +2,11 @@ namespace CirrusSearch\Maintenance; -use \CirrusSearch\Connection; -use \CirrusSearch\ElasticsearchIntermediary; -use \CirrusSearch\Util; -use \CirrusSearch\BuildDocument\SuggestBuilder; -use \CirrusSearch\BuildDocument\SuggestScoringMethodFactory; +use CirrusSearch\Connection; +use CirrusSearch\ElasticsearchIntermediary; +use CirrusSearch\Util; +use CirrusSearch\BuildDocument\SuggestBuilder; +use CirrusSearch\BuildDocument\SuggestScoringMethodFactory; use Elastica; use Elastica\Query; @@ -115,6 +115,7 @@ // Make sure we don't flood the pool counter unset( $wgPoolCounterConf['CirrusSearch-Search'] ); + // Set the timeout for maintenance actions $this->setConnectionTimeout(); @@ -137,7 +138,7 @@ try { $oldIndexIdentifier = $utils->pickIndexIdentifierFromOption( 'current', $this->getIndexTypeName() ); - $this->oldIndex = Connection::getIndex( $this->indexBaseName, $this->indexTypeName, $oldIndexIdentifier ); + $this->oldIndex = $this->getConnection()->getIndex( $this->indexBaseName, $this->indexTypeName, $oldIndexIdentifier ); $this->indexIdentifier = $utils->pickIndexIdentifierFromOption( 'now', $this->getIndexTypeName() ); $this->availablePlugins = $utils->scanAvailablePlugins( $this->bannedPlugins ); @@ -181,7 +182,7 @@ protected function setConnectionTimeout() { global $wgCirrusSearchMaintenanceTimeout; - Connection::setTimeout( $wgCirrusSearchMaintenanceTimeout ); + $this->getConnection()->setTimeout( $wgCirrusSearchMaintenanceTimeout ); } private function optimize() { @@ -212,7 +213,7 @@ // TODO: only content index for now ( we'll have to check how it works with commons ) - $sourceIndex = Connection::getIndex( $this->indexBaseName, Connection::CONTENT_INDEX_TYPE ); + $sourceIndex = $this->getConnection()->getIndex( $this->indexBaseName, Connection::CONTENT_INDEX_TYPE ); $result = $sourceIndex->search( $query, $scrollOptions ); $totalDocsInIndex = $result->getResponse()->getData(); $totalDocsInIndex = $totalDocsInIndex['hits']['total']; @@ -251,7 +252,7 @@ } public function validateAlias() { - $this->getIndex()->addAlias( Connection::getIndexName( $this->indexBaseName, Connection::TITLE_SUGGEST_TYPE_NAME ), true ); + $this->getIndex()->addAlias( $this->getConnection()->getIndexName( $this->indexBaseName, Connection::TITLE_SUGGEST_TYPE_NAME ), true ); } /** @@ -350,14 +351,14 @@ global $wgCirrusSearchShardCount; $this->outputIndented( "Updating tracking indexes..." ); - $index = Connection::getIndex( 'mw_cirrus_versions' ); + $index = $this->getConnection()->getIndex( 'mw_cirrus_versions' ); if ( !$index->exists() ) { throw new \Exception("mw_cirrus_versions does not exist, you must index your data first"); } list( $aMaj, $aMin ) = explode( '.', \CirrusSearch\Maintenance\SuggesterAnalysisConfigBuilder::VERSION ); list( $mMaj, $mMin ) = explode( '.', \CirrusSearch\Maintenance\SuggesterMappingConfigBuilder::VERSION ); $doc = new \Elastica\Document( - Connection::getIndexName( $this->indexBaseName, $this->indexTypeName ), + $this->getConnection()->getIndexName( $this->indexBaseName, $this->indexTypeName ), array ( 'analysis_maj' => $aMaj, 'analysis_min' => $aMin, @@ -394,7 +395,7 @@ * @return \Elastica\Index being updated */ public function getIndex() { - return Connection::getIndex( $this->indexBaseName, $this->indexTypeName, $this->indexIdentifier ); + return $this->getConnection()->getIndex( $this->indexBaseName, $this->indexTypeName, $this->indexIdentifier ); } public function getType() { @@ -405,14 +406,14 @@ * @return Elastica\Client */ protected function getClient() { - return Connection::getClient(); + return $this->getConnection()->getClient(); } /** * @return string name of the index type being updated */ protected function getIndexTypeName() { - return Connection::getIndexName( $this->indexBaseName, $this->indexTypeName ); + return $this->getConnection()->getIndexName( $this->indexBaseName, $this->indexTypeName ); } } diff --git a/maintenance/updateVersionIndex.php b/maintenance/updateVersionIndex.php index c196a8e..1948980 100644 --- a/maintenance/updateVersionIndex.php +++ b/maintenance/updateVersionIndex.php @@ -2,8 +2,6 @@ namespace CirrusSearch\Maintenance; -use \CirrusSearch\Connection; - /** * Update and check the CirrusSearch version index. * @@ -48,9 +46,9 @@ $this->update( $baseName ); } else { $filter = new \Elastica\Filter\BoolOr(); - foreach ( Connection::getAllIndexTypes() as $type ) { + foreach ( $this->getConnection()->getAllIndexTypes() as $type ) { $term = new \Elastica\Filter\Term(); - $term->setTerm( '_id', Connection::getIndexName( $baseName, $type ) ); + $term->setTerm( '_id', $this->getConnection()->getIndexName( $baseName, $type ) ); $filter->addFilter( $term ); } $this->show( $filter ); @@ -81,9 +79,9 @@ $docs = array(); list( $aMaj, $aMin ) = explode( '.', \CirrusSearch\Maintenance\AnalysisConfigBuilder::VERSION ); list( $mMaj, $mMin ) = explode( '.', \CirrusSearch\Maintenance\MappingConfigBuilder::VERSION ); - foreach( Connection::getAllIndexTypes() as $type ) { + foreach( $this->getConnection()->getAllIndexTypes() as $type ) { $docs[] = new \Elastica\Document( - Connection::getIndexName( $baseName, $type ), + $this->getConnection()->getIndexName( $baseName, $type ), array( 'analysis_maj' => $aMaj, 'analysis_min' => $aMin, @@ -98,7 +96,7 @@ } private function getType() { - $index = Connection::getIndex( 'mw_cirrus_versions' ); + $index = $this->getConnection()->getIndex( 'mw_cirrus_versions' ); if ( !$index->exists() ) { $this->outputIndented( "Creating tracking index..." ); $index->create( array( 'number_of_shards' => 1, diff --git a/tests/jenkins/cleanSetup.php b/tests/jenkins/cleanSetup.php index 2af5c53..38304db 100644 --- a/tests/jenkins/cleanSetup.php +++ b/tests/jenkins/cleanSetup.php @@ -1,7 +1,8 @@ <?php namespace CirrusSearch\Jenkins; -use \Maintenance; + +use CirrusSearch\Maintenance\Maintenance; /** * Calls maintenance scripts properly to get an empty and configured index and @@ -28,6 +29,7 @@ $IP = __DIR__ . '/../../../..'; } require_once( "$IP/maintenance/Maintenance.php" ); +require_once( __DIR__ . "/../../includes/Maintenance/Maintenance.php" ); class CleanSetup extends Maintenance { public function execute() { diff --git a/tests/jenkins/nukeAllIndexes.php b/tests/jenkins/nukeAllIndexes.php index 0ec4145..37e0118 100644 --- a/tests/jenkins/nukeAllIndexes.php +++ b/tests/jenkins/nukeAllIndexes.php @@ -1,8 +1,8 @@ <?php namespace CirrusSearch\Jenkins; -use \CirrusSearch\Connection; -use \Maintenance; + +use CirrusSearch\Maintenance\Maintenance; /** * Removes all indexes from the Elasticsearch cluster so Jenkins' Elasticsearch @@ -29,10 +29,11 @@ $IP = __DIR__ . '/../../../..'; } require_once( "$IP/maintenance/Maintenance.php" ); +require_once( __DIR__ . '/../../includes/Maintenance/Maintenance.php' ); class NukeAllIndexes extends Maintenance { public function execute() { - $client = Connection::getClient(); + $client = $this->getConnection()->getClient(); foreach ( $client->getStatus()->getIndexNames() as $index ) { $this->output( "Deleting $index..." ); $client->getIndex( $index )->delete(); diff --git a/tests/unit/ConnectionTest.php b/tests/unit/ConnectionTest.php index d0aef1f..a163b7e 100644 --- a/tests/unit/ConnectionTest.php +++ b/tests/unit/ConnectionTest.php @@ -2,7 +2,8 @@ namespace CirrusSearch; -use \PHPUnit_Framework_TestCase; +use ConfigFactory; +use PHPUnit_Framework_TestCase; /** * Make sure cirrus doens't break any hooks. @@ -32,7 +33,9 @@ $wgContentNamespaces = $contentNamespaces; $wgCirrusSearchNamespaceMappings = $namespaceMappings; - $this->assertEquals( $expected, Connection::namespacesInIndexType( $indexType ) ); + $config = ConfigFactory::getDefaultInstance()->makeConfig( 'CirrusSearch' ); + $conn = new Connection( $config ); + $this->assertEquals( $expected, $conn->namespacesInIndexType( $indexType ) ); } public static function provideNamespacesInIndexType() { -- To view, visit https://gerrit.wikimedia.org/r/255934 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ib6132f87641c4df83b476deb77f4926cc21b0b14 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/CirrusSearch Gerrit-Branch: REL1_26 Gerrit-Owner: Reedy <re...@wikimedia.org> Gerrit-Reviewer: EBernhardson <ebernhard...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits