Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 44 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
CopySearchIndex | |
0.00% |
0 / 37 |
|
0.00% |
0 / 2 |
12 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Maintenance; |
4 | |
5 | use 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' ); |
27 | if ( $IP === false ) { |
28 | $IP = __DIR__ . '/../../..'; |
29 | } |
30 | require_once "$IP/maintenance/Maintenance.php"; |
31 | require_once __DIR__ . '/../includes/Maintenance/Maintenance.php'; |
32 | |
33 | /** |
34 | * Update the elasticsearch configuration for this index. |
35 | */ |
36 | class 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 | /** @inheritDoc */ |
67 | public function execute() { |
68 | $this->indexSuffix = $this->getOption( 'indexSuffix' ); |
69 | $this->indexBaseName = $this->getOption( 'baseName', |
70 | $this->getSearchConfig()->get( SearchConfig::INDEX_BASE_NAME ) ); |
71 | |
72 | $reindexChunkSize = $this->getOption( 'reindexChunkSize', 100 ); |
73 | $targetCluster = $this->getOption( 'targetCluster' ); |
74 | $slices = $this->getOption( 'reindexSlices' ); |
75 | |
76 | $sourceConnection = $this->getConnection(); |
77 | $targetConnection = $this->getConnection( $targetCluster ); |
78 | |
79 | if ( $sourceConnection->getClusterName() == $targetConnection->getClusterName() ) { |
80 | $this->fatalError( 'Target cluster should be different from current cluster.' ); |
81 | } |
82 | |
83 | $targetIndexName = $targetConnection->getIndexName( $this->indexBaseName, $this->indexSuffix ); |
84 | $utils = new ConfigUtils( $targetConnection->getClient(), $this ); |
85 | $indexIdentifier = $this->unwrap( $utils->pickIndexIdentifierFromOption( |
86 | $this->getOption( 'indexIdentifier', 'current' ), |
87 | $targetIndexName |
88 | ) ); |
89 | |
90 | $reindexer = new Reindexer( |
91 | $this->getSearchConfig(), |
92 | $sourceConnection, |
93 | $targetConnection, |
94 | // Target Index |
95 | $targetConnection->getIndex( $this->indexBaseName, $this->indexSuffix, $indexIdentifier ), |
96 | // Source Index |
97 | $this->getConnection()->getIndex( $this->indexBaseName, $this->indexSuffix ), $this, [], [] |
98 | ); |
99 | $reindexer->reindex( $slices, 1, $reindexChunkSize ); |
100 | $reindexer->waitForGreen(); |
101 | |
102 | return true; |
103 | } |
104 | } |
105 | |
106 | $maintClass = CopySearchIndex::class; |
107 | require_once RUN_MAINTENANCE_IF_MAIN; |