Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CopySearchIndex
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 2
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace CirrusSearch\Maintenance;
4
5use CirrusSearch\SearchConfig;
6
7/**
8 * Copy search index from one cluster to another.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26$IP = getenv( 'MW_INSTALL_PATH' );
27if ( $IP === false ) {
28    $IP = __DIR__ . '/../../..';
29}
30require_once "$IP/maintenance/Maintenance.php";
31require_once __DIR__ . '/../includes/Maintenance/Maintenance.php';
32
33/**
34 * Update the elasticsearch configuration for this index.
35 */
36class CopySearchIndex extends Maintenance {
37    /**
38     * @var string
39     */
40    private $indexSuffix;
41
42    /**
43     * @var string
44     */
45    private $indexBaseName;
46
47    /**
48     * @var int
49     */
50    protected $refreshInterval;
51
52    public function __construct() {
53        parent::__construct();
54        $this->addDescription( "Copy index from one cluster to another.\n" .
55            "The index name and index type should be the same on both clusters." );
56        $this->addOption( 'indexSuffix', 'Source index.  Either content or general.', true, true );
57        $this->addOption( 'targetCluster', 'Target Cluster.', true, true );
58        $this->addOption( 'reindexChunkSize', 'Documents per shard to reindex in a batch.   ' .
59            'Note when changing the number of shards that the old shard size is used, not the new ' .
60            'one.  If you see many errors submitting documents in bulk but the automatic retry as ' .
61            'singles works then lower this number.  Defaults to 100.', false, true );
62        $this->addOption( 'reindexSlices', 'Number of pieces to slice the scan into, roughly ' .
63            'equivilent to concurrency. Defaults to the number of shards', false, true );
64    }
65
66    public function execute() {
67        $this->indexSuffix = $this->getOption( 'indexSuffix' );
68        $this->indexBaseName = $this->getOption( 'baseName',
69            $this->getSearchConfig()->get( SearchConfig::INDEX_BASE_NAME ) );
70
71        $reindexChunkSize = $this->getOption( 'reindexChunkSize', 100 );
72        $targetCluster = $this->getOption( 'targetCluster' );
73        $slices = $this->getOption( 'reindexSlices' );
74
75        $sourceConnection = $this->getConnection();
76        $targetConnection = $this->getConnection( $targetCluster );
77
78        if ( $sourceConnection->getClusterName() == $targetConnection->getClusterName() ) {
79            $this->fatalError( 'Target cluster should be different from current cluster.' );
80        }
81
82        $targetIndexName = $targetConnection->getIndexName( $this->indexBaseName, $this->indexSuffix );
83        $utils = new ConfigUtils( $targetConnection->getClient(), $this );
84        $indexIdentifier = $this->unwrap( $utils->pickIndexIdentifierFromOption(
85            $this->getOption( 'indexIdentifier', 'current' ),
86            $targetIndexName
87        ) );
88
89        $reindexer = new Reindexer(
90            $this->getSearchConfig(),
91            $sourceConnection,
92            $targetConnection,
93            // Target Index
94            $targetConnection->getIndex( $this->indexBaseName, $this->indexSuffix, $indexIdentifier ),
95            // Source Index
96            $this->getConnection()->getIndex( $this->indexBaseName, $this->indexSuffix ),
97            $this
98        );
99        $reindexer->reindex( $slices, 1, $reindexChunkSize );
100        $reindexer->waitForGreen();
101
102        return true;
103    }
104}
105
106$maintClass = CopySearchIndex::class;
107require_once RUN_MAINTENANCE_IF_MAIN;