@waffle-os/runtime 0.0.153 → 0.0.154
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.
- package/browser/net/helper.ts +114 -19
- package/browser/net/ring.ts +18 -6
- package/browser/net/runtime.ts +2 -2
- package/browser/notification-relay.ts +105 -18
- package/browser/notification-wait.ts +45 -4
- package/browser/notification-worker.ts +169 -76
- package/browser/preview.ts +15 -2
- package/core/filesystem.ts +4 -0
- package/core/wasi.ts +49 -0
- package/dist/browser/net/helper.js +83 -18
- package/dist/browser/net/ring.js +6 -5
- package/dist/browser/net/runtime.js +2 -2
- package/dist/browser/notification-relay.js +78 -15
- package/dist/browser/notification-wait.js +37 -5
- package/dist/browser/notification-worker.js +146 -67
- package/dist/core/wasi.js +40 -0
- package/dist/fs/backend.js +7 -0
- package/dist/fs/client.js +69 -15
- package/dist/fs/dirty-admission.js +15 -8
- package/dist/fs/indexeddb.js +288 -96
- package/dist/fs/memory.js +106 -20
- package/dist/fs/multitab-backend.js +121 -37
- package/dist/fs/multitab-protocol.js +1 -0
- package/dist/fs/persistent.js +70 -16
- package/dist/fs/protocol.js +15 -7
- package/dist/fs/publication-attachment.js +9 -7
- package/dist/fs/service.js +34 -9
- package/dist/fs/shared.js +96 -6
- package/dist/proc/command-stdio.js +1 -1
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +11 -0
- package/fs/client.ts +93 -16
- package/fs/dirty-admission.ts +22 -7
- package/fs/guest-watch.ts +1 -1
- package/fs/indexeddb.ts +379 -110
- package/fs/memory.ts +120 -19
- package/fs/multitab-backend.ts +206 -61
- package/fs/multitab-protocol.ts +1 -0
- package/fs/persistent.ts +82 -16
- package/fs/protocol.ts +26 -7
- package/fs/publication-attachment.ts +9 -7
- package/fs/service.ts +35 -7
- package/fs/shared.ts +106 -8
- package/package.json +1 -1
- package/proc/command-stdio.ts +1 -1
- package/testing/idle-workspace-cancellation-browser.ts +22 -13
- package/testing/indexeddb-storage-browser.ts +435 -1
- package/tooling/program/program-transformer.json +1 -1
package/fs/multitab-backend.ts
CHANGED
|
@@ -337,6 +337,16 @@ function waitForAbortOrCancel(
|
|
|
337
337
|
return { promise, cleanup };
|
|
338
338
|
}
|
|
339
339
|
|
|
340
|
+
/** Ends one local Host attempt whose only problem is a lapsed lease; the caller renews and retries. */
|
|
341
|
+
class LeaseLapsedError extends Error {}
|
|
342
|
+
const LEASE_LAPSED = new LeaseLapsedError("host lease lapsed");
|
|
343
|
+
/** How long one call may spend renewing a lapsed lease, in lease durations from its first lapse. */
|
|
344
|
+
const LEASE_RENEWAL_BUDGET_LEASES = 4;
|
|
345
|
+
/** One call's renewal deadline, set at its first lapse and shared by every retry of that call. */
|
|
346
|
+
interface LeaseRenewalBudget {
|
|
347
|
+
deadline?: number;
|
|
348
|
+
}
|
|
349
|
+
|
|
340
350
|
async function raceWithAbort(
|
|
341
351
|
promise: Promise<unknown>,
|
|
342
352
|
signal: AbortSignal,
|
|
@@ -601,6 +611,9 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
601
611
|
|
|
602
612
|
// Host authority state
|
|
603
613
|
private persistent?: PersistentBackend;
|
|
614
|
+
/** Bumped when entries are rebound, and by the outgoing backend's count plus one when the
|
|
615
|
+
* persistent backend changes, so unlinkGeneration() only ever moves forward. */
|
|
616
|
+
private unlinkEvents = 0;
|
|
604
617
|
private watchService?: WatchService;
|
|
605
618
|
private hostQueue: Promise<void> = Promise.resolve();
|
|
606
619
|
private readonly barrier = new BarrierGate();
|
|
@@ -814,6 +827,19 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
814
827
|
return Promise.resolve();
|
|
815
828
|
}
|
|
816
829
|
|
|
830
|
+
private setPersistent(next: PersistentBackend | undefined): void {
|
|
831
|
+
if (next === this.persistent) return;
|
|
832
|
+
this.unlinkEvents += (this.persistent?.unlinkGeneration() ?? 0) + 1;
|
|
833
|
+
this.persistent = next;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
/** Exact for the host, whose unlinks all go through its persistent backend. Any other role
|
|
837
|
+
* learns unlinks from the wire, so it reports NaN and every capability trim rescans. */
|
|
838
|
+
unlinkGeneration(): number {
|
|
839
|
+
if (this.role !== "host" || !this.persistent) return NaN;
|
|
840
|
+
return this.unlinkEvents + this.persistent.unlinkGeneration();
|
|
841
|
+
}
|
|
842
|
+
|
|
817
843
|
isUnlinked(handle: FileHandle): boolean {
|
|
818
844
|
const entry = this.byLocalHandle.get(handle);
|
|
819
845
|
if (!entry) return false;
|
|
@@ -867,11 +893,16 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
867
893
|
const admittedTerm = this.hostTerm;
|
|
868
894
|
const entry = this.byLocalHandle.get(handle);
|
|
869
895
|
if (!entry || entry.stale || entry.remoteHandle === undefined) return undefined;
|
|
896
|
+
// A lease that lapsed only because renewal ran late keeps the read going; execute() renews
|
|
897
|
+
// it before the bytes are used.
|
|
870
898
|
return this.persistent!.prepareDescriptorRead(
|
|
871
899
|
entry.remoteHandle,
|
|
872
900
|
offset,
|
|
873
901
|
length,
|
|
874
|
-
() =>
|
|
902
|
+
() =>
|
|
903
|
+
cancelled() ||
|
|
904
|
+
this.hostTerm !== admittedTerm ||
|
|
905
|
+
(!this.isPreparedReadValid() && !this.leaseOnlyLapsed(admittedTerm)),
|
|
875
906
|
);
|
|
876
907
|
}
|
|
877
908
|
|
|
@@ -1060,7 +1091,8 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
1060
1091
|
}
|
|
1061
1092
|
if (this.role === "host") {
|
|
1062
1093
|
const admittedTerm = this.hostTerm;
|
|
1063
|
-
|
|
1094
|
+
const budget: LeaseRenewalBudget = {};
|
|
1095
|
+
await this.assertHostAfterRenewal(admittedTerm, undefined, budget);
|
|
1064
1096
|
let resolvedTarget: FileHandle | string | undefined = target;
|
|
1065
1097
|
if (typeof target === "number") {
|
|
1066
1098
|
const entry = this.byLocalHandle.get(target);
|
|
@@ -1070,7 +1102,7 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
1070
1102
|
this.enterHostAdmitted();
|
|
1071
1103
|
try {
|
|
1072
1104
|
await this.hostQueue;
|
|
1073
|
-
this.
|
|
1105
|
+
await this.assertHostAfterRenewal(admittedTerm, undefined, budget);
|
|
1074
1106
|
await this.persistent!.flush(resolvedTarget);
|
|
1075
1107
|
} finally {
|
|
1076
1108
|
this.leaveHostAdmitted();
|
|
@@ -1112,6 +1144,9 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
1112
1144
|
if (!entry || entry.stale) throw new FilesystemError(8);
|
|
1113
1145
|
if (this.role === "host") {
|
|
1114
1146
|
const admittedTerm = this.hostTerm;
|
|
1147
|
+
if (this.leaseOnlyLapsed(admittedTerm)) {
|
|
1148
|
+
return this.watchOpenAfterRenewal(owner, handle, recursive, cancelled, admittedTerm);
|
|
1149
|
+
}
|
|
1115
1150
|
this.assertHostReadyForLocal(admittedTerm);
|
|
1116
1151
|
if (entry.remoteHandle === undefined) throw new FilesystemError(8);
|
|
1117
1152
|
this.watchService ??= new WatchService(this.persistent!);
|
|
@@ -1197,31 +1232,47 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
1197
1232
|
throw new FilesystemError(11);
|
|
1198
1233
|
}
|
|
1199
1234
|
|
|
1235
|
+
/** A watch opened while the lease lapsed only late: renew, then open it as usual. */
|
|
1236
|
+
private async watchOpenAfterRenewal(
|
|
1237
|
+
owner: number,
|
|
1238
|
+
handle: FileHandle,
|
|
1239
|
+
recursive: boolean,
|
|
1240
|
+
cancelled: () => boolean,
|
|
1241
|
+
admittedTerm: bigint,
|
|
1242
|
+
): Promise<number> {
|
|
1243
|
+
await this.renewLapsedLease(admittedTerm, cancelled);
|
|
1244
|
+
return this.watchOpen(owner, handle, recursive, cancelled);
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1200
1247
|
private async watchReadHostAfterRenew(
|
|
1201
1248
|
owner: number,
|
|
1202
1249
|
watch: LocalWatchEntry,
|
|
1203
1250
|
cancelled: () => boolean,
|
|
1204
1251
|
): Promise<HeldWatchBatch> {
|
|
1205
1252
|
if (this.disposing || cancelled()) throw new FilesystemError(11);
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
this.
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
cancelled,
|
|
1213
|
-
);
|
|
1253
|
+
if (this.leaseOnlyLapsed(watch.hostTerm)) {
|
|
1254
|
+
// A renewal that cannot finish in time reports one overflow below, not a failure.
|
|
1255
|
+
await this.renewLapsedLease(watch.hostTerm, cancelled).catch(() => {});
|
|
1256
|
+
} else {
|
|
1257
|
+
await raceWithAbort(this.renewLeaseNow(), this.disposeAbort.signal, cancelled);
|
|
1258
|
+
}
|
|
1214
1259
|
if (this.disposing || cancelled()) throw new FilesystemError(11);
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1260
|
+
const overflow: HeldWatchBatch = {
|
|
1261
|
+
batch: { kind: "overflow", events: [] },
|
|
1262
|
+
release() {},
|
|
1263
|
+
rollback() {},
|
|
1264
|
+
};
|
|
1265
|
+
// Only a term that really ended makes the overflow permanent for this watch.
|
|
1266
|
+
const termEnded =
|
|
1219
1267
|
watch.hostTerm !== this.hostTerm ||
|
|
1220
|
-
|
|
1221
|
-
|
|
1268
|
+
this.role !== "host" ||
|
|
1269
|
+
this.ownershipLost ||
|
|
1270
|
+
this.leaseClosed;
|
|
1271
|
+
if (watch.overflowed || termEnded || !this.watchService) {
|
|
1222
1272
|
watch.overflowed = true;
|
|
1223
|
-
return
|
|
1273
|
+
return overflow;
|
|
1224
1274
|
}
|
|
1275
|
+
if (!this.isLeaseValid()) return overflow;
|
|
1225
1276
|
return this.watchService.readHeld(owner, watch.hostWatchId!);
|
|
1226
1277
|
}
|
|
1227
1278
|
|
|
@@ -1343,7 +1394,7 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
1343
1394
|
|
|
1344
1395
|
if (this.persistent) {
|
|
1345
1396
|
const residual = this.persistent;
|
|
1346
|
-
this.
|
|
1397
|
+
this.setPersistent(undefined);
|
|
1347
1398
|
this.watchService = undefined;
|
|
1348
1399
|
await this.retireFailedMountBackend(residual);
|
|
1349
1400
|
}
|
|
@@ -1688,7 +1739,7 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
1688
1739
|
}
|
|
1689
1740
|
|
|
1690
1741
|
// Mount fixed hostTerm & bind PersistentBackend
|
|
1691
|
-
this.
|
|
1742
|
+
this.setPersistent(backend);
|
|
1692
1743
|
this.watchService = new WatchService(backend);
|
|
1693
1744
|
this.hostTerm = backend.initialReservationEra;
|
|
1694
1745
|
this.hostStorageEpoch = storageEpoch;
|
|
@@ -1768,7 +1819,7 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
1768
1819
|
): Promise<void> {
|
|
1769
1820
|
if (this.terminalFailure) throw wrapReleaseError(this.terminalFailure.reason, false);
|
|
1770
1821
|
if (backend) {
|
|
1771
|
-
this.
|
|
1822
|
+
this.setPersistent(undefined);
|
|
1772
1823
|
this.watchService = undefined;
|
|
1773
1824
|
await this.retireFailedMountBackend(backend);
|
|
1774
1825
|
} else {
|
|
@@ -2185,7 +2236,7 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
2185
2236
|
}
|
|
2186
2237
|
|
|
2187
2238
|
this.captureRetainedSegments(persistent);
|
|
2188
|
-
this.
|
|
2239
|
+
this.setPersistent(undefined);
|
|
2189
2240
|
this.watchService = undefined;
|
|
2190
2241
|
if (this.sharedBuffer) persistent.shared.resetForRemount(this.retainedSegments);
|
|
2191
2242
|
await this.releaseAttemptAndElectionLocks();
|
|
@@ -2257,7 +2308,7 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
2257
2308
|
this.closeAllHostPeerSessions();
|
|
2258
2309
|
if (this.persistent) {
|
|
2259
2310
|
const persistent = this.persistent;
|
|
2260
|
-
this.
|
|
2311
|
+
this.setPersistent(undefined);
|
|
2261
2312
|
this.watchService = undefined;
|
|
2262
2313
|
await persistent.retireFenced().catch(() => {});
|
|
2263
2314
|
this.captureRetainedSegments(persistent);
|
|
@@ -2319,7 +2370,7 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
2319
2370
|
this.closeAllHostPeerSessions();
|
|
2320
2371
|
if (this.persistent) {
|
|
2321
2372
|
const persistent = this.persistent;
|
|
2322
|
-
this.
|
|
2373
|
+
this.setPersistent(undefined);
|
|
2323
2374
|
this.watchService = undefined;
|
|
2324
2375
|
await persistent.retireFenced().catch(() => {});
|
|
2325
2376
|
this.captureRetainedSegments(persistent);
|
|
@@ -2344,7 +2395,7 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
2344
2395
|
this.closeAllHostPeerSessions();
|
|
2345
2396
|
if (this.persistent) {
|
|
2346
2397
|
const persistent = this.persistent;
|
|
2347
|
-
this.
|
|
2398
|
+
this.setPersistent(undefined);
|
|
2348
2399
|
this.watchService = undefined;
|
|
2349
2400
|
await persistent.retireFenced().catch(() => {});
|
|
2350
2401
|
this.captureRetainedSegments(persistent);
|
|
@@ -5409,6 +5460,99 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
5409
5460
|
}
|
|
5410
5461
|
}
|
|
5411
5462
|
|
|
5463
|
+
/**
|
|
5464
|
+
* The lease lapsed only because renewal ran late, for example behind a long task on this
|
|
5465
|
+
* worker. This tab is still the Host of this term, so a local operation renews and retries.
|
|
5466
|
+
*/
|
|
5467
|
+
private leaseOnlyLapsed(admittedTerm: bigint): boolean {
|
|
5468
|
+
return (
|
|
5469
|
+
!this.isLeaseValid() &&
|
|
5470
|
+
!this.leaseClosed &&
|
|
5471
|
+
!this.ownershipLost &&
|
|
5472
|
+
!this.terminalFailure &&
|
|
5473
|
+
!this.persistent?.isTerminallyFailed() &&
|
|
5474
|
+
!!this.persistent &&
|
|
5475
|
+
this.role === "host" &&
|
|
5476
|
+
this.hostTerm === admittedTerm &&
|
|
5477
|
+
!this.steppingDown &&
|
|
5478
|
+
!this.disposing
|
|
5479
|
+
);
|
|
5480
|
+
}
|
|
5481
|
+
|
|
5482
|
+
private assertLocalAttempt(admittedTerm: bigint, cancelled?: () => boolean): void {
|
|
5483
|
+
if (cancelled?.()) throw new FilesystemError(11, "operation cancelled");
|
|
5484
|
+
if (this.leaseOnlyLapsed(admittedTerm)) throw LEASE_LAPSED;
|
|
5485
|
+
this.assertHostReadyForLocal(admittedTerm);
|
|
5486
|
+
}
|
|
5487
|
+
|
|
5488
|
+
/** Abandon a local attempt's wait: cancelled, a new term, or a lease renewal cannot restore. */
|
|
5489
|
+
private localAttemptStale(admittedTerm: bigint, cancelled?: () => boolean): boolean {
|
|
5490
|
+
if (cancelled?.() || this.hostTerm !== admittedTerm) return true;
|
|
5491
|
+
return !this.isLeaseValid() && !this.leaseOnlyLapsed(admittedTerm);
|
|
5492
|
+
}
|
|
5493
|
+
|
|
5494
|
+
/** Runs a local attempt; a lapsed lease is renewed, then the whole attempt runs again. */
|
|
5495
|
+
private async retryAfterLeaseRenewal<T>(
|
|
5496
|
+
admittedTerm: bigint,
|
|
5497
|
+
cancelled: (() => boolean) | undefined,
|
|
5498
|
+
budget: LeaseRenewalBudget,
|
|
5499
|
+
attempt: () => Promise<T>,
|
|
5500
|
+
): Promise<T> {
|
|
5501
|
+
for (;;) {
|
|
5502
|
+
try {
|
|
5503
|
+
return await attempt();
|
|
5504
|
+
} catch (error) {
|
|
5505
|
+
if (error !== LEASE_LAPSED) throw error;
|
|
5506
|
+
}
|
|
5507
|
+
// A lost term or a step-down fails in the next attempt's assertHostReadyForLocal.
|
|
5508
|
+
await this.renewLapsedLease(admittedTerm, cancelled, budget);
|
|
5509
|
+
}
|
|
5510
|
+
}
|
|
5511
|
+
|
|
5512
|
+
/** Renews a lapsed lease, then asserts this tab is still the Host of the term. */
|
|
5513
|
+
private async assertHostAfterRenewal(
|
|
5514
|
+
admittedTerm: bigint,
|
|
5515
|
+
cancelled: (() => boolean) | undefined,
|
|
5516
|
+
budget: LeaseRenewalBudget,
|
|
5517
|
+
): Promise<void> {
|
|
5518
|
+
if (this.leaseOnlyLapsed(admittedTerm))
|
|
5519
|
+
await this.renewLapsedLease(admittedTerm, cancelled, budget);
|
|
5520
|
+
this.assertHostReadyForLocal(admittedTerm);
|
|
5521
|
+
}
|
|
5522
|
+
|
|
5523
|
+
/**
|
|
5524
|
+
* Renews until the lease holds or this tab stops being the Host of the term. On a loaded
|
|
5525
|
+
* worker a renewal can confirm the locks yet finish past its own deadline, which proves
|
|
5526
|
+
* nothing about now, so it tries again one poll interval later. A renewal that keeps failing
|
|
5527
|
+
* that way, or keeps throwing, gives up once the call's budget passes.
|
|
5528
|
+
*/
|
|
5529
|
+
private async renewLapsedLease(
|
|
5530
|
+
admittedTerm: bigint,
|
|
5531
|
+
cancelled?: () => boolean,
|
|
5532
|
+
budget: LeaseRenewalBudget = {},
|
|
5533
|
+
): Promise<void> {
|
|
5534
|
+
const fresh = budget.deadline === undefined;
|
|
5535
|
+
const deadline = (budget.deadline ??=
|
|
5536
|
+
performance.now() + this.timings.leaseDurationMs * LEASE_RENEWAL_BUDGET_LEASES);
|
|
5537
|
+
for (let attempt = 0; this.leaseOnlyLapsed(admittedTerm); attempt++) {
|
|
5538
|
+
if ((attempt > 0 || !fresh) && performance.now() >= deadline) {
|
|
5539
|
+
throw new FilesystemError(11, "host lease could not be renewed");
|
|
5540
|
+
}
|
|
5541
|
+
if (attempt > 0) await this.pauseBeforeRenewal(cancelled);
|
|
5542
|
+
await raceWithAbort(
|
|
5543
|
+
this.renewLeaseNow(this.currentAttemptId),
|
|
5544
|
+
this.disposeAbort.signal,
|
|
5545
|
+
cancelled,
|
|
5546
|
+
);
|
|
5547
|
+
if (cancelled?.()) throw new FilesystemError(11, "operation cancelled");
|
|
5548
|
+
}
|
|
5549
|
+
}
|
|
5550
|
+
|
|
5551
|
+
private async pauseBeforeRenewal(cancelled?: () => boolean): Promise<void> {
|
|
5552
|
+
await raceWithAbort(delay(this.timings.waitPollMs), this.disposeAbort.signal, cancelled);
|
|
5553
|
+
if (cancelled?.()) throw new FilesystemError(11, "operation cancelled");
|
|
5554
|
+
}
|
|
5555
|
+
|
|
5412
5556
|
private async executeLocalOnHost(
|
|
5413
5557
|
operation: string,
|
|
5414
5558
|
args: any[],
|
|
@@ -5418,21 +5562,23 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
5418
5562
|
): Promise<unknown> {
|
|
5419
5563
|
if (cancelled?.()) throw new FilesystemError(11, "operation cancelled");
|
|
5420
5564
|
const admittedTerm = this.hostTerm;
|
|
5565
|
+
const budget: LeaseRenewalBudget = {};
|
|
5421
5566
|
|
|
5422
5567
|
// If lease expired before execution: renew first (or fail ECANCELED if disposing!)
|
|
5423
5568
|
if (!this.isLeaseValid()) {
|
|
5424
5569
|
if (this.disposing) throw new FilesystemError(11, "host lease expired during dispose");
|
|
5425
|
-
|
|
5426
|
-
|
|
5427
|
-
|
|
5428
|
-
|
|
5429
|
-
|
|
5430
|
-
|
|
5431
|
-
|
|
5432
|
-
|
|
5570
|
+
if (this.leaseOnlyLapsed(admittedTerm)) {
|
|
5571
|
+
await this.renewLapsedLease(admittedTerm, cancelled, budget);
|
|
5572
|
+
} else {
|
|
5573
|
+
await raceWithAbort(
|
|
5574
|
+
this.renewLeaseNow(this.currentAttemptId),
|
|
5575
|
+
this.disposeAbort.signal,
|
|
5576
|
+
cancelled,
|
|
5577
|
+
);
|
|
5578
|
+
}
|
|
5433
5579
|
if (cancelled?.()) throw new FilesystemError(11, "operation cancelled");
|
|
5434
5580
|
if (this.disposing) throw new FilesystemError(11, "filesystem is disposing");
|
|
5435
|
-
if (!
|
|
5581
|
+
if (!this.isLeaseValid()) {
|
|
5436
5582
|
await this.awaitPendingFenceLoss(cancelled);
|
|
5437
5583
|
if (cancelled?.()) throw new FilesystemError(11, "operation cancelled");
|
|
5438
5584
|
if (this.disposing) throw new FilesystemError(11, "filesystem is disposing");
|
|
@@ -5465,21 +5611,25 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
5465
5611
|
operation === "sync" ? (translated[0] as number) : (translated[0] as string | undefined);
|
|
5466
5612
|
await this.hostQueue;
|
|
5467
5613
|
if (cancelled?.()) throw new FilesystemError(11, "operation cancelled");
|
|
5468
|
-
this.
|
|
5614
|
+
await this.assertHostAfterRenewal(admittedTerm, cancelled, budget);
|
|
5469
5615
|
await persistent.flush(target);
|
|
5470
5616
|
return operation === "sync" ? 0 : undefined;
|
|
5471
5617
|
}
|
|
5472
5618
|
if (operation === "read") {
|
|
5473
|
-
return await this.
|
|
5619
|
+
return await this.retryAfterLeaseRenewal(admittedTerm, cancelled, budget, () =>
|
|
5620
|
+
this.executeLocalHostRead(persistent, admittedTerm, translated, cancelled),
|
|
5621
|
+
);
|
|
5474
5622
|
}
|
|
5475
|
-
const result = await this.
|
|
5476
|
-
|
|
5477
|
-
|
|
5478
|
-
|
|
5479
|
-
|
|
5480
|
-
|
|
5481
|
-
|
|
5482
|
-
|
|
5623
|
+
const result = await this.retryAfterLeaseRenewal(admittedTerm, cancelled, budget, () =>
|
|
5624
|
+
this.executeLocalHostGeneral(
|
|
5625
|
+
persistent,
|
|
5626
|
+
admittedTerm,
|
|
5627
|
+
operation,
|
|
5628
|
+
translated,
|
|
5629
|
+
data,
|
|
5630
|
+
responseCookieTag,
|
|
5631
|
+
cancelled,
|
|
5632
|
+
),
|
|
5483
5633
|
);
|
|
5484
5634
|
return this.mapLocalHostHandles(persistent, admittedTerm, operation, result);
|
|
5485
5635
|
} finally {
|
|
@@ -5494,18 +5644,12 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
5494
5644
|
cancelled?: () => boolean,
|
|
5495
5645
|
): Promise<Uint8Array> {
|
|
5496
5646
|
const [remoteHandle, length, offset] = translated as [number, number, number];
|
|
5497
|
-
const assertValid = () =>
|
|
5498
|
-
if (cancelled?.()) throw new FilesystemError(11, "operation cancelled");
|
|
5499
|
-
this.assertHostReadyForLocal(admittedTerm);
|
|
5500
|
-
};
|
|
5647
|
+
const assertValid = () => this.assertLocalAttempt(admittedTerm, cancelled);
|
|
5501
5648
|
for (;;) {
|
|
5502
5649
|
assertValid();
|
|
5503
5650
|
const startVer = this.readVersion();
|
|
5504
|
-
const prep = persistent.prepareDescriptorRead(
|
|
5505
|
-
|
|
5506
|
-
offset,
|
|
5507
|
-
length,
|
|
5508
|
-
() => !!cancelled?.() || this.hostTerm !== admittedTerm || !this.isLeaseValid(),
|
|
5651
|
+
const prep = persistent.prepareDescriptorRead(remoteHandle, offset, length, () =>
|
|
5652
|
+
this.localAttemptStale(admittedTerm, cancelled),
|
|
5509
5653
|
);
|
|
5510
5654
|
if (prep) {
|
|
5511
5655
|
const accepted = await this.revalidateColdReadBytes(prep, startVer, assertValid);
|
|
@@ -5534,10 +5678,7 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
5534
5678
|
responseCookieTag?: bigint,
|
|
5535
5679
|
cancelled?: () => boolean,
|
|
5536
5680
|
): Promise<unknown> {
|
|
5537
|
-
const assertValid = () =>
|
|
5538
|
-
if (cancelled?.()) throw new FilesystemError(11, "operation cancelled");
|
|
5539
|
-
this.assertHostReadyForLocal(admittedTerm);
|
|
5540
|
-
};
|
|
5681
|
+
const assertValid = () => this.assertLocalAttempt(admittedTerm, cancelled);
|
|
5541
5682
|
for (;;) {
|
|
5542
5683
|
assertValid();
|
|
5543
5684
|
const preparing = persistent.prepare(operation, translated);
|
|
@@ -5562,9 +5703,8 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
5562
5703
|
result = step.result;
|
|
5563
5704
|
});
|
|
5564
5705
|
if (dirtyRetryChange !== undefined) {
|
|
5565
|
-
await persistent.waitForDirtyCapacity(
|
|
5566
|
-
|
|
5567
|
-
() => !!cancelled?.() || this.hostTerm !== admittedTerm || !this.isLeaseValid(),
|
|
5706
|
+
await persistent.waitForDirtyCapacity(dirtyRetryChange, () =>
|
|
5707
|
+
this.localAttemptStale(admittedTerm, cancelled),
|
|
5568
5708
|
);
|
|
5569
5709
|
continue;
|
|
5570
5710
|
}
|
|
@@ -6704,6 +6844,7 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
6704
6844
|
entry.remoteSessionId = sessionId;
|
|
6705
6845
|
entry.pinToken = wire.pinToken;
|
|
6706
6846
|
entry.unlinked = !!wire.unlinked;
|
|
6847
|
+
this.unlinkEvents++;
|
|
6707
6848
|
entry.stale = false;
|
|
6708
6849
|
}
|
|
6709
6850
|
} finally {
|
|
@@ -6713,6 +6854,7 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
6713
6854
|
|
|
6714
6855
|
private rebindLocalHandlesOnHost(term: bigint): void {
|
|
6715
6856
|
const persistent = this.persistent!;
|
|
6857
|
+
this.unlinkEvents++;
|
|
6716
6858
|
for (const entry of this.byLocalHandle.values()) {
|
|
6717
6859
|
const node = persistent.nodeByFileId(entry.fileId);
|
|
6718
6860
|
if (!node) {
|
|
@@ -6825,6 +6967,7 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
6825
6967
|
pinToken: number | undefined,
|
|
6826
6968
|
unlinked?: boolean,
|
|
6827
6969
|
): LocalHandleEntry {
|
|
6970
|
+
if (unlinked) this.unlinkEvents++;
|
|
6828
6971
|
const existing = this.byFileId.get(fileId);
|
|
6829
6972
|
if (existing) {
|
|
6830
6973
|
if (remoteSessionId !== undefined && pinToken !== undefined) {
|
|
@@ -6902,6 +7045,7 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
6902
7045
|
}
|
|
6903
7046
|
|
|
6904
7047
|
private detachHandlesForRemount(): void {
|
|
7048
|
+
this.unlinkEvents++;
|
|
6905
7049
|
for (const entry of this.byLocalHandle.values()) {
|
|
6906
7050
|
entry.remoteHandle = undefined;
|
|
6907
7051
|
entry.remoteTerm = undefined;
|
|
@@ -6981,7 +7125,8 @@ export class MultiTabBackend implements FilesystemBackend {
|
|
|
6981
7125
|
case "setTimes":
|
|
6982
7126
|
case "readdir":
|
|
6983
7127
|
case "readdirPage":
|
|
6984
|
-
case "resolve":
|
|
7128
|
+
case "resolve":
|
|
7129
|
+
case "realpath": {
|
|
6985
7130
|
const remote = mapHandle(args[0]);
|
|
6986
7131
|
if (remote === undefined) return undefined;
|
|
6987
7132
|
return [remote, ...args.slice(1)];
|
package/fs/multitab-protocol.ts
CHANGED
package/fs/persistent.ts
CHANGED
|
@@ -13,7 +13,14 @@ import {
|
|
|
13
13
|
import { FilesystemError, PAGE } from "./protocol.js";
|
|
14
14
|
import type { FileHandle, FileStat } from "../core/index.js";
|
|
15
15
|
import { MemoryBackend, type LocalMutationPlan, type MemoryBackendSharedRoot } from "./backend.js";
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
MemFS,
|
|
18
|
+
adoptInodes,
|
|
19
|
+
type DirNode,
|
|
20
|
+
type Node,
|
|
21
|
+
type FileNode,
|
|
22
|
+
type NamespacePlan,
|
|
23
|
+
} from "./memory.js";
|
|
17
24
|
import {
|
|
18
25
|
storeTime,
|
|
19
26
|
validatePersistedIndex,
|
|
@@ -57,6 +64,9 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
57
64
|
private readonly pressure: CheckpointPressure;
|
|
58
65
|
private nodes = new Map<number, Node>();
|
|
59
66
|
private byFileId = new Map<bigint, Node>();
|
|
67
|
+
private dirEntryBytes = new WeakMap<DirNode, number>();
|
|
68
|
+
private namespaceBytes = 0;
|
|
69
|
+
private idleChangeSequence: number | undefined;
|
|
60
70
|
private readonly initialWorkspaceId: string;
|
|
61
71
|
private readonly initialEra: bigint;
|
|
62
72
|
private readonly loadedIndex: boolean;
|
|
@@ -103,6 +113,7 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
103
113
|
this.links.set(node.ino, (this.links.get(node.ino) ?? 0) + 1);
|
|
104
114
|
if (this.nodes.has(node.ino)) return;
|
|
105
115
|
this.nodes.set(node.ino, node);
|
|
116
|
+
this.namespaceBytes += this.nodeMetadataBytes(node);
|
|
106
117
|
if (node.identity) this.byFileId.set(node.identity.id, node);
|
|
107
118
|
if (node.kind === "dir") for (const child of node.children.values()) visit(child);
|
|
108
119
|
};
|
|
@@ -116,6 +127,11 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
116
127
|
}
|
|
117
128
|
this.committedKeys = new Map(this.keys);
|
|
118
129
|
} else this.dirty.add(fs.root);
|
|
130
|
+
fs.onDirEntryDelta = (dir, delta) => {
|
|
131
|
+
const existing = this.dirEntryBytes.get(dir);
|
|
132
|
+
if (existing !== undefined) this.dirEntryBytes.set(dir, existing + delta);
|
|
133
|
+
if (this.nodes.has(dir.ino)) this.namespaceBytes += delta;
|
|
134
|
+
};
|
|
119
135
|
fs.onLinkChange = (node, delta) => this.changeLinks(node, delta);
|
|
120
136
|
fs.onChange = (node) => {
|
|
121
137
|
if (!this.detached.has(node)) {
|
|
@@ -160,10 +176,18 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
160
176
|
setTerminalFailureListener(listener: (reason: unknown) => void): void {
|
|
161
177
|
this.storage.setTerminalFailureListener?.(listener);
|
|
162
178
|
}
|
|
179
|
+
private hasPendingChanges(): boolean {
|
|
180
|
+
if (this.dirty.size > 0 || this.removed.size > 0 || this.shared.publication) return true;
|
|
181
|
+
return (
|
|
182
|
+
this.idleChangeSequence === undefined ||
|
|
183
|
+
this.shared.changeSequence !== this.idleChangeSequence
|
|
184
|
+
);
|
|
185
|
+
}
|
|
163
186
|
private startTimer(intervalMs: number): void {
|
|
164
187
|
if (intervalMs > 0)
|
|
165
188
|
this.timer = setInterval(() => {
|
|
166
|
-
if (!this.stopped && !this.pendingCheckpoints
|
|
189
|
+
if (!this.stopped && !this.pendingCheckpoints && this.hasPendingChanges())
|
|
190
|
+
void this.checkpoint().catch(() => {});
|
|
167
191
|
}, intervalMs);
|
|
168
192
|
}
|
|
169
193
|
static async open(
|
|
@@ -232,6 +256,7 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
232
256
|
else this.links.delete(node.ino);
|
|
233
257
|
if (delta === 1 && previous === 0) {
|
|
234
258
|
this.nodes.set(node.ino, node);
|
|
259
|
+
this.namespaceBytes += this.nodeMetadataBytes(node);
|
|
235
260
|
if (node.identity) this.byFileId.set(node.identity.id, node);
|
|
236
261
|
this.detached.delete(node);
|
|
237
262
|
const key = this.detachedKeys.get(node.ino);
|
|
@@ -249,6 +274,7 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
249
274
|
for (const child of node.children.values()) this.changeLinks(child, 1);
|
|
250
275
|
} else if (delta === -1 && count === 0) {
|
|
251
276
|
this.nodes.delete(node.ino);
|
|
277
|
+
this.namespaceBytes -= this.nodeMetadataBytes(node);
|
|
252
278
|
this.dirty.delete(node);
|
|
253
279
|
this.detached.add(node);
|
|
254
280
|
this.removed.add(node.ino);
|
|
@@ -486,9 +512,7 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
486
512
|
return this.ownWork(load);
|
|
487
513
|
}
|
|
488
514
|
private namespaceUnits(): number {
|
|
489
|
-
|
|
490
|
-
for (const node of this.nodes.values()) units += this.nodeMetadataBytes(node) * 4;
|
|
491
|
-
return units;
|
|
515
|
+
return this.namespaceBytes * 4;
|
|
492
516
|
}
|
|
493
517
|
private configureDirty(): void {
|
|
494
518
|
if (this.dirtyLimits === false) return;
|
|
@@ -501,7 +525,10 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
501
525
|
override attachShared(...args: Parameters<MemoryBackend["attachShared"]>): void {
|
|
502
526
|
const previous = this.shared;
|
|
503
527
|
super.attachShared(...args);
|
|
504
|
-
if (previous !== this.shared)
|
|
528
|
+
if (previous !== this.shared) {
|
|
529
|
+
this.idleChangeSequence = undefined;
|
|
530
|
+
this.configureDirty();
|
|
531
|
+
}
|
|
505
532
|
}
|
|
506
533
|
private namespaceGrowth(plan: NamespacePlan): number {
|
|
507
534
|
let units = plan.created.reduce((sum, node) => sum + this.nodeMetadataBytes(node), 0);
|
|
@@ -689,13 +716,18 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
689
716
|
void work.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
|
|
690
717
|
});
|
|
691
718
|
}
|
|
719
|
+
private cachedDirEntryBytes(node: DirNode): number {
|
|
720
|
+
let entryBytes = this.dirEntryBytes.get(node);
|
|
721
|
+
if (entryBytes !== undefined) return entryBytes;
|
|
722
|
+
entryBytes = 0;
|
|
723
|
+
for (const name of node.children.keys()) entryBytes += 64 + name.length * 2;
|
|
724
|
+
this.dirEntryBytes.set(node, entryBytes);
|
|
725
|
+
return entryBytes;
|
|
726
|
+
}
|
|
692
727
|
private nodeMetadataBytes(node: Node): number {
|
|
693
|
-
|
|
694
|
-
if (node.kind === "
|
|
695
|
-
|
|
696
|
-
if (node.kind !== "dir") return bytes;
|
|
697
|
-
for (const name of node.children.keys()) bytes += 64 + name.length * 2;
|
|
698
|
-
return bytes;
|
|
728
|
+
if (node.kind === "symlink") return 256 + node.target.length * 2 + utf8ByteLength(node.target);
|
|
729
|
+
if (node.kind === "dir") return 256 + this.cachedDirEntryBytes(node);
|
|
730
|
+
return 256;
|
|
699
731
|
}
|
|
700
732
|
private pinMetadataBytes(): number {
|
|
701
733
|
let bytes = 0;
|
|
@@ -754,11 +786,37 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
754
786
|
return true;
|
|
755
787
|
}
|
|
756
788
|
private invalidateCapturedState(): void {
|
|
789
|
+
this.idleChangeSequence = undefined;
|
|
757
790
|
this.keys = new Map(this.committedKeys);
|
|
758
791
|
// Cold baselines still identify retained durable bytes. Only resident captures need retry.
|
|
759
792
|
for (const handle of this.revisions.keys())
|
|
760
793
|
if (this.shared.isResident(handle)) this.revisions.delete(handle);
|
|
761
794
|
}
|
|
795
|
+
private reconcileDirtyAfterCommit(revision: number): void {
|
|
796
|
+
if (!this.shared.dirtyAdmission.enabled || this.pendingCheckpoints > 0) return;
|
|
797
|
+
const namespace = this.namespaceUnits();
|
|
798
|
+
if (
|
|
799
|
+
this.shared.dirtyAdmission.revision === revision &&
|
|
800
|
+
!this.dirty.size &&
|
|
801
|
+
!this.removed.size
|
|
802
|
+
) {
|
|
803
|
+
this.shared.reconcileDirty(this.registeredNodes.keys(), namespace, 0, () => false);
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
let dirtyNamespace = this.removed.size * 256;
|
|
807
|
+
for (const node of this.dirty) dirtyNamespace += this.nodeMetadataBytes(node);
|
|
808
|
+
this.shared.reconcileDirty(
|
|
809
|
+
this.registeredNodes.keys(),
|
|
810
|
+
namespace,
|
|
811
|
+
dirtyNamespace * 4,
|
|
812
|
+
(handle, kind, currentRevision, dirtyMark) => {
|
|
813
|
+
if (kind === 1) return currentRevision !== this.revisions.get(handle);
|
|
814
|
+
if (dirtyMark !== 1) return false;
|
|
815
|
+
const node = this.registeredNodes.get(handle);
|
|
816
|
+
return node !== undefined && this.dirty.has(node);
|
|
817
|
+
},
|
|
818
|
+
);
|
|
819
|
+
}
|
|
762
820
|
private reserveCheckpoint(request: CheckpointRequest): Promise<void> {
|
|
763
821
|
const cost = { ...this.captureCost(request) };
|
|
764
822
|
this.shared.dirtyAdmission.reserveCopy(cost, this.shared.resources().usedBytes);
|
|
@@ -772,13 +830,21 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
772
830
|
const revision = this.includesDetachedWriters(request)
|
|
773
831
|
? this.shared.dirtyAdmission.revision
|
|
774
832
|
: undefined;
|
|
833
|
+
const capturedChangeSeq = this.shared.changeSequence;
|
|
775
834
|
try {
|
|
776
835
|
return this.captureReserved(request, reservation, cost)
|
|
777
836
|
.then(() => {
|
|
778
|
-
if (
|
|
779
|
-
this.shared.mutate(undefined, undefined, () =>
|
|
780
|
-
|
|
781
|
-
|
|
837
|
+
if (this.stopped) return;
|
|
838
|
+
this.shared.mutate(undefined, undefined, () => {
|
|
839
|
+
if (
|
|
840
|
+
!this.pendingCheckpoints &&
|
|
841
|
+
!this.dirty.size &&
|
|
842
|
+
!this.removed.size &&
|
|
843
|
+
this.shared.changeSequence === capturedChangeSeq
|
|
844
|
+
)
|
|
845
|
+
this.idleChangeSequence = capturedChangeSeq;
|
|
846
|
+
if (revision !== undefined) this.reconcileDirtyAfterCommit(revision);
|
|
847
|
+
});
|
|
782
848
|
})
|
|
783
849
|
.finally(() => {
|
|
784
850
|
this.pressure.release(reservation);
|