Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 56 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| ExpectedIndicesBuilder | |
0.00% |
0 / 56 |
|
0.00% |
0 / 7 |
342 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| build | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| clusterInfo | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| requestedClusters | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| allIndexNames | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| shardCounts | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| getAlternativeIndexNames | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace CirrusSearch\Maintenance; |
| 4 | |
| 5 | use CirrusSearch\AlternativeIndices; |
| 6 | use CirrusSearch\ClusterSettings; |
| 7 | use CirrusSearch\Connection; |
| 8 | use CirrusSearch\SearchConfig; |
| 9 | |
| 10 | class ExpectedIndicesBuilder { |
| 11 | private SearchConfig $searchConfig; |
| 12 | private AlternativeIndices $alternativeIndices; |
| 13 | |
| 14 | public function __construct( SearchConfig $searchConfig ) { |
| 15 | $this->searchConfig = $searchConfig; |
| 16 | $this->alternativeIndices = AlternativeIndices::build( $searchConfig ); |
| 17 | } |
| 18 | |
| 19 | public function build( bool $withConnectionInfo, ?string $cluster ): array { |
| 20 | $clusters = $this->requestedClusters( $cluster ); |
| 21 | return [ |
| 22 | 'dbname' => $this->searchConfig->getWikiId(), |
| 23 | 'clusters' => $this->clusterInfo( $withConnectionInfo, $clusters ), |
| 24 | ]; |
| 25 | } |
| 26 | |
| 27 | private function clusterInfo( bool $withConnectionInfo, array $clusters ): array { |
| 28 | $assignment = $this->searchConfig->getClusterAssignment(); |
| 29 | $output = []; |
| 30 | foreach ( $clusters as $clusterName ) { |
| 31 | $connection = Connection::getPool( $this->searchConfig, $clusterName ); |
| 32 | $info = [ |
| 33 | 'aliases' => $this->allIndexNames( $connection ), |
| 34 | 'shard_count' => $this->shardCounts( $connection ), |
| 35 | 'group' => $assignment->getCrossClusterName(), |
| 36 | ]; |
| 37 | if ( $withConnectionInfo ) { |
| 38 | // Group should satisfy most automated use cases, server list |
| 39 | // is more for debugging or verifying. |
| 40 | $info['connection'] = $assignment->getServerList( $clusterName ); |
| 41 | } |
| 42 | $output[$clusterName] = $info; |
| 43 | } |
| 44 | return $output; |
| 45 | } |
| 46 | |
| 47 | private function requestedClusters( ?string $requested ): array { |
| 48 | $assignment = $this->searchConfig->getClusterAssignment(); |
| 49 | if ( $requested !== null ) { |
| 50 | return $assignment->canManageCluster( $requested ) |
| 51 | ? [ $requested ] |
| 52 | : []; |
| 53 | } |
| 54 | return $assignment->getManagedClusters(); |
| 55 | } |
| 56 | |
| 57 | private function allIndexNames( Connection $conn ): array { |
| 58 | $baseName = $this->searchConfig->get( SearchConfig::INDEX_BASE_NAME ); |
| 59 | $suffixes = $conn->getAllIndexSuffixes( null ); |
| 60 | if ( $this->searchConfig->isCompletionSuggesterEnabled() ) { |
| 61 | $suffixes[] = Connection::TITLE_SUGGEST_INDEX_SUFFIX; |
| 62 | } |
| 63 | $output = []; |
| 64 | foreach ( $suffixes as $indexSuffix ) { |
| 65 | $output[] = $conn->getIndexName( $baseName, $indexSuffix ); |
| 66 | } |
| 67 | return array_merge( $output, array_keys( $this->getAlternativeIndexNames( $conn ) ) ); |
| 68 | } |
| 69 | |
| 70 | private function shardCounts( Connection $conn ): array { |
| 71 | $baseName = $this->searchConfig->get( SearchConfig::INDEX_BASE_NAME ); |
| 72 | $suffixes = $conn->getAllIndexSuffixes( null ); |
| 73 | if ( $this->searchConfig->isCompletionSuggesterEnabled() ) { |
| 74 | $suffixes[] = Connection::TITLE_SUGGEST_INDEX_SUFFIX; |
| 75 | } |
| 76 | |
| 77 | $output = []; |
| 78 | foreach ( $suffixes as $indexSuffix ) { |
| 79 | $index = $conn->getIndexName( $baseName, $indexSuffix ); |
| 80 | $output[$index] = $conn->getSettings()->getShardCount( $indexSuffix ); |
| 81 | } |
| 82 | foreach ( $this->getAlternativeIndexNames( $conn ) as $name => $typeAndConn ) { |
| 83 | $output[$name] = $typeAndConn['settings']->getShardCount( $typeAndConn['type'] ); |
| 84 | } |
| 85 | return $output; |
| 86 | } |
| 87 | |
| 88 | private function getAlternativeIndexNames( Connection $conn ): array { |
| 89 | if ( !$this->searchConfig->isCompletionSuggesterEnabled() ) { |
| 90 | return []; |
| 91 | } |
| 92 | $altIndices = $this->alternativeIndices->getAlternativeIndices( AlternativeIndices::COMPLETION ); |
| 93 | $indices = []; |
| 94 | foreach ( $altIndices as $index ) { |
| 95 | $aliasName = $index->getIndex( $conn )->getName(); |
| 96 | $indices[$aliasName] = [ |
| 97 | "type" => Connection::TITLE_SUGGEST_INDEX_SUFFIX, |
| 98 | "settings" => new ClusterSettings( $index->getConfig(), $conn->getClusterName() ) |
| 99 | ]; |
| 100 | } |
| 101 | return $indices; |
| 102 | } |
| 103 | |
| 104 | } |