Matthias Mullie has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/176946

Change subject: Move validateAllAlias code into separate class
......................................................................

Move validateAllAlias code into separate class

This has a minor change in functionality:

It contains code from removeOldIndeciesIfRequired already, while that's
also still in the maintenance script. Goal is for validateSpecificAlias
to also move into a Validator class, that extends from this new
Validator class.
In maintenance script, both (AllAlias & SpecificAlias) were run, and
stuff in $this->removeIndecies was cleaned up after both were run. If
we move both into separate objects, the $this->removeIndecies alternative
will be run twice, right after each has run.

Change-Id: Idbbd403b8190ae832a6b15734adb25b71d43b534
---
M CirrusSearch.php
A includes/Maintenance/Validators/IndexAliasValidator.php
M maintenance/updateOneSearchIndexConfig.php
3 files changed, 139 insertions(+), 37 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CirrusSearch 
refs/changes/46/176946/1

diff --git a/CirrusSearch.php b/CirrusSearch.php
index 55a81ae..e81cb43 100644
--- a/CirrusSearch.php
+++ b/CirrusSearch.php
@@ -584,6 +584,7 @@
 
$wgAutoloadClasses['CirrusSearch\Maintenance\Validators\ReplicaRangeValidator'] 
= $maintenanceDir . '/Validators/ReplicaRangeValidator.php';
 $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\UpdateVersionIndex'] = __DIR__ . 
'/maintenance/updateVersionIndex.php';
 $wgAutoloadClasses['CirrusSearch\NearMatchPicker'] = $includes . 
'NearMatchPicker.php';
 $wgAutoloadClasses['CirrusSearch\OtherIndexes'] = $includes . 
