@waffle-os/runtime 0.0.155 → 0.0.157
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/filesystem-backend.ts +11 -1
- package/browser/preview-protocol.ts +2 -0
- package/browser/preview.ts +128 -18
- package/browser/runtime-terminals.ts +65 -24
- package/browser/shared-fs.ts +141 -14
- package/browser/worker.ts +73 -6
- package/core/descriptors.ts +19 -2
- package/core/devices.ts +237 -2
- package/core/fd-reopen.ts +105 -20
- package/core/filesystem.ts +79 -0
- package/core/index.ts +4 -0
- package/core/open-file.ts +212 -18
- package/core/wasi.ts +449 -47
- package/dist/browser/filesystem-backend.js +4 -2
- package/dist/browser/preview.js +120 -17
- package/dist/browser/runtime-terminals.js +53 -10
- package/dist/browser/shared-fs.js +124 -11
- package/dist/browser/worker.js +59 -6
- package/dist/core/descriptors.js +9 -1
- package/dist/core/devices.js +211 -1
- package/dist/core/fd-reopen.js +79 -18
- package/dist/core/filesystem.js +14 -0
- package/dist/core/open-file.js +166 -13
- package/dist/core/wasi.js +402 -29
- package/dist/fs/backend.js +51 -3
- package/dist/fs/client.js +204 -7
- package/dist/fs/cold-read-cache.js +23 -0
- package/dist/fs/index.js +21 -1
- package/dist/fs/indexeddb.js +30 -2
- package/dist/fs/locks.js +1112 -0
- package/dist/fs/memory.js +126 -2
- package/dist/fs/multitab-backend.js +317 -196
- package/dist/fs/multitab-protocol.js +2 -0
- package/dist/fs/persistent.js +32 -7
- package/dist/fs/protocol.js +9 -3
- package/dist/fs/service.js +387 -22
- package/dist/fs/shared.js +54 -1
- package/dist/fs/storage-geometry.js +2 -0
- package/dist/interrupt/rewriter-internal.js +4 -1
- package/dist/proc/abi.js +80 -6
- package/dist/proc/errors.js +1 -0
- package/dist/proc/filesystem-codec.js +40 -9
- package/dist/proc/index.js +10 -1
- package/dist/proc/pty.js +193 -0
- package/dist/proc/worker-client.js +11 -2
- package/dist/proc/worker-pipes.js +51 -0
- package/dist/runtime.js +11 -2
- package/dist/search/engine.js +53 -0
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +54 -2
- package/fs/client.ts +230 -6
- package/fs/cold-read-cache.ts +38 -0
- package/fs/index.ts +22 -1
- package/fs/indexeddb.ts +36 -2
- package/fs/locks.ts +1406 -0
- package/fs/memory.ts +132 -1
- package/fs/multitab-backend.ts +563 -304
- package/fs/multitab-protocol.ts +2 -0
- package/fs/persistent.ts +46 -4
- package/fs/protocol.ts +17 -5
- package/fs/service.ts +469 -23
- package/fs/shared.ts +60 -2
- package/fs/storage-geometry.ts +3 -0
- package/index.ts +2 -0
- package/interrupt/rewriter-internal.ts +4 -1
- package/package.json +2 -2
- package/proc/abi.ts +79 -6
- package/proc/errors.ts +1 -0
- package/proc/filesystem-codec.ts +39 -9
- package/proc/index.ts +11 -0
- package/proc/pty.ts +227 -0
- package/proc/worker-client.ts +28 -6
- package/proc/worker-pipes.ts +57 -0
- package/runtime.ts +12 -2
- package/search/engine.ts +49 -0
- package/testing/indexeddb-storage-browser.ts +173 -2
- package/tooling/program/program-transformer.json +1 -1
|
@@ -13,8 +13,11 @@ import {
|
|
|
13
13
|
CLIENT_MODE,
|
|
14
14
|
CLIENT_MODE_NO_CLIENT_CACHE,
|
|
15
15
|
CLIENT_MODE_NO_DIRECT_IO,
|
|
16
|
-
CLIENT_MODE_NO_WATCH_DRAIN
|
|
16
|
+
CLIENT_MODE_NO_WATCH_DRAIN,
|
|
17
|
+
machineIdFromWorkspace,
|
|
18
|
+
writeSharedMachineId
|
|
17
19
|
} from "./shared.js";
|
|
20
|
+
import { validateMachineId } from "../core/devices.js";
|
|
18
21
|
import { HEADER_WORDS, WATCH_OFFSET } from "./storage-geometry.js";
|
|
19
22
|
import { WatchRing } from "./watch-ring.js";
|
|
20
23
|
import { WatchService } from "./watch-service.js";
|
|
@@ -406,6 +409,7 @@ export class MultiTabBackend {
|
|
|
406
409
|
peerPendingWatchCloses = new Set;
|
|
407
410
|
peerPendingUploadAborts = new Set;
|
|
408
411
|
supportsSockets = false;
|
|
412
|
+
supportsLocks = false;
|
|
409
413
|
asyncErrorsAreClientResults = true;
|
|
410
414
|
peerFailoverInFlight;
|
|
411
415
|
pendingDirectHostReady;
|
|
@@ -529,6 +533,27 @@ export class MultiTabBackend {
|
|
|
529
533
|
const ring = new WatchRing(buffer, WATCH_OFFSET);
|
|
530
534
|
this.watchCells = ring.cells;
|
|
531
535
|
this.syncClientMode();
|
|
536
|
+
this.syncMachineId();
|
|
537
|
+
}
|
|
538
|
+
resolveMachineId() {
|
|
539
|
+
if (this.options.machineId !== undefined)
|
|
540
|
+
return validateMachineId(this.options.machineId);
|
|
541
|
+
if (this.cohortWorkspaceId !== undefined)
|
|
542
|
+
return machineIdFromWorkspace(this.cohortWorkspaceId);
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
syncMachineId() {
|
|
546
|
+
const id = this.resolveMachineId();
|
|
547
|
+
if (!id)
|
|
548
|
+
return;
|
|
549
|
+
if (this.persistent) {
|
|
550
|
+
this.persistent.shared.setMachineId(id);
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
if (this.sharedBuffer) {
|
|
554
|
+
const header = new Int32Array(this.sharedBuffer, 0, HEADER_WORDS);
|
|
555
|
+
writeSharedMachineId(header, id);
|
|
556
|
+
}
|
|
532
557
|
}
|
|
533
558
|
syncClientMode() {
|
|
534
559
|
const mode = CLIENT_MODE_NO_DIRECT_IO | CLIENT_MODE_NO_CLIENT_CACHE | (this.roleState === "host" ? 0 : CLIENT_MODE_NO_WATCH_DRAIN);
|
|
@@ -1067,6 +1092,7 @@ export class MultiTabBackend {
|
|
|
1067
1092
|
await Promise.allSettled(Array.from(this.activeBackgroundTasks));
|
|
1068
1093
|
}
|
|
1069
1094
|
if (this.terminalFailure) {
|
|
1095
|
+
this.persistent?.dispose();
|
|
1070
1096
|
throw wrapReleaseError(this.terminalFailure.reason, false);
|
|
1071
1097
|
}
|
|
1072
1098
|
if (this.disposedReleaseError) {
|
|
@@ -1296,6 +1322,7 @@ export class MultiTabBackend {
|
|
|
1296
1322
|
maxBytes: this.sharedMaxBytes ?? DEFAULT_SHARED_MAX_BYTES,
|
|
1297
1323
|
segments: this.retainedSegments
|
|
1298
1324
|
} : undefined,
|
|
1325
|
+
machineId: this.options.machineId,
|
|
1299
1326
|
signal
|
|
1300
1327
|
});
|
|
1301
1328
|
backend.setTerminalFailureListener((reason) => {
|
|
@@ -1402,8 +1429,10 @@ export class MultiTabBackend {
|
|
|
1402
1429
|
}
|
|
1403
1430
|
}
|
|
1404
1431
|
async rollbackFailedHostMount(backend, storage) {
|
|
1405
|
-
if (this.terminalFailure)
|
|
1432
|
+
if (this.terminalFailure) {
|
|
1433
|
+
backend?.dispose();
|
|
1406
1434
|
throw wrapReleaseError(this.terminalFailure.reason, false);
|
|
1435
|
+
}
|
|
1407
1436
|
if (backend) {
|
|
1408
1437
|
this.setPersistent(undefined);
|
|
1409
1438
|
this.watchService = undefined;
|
|
@@ -1567,6 +1596,7 @@ export class MultiTabBackend {
|
|
|
1567
1596
|
this.leaseMonoDeadline = 0;
|
|
1568
1597
|
this.leaseWallDeadline = 0;
|
|
1569
1598
|
this.stopHostTimers();
|
|
1599
|
+
this.persistent?.dispose();
|
|
1570
1600
|
const msg = describeReason(reason);
|
|
1571
1601
|
this.broadcastControl({
|
|
1572
1602
|
kind: "host-failed",
|
|
@@ -2585,6 +2615,7 @@ export class MultiTabBackend {
|
|
|
2585
2615
|
commitCohortEpoch(workspaceId, storageEpoch) {
|
|
2586
2616
|
this.cohortWorkspaceId = workspaceId;
|
|
2587
2617
|
this.cohortLastAcceptedEpoch = Math.max(this.cohortLastAcceptedEpoch ?? 0, storageEpoch);
|
|
2618
|
+
this.syncMachineId();
|
|
2588
2619
|
}
|
|
2589
2620
|
broadcastControl(message) {
|
|
2590
2621
|
if (this.hooks.onControlSend?.(message) === false)
|
|
@@ -3318,22 +3349,15 @@ export class MultiTabBackend {
|
|
|
3318
3349
|
upload.receivedBytes += msg.chunk.byteLength;
|
|
3319
3350
|
return { ok: true };
|
|
3320
3351
|
}
|
|
3321
|
-
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
|
|
3325
|
-
|
|
3326
|
-
|
|
3327
|
-
|
|
3328
|
-
if (this.ownershipLost || this.role !== "host" || this.hostTerm !== admittedTerm) {
|
|
3329
|
-
replyErr(11, "host term ended");
|
|
3330
|
-
return false;
|
|
3331
|
-
}
|
|
3332
|
-
if (requireLease && !this.isLeaseValid()) {
|
|
3333
|
-
replyErr(11, "host lease expired");
|
|
3352
|
+
async ensureChunkGetLease(session, admittedTerm, budget, replyErr, publish) {
|
|
3353
|
+
try {
|
|
3354
|
+
await this.ensurePeerLeaseAndPublish(session, admittedTerm, budget, publish ?? (() => {}));
|
|
3355
|
+
return true;
|
|
3356
|
+
} catch (error) {
|
|
3357
|
+
if (!session.revoked)
|
|
3358
|
+
replyErr(errorToErrno(error), describeReason(error));
|
|
3334
3359
|
return false;
|
|
3335
3360
|
}
|
|
3336
|
-
return true;
|
|
3337
3361
|
}
|
|
3338
3362
|
async handleHostChunkGet(session, msg) {
|
|
3339
3363
|
const slot = this.hostCreditSlots.get(session.peerId);
|
|
@@ -3342,6 +3366,7 @@ export class MultiTabBackend {
|
|
|
3342
3366
|
}
|
|
3343
3367
|
session.activeReqs.add(msg.reqId);
|
|
3344
3368
|
const admittedTerm = session.hostTerm;
|
|
3369
|
+
const budget = {};
|
|
3345
3370
|
const reclaimDownload = () => {
|
|
3346
3371
|
const existing = session.downloads.get(msg.downloadId);
|
|
3347
3372
|
if (existing && session.downloads.delete(msg.downloadId)) {
|
|
@@ -3365,43 +3390,11 @@ export class MultiTabBackend {
|
|
|
3365
3390
|
});
|
|
3366
3391
|
};
|
|
3367
3392
|
try {
|
|
3368
|
-
if (!this.
|
|
3393
|
+
if (!await this.ensureChunkGetLease(session, admittedTerm, budget, replyErr))
|
|
3369
3394
|
return;
|
|
3370
|
-
if (!this.isLeaseValid()) {
|
|
3371
|
-
await this.renewLeaseNow(this.currentAttemptId);
|
|
3372
|
-
if (!this.validateChunkGetState(session, admittedTerm, replyErr, true))
|
|
3373
|
-
return;
|
|
3374
|
-
}
|
|
3375
3395
|
this.enterHostAdmitted();
|
|
3376
3396
|
try {
|
|
3377
|
-
|
|
3378
|
-
if (!dl) {
|
|
3379
|
-
replyErr(11);
|
|
3380
|
-
return;
|
|
3381
|
-
}
|
|
3382
|
-
if (msg.offset < 0 || msg.offset >= dl.bytes.byteLength || msg.length <= 0) {
|
|
3383
|
-
replyErr(28);
|
|
3384
|
-
return;
|
|
3385
|
-
}
|
|
3386
|
-
const end = Math.min(dl.bytes.byteLength, msg.offset + Math.min(msg.length, MAX_FRAME_BYTES));
|
|
3387
|
-
const slice = unsharedBytes(dl.bytes.subarray(msg.offset, end));
|
|
3388
|
-
await this.hooks.beforeHostReplyPublish?.("chunk-get", msg.reqId);
|
|
3389
|
-
if (!this.validateChunkGetState(session, admittedTerm, replyErr, true))
|
|
3390
|
-
return;
|
|
3391
|
-
if (end >= dl.bytes.byteLength) {
|
|
3392
|
-
reclaimDownload();
|
|
3393
|
-
}
|
|
3394
|
-
this.sendPeerFrame(session.peerId, session.channel, {
|
|
3395
|
-
kind: "res",
|
|
3396
|
-
hostTerm: formatTerm(admittedTerm),
|
|
3397
|
-
sessionId: session.sessionId,
|
|
3398
|
-
reqId: msg.reqId,
|
|
3399
|
-
ok: true,
|
|
3400
|
-
data: slice,
|
|
3401
|
-
binary: true,
|
|
3402
|
-
creditFrames: 1,
|
|
3403
|
-
creditBytes: 0
|
|
3404
|
-
});
|
|
3397
|
+
await this.serveHostChunkGet(session, msg, admittedTerm, budget, reclaimDownload, replyErr);
|
|
3405
3398
|
} finally {
|
|
3406
3399
|
this.leaveHostAdmitted();
|
|
3407
3400
|
}
|
|
@@ -3410,6 +3403,39 @@ export class MultiTabBackend {
|
|
|
3410
3403
|
this.settleHostInFlightCredit(session, 1, 0);
|
|
3411
3404
|
}
|
|
3412
3405
|
}
|
|
3406
|
+
async serveHostChunkGet(session, msg, admittedTerm, budget, reclaimDownload, replyErr) {
|
|
3407
|
+
const dl = session.downloads.get(msg.downloadId);
|
|
3408
|
+
if (!dl) {
|
|
3409
|
+
replyErr(11);
|
|
3410
|
+
return;
|
|
3411
|
+
}
|
|
3412
|
+
if (msg.offset < 0 || msg.offset >= dl.bytes.byteLength || msg.length <= 0) {
|
|
3413
|
+
replyErr(28);
|
|
3414
|
+
return;
|
|
3415
|
+
}
|
|
3416
|
+
const end = Math.min(dl.bytes.byteLength, msg.offset + Math.min(msg.length, MAX_FRAME_BYTES));
|
|
3417
|
+
const slice = unsharedBytes(dl.bytes.subarray(msg.offset, end));
|
|
3418
|
+
const isFinalChunk = end >= dl.bytes.byteLength;
|
|
3419
|
+
await this.hooks.beforeHostReplyPublish?.("chunk-get", msg.reqId);
|
|
3420
|
+
await this.ensureChunkGetLease(session, admittedTerm, budget, replyErr, () => {
|
|
3421
|
+
this.publishChunkGetFrame(session, msg.reqId, admittedTerm, slice, isFinalChunk, reclaimDownload);
|
|
3422
|
+
});
|
|
3423
|
+
}
|
|
3424
|
+
publishChunkGetFrame(session, reqId, admittedTerm, slice, isFinalChunk, reclaimDownload) {
|
|
3425
|
+
if (isFinalChunk)
|
|
3426
|
+
reclaimDownload();
|
|
3427
|
+
this.sendPeerFrame(session.peerId, session.channel, {
|
|
3428
|
+
kind: "res",
|
|
3429
|
+
hostTerm: formatTerm(admittedTerm),
|
|
3430
|
+
sessionId: session.sessionId,
|
|
3431
|
+
reqId,
|
|
3432
|
+
ok: true,
|
|
3433
|
+
data: slice,
|
|
3434
|
+
binary: true,
|
|
3435
|
+
creditFrames: 1,
|
|
3436
|
+
creditBytes: 0
|
|
3437
|
+
});
|
|
3438
|
+
}
|
|
3413
3439
|
async handleHostPeerReq(session, msg) {
|
|
3414
3440
|
const payloadBytes = msg.data?.byteLength ?? 0;
|
|
3415
3441
|
const slot = this.hostCreditSlots.get(session.peerId);
|
|
@@ -3445,8 +3471,9 @@ export class MultiTabBackend {
|
|
|
3445
3471
|
reply({ ok: false, retry: true });
|
|
3446
3472
|
return;
|
|
3447
3473
|
}
|
|
3448
|
-
|
|
3449
|
-
|
|
3474
|
+
const budget = {};
|
|
3475
|
+
while (!this.hostReadyForPeer(session, admittedTerm)) {
|
|
3476
|
+
await this.renewLapsedLease(admittedTerm, () => session.revoked, budget);
|
|
3450
3477
|
if (session.revoked)
|
|
3451
3478
|
return;
|
|
3452
3479
|
if (this.terminalFailure || this.persistent?.isTerminallyFailed()) {
|
|
@@ -3457,14 +3484,10 @@ export class MultiTabBackend {
|
|
|
3457
3484
|
reply({ ok: false, retry: true });
|
|
3458
3485
|
return;
|
|
3459
3486
|
}
|
|
3460
|
-
if (!renewed || !this.isLeaseValid() || this.hostTerm !== admittedTerm) {
|
|
3461
|
-
reply({ ok: false, errno: 11, message: "host lease expired" });
|
|
3462
|
-
return;
|
|
3463
|
-
}
|
|
3464
3487
|
}
|
|
3465
3488
|
this.enterHostAdmitted();
|
|
3466
3489
|
try {
|
|
3467
|
-
await this.executePeerRpcOnHost(session, admittedTerm, msg, reply);
|
|
3490
|
+
await this.executePeerRpcOnHost(session, admittedTerm, msg, reply, budget);
|
|
3468
3491
|
} finally {
|
|
3469
3492
|
this.leaveHostAdmitted();
|
|
3470
3493
|
}
|
|
@@ -3483,64 +3506,108 @@ export class MultiTabBackend {
|
|
|
3483
3506
|
this.settleHostInFlightCredit(session, 1, payloadBytes);
|
|
3484
3507
|
}
|
|
3485
3508
|
}
|
|
3486
|
-
|
|
3509
|
+
hostReadyForPeer(session, admittedTerm) {
|
|
3487
3510
|
if (session.revoked)
|
|
3488
3511
|
throw new FilesystemError(11, "peer session revoked");
|
|
3489
|
-
if (this.terminalFailure || this.persistent?.isTerminallyFailed())
|
|
3512
|
+
if (this.terminalFailure || this.persistent?.isTerminallyFailed()) {
|
|
3490
3513
|
throw new FilesystemError(29, "host storage terminally failed");
|
|
3491
|
-
|
|
3492
|
-
|
|
3514
|
+
}
|
|
3515
|
+
if (this.ownershipLost || this.role !== "host" || this.hostTerm !== admittedTerm || !this.persistent) {
|
|
3516
|
+
throw new FilesystemError(11, "host term ended");
|
|
3517
|
+
}
|
|
3518
|
+
if (this.isLeaseValid())
|
|
3519
|
+
return true;
|
|
3520
|
+
if (this.lapseIsRenewable(admittedTerm))
|
|
3521
|
+
return false;
|
|
3522
|
+
throw new FilesystemError(11, "host lease invalid");
|
|
3523
|
+
}
|
|
3524
|
+
assertPeerAttempt(session, admittedTerm) {
|
|
3525
|
+
if (!this.hostReadyForPeer(session, admittedTerm))
|
|
3526
|
+
throw LEASE_LAPSED;
|
|
3527
|
+
}
|
|
3528
|
+
peerAttemptStale(session, admittedTerm) {
|
|
3529
|
+
if (session.revoked || this.hostTerm !== admittedTerm)
|
|
3530
|
+
return true;
|
|
3531
|
+
return !this.isLeaseValid() && !this.leaseOnlyLapsed(admittedTerm);
|
|
3532
|
+
}
|
|
3533
|
+
async ensurePeerLease(session, admittedTerm, budget) {
|
|
3534
|
+
await this.ensurePeerLeaseAndPublish(session, admittedTerm, budget, () => {});
|
|
3535
|
+
}
|
|
3536
|
+
async ensurePeerLeaseAndPublish(session, admittedTerm, budget, publish) {
|
|
3537
|
+
const cancelled = () => session.revoked;
|
|
3538
|
+
while (!this.hostReadyForPeer(session, admittedTerm)) {
|
|
3539
|
+
await this.renewLapsedLease(admittedTerm, cancelled, budget);
|
|
3540
|
+
}
|
|
3541
|
+
publish();
|
|
3542
|
+
}
|
|
3543
|
+
async retryPeerAfterLeaseRenewal(session, admittedTerm, budget, attempt) {
|
|
3544
|
+
const cancelled = () => session.revoked;
|
|
3545
|
+
for (;; ) {
|
|
3546
|
+
try {
|
|
3547
|
+
return await attempt();
|
|
3548
|
+
} catch (error) {
|
|
3549
|
+
if (error !== LEASE_LAPSED)
|
|
3550
|
+
throw error;
|
|
3551
|
+
}
|
|
3552
|
+
await this.renewLapsedLease(admittedTerm, cancelled, budget);
|
|
3553
|
+
}
|
|
3493
3554
|
}
|
|
3494
|
-
async executePeerRpcOnHost(session, admittedTerm, msg, reply) {
|
|
3555
|
+
async executePeerRpcOnHost(session, admittedTerm, msg, reply, budget) {
|
|
3495
3556
|
if (msg.op === "__rebind") {
|
|
3496
|
-
await this.handleHostPeerRebind(session, admittedTerm, msg, reply);
|
|
3557
|
+
await this.handleHostPeerRebind(session, admittedTerm, msg, reply, budget);
|
|
3497
3558
|
return;
|
|
3498
3559
|
}
|
|
3499
3560
|
if (msg.op === "watchOpen") {
|
|
3500
|
-
await this.handleHostPeerWatchOpen(session, admittedTerm, msg, reply);
|
|
3561
|
+
await this.handleHostPeerWatchOpen(session, admittedTerm, msg, reply, budget);
|
|
3501
3562
|
return;
|
|
3502
3563
|
}
|
|
3503
3564
|
if (msg.op === "watchRead") {
|
|
3504
|
-
await this.handleHostPeerWatchRead(session, admittedTerm, msg, reply);
|
|
3565
|
+
await this.handleHostPeerWatchRead(session, admittedTerm, msg, reply, budget);
|
|
3505
3566
|
return;
|
|
3506
3567
|
}
|
|
3507
3568
|
const { data, reservedUploadBytes } = this.consumeHostStagedUpload(session, msg);
|
|
3508
3569
|
try {
|
|
3509
3570
|
if (msg.op === "readFile") {
|
|
3510
|
-
await this.handleHostPeerReadFile(session, admittedTerm, msg.args[0], msg.reqId, reply);
|
|
3571
|
+
await this.handleHostPeerReadFile(session, admittedTerm, msg.args[0], msg.reqId, reply, budget);
|
|
3511
3572
|
return;
|
|
3512
3573
|
}
|
|
3513
3574
|
if (msg.op === "readdir" || msg.op === "lookup" || msg.op === "listFiles") {
|
|
3514
|
-
await this.handleHostPeerBoundedWireRead(session, admittedTerm, msg.op, msg.args, msg.reqId, reply);
|
|
3575
|
+
await this.handleHostPeerBoundedWireRead(session, admittedTerm, msg.op, msg.args, msg.reqId, reply, budget);
|
|
3515
3576
|
return;
|
|
3516
3577
|
}
|
|
3517
3578
|
if (msg.op === "readdirPage") {
|
|
3518
|
-
await this.handleHostPeerReaddirPage(session, admittedTerm, msg, reply);
|
|
3579
|
+
await this.handleHostPeerReaddirPage(session, admittedTerm, msg, reply, budget);
|
|
3519
3580
|
return;
|
|
3520
3581
|
}
|
|
3521
3582
|
if (msg.op === "flush" || msg.op === "sync") {
|
|
3522
|
-
|
|
3523
|
-
await this.barrier.runBarrier(async () => {
|
|
3524
|
-
await this.hostQueue;
|
|
3525
|
-
this.assertHostOpStillValid(session, admittedTerm);
|
|
3526
|
-
await this.persistent.flush(target);
|
|
3527
|
-
});
|
|
3528
|
-
await this.hooks.beforeHostReplyPublish?.(msg.op, msg.reqId);
|
|
3529
|
-
this.assertHostOpStillValid(session, admittedTerm);
|
|
3530
|
-
reply({ ok: true, value: msg.op === "sync" ? 0 : undefined });
|
|
3583
|
+
await this.handleHostPeerFlush(session, admittedTerm, msg, reply, budget);
|
|
3531
3584
|
return;
|
|
3532
3585
|
}
|
|
3533
3586
|
if (msg.op === "read") {
|
|
3534
|
-
await this.handleHostPeerRead(session, admittedTerm, msg, reply);
|
|
3587
|
+
await this.handleHostPeerRead(session, admittedTerm, msg, reply, budget);
|
|
3535
3588
|
return;
|
|
3536
3589
|
}
|
|
3537
|
-
await this.handleHostPeerGeneral(session, admittedTerm, msg, data, reply);
|
|
3590
|
+
await this.handleHostPeerGeneral(session, admittedTerm, msg, data, reply, budget);
|
|
3538
3591
|
} finally {
|
|
3539
3592
|
if (reservedUploadBytes > 0) {
|
|
3540
3593
|
this.hostUsedUploadBytes = Math.max(0, this.hostUsedUploadBytes - reservedUploadBytes);
|
|
3541
3594
|
}
|
|
3542
3595
|
}
|
|
3543
3596
|
}
|
|
3597
|
+
async handleHostPeerFlush(session, admittedTerm, msg, reply, budget) {
|
|
3598
|
+
const target = msg.op === "sync" ? msg.args[0] : msg.args[0];
|
|
3599
|
+
await this.barrier.runBarrier(async () => {
|
|
3600
|
+
await this.retryPeerAfterLeaseRenewal(session, admittedTerm, budget, async () => {
|
|
3601
|
+
await this.hostQueue;
|
|
3602
|
+
this.assertPeerAttempt(session, admittedTerm);
|
|
3603
|
+
});
|
|
3604
|
+
await this.persistent.flush(target);
|
|
3605
|
+
});
|
|
3606
|
+
await this.hooks.beforeHostReplyPublish?.(msg.op, msg.reqId);
|
|
3607
|
+
await this.ensurePeerLeaseAndPublish(session, admittedTerm, budget, () => {
|
|
3608
|
+
reply({ ok: true, value: msg.op === "sync" ? 0 : undefined });
|
|
3609
|
+
});
|
|
3610
|
+
}
|
|
3544
3611
|
consumeHostStagedUpload(session, msg) {
|
|
3545
3612
|
if (!msg.uploadId)
|
|
3546
3613
|
return { data: msg.data ?? new Uint8Array, reservedUploadBytes: 0 };
|
|
@@ -3552,44 +3619,57 @@ export class MultiTabBackend {
|
|
|
3552
3619
|
session.uploads.delete(msg.uploadId);
|
|
3553
3620
|
return { data: upload.buffer, reservedUploadBytes: upload.totalBytes };
|
|
3554
3621
|
}
|
|
3555
|
-
|
|
3622
|
+
rebindPeerFiles(session, persistent, fileIds, bound) {
|
|
3623
|
+
for (const fileId of fileIds) {
|
|
3624
|
+
const node = persistent.nodeByFileId(fileId);
|
|
3625
|
+
if (!node) {
|
|
3626
|
+
bound.push(null);
|
|
3627
|
+
continue;
|
|
3628
|
+
}
|
|
3629
|
+
const remoteHandle = persistent.fs.handleForNode(node);
|
|
3630
|
+
bound.push(this.mintHostPeerPin(session, remoteHandle, fileId));
|
|
3631
|
+
}
|
|
3632
|
+
}
|
|
3633
|
+
async handleHostPeerRebind(session, admittedTerm, msg, reply, budget) {
|
|
3556
3634
|
const persistent = this.persistent;
|
|
3557
3635
|
const fileIds = msg.args[0];
|
|
3558
3636
|
const bound = [];
|
|
3559
|
-
await this.enqueueHostSerial(() => {
|
|
3560
|
-
this.
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
if (!node) {
|
|
3564
|
-
bound.push(null);
|
|
3565
|
-
continue;
|
|
3566
|
-
}
|
|
3567
|
-
const remoteHandle = persistent.fs.handleForNode(node);
|
|
3568
|
-
bound.push(this.mintHostPeerPin(session, remoteHandle, fileId));
|
|
3569
|
-
}
|
|
3570
|
-
});
|
|
3637
|
+
await this.retryPeerAfterLeaseRenewal(session, admittedTerm, budget, () => this.enqueueHostSerial(() => {
|
|
3638
|
+
this.assertPeerAttempt(session, admittedTerm);
|
|
3639
|
+
this.rebindPeerFiles(session, persistent, fileIds, bound);
|
|
3640
|
+
}));
|
|
3571
3641
|
await this.hooks.beforeHostReplyPublish?.(msg.op, msg.reqId);
|
|
3572
|
-
|
|
3573
|
-
|
|
3642
|
+
try {
|
|
3643
|
+
await this.ensurePeerLeaseAndPublish(session, admittedTerm, budget, () => {
|
|
3644
|
+
reply({ ok: true, value: bound });
|
|
3645
|
+
});
|
|
3646
|
+
} catch (error) {
|
|
3647
|
+
const mintedTokens = bound.flatMap((item) => item ? [item.pinToken] : []);
|
|
3648
|
+
this.rollbackMintedPeerTokens(session, persistent, mintedTokens);
|
|
3649
|
+
throw error;
|
|
3650
|
+
}
|
|
3574
3651
|
}
|
|
3575
|
-
async handleHostPeerWatchOpen(session, admittedTerm, msg, reply) {
|
|
3652
|
+
async handleHostPeerWatchOpen(session, admittedTerm, msg, reply, budget) {
|
|
3576
3653
|
const persistent = this.persistent;
|
|
3577
3654
|
const [remoteHandle, recursive] = msg.args;
|
|
3578
3655
|
let hostWatchId = 0;
|
|
3579
|
-
await this.enqueueHostSerial(() => {
|
|
3580
|
-
this.
|
|
3656
|
+
await this.retryPeerAfterLeaseRenewal(session, admittedTerm, budget, () => this.enqueueHostSerial(() => {
|
|
3657
|
+
this.assertPeerAttempt(session, admittedTerm);
|
|
3581
3658
|
this.watchService ??= new WatchService(persistent);
|
|
3582
|
-
hostWatchId = this.watchService.open(session.ownerId, remoteHandle, recursive, () =>
|
|
3659
|
+
hostWatchId = this.watchService.open(session.ownerId, remoteHandle, recursive, () => this.peerAttemptStale(session, admittedTerm));
|
|
3583
3660
|
session.watchIds.add(hostWatchId);
|
|
3584
|
-
});
|
|
3661
|
+
}));
|
|
3585
3662
|
await this.hooks.beforeHostReplyPublish?.(msg.op, msg.reqId);
|
|
3586
|
-
|
|
3663
|
+
try {
|
|
3664
|
+
await this.ensurePeerLeaseAndPublish(session, admittedTerm, budget, () => {
|
|
3665
|
+
reply({ ok: true, value: hostWatchId });
|
|
3666
|
+
});
|
|
3667
|
+
} catch (error) {
|
|
3587
3668
|
if (session.watchIds.delete(hostWatchId)) {
|
|
3588
3669
|
this.watchService?.close(session.ownerId, hostWatchId);
|
|
3589
3670
|
}
|
|
3590
|
-
|
|
3671
|
+
throw error;
|
|
3591
3672
|
}
|
|
3592
|
-
reply({ ok: true, value: hostWatchId });
|
|
3593
3673
|
}
|
|
3594
3674
|
rollbackHeldWatchAck(session, ackId) {
|
|
3595
3675
|
if (ackId === undefined)
|
|
@@ -3601,21 +3681,28 @@ export class MultiTabBackend {
|
|
|
3601
3681
|
entry.held.rollback();
|
|
3602
3682
|
this.scheduleHostWatchWake(session);
|
|
3603
3683
|
}
|
|
3604
|
-
async handleHostPeerWatchRead(session, admittedTerm, msg, reply) {
|
|
3684
|
+
async handleHostPeerWatchRead(session, admittedTerm, msg, reply, budget) {
|
|
3605
3685
|
const [hostWatchId] = msg.args;
|
|
3606
3686
|
let held;
|
|
3607
|
-
await this.enqueueHostSerial(() => {
|
|
3608
|
-
this.
|
|
3687
|
+
await this.retryPeerAfterLeaseRenewal(session, admittedTerm, budget, () => this.enqueueHostSerial(() => {
|
|
3688
|
+
this.assertPeerAttempt(session, admittedTerm);
|
|
3609
3689
|
if (!this.watchService || !session.watchIds.has(hostWatchId))
|
|
3610
3690
|
throw new FilesystemError(8);
|
|
3611
3691
|
held = this.watchService.readHeld(session.ownerId, hostWatchId);
|
|
3612
|
-
});
|
|
3692
|
+
}));
|
|
3613
3693
|
await this.hooks.beforeHostReplyPublish?.(msg.op, msg.reqId);
|
|
3614
|
-
|
|
3694
|
+
try {
|
|
3695
|
+
await this.ensurePeerLeaseAndPublish(session, admittedTerm, budget, () => {
|
|
3696
|
+
if (!held)
|
|
3697
|
+
return;
|
|
3698
|
+
this.publishHeldWatchBatch(session, held, hostWatchId, reply);
|
|
3699
|
+
});
|
|
3700
|
+
} catch (error) {
|
|
3615
3701
|
held?.rollback();
|
|
3616
|
-
|
|
3617
|
-
return;
|
|
3702
|
+
throw error;
|
|
3618
3703
|
}
|
|
3704
|
+
}
|
|
3705
|
+
publishHeldWatchBatch(session, held, hostWatchId, reply) {
|
|
3619
3706
|
const batch = held.batch;
|
|
3620
3707
|
const encoded = pack(batch);
|
|
3621
3708
|
let ackId;
|
|
@@ -3643,30 +3730,34 @@ export class MultiTabBackend {
|
|
|
3643
3730
|
firstChunk: staged.firstChunk
|
|
3644
3731
|
});
|
|
3645
3732
|
}
|
|
3646
|
-
|
|
3733
|
+
reserveStagedDownload(session, byteLength) {
|
|
3734
|
+
if (byteLength <= MAX_FRAME_BYTES)
|
|
3735
|
+
return 0;
|
|
3736
|
+
if (session.downloads.size >= MAX_CONCURRENT_DOWNLOADS_PER_PEER || this.hostUsedDownloadBytes + byteLength > MAX_STAGED_DOWNLOAD_BYTES) {
|
|
3737
|
+
throw new FilesystemError(6, "staged download pool exhausted");
|
|
3738
|
+
}
|
|
3739
|
+
this.hostUsedDownloadBytes += byteLength;
|
|
3740
|
+
return byteLength;
|
|
3741
|
+
}
|
|
3742
|
+
async handleHostPeerReaddirPage(session, admittedTerm, msg, reply, budget) {
|
|
3647
3743
|
const persistent = this.persistent;
|
|
3648
3744
|
const [remoteHandle, rawCookie, byteLength] = msg.args;
|
|
3649
3745
|
const tag = msg.responseCookieTag ? parseTerm(msg.responseCookieTag) : undefined;
|
|
3650
3746
|
let bytes;
|
|
3651
3747
|
let reserved = 0;
|
|
3652
|
-
await this.enqueueHostSerial(() => {
|
|
3653
|
-
this.
|
|
3748
|
+
await this.retryPeerAfterLeaseRenewal(session, admittedTerm, budget, () => this.enqueueHostSerial(() => {
|
|
3749
|
+
this.assertPeerAttempt(session, admittedTerm);
|
|
3654
3750
|
const remaining = Math.max(0, MAX_STAGED_DOWNLOAD_BYTES - this.hostUsedDownloadBytes);
|
|
3655
3751
|
bytes = persistent.fs.readdirPage(remoteHandle, rawCookie, byteLength, tag, remaining, MAX_STAGED_DOWNLOAD_BYTES);
|
|
3656
|
-
|
|
3657
|
-
|
|
3658
|
-
throw new FilesystemError(6, "staged download pool exhausted");
|
|
3659
|
-
}
|
|
3660
|
-
reserved = bytes.byteLength;
|
|
3661
|
-
this.hostUsedDownloadBytes += reserved;
|
|
3662
|
-
}
|
|
3663
|
-
});
|
|
3752
|
+
reserved = this.reserveStagedDownload(session, bytes.byteLength);
|
|
3753
|
+
}));
|
|
3664
3754
|
try {
|
|
3665
3755
|
await this.hooks.beforeHostReplyPublish?.(msg.op, msg.reqId);
|
|
3666
|
-
this.
|
|
3667
|
-
|
|
3668
|
-
|
|
3669
|
-
|
|
3756
|
+
await this.ensurePeerLeaseAndPublish(session, admittedTerm, budget, () => {
|
|
3757
|
+
const transferReserved = reserved;
|
|
3758
|
+
reserved = 0;
|
|
3759
|
+
this.replyWithBytes(session, bytes, transferReserved, true, false, reply);
|
|
3760
|
+
});
|
|
3670
3761
|
} catch (error) {
|
|
3671
3762
|
if (reserved > 0) {
|
|
3672
3763
|
this.hostUsedDownloadBytes = Math.max(0, this.hostUsedDownloadBytes - reserved);
|
|
@@ -3674,33 +3765,46 @@ export class MultiTabBackend {
|
|
|
3674
3765
|
throw error;
|
|
3675
3766
|
}
|
|
3676
3767
|
}
|
|
3677
|
-
async handleHostPeerRead(session, admittedTerm, msg, reply) {
|
|
3768
|
+
async handleHostPeerRead(session, admittedTerm, msg, reply, budget) {
|
|
3678
3769
|
const persistent = this.persistent;
|
|
3679
3770
|
const [remoteHandle, length, offset] = msg.args;
|
|
3680
3771
|
for (;; ) {
|
|
3681
|
-
this.
|
|
3772
|
+
await this.ensurePeerLease(session, admittedTerm, budget);
|
|
3682
3773
|
const startVer = this.readVersion();
|
|
3683
|
-
const prep = persistent.prepareDescriptorRead(remoteHandle, offset, length, () =>
|
|
3774
|
+
const prep = persistent.prepareDescriptorRead(remoteHandle, offset, length, () => this.peerAttemptStale(session, admittedTerm));
|
|
3684
3775
|
if (prep) {
|
|
3685
|
-
const accepted = await this.
|
|
3776
|
+
const accepted = await this.tryColdPeerRead(session, admittedTerm, budget, prep, startVer);
|
|
3686
3777
|
if (accepted === undefined)
|
|
3687
3778
|
continue;
|
|
3688
3779
|
await this.hooks.beforeHostReplyPublish?.(msg.op, msg.reqId);
|
|
3689
|
-
this.
|
|
3690
|
-
reply({ ok: true, data: unsharedBytes(accepted), binary: true });
|
|
3780
|
+
await this.publishHostPeerReadBytes(session, admittedTerm, budget, accepted, reply);
|
|
3691
3781
|
return;
|
|
3692
3782
|
}
|
|
3693
3783
|
let readBytes;
|
|
3694
|
-
await this.enqueueHostSerial(() => {
|
|
3695
|
-
this.
|
|
3784
|
+
await this.retryPeerAfterLeaseRenewal(session, admittedTerm, budget, () => this.enqueueHostSerial(() => {
|
|
3785
|
+
this.assertPeerAttempt(session, admittedTerm);
|
|
3696
3786
|
readBytes = persistent.execute("read", [remoteHandle, length, offset], new Uint8Array);
|
|
3697
|
-
});
|
|
3787
|
+
}));
|
|
3698
3788
|
await this.hooks.beforeHostReplyPublish?.(msg.op, msg.reqId);
|
|
3699
|
-
this.
|
|
3700
|
-
|
|
3789
|
+
await this.publishHostPeerReadBytes(session, admittedTerm, budget, readBytes, reply);
|
|
3790
|
+
return;
|
|
3791
|
+
}
|
|
3792
|
+
}
|
|
3793
|
+
async tryColdPeerRead(session, admittedTerm, budget, prep, startVer) {
|
|
3794
|
+
try {
|
|
3795
|
+
return await this.revalidateColdReadBytes(prep, startVer, () => this.assertPeerAttempt(session, admittedTerm));
|
|
3796
|
+
} catch (error) {
|
|
3797
|
+
if (error !== LEASE_LAPSED)
|
|
3798
|
+
throw error;
|
|
3799
|
+
await this.ensurePeerLease(session, admittedTerm, budget);
|
|
3701
3800
|
return;
|
|
3702
3801
|
}
|
|
3703
3802
|
}
|
|
3803
|
+
publishHostPeerReadBytes(session, admittedTerm, budget, bytes, reply) {
|
|
3804
|
+
return this.ensurePeerLeaseAndPublish(session, admittedTerm, budget, () => {
|
|
3805
|
+
reply({ ok: true, data: unsharedBytes(bytes), binary: true });
|
|
3806
|
+
});
|
|
3807
|
+
}
|
|
3704
3808
|
async revalidateColdReadBytes(prep, startVer, assertValid) {
|
|
3705
3809
|
const coldBytes = await prep;
|
|
3706
3810
|
let accepted;
|
|
@@ -3754,44 +3858,47 @@ export class MultiTabBackend {
|
|
|
3754
3858
|
persistent.release(h);
|
|
3755
3859
|
}
|
|
3756
3860
|
}
|
|
3757
|
-
async executeHostPeerGeneralLoop(session, admittedTerm, persistent, msg, data) {
|
|
3861
|
+
async executeHostPeerGeneralLoop(session, admittedTerm, persistent, msg, data, budget) {
|
|
3758
3862
|
for (;; ) {
|
|
3759
|
-
this.
|
|
3863
|
+
await this.ensurePeerLease(session, admittedTerm, budget);
|
|
3760
3864
|
const preparing = persistent.prepare(msg.op, msg.args);
|
|
3761
3865
|
if (preparing)
|
|
3762
3866
|
await preparing;
|
|
3763
|
-
this.
|
|
3867
|
+
await this.ensurePeerLease(session, admittedTerm, budget);
|
|
3764
3868
|
let result;
|
|
3765
3869
|
let dirtyRetryChange;
|
|
3766
|
-
await this.enqueueHostSerial(() => {
|
|
3767
|
-
this.
|
|
3870
|
+
await this.retryPeerAfterLeaseRenewal(session, admittedTerm, budget, () => this.enqueueHostSerial(() => {
|
|
3871
|
+
this.assertPeerAttempt(session, admittedTerm);
|
|
3768
3872
|
const step = this.executePersistentOrDirtyRetry(persistent, msg.op, msg.args, data);
|
|
3769
3873
|
if (step.retryChange !== undefined) {
|
|
3770
3874
|
dirtyRetryChange = step.retryChange;
|
|
3771
3875
|
return;
|
|
3772
3876
|
}
|
|
3773
3877
|
result = step.result;
|
|
3774
|
-
});
|
|
3878
|
+
}));
|
|
3775
3879
|
if (dirtyRetryChange !== undefined) {
|
|
3776
|
-
await persistent.waitForDirtyCapacity(dirtyRetryChange, () =>
|
|
3880
|
+
await persistent.waitForDirtyCapacity(dirtyRetryChange, () => this.peerAttemptStale(session, admittedTerm));
|
|
3777
3881
|
continue;
|
|
3778
3882
|
}
|
|
3779
3883
|
return result;
|
|
3780
3884
|
}
|
|
3781
3885
|
}
|
|
3782
|
-
async handleHostPeerGeneral(session, admittedTerm, msg, data, reply) {
|
|
3886
|
+
async handleHostPeerGeneral(session, admittedTerm, msg, data, reply, budget) {
|
|
3783
3887
|
const persistent = this.persistent;
|
|
3784
|
-
const result = MUTATION_OPS.has(msg.op) ? await this.barrier.runMutation(() => this.executeHostPeerGeneralLoop(session, admittedTerm, persistent, msg, data)) : await this.executeHostPeerGeneralLoop(session, admittedTerm, persistent, msg, data);
|
|
3888
|
+
const result = MUTATION_OPS.has(msg.op) ? await this.barrier.runMutation(() => this.executeHostPeerGeneralLoop(session, admittedTerm, persistent, msg, data, budget)) : await this.executeHostPeerGeneralLoop(session, admittedTerm, persistent, msg, data, budget);
|
|
3785
3889
|
const { wireValue, mintedTokens } = this.wrapHostPeerResult(session, persistent, msg.op, result);
|
|
3786
3890
|
await this.hooks.beforeHostReplyPublish?.(msg.op, msg.reqId);
|
|
3787
|
-
|
|
3891
|
+
try {
|
|
3892
|
+
await this.ensurePeerLeaseAndPublish(session, admittedTerm, budget, () => {
|
|
3893
|
+
if (wireValue instanceof Uint8Array) {
|
|
3894
|
+
reply({ ok: true, data: unsharedBytes(wireValue), binary: true });
|
|
3895
|
+
} else {
|
|
3896
|
+
reply({ ok: true, value: wireValue });
|
|
3897
|
+
}
|
|
3898
|
+
});
|
|
3899
|
+
} catch (error) {
|
|
3788
3900
|
this.rollbackMintedPeerTokens(session, persistent, mintedTokens);
|
|
3789
|
-
|
|
3790
|
-
}
|
|
3791
|
-
if (wireValue instanceof Uint8Array) {
|
|
3792
|
-
reply({ ok: true, data: unsharedBytes(wireValue), binary: true });
|
|
3793
|
-
} else {
|
|
3794
|
-
reply({ ok: true, value: wireValue });
|
|
3901
|
+
throw error;
|
|
3795
3902
|
}
|
|
3796
3903
|
}
|
|
3797
3904
|
executePersistentOrDirtyRetry(persistent, op, args, data, responseCookieTag) {
|
|
@@ -3829,10 +3936,10 @@ export class MultiTabBackend {
|
|
|
3829
3936
|
this.hostUsedDownloadBytes += delta;
|
|
3830
3937
|
return neededSize;
|
|
3831
3938
|
}
|
|
3832
|
-
async handleHostPeerReadFile(session, admittedTerm, path, reqId, reply) {
|
|
3939
|
+
async handleHostPeerReadFile(session, admittedTerm, path, reqId, reply, budget) {
|
|
3833
3940
|
const persistent = this.persistent;
|
|
3834
3941
|
for (;; ) {
|
|
3835
|
-
this.
|
|
3942
|
+
await this.ensurePeerLease(session, admittedTerm, budget);
|
|
3836
3943
|
const attempt = { handle: undefined, reservedBytes: 0, fileId: 0n };
|
|
3837
3944
|
const releaseAttempt = () => {
|
|
3838
3945
|
if (attempt.reservedBytes > 0) {
|
|
@@ -3845,22 +3952,21 @@ export class MultiTabBackend {
|
|
|
3845
3952
|
}
|
|
3846
3953
|
};
|
|
3847
3954
|
try {
|
|
3848
|
-
const admitted = await this.admitPeerReadFile(persistent, session, admittedTerm, path, attempt);
|
|
3955
|
+
const admitted = await this.admitPeerReadFile(persistent, session, admittedTerm, path, attempt, budget);
|
|
3849
3956
|
if (!admitted) {
|
|
3850
3957
|
await this.hooks.beforeHostReplyPublish?.("readFile", reqId);
|
|
3851
|
-
this.
|
|
3852
|
-
reply({ ok: true, value: undefined });
|
|
3958
|
+
await this.publishHostPeerMissingFile(session, admittedTerm, budget, reply);
|
|
3853
3959
|
return;
|
|
3854
3960
|
}
|
|
3855
3961
|
await this.hooks.beforeReadFileHydrate?.(path, attempt.fileId);
|
|
3856
|
-
const stillSame = await this.revalidatePeerReadFileBeforeHydrate(persistent, session, admittedTerm, path, attempt);
|
|
3962
|
+
const stillSame = await this.revalidatePeerReadFileBeforeHydrate(persistent, session, admittedTerm, path, attempt, budget);
|
|
3857
3963
|
if (!stillSame) {
|
|
3858
3964
|
releaseAttempt();
|
|
3859
3965
|
continue;
|
|
3860
3966
|
}
|
|
3861
3967
|
await persistent.ensureLoadedHandle(attempt.handle);
|
|
3862
3968
|
await this.hooks.afterReadFilePrepare?.(path, attempt.fileId);
|
|
3863
|
-
const outcome = await this.readHydratedPeerFile(persistent, session, admittedTerm, path, attempt);
|
|
3969
|
+
const outcome = await this.readHydratedPeerFile(persistent, session, admittedTerm, path, attempt, budget);
|
|
3864
3970
|
if (outcome.kind === "restart") {
|
|
3865
3971
|
releaseAttempt();
|
|
3866
3972
|
continue;
|
|
@@ -3868,17 +3974,13 @@ export class MultiTabBackend {
|
|
|
3868
3974
|
if (outcome.kind === "missing") {
|
|
3869
3975
|
releaseAttempt();
|
|
3870
3976
|
await this.hooks.beforeHostReplyPublish?.("readFile", reqId);
|
|
3871
|
-
this.
|
|
3872
|
-
reply({ ok: true, value: undefined });
|
|
3977
|
+
await this.publishHostPeerMissingFile(session, admittedTerm, budget, reply);
|
|
3873
3978
|
return;
|
|
3874
3979
|
}
|
|
3875
3980
|
persistent.release(attempt.handle);
|
|
3876
3981
|
attempt.handle = undefined;
|
|
3877
3982
|
await this.hooks.beforeHostReplyPublish?.("readFile", reqId);
|
|
3878
|
-
this.
|
|
3879
|
-
const transferReserved = attempt.reservedBytes;
|
|
3880
|
-
attempt.reservedBytes = 0;
|
|
3881
|
-
this.replyWithBytes(session, outcome.bytes, transferReserved, true, false, reply);
|
|
3983
|
+
await this.publishHostPeerStagedBytes(session, admittedTerm, budget, attempt, outcome.bytes, reply);
|
|
3882
3984
|
return;
|
|
3883
3985
|
} catch (error) {
|
|
3884
3986
|
releaseAttempt();
|
|
@@ -3886,10 +3988,22 @@ export class MultiTabBackend {
|
|
|
3886
3988
|
}
|
|
3887
3989
|
}
|
|
3888
3990
|
}
|
|
3889
|
-
|
|
3991
|
+
publishHostPeerMissingFile(session, admittedTerm, budget, reply) {
|
|
3992
|
+
return this.ensurePeerLeaseAndPublish(session, admittedTerm, budget, () => {
|
|
3993
|
+
reply({ ok: true, value: undefined });
|
|
3994
|
+
});
|
|
3995
|
+
}
|
|
3996
|
+
publishHostPeerStagedBytes(session, admittedTerm, budget, attempt, bytes, reply) {
|
|
3997
|
+
return this.ensurePeerLeaseAndPublish(session, admittedTerm, budget, () => {
|
|
3998
|
+
const transferReserved = attempt.reservedBytes;
|
|
3999
|
+
attempt.reservedBytes = 0;
|
|
4000
|
+
this.replyWithBytes(session, bytes, transferReserved, true, false, reply);
|
|
4001
|
+
});
|
|
4002
|
+
}
|
|
4003
|
+
async admitPeerReadFile(persistent, session, admittedTerm, path, attempt, budget) {
|
|
3890
4004
|
let found = false;
|
|
3891
|
-
await this.enqueueHostSerial(() => {
|
|
3892
|
-
this.
|
|
4005
|
+
await this.retryPeerAfterLeaseRenewal(session, admittedTerm, budget, () => this.enqueueHostSerial(() => {
|
|
4006
|
+
this.assertPeerAttempt(session, admittedTerm);
|
|
3893
4007
|
const node = persistent.resolveNodePublic(path);
|
|
3894
4008
|
if (!node || node.kind !== "file")
|
|
3895
4009
|
return;
|
|
@@ -3900,13 +4014,13 @@ export class MultiTabBackend {
|
|
|
3900
4014
|
const estimatedSize = persistent.shared.info(h)?.size ?? node.size ?? node.data.byteLength;
|
|
3901
4015
|
attempt.reservedBytes = this.adjustPeerDownloadBudget(session, 0, estimatedSize, false);
|
|
3902
4016
|
found = true;
|
|
3903
|
-
});
|
|
4017
|
+
}));
|
|
3904
4018
|
return found;
|
|
3905
4019
|
}
|
|
3906
|
-
async revalidatePeerReadFileBeforeHydrate(persistent, session, admittedTerm, path, attempt) {
|
|
4020
|
+
async revalidatePeerReadFileBeforeHydrate(persistent, session, admittedTerm, path, attempt, budget) {
|
|
3907
4021
|
let valid = false;
|
|
3908
|
-
await this.enqueueHostSerial(() => {
|
|
3909
|
-
this.
|
|
4022
|
+
await this.retryPeerAfterLeaseRenewal(session, admittedTerm, budget, () => this.enqueueHostSerial(() => {
|
|
4023
|
+
this.assertPeerAttempt(session, admittedTerm);
|
|
3910
4024
|
const current = persistent.resolveNodePublic(path);
|
|
3911
4025
|
if (!current || current.kind !== "file")
|
|
3912
4026
|
return;
|
|
@@ -3915,15 +4029,15 @@ export class MultiTabBackend {
|
|
|
3915
4029
|
const currentSize = persistent.shared.info(attempt.handle)?.size ?? current.size ?? current.data.byteLength;
|
|
3916
4030
|
attempt.reservedBytes = this.adjustPeerDownloadBudget(session, attempt.reservedBytes, currentSize, false);
|
|
3917
4031
|
valid = true;
|
|
3918
|
-
});
|
|
4032
|
+
}));
|
|
3919
4033
|
return valid;
|
|
3920
4034
|
}
|
|
3921
|
-
async readHydratedPeerFile(persistent, session, admittedTerm, path, attempt) {
|
|
4035
|
+
async readHydratedPeerFile(persistent, session, admittedTerm, path, attempt, budget) {
|
|
3922
4036
|
let outcome = {
|
|
3923
4037
|
kind: "missing"
|
|
3924
4038
|
};
|
|
3925
|
-
await this.enqueueHostSerial(() => {
|
|
3926
|
-
this.
|
|
4039
|
+
await this.retryPeerAfterLeaseRenewal(session, admittedTerm, budget, () => this.enqueueHostSerial(() => {
|
|
4040
|
+
this.assertPeerAttempt(session, admittedTerm);
|
|
3927
4041
|
const current = persistent.resolveNodePublic(path);
|
|
3928
4042
|
if (!current || current.kind !== "file") {
|
|
3929
4043
|
outcome = { kind: "missing" };
|
|
@@ -3938,14 +4052,14 @@ export class MultiTabBackend {
|
|
|
3938
4052
|
const out = new Uint8Array(actualSize);
|
|
3939
4053
|
const bytes = out.subarray(0, persistent.shared.read(attempt.handle, [out], 0));
|
|
3940
4054
|
outcome = { kind: "bytes", bytes };
|
|
3941
|
-
});
|
|
4055
|
+
}));
|
|
3942
4056
|
return outcome;
|
|
3943
4057
|
}
|
|
3944
|
-
async handleHostPeerBoundedWireRead(session, admittedTerm, op, args, reqId, reply) {
|
|
4058
|
+
async handleHostPeerBoundedWireRead(session, admittedTerm, op, args, reqId, reply, budget) {
|
|
3945
4059
|
const persistent = this.persistent;
|
|
3946
4060
|
let built;
|
|
3947
|
-
await this.enqueueHostSerial(() => {
|
|
3948
|
-
this.
|
|
4061
|
+
await this.retryPeerAfterLeaseRenewal(session, admittedTerm, budget, () => this.enqueueHostSerial(() => {
|
|
4062
|
+
this.assertPeerAttempt(session, admittedTerm);
|
|
3949
4063
|
const builder = new BoundedWireBuilder(MAX_STAGED_DOWNLOAD_BYTES, (delta) => {
|
|
3950
4064
|
if (this.hostUsedDownloadBytes + delta > MAX_STAGED_DOWNLOAD_BYTES)
|
|
3951
4065
|
return false;
|
|
@@ -3961,25 +4075,15 @@ export class MultiTabBackend {
|
|
|
3961
4075
|
builder.abort();
|
|
3962
4076
|
throw error;
|
|
3963
4077
|
}
|
|
3964
|
-
});
|
|
4078
|
+
}));
|
|
3965
4079
|
let remainingReserved = built.reservedBytes;
|
|
3966
4080
|
try {
|
|
3967
4081
|
await this.hooks.beforeHostReplyPublish?.(op, reqId);
|
|
3968
|
-
this.
|
|
3969
|
-
|
|
3970
|
-
|
|
3971
|
-
|
|
3972
|
-
|
|
3973
|
-
}
|
|
3974
|
-
reply({ ok: true, data: unsharedBytes(built.bytes), packed: true });
|
|
3975
|
-
return;
|
|
3976
|
-
}
|
|
3977
|
-
if (session.downloads.size >= MAX_CONCURRENT_DOWNLOADS_PER_PEER) {
|
|
3978
|
-
throw new FilesystemError(6, "staged download pool exhausted");
|
|
3979
|
-
}
|
|
3980
|
-
const transferReserved = remainingReserved;
|
|
3981
|
-
remainingReserved = 0;
|
|
3982
|
-
this.replyWithBytes(session, built.bytes, transferReserved, false, true, reply);
|
|
4082
|
+
await this.ensurePeerLeaseAndPublish(session, admittedTerm, budget, () => {
|
|
4083
|
+
const reserved = remainingReserved;
|
|
4084
|
+
remainingReserved = 0;
|
|
4085
|
+
this.publishBoundedWireRead(session, built.bytes, reserved, reply);
|
|
4086
|
+
});
|
|
3983
4087
|
} catch (error) {
|
|
3984
4088
|
if (remainingReserved > 0) {
|
|
3985
4089
|
this.hostUsedDownloadBytes = Math.max(0, this.hostUsedDownloadBytes - remainingReserved);
|
|
@@ -3987,6 +4091,22 @@ export class MultiTabBackend {
|
|
|
3987
4091
|
throw error;
|
|
3988
4092
|
}
|
|
3989
4093
|
}
|
|
4094
|
+
publishBoundedWireRead(session, bytes, reserved, reply) {
|
|
4095
|
+
if (bytes.byteLength <= MAX_FRAME_BYTES) {
|
|
4096
|
+
if (reserved > 0) {
|
|
4097
|
+
this.hostUsedDownloadBytes = Math.max(0, this.hostUsedDownloadBytes - reserved);
|
|
4098
|
+
}
|
|
4099
|
+
reply({ ok: true, data: unsharedBytes(bytes), packed: true });
|
|
4100
|
+
return;
|
|
4101
|
+
}
|
|
4102
|
+
if (session.downloads.size >= MAX_CONCURRENT_DOWNLOADS_PER_PEER) {
|
|
4103
|
+
if (reserved > 0) {
|
|
4104
|
+
this.hostUsedDownloadBytes = Math.max(0, this.hostUsedDownloadBytes - reserved);
|
|
4105
|
+
}
|
|
4106
|
+
throw new FilesystemError(6, "staged download pool exhausted");
|
|
4107
|
+
}
|
|
4108
|
+
this.replyWithBytes(session, bytes, reserved, false, true, reply);
|
|
4109
|
+
}
|
|
3990
4110
|
encodeBoundedWireOp(persistent, builder, op, args) {
|
|
3991
4111
|
if (op === "readdir") {
|
|
3992
4112
|
builder.appendText("[");
|
|
@@ -5342,7 +5462,8 @@ export class MultiTabBackend {
|
|
|
5342
5462
|
case "readdir":
|
|
5343
5463
|
case "readdirPage":
|
|
5344
5464
|
case "resolve":
|
|
5345
|
-
case "realpath":
|
|
5465
|
+
case "realpath":
|
|
5466
|
+
case "probe": {
|
|
5346
5467
|
const remote = mapHandle(args[0]);
|
|
5347
5468
|
if (remote === undefined)
|
|
5348
5469
|
return;
|