Make the ordered pass of optimize_change() a function next to
align_pgs(), collect the previous positions with a plain for loop, and
move the new test cases into their own functions.
Build the map of the previous set's members once per PG, place them at
their old positions once, and try only the new items over rotations,
inline in optimize_change() instead of keep_positions() per rotation.
The result is the same. Test the rotation of two new items per PG
(a new host and NO_OSD) and numeric previous sets via optimize_change().
Previous sets read from etcd may hold OSD numbers as JSON numbers, for
example after vitastor-cli rm-osd rewrote them, while the optimizer
returns strings. keep_positions() returned the previous set's items, so
new sets mixed both types and strict comparisons with the stored primary
(includes() in pick_primary) failed for surviving OSDs. Return the new
set's own items instead.
Sets with NO_OSD never pass check_combinations, so surviving OSDs of
undersized PGs did not keep their positions at all; accept the kept
order when the optimizer's own set fails the check too. A rotation of
the new items may break a positional rule: try the other rotations
before falling back to the optimizer's order.
Remove two hosts in the ordered keep-positions test, so that some PGs
get two new OSDs and the rotation of new items runs, and check that
every new set is complete.
Folded failure domains may repeat in a set, so match the previous set by
counts, not by presence: otherwise a repeated item could be kept twice
and another one dropped. Keep the new order only if it still passes the
pool's placement rules, else use the optimizer's order, so positional
rules like host=112233 are not broken by the rotation.
In EC pools the position of an OSD in a PG is the number of the chunk
it holds. optimize_change() takes changed sets from sorted combinations,
so every changed PG came back sorted: OSDs that stayed in the PG changed
positions and their chunks moved too, and after a few re-layouts the
last hosts held only parity chunks. Keep the positions of the OSDs that
stay and put new ones into the vacated positions, rotated by PG number
as optimize_initial() does.