'OtherIndexes.php';
diff --git a/includes/Maintenance/Validators/IndexAliasValidator.php 
b/includes/Maintenance/Validators/IndexAliasValidator.php
new file mode 100644
index 0000000..9e9604a
--- /dev/null
+++ b/includes/Maintenance/Validators/IndexAliasValidator.php
@@ -0,0 +1,131 @@
+<?php
+
+namespace CirrusSearch\Maintenance\Validators;
+
+use CirrusSearch\Maintenance\Maintenance;
+use Elastica\Client;
+use RawMessage;
+use Status;
+
+class IndexAliasValidator extends Validator {
+       /**
+        * @var Client
+        */
+       protected $client;
+
+       /**
+        * @var string
+        */
+       protected $aliasName;
+
+       /**
+        * @var string
+        */
+       protected $specificIndexName;
+
+       /**
+        * @var bool
+        */
+       private $startOver;
+
+       /**
+        * @var array
+        */
+       protected $create = array();
+
+       /**
+        * @var array
+        */
+       protected $remove = array();
+
+       /**
+        * @param Client $client
+        * @param string $aliasName
+        * @param string $specificIndexName
+        * @param bool $startOver
+        * @param Maintenance $out
+        */
+       public function __construct( Client $client, $aliasName, 
$specificIndexName, $startOver, Maintenance $out = null ) {
+               parent::__construct( $out );
+
+               $this->client = $client;
+               $this->aliasName = $aliasName;
+               $this->specificIndexName = $specificIndexName;
+               $this->startOver = $startOver;
+       }
+
+       /**
+        * @return Status
+        */
+       public function validate() {
+               // arrays of aliases to be added/removed
+               $add = $remove = array();
+
+               $this->outputIndented( "\tValidating $this->aliasName alias..." 
);
+               $status = $this->client->getStatus();
+               if ( $status->indexExists( $this->aliasName ) ) {
+                       $this->output( "is an index..." );
+                       if ( $this->startOver ) {
+                               $this->client->getIndex( $this->aliasName 
)->delete();
+                               $this->output( "index removed..." );
+
+                               $add[] = $this->specificIndexName;
+                       } else {
+                               $this->output( "cannot correct!\n" );
+                               return Status::newFatal( new RawMessage(
+                                       "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" ) );
+                       }
+               } else {
+                       foreach ( $status->getIndicesWithAlias( 
$this->aliasName ) as $index ) {
+                               if ( $index->getName() === 
$this->specificIndexName ) {
+                                       $this->output( "ok\n" );
+                                       return Status::newGood();
+                               } else {
+                                       $remove[] = $index->getName();
+                               }
+                       }
+
+                       $add[] = $this->specificIndexName;
+               }
+
+               return $this->updateIndices( $add, $remove );
+       }
+
+       /**
+        * @param string[] $add Array of indices to add
+        * @param string[] $remove Array of indices to remove
+        * @return Status
+        */
+       protected function updateIndices( array $add, array $remove ) {
+               $data = array();
+
+               $this->output( "alias not already assigned to this index..." );
+
+               // We'll remove the all alias from the indices that we're about 
to delete while
+               // we add it to this index.  Elastica doesn't support this well 
so we have to
+               // build the request to Elasticsearch ourselves.
+
+               foreach ( $add as $indexName ) {
+                       $data['action'][] = array( 'add' => array( 'index' => 
$indexName, 'alias' => $this->aliasName ) );
+               }
+
+               foreach ( $remove as $indexName ) {
+                       $data['action'][] = array( 'remove' => array( 'index' 
=> $indexName, 'alias' => $this->aliasName ) );
+               }
+
+               $this->client->request( '_aliases', \Elastica\Request::POST, 
$data );
+               $this->output( "corrected\n" );
+
+               if ( $remove ) {
+                       $this->outputIndented( "\tRemoving old indices...\n" );
+                       foreach ( $remove as $indexName ) {
+                               $this->outputIndented( "\t\t$indexName..." );
+                               $this->client->getIndex( $indexName )->delete();
+                               $this->output( "done\n" );
+                       }
+               }
+
+               return Status::newGood();
+       }
+}
diff --git a/maintenance/updateOneSearchIndexConfig.php 
b/maintenance/updateOneSearchIndexConfig.php
index 1111035..86c3b76 100644
--- a/maintenance/updateOneSearchIndexConfig.php
+++ b/maintenance/updateOneSearchIndexConfig.php
@@ -504,45 +504,15 @@
        }
 
        public function validateAllAlias() {
-               $this->outputIndented( "\tValidating all alias..." );
-               $allAliasName = $this->getIndexName();
-               $status = $this->getClient()->getStatus();
-               if ( $status->indexExists( $allAliasName ) ) {
-                       $this->output( "is an index..." );
-                       if ( $this->startOver ) {
-                               $this->getClient()->getIndex( $allAliasName 
)->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 );
-                               return;
-                       }
-               } else {
-                       foreach ( $status->getIndicesWithAlias( $allAliasName ) 
as $index ) {
-                               if( $index->getName() === 
$this->getSpecificIndexName() ) {
-                                       $this->output( "ok\n" );
-                                       return;
-                               }
-                       }
+               $validator = new 
\CirrusSearch\Maintenance\Validators\IndexAliasValidator( $this->getClient(), 
$this->getIndexName(), $this->getSpecificIndexName(), $this->startOver, $this );
+               $status = $validator->validate();
+               if ( !$status->isOK() ) {
+                       $this->error( $status->getMessage()->text(), 1 );
                }
-               $this->output( "alias not already assigned to this index..." );
-               // We'll remove the all alias from the indecies that we're 
about to delete while
-               // we add it to this index.  Elastica doesn't support this well 
so we have to
-               // build the request to Elasticsearch ourselves.
-               $data = array(
-                       'action' => array(
-                               array( 'add' => array( 'index' => 
$this->getSpecificIndexName(), 'alias' => $allAliasName ) )
-                       )
-               );
-               if ( $this->removeIndecies ) {
-                       foreach ( $this->removeIndecies as $oldIndex ) {
-                               $data['action'][] = array( 'remove' => array( 
'index' => $oldIndex, 'alias' => $allAliasName ) );
-                       }
+
+               if ( $this->tooFewReplicas ) {
+                       $this->validateIndexSettings();
                }
-               $this->getClient()->request( '_aliases', 
\Elastica\Request::POST, $data );
-               $this->output( "corrected\n" );
        }
 
        public function removeOldIndeciesIfRequired() {

-- 
To view, visit https://gerrit.wikimedia.org/r/176946
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idbbd403b8190ae832a6b15734adb25b71d43b534
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: Matthias Mullie <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to