Matthias Mullie has uploaded a new change for review. https://gerrit.wikimedia.org/r/177223
Change subject: Move validateSpecificAlias code into separate class ...................................................................... Move validateSpecificAlias code into separate class Change-Id: Ie5a52becad8da3a829b56d805d3b221fa3d91837 --- M CirrusSearch.php A includes/Maintenance/Validators/SpecificAliasValidator.php M maintenance/updateOneSearchIndexConfig.php 3 files changed, 160 insertions(+), 74 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CirrusSearch refs/changes/23/177223/1 diff --git a/CirrusSearch.php b/CirrusSearch.php index 13f8ae3..4483828 100644 --- a/CirrusSearch.php +++ b/CirrusSearch.php @@ -586,6 +586,7 @@ $wgAutoloadClasses['CirrusSearch\Maintenance\Validators\AnalyzersValidator'] = $maintenanceDir . '/Validators/AnalyzersValidator.php'; $wgAutoloadClasses['CirrusSearch\Maintenance\Validators\MappingValidator'] = $maintenanceDir . '/Validators/MappingValidator.php'; $wgAutoloadClasses['CirrusSearch\Maintenance\Validators\IndexAliasValidator'] = $maintenanceDir . '/Validators/IndexAliasValidator.php'; +$wgAutoloadClasses['CirrusSearch\Maintenance\Validators\SpecificAliasValidator'] = $maintenanceDir . '/Validators/SpecificAliasValidator.php'; $wgAutoloadClasses['CirrusSearch\Maintenance\UpdateVersionIndex'] = __DIR__ . '/maintenance/updateVersionIndex.php'; $wgAutoloadClasses['CirrusSearch\NearMatchPicker'] = $includes . 'NearMatchPicker.php'; $wgAutoloadClasses['CirrusSearch\OtherIndexes'] = $includes . 'OtherIndexes.php'; diff --git a/includes/Maintenance/Validators/SpecificAliasValidator.php b/includes/Maintenance/Validators/SpecificAliasValidator.php new file mode 100644 index 0000000..90c282a --- /dev/null +++ b/includes/Maintenance/Validators/SpecificAliasValidator.php @@ -0,0 +1,128 @@ +<?php + +namespace CirrusSearch\Maintenance\Validators; + +use CirrusSearch\ElasticsearchIntermediary; +use CirrusSearch\Maintenance\Maintenance; +use CirrusSearch\Maintenance\Reindexer; +use Elastica\Client; +use Elastica\Index; +use RawMessage; +use Status; + +class SpecificAliasValidator extends IndexAliasValidator { + /** + * @var Reindexer + */ + private $reindexer; + + /** + * @var array + */ + private $reindexParams; + + /** + * @var bool + */ + private $reindexAndRemoveOk; + + /** + * @var bool + */ + private $tooFewReplicas; + + /** + * @param Client $client + * @param string $aliasName + * @param string $specificIndexName + * @param bool $startOver + * @param Reindexer $reindexer + * @param array $reindexParams + * @param bool $reindexAndRemoveOk + * @param bool $tooFewReplicas + * @param Maintenance $out + */ + public function __construct( Client $client, $aliasName, $specificIndexName, $startOver, Reindexer $reindexer, array $reindexParams, $reindexAndRemoveOk, $tooFewReplicas, Maintenance $out = null ) { + // @todo: this constructor takes too many arguments - refactor! + + parent::__construct( $client, $aliasName, $specificIndexName, $startOver, $out ); + + $this->reindexer = $reindexer; + $this->reindexParams = $reindexParams; + $this->reindexAndRemoveOk = $reindexAndRemoveOk; + $this->tooFewReplicas = $tooFewReplicas; + } + + /** + * @param array $add + * @param array $remove + * @return Status + */ + protected function updateIndices( array $add, array $remove ) { + if ( !$remove ) { + return $this->updateFreeIndices( $add ); + } + + if ( !$this->reindexAndRemoveOk ) { + $this->output( "cannot correct!\n" ); + return Status::newFatal( new RawMessage( + "The alias is held by another index which means it might be actively serving\n" . + "queries. You can solve this problem by running this program again with\n" . + "--reindexAndRemoveOk. Make sure you understand the consequences of either\n" . + "choice." ) ); + } + + try { + $this->output( "is taken...\n" ); + $this->outputIndented( "\tReindexing...\n" ); + call_user_func_array( array( $this, 'reindexer' ), $this->reindexParams ); + if ( $this->tooFewReplicas ) { + $this->reindexer->optimize(); + // @todo: whoever reviews this: validateIndexSettings used to be called here; I'm now calling it in updateOneSearchIndexConfig *after* this method is executed; is that ok? + $this->reindexer->waitForShards(); + } + } catch ( \Exception $e ) { + return Status::newFatal( new RawMessage( $e->getMessage() ) ); + } + + // now add alias & remove indices for real + $status = Status::newGood(); + $status->merge( $this->swapAliases( $add ) ); + $status->merge( parent::updateIndices( array(), $remove ) ); + return $status; + } + + /** + * @param array $add + * @return Status + */ + public function updateFreeIndices( array $add ) { + $this->output( "alias is free..." ); + + foreach ( $add as $indexName ) { + $index = $this->client->getIndex( $indexName ); + $index->addAlias( $this->aliasName, false ); + } + + $this->output( "corrected\n" ); + + return Status::newGood(); + } + + /** + * @param array $add + * @return Status + */ + public function swapAliases( array $add ) { + $this->outputIndented( "\tSwapping alias..."); + + foreach ( $add as $indexName ) { + $index = $this->client->getIndex( $indexName ); + $index->addAlias( $this->aliasName, true ); + } + + $this->output( "done\n" ); + + return Status::newGood(); + } +} diff --git a/maintenance/updateOneSearchIndexConfig.php b/maintenance/updateOneSearchIndexConfig.php index 5e4e319..5fc9385 100644 --- a/maintenance/updateOneSearchIndexConfig.php +++ b/maintenance/updateOneSearchIndexConfig.php @@ -397,79 +397,40 @@ * Validate the alias that is just for this index's type. */ private function validateSpecificAlias() { - $this->outputIndented( "\tValidating $this->indexType alias..." ); - $otherIndeciesWithAlias = array(); - $specificAliasName = $this->getIndexTypeName(); - $status = $this->getClient()->getStatus(); - if ( $status->indexExists( $specificAliasName ) ) { - $this->output( "is an index..." ); - if ( $this->startOver ) { - $this->getClient()->getIndex( $specificAliasName )->delete(); - $this->output( "index removed..." ); - } else { - $this->output( "cannot correct!\n" ); - $this->error( - "There is currently an index with the name of the alias. Rerun this\n" . - "script with --startOver and it'll remove the index and continue.\n", 1 ); - } - } else { - foreach ( $status->getIndicesWithAlias( $specificAliasName ) as $index ) { - if( $index->getName() === $this->getSpecificIndexName() ) { - $this->output( "ok\n" ); - if ( $this->tooFewReplicas ) { - $this->validateIndexSettings(); - } - return; - } else { - $otherIndeciesWithAlias[] = $index->getName(); - } - } - } - if ( !$otherIndeciesWithAlias ) { - $this->output( "alias is free..." ); - $this->getIndex()->addAlias( $specificAliasName, false ); - $this->output( "corrected\n" ); - if ( $this->tooFewReplicas ) { - $this->validateIndexSettings(); - } - return; - } - if ( $this->reindexAndRemoveOk ) { - global $wgCirrusSearchMaintenanceTimeout; - $reindexer = new Reindexer( - $this->getIndex(), - Connection::getSingleton(), - $this->getPageType(), - $this->getOldPageType(), - $this->getShardCount(), - $this->getReplicaCount(), - $wgCirrusSearchMaintenanceTimeout, - $this->getMergeSettings(), - $this->getMappingConfig(), - $this - ); + global $wgCirrusSearchMaintenanceTimeout; - $this->output( "is taken...\n" ); - $this->outputIndented( "\tReindexing...\n" ); - $reindexer->reindex( $this->reindexProcesses, $this->refreshInterval, $this->reindexRetryAttempts, $this->reindexChunkSize, $this->reindexAcceptableCountDeviation); + $reindexer = new Reindexer( + $this->getIndex(), + Connection::getSingleton(), + $this->getPageType(), + $this->getOldPageType(), + $this->getShardCount(), + $this->getReplicaCount(), + $wgCirrusSearchMaintenanceTimeout, + $this->getMergeSettings(), + $this->getMappingConfig(), + $this + ); - if ( $this->tooFewReplicas ) { - $reindexer->optimize(); - $this->validateIndexSettings(); - $reindexer->waitForShards(); - } - $this->outputIndented( "\tSwapping alias..."); - $this->getIndex()->addAlias( $specificAliasName, true ); - $this->output( "done\n" ); - $this->removeIndecies = $otherIndeciesWithAlias; - return; + $validator = new \CirrusSearch\Maintenance\Validators\SpecificAliasValidator( + $this->getClient(), + $this->getIndexName(), + $this->getSpecificIndexName(), + $this->startOver, + $reindexer, + array( $this->reindexProcesses, $this->refreshInterval, $this->reindexRetryAttempts, $this->reindexChunkSize, $this->reindexAcceptableCountDeviation ), + $this->reindexAndRemoveOk, + $this->tooFewReplicas, + $this + ); + $status = $validator->validate(); + if ( !$status->isOK() ) { + $this->error( $status->getMessage()->text(), 1 ); } - $this->output( "cannot correct!\n" ); - $this->error( - "The alias is held by another index which means it might be actively serving\n" . - "queries. You can solve this problem by running this program again with\n" . - "--reindexAndRemoveOk. Make sure you understand the consequences of either\n" . - "choice.", 1 ); + + if ( $this->tooFewReplicas ) { + $this->validateIndexSettings(); + } } public function validateAllAlias() { @@ -668,10 +629,6 @@ protected function setConnectionTimeout() { global $wgCirrusSearchMaintenanceTimeout; Connection::setTimeout( $wgCirrusSearchMaintenanceTimeout ); - } - - protected function destroySingleton() { - Connection::destroySingleton(); } /** -- To view, visit https://gerrit.wikimedia.org/r/177223 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie5a52becad8da3a829b56d805d3b221fa3d91837 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/CirrusSearch Gerrit-Branch: master Gerrit-Owner: Matthias Mullie <mmul...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits