jenkins-bot has submitted this change and it was merged.

Change subject: Rewrite replica handling
......................................................................


Rewrite replica handling

Use auto_expand_replicas. It's way nicer and easier to control.

Change-Id: Ia707b3cabefab5ac5abf002017dc4d4c5f3fd078
---
M CirrusSearch.php
M README
M maintenance/checkIndexes.php
M maintenance/updateOneSearchIndexConfig.php
M maintenance/updateVersionIndex.php
5 files changed, 31 insertions(+), 40 deletions(-)

Approvals:
  Manybubbles: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/CirrusSearch.php b/CirrusSearch.php
index b1b3364..c9ced2a 100644
--- a/CirrusSearch.php
+++ b/CirrusSearch.php
@@ -45,11 +45,12 @@
 // Number of shards for each index
 $wgCirrusSearchShardCount = array( 'content' => 4, 'general' => 4 );
 
-// Number of replicas per shard for each index
-// The default of 0 is fine for single-node setups, but if this is
-// deployed to a multi-node setting you probably at least want these
-// set to 1 for some redundancy, if not 2 for more redundancy.
-$wgCirrusSearchReplicaCount = array( 'content' => 0, 'general' => 0 );
+// Number of replicas Elasticsearch can expand or contract to. This allows for
+// easy development and deployment to a single node (0 replicas) to scale up to
+// higher levels of replication. You if you need more redundancy you could
+// adjust this to '0-10' or '0-all' or even 'false' (string, not boolean) to
+// disable the behavior entirely. The default should be fine for most people.
+$wgCirrusSearchReplicas = '0-2';
 
 // How many seconds must a search of Elasticsearch be before we consider it
 // slow?  Default value is 10 seconds which should be fine for catching the 
rare
@@ -80,8 +81,7 @@
 // mapped to specific index suffixes. The keys are the namespace number, and
 // the value is a string name of what index suffix to use. Changing this 
setting
 // requires a full reindex (not in-place) of the wiki.  If this setting 
contains
-// any values then the index names must also exist in $wgCirrusSearchShardCount
-// and $wgCirrusSearchReplicaCount.
+// any values then the index names must also exist in 
$wgCirrusSearchShardCount.
 $wgCirrusSearchNamespaceMappings = array();
 
 // Extra indexes (if any) you want to search, and for what namespaces?
diff --git a/README b/README
index af38c63..21d9ef5 100644
--- a/README
+++ b/README
@@ -17,8 +17,6 @@
 Configure your search servers in LocalSettings.php if you aren't running 
Elasticsearch on localhost:
  $wgCirrusSearchServers = array( 'elasticsearch0', 'elasticsearch1', 
'elasticsearch2', 'elasticsearch3' );
 There are other $wgCirrusSearch variables that you might want to change from 
their defaults.
-In production setups, you will very likely want to increase the number of 
replicas using
-$wgCirrusSearchReplicaCount.
 
 Now run this script to generate your elasticsearch index:
  php 
$MW_INSTALL_PATH/extensions/CirrusSearch/maintenance/updateSearchIndexConfig.php
@@ -198,15 +196,6 @@
  # Disable ability to shutdown nodes via REST API.
  ##
  action.disable_shutdown: true
-
-
-CirrusSearch
-
-You should change the number of replicas to at least one, two if you want 
protection from crashes
-during upgrades.  More replicas will help distribute queries across more 
nodes.  Having more
-replicas than nodes doesn't make sense and Elasticsearch will stay "yellow" if 
you do that.
-Anyway, you should add this to LocalSettings.php to change it:
- $wgCirrusSearchReplicaCount = array( 'content' => 2, 'general' => 2 );
 
 
 Testing
diff --git a/maintenance/checkIndexes.php b/maintenance/checkIndexes.php
index 15739c4..d85df37 100644
--- a/maintenance/checkIndexes.php
+++ b/maintenance/checkIndexes.php
@@ -47,7 +47,7 @@
                }
                $this->ensureClusterStateFetched();
                $this->ensureCirrusInfoFetched();
-               $this->checkIndex( 'mw_cirrus_versions', 1, 1 );
+               $this->checkIndex( 'mw_cirrus_versions', 1 );
                $aliases = array();
                foreach ( $this->clusterState[ 'metadata' ][ 'indices' ] as 
$indexName => $data ) {
                        foreach ( $data[ 'aliases' ] as $alias ) {
@@ -56,7 +56,7 @@
                }
                foreach ( $this->cirrusInfo as $alias => $data ) {
                        foreach ( $aliases[ $alias ] as $indexName ) {
-                               $this->checkIndex( $indexName, $data[ 
'shard_count'], $data[ 'replica_count' ] );
+                               $this->checkIndex( $indexName, $data[ 
'shard_count'] );
                        }
                }
                $indexCount = count( $this->cirrusInfo );
@@ -77,7 +77,7 @@
                }
        }
 
-       private function checkIndex( $indexName, $expectedShardCount, 
$expectedReplicaCount ) {
+       private function checkIndex( $indexName, $expectedShardCount ) {
                $this->path = array();
                $metdata = $this->getIndexMetadata( $indexName );
                $this->in( $indexName );
@@ -92,7 +92,6 @@
                $this->check( 'shard count', $expectedShardCount, count( 
$routingTable[ 'shards' ] ) );
                foreach ( $routingTable[ 'shards' ] as $shardIndex => 
$shardRoutingTable ) {
                        $this->in( "shard $shardIndex" );
-                       $this->check( 'replica count', $expectedReplicaCount, 
count( $shardRoutingTable ) - 1 );
                        foreach ( $shardRoutingTable as $replicaIndex => 
$replica ) {
                                $this->in( "replica $replicaIndex" );
                                $this->check( 'state', array( 'STARTED', 
'RELOCATING' ), $replica[ 'state' ] );
@@ -187,7 +186,6 @@
                                $data = $r->getData();
                                $this->cirrusInfo[ $r->getId() ] = array(
                                        'shard_count' => $data[ 'shard_count' ],
-                                       'replica_count' => $data[ 
'replica_count' ],
                                );
                        }
                }
diff --git a/maintenance/updateOneSearchIndexConfig.php 
b/maintenance/updateOneSearchIndexConfig.php
index 98d50fc..bf9bd9e 100644
--- a/maintenance/updateOneSearchIndexConfig.php
+++ b/maintenance/updateOneSearchIndexConfig.php
@@ -305,13 +305,13 @@
                                "validate everything else.", 1 );
                }
 
-               $this->output( $this->indent . "\tValidating number of 
replicas..." );
-               $actualReplicaCount = $settings[ 'number_of_replicas' ];
+               $this->output( $this->indent . "\tValidating replica range..." 
);
+               $actualReplicaCount = isset( $settings[ 'auto_expand_replicas' 
] ) ? $settings[ 'auto_expand_replicas' ] : 'false';
                if ( $actualReplicaCount == $this->getReplicaCount() ) {
                        $this->output( "ok\n" );
                } else {
                        $this->output( "is $actualReplicaCount but should be " 
. $this->getReplicaCount() . '...' );
-                       $this->getIndex()->getSettings()->setNumberOfReplicas( 
$this->getReplicaCount() );
+                       $this->getIndex()->getSettings()->set( array( 
'auto_expand_replicas' => $this->getReplicaCount() ) );
                        $this->output( "corrected\n" );
                }
        }
