mon: Add sticky_primaries to keep live primaries and balance new ones #198

Merged
vitalif merged 4 commits from koolkhel/vitastor:mon-primary-spread into master 2026-11-29 14:14:51 +03:00
Contributor

Description

The monitor picks the primary OSD of a PG as alive_set[pg_num % alive_set.length] (pick_primary() in mon/pg_gen.js) and re-picks it on every OSD up/down. Two consequences:

  • When an OSD fails, the rotation index of every PG that lost a member changes, so live primaries move too, and they all move back when the OSD returns. Every move restarts the PG on both OSDs.
  • The rotation is uneven. On 12 hosts x 48 OSDs, EC 8+2, 2048 PGs the busiest OSD is primary for 10-12 PGs against an average of 3.6, and some OSDs for none. After a reweight such an OSD peers all of them one after another.

This adds sticky_primaries, off by default; without it nothing changes.

  • A live primary stays while it is one of the PG's primary candidates (the same candidates as now: affinity OSDs, EC data OSDs first).
  • A PG that needs a new primary (its primary failed, or left the PG or the candidates) gets the candidate which is primary for the fewest PGs of all pools; ties go to the PG number rotation.
  • The counts cover all pools: recheck_primary() keeps its own, apply_pool_pgs() passes one object to every save_new_pgs_txn() call of a change, and a pool whose PG count changes or which is deleted gives its primaries back first.
  • Once the cluster is calm, primaries are evened out (primary_rebalance_time, 300 s by default, 0 disables): when all PGs stay clean and no OSD goes up or down for that long, a live primary moves to the least loaded candidate of its PG if it has at least 2 primaries more. One pass gives each OSD at most one new primary, so a returned OSD does not re-peer a batch of PGs at once; the next pass waits until the cluster is calm again. A monitor that becomes master also waits the full time before its first pass, and an OSD going up or down or the option set to 0 after the timer fired cancels the pass. This also evens out a running cluster after the option is enabled.

Models with this branch (recheck_primary(), save_new_pgs_txn(), and generate_pool_pgs() with lp_solve for the reweight; equal OSD sizes):

without with sticky_primaries
12 x 48 OSDs, EC 8+2, 2048 PGs: primaries per OSD, max / min 10 / 0 4 / 3
the same, every host restarted in turn: primary moves 28694 2409
the same, weight of 2 OSDs of one host 0 -> 1 one after another: most re-peering PGs on one primary 7 4
5 x 16 OSDs, EC 3+2, 256 PGs, every host restarted in turn: primary moves 1028 352

The move counts above are without the calm-time passes. In the test layout (6 x 16 OSDs, EC 4+2, 1024 PGs) a host that failed and returned holds no primaries (max 15 / min 0); the calm-time passes bring it to max 12 / min 10 with 228 moves over 11 passes, about an hour at the default 300 s.

Related: #197 lets the event loop run between peering calculations, #180 shortens one calculation; this one lowers how many of them a single OSD gets.

Found: seen in a run. On a cluster with EC 8+2, 2048 PGs over 576 OSDs and about 850k objects per PG, a weight change made OSDs primary for up to 11 re-peering PGs at once. Each calculation took 1-3 s, and the series kept the OSD from answering pings, so peers dropped it and the PGs peered again.

Tested:

  • node mon/test-sticky-primaries.js: without the option, the old rotation and its move back; with it, an even spread from scratch, a failed host moves only its own primaries and nothing moves back when it returns, new OSD sets move only the primaries that left the set, exact counts after a PG count change and a pool deletion, and calm-time passes that give a returned host its primaries back with at most one new primary per OSD per pass (fails without that limit: one OSD gets 11 in one pass).
  • eslint with mon/.eslintrc.js on the changed files.

Contributor Name

