Atomic, EOF-checked extract downloads; Apply starts builds one after another so routing waits for the offline map's downloads

This commit is contained in:
randy committed 2026-11-13 22:28:37 +01:00
1 parent bf912b96e5
commit d93c77727f
3 files changed
+58 -2

No files matched your search

+13 -1
View File
@@ -543,11 +543,23 @@ module Maptool
last_modified = nil last_modified = nil
redirect_to = nil redirect_to = nil
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http| Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
# Net::HTTP's default (true) quietly accepts a body cut short of
# its Content-Length; a dropped connection must fail the download.
http.ignore_eof = false
http.request_get(uri) do |response| http.request_get(uri) do |response|
case response case response
when Net::HTTPSuccess when Net::HTTPSuccess
last_modified = response["last-modified"] last_modified = response["last-modified"]
File.open(dest, "wb") { |file| response.read_body { |chunk| file.write(chunk) } } # Into a .part file renamed at the end: an interrupted download
# never leaves a truncated extract behind its old stamp, and a
# routing build that hard-linked the old file keeps reading
# that complete old file (see docs/routing-design.adoc).
begin
File.open("#{dest}.part", "wb") { |file| response.read_body { |chunk| file.write(chunk) } }
File.rename("#{dest}.part", dest)
ensure
FileUtils.rm_f("#{dest}.part")
end
when Net::HTTPRedirection when Net::HTTPRedirection
redirect_to = URI.join(url, response["location"]).to_s redirect_to = URI.join(url, response["location"]).to_s
else else
+7 -1
View File
@@ -397,7 +397,13 @@ export class OfflineDataControl {
formatMeta: formatRoutingMeta, formatMeta: formatRoutingMeta,
}), }),
]; ];
this._regionPicker = new RegionPicker(() => this._rows.forEach((row) => row.triggerBuild())); // One after another (each returns once its build has started, not
// finished): the routing request must reach the server after the
// offline build has, so it waits for the extracts that build downloads
// (Routing.request_build) instead of building from half-written ones.
this._regionPicker = new RegionPicker(async () => {
for (const row of this._rows) await row.triggerBuild();
});
} }
mount(sidebar) { mount(sidebar) {
+38
View File
@@ -1,4 +1,5 @@
require_relative "test_helper" require_relative "test_helper"
require "socket"
require "maptool/pmtiles_builder" require "maptool/pmtiles_builder"
require "tmpdir" require "tmpdir"
require "fileutils" require "fileutils"
@@ -689,4 +690,41 @@ class PmtilesBuilderTest < Minitest::Test
gate.push(fake_manifest) gate.push(fake_manifest)
thread&.join thread&.join
end end
# A download replaces the extract by rename, so a hard link to the old one
# (a routing build reading it) keeps the complete old file, and a broken
# download leaves the old extract alone.
def test_download_extract_replaces_the_file_atomically
dest = File.join(@data_dir, "SK.osm.pbf")
File.write(dest, "old")
linked = File.join(@data_dir, "linked.pbf")
File.link(dest, linked)
serve_once("HTTP/1.1 200 OK\r\nContent-Length: 3\r\nLast-Modified: today\r\n\r\nnew") do |url|
assert_equal "today", Maptool::PmtilesBuilder.download_extract(url, dest)
end
assert_equal ["new", "old"], [File.read(dest), File.read(linked)]
serve_once("HTTP/1.1 200 OK\r\nContent-Length: 100\r\n\r\ncut") do |url|
assert_raises(StandardError) { Maptool::PmtilesBuilder.download_extract(url, dest) }
end
assert_equal "new", File.read(dest)
refute File.exist?("#{dest}.part")
end
private
def serve_once(response)
server = TCPServer.new("127.0.0.1", 0)
thread = Thread.new do
client = server.accept
client.readpartial(4096)
client.write(response)
client.close
end
yield "http://127.0.0.1:#{server.addr[1]}/x.osm.pbf"
ensure
thread&.join
server&.close
end
end end