Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExpectedIndices
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 clusterInfo
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 requestedClusters
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 allIndexNames
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace CirrusSearch\Maintenance;
4
5use CirrusSearch\Connection;
6use CirrusSearch\SearchConfig;
7use MediaWiki\WikiMap\WikiMap;
8
9$IP = getenv( 'MW_INSTALL_PATH' );
10if ( $IP === false ) {
11    $IP = __DIR__ . '/../../..';
12}
13require_once "$IP/maintenance/Maintenance.php";
14require_once __DIR__ . '/../includes/Maintenance/Maintenance.php';
15
16/**
17 * Reports index aliases that CirrusSearch owns for this wiki.
18 *
19 * This information can be used as part of a more complete solution to
20 * account for the indices that should exist on an elasticsearch cluster.
21 * The output here is strictly related to the configuration of CirrusSearch
22 * and does not reference state of any live cluster.
23 *
24 * CirrusSearch almost always refers to indices by alias, the only time
25 * when CirrusSearch owns an index without an alias is during index
26 * creation and reindexing. A reasonable proxy to detect this would be
27 * updates in the last few minutes. If CirrusSearch owns an index but
28 * does not have an alias yet it will be under constant indexing load.
29 */
30class ExpectedIndices extends Maintenance {
31
32    public function __construct() {
33        parent::__construct();
34        $this->addDescription( 'Report index alias that CirrusSearch owns.' );
35        $this->addOption( 'oneline', 'Dont pretty print the output', false, false );
36    }
37
38    public function execute() {
39        $clusters = $this->requestedClusters(
40            $this->getOption( 'cluster', null ) );
41        echo \FormatJson::encode( [
42            'dbname' => WikiMap::getCurrentWikiId(),
43            'clusters' => $this->clusterInfo( $clusters ),
44        ], !$this->getOption( 'oneline' ) ), "\n";
45    }
46
47    private function clusterInfo( array $clusters ): array {
48        $config = $this->getSearchConfig();
49        $assignment = $config->getClusterAssignment();
50        $output = [];
51        foreach ( $clusters as $clusterName ) {
52            $output[$clusterName] = [
53                'aliases' => $this->allIndexNames(
54                    Connection::getPool( $config, $clusterName ) ),
55                'group' => $assignment->getCrossClusterName(),
56                // Group should satisfy most automated use cases, server list
57                // is more for debugging or verifying.
58                'connection' => $assignment->getServerList( $clusterName )
59            ];
60        }
61        return $output;
62    }
63
64    private function requestedClusters( ?string $requested ): array {
65        $assignment = $this->getSearchConfig()->getClusterAssignment();
66        if ( $requested !== null ) {
67            return $assignment->hasCluster( $requested )
68                ? [ $requested ]
69                : [];
70        }
71        return $assignment->getAllKnownClusters();
72    }
73
74    private function allIndexNames( Connection $conn ): array {
75        $config = $this->getSearchConfig();
76        $baseName = $config->get( SearchConfig::INDEX_BASE_NAME );
77        $suffixes = $conn->getAllIndexSuffixes( null );
78        if ( $config->isCompletionSuggesterEnabled() ) {
79            $suffixes[] = Connection::TITLE_SUGGEST_INDEX_SUFFIX;
80        }
81        $output = [];
82        foreach ( $suffixes as $indexSuffix ) {
83            $output[] = $conn->getIndexName( $baseName, $indexSuffix );
84        }
85        return $output;
86    }
87}
88
89$maintClass = ExpectedIndices::class;
90require_once RUN_MAINTENANCE_IF_MAIN;