Cache boxes sized by the city's own traffic split: Russia, China and Korea use their network lists, no box without local traffic (closes #260)

This commit is contained in:
Frederic Jung 2026-11-02 07:36:17 +01:00
parent add3d67e48
commit 295908f859
No known key found for this signature in database
4 changed files with 34 additions and 5 deletions

View file

@ -6,6 +6,7 @@ All notable changes to this project are documented here. The project follows
## [Unreleased]
- Cache boxes are sized for the traffic each city really sends (#260): Russian and Chinese cities get boxes for the networks people there actually use, and none for networks they cannot reach. Boxes already installed stay; remove idle ones from the POP.
- A 1.6T transponder on routers with 800G ports now takes two ports at each end, one for each 800G it hands over, and the optics list says so (#256). Before, a full 1.6T fit into a single 800G port. On 1.6T routers it still takes one.
- The calendar is real now: months have 28 to 31 days and every fourth year has a 29 February (#257). Monthly prices, loan statements, the intern's budget and paydays follow calendar months, and conferences fall on the 10th of their month.
- On big networks, clicks that change nothing about your traffic now answer at once: dismissing an offer or renaming your network no longer works out every route again, which also saves battery on phones.

View file

@ -166,7 +166,7 @@ const OPS_RUN={
cap+=optG(o,l);X.did('optics',l.a+'|'+l.b);}}},
caches(X){const g=opsP('caches','g');
for(const c of opsPops('caches')){const q=utilityCaches(c);if(!q||!q.add.length)continue;
const want=q.add.filter(x=>(R.cityDem[c]||0)*(NSH[x.net]||0)>=g),free=()=>portsMax(c)-portsUsed(c)+1e-9;
const want=q.add.filter(x=>cacheDem(c,x.net)>=g),free=()=>portsMax(c)-portsUsed(c)+1e-9;
for(const x of want){if(cachePortsAdd(c,[x])>free())continue;if(!X.can('caches',0,cacheMo(x)))return;S.caches.push({id:uid(),city:c,net:x.net,site:x.site,...(x.g?{g:x.g}:{})});X.book('caches',0,cacheMo(x));X.did('caches',c);}}},
// Weekly, like a person going through PeeringDB on a Monday: route servers, then every willing member (#192).
ix(X){if(S.day%7)return;for(const ix of utilityRouteServers()){S.rs[ix]=true;X.did('rs');}

View file

@ -10,12 +10,19 @@ function utilityCacheMoves(city){if(!S||!S.pops[city])return [];const groups=new
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;}
// One box per site first; then more boxes while this city's demand to the network outgrows them (100G each, #248).
// A city's traffic to one network, split as routing splits it (#260): China, Russia and Korea's exit rule have their own
// list of networks, and Goggle is throttled to half in Russia. The world shares (NSH) would size boxes for networks the
// city cannot even reach, and too few for the ones it uses most.
const cityShTab=c=>cnCity(c)?NSH_CN:ruCity(c)?NSH_RU:CITY[c].kr&&S.krExit?NSH_KRX:NSH;
const cityNetSh=(c,id)=>(cityShTab(c)[id]||0)*(id==='ggl'&&ruCity(c)?0.5:1);
function cacheDem(c,id){const T=cityShTab(c);let t=0;for(const k in T)t+=cityNetSh(c,k);return t>0?((R.cityDem&&R.cityDem[c])||0)*cityNetSh(c,id)/t:0;}
// One box per site first, if the city sends the network anything at all; then more boxes while that demand outgrows them
// (100G each, #248).
function utilityCaches(city){const p=S&&S.pops[city];if(!p)return null;const sites=p.site2?['a','b']:['a'],add=[],have={};
for(const c of S.caches)if(c.city===city){const k=c.net+'@'+(c.site==='b'?'b':'a');have[k]=(have[k]||0)+1;}
for(const n of NETS)if(n.cache&&(R.demTo[n.id]||0)>=n.cache.min){const box=sites.map(st=>have[n.id+'@'+st]||0);
for(const n of NETS)if(n.cache&&(R.demTo[n.id]||0)>=n.cache.min&&cacheDem(city,n.id)>1e-6){const box=sites.map(st=>have[n.id+'@'+st]||0);
sites.forEach((site,i)=>{if(!box[i]){add.push({net:n.id,site,...cacheBox(city)});box[i]=1;}});
const dem=((R.cityDem&&R.cityDem[city])||0)*(NSH[n.id]||0),nb=cacheBox(city);
const dem=cacheDem(city,n.id),nb=cacheBox(city);
let cap=S.caches.filter(c=>c.city===city&&c.net===n.id).reduce((a,c)=>a+cacheG(c),0)+add.filter(x=>x.net===n.id).length*cacheG(nb);
while(cap+1e-9<dem){const i=box.indexOf(Math.min(...box));add.push({net:n.id,site:sites[i],...nb});box[i]++;cap+=cacheG(nb);}}
return {add,ports:cachePortsAdd(city,add),key:add.map(c=>c.net+'@'+c.site).join(','),mo:add.reduce((a,x)=>a+cacheMo(x),0),free:Math.max(0,portsMax(city)-portsUsed(city))};}

View file

@ -5,7 +5,8 @@ const { load } = require('./harness');
function setup(t) {
const h = load();
t.after(() => h.dom.window.close());
h.ev("ACT.start({});S.cash=1e8;UI.speed=0;for(const g of GOALS)S.done[g.id]=0;compute()");
// Frankfurt sells broadband, so it sends every network some traffic (no traffic, no box: #260).
h.ev("ACT.start({});S.cash=1e8;UI.speed=0;for(const g of GOALS)S.done[g.id]=0;ACT.launchBB({city:'fra'});compute()");
return h;
}
@ -132,3 +133,23 @@ test('more boxes while demand outgrows them, balanced across sites (#248)', t =>
assert.equal(ev("S.caches.filter(c=>c.net==='cfl').length"), 4);
assert.deepEqual(errors, []);
});
test('boxes follow the traffic each city really sends: Russia and China have their own networks (#260)', t => {
const { ev, errors } = setup(t);
// Murmansk and Shanghai, every cache network eligible, plenty of demand.
ev("for(const c of ['mmk','sha'])S.pops[c]={day:0,routers:2};S.caches=[];compute();R.demTo=Object.fromEntries(NETS.filter(n=>n.cache).map(n=>[n.id,n.cache.min]));R.cityDem.mmk=5000;R.cityDem.sha=5000");
const nets = c => JSON.parse(ev(`JSON.stringify(utilityCaches('${c}').add.map(x=>x.net))`));
const count = (c, id) => nets(c).filter(n => n === id).length;
// NetChill and Faceplant reach nobody in Russia: no boxes. Akamaybe and Crowdflare have more of Russia than of the world.
assert.equal(count('mmk', 'nfx'), 0);
assert.equal(count('mmk', 'fbk'), 0);
assert.ok(ev("cacheDem('mmk','cfl')") > 5000 * ev('NSH.cfl') * 2, 'Crowdflare: more than twice its world share');
assert.equal(count('mmk', 'cfl') * ev("cacheG(cacheBox('mmk'))") >= ev("cacheDem('mmk','cfl')") - 1e-6, true, 'enough Crowdflare boxes');
// Goggle is throttled to half in Russia.
assert.ok(Math.abs(ev("cacheDem('mmk','ggl')/cacheDem('mmk','akm')") - ev('NSH_RU.ggl*0.5/NSH_RU.akm')) < 1e-9);
// In China only the networks on its own list get boxes.
for (const id of nets('sha')) assert.ok(ev(`NSH_CN[${JSON.stringify(id)}]>0`), `${id} reaches Shanghai`);
// Elsewhere nothing changes: the world share.
assert.ok(Math.abs(ev("cacheDem('fra','nfx')") - ev('(R.cityDem.fra||0)*NSH.nfx')) < 1e-6);
assert.deepEqual(errors, []);
});