Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
MaxShardsPerNodeValidator
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 2
12
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
 validate
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace CirrusSearch\Maintenance\Validators;
4
5use CirrusSearch\Maintenance\Printer;
6use Elastica\Index;
7use Status;
8
9class MaxShardsPerNodeValidator extends Validator {
10    /**
11     * @var Index
12     */
13    private $index;
14
15    /**
16     * @var int
17     */
18    private $maxShardsPerNode;
19
20    /**
21     * @param Index $index
22     * @param int $maxShardsPerNode
23     * @param Printer|null $out
24     */
25    public function __construct( Index $index, $maxShardsPerNode, Printer $out = null ) {
26        parent::__construct( $out );
27        $this->index = $index;
28        $this->maxShardsPerNode = $maxShardsPerNode;
29    }
30
31    /**
32     * @return Status
33     */
34    public function validate() {
35        $this->outputIndented( "\tValidating max shards per node..." );
36        $settings = $this->index->getSettings()->get();
37        // Elasticsearch uses negative numbers or an unset value to represent unlimited. We accept 'unlimited'
38        // but it is resolved when reading configuration and not here.
39        $actualMaxShardsPerNode = $settings['routing']['allocation']['total_shards_per_node'] ?? -1;
40        $expectedMaxShardsPerNode = $this->maxShardsPerNode;
41        if ( $actualMaxShardsPerNode == $expectedMaxShardsPerNode ) {
42            $this->output( "ok\n" );
43        } else {
44            $this->output( "is $actualMaxShardsPerNode but should be $expectedMaxShardsPerNode..." );
45            $this->index->getSettings()->set( [
46                'routing.allocation.total_shards_per_node' => $expectedMaxShardsPerNode
47            ] );
48            $this->output( "corrected\n" );
49        }
50
51        return Status::newGood();
52    }
53}