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