@@ -550,7 +550,7 @@
                                }
                                $this->validateIndexSettings();
                                $this->output( $this->indent . "\tWaiting for 
all shards to start...\n" );
-                               $expectedActive = $this->getShardCount() * ( 1 
+ $this->getReplicaCount() );
+                               list( $lower, $upper ) = explode( '-', 
$this->getReplicaCount() );
                                $indexName = $this->getSpecificIndexName();
                                $path = "_cluster/health/$indexName";
                                $each = 0;
@@ -566,6 +566,17 @@
                                        $relocating = $health[ 
'relocating_shards' ];
                                        $initializing = $health[ 
'initializing_shards' ];
                                        $unassigned = $health[ 
'unassigned_shards' ];
+                                       $nodes = $health['number_of_nodes'];
+                                       if ( $nodes < $lower ) {
+                                               $this->error( "Require $lower 
replicas but only have $nodes nodes. "
+                                                       . "This is almost 
always due to misconfiguration, aborting.", 1 );
+                                       }
+                                       // If the upper range is all, expect 
the upper bound to be the number of nodes
+                                       if ( $upper === 'all' ) {
+                                               $upper = $nodes;
+                                       }
+                                       $expectedReplicas =  min( max( $nodes, 
$lower ), $upper );
+                                       $expectedActive = 
$this->getShardCount() * ( 1 + $expectedReplicas );
                                        if ( $each === 0 || $active === 
$expectedActive ) {
                                                $this->output( $this->indent . 
"\t\tactive:$active/$expectedActive relocating:$relocating " .
                                                        
"initializing:$initializing unassigned:$unassigned\n" );
@@ -831,7 +842,7 @@
                $this->getIndex()->create( array(
                        'settings' => array(
                                'number_of_shards' => $this->getShardCount(),
-                               'number_of_replicas' => 
$this->reindexAndRemoveOk ? 0 : $this->getReplicaCount(),
+                               'auto_expand_replicas' => 
$this->getReplicaCount(),
                                'analysis' => 
$this->analysisConfigBuilder->buildConfig(),
                                'translog.flush_threshold_ops' => 50000,   // 
This is supposed to help with bulk index io load.
                                'index.query.default_field' => 'page.text', // 
Since the _all field is disabled, we should query something.
@@ -930,12 +941,8 @@
        }
 
        private function getReplicaCount() {
-               global $wgCirrusSearchReplicaCount;
-               if ( !isset( $wgCirrusSearchReplicaCount[ $this->indexType ] ) 
) {
-                       $this->error( 'Could not find a replica count for ' . 
$this->indexType . '.  Did you add an index to ' .
-                               '$wgCirrusSearchNamespaceMappings but forget to 
add it to $wgCirrusSearchReplicaCount?', 1 );
-               }
-               return $wgCirrusSearchReplicaCount[ $this->indexType ];
+               global $wgCirrusSearchReplicas;
+               return $wgCirrusSearchReplicas;
        }
 
        private function parsePotentialPercent( $str ) {
diff --git a/maintenance/updateVersionIndex.php 
b/maintenance/updateVersionIndex.php
index 1857bca..a9b7f32 100644
--- a/maintenance/updateVersionIndex.php
+++ b/maintenance/updateVersionIndex.php
@@ -73,14 +73,13 @@
                                        
"{$data['analysis_maj']}.{$data['analysis_min']}\n" .
                                "{$this->indent}  mapping version: " .
                                        
"{$data['mapping_maj']}.{$data['mapping_min']}\n" .
-                               "{$this->indent}  shards: 
{$data['shard_count']}\n" .
-                               "{$this->indent}  replicas: 
{$data['replica_count']}\n"
+                               "{$this->indent}  shards: 
{$data['shard_count']}\n"
                        );
                }
        }
 
        private function update( $baseName ) {
-               global $wgCirrusSearchShardCount, $wgCirrusSearchReplicaCount;
+               global $wgCirrusSearchShardCount;
                $versionType = $this->getType();
                $this->output( "{$this->indent}Updating tracking indexes..." );
                $docs = array();
@@ -95,7 +94,6 @@
                                        'mapping_maj' => $mMaj,
                                        'mapping_min' => $mMin,
                                        'shard_count' => 
$wgCirrusSearchShardCount[ $type ],
-                                       'replica_count' => 
$wgCirrusSearchReplicaCount[ $type ],
                                )
                        );
                }
@@ -108,7 +106,7 @@
                if ( !$index->exists() ) {
                        $this->output( "{$this->indent}Creating tracking 
index..." );
                        $index->create( array( 'number_of_shards' => 1,
-                               'number_of_replicas' => 0, ), true );
+                               'auto_expand_replicas' => '0-2', ), true );
                        $mapping = new \Elastica\Type\Mapping();
                        $mapping->setType( $index->getType( 'version' ) );
                        $mapping->setProperties( array(
@@ -117,7 +115,6 @@
                                'mapping_maj' => array( 'type' => 'long', 
'include_in_all' => false ),
                                'mapping_min' => array( 'type' => 'long', 
'include_in_all' => false ),
                                'shard_count' => array( 'type' => 'long', 
'include_in_all' => false ),
-                               'replica_count' => array( 'type' => 'long', 
'include_in_all' => false ),
                        ) );
                        $mapping->send();
                        $this->output( "done\n" );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia707b3cabefab5ac5abf002017dc4d4c5f3fd078
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: Chad <ch...@wikimedia.org>
Gerrit-Reviewer: Chad <ch...@wikimedia.org>
Gerrit-Reviewer: Manybubbles <never...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to