From 1a26f16a124f0f92c807a27ac71ca8aeb6a28950 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Tue, 1 Dec 2026 14:57:21 +0300 Subject: [PATCH 1/2] mon: Let any EC position lead a PG with sticky_primaries OSD sets from optimize_change() come in host order (RuleCombinator is created without `ordered`, only optimize_initial() rotates the sets), and EC primaries are picked from data positions only. So after a host was out and back, a host was added or a weight changed, the last parity_chunks hosts lead almost no PG: with 12 hosts and EC 8+2, hosts 11 and 12 lead none. With sticky_primaries, pick the least loaded live OSD of any position. Turning the option on moves no live primary; the calm rebalance then evens them out. --- docs/config/monitor.en.md | 4 ++++ docs/config/monitor.ru.md | 4 ++++ docs/config/src/monitor.yml | 8 ++++++++ mon/pg_gen.js | 3 ++- mon/test-sticky-primaries.js | 34 ++++++++++++++++++++++++++++++++-- 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/docs/config/monitor.en.md b/docs/config/monitor.en.md index 7e31299d..08ae8a1f 100644 --- a/docs/config/monitor.en.md +++ b/docs/config/monitor.en.md @@ -192,6 +192,10 @@ primary candidates, and give a PG that needs a new primary (because its primary failed or left the PG) to the candidate which is primary for the fewest PGs of all pools. +In EC pools every OSD of the PG is a candidate, parity ones included, so +hosts that come last in OSD sets lead PGs too. A parity primary reads all +data from other OSDs. + By default primaries are picked by PG number and move back as soon as their OSD returns. Every such move restarts the PG on both OSDs, and picking by PG number may leave some OSDs primary for several times more PGs than others. diff --git a/docs/config/monitor.ru.md b/docs/config/monitor.ru.md index d62322d4..22ce656e 100644 --- a/docs/config/monitor.ru.md +++ b/docs/config/monitor.ru.md @@ -195,6 +195,10 @@ OSD перед обновлением агрегированной статис (потому что прежний отказал или вышел из PG), отдавать тому кандидату, который является первичным для наименьшего числа PG всех пулов. +В пулах EC кандидаты — все OSD PG, включая OSD чётности, поэтому узлы, +стоящие последними в наборах OSD, тоже становятся первичными. Первичный OSD +чётности читает все данные с других OSD. + По умолчанию первичные OSD выбираются по номеру PG и возвращаются обратно, как только их OSD снова включается. Каждый такой перенос перезапускает PG на обоих OSD, а выбор по номеру PG может сделать часть OSD первичными для в несколько diff --git a/docs/config/src/monitor.yml b/docs/config/src/monitor.yml index 6458eed2..06cc2059 100644 --- a/docs/config/src/monitor.yml +++ b/docs/config/src/monitor.yml @@ -187,6 +187,10 @@ primary failed or left the PG) to the candidate which is primary for the fewest PGs of all pools. + In EC pools every OSD of the PG is a candidate, parity ones included, so + hosts that come last in OSD sets lead PGs too. A parity primary reads all + data from other OSDs. + By default primaries are picked by PG number and move back as soon as their OSD returns. Every such move restarts the PG on both OSDs, and picking by PG number may leave some OSDs primary for several times more PGs than others. @@ -198,6 +202,10 @@ (потому что прежний отказал или вышел из PG), отдавать тому кандидату, который является первичным для наименьшего числа PG всех пулов. + В пулах EC кандидаты — все OSD PG, включая OSD чётности, поэтому узлы, + стоящие последними в наборах OSD, тоже становятся первичными. Первичный OSD + чётности читает все данные с других OSD. + По умолчанию первичные OSD выбираются по номеру PG и возвращаются обратно, как только их OSD снова включается. Каждый такой перенос перезапускает PG на обоих OSD, а выбор по номеру PG может сделать часть OSD первичными для в несколько diff --git a/mon/pg_gen.js b/mon/pg_gen.js index 5f3b5501..6f43f298 100644 --- a/mon/pg_gen.js +++ b/mon/pg_gen.js @@ -25,7 +25,8 @@ function pick_primary(pool_id, pg_num, pool_config, osd_set, up_osds, aff_osds, else { // Prefer data OSDs for EC because they can actually read something without an additional network hop - const pg_data_size = (pool_config.pg_size||0) - (pool_config.parity_chunks||0); + // Not with counts: OSD sets in host order would leave the last parity_chunks hosts without primaries + const pg_data_size = counts ? osd_set.length : (pool_config.pg_size||0) - (pool_config.parity_chunks||0); alive_set = osd_set.slice(0, pg_data_size).filter(osd_num => osd_num && aff_osds[osd_num]); if (!alive_set.length) alive_set = osd_set.filter(osd_num => osd_num && aff_osds[osd_num]); diff --git a/mon/test-sticky-primaries.js b/mon/test-sticky-primaries.js index f6cc4597..fb7f0831 100644 --- a/mon/test-sticky-primaries.js +++ b/mon/test-sticky-primaries.js @@ -20,7 +20,8 @@ function up_except(down) } // EC 4+2 over 6 hosts, one OSD of each host per PG, primaries picked from scratch -function make_state(pg_count, global_config) +// host_ordered: position h of every set is on host h, as optimize_change() leaves the sets +function make_state(pg_count, global_config, host_ordered = false) { const state = { config: { pools: { 1: { name: 'ec', scheme: 'ec', pg_size: 6, parity_chunks: 2, pg_minsize: 4, @@ -31,7 +32,7 @@ function make_state(pg_count, global_config) { const osd_set = []; for (let h = 0; h < HOSTS; h++) - osd_set.push(host_osds((h + pg) % HOSTS)[(Math.floor(pg / HOSTS) * 7 + h) % PER_HOST]); + osd_set.push(host_osds(host_ordered ? h : (h + pg) % HOSTS)[(Math.floor(pg / HOSTS) * 7 + h) % PER_HOST]); state.pg.config.items[1][pg] = { osd_set, primary: 0 }; } state.pg.config = recheck_primary(state, global_config, up_except([]), {}); @@ -108,6 +109,34 @@ function test_rebalance_when_calm() assert(empty.min == 0 && even.min > 0 && even.max - even.min <= 2, 'the returned host gets primaries again'); } +// OSD sets in host order: the parity hosts lead PGs too, turning the option on moves no primary +// and the calm rebalance then gives the parity hosts their share +function test_host_ordered_sets() +{ + const cfg = { sticky_primaries: true }; + const all_up = up_except([]); + const parity_osds = [ ...host_osds(HOSTS-2), ...host_osds(HOSTS-1) ]; + const fresh = spread(make_state(1024, cfg, true).pg.config, all_up); + assert(fresh.max - fresh.min <= 1, 'primaries picked from scratch differ by at most 1 per OSD'); + const state = make_state(1024, {}, true); + const before = count_primaries(state.pg.config); + assert(parity_osds.every(osd => !before[osd]), 'without the option only data positions lead'); + assert(!recheck_primary(state, cfg, all_up, {}), 'turning the option on moves no primary'); + let passes = 0, moved = 0, cur; + while ((cur = recheck_primary(state, cfg, all_up, {}, {}, null, true))) + { + moved += moves(state.pg.config, cur).length; + state.pg.config = cur; + passes++; + assert(passes < 30, 'rebalance converges'); + } + const even = spread(state.pg.config, all_up); + const after = count_primaries(state.pg.config); + console.log(`host-ordered sets: from scratch max ${fresh.max} min ${fresh.min}; after the option is turned on `+ + `max ${even.max} min ${even.min}, ${moved} primaries moved in ${passes} passes`); + assert(parity_osds.every(osd => after[osd] > 0) && even.max - even.min <= 2, 'parity hosts get primaries'); +} + // New OSD sets only move the primaries that left the set function test_new_sets_keep_primaries() { @@ -145,6 +174,7 @@ const log = console.log; console.log = (s) => /^Moving pool/.exec(s) || log(s); test_host_restart(); test_rebalance_when_calm(); +test_host_ordered_sets(); test_new_sets_keep_primaries(); test_counts_on_pg_count_change(); console.log('OK'); -- 2.47.3 From 990709ec3adfc40993adc911a22384f485494366 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Tue, 1 Dec 2026 15:32:43 +0300 Subject: [PATCH 2/2] mon: Branch on counts next to the replicated check, share the rebalance test loop --- mon/pg_gen.js | 6 +++--- mon/test-sticky-primaries.js | 35 ++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/mon/pg_gen.js b/mon/pg_gen.js index 6f43f298..9b51fc16 100644 --- a/mon/pg_gen.js +++ b/mon/pg_gen.js @@ -15,7 +15,8 @@ const { make_hier_tree, filter_osds_by_root_node, function pick_primary(pool_id, pg_num, pool_config, osd_set, up_osds, aff_osds, cur_primary = 0, counts = null, gained = null) { let alive_set; - if (pool_config.scheme === 'replicated') + // With counts, EC too: OSD sets in host order would leave the last parity_chunks hosts without primaries + if (pool_config.scheme === 'replicated' || counts) { // Prefer "affinity" OSDs alive_set = osd_set.filter(osd_num => osd_num && aff_osds[osd_num]); @@ -25,8 +26,7 @@ function pick_primary(pool_id, pg_num, pool_config, osd_set, up_osds, aff_osds, else { // Prefer data OSDs for EC because they can actually read something without an additional network hop - // Not with counts: OSD sets in host order would leave the last parity_chunks hosts without primaries - const pg_data_size = counts ? osd_set.length : (pool_config.pg_size||0) - (pool_config.parity_chunks||0); + const pg_data_size = (pool_config.pg_size||0) - (pool_config.parity_chunks||0); alive_set = osd_set.slice(0, pg_data_size).filter(osd_num => osd_num && aff_osds[osd_num]); if (!alive_set.length) alive_set = osd_set.filter(osd_num => osd_num && aff_osds[osd_num]); diff --git a/mon/test-sticky-primaries.js b/mon/test-sticky-primaries.js index fb7f0831..125d4d5a 100644 --- a/mon/test-sticky-primaries.js +++ b/mon/test-sticky-primaries.js @@ -83,16 +83,9 @@ function test_host_restart() assert.equal(sticky.back.length, 0, 'a live primary stays when the host returns'); } -// A returned host gets no primaries back until the cluster is calm, then they are evened out, -// each OSD getting at most one new primary per pass -function test_rebalance_when_calm() +// Calm rebalance passes until nothing moves, each OSD getting at most one new primary per pass +function rebalance_until_even(state, cfg, all_up) { - const cfg = { sticky_primaries: true }; - const state = make_state(1024, cfg); - const all_up = up_except([]); - state.pg.config = recheck_primary(state, cfg, up_except(host_osds(4)), {}); - assert(!recheck_primary(state, cfg, all_up, {}), 'nothing moves back by itself'); - const empty = spread(state.pg.config, all_up); let passes = 0, moved = 0, cur; while ((cur = recheck_primary(state, cfg, all_up, {}, true))) { @@ -103,6 +96,20 @@ function test_rebalance_when_calm() passes++; assert(passes < 30, 'rebalance converges'); } + return { passes, moved }; +} + +// A returned host gets no primaries back until the cluster is calm, then they are evened out, +// each OSD getting at most one new primary per pass +function test_rebalance_when_calm() +{ + const cfg = { sticky_primaries: true }; + const state = make_state(1024, cfg); + const all_up = up_except([]); + state.pg.config = recheck_primary(state, cfg, up_except(host_osds(4)), {}); + assert(!recheck_primary(state, cfg, all_up, {}), 'nothing moves back by itself'); + const empty = spread(state.pg.config, all_up); + const { passes, moved } = rebalance_until_even(state, cfg, all_up); const even = spread(state.pg.config, all_up); console.log(`calm rebalance: max ${empty.max} min ${empty.min} -> max ${even.max} min ${even.min}, `+ `${moved} primaries moved in ${passes} passes`); @@ -122,14 +129,8 @@ function test_host_ordered_sets() const before = count_primaries(state.pg.config); assert(parity_osds.every(osd => !before[osd]), 'without the option only data positions lead'); assert(!recheck_primary(state, cfg, all_up, {}), 'turning the option on moves no primary'); - let passes = 0, moved = 0, cur; - while ((cur = recheck_primary(state, cfg, all_up, {}, {}, null, true))) - { - moved += moves(state.pg.config, cur).length; - state.pg.config = cur; - passes++; - assert(passes < 30, 'rebalance converges'); - } + const { passes, moved } = rebalance_until_even(state, cfg, all_up); + assert(moved < 1024 / 2, 'fewer than half of the PGs restart'); const even = spread(state.pg.config, all_up); const after = count_primaries(state.pg.config); console.log(`host-ordered sets: from scratch max ${fresh.max} min ${fresh.min}; after the option is turned on `+ -- 2.47.3