Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion orchestrator/src/files.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'node:assert/strict';
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
import {
createServer,
type IncomingHttpHeaders,
Expand Down Expand Up @@ -130,6 +130,19 @@ test('a read is tried once more when the connection ended during it', async () =
assert.equal(counter.spawns, 2);
});

test('a small file is stored while a large file is sent on the same connection', async () => {
setFileServiceSpawnerForTests(real);
const files = fileService('c1');
await files.list('');
const large = files.create('', 'large.bin', Buffer.alloc(14 * 1024 * 1024, 1));
// The small file starts when the large file is in the connection.
await new Promise((resolve) => setImmediate(resolve));
const small = files.create('', 'small.bin', Buffer.alloc(1024 * 1024, 1));
assert.deepEqual(await Promise.all([large, small]), ['large.bin', 'small.bin']);
assert.equal(statSync(join(workspace, 'large.bin')).size, 14 * 1024 * 1024);
assert.equal(statSync(join(workspace, 'small.bin')).size, 1024 * 1024);
});

test('a write is not tried again', async () => {
const { counter, spawner } = counted(dropping, real);
setFileServiceSpawnerForTests(spawner);
Expand Down
26 changes: 22 additions & 4 deletions orchestrator/src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const REQUEST_TIMEOUT_MS = 30_000;
/** How long the walk for the disk usage may take, in milliseconds. It matches a short command in the box. */
const SIZE_TIMEOUT_MS = RUN_TIMEOUT_MS;

/** The size of one piece of a request body that is a buffer, in bytes. */
const BODY_PIECE = 64 * 1024;

/** The most bytes of a JSON reply. A full directory listing fits. */
const MAX_JSON_REPLY = 4 * 1024 * 1024;

Expand Down Expand Up @@ -227,7 +230,7 @@ interface Request {
query?: Record<string, string>;
/** Extra request headers. */
headers?: OutgoingHttpHeaders;
/** The body. A stream is piped, so it is never held in memory as a whole. */
/** The body. A stream is piped, so it is never held in memory as a whole. A buffer is sent in pieces. */
body?: string | Buffer | Readable;
/** The timeout in milliseconds. REQUEST_TIMEOUT_MS applies when it is absent. */
timeoutMs?: number;
Expand Down Expand Up @@ -602,10 +605,13 @@ export class FileService {
});
stream.on('error', (err) => reject(err instanceof TimedOut ? err : new ConnectionLost()));
stream.on('close', () => reject(new ConnectionLost()));
if (req.body instanceof Readable) {
pipeline(req.body, stream).catch(() => {});
// The session refuses new requests while it holds more than its memory limit, so a
// buffer goes to the session in pieces, as fast as the file service reads them.
const body = Buffer.isBuffer(req.body) ? Readable.from(pieces(req.body)) : req.body;
if (body instanceof Readable) {
pipeline(body, stream).catch(() => {});
} else {
stream.end(req.body);
stream.end(body);
}
});
} catch (err) {
Expand Down Expand Up @@ -706,6 +712,18 @@ export class FileService {
}
}

/**
* Cuts a buffer into pieces of at most {@link BODY_PIECE} bytes. The pieces share the memory of the buffer.
*
* @param bytes The buffer.
* @returns The pieces, in order.
*/
function* pieces(bytes: Buffer): Generator<Buffer> {
for (let start = 0; start < bytes.length; start += BODY_PIECE) {
yield bytes.subarray(start, start + BODY_PIECE);
}
}

/**
* Returns the body of a reply stream, as a stream of its own.
*
Expand Down