@torrent-tv/proxy 2.80.12 → 2.80.13
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/CHANGELOG.md +9 -0
- package/docs/download-architecture.md +23 -0
- package/docs/encode-architecture.md +30 -0
- package/package.json +1 -1
- package/routes/stream/get.js +324 -300
- package/routes/transcode/session-file/get.js +22 -2
- package/services/encode/SegmentDemand.js +34 -0
- package/services/hls-session-manager.js +14 -16
- package/services/orchestrators/EncodeOrchestrator.js +735 -726
- package/services/priority/WaitLedger.js +142 -0
- package/services/torrent-pool.js +38 -1
- package/services/torrent-worker/client.js +27 -12
- package/test/wait-ledger.test.js +100 -0
|
@@ -1,726 +1,735 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file How many encoders run on this proxy, on which outputs, and over which
|
|
3
|
-
* stretches.
|
|
4
|
-
*
|
|
5
|
-
* The application layer over `encode/`. It holds four things and owns none of
|
|
6
|
-
* their rules:
|
|
7
|
-
*
|
|
8
|
-
* 1. what viewers want (`SegmentDemand`), stated once each and read as a union;
|
|
9
|
-
* 2. what has been made and what is being made, one `CoverageMap` per output;
|
|
10
|
-
* 3. the encoders that are running (`EncodeRun`), each over a stretch;
|
|
11
|
-
* 4. a tally of how every run ended, which is what makes "abnormal endings do
|
|
12
|
-
* not happen" a number rather than an impression.
|
|
13
|
-
*
|
|
14
|
-
* The decision itself is `EncodePlan.planEncoders`, from numbers alone. This
|
|
15
|
-
* carries it out, and everything it cannot know is injected: how many encoders
|
|
16
|
-
* this machine can afford, how a run is built for a given stretch, and which
|
|
17
|
-
* segments already exist.
|
|
18
|
-
*
|
|
19
|
-
* **No viewer reaches the decision.** A viewer states a window and is forgotten
|
|
20
|
-
* as a name; what the plan sees is a union of windows. That is the rule the
|
|
21
|
-
* layer exists for, stated by the user 2026-09-04: requests come from any
|
|
22
|
-
* viewers in any number, encoders are managed to suit them, and viewers get the
|
|
23
|
-
* result when it is ready.
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
import { CoverageMap } from "../encode/CoverageMap.js";
|
|
27
|
-
import { firstUnmetWant, planEncoders } from "../encode/EncodePlan.js";
|
|
28
|
-
import { endOfRun } from "../encode/EncodeRun.js";
|
|
29
|
-
import { ENCODE_EXIT } from "../encode/encode-exit.js";
|
|
30
|
-
import { affordableRuns } from "../encode/run-budget.js";
|
|
31
|
-
import { RunCosts } from "../encode/run-costs.js";
|
|
32
|
-
import { contentionPenalty } from "../encode/contention.js";
|
|
33
|
-
import {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
* @param {
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* @param {
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* @param {() =>
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
//
|
|
93
|
-
//
|
|
94
|
-
//
|
|
95
|
-
|
|
96
|
-
this.
|
|
97
|
-
this.
|
|
98
|
-
|
|
99
|
-
//
|
|
100
|
-
//
|
|
101
|
-
//
|
|
102
|
-
this
|
|
103
|
-
|
|
104
|
-
//
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
//
|
|
108
|
-
//
|
|
109
|
-
//
|
|
110
|
-
|
|
111
|
-
this.
|
|
112
|
-
this.
|
|
113
|
-
this.
|
|
114
|
-
this.
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* @
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
* @
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
* the
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
* two
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
* @param {
|
|
175
|
-
* @param {
|
|
176
|
-
* @param {
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
* @
|
|
180
|
-
*
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
const
|
|
191
|
-
const
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
* @
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
* @param {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
* @param {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
* @param {
|
|
236
|
-
* @param {string} params.
|
|
237
|
-
* @param {
|
|
238
|
-
* @param {number} params.
|
|
239
|
-
* @param {number}
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
* @param {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
* @param {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
//
|
|
277
|
-
//
|
|
278
|
-
//
|
|
279
|
-
|
|
280
|
-
this.
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
* @param {
|
|
289
|
-
* @param {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
//
|
|
299
|
-
//
|
|
300
|
-
//
|
|
301
|
-
//
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
//
|
|
321
|
-
//
|
|
322
|
-
//
|
|
323
|
-
//
|
|
324
|
-
//
|
|
325
|
-
//
|
|
326
|
-
//
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
this
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
//
|
|
340
|
-
//
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
//
|
|
344
|
-
//
|
|
345
|
-
//
|
|
346
|
-
//
|
|
347
|
-
//
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
//
|
|
360
|
-
//
|
|
361
|
-
//
|
|
362
|
-
// encoder
|
|
363
|
-
//
|
|
364
|
-
//
|
|
365
|
-
//
|
|
366
|
-
//
|
|
367
|
-
//
|
|
368
|
-
//
|
|
369
|
-
//
|
|
370
|
-
//
|
|
371
|
-
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
//
|
|
375
|
-
//
|
|
376
|
-
|
|
377
|
-
const
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
//
|
|
382
|
-
//
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
//
|
|
388
|
-
//
|
|
389
|
-
//
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
//
|
|
393
|
-
//
|
|
394
|
-
//
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
//
|
|
398
|
-
//
|
|
399
|
-
//
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
//
|
|
403
|
-
//
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
this.
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
//
|
|
413
|
-
//
|
|
414
|
-
//
|
|
415
|
-
//
|
|
416
|
-
//
|
|
417
|
-
//
|
|
418
|
-
// the
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
`
|
|
423
|
-
`
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
//
|
|
434
|
-
//
|
|
435
|
-
//
|
|
436
|
-
|
|
437
|
-
this.#
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
//
|
|
446
|
-
//
|
|
447
|
-
//
|
|
448
|
-
//
|
|
449
|
-
//
|
|
450
|
-
//
|
|
451
|
-
//
|
|
452
|
-
//
|
|
453
|
-
//
|
|
454
|
-
//
|
|
455
|
-
//
|
|
456
|
-
//
|
|
457
|
-
//
|
|
458
|
-
//
|
|
459
|
-
//
|
|
460
|
-
//
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
//
|
|
466
|
-
//
|
|
467
|
-
//
|
|
468
|
-
//
|
|
469
|
-
//
|
|
470
|
-
//
|
|
471
|
-
//
|
|
472
|
-
//
|
|
473
|
-
//
|
|
474
|
-
//
|
|
475
|
-
//
|
|
476
|
-
//
|
|
477
|
-
//
|
|
478
|
-
|
|
479
|
-
const
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
this.
|
|
486
|
-
|
|
487
|
-
`
|
|
488
|
-
`
|
|
489
|
-
`
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
* @param {
|
|
497
|
-
* @param {number}
|
|
498
|
-
* @param {
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
//
|
|
503
|
-
//
|
|
504
|
-
//
|
|
505
|
-
//
|
|
506
|
-
//
|
|
507
|
-
//
|
|
508
|
-
//
|
|
509
|
-
//
|
|
510
|
-
//
|
|
511
|
-
//
|
|
512
|
-
//
|
|
513
|
-
//
|
|
514
|
-
//
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
//
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
//
|
|
524
|
-
//
|
|
525
|
-
//
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
onThisOutput.
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
//
|
|
535
|
-
//
|
|
536
|
-
//
|
|
537
|
-
//
|
|
538
|
-
//
|
|
539
|
-
//
|
|
540
|
-
//
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
this.
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
*
|
|
550
|
-
*
|
|
551
|
-
*
|
|
552
|
-
*
|
|
553
|
-
*
|
|
554
|
-
*
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
*
|
|
558
|
-
*
|
|
559
|
-
*
|
|
560
|
-
* @param {
|
|
561
|
-
* @
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
//
|
|
567
|
-
//
|
|
568
|
-
//
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
this.
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
//
|
|
580
|
-
//
|
|
581
|
-
//
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
this.
|
|
587
|
-
|
|
588
|
-
`(
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
*
|
|
597
|
-
*
|
|
598
|
-
*
|
|
599
|
-
*
|
|
600
|
-
*
|
|
601
|
-
*
|
|
602
|
-
*
|
|
603
|
-
*
|
|
604
|
-
* @param {
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
this.
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
* @param {
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
* it
|
|
630
|
-
*
|
|
631
|
-
*
|
|
632
|
-
*
|
|
633
|
-
*
|
|
634
|
-
*
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
//
|
|
640
|
-
//
|
|
641
|
-
//
|
|
642
|
-
//
|
|
643
|
-
//
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
.
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
*
|
|
662
|
-
*
|
|
663
|
-
*
|
|
664
|
-
*
|
|
665
|
-
*
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
*
|
|
679
|
-
*
|
|
680
|
-
*
|
|
681
|
-
*
|
|
682
|
-
*
|
|
683
|
-
*
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
const
|
|
690
|
-
const
|
|
691
|
-
const
|
|
692
|
-
const
|
|
693
|
-
|
|
694
|
-
.
|
|
695
|
-
|
|
696
|
-
//
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
.
|
|
700
|
-
.
|
|
701
|
-
|
|
702
|
-
//
|
|
703
|
-
//
|
|
704
|
-
//
|
|
705
|
-
//
|
|
706
|
-
//
|
|
707
|
-
//
|
|
708
|
-
//
|
|
709
|
-
//
|
|
710
|
-
//
|
|
711
|
-
//
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @file How many encoders run on this proxy, on which outputs, and over which
|
|
3
|
+
* stretches.
|
|
4
|
+
*
|
|
5
|
+
* The application layer over `encode/`. It holds four things and owns none of
|
|
6
|
+
* their rules:
|
|
7
|
+
*
|
|
8
|
+
* 1. what viewers want (`SegmentDemand`), stated once each and read as a union;
|
|
9
|
+
* 2. what has been made and what is being made, one `CoverageMap` per output;
|
|
10
|
+
* 3. the encoders that are running (`EncodeRun`), each over a stretch;
|
|
11
|
+
* 4. a tally of how every run ended, which is what makes "abnormal endings do
|
|
12
|
+
* not happen" a number rather than an impression.
|
|
13
|
+
*
|
|
14
|
+
* The decision itself is `EncodePlan.planEncoders`, from numbers alone. This
|
|
15
|
+
* carries it out, and everything it cannot know is injected: how many encoders
|
|
16
|
+
* this machine can afford, how a run is built for a given stretch, and which
|
|
17
|
+
* segments already exist.
|
|
18
|
+
*
|
|
19
|
+
* **No viewer reaches the decision.** A viewer states a window and is forgotten
|
|
20
|
+
* as a name; what the plan sees is a union of windows. That is the rule the
|
|
21
|
+
* layer exists for, stated by the user 2026-09-04: requests come from any
|
|
22
|
+
* viewers in any number, encoders are managed to suit them, and viewers get the
|
|
23
|
+
* result when it is ready.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { CoverageMap } from "../encode/CoverageMap.js";
|
|
27
|
+
import { firstUnmetWant, planEncoders } from "../encode/EncodePlan.js";
|
|
28
|
+
import { endOfRun } from "../encode/EncodeRun.js";
|
|
29
|
+
import { ENCODE_EXIT } from "../encode/encode-exit.js";
|
|
30
|
+
import { affordableRuns } from "../encode/run-budget.js";
|
|
31
|
+
import { RunCosts } from "../encode/run-costs.js";
|
|
32
|
+
import { contentionPenalty } from "../encode/contention.js";
|
|
33
|
+
import { waits } from "../priority/WaitLedger.js";
|
|
34
|
+
import { SegmentDemand } from "../encode/SegmentDemand.js";
|
|
35
|
+
|
|
36
|
+
export class EncodeOrchestrator {
|
|
37
|
+
/** Output address to what has been made of it. @type {Map<string, CoverageMap>} */
|
|
38
|
+
#coverage = new Map();
|
|
39
|
+
|
|
40
|
+
/** Output address to the runs on it. @type {Map<string, import("../encode/EncodeRun.js").EncodeRun[]>} */
|
|
41
|
+
#runs = new Map();
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
/** The fastest speed measured on one output, kept across restarts. @type {Map<string, number>} */
|
|
45
|
+
#lastSpeed = new Map();
|
|
46
|
+
|
|
47
|
+
/** How runs have ended, by cause. @type {Map<string, number>} */
|
|
48
|
+
#endings = new Map();
|
|
49
|
+
|
|
50
|
+
/** The last state said out loud, so an unchanged state is not repeated. */
|
|
51
|
+
#lastDescribed = "";
|
|
52
|
+
|
|
53
|
+
/** What a stop and a start have cost on this host. */
|
|
54
|
+
#costs = new RunCosts();
|
|
55
|
+
|
|
56
|
+
/** The last reason a budget was cut, so the same one is not said twice. */
|
|
57
|
+
#lastBudgetReason = new Map();
|
|
58
|
+
|
|
59
|
+
/** The last unmet want said out loud, so a stuck one is said once. */
|
|
60
|
+
#lastUnmet = new Map();
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @param {object} params
|
|
64
|
+
* @param {(address: string) => number} params.maxRunsFor - How many encoders
|
|
65
|
+
* this machine can afford on one output. The same arithmetic that decides
|
|
66
|
+
* the quality offer; measured per host, never chosen here.
|
|
67
|
+
* @param {(params: { address: string, from: number, to: number, because: string }) =>
|
|
68
|
+
* import("../encode/EncodeRun.js").EncodeRun} params.makeRun - Build a run
|
|
69
|
+
* for a stretch. What to read, what to map and how to cut belong to whoever
|
|
70
|
+
* knows the source.
|
|
71
|
+
* @param {number} params.segmentSeconds
|
|
72
|
+
* @param {import("../encode/contention.js").ContentionPenalties | null}
|
|
73
|
+
* [params.contentionPenalties] - How much slower one encoder runs beside
|
|
74
|
+
* others, MEASURED on this host at startup and keyed by how many others
|
|
75
|
+
* there are. Null until something has measured it, and then the penalty is
|
|
76
|
+
* 1 — a number invented here would be the same mistake as an invented
|
|
77
|
+
* encoding speed.
|
|
78
|
+
* @param {{ info: (line: string) => void, warn: (line: string) => void }} params.logger
|
|
79
|
+
* @param {() => number} [params.now]
|
|
80
|
+
*/
|
|
81
|
+
constructor({
|
|
82
|
+
maxRunsFor,
|
|
83
|
+
makeRun,
|
|
84
|
+
segmentSeconds,
|
|
85
|
+
contentionPenalties = null,
|
|
86
|
+
refetchSecPerFilmSecond = () => 0,
|
|
87
|
+
startingSpeedFor = () => 0,
|
|
88
|
+
segmentStore = null,
|
|
89
|
+
logger,
|
|
90
|
+
now
|
|
91
|
+
}) {
|
|
92
|
+
// The store of produced segments — the layer below this one. It is asked to
|
|
93
|
+
// clean up after a run that ended other than by reaching the end of its
|
|
94
|
+
// stretch, which is the one thing an ending must not leave behind: a file
|
|
95
|
+
// under a name that promises a whole segment.
|
|
96
|
+
this.segmentStore = segmentStore;
|
|
97
|
+
this.demand = new SegmentDemand();
|
|
98
|
+
this.maxRunsFor = maxRunsFor;
|
|
99
|
+
// Seconds of swarm time per second of film: what re-encoding material that
|
|
100
|
+
// already exists costs the download, over and above the encoder's own time.
|
|
101
|
+
// Injected, because the film's byte rate and the swarm's are measured
|
|
102
|
+
// elsewhere and this class must not reach for them.
|
|
103
|
+
this.refetchSecPerFilmSecond = refetchSecPerFilmSecond;
|
|
104
|
+
// Measured per host: what a second encoder costs the first. Unmeasured is 1,
|
|
105
|
+
// and then only the budget bounds how many there are.
|
|
106
|
+
this.contentionPenalties = contentionPenalties instanceof Map ? contentionPenalties : null;
|
|
107
|
+
// WHAT THIS HOST ENCODES AT BEFORE ANY RUN HAS REPORTED. The startup
|
|
108
|
+
// benchmark measures it — a real pipeline over real clips, before a viewer
|
|
109
|
+
// exists — so the plan is never asked to compare arrivals with no speed to
|
|
110
|
+
// compute them from. Every run that then works refines it.
|
|
111
|
+
this.startingSpeedFor = startingSpeedFor;
|
|
112
|
+
this.makeRun = makeRun;
|
|
113
|
+
this.segmentSeconds = segmentSeconds;
|
|
114
|
+
this.logger = logger;
|
|
115
|
+
this.now = typeof now === "function" ? now : Date.now;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The map of one output, made on first mention.
|
|
120
|
+
*
|
|
121
|
+
* @param {string} address
|
|
122
|
+
* @returns {CoverageMap}
|
|
123
|
+
*/
|
|
124
|
+
coverageOf(address) {
|
|
125
|
+
let map = this.#coverage.get(address);
|
|
126
|
+
if (!map) {
|
|
127
|
+
map = new CoverageMap();
|
|
128
|
+
this.#coverage.set(address, map);
|
|
129
|
+
}
|
|
130
|
+
return map;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Put the map's picture of what is ready back in step with the disk.
|
|
135
|
+
*
|
|
136
|
+
* ONE AUTHORITY ON WHAT EXISTS, AND IT IS THE STORE. The map holds no memory
|
|
137
|
+
* of readiness between calls: it is handed the whole answer, replacing
|
|
138
|
+
* whatever it had, immediately before anything is decided from it. So a
|
|
139
|
+
* segment whose file was discarded with the run that had it open, dropped to
|
|
140
|
+
* make room, or reopened by a run restarting on it stops being ready in the
|
|
141
|
+
* same breath — without anything having to notice and say so.
|
|
142
|
+
*
|
|
143
|
+
* The map used to be filled from outside, by the session manager, with a
|
|
144
|
+
* method that only ever added. Nothing anywhere took a number back. Field
|
|
145
|
+
* 2026-09-07: the map claimed all 482 segments of a film while the directory
|
|
146
|
+
* held nothing, so every arrangement scored perfect, the only encoder was
|
|
147
|
+
* stopped as unnecessary and none was placed again — two sessions in a row
|
|
148
|
+
* with no picture at all.
|
|
149
|
+
*
|
|
150
|
+
* A store is optional here only in the sense that an authority which was not
|
|
151
|
+
* supplied cannot be consulted: without one the map keeps what it was told
|
|
152
|
+
* directly, which is how this class is exercised with plain numbers.
|
|
153
|
+
*
|
|
154
|
+
* @param {string} address
|
|
155
|
+
* @returns {CoverageMap}
|
|
156
|
+
*/
|
|
157
|
+
#upToDateCoverage(address) {
|
|
158
|
+
const coverage = this.coverageOf(address);
|
|
159
|
+
if (this.segmentStore) {
|
|
160
|
+
coverage.setReady(this.segmentStore.provenNumbers(address));
|
|
161
|
+
}
|
|
162
|
+
return coverage;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Where a run started here must stop: the free stretch in front of it.
|
|
167
|
+
*
|
|
168
|
+
* Asked of the one map, brought up to date first. It used to be worked out by
|
|
169
|
+
* the session manager, which reached into this layer for the map and into the
|
|
170
|
+
* store for what was on the disk and put the two together itself — one fact
|
|
171
|
+
* with two owners and a third party carrying it between them, which is how the
|
|
172
|
+
* two came to disagree.
|
|
173
|
+
*
|
|
174
|
+
* @param {object} params
|
|
175
|
+
* @param {string} params.address
|
|
176
|
+
* @param {number} params.from - Where the run will start.
|
|
177
|
+
* @param {object | null} [params.exceptRun] - The run being replaced, whose
|
|
178
|
+
* own claim is not somebody else's material.
|
|
179
|
+
* @param {number} [params.segmentCount] - The output's length, when known.
|
|
180
|
+
* @returns {number} The last number to work through, or `-1` for the end of
|
|
181
|
+
* the film.
|
|
182
|
+
*/
|
|
183
|
+
freeStretchEnd({ address, from, exceptRun = null, segmentCount = 0 }) {
|
|
184
|
+
if (!address) {
|
|
185
|
+
return -1;
|
|
186
|
+
}
|
|
187
|
+
if (segmentCount > 0) {
|
|
188
|
+
this.coverageOf(address).setSegmentCount(segmentCount);
|
|
189
|
+
}
|
|
190
|
+
const coverage = this.#upToDateCoverage(address);
|
|
191
|
+
const start = Math.max(0, from);
|
|
192
|
+
const free = coverage.freeRunFrom(start, exceptRun);
|
|
193
|
+
if (!Number.isFinite(free)) {
|
|
194
|
+
return -1;
|
|
195
|
+
}
|
|
196
|
+
const end = start + Math.max(1, free) - 1;
|
|
197
|
+
return segmentCount > 0 && end >= segmentCount - 1 ? -1 : end;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* @param {string} address
|
|
202
|
+
* @returns {import("../encode/EncodeRun.js").EncodeRun[]}
|
|
203
|
+
*/
|
|
204
|
+
runsOn(address) {
|
|
205
|
+
return this.#runs.get(address) ?? [];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* How long an output is, once its playlist is known.
|
|
210
|
+
*
|
|
211
|
+
* @param {string} address
|
|
212
|
+
* @param {number} segmentCount
|
|
213
|
+
*/
|
|
214
|
+
setSegmentCount(address, segmentCount) {
|
|
215
|
+
this.coverageOf(address).setSegmentCount(segmentCount);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Segments that already exist — from a previous life of this process, or
|
|
220
|
+
* because somebody else made them. Told to the map, which is what stops an
|
|
221
|
+
* encoder being started to make them again.
|
|
222
|
+
*
|
|
223
|
+
* @param {string} address
|
|
224
|
+
* @param {Iterable<number>} indexes
|
|
225
|
+
*/
|
|
226
|
+
noteAlreadyMade(address, indexes) {
|
|
227
|
+
for (const index of indexes) {
|
|
228
|
+
this.noteProduced(address, index);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* A viewer states what it is waiting for. Replaces whatever it said before.
|
|
234
|
+
*
|
|
235
|
+
* @param {object} params
|
|
236
|
+
* @param {string} params.claimant
|
|
237
|
+
* @param {string} params.address
|
|
238
|
+
* @param {number} params.from
|
|
239
|
+
* @param {number} params.to
|
|
240
|
+
* @param {number} [params.priority] - Higher is sooner. One viewer states
|
|
241
|
+
* several stretches at once — what must be ready before they set off, what
|
|
242
|
+
* is reachable while they watch it, the rest of the track — and the filling
|
|
243
|
+
* takes them in this order. Absent means one undifferentiated want, which
|
|
244
|
+
* is what a caller that knows only a position states.
|
|
245
|
+
*/
|
|
246
|
+
/**
|
|
247
|
+
* What is wanted of one output, in its own segment numbers.
|
|
248
|
+
*
|
|
249
|
+
* ONE MAP, ALREADY MERGED, AND WITH NOBODY'S NAME ON IT. It is built once per
|
|
250
|
+
* film by the layer that knows where the viewers are; this layer receives it
|
|
251
|
+
* converted into an output's own numbering and never asks who is in it.
|
|
252
|
+
*
|
|
253
|
+
* That replaced a window per viewer per band stated here and merged here,
|
|
254
|
+
* which was the same work done twice in two layers, with the viewer's name as
|
|
255
|
+
* the key of a claim — against the rule that the encoding and the viewer are
|
|
256
|
+
* not connected at all.
|
|
257
|
+
*
|
|
258
|
+
* An empty map says nobody is coming anywhere in this output, and the plan
|
|
259
|
+
* stops its encoders for it. Nothing has to be released when somebody leaves:
|
|
260
|
+
* the map that arrives next simply does not have them in it.
|
|
261
|
+
*
|
|
262
|
+
* @param {string} address
|
|
263
|
+
* @param {{ from: number, to: number, priority: number, withinSeconds: number }[]} zones
|
|
264
|
+
*/
|
|
265
|
+
notePriorityMap(address, zones) {
|
|
266
|
+
this.demand.state(address, zones);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* A segment has been finished, by whichever run made it.
|
|
271
|
+
*
|
|
272
|
+
* @param {string} address
|
|
273
|
+
* @param {number} index
|
|
274
|
+
*/
|
|
275
|
+
noteProduced(address, index) {
|
|
276
|
+
// TOLD TO THE AUTHORITY, not only to the map. A piece being closed is a fact
|
|
277
|
+
// about the disk, and the store is what holds those; told to the map alone
|
|
278
|
+
// it would survive exactly until the next time the map is brought back into
|
|
279
|
+
// step, and then be gone with no file to show for it.
|
|
280
|
+
this.segmentStore?.markClosed(address, index);
|
|
281
|
+
this.coverageOf(address).markReady(index);
|
|
282
|
+
for (const run of this.runsOn(address)) {
|
|
283
|
+
run.noteProduced(index);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* @param {string} address
|
|
289
|
+
* @param {object} run
|
|
290
|
+
* @param {number} speedX
|
|
291
|
+
*/
|
|
292
|
+
noteSpeed(address, wanted, speedX) {
|
|
293
|
+
for (const run of this.runsOn(address)) {
|
|
294
|
+
if (run === wanted) {
|
|
295
|
+
run.noteSpeed(speedX);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
// HOW FAST THIS MACHINE ENCODES THIS OUTPUT is a property of the machine and
|
|
299
|
+
// the material, not of one process. Read off `run.speedX` alone it was lost
|
|
300
|
+
// at every restart: a moved encoder is a new object that has measured
|
|
301
|
+
// nothing, so the plan fell back to "nothing is known" and stopped comparing
|
|
302
|
+
// arrivals at all — which is every decision in this layer.
|
|
303
|
+
if (speedX > 0 && speedX > (this.#lastSpeed.get(address) ?? 0)) {
|
|
304
|
+
this.#lastSpeed.set(address, speedX);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Decide and act, for every output anybody wants anything of and every output
|
|
310
|
+
* that still has an encoder on it.
|
|
311
|
+
*
|
|
312
|
+
* Safe to call as often as anything changes: the plan is a function of the
|
|
313
|
+
* state, so a pass that finds nothing to change does nothing.
|
|
314
|
+
*/
|
|
315
|
+
reconcile() {
|
|
316
|
+
const addresses = new Set([...this.demand.addresses(), ...this.#runs.keys()]);
|
|
317
|
+
for (const address of addresses) {
|
|
318
|
+
this.#reconcileOne(address);
|
|
319
|
+
}
|
|
320
|
+
// WHAT THIS CLASS BELIEVES, said by this class. `describe()` was written
|
|
321
|
+
// and called from nowhere, so on 2026-09-05 the question "why did the plan
|
|
322
|
+
// not see the gap the viewer was stopped at" had to be answered by
|
|
323
|
+
// inference from start and stop lines, and was not answered at all.
|
|
324
|
+
//
|
|
325
|
+
// Printed on CHANGE rather than on a timer: a quiet session says nothing, a
|
|
326
|
+
// session that is deciding something says what it decided, and there is no
|
|
327
|
+
// interval to choose.
|
|
328
|
+
const state = this.describe();
|
|
329
|
+
if (state !== this.#lastDescribed) {
|
|
330
|
+
this.#lastDescribed = state;
|
|
331
|
+
this.logger.info(state);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* @param {string} address
|
|
337
|
+
*/
|
|
338
|
+
#reconcileOne(address) {
|
|
339
|
+
// WHAT EXISTS IS ASKED OF THE DISK, HERE, EVERY TIME. The plan is arithmetic
|
|
340
|
+
// over what is made, what is being made and what is wanted, and the first of
|
|
341
|
+
// those is not this layer's to remember.
|
|
342
|
+
const coverage = this.#upToDateCoverage(address);
|
|
343
|
+
// A run that has ended and said nothing. One built here reports its own
|
|
344
|
+
// ending and is released by `noteEnded`; one ADOPTED from elsewhere — a
|
|
345
|
+
// session whose encoder stopped — has no such promise, and its claim would
|
|
346
|
+
// otherwise sit in the map for the life of the process, telling the plan
|
|
347
|
+
// that a stretch nobody is making is being made. Nothing would ever be
|
|
348
|
+
// started there again.
|
|
349
|
+
for (const run of this.runsOn(address)) {
|
|
350
|
+
if (!run.isAlive && !run.isStopping) {
|
|
351
|
+
this.noteEnded({
|
|
352
|
+
address,
|
|
353
|
+
run,
|
|
354
|
+
ending: ENCODE_EXIT.GONE,
|
|
355
|
+
because: "it is no longer running, and it did not say so"
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
// ONE MAP, NOT ONE WINDOW PER VIEWER PER ZONE.
|
|
360
|
+
//
|
|
361
|
+
// Two viewers a few seconds apart state stretches that overlap, and the plan
|
|
362
|
+
// puts one encoder on each stretch it is given — so unmerged windows buy an
|
|
363
|
+
// encoder per viewer for film they both want, which is the opposite of what
|
|
364
|
+
// sharing the output is for. Merged, the highest rank and the soonest time
|
|
365
|
+
// per number win and the stretches do not overlap, so one encoder serves
|
|
366
|
+
// everyone standing in front of it.
|
|
367
|
+
//
|
|
368
|
+
// Asked of the register, which is the thing that holds the windows. This
|
|
369
|
+
// used to reach into the layer that STATES them for the same arithmetic,
|
|
370
|
+
// which is the coupling the layer rule forbids; the arithmetic itself now
|
|
371
|
+
// lives where it belongs to nobody.
|
|
372
|
+
const windows = this.demand.mapOn(address);
|
|
373
|
+
const live = this.runsOn(address).filter((run) => run.isAlive);
|
|
374
|
+
// Asked ONCE. It is arithmetic over measurements, but it also says out loud
|
|
375
|
+
// when the reason it cuts the budget changes, so asking it three times in
|
|
376
|
+
// one pass is three chances to say a thing that happened once.
|
|
377
|
+
const maxRuns = this.#affordableOn(address, live);
|
|
378
|
+
const actions = planEncoders({
|
|
379
|
+
coverage,
|
|
380
|
+
windows,
|
|
381
|
+
// The runs themselves. The plan is arithmetic and reads four numbers off
|
|
382
|
+
// each; what it hands back names the run by BEING it, so nothing has to
|
|
383
|
+
// invent a token to refer to one by.
|
|
384
|
+
runs: live,
|
|
385
|
+
maxRuns,
|
|
386
|
+
segmentSeconds: this.segmentSeconds,
|
|
387
|
+
// What a start and a kill cost, measured from this host's own runs rather
|
|
388
|
+
// than written into the code from one machine's reading. Zero until
|
|
389
|
+
// something has been measured, which is the same convention as the
|
|
390
|
+
// refetch price below and is stated so the bias is known.
|
|
391
|
+
...this.#costs.seconds(),
|
|
392
|
+
// What a second of film costs to fetch again, in seconds of swarm time.
|
|
393
|
+
// Answered by whoever measures the film's own byte rate and the swarm's;
|
|
394
|
+
// zero until they have, which makes driving through look cheaper than it
|
|
395
|
+
// is and is stated here so the bias is known.
|
|
396
|
+
refetchSecPerFilmSecond: this.refetchSecPerFilmSecond(address),
|
|
397
|
+
// How much slower one encoder runs beside others, read off this host's own
|
|
398
|
+
// startup measurement. A pure function over a measured table: beyond what
|
|
399
|
+
// was measured it holds the largest reading rather than continuing a curve
|
|
400
|
+
// nothing observed.
|
|
401
|
+
contentionPenaltyFor: (others) => contentionPenalty(others, this.contentionPenalties).penalty,
|
|
402
|
+
// The best figure this host has: what a run here is doing now, what one
|
|
403
|
+
// was last measured doing, or what the startup benchmark predicted. The
|
|
404
|
+
// first two are this output's own; the third exists before either.
|
|
405
|
+
speedX: Math.max(
|
|
406
|
+
live.reduce((best, run) => Math.max(best, run.speedX || 0), 0),
|
|
407
|
+
this.#lastSpeed.get(address) ?? 0,
|
|
408
|
+
this.startingSpeedFor(address) || 0
|
|
409
|
+
)
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
// A move is the plan taking a running encoder away from where it already
|
|
413
|
+
// stands, which is exactly the decision that was found wandering back and
|
|
414
|
+
// forth in the field on 2026-09-07 with no way to see why: the "because"
|
|
415
|
+
// line names the comparison in words, never the numbers it was decided
|
|
416
|
+
// from. Said here, once per reconcile, and only when a move actually
|
|
417
|
+
// happens — everything a rerun of the same decision needs: the windows
|
|
418
|
+
// this call saw (priority, the real time, which side of the viewers),
|
|
419
|
+
// the budget, and where every live run stood.
|
|
420
|
+
if (actions.some((action) => action.type === "move")) {
|
|
421
|
+
this.logger.info(
|
|
422
|
+
`encode-plan move on ${address}: windows=${JSON.stringify(windows)} ` +
|
|
423
|
+
`maxRuns=${maxRuns} ` +
|
|
424
|
+
`live=${JSON.stringify(live.map((run) => ({ from: run.from, to: run.to, head: run.head, speedX: run.speedX })))}`
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
for (const action of actions) {
|
|
428
|
+
if (action.type === "stop") {
|
|
429
|
+
this.#stop(action.run, action.because);
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
if (action.type === "move") {
|
|
433
|
+
// A running encoder's position cannot be changed — it is fixed when the
|
|
434
|
+
// process starts — so a move is this one ending and another beginning
|
|
435
|
+
// where the material is missing. Both halves are recorded as what they
|
|
436
|
+
// are, which is why the ending of a moved run is not called normal.
|
|
437
|
+
this.#stop(action.run, action.because);
|
|
438
|
+
this.#start(address, action.from, action.to, action.because);
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
if (action.type === "start") {
|
|
442
|
+
this.#start(address, action.from, action.to, action.because);
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
// A run that stays keeps its claim current: the free stretch ahead of it
|
|
446
|
+
// may have shrunk since it was given one.
|
|
447
|
+
//
|
|
448
|
+
// THE CLAIM IS THE STRETCH IT WAS GIVEN, and there is one rule for that
|
|
449
|
+
// everywhere. It used to be narrowed here to what the run had already
|
|
450
|
+
// MADE whenever the run had no end, which meant a run claimed the single
|
|
451
|
+
// number it was writing and nothing beyond. The plan then read the road
|
|
452
|
+
// in front of a working encoder as free and started more encoders on it:
|
|
453
|
+
// three processes writing one directory with the same names, field
|
|
454
|
+
// 2026-09-06, and a piece of the film lost for good when the first of
|
|
455
|
+
// them was cleaned up after.
|
|
456
|
+
//
|
|
457
|
+
// The worry that narrowing was written for is real and is answered where
|
|
458
|
+
// it belongs — a viewer opening the same film further in must not find
|
|
459
|
+
// every number taken. That is the plan's business, and the plan can take
|
|
460
|
+
// road away from a run that has no end, because such a run carries no
|
|
461
|
+
// `-to` and simply stops when its head meets somebody else's claim.
|
|
462
|
+
coverage.claim(action.run, action.from, endOfRun({ from: action.from, to: action.to }));
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// NOBODY IS MAKING WHAT SOMEBODY IS WAITING FOR. Said here, with the numbers
|
|
466
|
+
// the decision was taken from, because it is the one state in which a viewer
|
|
467
|
+
// waits for ever and every line above it reads as a healthy proxy.
|
|
468
|
+
//
|
|
469
|
+
// Field 2026-09-07, twice in one evening: the last word about an output was
|
|
470
|
+
// "the film is no worse off without it", and after it nothing — no run, no
|
|
471
|
+
// refusal, no answer to the browser's request for the header. The wait ended
|
|
472
|
+
// at the browser's own timeout with a message naming no cause, and the
|
|
473
|
+
// proxy's log named none either.
|
|
474
|
+
//
|
|
475
|
+
// SAID ONCE PER STATE, not once per pass. A stuck output is reconciled on
|
|
476
|
+
// every event that touches it, and a line repeated for as long as the state
|
|
477
|
+
// lasts is what buried the last one: 49 295 copies of `send queue stuck` in
|
|
478
|
+
// a 159 090-line file, 31 % of the log, all of one wedge.
|
|
479
|
+
const wanting = firstUnmetWant(coverage, windows);
|
|
480
|
+
const stillRunning = this.runsOn(address).filter((run) => run.isAlive);
|
|
481
|
+
if (wanting === null || stillRunning.length > 0) {
|
|
482
|
+
this.#lastUnmet.delete(address);
|
|
483
|
+
} else if (this.#lastUnmet.get(address) !== wanting) {
|
|
484
|
+
this.#lastUnmet.set(address, wanting);
|
|
485
|
+
const held = this.segmentStore ? this.segmentStore.filesHeld(address) : -1;
|
|
486
|
+
this.logger.warn(
|
|
487
|
+
`encode: #${wanting} of ${address} is wanted and NO ENCODER IS MAKING IT — ` +
|
|
488
|
+
`ready=${coverage.stats().ready} of ${coverage.segmentCount} ` +
|
|
489
|
+
`files=${held < 0 ? "?" : held} maxRuns=${maxRuns} ` +
|
|
490
|
+
`windows=${JSON.stringify(windows)}`
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* @param {string} address
|
|
497
|
+
* @param {number} from
|
|
498
|
+
* @param {number} to
|
|
499
|
+
* @param {string} because
|
|
500
|
+
*/
|
|
501
|
+
#start(address, from, to, because) {
|
|
502
|
+
// The encoder is built here and now: whoever builds one waits for nothing,
|
|
503
|
+
// so it exists by the time this line returns. That is what makes the
|
|
504
|
+
// stretch held from this instant — this class knows what it is making
|
|
505
|
+
// because it has just made it, and no second encoder can be started for the
|
|
506
|
+
// same stretch on the next pass.
|
|
507
|
+
//
|
|
508
|
+
// It was not always so. The builder used to answer with nothing and start
|
|
509
|
+
// the encoder behind the answer, so the stretch stayed FREE for as long as
|
|
510
|
+
// that took, and every pass in between started another one: 684 starts in
|
|
511
|
+
// 482 seconds of field 2026-09-05, of which 973 answers said the encoder
|
|
512
|
+
// was not there yet — every start without exception.
|
|
513
|
+
//
|
|
514
|
+
// The run names itself: identity is a property of the thing, and two
|
|
515
|
+
// places minting names is how one stops being unique.
|
|
516
|
+
const run = this.makeRun({ address, from, to, because });
|
|
517
|
+
if (!run) {
|
|
518
|
+
// A refusal, not a wait: no session serves this output, or this position
|
|
519
|
+
// has failed to start too many times running.
|
|
520
|
+
this.logger.warn(`encode: no encoder could be made for #${from}..#${to} of ${address}`);
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
// What this machine has been measured to do on this output, carried over.
|
|
524
|
+
// A restart does not make the machine slower, and without this every moved
|
|
525
|
+
// encoder began as one whose speed nothing had measured — which the plan
|
|
526
|
+
// reads as "no arrival can be computed" and answers by comparing nothing.
|
|
527
|
+
const known = this.#lastSpeed.get(address) ?? 0;
|
|
528
|
+
if (known > 0) {
|
|
529
|
+
run.noteSpeed(known);
|
|
530
|
+
}
|
|
531
|
+
const onThisOutput = this.#runs.get(address) ?? [];
|
|
532
|
+
onThisOutput.push(run);
|
|
533
|
+
this.#runs.set(address, onThisOutput);
|
|
534
|
+
// This run rewrites the stretch it was given, so what was closed inside that
|
|
535
|
+
// stretch is no longer closed. Without this a number closed by an earlier run
|
|
536
|
+
// stays servable while a later one is halfway through writing it again.
|
|
537
|
+
//
|
|
538
|
+
// Bounded by the run's own end, which is the same number the claim below
|
|
539
|
+
// carries. Unbounded it unproved the whole film beyond the start of any run,
|
|
540
|
+
// and readiness is now a projection of what is proven — so a one-segment run
|
|
541
|
+
// at the beginning would have declared the rest of the output unmade.
|
|
542
|
+
const runsTo = endOfRun({ from, to });
|
|
543
|
+
this.segmentStore?.forgetClosed(address, from, runsTo);
|
|
544
|
+
this.coverageOf(address).claim(run, from, runsTo);
|
|
545
|
+
run.start(because);
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* How many encoders may run on this output, from every limit at once.
|
|
550
|
+
*
|
|
551
|
+
* The processor is one of them and is answered from outside, where the
|
|
552
|
+
* machine is measured. The other two are known here: what the swarm delivers,
|
|
553
|
+
* through the seconds of swarm time a second of film costs, and — once it is
|
|
554
|
+
* supplied — the memory the piece store may hold against what one encoder's
|
|
555
|
+
* reader keeps.
|
|
556
|
+
*
|
|
557
|
+
* Said out loud when it is not the processor that decided, because "why is
|
|
558
|
+
* there only one encoder" is otherwise a question no log can answer.
|
|
559
|
+
*
|
|
560
|
+
* @param {string} address
|
|
561
|
+
* @param {{ speedX: number }[]} live
|
|
562
|
+
* @returns {number}
|
|
563
|
+
*/
|
|
564
|
+
#affordableOn(address, live) {
|
|
565
|
+
const byProcessor = Math.max(0, this.maxRunsFor(address));
|
|
566
|
+
// The best figure this host has: what a run here is doing now, what one was
|
|
567
|
+
// last measured doing, or what the startup benchmark predicted. The first
|
|
568
|
+
// two are this output's own; the third exists before either, so the budget
|
|
569
|
+
// is never asked to price encoders at a speed of zero.
|
|
570
|
+
const fastest = Math.max(
|
|
571
|
+
live.reduce((best, run) => Math.max(best, run.speedX || 0), 0),
|
|
572
|
+
this.#lastSpeed.get(address) ?? 0,
|
|
573
|
+
this.startingSpeedFor(address) || 0
|
|
574
|
+
);
|
|
575
|
+
const budget = affordableRuns({
|
|
576
|
+
byProcessor,
|
|
577
|
+
speedX: fastest,
|
|
578
|
+
refetchSecPerFilmSecond: this.refetchSecPerFilmSecond(address),
|
|
579
|
+
// How much slower one encoder runs beside others, read off this host's own
|
|
580
|
+
// startup measurement. A pure function over a measured table: beyond what
|
|
581
|
+
// was measured it holds the largest reading rather than continuing a curve
|
|
582
|
+
// nothing observed.
|
|
583
|
+
contentionPenaltyFor: (others) => contentionPenalty(others, this.contentionPenalties).penalty
|
|
584
|
+
});
|
|
585
|
+
if (budget.runs !== byProcessor && budget.because !== this.#lastBudgetReason.get(address)) {
|
|
586
|
+
this.#lastBudgetReason.set(address, budget.because);
|
|
587
|
+
this.logger.info(
|
|
588
|
+
`encode: ${budget.runs} encoder(s) on ${address.slice(0, 60)} — ${budget.because} ` +
|
|
589
|
+
`(the processor alone would allow ${byProcessor})`
|
|
590
|
+
);
|
|
591
|
+
}
|
|
592
|
+
return budget.runs;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* Take charge of a run this class did not start.
|
|
597
|
+
*
|
|
598
|
+
* The browser asks for a stream and a run begins for it, long before this
|
|
599
|
+
* class has an opinion. Left unknown, that run would be invisible to the plan
|
|
600
|
+
* — which would then start a second encoder over the same numbers, believing
|
|
601
|
+
* nothing was being made there. So whoever starts one hands it over, and from
|
|
602
|
+
* then on it is planned like any other.
|
|
603
|
+
*
|
|
604
|
+
* @param {string} address
|
|
605
|
+
* @param {{ id: string, from: number, to: number, head: number, speedX: number, isAlive: boolean, stop: (because: string) => void }} run
|
|
606
|
+
*/
|
|
607
|
+
adopt(address, run) {
|
|
608
|
+
if (!run) {
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
const onThisOutput = this.#runs.get(address) ?? [];
|
|
612
|
+
if (onThisOutput.includes(run)) {
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
onThisOutput.push(run);
|
|
616
|
+
this.#runs.set(address, onThisOutput);
|
|
617
|
+
this.coverageOf(address).claim(run, run.from, endOfRun(run));
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* @param {object} run
|
|
622
|
+
* @param {string} because
|
|
623
|
+
*/
|
|
624
|
+
#stop(run, because) {
|
|
625
|
+
run.stop(because);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* A run has ended, however it ended. Its stretch goes back to the map — what
|
|
630
|
+
* it finished stays made — and the ending is counted.
|
|
631
|
+
*
|
|
632
|
+
* Wired by whoever builds the run, so that a run built outside this class is
|
|
633
|
+
* still accounted for.
|
|
634
|
+
*
|
|
635
|
+
* @param {import("../encode/EncodeRun.js").RunEnded} ended
|
|
636
|
+
*/
|
|
637
|
+
noteEnded(ended) {
|
|
638
|
+
this.#costs.note(ended);
|
|
639
|
+
// Exactly one ending is normal — the run reached the end of the stretch it
|
|
640
|
+
// was given and closed its last file. Every other leaves a piece open, and
|
|
641
|
+
// that file looks finished however the run ended: stopped, ffmpeg writes it
|
|
642
|
+
// out and names it like any other; killed harder, it leaves the bytes it
|
|
643
|
+
// had. Either way it decodes and holds less film than its number promises.
|
|
644
|
+
// So what is kept is what the run PROVED it finished, and nothing beyond.
|
|
645
|
+
if (ended.ending !== ENCODE_EXIT.COMPLETE && this.segmentStore) {
|
|
646
|
+
void this.segmentStore
|
|
647
|
+
.discardOpenPieceOf(ended.address, { from: ended.from, to: ended.to }, null, ended.provenName)
|
|
648
|
+
.catch(() => {});
|
|
649
|
+
}
|
|
650
|
+
this.coverageOf(ended.address).release(ended.run);
|
|
651
|
+
const remaining = this.runsOn(ended.address).filter((run) => run !== ended.run);
|
|
652
|
+
if (remaining.length === 0) {
|
|
653
|
+
this.#runs.delete(ended.address);
|
|
654
|
+
} else {
|
|
655
|
+
this.#runs.set(ended.address, remaining);
|
|
656
|
+
}
|
|
657
|
+
this.#endings.set(ended.ending, (this.#endings.get(ended.ending) ?? 0) + 1);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* How runs have ended over the life of this process, by cause.
|
|
662
|
+
*
|
|
663
|
+
* The abnormal classes are meant to stand at zero. Without the count,
|
|
664
|
+
* "we understand why it ended" is indistinguishable from "we noticed it once".
|
|
665
|
+
*
|
|
666
|
+
* @returns {Record<string, number>}
|
|
667
|
+
*/
|
|
668
|
+
endings() {
|
|
669
|
+
/** @type {Record<string, number>} */
|
|
670
|
+
const tally = {};
|
|
671
|
+
for (const ending of Object.values(ENCODE_EXIT)) {
|
|
672
|
+
tally[ending] = this.#endings.get(ending) ?? 0;
|
|
673
|
+
}
|
|
674
|
+
return tally;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* One line saying what this proxy is encoding and whether anybody is waiting.
|
|
679
|
+
*
|
|
680
|
+
* `waiting` is the point of it: a proxy with encoders running and a viewer
|
|
681
|
+
* still stopped at a segment nobody is making is the failure this layer was
|
|
682
|
+
* built to remove, and it is visible here rather than inferred from a log.
|
|
683
|
+
*
|
|
684
|
+
* @returns {string}
|
|
685
|
+
*/
|
|
686
|
+
describe() {
|
|
687
|
+
const parts = [];
|
|
688
|
+
for (const address of new Set([...this.demand.addresses(), ...this.#runs.keys()])) {
|
|
689
|
+
const coverage = this.coverageOf(address);
|
|
690
|
+
const stated = this.demand.mapOn(address);
|
|
691
|
+
const windows = stated.map((zone) => ({ from: zone.from, to: zone.to }));
|
|
692
|
+
const waiting = firstUnmetWant(coverage, windows);
|
|
693
|
+
const runs = this.runsOn(address)
|
|
694
|
+
.map((run) => `#${run.head}..#${run.to}@${run.speedX.toFixed(1)}x`)
|
|
695
|
+
.join(" ");
|
|
696
|
+
// The zones as they were stated, with their order, so a plan that is
|
|
697
|
+
// working at the wrong end of the film is visible rather than inferred.
|
|
698
|
+
const zones = [...stated]
|
|
699
|
+
.sort((left, right) => (right.priority ?? 0) - (left.priority ?? 0) || left.from - right.from)
|
|
700
|
+
.map((w) => `p${w.priority ?? 0}:#${w.from}..#${w.to}`)
|
|
701
|
+
.join(" ");
|
|
702
|
+
// THE WHOLE ADDRESS. Cut to sixty characters, every output of one film
|
|
703
|
+
// printed the same string — the picture, its quality steps and each
|
|
704
|
+
// soundtrack are told apart only by the tail — so three lines of this
|
|
705
|
+
// could not be matched to the three things they describe. Read on
|
|
706
|
+
// 2026-09-07 while accounting for a session that produced nothing, and
|
|
707
|
+
// the accounting had to be done by which line carried a run.
|
|
708
|
+
//
|
|
709
|
+
// And WHAT THE DISK HOLDS beside what is proven closed. They are two
|
|
710
|
+
// different statements: files with nothing proving them closed reads as a
|
|
711
|
+
// reporting fault, no files at all reads as an output yet to be made, and
|
|
712
|
+
// the difference decides where to look.
|
|
713
|
+
const held = this.segmentStore ? this.segmentStore.filesHeld(address) : -1;
|
|
714
|
+
// AND WHETHER THE MAP IS BEING SERVED IN ITS OWN ORDER. The zones say
|
|
715
|
+
// what matters most; this says what the viewer actually waited for, by
|
|
716
|
+
// band. Waits at `now` are the ones that cost a spinner, and until this
|
|
717
|
+
// existed there was no figure anywhere saying whether the urgent zone was
|
|
718
|
+
// served first — the map could have been read backwards and every line
|
|
719
|
+
// above would have looked the same.
|
|
720
|
+
const served = waits.describe(address);
|
|
721
|
+
parts.push(
|
|
722
|
+
`${address} ready=${coverage.stats().ready}` +
|
|
723
|
+
`${held < 0 ? "" : ` of ${held} file(s) on disk`} ` +
|
|
724
|
+
`zones=[${zones}] runs=[${runs}] ` +
|
|
725
|
+
`waiting=${waiting === null ? "nobody" : `#${waiting}`}` +
|
|
726
|
+
`${served ? ` served[${served}]` : ""}`
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
const tally = this.endings();
|
|
730
|
+
const endings = Object.entries(tally)
|
|
731
|
+
.map(([cause, count]) => `${cause}=${count}`)
|
|
732
|
+
.join(" ");
|
|
733
|
+
return `encode: ${parts.length === 0 ? "nothing wanted" : parts.join(" | ")} :: endings ${endings}`;
|
|
734
|
+
}
|
|
735
|
+
}
|