diff --git a/mon/lp_optimizer/lp_optimizer.js b/mon/lp_optimizer/lp_optimizer.js index c5bc8d79..513c1b74 100644 --- a/mon/lp_optimizer/lp_optimizer.js +++ b/mon/lp_optimizer/lp_optimizer.js @@ -237,20 +237,13 @@ function calc_intersect_weights(old_pg_size, pg_size, pg_count, prev_weights, al return move_weights; } -// Try to minimize data movement -async function optimize_change({ prev_pgs: prev_int_pgs, osd_weights, combinator, pg_size = 3, pg_minsize = 2, parity_space = 1, ordered = false }) +// Preprocess old PGs (stats, rule check, and idempotent shortcut) +function prepare_optimize_change({ prev_int_pgs, osd_weights, combinator, pg_size, pg_minsize, parity_space, total_weight }) { - if (!osd_weights) - { - return null; - } - // FIXME: use parity_chunks with parity_space instead of pg_minsize - let all_pgs = make_cyclic(combinator.random_combinations(), parity_space); - let pg_effsize = Object.values(all_pgs).reduce((a, c) => Math.max(a, c.filter(e => e != NO_OSD).length), 0); - pg_effsize = Math.min(pg_minsize, pg_effsize) + Math.max(0, Math.min(pg_size, pg_effsize) - pg_minsize) * parity_space; - const pg_count = prev_int_pgs.length; + // 1. Old layout stats & rule check: collect prev_weights / prev_pg_per_osd / cur_osd_load and validate still_valid once const prev_weights = {}; const prev_pg_per_osd = {}; + const cur_osd_load = {}; for (const pg of prev_int_pgs) { const pg_name = 'pg_'+pg.join('_'); @@ -258,15 +251,92 @@ async function optimize_change({ prev_pgs: prev_int_pgs, osd_weights, combinator for (let i = 0; i < pg.length; i++) { const osd = pg[i]; + const slot_weight = (i >= pg_minsize ? parity_space : 1); prev_pg_per_osd[osd] = prev_pg_per_osd[osd] || []; - prev_pg_per_osd[osd].push([ pg_name, (i >= pg_minsize ? parity_space : 1) ]); + prev_pg_per_osd[osd].push([ pg_name, slot_weight ]); + cur_osd_load[osd] = (cur_osd_load[osd] || 0) + slot_weight; } } - const old_pg_size = prev_int_pgs[0].length; + const old_pg_size = prev_int_pgs.length ? prev_int_pgs[0].length : pg_size; + // Check deduplicated old combinations from prev_weights instead of full prev_int_pgs to avoid redundant rule checks + const still_valid = (old_pg_size == pg_size) + ? combinator.check_combinations(Object.keys(prev_weights).map(pg_name => pg_name.substr(3).split('_'))) + : []; + // 2. Idempotent shortcut: return immediately if all old PGs are valid, all OSDs are live, and load is within tolerance + let shortcut = null; + const pg_count = prev_int_pgs.length; + if (pg_count && total_weight > 0 && still_valid.length === Object.keys(prev_weights).length) + { + let can_shortcut = true; + for (const osd in prev_pg_per_osd) + { + if (osd !== NO_OSD && !(Number(osd_weights[osd]) > 0)) + { + can_shortcut = false; + break; + } + } + if (can_shortcut) + { + const max_slots = prev_int_pgs.reduce((a, c) => Math.max(a, c.filter(e => e != NO_OSD).length), 0); + const pg_effsize = Math.min(pg_minsize, max_slots) + Math.max(0, Math.min(pg_size, max_slots) - pg_minsize) * parity_space; + for (const osd in osd_weights) + { + if (!(Number(osd_weights[osd]) > 0)) + { + continue; + } + const cur_l = cur_osd_load[osd] || 0; + const tgt_l = osd_weights[osd] * pg_effsize / total_weight * pg_count; + if (Math.abs(cur_l - tgt_l) > Math.max(1.5, tgt_l * 0.015)) + { + can_shortcut = false; + break; + } + } + if (can_shortcut) + { + const kept_pgs = prev_int_pgs.map(pg => [ ...pg ]); + shortcut = { + prev_pgs: prev_int_pgs, + score: pg_count * (pg_size + 1), + weights: { ...prev_weights }, + int_pgs: kept_pgs, + differs: 0, + osd_differs: 0, + pg_effsize, + space: pg_effsize * pg_list_space_efficiency(kept_pgs, osd_weights, pg_minsize, parity_space), + total_space: total_weight, + }; + } + } + } + return { prev_weights, prev_pg_per_osd, old_pg_size, still_valid, shortcut }; +} + +// Try to minimize data movement +async function optimize_change({ prev_pgs: prev_int_pgs, osd_weights, combinator, pg_size = 3, pg_minsize = 2, parity_space = 1, ordered = false }) +{ + if (!osd_weights) + { + return null; + } + const pg_count = prev_int_pgs.length; + const total_weight = Object.values(osd_weights).reduce((a, c) => Number(a) + Number(c), 0); + const { prev_weights, prev_pg_per_osd, old_pg_size, still_valid, shortcut } = prepare_optimize_change({ + prev_int_pgs, osd_weights, combinator, pg_size, pg_minsize, parity_space, total_weight, + }); + if (shortcut) + { + return shortcut; + } + // FIXME: use parity_chunks with parity_space instead of pg_minsize + let all_pgs = make_cyclic(combinator.random_combinations(), parity_space); + let pg_effsize = Object.values(all_pgs).reduce((a, c) => Math.max(a, c.filter(e => e != NO_OSD).length), 0); + pg_effsize = Math.min(pg_minsize, pg_effsize) + Math.max(0, Math.min(pg_size, pg_effsize) - pg_minsize) * parity_space; // Get all combinations if (old_pg_size == pg_size) { - const still_valid = combinator.check_combinations(Object.keys(prev_weights).map(pg_name => pg_name.substr(3).split('_'))); for (const pg of still_valid) { all_pgs['pg_'+pg.join('_')] = pg; @@ -289,7 +359,6 @@ async function optimize_change({ prev_pgs: prev_int_pgs, osd_weights, combinator // Calculate total weight - old PG weights const all_pg_names = all_pgs.map(pg => 'pg_'+pg.join('_')); const all_pgs_hash = all_pg_names.reduce((a, c) => { a[c] = true; return a; }, {}); - const total_weight = Object.values(osd_weights).reduce((a, c) => Number(a) + Number(c), 0); // Generate the LP problem let lp = ''; lp += 'max: '+all_pg_names.map(pg_name => ( diff --git a/mon/lp_optimizer/test-optimize-idempotency.js b/mon/lp_optimizer/test-optimize-idempotency.js new file mode 100644 index 00000000..0def5867 --- /dev/null +++ b/mon/lp_optimizer/test-optimize-idempotency.js @@ -0,0 +1,96 @@ +// Tests for optimize_change idempotent shortcut +// Cluster: 10 hosts x 6 OSDs (60 OSDs), 1024 PGs, EC 4+2 and Rep3 + +const assert = require('assert'); +const { fold_failure_domains, unfold_failure_domains, fold_prev_pgs } = require('./fold.js'); +const DSL = require('./dsl_pgs.js'); +const LPOptimizer = require('./lp_optimizer.js'); + +function make_cluster_nodes(num_hosts = 10, osds_per_host = 6) +{ + const nodes = []; + let osd_id = 1; + for (let h = 1; h <= num_hosts; h++) + { + const host = `host${h}`; + nodes.push({ id: host, level: 'host', parent: 'root' }); + for (let d = 1; d <= osds_per_host; d++) + { + nodes.push({ id: osd_id++, level: 'osd', size: 1, parent: host }); + } + } + return nodes; +} + +function osd_weights_map(nodes) +{ + return nodes.reduce((a, c) => { if (c.level === 'osd') a[c.id] = c.size; return a; }, {}); +} + +function calc_osd_eff(pgs, weights, pg_minsize, pg_effsize) +{ + const tw = Object.values(weights).reduce((a, c) => a + c, 0); + return (pg_effsize * LPOptimizer.pg_list_space_efficiency(pgs, weights, pg_minsize, 1) / tw * 100).toFixed(2) + '%'; +} + +function calc_move_pct(res, pg_count, pg_size) +{ + return (res.osd_differs / (pg_count * pg_size) * 100).toFixed(2) + '%'; +} + +async function test_pool({ label, pg_size, pg_minsize, ordered }) +{ + const nodes = make_cluster_nodes(10, 6); + const rules = DSL.parse_level_indexes({ host: '123456'.slice(0, pg_size) }, [ 'host', 'osd' ]); + const pg_count = 1024; + const { nodes: f, leaves } = fold_failure_domains(nodes, rules); + const fw = f.reduce((a, c) => { if (Number(c.id)) a[c.id] = c.size; return a; }, {}); + + const init = await LPOptimizer.optimize_initial({ + osd_weights: fw, + combinator: new DSL.RuleCombinator(f, rules, 10000, ordered), + pg_size, pg_count, pg_minsize, ordered, + }); + assert.strictEqual(init.int_pgs.length, pg_count); + const init_u = unfold_failure_domains(init.int_pgs, null, leaves); + const prev_folded = fold_prev_pgs(init_u, leaves); + const host10_id = Object.keys(leaves).find(id => leaves[id].some(o => o.parent === 'host10')); + + const run_change = weights => LPOptimizer.optimize_change({ + prev_pgs: prev_folded, + osd_weights: weights, + combinator: new DSL.RuleCombinator(f, rules, 10000, ordered), + pg_size, pg_minsize, ordered, + }); + + // 1. Idempotent shortcut: unchanged topology and weights should hit shortcut with 0 movement + const res_keep = await run_change(fw); + const res_keep_u = unfold_failure_domains(res_keep.int_pgs, init_u, leaves); + assert.strictEqual(res_keep.differs, 0, 'Idempotent shortcut should have 0 differs'); + assert.strictEqual(res_keep.osd_differs, 0, 'Idempotent shortcut should have 0 osd_differs'); + assert.deepStrictEqual(res_keep_u, init_u, 'Idempotent shortcut should preserve unfolded PGs'); + console.log(`test_idempotent_shortcut (10x6, 1024PG, ${label}): PASS | OSD space_eff=${calc_osd_eff(res_keep_u, osd_weights_map(nodes), pg_minsize, res_keep.pg_effsize)}, movement=${calc_move_pct(res_keep, pg_count, pg_size)} (${res_keep.differs} PGs)`); + + // 2. Zero weight: host10 stays in tree with weight 0 -> must bypass shortcut and drain host10 + const res_zero = await run_change({ ...fw, [host10_id]: 0 }); + assert.ok(res_zero.differs > 0, 'Zero-weight node must bypass shortcut and trigger movement'); + assert.ok(res_zero.int_pgs.every(pg => !pg.includes(host10_id) && !pg.includes(Number(host10_id))), 'Drained node must have 0 PGs'); + const nodes_live = nodes.filter(n => n.parent !== 'host10'); + const res_zero_u = unfold_failure_domains(res_zero.int_pgs, init_u, leaves); + console.log(`test_no_shortcut_zero_weight (10x6, 1024PG, ${label}): PASS | OSD space_eff=${calc_osd_eff(res_zero_u, osd_weights_map(nodes_live), pg_minsize, res_zero.pg_effsize)}, movement=${calc_move_pct(res_zero, pg_count, pg_size)} (${res_zero.differs} PGs)`); + + // 3. Weight change: halving host10 weight exceeds tolerance -> must bypass shortcut and rebalance via LP + const res_half = await run_change({ ...fw, [host10_id]: fw[host10_id] / 2 }); + assert.ok(res_half.differs > 0, 'Weight change exceeding load tolerance must bypass shortcut'); + const nodes_half = nodes.map(n => (n.parent === 'host10' ? { ...n, size: 0.5 } : n)); + const res_half_u = unfold_failure_domains(res_half.int_pgs, init_u, leaves); + console.log(`test_no_shortcut_weight_change (10x6, 1024PG, ${label}): PASS | OSD space_eff=${calc_osd_eff(res_half_u, osd_weights_map(nodes_half), pg_minsize, res_half.pg_effsize)}, movement=${calc_move_pct(res_half, pg_count, pg_size)} (${res_half.differs} PGs)`); +} + +async function run() +{ + await test_pool({ label: 'EC 4+2', pg_size: 6, pg_minsize: 4, ordered: true }); + await test_pool({ label: 'Rep3', pg_size: 3, pg_minsize: 2, ordered: false }); +} + +run().catch(err => { console.error(err); process.exit(1); });