Distribute odd cache totals evenly between sites

This commit is contained in:
Charles Miller 2026-10-28 10:54:16 +00:00
parent 0f1db884c2
commit ce83c01f83
2 changed files with 17 additions and 1 deletions

View file

@ -2,8 +2,13 @@
function utilityRouteServers(){return S?Object.keys(S.ixp).filter(ix=>isIx(ix)&&!S.myIx[ix]&&!S.rs[ix]):[];}
function utilityCacheMoves(city){if(!S||!S.pops[city])return [];const groups=new Map(),moves=[];
for(const c of S.caches)if(c.city===city){let g=groups.get(c.net);if(!g){g={a:[],b:[]};groups.set(c.net,g);}g[c.site==='b'?'b':'a'].push(c);}
const move=(g,from,n)=>{const to=from==='a'?'b':'a';for(const c of g[from].splice(0,n)){g[to].push(c);moves.push({id:c.id,site:to});}};
for(const g of groups.values()){const from=g.a.length>g.b.length?'a':'b',to=from==='a'?'b':'a';
for(const c of g[from].slice(0,Math.floor(Math.abs(g.a.length-g.b.length)/2)))moves.push({id:c.id,site:to});}
move(g,from,Math.floor((g[from].length-g[to].length)/2));}
// Spread odd-provider extras too, so the overall site counts also differ by at most one.
let diff=0;for(const g of groups.values())diff+=g.a.length-g.b.length;
const from=diff>0?'a':'b',to=from==='a'?'b':'a';let left=Math.floor(Math.abs(diff)/2);
for(const g of groups.values())if(left&&g[from].length>g[to].length){move(g,from,1);left--;}
return moves;}
function utilityCaches(city){const p=S&&S.pops[city];if(!p)return null;const sites=p.site2?['a','b']:['a'],add=[],have=new Set();
for(const c of S.caches)if(c.city===city)have.add(c.net+'@'+(c.site==='b'?'b':'a'));

View file

@ -50,6 +50,17 @@ test('a rejected kit purchase never moves caches or partially installs hardware'
assert.deepEqual(errors, []);
});
test('odd provider totals are balanced across the whole PoP with the fewest moves', t => {
const { ev, errors } = setup(t);
ev("Object.assign(S.pops.fra,{gen:true,site2:true,routers:2});for(const [net,n] of [['nfx',3],['cfl',1],['ggl',1]])for(let i=0;i<n;i++)S.caches.push({id:net+i,city:'fra',net});compute()");
assert.equal(ev("utilityCacheMoves('fra').length"), 2, 'five boxes all in A require two moves');
ev("ACT.utilityKit({city:'fra',cost:0})");
assert.equal(ev("S.caches.filter(c=>c.site==='b').length"), 2);
assert.equal(ev("['nfx','cfl','ggl'].every(net=>Math.abs(S.caches.filter(c=>c.net===net&&c.site==='b').length-S.caches.filter(c=>c.net===net&&c.site!=='b').length)<=1)"), true);
assert.equal(ev("utilityCacheMoves('fra').length"), 0);
assert.deepEqual(errors, []);
});
test('cache batch fills missing provider/site pairs, keeps extra boxes and respects eligibility', t => {
const { ev, errors } = setup(t);
ev("Object.assign(S.pops.fra,{site2:true,routers:2});S.caches=[{id:'keep1',city:'fra',net:'cfl'},{id:'keep2',city:'fra',net:'cfl',site:'a'}];compute();R.demTo={cfl:NET.cfl.cache.min,ggl:NET.ggl.cache.min-1}");