Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.79% covered (warning)
78.79%
26 / 33
66.67% covered (warning)
66.67%
6 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
ClusterSettings
78.79% covered (warning)
78.79%
26 / 33
66.67% covered (warning)
66.67%
6 / 9
21.09
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isPrivateCluster
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getShardCount
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
3.21
 getReplicaCount
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
4.03
 getMaxShardsPerNode
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 isIsolated
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getElasticaWritePartitionCount
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getConnectTimeout
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace CirrusSearch;
4
5/**
6 * Handles resolving configuration variables into specific settings
7 * for a specific cluster.
8 */
9class ClusterSettings {
10
11    /**
12     * @var SearchConfig
13     */
14    protected $config;
15
16    /**
17     * @var string
18     */
19    protected $cluster;
20
21    /**
22     * @param SearchConfig $config
23     * @param string $cluster
24     */
25    public function __construct( SearchConfig $config, $cluster ) {
26        $this->config = $config;
27        $this->cluster = $cluster;
28    }
29
30    public function getName(): string {
31        return $this->cluster;
32    }
33
34    /**
35     * @return bool True when the cluster is allowed to contain private indices
36     */
37    public function isPrivateCluster() {
38        $privateClusters = $this->config->get( 'CirrusSearchPrivateClusters' );
39        if ( $privateClusters === null ) {
40            return true;
41        } else {
42            return in_array( $this->cluster, $privateClusters );
43        }
44    }
45
46    /**
47     * @param string $indexSuffix
48     * @return int Number of shards the index should have
49     */
50    public function getShardCount( $indexSuffix ) {
51        $settings = $this->config->get( 'CirrusSearchShardCount' );
52        if ( isset( $settings[$this->cluster][$indexSuffix] ) ) {
53            return $settings[$this->cluster][$indexSuffix];
54        } elseif ( isset( $settings[$indexSuffix] ) ) {
55            return $settings[$indexSuffix];
56        }
57        throw new \Exception( "Could not find a shard count for "
58            . "{$indexSuffix}. Did you add an index to "
59            . "\$wgCirrusSearchNamespaceMappings but forget to "
60            . "add it to \$wgCirrusSearchShardCount?" );
61    }
62
63    /**
64     * @param string $indexSuffix
65     * @return string Number of replicas Elasticsearch can expand or contract to
66     *  in the format of '0-2' for the minimum and maximum number of replicas. May
67     *  also be the string 'false' when replicas are disabled.
68     */
69    public function getReplicaCount( $indexSuffix ) {
70        $settings = $this->config->get( 'CirrusSearchReplicas' );
71        if ( !is_array( $settings ) ) {
72            return $settings;
73        } elseif ( isset( $settings[$this->cluster][$indexSuffix] ) ) {
74            return $settings[$this->cluster][$indexSuffix];
75        } elseif ( isset( $settings[$indexSuffix] ) ) {
76            return $settings[$indexSuffix];
77        }
78        throw new \Exception( "If \$wgCirrusSearchReplicas is " .
79            "an array it must contain all index types." );
80    }
81
82    /**
83     * @param string $indexSuffix
84     * @return int Number of shards per node, or 'unlimited'.
85     */
86    public function getMaxShardsPerNode( $indexSuffix ) {
87        $settings = $this->config->get( 'CirrusSearchMaxShardsPerNode' );
88        $max = $settings[$this->cluster][$indexSuffix] ?? $settings[$indexSuffix] ?? -1;
89        // Allow convenience setting of 'unlimited' which translates to elasticsearch -1 (unbounded).
90        return $max === 'unlimited' ? -1 : $max;
91    }
92
93    /**
94     * @return bool True when write isolation is configured for this cluster.
95     */
96    public function isIsolated(): bool {
97        $isolate = $this->config->get( 'CirrusSearchWriteIsolateClusters' );
98        return $isolate === null || in_array( $this->cluster, $isolate );
99    }
100
101    /**
102     * @return int Number of partitions the ElasticaWrite job can be split into
103     */
104    public function getElasticaWritePartitionCount(): int {
105        $settings = $this->config->get( 'CirrusSearchElasticaWritePartitionCounts' );
106        return $settings[$this->cluster] ?? 1;
107    }
108
109    /**
110     * @return int Connect timeout to use when initializing connection.
111     * Fallback to 0 (300 sec) if not specified in cirrus config.
112     */
113    public function getConnectTimeout() {
114        $timeout = $this->config->get( 'CirrusSearchClientSideConnectTimeout' );
115        if ( is_int( $timeout ) ) {
116            return $timeout;
117        }
118        // 0 means no timeout (defaults to 300 sec)
119        return $timeout[$this->cluster] ?? 0;
120    }
121}