hammerkit 2.1.0-next.19 → 2.1.0-next.20
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/dist/kubernetes/ensure-persistent-data.js +1 -1
- package/dist/kubernetes/sync-changes.js +322 -39
- package/dist/kubernetes/volumes.js +23 -1
- package/dist/server/executor.js +42 -26
- package/dist/server/run-worker.js +32 -54
- package/dist/server/sandbox-sync.js +269 -234
- package/dist/server/server.js +5 -6
- package/dist/utils/tar-files.js +52 -0
- package/package.json +1 -1
|
@@ -1,40 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SandboxSync = exports.unsafePath = exports.
|
|
4
|
-
const
|
|
5
|
-
const promises_1 = require("fs/promises");
|
|
6
|
-
const path_1 = require("path");
|
|
7
|
-
const promises_2 = require("stream/promises");
|
|
3
|
+
exports.SandboxSync = exports.unsafePath = exports.MAX_BATCH_FILES = exports.MAX_BATCH_BYTES = void 0;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
8
5
|
const log_1 = require("../log");
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
// the most files one batch
|
|
6
|
+
const tar_files_1 = require("../utils/tar-files");
|
|
7
|
+
const sandbox_files_1 = require("./sandbox-files");
|
|
8
|
+
// the most bytes of files one part of a batch sends; a larger batch goes in several parts
|
|
9
|
+
exports.MAX_BATCH_BYTES = 16 * 1024 * 1024;
|
|
10
|
+
// the most files one part of a batch sends
|
|
12
11
|
exports.MAX_BATCH_FILES = 10000;
|
|
13
|
-
// how
|
|
14
|
-
const
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
const
|
|
12
|
+
// how many files of a sandbox are read at once, while a batch is worked out
|
|
13
|
+
const READ_CONCURRENCY = 8;
|
|
14
|
+
// a file whose mtime is this recent may be written again in the same timestamp: it is read again
|
|
15
|
+
// on every sync, as git does with its index (the racy rule)
|
|
16
|
+
const RACY_MS = 2000;
|
|
17
|
+
// how long a run whose services failed to apply a batch waits before the full set is sent again:
|
|
18
|
+
// the first wait, doubled on each failure up to RETRY_MAX_MS
|
|
19
|
+
const RETRY_MIN_MS = 1000;
|
|
18
20
|
const RETRY_MAX_MS = 10000;
|
|
19
|
-
// a
|
|
20
|
-
const
|
|
21
|
-
/**
|
|
22
|
-
* How long the next retry of a sync waits, after one that waited `previous` ms (undefined for
|
|
23
|
-
* the first): `base` at first, then doubled up to RETRY_MAX_MS.
|
|
24
|
-
*/
|
|
25
|
-
function nextRetryMs(previous, base) {
|
|
26
|
-
return previous === undefined ? Math.min(base, RETRY_MAX_MS) : Math.min(previous * 2, RETRY_MAX_MS);
|
|
27
|
-
}
|
|
28
|
-
exports.nextRetryMs = nextRetryMs;
|
|
29
|
-
// a batch with more than MAX_BATCH_BYTES or MAX_BATCH_FILES is not staged at all
|
|
30
|
-
class BatchTooLargeError extends Error {
|
|
31
|
-
constructor() {
|
|
32
|
-
super(`the changes are more than ${exports.MAX_BATCH_FILES} files or ${exports.MAX_BATCH_BYTES} bytes, the services keep their files`);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
21
|
+
// what a path is in the services when it is gone
|
|
22
|
+
const ABSENT = 'absent';
|
|
35
23
|
/**
|
|
36
24
|
* Why a path of the sandbox can not be a path of a service's folder, or none. A name with a
|
|
37
|
-
* control character (a newline in particular), an absolute path
|
|
25
|
+
* control character (a newline in particular), an absolute path, an empty segment, a trailing `/`
|
|
26
|
+
* or a `.` or `..` segment is refused: each of those can name the folder itself or one above it.
|
|
38
27
|
*/
|
|
39
28
|
function unsafePath(path) {
|
|
40
29
|
if (path === '') {
|
|
@@ -46,37 +35,91 @@ function unsafePath(path) {
|
|
|
46
35
|
if (path.startsWith('/') || /^[A-Za-z]:/.test(path)) {
|
|
47
36
|
return 'it is not relative to the sandbox';
|
|
48
37
|
}
|
|
38
|
+
if (path.endsWith('/') || path.split('/').includes('')) {
|
|
39
|
+
return 'it has an empty segment or ends in /';
|
|
40
|
+
}
|
|
49
41
|
if (path.split('/').some((segment) => segment === '..' || segment === '.')) {
|
|
50
42
|
return 'it has a . or .. segment';
|
|
51
43
|
}
|
|
52
44
|
return undefined;
|
|
53
45
|
}
|
|
54
46
|
exports.unsafePath = unsafePath;
|
|
47
|
+
// whether a file with this mtime may still change without its mtime changing: an mtime that is
|
|
48
|
+
// recent, or one that is not a date, is read on every sync
|
|
49
|
+
function racy(modifiedAt) {
|
|
50
|
+
const time = Date.parse(modifiedAt);
|
|
51
|
+
return Number.isNaN(time) || Date.now() - time < RACY_MS;
|
|
52
|
+
}
|
|
53
|
+
// the mtime of a file as it is in the sandbox, for its entry of an archive
|
|
54
|
+
function mtimeOf(modifiedAt) {
|
|
55
|
+
const time = Date.parse(modifiedAt);
|
|
56
|
+
return Number.isNaN(time) ? new Date() : new Date(time);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The files of a batch in parts, each under the caps (MAX_BATCH_FILES, MAX_BATCH_BYTES), in the
|
|
60
|
+
* order they came. A file over the byte cap goes alone in its part: the parts are sent one after
|
|
61
|
+
* another, so each stays under the cap in memory.
|
|
62
|
+
*/
|
|
63
|
+
function partition(writes) {
|
|
64
|
+
const parts = [];
|
|
65
|
+
let part = [];
|
|
66
|
+
let bytes = 0;
|
|
67
|
+
for (const write of writes) {
|
|
68
|
+
if (part.length > 0 && (part.length === exports.MAX_BATCH_FILES || bytes + write.size > exports.MAX_BATCH_BYTES)) {
|
|
69
|
+
parts.push(part);
|
|
70
|
+
part = [];
|
|
71
|
+
bytes = 0;
|
|
72
|
+
}
|
|
73
|
+
part.push(write);
|
|
74
|
+
bytes += write.size;
|
|
75
|
+
}
|
|
76
|
+
if (part.length > 0) {
|
|
77
|
+
parts.push(part);
|
|
78
|
+
}
|
|
79
|
+
return parts;
|
|
80
|
+
}
|
|
81
|
+
// the results of `fn` for `items`, with at most `limit` calls in flight
|
|
82
|
+
async function mapLimit(items, limit, fn) {
|
|
83
|
+
const results = new Array(items.length);
|
|
84
|
+
let next = 0;
|
|
85
|
+
const worker = async () => {
|
|
86
|
+
while (next < items.length) {
|
|
87
|
+
const index = next++;
|
|
88
|
+
results[index] = await fn(items[index]);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
|
92
|
+
return results;
|
|
93
|
+
}
|
|
55
94
|
/**
|
|
56
95
|
* Keeps the services of the service runs on a sandbox in step with its files. Each change of the
|
|
57
|
-
* sandbox (batched by SandboxFollow) goes to each run that is up, as the files that changed
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
96
|
+
* sandbox (batched by SandboxFollow) goes to each run that is up, as the files that changed since
|
|
97
|
+
* the services last got their files. Nothing else of the services is touched, and nothing is
|
|
98
|
+
* restarted here.
|
|
99
|
+
*
|
|
100
|
+
* The record of what the services have is kept in memory, per run: for each path, the stat and
|
|
101
|
+
* the hash of the file the services got, or ABSENT once its deletion went out. A file whose stat
|
|
102
|
+
* did not change, and whose mtime is not recent, is not read at all. A run without a record (the
|
|
103
|
+
* server started since it did) gets every file that differs from the commit once: "the full set"
|
|
104
|
+
* means exactly that. A run whose services failed to apply a batch loses its record, and the full
|
|
105
|
+
* set is sent again after a backoff.
|
|
62
106
|
*/
|
|
63
107
|
class SandboxSync {
|
|
64
108
|
constructor(options) {
|
|
65
109
|
this.options = options;
|
|
66
|
-
// per run, the
|
|
67
|
-
this.
|
|
110
|
+
// per run, what the services have of each path they got (see the class comment)
|
|
111
|
+
this.records = new Map();
|
|
112
|
+
// the sandbox of each run that was started here, for the retry of its failed batch
|
|
113
|
+
this.sandboxes = new Map();
|
|
114
|
+
// per run, the timer of its retry and how long the wait was
|
|
115
|
+
this.retries = new Map();
|
|
68
116
|
// the batches of each sandbox, one after another
|
|
69
117
|
this.queues = new Map();
|
|
70
118
|
// the batch of each sandbox that waits to start: it reads the files when it starts, so a
|
|
71
119
|
// change that comes meanwhile is in it too
|
|
72
120
|
this.waiting = new Map();
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
// the last one waited
|
|
76
|
-
this.retries = new Map();
|
|
77
|
-
this.retryDelays = new Map();
|
|
78
|
-
// the runs that were told their changed files are over the cap, once each
|
|
79
|
-
this.overCap = new Set();
|
|
121
|
+
// per run, the lines already logged: a path that is refused says so once
|
|
122
|
+
this.logged = new Map();
|
|
80
123
|
}
|
|
81
124
|
/**
|
|
82
125
|
* A service run starts from a copy of the files now: what differs from the commit in the copy
|
|
@@ -86,32 +129,63 @@ class SandboxSync {
|
|
|
86
129
|
if (run.source.type !== 'sandbox') {
|
|
87
130
|
return;
|
|
88
131
|
}
|
|
89
|
-
this.
|
|
132
|
+
this.sandboxes.set(run.id, run.source.id);
|
|
133
|
+
const files = this.options.files(run.owner, run.source.id);
|
|
134
|
+
const record = new Map();
|
|
135
|
+
const paths = (await this.changedPaths(files)).filter((path) => !unsafePath(path));
|
|
136
|
+
const looked = await mapLimit(paths, READ_CONCURRENCY, (path) => this.look(files, path, undefined, () => undefined));
|
|
137
|
+
for (const item of looked) {
|
|
138
|
+
if (item.kind === 'same') {
|
|
139
|
+
record.set(item.path, item.mark);
|
|
140
|
+
}
|
|
141
|
+
else if (item.kind === 'changed') {
|
|
142
|
+
record.set(item.path, item.mark);
|
|
143
|
+
}
|
|
144
|
+
else if (item.kind === 'gone') {
|
|
145
|
+
record.set(item.path, ABSENT);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
this.records.set(run.id, record);
|
|
149
|
+
}
|
|
150
|
+
// A run that ended: nothing is synced for it any more, and its retry is stopped
|
|
151
|
+
async forget(run) {
|
|
152
|
+
this.records.delete(run);
|
|
153
|
+
this.sandboxes.delete(run);
|
|
154
|
+
this.logged.delete(run);
|
|
155
|
+
this.clearRetry(run);
|
|
90
156
|
}
|
|
91
|
-
|
|
157
|
+
/**
|
|
158
|
+
* The services of a run did not apply the last batch: its record goes, and the full set is sent
|
|
159
|
+
* again after a backoff (1 s, doubled on each failure up to 10 s).
|
|
160
|
+
*/
|
|
161
|
+
failed(run) {
|
|
162
|
+
this.records.delete(run);
|
|
163
|
+
const sandbox = this.sandboxes.get(run);
|
|
164
|
+
if (sandbox === undefined) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const previous = this.retries.get(run);
|
|
168
|
+
const delay = previous ? Math.min(previous.delay * 2, RETRY_MAX_MS) : RETRY_MIN_MS;
|
|
169
|
+
if (previous) {
|
|
170
|
+
clearTimeout(previous.timer);
|
|
171
|
+
}
|
|
172
|
+
const timer = setTimeout(() => void this.follow(sandbox), delay);
|
|
173
|
+
timer.unref?.();
|
|
174
|
+
this.retries.set(run, { timer, delay });
|
|
175
|
+
}
|
|
176
|
+
// Stops the retries: nothing is sent after the server closes
|
|
92
177
|
close() {
|
|
93
|
-
for (const timer of this.retries.values()) {
|
|
178
|
+
for (const { timer } of this.retries.values()) {
|
|
94
179
|
clearTimeout(timer);
|
|
95
180
|
}
|
|
96
181
|
this.retries.clear();
|
|
97
|
-
this.retryDelays.clear();
|
|
98
182
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
this.overCap.delete(key);
|
|
105
|
-
}
|
|
183
|
+
clearRetry(run) {
|
|
184
|
+
const retry = this.retries.get(run);
|
|
185
|
+
if (retry) {
|
|
186
|
+
clearTimeout(retry.timer);
|
|
187
|
+
this.retries.delete(run);
|
|
106
188
|
}
|
|
107
|
-
await this.remove((0, path_1.join)(this.options.stagingRoot, run));
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* At server start: what a server that stopped staged is no batch of any run now, so the
|
|
111
|
-
* staging folder goes.
|
|
112
|
-
*/
|
|
113
|
-
async sweep() {
|
|
114
|
-
await this.remove(this.options.stagingRoot);
|
|
115
189
|
}
|
|
116
190
|
// The batch of changes of a sandbox, after the ones before it. One that waits already has the
|
|
117
191
|
// newest files when it starts, so a second one is not queued behind it.
|
|
@@ -147,119 +221,163 @@ class SandboxSync {
|
|
|
147
221
|
throw new Error(`the service runs on ${sandbox} have more than one owner, their changes are not synced`);
|
|
148
222
|
}
|
|
149
223
|
const files = this.options.files(owner, sandbox);
|
|
150
|
-
const changed = await this.
|
|
224
|
+
const changed = await this.changedPaths(files);
|
|
151
225
|
for (const run of runs) {
|
|
152
|
-
|
|
153
|
-
if (await this.pending(run.id)) {
|
|
154
|
-
// the worker has not applied the last batch of the run: these paths go with the next one
|
|
155
|
-
if (known) {
|
|
156
|
-
this.differing.set(run.id, new Set([...known, ...changed]));
|
|
157
|
-
}
|
|
158
|
-
this.retryLater(sandbox);
|
|
159
|
-
continue;
|
|
160
|
-
}
|
|
161
|
-
await this.sendTo(run, sandbox, files, known, changed);
|
|
226
|
+
await this.sendTo(run, sandbox, files, changed);
|
|
162
227
|
}
|
|
163
228
|
}
|
|
164
229
|
/**
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
230
|
+
* Sends the batch of one run: the paths that changed since the record of the run (or, without
|
|
231
|
+
* one, the paths that differ from the commit). A batch over the caps goes in parts, one after the
|
|
232
|
+
* other. The record moves on for each part the services took; a part they refuse is in the next
|
|
233
|
+
* batch.
|
|
168
234
|
*/
|
|
169
|
-
async
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const
|
|
175
|
-
if (
|
|
176
|
-
|
|
177
|
-
|
|
235
|
+
async sendTo(run, sandbox, files, changed) {
|
|
236
|
+
const record = this.records.get(run.id) ?? new Map();
|
|
237
|
+
this.records.set(run.id, record);
|
|
238
|
+
const say = (line) => this.logOnce(run.id, line);
|
|
239
|
+
const candidates = [...new Set([...changed, ...record.keys()])].filter((path) => {
|
|
240
|
+
const problem = unsafePath(path);
|
|
241
|
+
if (problem) {
|
|
242
|
+
say(`skipped ${JSON.stringify(path)} of the sandbox: ${problem}`);
|
|
243
|
+
record.delete(path);
|
|
244
|
+
}
|
|
245
|
+
return !problem;
|
|
246
|
+
});
|
|
247
|
+
const writes = [];
|
|
248
|
+
const gone = [];
|
|
249
|
+
const looked = await mapLimit(candidates, READ_CONCURRENCY, (path) => this.look(files, path, record.get(path), say));
|
|
250
|
+
for (const item of looked) {
|
|
251
|
+
if (item.kind === 'refused') {
|
|
252
|
+
// not in the record: the next batch that has it sends it again
|
|
253
|
+
record.delete(item.path);
|
|
254
|
+
}
|
|
255
|
+
else if (item.kind === 'same') {
|
|
256
|
+
record.set(item.path, item.mark);
|
|
257
|
+
}
|
|
258
|
+
else if (item.kind === 'gone') {
|
|
259
|
+
gone.push(item.path);
|
|
178
260
|
}
|
|
179
261
|
else {
|
|
180
|
-
|
|
262
|
+
writes.push({ ...item.mark, path: item.path, content: item.content });
|
|
181
263
|
}
|
|
182
264
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
// Each try waits longer than the one before, up to RETRY_MAX_MS, until a batch is sent.
|
|
187
|
-
retryLater(sandbox) {
|
|
188
|
-
if (this.retries.has(sandbox)) {
|
|
189
|
-
return;
|
|
265
|
+
const parts = partition(writes);
|
|
266
|
+
if (parts.length > 1) {
|
|
267
|
+
say(`the changes of ${sandbox} are more than a batch takes, ${run.id} gets them in ${parts.length} parts`);
|
|
190
268
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
269
|
+
// the gone paths go with the first part; with no file written, they go alone
|
|
270
|
+
const batches = parts.length > 0 ? parts : [[]];
|
|
271
|
+
for (const [index, part] of batches.entries()) {
|
|
272
|
+
const removed = index === 0 ? gone : [];
|
|
273
|
+
if (part.length + removed.length === 0) {
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
const changes = {
|
|
277
|
+
archive: (0, tar_files_1.tarFiles)(part.map((write) => ({
|
|
278
|
+
path: write.path,
|
|
279
|
+
mode: write.exec ? 0o755 : 0o644,
|
|
280
|
+
content: write.content,
|
|
281
|
+
mtime: mtimeOf(write.modifiedAt),
|
|
282
|
+
}))),
|
|
283
|
+
written: part.map((write) => write.path),
|
|
284
|
+
deleted: removed,
|
|
285
|
+
restart: false,
|
|
286
|
+
};
|
|
287
|
+
if (!this.options.send(run.id, changes)) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
for (const write of part) {
|
|
291
|
+
record.set(write.path, { size: write.size, modifiedAt: write.modifiedAt, exec: write.exec, hash: write.hash });
|
|
292
|
+
}
|
|
293
|
+
// a deletion stays in the record as ABSENT: git lists a deleted file until it is committed,
|
|
294
|
+
// and a deletion that is not recorded would be sent again on every sync
|
|
295
|
+
for (const path of removed) {
|
|
296
|
+
record.set(path, ABSENT);
|
|
297
|
+
}
|
|
298
|
+
// a batch went: the next failure starts the backoff again
|
|
299
|
+
this.clearRetry(run.id);
|
|
206
300
|
}
|
|
207
301
|
}
|
|
208
302
|
/**
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
303
|
+
* What a path of the sandbox is, against what the services have of it (`known`). A file whose
|
|
304
|
+
* size, mtime and exec bit are the ones the services have, and whose mtime is not racy, is the
|
|
305
|
+
* same without a read. Otherwise its content is read once and hashed, and the read is kept for
|
|
306
|
+
* the entry of the batch.
|
|
212
307
|
*/
|
|
213
|
-
async
|
|
214
|
-
|
|
215
|
-
let paths = known ? new Set([...known, ...changed]) : new Set([...(await this.allFiles(files)), ...changed]);
|
|
216
|
-
let changes;
|
|
308
|
+
async look(files, path, known, say) {
|
|
309
|
+
let stat;
|
|
217
310
|
try {
|
|
218
|
-
|
|
311
|
+
stat = await files.stat(path);
|
|
219
312
|
}
|
|
220
313
|
catch (e) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
this.logOverCap(run.id, 'more', `the files of ${sandbox} are more than a batch takes, ${run.id} gets the changed files only: a file reverted to the commit may be stale`);
|
|
226
|
-
paths = changed;
|
|
227
|
-
try {
|
|
228
|
-
changes = await this.stage(run.id, files, [...paths]);
|
|
229
|
-
}
|
|
230
|
-
catch (again) {
|
|
231
|
-
if (!(again instanceof BatchTooLargeError)) {
|
|
232
|
-
this.log(`the changes of ${sandbox} are not synced to ${run.id}: ${(0, log_1.getErrorMessage)(again)}`);
|
|
233
|
-
return;
|
|
234
|
-
}
|
|
235
|
-
// the changed files alone are over the cap: they are the record from now, and nothing is sent
|
|
236
|
-
this.logOverCap(run.id, 'alone', `the changed files of ${sandbox} alone are more than a batch takes: ${run.id} gets none of them, they are its baseline`);
|
|
237
|
-
this.differing.set(run.id, changed);
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
314
|
+
say(`skipped ${JSON.stringify(path)} of the sandbox: ${(0, log_1.getErrorMessage)(e)}`);
|
|
315
|
+
return { path, kind: 'refused' };
|
|
240
316
|
}
|
|
241
|
-
if (
|
|
242
|
-
|
|
243
|
-
|
|
317
|
+
if (stat?.type !== 'file') {
|
|
318
|
+
return known === ABSENT ? { path, kind: 'same', mark: ABSENT } : { path, kind: 'gone' };
|
|
319
|
+
}
|
|
320
|
+
if (stat.size > sandbox_files_1.MAX_FILE_BYTES) {
|
|
321
|
+
say(`refused ${JSON.stringify(path)} of the sandbox: it is over ${sandbox_files_1.MAX_FILE_BYTES} bytes`);
|
|
322
|
+
return { path, kind: 'refused' };
|
|
323
|
+
}
|
|
324
|
+
if (known !== undefined &&
|
|
325
|
+
known !== ABSENT &&
|
|
326
|
+
known.size === stat.size &&
|
|
327
|
+
known.modifiedAt === stat.modifiedAt &&
|
|
328
|
+
known.exec === stat.exec &&
|
|
329
|
+
!racy(stat.modifiedAt)) {
|
|
330
|
+
return { path, kind: 'same', mark: known };
|
|
331
|
+
}
|
|
332
|
+
let read;
|
|
333
|
+
try {
|
|
334
|
+
read = await this.readOf(files, path);
|
|
335
|
+
}
|
|
336
|
+
catch (e) {
|
|
337
|
+
say(`skipped ${JSON.stringify(path)} of the sandbox: ${(0, log_1.getErrorMessage)(e)}`);
|
|
338
|
+
return { path, kind: 'refused' };
|
|
244
339
|
}
|
|
245
|
-
if (
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
this.retryDelays.delete(sandbox);
|
|
340
|
+
if (read === undefined || read.content.length > sandbox_files_1.MAX_FILE_BYTES) {
|
|
341
|
+
say(`refused ${JSON.stringify(path)} of the sandbox: it is over ${sandbox_files_1.MAX_FILE_BYTES} bytes`);
|
|
342
|
+
return { path, kind: 'refused' };
|
|
249
343
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
344
|
+
const mark = {
|
|
345
|
+
size: stat.size,
|
|
346
|
+
modifiedAt: stat.modifiedAt,
|
|
347
|
+
exec: stat.exec,
|
|
348
|
+
hash: read.sha256 ?? (0, crypto_1.createHash)('sha256').update(read.content).digest('hex'),
|
|
349
|
+
};
|
|
350
|
+
if (known !== undefined && known !== ABSENT && known.hash === mark.hash && known.exec === mark.exec) {
|
|
351
|
+
return { path, kind: 'same', mark };
|
|
352
|
+
}
|
|
353
|
+
return { path, kind: 'changed', mark, content: read.content };
|
|
354
|
+
}
|
|
355
|
+
// logs a line of a run once: a path that stays refused is not said again on every sync
|
|
356
|
+
logOnce(run, line) {
|
|
357
|
+
const lines = this.logged.get(run) ?? new Set();
|
|
358
|
+
this.logged.set(run, lines);
|
|
359
|
+
if (!lines.has(line)) {
|
|
360
|
+
lines.add(line);
|
|
361
|
+
this.log(line);
|
|
254
362
|
}
|
|
255
363
|
}
|
|
256
364
|
log(line) {
|
|
257
365
|
this.options.log?.(line);
|
|
258
366
|
}
|
|
259
|
-
|
|
260
|
-
|
|
367
|
+
/**
|
|
368
|
+
* The content of a file of the sandbox, read whole, with its hash when the files give one.
|
|
369
|
+
* Undefined for a file that is over the cap at open: its stream is destroyed, and nothing is
|
|
370
|
+
* read into memory.
|
|
371
|
+
*/
|
|
372
|
+
async readOf(files, path) {
|
|
373
|
+
const raw = await files.raw(path);
|
|
374
|
+
if (raw.content) {
|
|
375
|
+
return { content: raw.content, sha256: raw.sha256 };
|
|
376
|
+
}
|
|
377
|
+
raw.stream?.destroy();
|
|
378
|
+
return undefined;
|
|
261
379
|
}
|
|
262
|
-
async
|
|
380
|
+
async changedPaths(files) {
|
|
263
381
|
const paths = new Set();
|
|
264
382
|
for (const change of await files.changes()) {
|
|
265
383
|
paths.add(change.path);
|
|
@@ -267,90 +385,7 @@ class SandboxSync {
|
|
|
267
385
|
paths.add(change.from);
|
|
268
386
|
}
|
|
269
387
|
}
|
|
270
|
-
return paths;
|
|
271
|
-
}
|
|
272
|
-
// every file of the sandbox, page by page
|
|
273
|
-
async allFiles(files) {
|
|
274
|
-
const paths = [];
|
|
275
|
-
let cursor;
|
|
276
|
-
do {
|
|
277
|
-
const page = await files.tree({ depth: WALK_DEPTH, limit: 1000, cursor });
|
|
278
|
-
for (const entry of page.entries) {
|
|
279
|
-
if (entry.type === 'file') {
|
|
280
|
-
paths.push(entry.path);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
cursor = page.truncated && page.cursor ? page.cursor : undefined;
|
|
284
|
-
} while (cursor);
|
|
285
|
-
return paths;
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Writes the files of `paths` that are there now into a folder of the batch, and says which
|
|
289
|
-
* paths are gone. A path that is unsafe, or whose file can not be read, is skipped and logged;
|
|
290
|
-
* the rest is synced. Undefined when none of the paths is left to say. Throws when the batch is
|
|
291
|
-
* too large, before anything is written: the sizes are summed first.
|
|
292
|
-
*/
|
|
293
|
-
async stage(run, files, paths) {
|
|
294
|
-
const writes = [];
|
|
295
|
-
const deleted = [];
|
|
296
|
-
let bytes = 0;
|
|
297
|
-
for (const path of paths) {
|
|
298
|
-
const problem = unsafePath(path);
|
|
299
|
-
if (problem) {
|
|
300
|
-
this.log(`skipped ${JSON.stringify(path)} of the sandbox: ${problem}`);
|
|
301
|
-
continue;
|
|
302
|
-
}
|
|
303
|
-
try {
|
|
304
|
-
const stat = await files.stat(path);
|
|
305
|
-
if (stat?.type !== 'file') {
|
|
306
|
-
deleted.push(path);
|
|
307
|
-
continue;
|
|
308
|
-
}
|
|
309
|
-
bytes += stat.size;
|
|
310
|
-
writes.push({ path, exec: stat.exec });
|
|
311
|
-
}
|
|
312
|
-
catch (e) {
|
|
313
|
-
this.log(`skipped ${JSON.stringify(path)} of the sandbox: ${(0, log_1.getErrorMessage)(e)}`);
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
if (writes.length > exports.MAX_BATCH_FILES || bytes > exports.MAX_BATCH_BYTES) {
|
|
317
|
-
throw new BatchTooLargeError();
|
|
318
|
-
}
|
|
319
|
-
const folder = (0, path_1.join)(this.options.stagingRoot, run, `${++this.batch}`);
|
|
320
|
-
const dir = (0, path_1.join)(folder, 'files');
|
|
321
|
-
const written = [];
|
|
322
|
-
try {
|
|
323
|
-
for (const { path, exec } of writes) {
|
|
324
|
-
try {
|
|
325
|
-
const raw = await files.raw(path);
|
|
326
|
-
const target = (0, path_1.join)(dir, path);
|
|
327
|
-
await (0, promises_1.mkdir)((0, path_1.dirname)(target), { recursive: true });
|
|
328
|
-
if (raw.content) {
|
|
329
|
-
await (0, promises_1.writeFile)(target, raw.content);
|
|
330
|
-
}
|
|
331
|
-
else if (raw.stream) {
|
|
332
|
-
await (0, promises_2.pipeline)(raw.stream, (0, fs_1.createWriteStream)(target));
|
|
333
|
-
}
|
|
334
|
-
// the exec bit is kept: a script the services run is still executable after the sync
|
|
335
|
-
await (0, promises_1.chmod)(target, exec ? 0o755 : 0o644);
|
|
336
|
-
written.push(path);
|
|
337
|
-
}
|
|
338
|
-
catch (e) {
|
|
339
|
-
// a file that changed or can not be read: the next batch brings it
|
|
340
|
-
this.log(`skipped ${JSON.stringify(path)} of the sandbox: ${(0, log_1.getErrorMessage)(e)}`);
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
if (written.length + deleted.length === 0) {
|
|
344
|
-
await this.remove(folder);
|
|
345
|
-
return undefined;
|
|
346
|
-
}
|
|
347
|
-
await (0, promises_1.mkdir)(dir, { recursive: true });
|
|
348
|
-
return { dir, written, deleted, restart: false };
|
|
349
|
-
}
|
|
350
|
-
catch (e) {
|
|
351
|
-
await this.remove(folder);
|
|
352
|
-
throw e;
|
|
353
|
-
}
|
|
388
|
+
return [...paths];
|
|
354
389
|
}
|
|
355
390
|
}
|
|
356
391
|
exports.SandboxSync = SandboxSync;
|
package/dist/server/server.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.startServer = exports.runsTheSame = exports.refuseWake = exports.getDiscoveryDocument = exports.SERVER_PROTOCOL = exports.DISCOVERY_PATH = void 0;
|
|
4
|
+
const promises_1 = require("fs/promises");
|
|
4
5
|
const http_1 = require("http");
|
|
5
6
|
const https_1 = require("https");
|
|
6
7
|
const tls_1 = require("tls");
|
|
@@ -1007,17 +1008,15 @@ async function startServer(config, environment, options = {}) {
|
|
|
1007
1008
|
// a local task of a host runs as the user of this server: what it can write is never served
|
|
1008
1009
|
const localTasksOnHost = config.backend.type === 'host' && config.runtimes.includes('local');
|
|
1009
1010
|
// a service run from a sandbox on a cluster gets the changed files of the sandbox into its
|
|
1010
|
-
// services, batched (SandboxFollow), as the
|
|
1011
|
-
|
|
1011
|
+
// services, batched (SandboxFollow), as the files that changed since the services last got theirs
|
|
1012
|
+
// the folder a server before the direct sync staged the changes in is no longer used
|
|
1013
|
+
await (0, promises_1.rm)((0, path_1.join)(storage, 'sandboxes', 'sync'), { recursive: true, force: true });
|
|
1012
1014
|
const sandboxSync = new sandbox_sync_1.SandboxSync({
|
|
1013
1015
|
files: (owner, id) => sandboxes.registry.files(owner, id),
|
|
1014
1016
|
upOn: (id) => registry.upOn(id),
|
|
1015
1017
|
send: (run, changes) => registry.syncFiles(run, changes),
|
|
1016
|
-
stagingRoot: sandboxSyncStaging,
|
|
1017
1018
|
log,
|
|
1018
1019
|
});
|
|
1019
|
-
// what a server that stopped staged is no batch of any run now
|
|
1020
|
-
await sandboxSync.sweep();
|
|
1021
1020
|
const executorOptions = {
|
|
1022
1021
|
definitions,
|
|
1023
1022
|
planning,
|
|
@@ -1030,7 +1029,7 @@ async function startServer(config, environment, options = {}) {
|
|
|
1030
1029
|
network: config.sandboxes.network,
|
|
1031
1030
|
started: async (run) => sandboxSync.started(run),
|
|
1032
1031
|
ended: async (run) => sandboxSync.forget(run.id),
|
|
1033
|
-
|
|
1032
|
+
syncFailed: (runId) => sandboxSync.failed(runId),
|
|
1034
1033
|
},
|
|
1035
1034
|
localTasksOnHost,
|
|
1036
1035
|
...(config.backend.type === 'host' && config.backend.capabilities
|