perf(browser-transfer): 이어 올리는 스트림 업로드는 보낼 part만 해시한다
문제: FILE_BYTE_SOURCE로 이어 올리면 runtime이 서버가 이미 받은 part까지 SHA-256으로 다시 검증한다. 검증(verifyUploadPartBytes)이 수락 확인보다 앞에 있기 때문이다. 8 part 중 6개가 수락된 뒤 이어 올리면 part 해시가 16번 돈다. RANGE_READER 경로는 같은 상황에서 10번이다(manifest 8 + 보낼 part 2). 고친 방법: 스트림 경로에서 수락된 part는 검증 전에 건너뛴다. 보내지 않는 part의 바이트는 검증할 필요가 없다. 파일이 checkpoint와 같은지는 그 앞의 manifest 계산이 이미 확인한다(다른 파일이면 STALE_RESULT). part의 offset·길이 확인은 그대로 모든 part에 한다. 위험·한계: 스트림은 여전히 처음부터 끝까지 읽는다. 읽기를 건너뛰는 것은 스트림 모양상 할 수 없다. 검증: - 새 테스트(8 part 중 6개 수락 뒤 FILE 재개, part 크기 digest 수): 고치기 전 16, 고친 뒤 10 - 영역 단위 테스트 15파일 275건 통과 - check:types(test), eslint(바꾼 파일) 통과 Refs: docs/reviews/2026-11-03-maturity-review/01-adapters-browser-transfer.md 1101/01-02
This commit is contained in:
@@ -879,6 +879,9 @@ async function transferMissingParts<Capability>(
|
||||
{ recovery: "RESELECT" },
|
||||
);
|
||||
}
|
||||
// 1101/01-02. An accepted part is not sent, so it is not hashed either;
|
||||
// the manifest above already checked the file against the checkpoint.
|
||||
if (accepted.has(part.partNumber)) continue;
|
||||
const verified = await verifyUploadPartBytes({
|
||||
bytes: partResult.value.bytes,
|
||||
part,
|
||||
@@ -886,7 +889,6 @@ async function transferMissingParts<Capability>(
|
||||
signal: request.signal,
|
||||
});
|
||||
if (!verified.ok) return verified;
|
||||
if (accepted.has(part.partNumber)) continue;
|
||||
const attempts = { count: 0 };
|
||||
const uploaded = await uploadPartWithRetry(
|
||||
dependencies,
|
||||
|
||||
@@ -1321,6 +1321,93 @@ describe("resume after a policy change (01-08)", () => {
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* 1101/01-02 (2026-11-03 리뷰 2절). A resumed FILE_BYTE_SOURCE upload reads
|
||||
* the whole stream again, but hashes only the parts it still has to send,
|
||||
* as the RANGE_READER path already does. The manifest hashes every part once.
|
||||
*/
|
||||
describe("resumed stream upload hashing (1101/01-02)", () => {
|
||||
const PARTS = Array.from({ length: 8 }, (_, index) =>
|
||||
Uint8Array.from([1, 2, 3, 4], (offset) => index * 4 + offset),
|
||||
);
|
||||
const FILE = Uint8Array.from(PARTS.flatMap((part) => [...part]));
|
||||
|
||||
const streamSource = () =>
|
||||
Object.freeze({
|
||||
kind: "FILE_BYTE_SOURCE" as const,
|
||||
bytes: Object.freeze({
|
||||
byteLength: FILE.byteLength,
|
||||
async *stream() {
|
||||
for (let offset = 0; offset < FILE.byteLength; offset += 5) {
|
||||
yield browserDataSuccess(FILE.slice(offset, offset + 5));
|
||||
}
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
/** Counts SHA-256 digests whose input is exactly one part's bytes. */
|
||||
function countingCrypto() {
|
||||
let partDigests = 0;
|
||||
const isPart = (data: Uint8Array) =>
|
||||
PARTS.some(
|
||||
(part) =>
|
||||
part.byteLength === data.byteLength &&
|
||||
part.every((byte, index) => byte === data[index]),
|
||||
);
|
||||
const counting = {
|
||||
subtle: {
|
||||
async digest(algorithm: AlgorithmIdentifier, data: Uint8Array<ArrayBuffer>) {
|
||||
if (isPart(data)) partDigests += 1;
|
||||
return await crypto.subtle.digest(algorithm, data);
|
||||
},
|
||||
},
|
||||
} as unknown as Crypto;
|
||||
return { crypto: counting, partDigests: () => partDigests };
|
||||
}
|
||||
|
||||
it("hashes only the two parts the server has not accepted", async () => {
|
||||
const harness = createControlHarness();
|
||||
const checkpoints = new MemoryCheckpointStore();
|
||||
const key = "upload_key_stream_resume_hash";
|
||||
const partial = await createResumableUploadRuntime({
|
||||
controlPlane: harness.control,
|
||||
partExecutor: {
|
||||
async uploadPart(input) {
|
||||
return input.part.partNumber >= 7
|
||||
? browserDataFailure("UNAVAILABLE", "UPLOAD_PART", {
|
||||
recovery: "RESUME",
|
||||
})
|
||||
: await executorFor(harness).uploadPart(input);
|
||||
},
|
||||
},
|
||||
checkpoints,
|
||||
mutationLock: noContentionLock,
|
||||
crypto,
|
||||
policy: runtimePolicy({ maxRetries: 0 }),
|
||||
now: () => 1_000,
|
||||
sleep: async () => {},
|
||||
}).upload({ ...request(key), source: streamSource() });
|
||||
expect(partial.ok).toBe(false);
|
||||
expect(harness.accepted.size).toBe(6);
|
||||
|
||||
const counted = countingCrypto();
|
||||
const resumed = await createResumableUploadRuntime({
|
||||
controlPlane: harness.control,
|
||||
partExecutor: executorFor(harness),
|
||||
checkpoints,
|
||||
mutationLock: noContentionLock,
|
||||
crypto: counted.crypto,
|
||||
policy: runtimePolicy({ maxRetries: 0 }),
|
||||
now: () => 1_000,
|
||||
sleep: async () => {},
|
||||
}).upload({ ...request(key), source: streamSource() });
|
||||
|
||||
expect(resumed).toMatchObject({ ok: true, value: { state: "QUARANTINED" } });
|
||||
// 8 for the manifest, 2 for the parts sent. The old order made it 16.
|
||||
expect(counted.partDigests()).toBe(10);
|
||||
});
|
||||
});
|
||||
|
||||
describe("upload part observation (01-10)", () => {
|
||||
it("records a part that succeeded on its second attempt as one UPLOAD_PART", async () => {
|
||||
const observations: BrowserDataObservation[] = [];
|
||||
|
||||
Reference in New Issue
Block a user