@kvbox/server 0.6.10 → 0.6.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -466,6 +466,12 @@ var ConfigSchema = z2.object({
466
466
  * refused when it could not fill up with that much still free. 0 only on a host whose disk is for tests.
467
467
  */
468
468
  volumeHeadroomGib: z2.number().int().min(0).default(20),
469
+ /**
470
+ * MiB of RAM the host keeps back from its boxes: they may reserve the host's RAM less this. 0 by default
471
+ * (boxes rarely touch all they are sized at); raise it on a host whose boxes do fill their memory. Read at
472
+ * every check, so an edit needs no restart; reported in `/capacity`, which is what placement goes by.
473
+ */
474
+ memHeadroomMib: z2.number().int().min(0).default(0),
469
475
  /** Absent until the host joins a control plane; then the server also listens on HTTPS. */
470
476
  controlPlane: ControlPlaneSchema.optional(),
471
477
  /** Absent unless the host sits behind a relay. */
@@ -1744,7 +1750,6 @@ import { rename as rename5, rm as rm6 } from "node:fs/promises";
1744
1750
  import { cpus, totalmem } from "node:os";
1745
1751
 
1746
1752
  // ../shared/src/capacity.ts
1747
- var MEM_HEADROOM_MIB = 2048;
1748
1753
  function sellableGib(d) {
1749
1754
  return Math.max(0, d.freeGib + d.volumesUsedGib - d.headroomGib);
1750
1755
  }
@@ -2291,6 +2296,11 @@ var CapacitySchema = z5.object({
2291
2296
  reservedMemMib: z5.number().int(),
2292
2297
  reservedVcpu: z5.number().int(),
2293
2298
  running: z5.number().int(),
2299
+ /**
2300
+ * MiB of RAM the host keeps back from its boxes (config.json `memHeadroomMib`). Absent on a kvbox-server
2301
+ * from before the setting, which keeps 2 GB (`OLD_HOST_MEM_HEADROOM_MIB`).
2302
+ */
2303
+ memHeadroomMib: z5.number().int().optional(),
2294
2304
  /** Absent on a host from before volumes (#91): it cannot take one. */
2295
2305
  disk: DiskCapacitySchema.optional(),
2296
2306
  /**
@@ -3415,15 +3425,17 @@ async function runningReservations(p = paths()) {
3415
3425
  async function capacity(p = paths()) {
3416
3426
  const host = hostResources();
3417
3427
  const running = await runningReservations(p);
3428
+ const cfg = await loadConfig(p);
3418
3429
  return {
3419
3430
  hostMemMib: host.memMib,
3431
+ memHeadroomMib: cfg.memHeadroomMib,
3420
3432
  hostCores: host.cores,
3421
3433
  reservedMemMib: running.reduce((n, r) => n + r.memMib, 0),
3422
3434
  reservedVcpu: running.reduce((n, r) => n + r.vcpu, 0),
3423
3435
  running: running.length,
3424
3436
  disk: await diskCapacity(p),
3425
3437
  // What a box can be made from here (#87): the control plane places `--base postgres` by it.
3426
- images: await listImages(await loadConfig(p), p),
3438
+ images: await listImages(cfg, p),
3427
3439
  ...await backupStoreCapacity(p).then((store) => store ? { backupStore: store } : {}),
3428
3440
  // Backups and restores here reach a store by its certificate's pin (core/storeFetch.ts).
3429
3441
  storeClient: true
@@ -3453,11 +3465,12 @@ function roomFor(host, running, want, opts = {}) {
3453
3465
  const { viewer, by = "new" } = opts;
3454
3466
  const reservedMem = running.reduce((n, r) => n + r.memMib, 0);
3455
3467
  const reservedVcpu = running.reduce((n, r) => n + r.vcpu, 0);
3456
- const memLimit = host.memMib - MEM_HEADROOM_MIB;
3468
+ const headroom = host.headroomMib ?? 0;
3469
+ const memLimit = host.memMib - headroom;
3457
3470
  const vcpuLimit = host.cores * VCPU_OVERSUBSCRIPTION;
3458
3471
  const hint = HINTS[by];
3459
3472
  if (reservedMem + want.memMib > memLimit) {
3460
- return `not enough memory for a ${gb(want.memMib)} box: ${describeRunning(running, viewer)}; this host has ${gb(host.memMib)} and keeps ${gb(MEM_HEADROOM_MIB)} for itself, so boxes may reserve ${gb(memLimit)} in total. ` + hint;
3473
+ return `not enough memory for a ${gb(want.memMib)} box: ${describeRunning(running, viewer)}; ` + (headroom > 0 ? `this host has ${gb(host.memMib)} and keeps ${gb(headroom)} for itself, so boxes may reserve ${gb(memLimit)} in total. ` : `this host has ${gb(host.memMib)}, all of it for its boxes. `) + hint;
3461
3474
  }
3462
3475
  if (reservedVcpu + want.vcpu > vcpuLimit) {
3463
3476
  return `too many vCPUs for a ${want.vcpu} vCPU box: ${describeRunning(running, viewer)}; this host has ${host.cores} cores and allows ${vcpuLimit} vCPUs in total (${VCPU_OVERSUBSCRIPTION}x). ` + hint;
@@ -3467,7 +3480,8 @@ function roomFor(host, running, want, opts = {}) {
3467
3480
  async function assertRoomFor(vcpu, memMib, force, box, by, p = paths()) {
3468
3481
  if (force) return;
3469
3482
  const running = (await runningReservations(p)).filter((r) => box.name === void 0 || !(r.owner === box.owner && r.name === box.name));
3470
- const reason = roomFor(hostResources(), running, { vcpu, memMib }, { viewer: box.owner, by });
3483
+ const host = { ...hostResources(), headroomMib: (await loadConfig(p)).memHeadroomMib };
3484
+ const reason = roomFor(host, running, { vcpu, memMib }, { viewer: box.owner, by });
3471
3485
  if (reason !== void 0) throw new KvboxError(reason);
3472
3486
  }
3473
3487
 
@@ -6412,7 +6426,7 @@ function versionOf(srcFileUrl, injected) {
6412
6426
  }
6413
6427
 
6414
6428
  // src/version.ts
6415
- var VERSION = versionOf(import.meta.url, true ? "0.6.10" : void 0);
6429
+ var VERSION = versionOf(import.meta.url, true ? "0.6.11" : void 0);
6416
6430
 
6417
6431
  // src/caller.ts
6418
6432
  import { createHash as createHash3, timingSafeEqual } from "node:crypto";