From 4f4291342f04d3df91712428d479d27d04703dfb Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Tue, 24 Nov 2026 23:06:43 +0300 Subject: [PATCH 1/2] mon: Optimise from current PGs if last_clean_pgs would move more data last_clean_pgs is only updated when the whole pool is clean, and every re-optimisation (reweight, OSD out, pool change) starts from it. While the pool stays unclean and the current PGs drift away from last_clean_pgs, each round lands on a new layout and moves a large part of the pool again. In that case also optimise from the current PGs and take the result which places fewer PG items on OSDs without their data, i.e. neither the current OSD nor one from the PG history. Ties go to last_clean_pgs, so an OSD which comes back before its PGs recover gets the same PGs. --- mon/pg_gen.js | 65 ++++++++++++++++++----- mon/test-pg-base.js | 123 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+), 14 deletions(-) create mode 100644 mon/test-pg-base.js diff --git a/mon/pg_gen.js b/mon/pg_gen.js index 21cdf68b..2955fe2a 100644 --- a/mon/pg_gen.js +++ b/mon/pg_gen.js @@ -157,6 +157,30 @@ function save_new_pgs_txn(save_to, request, state, etcd_prefix, etcd_watch_revis } } +function get_pool_pgs(pg_config, pool_id) +{ + const pgs = []; + for (const pg in ((pg_config.items||{})[pool_id]||{})) + { + pgs[pg-1] = [ ...pg_config.items[pool_id][pg].osd_set ]; + } + return pgs; +} + +// Count PG items placed on OSDs which don't have their data: neither the current OSD +// nor an OSD from the PG history (OSD sets since the PG was clean the last time) +function count_moved_osds(new_pgs, cur_pgs, pool_history, ordered) +{ + let moved = 0; + for (let i = 0; i < new_pgs.length; i++) + { + const old_sets = [ cur_pgs[i] || [], ...((pool_history[i+1]||{}).osd_sets||[]) ]; + moved += new_pgs[i].filter((osd, j) => osd != LPOptimizer.NO_OSD && + !old_sets.some(set => (ordered ? set[j] == osd : set.some(o => o == osd)))).length; + } + return moved; +} + async function generate_pool_pgs(state, global_config, pool_id, osd_tree, levels) { const pool_cfg = state.config.pools[pool_id]; @@ -175,18 +199,12 @@ async function generate_pool_pgs(state, global_config, pool_id, osd_tree, levels pool_cfg.immediate_commit || global_config.immediate_commit || 'all' ); // First try last_clean_pgs to minimize data movement - let prev_pgs = []; - for (const pg in ((state.history.last_clean_pgs.items||{})[pool_id]||{})) - { - prev_pgs[pg-1] = [ ...state.history.last_clean_pgs.items[pool_id][pg].osd_set ]; - } + let prev_pgs = get_pool_pgs(state.history.last_clean_pgs, pool_id); + const cur_pgs = get_pool_pgs(state.pg.config, pool_id); if (!prev_pgs.length) { // Fall back to pg/config if it's empty - for (const pg in ((state.pg.config.items||{})[pool_id]||{})) - { - prev_pgs[pg-1] = [ ...state.pg.config.items[pool_id][pg].osd_set ]; - } + prev_pgs = get_pool_pgs(state.pg.config, pool_id); } const use_rules = !global_config.use_old_pg_combinator || pool_cfg.level_placement || pool_cfg.raw_placement; const rules = use_rules ? get_pg_rules(pool_id, pool_cfg, global_config.placement_levels) : null; @@ -206,9 +224,7 @@ async function generate_pool_pgs(state, global_config, pool_id, osd_tree, levels pg_minsize: pool_cfg.pg_minsize, ordered: pool_cfg.scheme != 'replicated', }; - let optimize_result; - // Re-shuffle PGs if pg/config.hash is empty - if (old_pg_count > 0 && state.pg.config.hash) + const optimize_from = async (prev_pgs) => { if (prev_pgs.length != pool_cfg.pg_count) { @@ -223,16 +239,37 @@ async function generate_pool_pgs(state, global_config, pool_id, osd_tree, levels pg.push(0); } const folded_prev_pgs = fold_prev_pgs(prev_pgs, folded.leaves); - optimize_result = await LPOptimizer.optimize_change({ + const res = await LPOptimizer.optimize_change({ prev_pgs: folded_prev_pgs, ...optimize_cfg, }); + res.int_pgs = unfold_failure_domains(res.int_pgs, prev_pgs, folded.leaves); + return res; + }; + let optimize_result; + // Re-shuffle PGs if pg/config.hash is empty + if (old_pg_count > 0 && state.pg.config.hash) + { + const clean_differs = JSON.stringify(cur_pgs) != JSON.stringify(prev_pgs); + optimize_result = await optimize_from(prev_pgs); + if (clean_differs && cur_pgs.length == pool_cfg.pg_count) + { + // last_clean_pgs may be far from the current PGs if the pool hasn't been clean for long, + // and then every change would move a lot of PGs again. Start from the current PGs if it moves less + const from_cur = await optimize_from(get_pool_pgs(state.pg.config, pool_id)); + const pool_history = (state.pg.history||{})[pool_id] || {}; + if (count_moved_osds(from_cur.int_pgs, cur_pgs, pool_history, optimize_cfg.ordered) < + count_moved_osds(optimize_result.int_pgs, cur_pgs, pool_history, optimize_cfg.ordered)) + { + optimize_result = from_cur; + } + } } else { optimize_result = await LPOptimizer.optimize_initial(optimize_cfg); + optimize_result.int_pgs = unfold_failure_domains(optimize_result.int_pgs, prev_pgs, folded.leaves); } - optimize_result.int_pgs = unfold_failure_domains(optimize_result.int_pgs, prev_pgs, folded.leaves); const osd_weights = Object.values(pool_tree).reduce((a, c) => { if (c.level === 'osd') { a[c.id] = c.size; } return a; }, {}); optimize_result.space = optimize_result.pg_effsize * LPOptimizer.pg_list_space_efficiency(optimize_result.int_pgs, osd_weights, optimize_cfg.pg_minsize, 1); diff --git a/mon/test-pg-base.js b/mon/test-pg-base.js new file mode 100644 index 00000000..f6bc57e7 --- /dev/null +++ b/mon/test-pg-base.js @@ -0,0 +1,123 @@ +// Copyright (c) Vitaliy Filippov, 2019+ +// License: VNPL-1.1 (see README.md for details) + +// PG re-optimization must not move PGs again on every change while the pool is not clean +// and last_clean_pgs is far from the current PGs. Run: node mon/test-pg-base.js + +const assert = require('assert'); +const { get_osd_tree } = require('./osd_tree.js'); +const { generate_pool_pgs } = require('./pg_gen.js'); + +const HOSTS = 12, OSDS_PER_HOST = 8, PG_COUNT = 256, PG_SIZE = 10; + +function make_state() +{ + const state = { + config: { + pools: { 1: { name: 'ec', scheme: 'ec', pg_size: PG_SIZE, parity_chunks: 2, pg_minsize: 9, + pg_count: PG_COUNT, failure_domain: 'host', max_osd_combinations: 10000 } }, + osd: {}, + }, + osd: { stats: {}, state: {} }, + pg: { config: { items: {} }, history: { 1: {} } }, + history: { last_clean_pgs: { items: {} } }, + }; + for (let osd = 1; osd <= HOSTS*OSDS_PER_HOST; osd++) + { + state.osd.stats[osd] = { host: 'host'+Math.ceil(osd/OSDS_PER_HOST), size: 2**40, time: Date.now()/1000 }; + state.osd.state[osd] = { state: [ 'up', 'in' ] }; + } + return state; +} + +// Re-optimize like the monitor does on a tree change, return the number of PG items +// moved to OSDs which don't have them: neither the current OSD nor one from the PG history +async function recalc(state) +{ + const global_config = { osd_out_time: 600, placement_levels: {} }; + const { osd_tree, levels } = get_osd_tree(global_config, state); + const log = console.log; + console.log = () => {}; + let res; + try + { + res = await generate_pool_pgs(state, global_config, '1', osd_tree, levels); + } + finally + { + console.log = log; + } + const cur = state.pg.config.items[1] || {}; + const history = state.pg.history[1]; + let moved = 0; + const items = {}; + res.pgs.forEach((pg, i) => + { + const osd_set = pg.map(osd => (osd === 'Z' ? 0 : Number(osd))); + const prev = (cur[i+1] || {}).osd_set; + const old_sets = prev ? [ prev, ...((history[i+1] || {}).osd_sets || []) ] : []; + moved += osd_set.filter((osd, j) => osd && !old_sets.some(set => set[j] == osd)).length; + if (prev && prev.join(' ') != osd_set.join(' ')) + { + // The monitor remembers the previous OSD set in the PG history + history[i+1] = { osd_sets: [ ...((history[i+1] || {}).osd_sets || []), prev ] }; + } + items[i+1] = { osd_set }; + }); + state.pg.config = { hash: 'changed', items: { 1: items } }; + return moved; +} + +function mark_clean(state) +{ + state.history.last_clean_pgs = JSON.parse(JSON.stringify(state.pg.config)); + state.pg.history[1] = {}; +} + +function changed_pgs(a, b) +{ + return Object.keys(a).filter(pg => a[pg].osd_set.join(' ') != b[pg].osd_set.join(' ')).length; +} + +async function test_stale_last_clean() +{ + const state = make_state(); + await recalc(state); + mark_clean(state); + // Two OSDs of one host are taken out, PGs move away from them and recover, + // but one PG stays unclean, so last_clean_pgs isn't updated + const [ a, b ] = [ 8*OSDS_PER_HOST+1, 8*OSDS_PER_HOST+2 ]; + state.config.osd[a] = state.config.osd[b] = { reweight: 0 }; + await recalc(state); + state.pg.history[1] = { 1: state.pg.history[1][1] || { osd_sets: [] } }; + // One OSD comes back: only about its share of PG items should move + state.config.osd[a] = { reweight: 1 }; + const moved = await recalc(state); + const share = PG_COUNT*PG_SIZE / (HOSTS*OSDS_PER_HOST); + console.log(`stale last_clean_pgs: ${moved} PG items moved for one OSD back (its share is ${share.toFixed(1)})`); + assert(moved <= 10*share, `one OSD back moved ${moved} PG items, more than 10x its share`); +} + +async function test_short_outage() +{ + const state = make_state(); + await recalc(state); + mark_clean(state); + const clean = state.pg.config.items[1]; + // An OSD goes out and comes back before its PGs recover: it should get the same PGs back + const a = 8*OSDS_PER_HOST+1; + state.config.osd[a] = { reweight: 0 }; + await recalc(state); + state.config.osd[a] = { reweight: 1 }; + await recalc(state); + const changed = changed_pgs(state.pg.config.items[1], clean); + console.log(`short outage: ${changed} PGs differ from the last clean state after the OSD is back`); + assert(changed <= PG_COUNT/20, `${changed} PGs differ from the last clean state`); +} + +(async () => +{ + await test_stale_last_clean(); + await test_short_outage(); + console.log('OK'); +})().catch(e => { console.error(e); process.exit(1); }); -- 2.47.3 From 70614e534d73b57ea1929100a083b424cd28b7f7 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 25 Nov 2026 02:43:39 +0300 Subject: [PATCH 2/2] mon: Also run test-pg-base.js on a replicated pool --- mon/test-pg-base.js | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/mon/test-pg-base.js b/mon/test-pg-base.js index f6bc57e7..1a9dffa0 100644 --- a/mon/test-pg-base.js +++ b/mon/test-pg-base.js @@ -8,14 +8,18 @@ const assert = require('assert'); const { get_osd_tree } = require('./osd_tree.js'); const { generate_pool_pgs } = require('./pg_gen.js'); -const HOSTS = 12, OSDS_PER_HOST = 8, PG_COUNT = 256, PG_SIZE = 10; +const HOSTS = 12, OSDS_PER_HOST = 8, PG_COUNT = 256; +// max_moved: how many shares of one OSD may move when it comes back +const POOLS = { + ec: { pool: { scheme: 'ec', pg_size: 10, parity_chunks: 2, pg_minsize: 9 }, max_moved: 10 }, + replicated: { pool: { scheme: 'replicated', pg_size: 3, pg_minsize: 2 }, max_moved: 3 }, +}; -function make_state() +function make_state(pool) { const state = { config: { - pools: { 1: { name: 'ec', scheme: 'ec', pg_size: PG_SIZE, parity_chunks: 2, pg_minsize: 9, - pg_count: PG_COUNT, failure_domain: 'host', max_osd_combinations: 10000 } }, + pools: { 1: { name: 'test', ...pool, pg_count: PG_COUNT, failure_domain: 'host', max_osd_combinations: 10000 } }, osd: {}, }, osd: { stats: {}, state: {} }, @@ -49,6 +53,7 @@ async function recalc(state) } const cur = state.pg.config.items[1] || {}; const history = state.pg.history[1]; + const ordered = state.config.pools[1].scheme != 'replicated'; let moved = 0; const items = {}; res.pgs.forEach((pg, i) => @@ -56,7 +61,7 @@ async function recalc(state) const osd_set = pg.map(osd => (osd === 'Z' ? 0 : Number(osd))); const prev = (cur[i+1] || {}).osd_set; const old_sets = prev ? [ prev, ...((history[i+1] || {}).osd_sets || []) ] : []; - moved += osd_set.filter((osd, j) => osd && !old_sets.some(set => set[j] == osd)).length; + moved += osd_set.filter((osd, j) => osd && !old_sets.some(set => (ordered ? set[j] == osd : set.includes(osd)))).length; if (prev && prev.join(' ') != osd_set.join(' ')) { // The monitor remembers the previous OSD set in the PG history @@ -79,9 +84,10 @@ function changed_pgs(a, b) return Object.keys(a).filter(pg => a[pg].osd_set.join(' ') != b[pg].osd_set.join(' ')).length; } -async function test_stale_last_clean() +async function test_stale_last_clean(name) { - const state = make_state(); + const { pool, max_moved } = POOLS[name]; + const state = make_state(pool); await recalc(state); mark_clean(state); // Two OSDs of one host are taken out, PGs move away from them and recover, @@ -93,14 +99,14 @@ async function test_stale_last_clean() // One OSD comes back: only about its share of PG items should move state.config.osd[a] = { reweight: 1 }; const moved = await recalc(state); - const share = PG_COUNT*PG_SIZE / (HOSTS*OSDS_PER_HOST); - console.log(`stale last_clean_pgs: ${moved} PG items moved for one OSD back (its share is ${share.toFixed(1)})`); - assert(moved <= 10*share, `one OSD back moved ${moved} PG items, more than 10x its share`); + const share = PG_COUNT*pool.pg_size / (HOSTS*OSDS_PER_HOST); + console.log(`${name}, stale last_clean_pgs: ${moved} PG items moved for one OSD back (its share is ${share.toFixed(1)})`); + assert(moved <= max_moved*share, `${name}: one OSD back moved ${moved} PG items, more than ${max_moved}x its share`); } -async function test_short_outage() +async function test_short_outage(name) { - const state = make_state(); + const state = make_state(POOLS[name].pool); await recalc(state); mark_clean(state); const clean = state.pg.config.items[1]; @@ -111,13 +117,16 @@ async function test_short_outage() state.config.osd[a] = { reweight: 1 }; await recalc(state); const changed = changed_pgs(state.pg.config.items[1], clean); - console.log(`short outage: ${changed} PGs differ from the last clean state after the OSD is back`); - assert(changed <= PG_COUNT/20, `${changed} PGs differ from the last clean state`); + console.log(`${name}, short outage: ${changed} PGs differ from the last clean state after the OSD is back`); + assert(changed <= PG_COUNT/20, `${name}: ${changed} PGs differ from the last clean state`); } (async () => { - await test_stale_last_clean(); - await test_short_outage(); + for (const name in POOLS) + { + await test_stale_last_clean(name); + await test_short_outage(name); + } console.log('OK'); })().catch(e => { console.error(e); process.exit(1); }); -- 2.47.3