Yury Luneff (MIND Software LLC, https://mindsw.io)

CLA

### Description The monitor picks the primary OSD of a PG as `alive_set[pg_num % alive_set.length]` (`pick_primary()` in mon/pg_gen.js) and re-picks it on every OSD up/down. Two consequences: - When an OSD fails, the rotation index of every PG that lost a member changes, so live primaries move too, and they all move back when the OSD returns. Every move restarts the PG on both OSDs. - The rotation is uneven. On 12 hosts x 48 OSDs, EC 8+2, 2048 PGs the busiest OSD is primary for 10-12 PGs against an average of 3.6, and some OSDs for none. After a reweight such an OSD peers all of them one after another. This adds `sticky_primaries`, off by default; without it nothing changes. - A live primary stays while it is one of the PG's primary candidates (the same candidates as now: affinity OSDs, EC data OSDs first). - A PG that needs a new primary (its primary failed, or left the PG or the candidates) gets the candidate which is primary for the fewest PGs of all pools; ties go to the PG number rotation. - The counts cover all pools: `recheck_primary()` keeps its own, `apply_pool_pgs()` passes one object to every `save_new_pgs_txn()` call of a change, and a pool whose PG count changes or which is deleted gives its primaries back first. - Once the cluster is calm, primaries are evened out (`primary_rebalance_time`, 300 s by default, 0 disables): when all PGs stay clean and no OSD goes up or down for that long, a live primary moves to the least loaded candidate of its PG if it has at least 2 primaries more. One pass gives each OSD at most one new primary, so a returned OSD does not re-peer a batch of PGs at once; the next pass waits until the cluster is calm again. A monitor that becomes master also waits the full time before its first pass, and an OSD going up or down or the option set to 0 after the timer fired cancels the pass. This also evens out a running cluster after the option is enabled. Models with this branch (`recheck_primary()`, `save_new_pgs_txn()`, and `generate_pool_pgs()` with lp_solve for the reweight; equal OSD sizes): | | without | with `sticky_primaries` | |---|---|---| | 12 x 48 OSDs, EC 8+2, 2048 PGs: primaries per OSD, max / min | 10 / 0 | 4 / 3 | | the same, every host restarted in turn: primary moves | 28694 | 2409 | | the same, weight of 2 OSDs of one host 0 -> 1 one after another: most re-peering PGs on one primary | 7 | 4 | | 5 x 16 OSDs, EC 3+2, 256 PGs, every host restarted in turn: primary moves | 1028 | 352 | The move counts above are without the calm-time passes. In the test layout (6 x 16 OSDs, EC 4+2, 1024 PGs) a host that failed and returned holds no primaries (max 15 / min 0); the calm-time passes bring it to max 12 / min 10 with 228 moves over 11 passes, about an hour at the default 300 s. Related: #197 lets the event loop run between peering calculations, #180 shortens one calculation; this one lowers how many of them a single OSD gets. Found: seen in a run. On a cluster with EC 8+2, 2048 PGs over 576 OSDs and about 850k objects per PG, a weight change made OSDs primary for up to 11 re-peering PGs at once. Each calculation took 1-3 s, and the series kept the OSD from answering pings, so peers dropped it and the PGs peered again. Tested: - `node mon/test-sticky-primaries.js`: without the option, the old rotation and its move back; with it, an even spread from scratch, a failed host moves only its own primaries and nothing moves back when it returns, new OSD sets move only the primaries that left the set, exact counts after a PG count change and a pool deletion, and calm-time passes that give a returned host its primaries back with at most one new primary per OSD per pass (fails without that limit: one OSD gets 11 in one pass). - eslint with mon/.eslintrc.js on the changed files. ### Contributor Name Yury Luneff (MIND Software LLC, https://mindsw.io) ### CLA - [x] I accept Vitastor CLA agreement: https://git.yourcmc.ru/vitalif/vitastor/src/branch/master/CLA-en.md
koolkhel added 1 commit 2026-11-28 00:46:25 +03:00
The monitor picks the primary OSD of a PG by PG number among the alive
candidates and re-picks it on every OSD up/down. So when an OSD fails,
live primaries of PGs that lost a member move too, and they all move
back when it returns; every move restarts the PG on both OSDs. The
rotation is also uneven: some OSDs become primary for several times
more PGs than the average and peer them one after another.

With sticky_primaries, a live primary stays while it is one of the PG's
candidates, and a PG that needs a new primary gets the candidate which
is primary for the fewest PGs of all pools, ties by PG number. The
counts are shared by all pools of one change. Off by default.
koolkhel added 1 commit 2026-11-28 01:13:47 +03:00
With sticky_primaries a returned OSD got primaries again only when other
PGs needed a new one. Once all PGs stay clean and no OSD goes up or down
for primary_rebalance_time (300 s by default, 0 disables), move a live
primary to the least loaded candidate of its PG when it has at least 2
primaries more. One pass gives each OSD at most one new primary, so no
OSD re-peers a batch of PGs at once; the next pass waits until the
cluster is calm again.
koolkhel added 1 commit 2026-11-28 01:32:06 +03:00
A monitor that took over a clean cluster never armed the rebalance
timer, an OSD could go down between the timer and the pass, and setting
primary_rebalance_time to 0 did not cancel an armed pass. Check calm in
one place, when the timer fires and again when the pass runs, and run
save_last_clean() on start.
koolkhel added 1 commit 2026-11-28 03:13:20 +03:00
A standby monitor sees no changes, so on takeover it could run a pass at
once, and a pass could follow another before the moved PGs reported
their state. Restart the wait when the monitor becomes master and when a
pass runs. Re-arm the timer when the pass lands on a re-optimize round
instead of dropping it, and treat a negative value as 0.
vitalif merged commit 00bd701a97 into master 2026-11-29 14:14:51 +03:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: vitalif/vitastor#198
No description provided.