From c910ca706871681de84836f113aa16d855762362 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Tue, 24 Nov 2026 23:04:35 +0300 Subject: [PATCH 1/3] Keep PG count when optimize_change drops PGs instead of moving them The LP in optimize_change() has no constraint on the total PG count, and make_int_pgs() scales whatever is left back to pg_count. In an EC pool (ordered) a moved PG costs 2 or more, so the LP may prefer to drop PGs from overloaded failure domains rather than move them. The rest are then scaled back onto the same domains: a host returning to an EC 4+2 pool on 7 hosts keeps 0 PGs and the pool stays at 6/7 efficiency, and a host leaving an EC 8+2 pool on 12 hosts leaves another host empty. If the solution loses PGs, solve again with the PG count fixed, and keep the first solution when that is infeasible. --- mon/lp_optimizer/lp_optimizer.js | 16 ++++- mon/lp_optimizer/test-optimize-keep-count.js | 71 ++++++++++++++++++++ 2 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 mon/lp_optimizer/test-optimize-keep-count.js diff --git a/mon/lp_optimizer/lp_optimizer.js b/mon/lp_optimizer/lp_optimizer.js index 6b886f38..992e893f 100644 --- a/mon/lp_optimizer/lp_optimizer.js +++ b/mon/lp_optimizer/lp_optimizer.js @@ -326,14 +326,24 @@ async function optimize_change({ prev_pgs: prev_int_pgs, osd_weights, combinator lp += `${pg_name} >= 0;\n`; } } - lp += 'sec '+pg_vars.join(', ')+';\n'; + const sec = 'sec '+pg_vars.join(', ')+';\n'; // Solve it - const lp_result = await lp_solve(lp); + let lp_result = await lp_solve(lp+sec); if (!lp_result) { - console.log(lp); + console.log(lp+sec); throw new Error('Problem is infeasible or unbounded - is it a bug?'); } + // make_int_pgs() scales dropped PGs back onto the same OSDs, so keep the PG count if possible + const kept_count = all_pg_names.reduce((a, pg_name) => a + (prev_weights[pg_name]||0), 0); + const new_count = kept_count + Object.keys(lp_result.vars).reduce((a, k) => a + + (k.substr(0, 4) === 'del_' ? -1 : 1)*Number(lp_result.vars[k]), 0); + if (new_count < pg_count - 0.5) + { + const keep_count = all_pg_names.map(pg_name => prev_weights[pg_name] ? `add_${pg_name} - del_${pg_name}` : pg_name) + .join(' + ')+' = '+(pg_count-kept_count)+';\n'; + lp_result = await lp_solve(lp+keep_count+sec) || lp_result; + } // Generate the new distribution const weights = { ...prev_weights }; for (const k in prev_weights) diff --git a/mon/lp_optimizer/test-optimize-keep-count.js b/mon/lp_optimizer/test-optimize-keep-count.js new file mode 100644 index 00000000..57e79ccb --- /dev/null +++ b/mon/lp_optimizer/test-optimize-keep-count.js @@ -0,0 +1,71 @@ +// Copyright (c) Vitaliy Filippov, 2019+ +// License: VNPL-1.1 (see README.md for details) + +// optimize_change() must not lose PGs and leave failure domains empty or overloaded +// when a host leaves or returns to an EC pool + +const assert = require('assert'); +const LPOptimizer = require('./lp_optimizer.js'); + +// Hosts folded into one item each, all their combinations +function host_cfg(hosts, pg_size, pg_minsize) +{ + const all_sets = {}; + const add_sets = (start, cur) => + { + if (cur.length == pg_size) + all_sets['pg_'+cur.join('_')] = cur; + else + for (let i = start; i < hosts.length; i++) + add_sets(i+1, [ ...cur, hosts[i] ]); + }; + add_sets(0, []); + return { + osd_weights: hosts.reduce((a, h) => { a[h] = 1; return a; }, {}), + combinator: { + random_combinations: () => ({ ...all_sets }), + check_combinations: pgs => pgs.filter(pg => pg.every(h => hosts.includes(h))), + }, + pg_size, + pg_minsize, + ordered: true, + }; +} + +function check_balance(res, hosts, pg_count, pg_size) +{ + LPOptimizer.print_change_stats(res); + const per_host = {}; + for (const pg of res.int_pgs) + for (const h of pg) + per_host[h] = (per_host[h]||0) + 1; + console.log('PGs per host:', per_host); + assert(res.int_pgs.length == pg_count); + const fair = pg_count*pg_size/hosts.length; + for (const h of hosts) + assert(Math.abs(per_host[h] - fair) <= 0.05*fair, 'host '+h+' has '+per_host[h]+' PGs, expected '+fair); +} + +async function run() +{ + // EC 4+2 on 7 hosts, host 3 returns after all 128 PGs moved to the other 6 + let hosts = [ '1', '2', '3', '4', '5', '6', '7' ]; + const prev_pgs = []; + for (let i = 0; i < 128; i++) + { + const pg = [ '1', '2', '4', '5', '6', '7' ]; + prev_pgs.push([ ...pg.slice(i % 6), ...pg.slice(0, i % 6) ]); + } + let res = await LPOptimizer.optimize_change({ prev_pgs, ...host_cfg(hosts, 6, 4) }); + check_balance(res, hosts, 128, 6); + // EC 8+2 on 12 hosts, host 9 leaves + hosts = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12' ]; + res = await LPOptimizer.optimize_initial({ pg_count: 256, ...host_cfg(hosts, 10, 9) }); + check_balance(res, hosts, 256, 10); + hosts = hosts.filter(h => h != '9'); + res = await LPOptimizer.optimize_change({ prev_pgs: res.int_pgs, ...host_cfg(hosts, 10, 9) }); + check_balance(res, hosts, 256, 10); + console.log('OK'); +} + +run().catch(e => { console.error(e); process.exit(1); }); -- 2.47.3 From f3436a5ab418a2eff42f806cfc92edabe015e97a Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 25 Nov 2026 02:42:14 +0300 Subject: [PATCH 2/3] Log when optimize_change can't keep the PG count When the PG count can't be kept within OSD sizes (failure domains too unequal for pg_count PGs at their shares), the first solution is kept and some OSDs get more than their share. Say so instead of keeping it silently. --- mon/lp_optimizer/lp_optimizer.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mon/lp_optimizer/lp_optimizer.js b/mon/lp_optimizer/lp_optimizer.js index 992e893f..7e3164b0 100644 --- a/mon/lp_optimizer/lp_optimizer.js +++ b/mon/lp_optimizer/lp_optimizer.js @@ -342,7 +342,11 @@ async function optimize_change({ prev_pgs: prev_int_pgs, osd_weights, combinator { const keep_count = all_pg_names.map(pg_name => prev_weights[pg_name] ? `add_${pg_name} - del_${pg_name}` : pg_name) .join(' + ')+' = '+(pg_count-kept_count)+';\n'; - lp_result = await lp_solve(lp+keep_count+sec) || lp_result; + const keep_result = await lp_solve(lp+keep_count+sec); + if (keep_result) + lp_result = keep_result; + else + console.log('Can\'t place all '+pg_count+' PGs within OSD sizes, some OSDs will get more than their share'); } // Generate the new distribution const weights = { ...prev_weights }; -- 2.47.3 From 5cf776364e583819f3a9195e0e5f8b0a3e531895 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Thu, 26 Nov 2026 14:36:40 +0300 Subject: [PATCH 3/3] Solve with the PG count kept first in optimize_change Add the PG count condition to the first solve and solve without it only when that is infeasible, instead of re-solving after a solution that lost PGs. Return the fallback as over_share and print it with the pool's change stats: pools are optimized concurrently, so a separate log line can't be matched to its pool. Bound the data movement in the test and add a case where the PG count can't be kept. --- mon/lp_optimizer/lp_optimizer.js | 27 ++++++++------------ mon/lp_optimizer/test-optimize-keep-count.js | 13 ++++++++++ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/mon/lp_optimizer/lp_optimizer.js b/mon/lp_optimizer/lp_optimizer.js index 7e3164b0..cb13d35a 100644 --- a/mon/lp_optimizer/lp_optimizer.js +++ b/mon/lp_optimizer/lp_optimizer.js @@ -327,27 +327,19 @@ async function optimize_change({ prev_pgs: prev_int_pgs, osd_weights, combinator } } const sec = 'sec '+pg_vars.join(', ')+';\n'; - // Solve it - let lp_result = await lp_solve(lp+sec); + // make_int_pgs() scales dropped PGs back onto the same OSDs, so keep the PG count if possible + const kept_count = all_pg_names.reduce((a, pg_name) => a + (prev_weights[pg_name]||0), 0); + const keep_count = all_pg_names.map(pg_name => prev_weights[pg_name] ? `add_${pg_name} - del_${pg_name}` : pg_name) + .join(' + ')+' = '+(pg_count-kept_count)+';\n'; + let lp_result = await lp_solve(lp+keep_count+sec); + const over_share = !lp_result; + if (over_share) + lp_result = await lp_solve(lp+sec); if (!lp_result) { console.log(lp+sec); throw new Error('Problem is infeasible or unbounded - is it a bug?'); } - // make_int_pgs() scales dropped PGs back onto the same OSDs, so keep the PG count if possible - const kept_count = all_pg_names.reduce((a, pg_name) => a + (prev_weights[pg_name]||0), 0); - const new_count = kept_count + Object.keys(lp_result.vars).reduce((a, k) => a + - (k.substr(0, 4) === 'del_' ? -1 : 1)*Number(lp_result.vars[k]), 0); - if (new_count < pg_count - 0.5) - { - const keep_count = all_pg_names.map(pg_name => prev_weights[pg_name] ? `add_${pg_name} - del_${pg_name}` : pg_name) - .join(' + ')+' = '+(pg_count-kept_count)+';\n'; - const keep_result = await lp_solve(lp+keep_count+sec); - if (keep_result) - lp_result = keep_result; - else - console.log('Can\'t place all '+pg_count+' PGs within OSD sizes, some OSDs will get more than their share'); - } // Generate the new distribution const weights = { ...prev_weights }; for (const k in prev_weights) @@ -428,6 +420,7 @@ async function optimize_change({ prev_pgs: prev_int_pgs, osd_weights, combinator int_pgs: new_pgs, differs, osd_differs, + over_share, pg_effsize, space: pg_effsize * pg_list_space_efficiency(new_pgs, osd_weights, pg_minsize, parity_space), total_space: total_weight, @@ -454,6 +447,8 @@ function print_change_stats(retval, detailed) "Data movement: "+retval.differs+" pgs, "+ retval.osd_differs+" pg*osds = "+Math.round(retval.osd_differs / prev_int_pgs.length / 3 * 10000)/100+" %" ); + if (retval.over_share) + console.log("Can't place all "+new_pgs.length+" PGs within OSD sizes, some OSDs get more than their share"); } console.log( "Total space (raw): "+Math.round(retval.space*100)/100+" TB, space efficiency: "+ diff --git a/mon/lp_optimizer/test-optimize-keep-count.js b/mon/lp_optimizer/test-optimize-keep-count.js index 57e79ccb..a9478660 100644 --- a/mon/lp_optimizer/test-optimize-keep-count.js +++ b/mon/lp_optimizer/test-optimize-keep-count.js @@ -58,6 +58,8 @@ async function run() } let res = await LPOptimizer.optimize_change({ prev_pgs, ...host_cfg(hosts, 6, 4) }); check_balance(res, hosts, 128, 6); + // Host 3 needs ~110 PGs, one per moved PG + assert(res.differs <= 120, res.differs+' PGs moved'); // EC 8+2 on 12 hosts, host 9 leaves hosts = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12' ]; res = await LPOptimizer.optimize_initial({ pg_count: 256, ...host_cfg(hosts, 10, 9) }); @@ -65,6 +67,17 @@ async function run() hosts = hosts.filter(h => h != '9'); res = await LPOptimizer.optimize_change({ prev_pgs: res.int_pgs, ...host_cfg(hosts, 10, 9) }); check_balance(res, hosts, 256, 10); + // ~213 PGs lose host 9, plus some rebalancing, but not all 256 + assert(res.differs <= 245, res.differs+' PGs moved'); + // EC 4+2 on 6 hosts, host 1 is 3 times bigger: the others can't take 128 PGs at their share + hosts = [ '1', '2', '3', '4', '5', '6' ]; + res = await LPOptimizer.optimize_change({ + prev_pgs: [ ...Array(128) ].map(() => [ ...hosts ]), + ...host_cfg(hosts, 6, 4), + osd_weights: { '1': 3, '2': 1, '3': 1, '4': 1, '5': 1, '6': 1 }, + }); + LPOptimizer.print_change_stats(res); + assert(res.over_share && res.int_pgs.length == 128); console.log('OK'); } -- 2.47.3