Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ShardAllocationValidator
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 4
110
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 / 27
0.00% covered (danger)
0.00%
0 / 1
56
 fetchActualAllocation
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 set
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace CirrusSearch\Maintenance\Validators;
4
5use CirrusSearch\Maintenance\Printer;
6use Elastica\Index;
7use MediaWiki\Status\Status;
8
9class ShardAllocationValidator extends Validator {
10    /**
11     * @var Index
12     */
13    private $index;
14
15    /**
16     * @var array
17     */
18    private $indexAllocation;
19
20    /**
21     * @param Index $index
22     * @param array $indexAllocation
23     * @param Printer|null $out
24     */
25    public function __construct( Index $index, array $indexAllocation, ?Printer $out = null ) {
26        parent::__construct( $out );
27
28        $this->index = $index;
29        $this->indexAllocation = $indexAllocation;
30    }
31
32    /**
33     * @return Status
34     */
35    public function validate() {
36        $this->outputIndented( "\tValidating shard allocation settings..." );
37
38        $actual = $this->fetchActualAllocation();
39        $changed = false;
40        foreach ( [ 'include', 'exclude', 'require' ] as $type ) {
41            $desired = $this->indexAllocation[$type];
42            if ( $desired ) {
43                $this->output( "\n" );
44                $this->outputIndented( "\t\tUpdating '$type' allocations..." );
45                $this->set( $type, $desired );
46                $this->output( "done" );
47                $changed = true;
48            }
49            if ( isset( $actual[$type] ) ) {
50                $undesired = array_filter( array_keys( $actual[$type] ),
51                    static function ( $key ) use ( $actual, $type, $desired ) {
52                        return $actual[$type][$key] !== '' && !isset( $desired[$key] );
53                    }
54                );
55
56                if ( $undesired ) {
57                    $this->output( "\n" );
58                    $this->outputIndented( "\t\tClearing '$type' allocations..." );
59                    $this->set( $type, array_fill_keys( $undesired, '' ) );
60                    $this->output( "done" );
61                    $changed = true;
62                }
63            }
64        }
65        if ( $changed ) {
66            $this->output( "\n" );
67        } else {
68            $this->output( "done\n" );
69        }
70
71        return Status::newGood();
72    }
73
74    /**
75     * @return array
76     */
77    private function fetchActualAllocation() {
78        $settings = $this->index->getSettings()->get();
79        return $settings['routing']['allocation'] ?? [];
80    }
81
82    /**
83     * @param string $type
84     * @param array $allocation
85     */
86    private function set( $type, $allocation ) {
87        $this->index->getSettings()->set( [
88            'routing' => [
89                'allocation' => [
90                    $type => $allocation,
91                ]
92            ]
93        ] );
94    }
95}