pmtiles-swarm 0.4.6 → 0.5.0
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 +23 -0
- package/package.json +1 -1
- package/src/config.js +23 -0
- package/src/index.js +13 -0
- package/src/prewarm.js +152 -0
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,29 @@
|
|
|
7
7
|
### 🐞 Bug fixes
|
|
8
8
|
- _...Add new stuff here..._
|
|
9
9
|
|
|
10
|
+
## 0.5.0
|
|
11
|
+
### ✨ Features and improvements
|
|
12
|
+
- **The head of a newly joined archive is read without waiting to be asked.** A PMTiles archive
|
|
13
|
+
is useless until its 127-byte header has been read: it names where the root directory and the
|
|
14
|
+
JSON metadata live, and reading it raises both to a high piece priority — so the head of the
|
|
15
|
+
file arrives out of order instead of whenever a download happens to reach byte zero. That
|
|
16
|
+
machinery existed and was spec-correct, but only ran when something read the archive, and the
|
|
17
|
+
backfill that would have followed up began by requiring a summary to already exist. A freshly
|
|
18
|
+
joined archive has none, and the one thing that would have created one was the TileJSON route,
|
|
19
|
+
which is exactly what fails without a header. So an archive being mirrored stayed unservable
|
|
20
|
+
for hours while the few kilobytes that would have made it servable sat at position zero.
|
|
21
|
+
|
|
22
|
+
It now reads one archive's head at a time — several at once turn a queue of archives into a
|
|
23
|
+
queue of stalled reads competing for the same bandwidth — with the long metadata timeout
|
|
24
|
+
rather than the interactive one, backing off between attempts, because a young archive having
|
|
25
|
+
no peer that holds its first piece is ordinary rather than exceptional. It comes back for
|
|
26
|
+
vector layers separately, since a writer may put the JSON metadata after every tile and one
|
|
27
|
+
read routinely gets the header and not the metadata.
|
|
28
|
+
|
|
29
|
+
New under `tiles`: `prewarm` (default true), `prewarmIntervalSeconds` (30) and
|
|
30
|
+
`prewarmBackoffSeconds` (120). Turn it off on a node that distributes archives but never
|
|
31
|
+
serves tiles from them.
|
|
32
|
+
|
|
10
33
|
## 0.4.6
|
|
11
34
|
### ✨ Features and improvements
|
|
12
35
|
- **A feed's categories are a list, and are called categories.** The console offered a single
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pmtiles-swarm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "BitTorrent distribution for PMTiles map archives: create torrents, watch folders, publish and subscribe to RSS feeds, and seed through qBittorrent or an embedded client",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
package/src/config.js
CHANGED
|
@@ -444,6 +444,29 @@ const DEFAULTS = {
|
|
|
444
444
|
* like a hang rather than like "not yet".
|
|
445
445
|
*/
|
|
446
446
|
headerTimeoutMs: 12000,
|
|
447
|
+
/**
|
|
448
|
+
* Read the head of a newly joined archive without waiting to be asked.
|
|
449
|
+
*
|
|
450
|
+
* A PMTiles archive is useless until its header has been read: it names
|
|
451
|
+
* where the root directory and the JSON metadata are, and reading it also
|
|
452
|
+
* raises those to a high piece priority, so the head of the file arrives
|
|
453
|
+
* out of order rather than whenever a download happens to reach it. Left
|
|
454
|
+
* to the first request, an archive being mirrored is unservable for hours
|
|
455
|
+
* while the bytes that would make it servable sit at position zero.
|
|
456
|
+
*
|
|
457
|
+
* Off makes sense on a node that only distributes and never serves tiles.
|
|
458
|
+
*/
|
|
459
|
+
prewarm: true,
|
|
460
|
+
/** How often to look for an archive whose head has not been read. */
|
|
461
|
+
prewarmIntervalSeconds: 30,
|
|
462
|
+
/**
|
|
463
|
+
* How long to leave an archive alone after a failed read.
|
|
464
|
+
*
|
|
465
|
+
* Failure here is ordinary rather than exceptional — a brand-new archive
|
|
466
|
+
* may have no peer holding its first piece yet — so this is a retry
|
|
467
|
+
* interval, not an error budget.
|
|
468
|
+
*/
|
|
469
|
+
prewarmBackoffSeconds: 120,
|
|
447
470
|
/**
|
|
448
471
|
* What a missing tile answers with: true for 404, false for 204.
|
|
449
472
|
*
|
package/src/index.js
CHANGED
|
@@ -19,6 +19,7 @@ import { closeServer, installSignalHandlers, runStoppers } from './shutdown.js';
|
|
|
19
19
|
import { ScheduledSourceManager } from './sources.js';
|
|
20
20
|
import { SubscriptionManager } from './subscriptions.js';
|
|
21
21
|
import { TileStore } from './tiles.js';
|
|
22
|
+
import { HeadWarmer } from './prewarm.js';
|
|
22
23
|
import { WarmRunner } from './warm.js';
|
|
23
24
|
import { WatchManager } from './watch.js';
|
|
24
25
|
|
|
@@ -216,6 +217,12 @@ PMTILES_SWARM_PUBLIC_URL
|
|
|
216
217
|
library.attachTiles(tiles);
|
|
217
218
|
const warm = new WarmRunner(tiles);
|
|
218
219
|
|
|
220
|
+
// Reads the head of anything joined but not yet understood — the header,
|
|
221
|
+
// then the root directory and metadata it points at. Without this an archive
|
|
222
|
+
// being mirrored is unservable until the download happens to reach byte
|
|
223
|
+
// zero, and the request that would have read it times out long before.
|
|
224
|
+
const headWarmer = new HeadWarmer(tiles, catalog, config);
|
|
225
|
+
|
|
219
226
|
// Restarting one subsystem, rather than the process, for the settings that
|
|
220
227
|
// only that subsystem reads. Each stops and starts from the live config, so
|
|
221
228
|
// nothing here has to know what changed — only what to rebuild.
|
|
@@ -337,6 +344,11 @@ PMTILES_SWARM_PUBLIC_URL
|
|
|
337
344
|
}
|
|
338
345
|
hooks.start();
|
|
339
346
|
completion.start();
|
|
347
|
+
// The header is 127 bytes and names where the root directory and the JSON
|
|
348
|
+
// metadata live (PMTiles v3 spec, fields at offsets 8 and 24). Reading it
|
|
349
|
+
// raises both to a high piece priority, so an archive becomes servable in
|
|
350
|
+
// seconds rather than at whatever hour the download reaches byte zero.
|
|
351
|
+
headWarmer.start();
|
|
340
352
|
|
|
341
353
|
// Watch the sources archives were built from. A changed source does not
|
|
342
354
|
// invalidate its torrent, but it does mean any web seed pointing there will
|
|
@@ -375,6 +387,7 @@ PMTILES_SWARM_PUBLIC_URL
|
|
|
375
387
|
completion.stop();
|
|
376
388
|
subscriptions.stop();
|
|
377
389
|
warm.stop();
|
|
390
|
+
headWarmer.stop();
|
|
378
391
|
}, ms: 1000 },
|
|
379
392
|
{ label: 'watchers', stop: () => watch.stop() },
|
|
380
393
|
{
|
package/src/prewarm.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading the head of a newly joined archive, before anybody asks for it.
|
|
3
|
+
*
|
|
4
|
+
* A PMTiles archive is useless until its header has been read: the header is
|
|
5
|
+
* the first 127 bytes and names where the root directory and the JSON metadata
|
|
6
|
+
* live, and without it there is no TileJSON, no vector layers, and a preview
|
|
7
|
+
* that renders black. Reading it also *prioritises* what it found — the source
|
|
8
|
+
* hints the root directory as critical and the metadata as high — so the head
|
|
9
|
+
* of the file arrives out of order rather than whenever the download reaches
|
|
10
|
+
* it.
|
|
11
|
+
*
|
|
12
|
+
* None of that happened on its own. The read is on the interactive path, so it
|
|
13
|
+
* ran when somebody opened the archive; and the backfill that follows it up
|
|
14
|
+
* required a summary to already exist, which is precisely what a freshly joined
|
|
15
|
+
* archive does not have. So the first request paid for the header, and if it
|
|
16
|
+
* timed out first — which against a 72 GiB mirror with no web seed it does —
|
|
17
|
+
* nothing ever tried again.
|
|
18
|
+
*
|
|
19
|
+
* A mirror gets there eventually by downloading everything. The point of doing
|
|
20
|
+
* it deliberately is that "eventually" is hours, and the archive is servable in
|
|
21
|
+
* the first few seconds if the right few kilobytes are asked for first.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** How long to leave an archive alone after a failed attempt. */
|
|
25
|
+
const DEFAULT_BACKOFF_MS = 120000;
|
|
26
|
+
|
|
27
|
+
export class HeadWarmer {
|
|
28
|
+
#tiles;
|
|
29
|
+
#catalog;
|
|
30
|
+
#config;
|
|
31
|
+
#now;
|
|
32
|
+
#timer;
|
|
33
|
+
#tried = new Map();
|
|
34
|
+
#running = false;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param {object} tiles - The tile store, for `summarize`.
|
|
38
|
+
* @param {object} catalog - Where the summary is written.
|
|
39
|
+
* @param {object} config - Resolved configuration.
|
|
40
|
+
* @param {Function} [now] - Clock, for testing.
|
|
41
|
+
*/
|
|
42
|
+
constructor(tiles, catalog, config, now = () => Date.now()) {
|
|
43
|
+
this.#tiles = tiles;
|
|
44
|
+
this.#catalog = catalog;
|
|
45
|
+
this.#config = config;
|
|
46
|
+
this.#now = now;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** @returns {boolean} - Whether this node can and should warm anything. */
|
|
50
|
+
get enabled() {
|
|
51
|
+
return (
|
|
52
|
+
this.#config.tiles?.prewarm !== false &&
|
|
53
|
+
typeof this.#tiles?.summarize === 'function'
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Whether an archive is worth reading the head of right now.
|
|
59
|
+
*
|
|
60
|
+
* Deliberately narrow: an archive that has been summarised is done, unless it
|
|
61
|
+
* is vector and its layers are still missing — that section sits at the far
|
|
62
|
+
* end of the file and routinely arrives later than the header.
|
|
63
|
+
* @param {object} entry - A catalog entry.
|
|
64
|
+
* @returns {boolean} - True to attempt a read.
|
|
65
|
+
*/
|
|
66
|
+
due(entry) {
|
|
67
|
+
// Only PMTiles has a head worth reading. A .osm.pbf from a feed is not an
|
|
68
|
+
// archive this can say anything about.
|
|
69
|
+
if (entry.kind && entry.kind !== 'pmtiles') return false;
|
|
70
|
+
|
|
71
|
+
const summary = entry.pmtiles;
|
|
72
|
+
if (summary && (summary.format !== 'pbf' || summary.vectorLayers)) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const last = this.#tried.get(entry.infoHash) ?? 0;
|
|
77
|
+
const backoff = (this.#config.tiles?.prewarmBackoffSeconds ?? 120) * 1000;
|
|
78
|
+
return this.#now() - last >= (backoff || DEFAULT_BACKOFF_MS);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Reads the head of one archive that needs it.
|
|
83
|
+
*
|
|
84
|
+
* One per pass on purpose. Each read is a byte range fetched out of a swarm
|
|
85
|
+
* that may have no peer holding it yet, and starting several at once turns a
|
|
86
|
+
* queue of archives into a queue of stalled reads competing for the same
|
|
87
|
+
* bandwidth — the same mistake as prefetching leaf directories eagerly, which
|
|
88
|
+
* measured four times slower than not bothering.
|
|
89
|
+
* @returns {Promise<object|null>} - The entry warmed, or null.
|
|
90
|
+
*/
|
|
91
|
+
async sweep() {
|
|
92
|
+
if (!this.enabled || this.#running) return null;
|
|
93
|
+
|
|
94
|
+
const entry = this.#catalog.list().find((candidate) => this.due(candidate));
|
|
95
|
+
if (!entry) return null;
|
|
96
|
+
|
|
97
|
+
this.#running = true;
|
|
98
|
+
this.#tried.set(entry.infoHash, this.#now());
|
|
99
|
+
try {
|
|
100
|
+
// The long timeout, not the interactive one: this is a byte range from
|
|
101
|
+
// an archive nobody has asked for a piece of yet.
|
|
102
|
+
const summary = await this.#tiles.summarize(entry.infoHash, {
|
|
103
|
+
timeoutMs: this.#config.tiles?.metadataTimeoutMs ?? 120000,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const stored = await this.#catalog.put({
|
|
107
|
+
infoHash: entry.infoHash,
|
|
108
|
+
pmtiles: { ...entry.pmtiles, ...summary },
|
|
109
|
+
});
|
|
110
|
+
console.log(
|
|
111
|
+
`[warm] read the head of ${entry.name}` +
|
|
112
|
+
(summary.vectorLayers
|
|
113
|
+
? ` (${summary.vectorLayers.length} vector layers)`
|
|
114
|
+
: ''),
|
|
115
|
+
);
|
|
116
|
+
return stored;
|
|
117
|
+
} catch (error) {
|
|
118
|
+
// Ordinary while an archive is young — the piece holding the header may
|
|
119
|
+
// not exist anywhere reachable yet. Worth saying once per attempt, since
|
|
120
|
+
// it is the answer to "why is my preview blank", and the backoff keeps
|
|
121
|
+
// it from becoming noise.
|
|
122
|
+
console.warn(`[warm] ${entry.name}: ${error.message}`);
|
|
123
|
+
return null;
|
|
124
|
+
} finally {
|
|
125
|
+
this.#running = false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Starts warming, and keeps at it.
|
|
131
|
+
* @returns {void}
|
|
132
|
+
*/
|
|
133
|
+
start() {
|
|
134
|
+
if (!this.enabled) return;
|
|
135
|
+
const seconds = this.#config.tiles?.prewarmIntervalSeconds ?? 30;
|
|
136
|
+
if (seconds <= 0) return;
|
|
137
|
+
|
|
138
|
+
const run = () =>
|
|
139
|
+
this.sweep().catch((error) =>
|
|
140
|
+
console.error(`[warm] sweep failed: ${error.message}`),
|
|
141
|
+
);
|
|
142
|
+
run();
|
|
143
|
+
this.#timer = setInterval(run, seconds * 1000);
|
|
144
|
+
this.#timer.unref?.();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Stops warming. @returns {void} */
|
|
148
|
+
stop() {
|
|
149
|
+
if (this.#timer) clearInterval(this.#timer);
|
|
150
|
+
this.#timer = undefined;
|
|
151
|
+
}
|
|
152
|
+
}
|