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