@torrent-tv/proxy 2.83.4 → 2.83.6
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 +21 -0
- package/package.json +1 -1
- package/research/piece-withdrawn-but-still-claimed-2026-09-12.md +175 -0
- package/research/priority-map-is-the-truth-2026-09-12.md +35 -17
- package/services/download/withdraw-claim.js +80 -0
- package/services/hls-session-manager.js +14 -42
- package/services/orchestrators/EncodeOrchestrator.js +163 -0
- package/services/piece-store/piece-disk-store.js +24 -1
- package/services/piece-store/shared-piece-store.js +71 -1
- package/services/torrent-pool.js +76 -2
- package/services/torrent-worker/client.js +5 -2
- package/services/torrent-worker/piece-reader.js +30 -1
- package/services/torrent-worker/worker.js +29 -3
- package/test/input-lost-quiets-the-plan.test.js +261 -0
- package/test/logger-repeats.test.js +134 -0
- package/test/read-survives-withdrawal.test.js +164 -0
- package/test/withdraw-piece-claim.test.js +212 -0
- package/utils/logger.js +135 -7
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file One owner of the fact "this proxy has piece N".
|
|
3
|
+
*
|
|
4
|
+
* The store holds the bytes, so it owns the fact. The library keeps a second
|
|
5
|
+
* copy of it in its completion bitfield, and until 2026-09-12 nothing
|
|
6
|
+
* reconciled them: the disk tier dropped a piece once every reader was past it
|
|
7
|
+
* — correctly, that is what bounds the spill — and the bitfield went on saying
|
|
8
|
+
* the piece was verified. A read then concluded the piece was had, asked for it,
|
|
9
|
+
* was told it was absent, and failed; nothing fetched it again either, because
|
|
10
|
+
* the library does not download what it believes it owns. Field: a film played
|
|
11
|
+
* 80 seconds and then answered `Piece 0 is verified but absent from the store`
|
|
12
|
+
* for 92 minutes.
|
|
13
|
+
*
|
|
14
|
+
* Pinned here: the announcement and its one rule — said when the piece has gone
|
|
15
|
+
* from EVERYWHERE, never when one tier alone lost it — and the withdrawal.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import test from "node:test";
|
|
19
|
+
import assert from "node:assert/strict";
|
|
20
|
+
import os from "node:os";
|
|
21
|
+
import path from "node:path";
|
|
22
|
+
import fs from "node:fs/promises";
|
|
23
|
+
import { SharedPieceStore } from "../services/piece-store/shared-piece-store.js";
|
|
24
|
+
import { withdrawClaim } from "../services/download/withdraw-claim.js";
|
|
25
|
+
|
|
26
|
+
const PIECE = 1024;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A store of four pieces with room for one, so every admission spills the last,
|
|
30
|
+
* plus the announcements it made.
|
|
31
|
+
*/
|
|
32
|
+
async function storeWithGone(extras = {}) {
|
|
33
|
+
const directory = await fs.mkdtemp(path.join(os.tmpdir(), "withdraw-test-"));
|
|
34
|
+
/** @type {number[]} */
|
|
35
|
+
const gone = [];
|
|
36
|
+
const store = new SharedPieceStore(PIECE, {
|
|
37
|
+
length: 4 * PIECE,
|
|
38
|
+
memoryBytes: PIECE,
|
|
39
|
+
path: directory,
|
|
40
|
+
name: "test",
|
|
41
|
+
files: [{ offset: 0, length: 4 * PIECE, name: "file.bin" }],
|
|
42
|
+
onPieceGone: ({ index }) => gone.push(index),
|
|
43
|
+
...extras
|
|
44
|
+
});
|
|
45
|
+
const put = (index) => new Promise((resolve, reject) => {
|
|
46
|
+
store.put(index, Buffer.alloc(PIECE, index + 1), (error) => (error ? reject(error) : resolve()));
|
|
47
|
+
});
|
|
48
|
+
const clean = async () => {
|
|
49
|
+
store.destroy(() => undefined);
|
|
50
|
+
await fs.rm(directory, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
|
|
51
|
+
};
|
|
52
|
+
return { store, gone, put, clean };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Wait for the CONDITION, with a deadline only as a backstop. A fixed pause
|
|
57
|
+
* here would measure the machine rather than the store.
|
|
58
|
+
*
|
|
59
|
+
* @param {() => boolean} ready
|
|
60
|
+
* @param {string} what
|
|
61
|
+
*/
|
|
62
|
+
async function until(ready, what) {
|
|
63
|
+
const deadline = Date.now() + 5_000;
|
|
64
|
+
while (!ready()) {
|
|
65
|
+
if (Date.now() > deadline) {
|
|
66
|
+
throw new Error(`timed out waiting for ${what}`);
|
|
67
|
+
}
|
|
68
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
test("a piece left behind every reader is dropped AND the claim withdrawn", async () => {
|
|
73
|
+
const { store, gone, put, clean } = await storeWithGone();
|
|
74
|
+
try {
|
|
75
|
+
await put(0);
|
|
76
|
+
await put(1);
|
|
77
|
+
await put(2);
|
|
78
|
+
// WHERE THE READERS STAND, which is the whole trigger: the encoder ran
|
|
79
|
+
// ahead, so pieces 0-1 are behind every one of them. This is the production
|
|
80
|
+
// path — `reviseSpillCeiling` asks `forgetBehind(readHeads)` — and it is
|
|
81
|
+
// what dropped 565 pieces in the field.
|
|
82
|
+
store.protectRange("reader", 2, 3, 0);
|
|
83
|
+
const revision = store.reviseSpillCeiling(null);
|
|
84
|
+
|
|
85
|
+
assert.ok(revision.behind >= 1, `something should have been dropped, got ${revision.behind}`);
|
|
86
|
+
assert.ok(gone.includes(0), `piece 0 has gone and should say so, got ${JSON.stringify(gone)}`);
|
|
87
|
+
assert.ok(
|
|
88
|
+
gone.every((index) => index < 2),
|
|
89
|
+
`nothing a reader still wants may be announced, got ${JSON.stringify(gone)}`
|
|
90
|
+
);
|
|
91
|
+
} finally {
|
|
92
|
+
await clean();
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("a piece dropped as a duplicate of a file held whole is NOT announced", async () => {
|
|
97
|
+
const { store, gone, put, clean } = await storeWithGone({
|
|
98
|
+
// Every piece can be had from the assembled file, which is exactly why the
|
|
99
|
+
// spilled copy is being dropped. Nothing has been lost, so nothing is said.
|
|
100
|
+
isPieceElsewhere: () => true
|
|
101
|
+
});
|
|
102
|
+
try {
|
|
103
|
+
await put(0);
|
|
104
|
+
await put(1);
|
|
105
|
+
await put(2);
|
|
106
|
+
// The spill is what puts a piece on disk, and it finishes on its own time;
|
|
107
|
+
// dropping duplicates deliberately leaves a piece whose spill is still in
|
|
108
|
+
// flight alone, so the precondition is waited for rather than assumed.
|
|
109
|
+
await until(() => store.stats().spilled >= 1, "a piece to reach the disk");
|
|
110
|
+
const dropped = store.dropDuplicatesHeldElsewhere();
|
|
111
|
+
assert.ok(dropped >= 1, "the spilled duplicate should have been dropped");
|
|
112
|
+
assert.deepEqual(gone, [], "a piece still readable from a whole file has not gone");
|
|
113
|
+
} finally {
|
|
114
|
+
await clean();
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("a closing store announces nothing, because its torrent is going too", async () => {
|
|
119
|
+
const { store, gone, put, clean } = await storeWithGone();
|
|
120
|
+
try {
|
|
121
|
+
await put(0);
|
|
122
|
+
await put(1);
|
|
123
|
+
store.protectRange("reader", 2, 3, 0);
|
|
124
|
+
store.close(() => undefined);
|
|
125
|
+
store.reviseSpillCeiling(null);
|
|
126
|
+
assert.deepEqual(gone, [], "a claim withdrawn against a dying torrent reaches nothing useful");
|
|
127
|
+
} finally {
|
|
128
|
+
await clean();
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("the withdrawal is counted in the store's own figures", async () => {
|
|
133
|
+
const { store, put, clean } = await storeWithGone();
|
|
134
|
+
try {
|
|
135
|
+
await put(0);
|
|
136
|
+
await put(1);
|
|
137
|
+
await put(2);
|
|
138
|
+
store.protectRange("reader", 2, 3, 0);
|
|
139
|
+
store.reviseSpillCeiling(null);
|
|
140
|
+
assert.ok(
|
|
141
|
+
store.stats().withdrawn >= 1,
|
|
142
|
+
"the figure that makes the eviction's bargain checkable must move"
|
|
143
|
+
);
|
|
144
|
+
} finally {
|
|
145
|
+
await clean();
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("a piece the library thinks it has is withdrawn", () => {
|
|
150
|
+
const asked = [];
|
|
151
|
+
const torrent = {
|
|
152
|
+
name: "film.mkv",
|
|
153
|
+
destroyed: false,
|
|
154
|
+
bitfield: { get: () => true },
|
|
155
|
+
_markUnverified: (index) => asked.push(index)
|
|
156
|
+
};
|
|
157
|
+
assert.equal(withdrawClaim({ index: 3, files: [{ _torrent: torrent }] }), "withdrawn");
|
|
158
|
+
assert.deepEqual(asked, [3]);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test("a piece the library already knows is missing is left alone", () => {
|
|
162
|
+
let touched = 0;
|
|
163
|
+
const torrent = {
|
|
164
|
+
destroyed: false,
|
|
165
|
+
bitfield: { get: () => false },
|
|
166
|
+
_markUnverified: () => { touched += 1; }
|
|
167
|
+
};
|
|
168
|
+
assert.equal(
|
|
169
|
+
withdrawClaim({ index: 7, torrent }),
|
|
170
|
+
"nothing-to-withdraw",
|
|
171
|
+
"re-creating a piece the library is already fetching would discard its blocks in flight"
|
|
172
|
+
);
|
|
173
|
+
assert.equal(touched, 0);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test("a destroyed torrent is left alone", () => {
|
|
177
|
+
let touched = 0;
|
|
178
|
+
const torrent = {
|
|
179
|
+
destroyed: true,
|
|
180
|
+
bitfield: { get: () => true },
|
|
181
|
+
_markUnverified: () => { touched += 1; }
|
|
182
|
+
};
|
|
183
|
+
assert.equal(withdrawClaim({ index: 1, torrent }), "no-torrent");
|
|
184
|
+
assert.equal(touched, 0);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("a library that refuses says so instead of failing the eviction", () => {
|
|
188
|
+
const said = [];
|
|
189
|
+
const torrent = {
|
|
190
|
+
name: "film.mkv",
|
|
191
|
+
destroyed: false,
|
|
192
|
+
bitfield: { get: () => true },
|
|
193
|
+
_markUnverified: () => { throw new Error("no such method any more"); }
|
|
194
|
+
};
|
|
195
|
+
assert.equal(withdrawClaim({ index: 2, torrent, warn: (line) => said.push(line) }), "refused");
|
|
196
|
+
assert.equal(said.length, 1);
|
|
197
|
+
assert.match(said[0], /piece 2 of film\.mkv/);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test("a store with nobody listening evicts exactly as it did before", async () => {
|
|
201
|
+
const { store, put, clean } = await storeWithGone({ onPieceGone: undefined });
|
|
202
|
+
try {
|
|
203
|
+
await put(0);
|
|
204
|
+
await put(1);
|
|
205
|
+
await put(2);
|
|
206
|
+
store.protectRange("reader", 2, 3, 0);
|
|
207
|
+
const revision = store.reviseSpillCeiling(null);
|
|
208
|
+
assert.ok(revision.behind >= 1, "the eviction does not depend on anybody listening");
|
|
209
|
+
} finally {
|
|
210
|
+
await clean();
|
|
211
|
+
}
|
|
212
|
+
});
|
package/utils/logger.js
CHANGED
|
@@ -27,11 +27,18 @@ const PREFIX = "[proxy-client]";
|
|
|
27
27
|
/**
|
|
28
28
|
* When the file is rotated, and how many turns are kept.
|
|
29
29
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
30
|
+
* **Why it is this large.** It was 32 MiB, and that erased the beginning of
|
|
31
|
+
* the very failure it was needed for. Field 2026-09-12: a session froze at
|
|
32
|
+
* 17:20 and printed one established fact about 55 times a second, so the file
|
|
33
|
+
* turned over twice before the session ended — 159 000 lines covering
|
|
34
|
+
* 17:51-18:29, then 76 385 covering 18:29-18:52. Sixty-one minutes was all
|
|
35
|
+
* that survived of ninety-two, and the second rotation overwrote the turn that
|
|
36
|
+
* held the onset. The disk it is bounded for had 91.4 GB free at the time.
|
|
37
|
+
*
|
|
38
|
+
* The repetition is a separate fault and is being fixed separately; a log that
|
|
39
|
+
* cannot hold a session either way is the one that has to go first.
|
|
33
40
|
*/
|
|
34
|
-
const MAX_FILE_BYTES =
|
|
41
|
+
const MAX_FILE_BYTES = 1024 * 1024 * 1024;
|
|
35
42
|
|
|
36
43
|
/** @type {import("node:fs").WriteStream | null} */
|
|
37
44
|
let fileStream = null;
|
|
@@ -159,6 +166,122 @@ export const logger = {
|
|
|
159
166
|
error: (message) => write("error", message, chalk.red, console.error)
|
|
160
167
|
};
|
|
161
168
|
|
|
169
|
+
/**
|
|
170
|
+
* Write a line that has ALREADY been through the repeat rule on another thread.
|
|
171
|
+
*
|
|
172
|
+
* The worker holds the same rule and applies it before forwarding, so running
|
|
173
|
+
* it again here would be one decision taken twice, on two different histories.
|
|
174
|
+
* It does not lose lines today — a re-printed line carries its held-back count,
|
|
175
|
+
* which makes the text unique — but that is an accident of the wording, and the
|
|
176
|
+
* file's own promise is that a line cannot reach the console and miss the file.
|
|
177
|
+
* It also filled this thread's bounded map with keys that can never repeat.
|
|
178
|
+
*
|
|
179
|
+
* @param {string} level
|
|
180
|
+
* @param {string} message - Already decided; written as it is.
|
|
181
|
+
* @returns {void}
|
|
182
|
+
*/
|
|
183
|
+
export function writeAlreadyDecided(level, message) {
|
|
184
|
+
const colour = level === "error"
|
|
185
|
+
? chalk.red
|
|
186
|
+
: level === "warn" ? chalk.yellow : level === "success" ? chalk.green : chalk.cyan;
|
|
187
|
+
const toConsole = level === "error" ? console.error : level === "warn" ? console.warn : console.log;
|
|
188
|
+
const line = `${PREFIX} [${ts()}] ${message}`;
|
|
189
|
+
toConsole(colour(line));
|
|
190
|
+
toFile(line);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* An established fact is said once, then with decreasing frequency.
|
|
195
|
+
*
|
|
196
|
+
* **Why.** A failure that establishes itself and does not change is printed by
|
|
197
|
+
* whatever loop meets it, at that loop's own rate. Field 2026-09-12: one
|
|
198
|
+
* absent piece produced 235 000 lines in 92 minutes — `Error opening input
|
|
199
|
+
* file …` 8514 times, `Error opening input files: End of file` 5712, the same
|
|
200
|
+
* `run-state` transition 2892, the same read failure 2884 — about 55 lines a
|
|
201
|
+
* second, and it turned the log over twice so the beginning of the failure was
|
|
202
|
+
* gone before anyone read it. A log is not vitiated by its size alone; it is
|
|
203
|
+
* vitiated by uniformity, and a bigger file does not fix that.
|
|
204
|
+
*
|
|
205
|
+
* **Matched VERBATIM — the whole line, no normalisation of numbers.** Measured
|
|
206
|
+
* on that log: exact repeats are 52 567 of 76 385 lines, 68.8 %, which is
|
|
207
|
+
* nearly all of the flood and carries no risk at all of merging two different
|
|
208
|
+
* statements. Normalising digits would catch a little more and would also merge
|
|
209
|
+
* the memory series — `rss=327MB`, `rss=726MB` — which exists precisely to
|
|
210
|
+
* catch a runaway, and suppressing it would be worse than the flood.
|
|
211
|
+
*/
|
|
212
|
+
const REPEAT_FIRST_MS = 1_000;
|
|
213
|
+
const REPEAT_MAX_MS = 60_000;
|
|
214
|
+
/**
|
|
215
|
+
* How many distinct lines are tracked. Bounded because it is keyed by the full
|
|
216
|
+
* text: a process that logs unique lines for ever must not grow a map of them.
|
|
217
|
+
*/
|
|
218
|
+
const REPEAT_KEYS = 512;
|
|
219
|
+
/** @type {Map<string, { suppressed: number, printedAt: number, interval: number }>} */
|
|
220
|
+
const recent = new Map();
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* What to append when a line is said again after repeats were held back.
|
|
224
|
+
*
|
|
225
|
+
* One function because it was written twice, in the two branches that decide to
|
|
226
|
+
* speak — which is how two statements of one rule drift apart.
|
|
227
|
+
*
|
|
228
|
+
* @param {number} heldBack
|
|
229
|
+
* @param {number} overMs
|
|
230
|
+
* @returns {string}
|
|
231
|
+
*/
|
|
232
|
+
function heldBackSuffix(heldBack, overMs) {
|
|
233
|
+
return heldBack > 0
|
|
234
|
+
? ` [said ${heldBack} more time(s) in the last ${(overMs / 1000).toFixed(1)}s]`
|
|
235
|
+
: "";
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Whether this line is a repeat to hold back, and what to say if it is not.
|
|
240
|
+
*
|
|
241
|
+
* @param {string} message
|
|
242
|
+
* @returns {{ hold: true } | { hold: false, suffix: string }}
|
|
243
|
+
*/
|
|
244
|
+
function repeatCheck(message) {
|
|
245
|
+
const now = Date.now();
|
|
246
|
+
const seen = recent.get(message);
|
|
247
|
+
// Unseen, or not seen for longer than the longest interval — which makes it
|
|
248
|
+
// news again rather than a continuing fact.
|
|
249
|
+
if (!seen || now - seen.printedAt > REPEAT_MAX_MS) {
|
|
250
|
+
// WHAT WAS HELD BACK IS STILL SAID. A stale entry can carry repeats that
|
|
251
|
+
// were never reported — a line said just under its interval and then not
|
|
252
|
+
// again for a while — and dropping the count here would be the quiet lie
|
|
253
|
+
// this whole rule exists to avoid.
|
|
254
|
+
const heldBack = seen?.suppressed ?? 0;
|
|
255
|
+
const overMs = seen ? now - seen.printedAt : 0;
|
|
256
|
+
recent.delete(message);
|
|
257
|
+
if (recent.size >= REPEAT_KEYS) {
|
|
258
|
+
// The least recently printed goes: `Map` keeps insertion order and every
|
|
259
|
+
// print re-inserts, so the first key is the oldest.
|
|
260
|
+
const oldest = recent.keys().next();
|
|
261
|
+
if (!oldest.done) {
|
|
262
|
+
recent.delete(oldest.value);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
recent.set(message, { suppressed: 0, printedAt: now, interval: REPEAT_FIRST_MS });
|
|
266
|
+
return { hold: false, suffix: heldBackSuffix(heldBack, overMs) };
|
|
267
|
+
}
|
|
268
|
+
if (now - seen.printedAt < seen.interval) {
|
|
269
|
+
seen.suppressed += 1;
|
|
270
|
+
return { hold: true };
|
|
271
|
+
}
|
|
272
|
+
const heldBack = seen.suppressed;
|
|
273
|
+
const overMs = now - seen.printedAt;
|
|
274
|
+
recent.delete(message);
|
|
275
|
+
recent.set(message, {
|
|
276
|
+
suppressed: 0,
|
|
277
|
+
printedAt: now,
|
|
278
|
+
interval: Math.min(REPEAT_MAX_MS, seen.interval * 2)
|
|
279
|
+
});
|
|
280
|
+
// SAID, not merely hidden: the rate is the fact here, and a log that quietly
|
|
281
|
+
// drops repeats reports a healthy proxy where a loop was spinning.
|
|
282
|
+
return { hold: false, suffix: heldBackSuffix(heldBack, overMs) };
|
|
283
|
+
}
|
|
284
|
+
|
|
162
285
|
/**
|
|
163
286
|
* One path for every level, so a line cannot reach the console and miss the
|
|
164
287
|
* file depending on which method was called or which thread called it.
|
|
@@ -170,15 +293,20 @@ export const logger = {
|
|
|
170
293
|
* @returns {void}
|
|
171
294
|
*/
|
|
172
295
|
function write(level, message, colour, toConsole) {
|
|
173
|
-
|
|
296
|
+
const repeat = repeatCheck(message);
|
|
297
|
+
if (repeat.hold) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
const line = `${message}${repeat.suffix}`;
|
|
301
|
+
toConsole(colour(`${PREFIX} [${ts()}] ${line}`));
|
|
174
302
|
if (forward) {
|
|
175
303
|
try {
|
|
176
|
-
forward(level,
|
|
304
|
+
forward(level, line);
|
|
177
305
|
} catch {
|
|
178
306
|
// silent-ok: a thread whose channel has closed is shutting down, and a
|
|
179
307
|
// failed log line must not be what ends it.
|
|
180
308
|
}
|
|
181
309
|
return;
|
|
182
310
|
}
|
|
183
|
-
toFile(`${PREFIX} [${ts()}] ${
|
|
311
|
+
toFile(`${PREFIX} [${ts()}] ${line}`);
|
|
184
312
|
}
|