maps: resumable, hash-verified planet upload to R2 (#perf)
- Commit
27fb828183a240299710298c8cbe6332a1fda97a- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-11 01:53 UTC (25 days ago)
- Parents
17469fe
What: upload_planet.py's base upload is rewritten around its own
multipart engine instead of boto3's TransferManager, and every mode
gains --dry-run. No new dependency: boto3 is now imported lazily, so
--dry-run and the new unit tests need nothing beyond the stdlib.
Why: the ~80 GB planet will be pulled back from a vast.ai build box to
the maproj server (8 vCPU, ~147 GB free, direct internet) and pushed to
R2 from there. The old script used fixed 100 MB parts x 10 threads, had
no way to resume (a dropped link after 70 GB restarted from zero), and
only checked the size afterwards, so a bad copy of the archive (e.g. a
corrupt pull from the build box) would have been published silently.
Changes:
(a) --part-size-mb / --concurrency (env R2_PART_SIZE_MB /
R2_CONCURRENCY). Default 64 MiB x 16 in flight; the part size is
raised automatically to stay under 9,000 parts (R2: <= 10,000,
equal parts 5 MiB..5 GiB but the last): an 81.3 GB planet is 1,212
parts, ~1 GiB of part buffers. boto3 max_pool_connections =
concurrency + 4, adaptive retries, and checksum calculation
"when_required" (botocore >= 1.36 adds CRCs R2 does not take);
each part carries Content-MD5 instead and its ETag is checked.
(b) Whole-file SHA-256, computed by one sequential reader beside the
part workers and compared with <file>.sha256 (the `sha256sum`
line mamaps_pack --sha256-out writes; or --sha256 HEX) BEFORE
CompleteMultipartUpload: on a mismatch the upload is aborted and
nothing becomes visible (exit 3). The hash rides
CreateMultipartUpload as x-amz-meta-sha256 and is checked after
completion with HEAD (size + hash). Without a sidecar it is
computed first so it can ride the create.
(c) Resume: upload id, file identity (size, mtime, part size, key)
and each completed part's ETag are saved atomically after every
part to <file>.upload-state.json (--state-file). A rerun lists the
server's parts, keeps those whose size is right and whose ETag
matches (recorded ETag, or the MD5 of the local bytes for parts
uploaded but not yet recorded), and uploads only the rest. A
changed file/key/part size aborts the stale upload and starts
over; an upload that already completed (crash after Complete) is
recognised by HEAD and reported done; --no-resume starts fresh.
(d) --dry-run runs all of it -- reads, MD5s, SHA-256, part plan,
state file, resume -- against FakeS3, an in-process bucket
persisted next to the state file (sizes/ETags/metadata only, so a
planet dry run costs kilobytes), and makes no network calls.
(e) --manifest compares server vs local: the base the manifest pins
is HEAD-checked against --base-len and (new) --base-sha256; a
segment R2 already holds with the same size and x-amz-meta-sha256
is kept, not re-uploaded; every segment is HEAD-verified (size +
stored hash == local SHA-256) before planet3.manifest.json is
written. The manifest JSON schema is unchanged.
Verified (nothing written to R2 or any remote):
- scripts/maps/test/test_upload_planet.py: 22 unittest cases on
FakeS3 (part planning, sidecar parsing, verified upload, mismatch
abort, resume after a dropped link, lost-state recovery, corrupt
server part, changed file, expired upload, crash after complete,
manifest keep/replace/base checks, CLI dry run); 3 s, no boto3.
- Real boto3 1.44 against a local moto server (venv outside the
repo): 301 MiB file in 5 MiB parts killed mid-upload (51 parts in
state, 52 on the server) -> rerun resumed with 9 parts, HEAD
metadata and GET bytes match the sidecar sha256; a local copy
corrupted after hashing -> exit 3, upload aborted, previous object
untouched; --manifest uploads, then keeps both segments on rerun,
and refuses a wrong --base-len.
- --dry-run on the real 81.3 GB world.mamaps: 1,212 x 64 MiB parts,
local sha256 == the build's world.mamaps.sha256 (73e488ee...),
1.07 GB peak RSS.Diff
scripts/maps/test/test_upload_planet.py
diff --git a/scripts/maps/test/test_upload_planet.py b/scripts/maps/test/test_upload_planet.pynew file mode 100644index 0000000000..a58555b8aa--- /dev/null+++ b/scripts/maps/test/test_upload_planet.py@@ -0,0 +1,290 @@+"""Unit tests for scripts/maps/upload_planet.py, against its in-process FakeS3.++No network and no boto3 needed:++ python3 -m unittest scripts/maps/test/test_upload_planet.py+"""+import contextlib+import hashlib+import importlib.util+import io+import json+import os+import random+import tempfile+import unittest++HERE = os.path.dirname(os.path.abspath(__file__))+spec = importlib.util.spec_from_file_location("upload_planet", os.path.join(HERE, "..", "upload_planet.py"))+up = importlib.util.module_from_spec(spec)+spec.loader.exec_module(up)++MIB = up.MIB+up.RETRY_BASE_S = 0.0 # the fake's dropped link fails every retry; don't sleep through them+++def load(path):+ with open(path) as f:+ return json.load(f)+++def save(path, obj):+ with open(path, "w") as f:+ json.dump(obj, f)+++def quiet(fn, *a, **kw):+ with contextlib.redirect_stdout(io.StringIO()) as out:+ rc = fn(*a, **kw)+ return rc, out.getvalue()+++class Files(unittest.TestCase):+ def setUp(self):+ self.tmp = tempfile.TemporaryDirectory()+ self.dir = self.tmp.name+ self.src = os.path.join(self.dir, "world.mamaps")+ # 23 MiB of non-repeating bytes: five 5 MiB parts, the last 3 MiB.+ data = random.Random(23).randbytes(23 * MIB)+ with open(self.src, "wb") as f:+ f.write(data)+ self.sha = hashlib.sha256(data).hexdigest()+ self.state = self.src + ".upload-state.json"+ self.bucket_file = self.state + ".dryrun-bucket.json"++ def tearDown(self):+ self.tmp.cleanup()++ def upload(self, s3, **kw):+ kw.setdefault("expected_sha", None)+ return quiet(up.multipart_upload, s3, self.src, "maps", "planet3.mamaps", 5, 3, self.state, **kw)++ def parts_sent(self, s3):+ return sorted(n for op, n in s3.calls if op == "part")+++class PartPlanning(unittest.TestCase):+ def test_planet_sized_file_uses_64_mib_parts(self):+ size = 81_331_811_296+ part = up.plan_part_size(size, 64)+ self.assertEqual(part, 64 * MIB)+ self.assertEqual(-(-size // part), 1212)++ def test_part_size_rises_to_stay_under_the_part_budget(self):+ size = 2 * 10**12+ part = up.plan_part_size(size, 64)+ self.assertLessEqual(-(-size // part), up.TARGET_MAX_PARTS)+ self.assertEqual(part % MIB, 0)++ def test_requests_below_the_s3_floor_are_raised(self):+ self.assertEqual(up.plan_part_size(100, 1), up.MIN_PART)+++class Sidecar(Files):+ def write(self, text):+ with open(self.src + ".sha256", "w") as f:+ f.write(text)++ def test_sha256sum_line_is_read(self):+ self.write(f"{self.sha} world.mamaps\n")+ self.assertEqual(up.read_sidecar(self.src + ".sha256", "world.mamaps"), self.sha)++ def test_absent_sidecar_is_none(self):+ self.assertIsNone(up.read_sidecar(self.src + ".sha256", "world.mamaps"))++ def test_a_sidecar_for_another_file_is_refused(self):+ self.write(f"{self.sha} other.mamaps\n")+ with self.assertRaises(SystemExit):+ up.read_sidecar(self.src + ".sha256", "world.mamaps")++ def test_a_malformed_sidecar_is_refused(self):+ self.write("not-a-hash world.mamaps\n")+ with self.assertRaises(SystemExit):+ up.read_sidecar(self.src + ".sha256", "world.mamaps")+++class Upload(Files):+ def test_upload_stores_and_verifies_the_hash(self):+ s3 = up.FakeS3()+ rc, out = self.upload(s3, expected_sha=self.sha)+ self.assertEqual(rc, 0, out)+ head = s3.head_object(Bucket="maps", Key="planet3.mamaps")+ self.assertEqual(head["ContentLength"], os.path.getsize(self.src))+ self.assertEqual(head["Metadata"]["sha256"], self.sha)+ self.assertEqual(self.parts_sent(s3), [1, 2, 3, 4, 5])+ self.assertFalse(os.path.exists(self.state), "state is removed after a verified upload")++ def test_without_a_sidecar_the_hash_is_computed_first(self):+ s3 = up.FakeS3()+ rc, out = self.upload(s3)+ self.assertEqual(rc, 0, out)+ self.assertEqual(s3.head_object(Bucket="maps", Key="planet3.mamaps")["Metadata"]["sha256"], self.sha)++ def test_a_local_copy_that_does_not_match_the_build_is_never_completed(self):+ s3 = up.FakeS3()+ rc, out = self.upload(s3, expected_sha="0" * 64)+ self.assertEqual(rc, 3, out)+ self.assertIn("HASH MISMATCH", out)+ self.assertIn(("abort", "planet3.mamaps"), s3.calls)+ self.assertNotIn(("complete", "planet3.mamaps"), s3.calls)+ self.assertIsNone(up.head_or_none(s3, "maps", "planet3.mamaps"))+++class Resume(Files):+ def test_an_interrupted_upload_resumes_with_only_the_missing_parts(self):+ rc, out = self.upload(up.FakeS3(self.bucket_file, fail_after_parts=2), expected_sha=self.sha)+ self.assertEqual(rc, 1, out)+ self.assertIn("rerun the same command to resume", out)+ st = load(self.state)+ self.assertEqual(len(st["parts"]), 2)+ s3 = up.FakeS3(self.bucket_file)+ rc, out = self.upload(s3, expected_sha=self.sha)+ self.assertEqual(rc, 0, out)+ self.assertIn("resuming upload", out)+ self.assertEqual(len(self.parts_sent(s3)), 3, "only the three missing parts are sent")+ self.assertEqual(s3.head_object(Bucket="maps", Key="planet3.mamaps")["Metadata"]["sha256"], self.sha)++ def test_a_part_the_state_file_missed_is_recovered_from_the_server(self):+ self.upload(up.FakeS3(self.bucket_file, fail_after_parts=3), expected_sha=self.sha)+ st = load(self.state)+ dropped = sorted(st["parts"])[0]+ del st["parts"][dropped] # crash after the upload, before the save+ save(self.state, st)+ s3 = up.FakeS3(self.bucket_file)+ rc, out = self.upload(s3, expected_sha=self.sha)+ self.assertEqual(rc, 0, out)+ self.assertNotIn(int(dropped), self.parts_sent(s3), "re-hashed locally, kept, not re-sent")+ self.assertEqual(len(self.parts_sent(s3)), 2)++ def test_a_server_part_with_the_wrong_bytes_is_re_sent(self):+ self.upload(up.FakeS3(self.bucket_file, fail_after_parts=2), expected_sha=self.sha)+ bucket = load(self.bucket_file)+ uid = next(iter(bucket["uploads"]))+ bad = sorted(bucket["uploads"][uid]["parts"])[0]+ bucket["uploads"][uid]["parts"][bad][1] = '"deadbeef"'+ save(self.bucket_file, bucket)+ s3 = up.FakeS3(self.bucket_file)+ rc, out = self.upload(s3, expected_sha=self.sha)+ self.assertEqual(rc, 0, out)+ self.assertIn(int(bad), self.parts_sent(s3))++ def test_a_changed_file_aborts_the_stale_upload_and_starts_over(self):+ self.upload(up.FakeS3(self.bucket_file, fail_after_parts=2), expected_sha=self.sha)+ os.utime(self.src, ns=(1, 1))+ s3 = up.FakeS3(self.bucket_file)+ rc, out = self.upload(s3, expected_sha=self.sha)+ self.assertEqual(rc, 0, out)+ self.assertIn(("abort", "planet3.mamaps"), s3.calls)+ self.assertEqual(self.parts_sent(s3), [1, 2, 3, 4, 5])++ def test_an_expired_upload_starts_over(self):+ self.upload(up.FakeS3(self.bucket_file, fail_after_parts=2), expected_sha=self.sha)+ bucket = load(self.bucket_file)+ bucket["uploads"] = {}+ save(self.bucket_file, bucket)+ s3 = up.FakeS3(self.bucket_file)+ rc, out = self.upload(s3, expected_sha=self.sha)+ self.assertEqual(rc, 0, out)+ self.assertIn("no longer exists", out)+ self.assertEqual(self.parts_sent(s3), [1, 2, 3, 4, 5])++ def test_a_crash_after_complete_is_recognised_as_done(self):+ s3 = up.FakeS3(self.bucket_file)+ self.assertEqual(self.upload(s3, expected_sha=self.sha)[0], 0)+ # Put back a state file for the (now completed) upload id.+ save(self.state, {"bucket": "maps", "key": "planet3.mamaps", "size": os.path.getsize(self.src),+ "mtime_ns": os.stat(self.src).st_mtime_ns, "part_size": 5 * MIB,+ "upload_id": "dryrun-gone", "sha256": self.sha, "parts": {}})+ s3 = up.FakeS3(self.bucket_file)+ rc, out = self.upload(s3, expected_sha=self.sha)+ self.assertEqual(rc, 0, out)+ self.assertIn("already completed", out)+ self.assertEqual(self.parts_sent(s3), [])++ def test_no_resume_starts_over(self):+ self.upload(up.FakeS3(self.bucket_file, fail_after_parts=2), expected_sha=self.sha)+ s3 = up.FakeS3(self.bucket_file)+ rc, out = self.upload(s3, expected_sha=self.sha, resume=False)+ self.assertEqual(rc, 0, out)+ self.assertEqual(self.parts_sent(s3), [1, 2, 3, 4, 5])+++class Cli(Files):+ def test_dry_run_reads_the_sidecar_and_makes_no_client(self):+ with open(self.src + ".sha256", "w") as f:+ f.write(f"{self.sha} world.mamaps\n")+ env = {k: os.environ.pop(k) for k in ("R2_ENDPOINT", "R2_ACCESS_KEY_ID", "R2_SECRET_ACCESS_KEY") if k in os.environ}+ try:+ rc, out = quiet(up.upload_base, [self.src, "--dry-run", "--part-size-mb", "5", "--concurrency", "4"])+ finally:+ os.environ.update(env)+ self.assertEqual(rc, 0, out)+ self.assertIn("DRY RUN", out)+ self.assertIn(f"remote sha256 {self.sha}", out)+++class Manifest(unittest.TestCase):+ def setUp(self):+ self.tmp = tempfile.TemporaryDirectory()+ self.dir = self.tmp.name+ save(os.path.join(self.dir, "overlay.json"), {"overlay_build_id": "0x1234", "tombstones": []})+ self.segs = {}+ for i in (1, 2):+ p = os.path.join(self.dir, f"overlay-{i:04d}.mamaps")+ body = bytes([i]) * (1000 * i)+ with open(p, "wb") as f:+ f.write(body)+ self.segs[f"overlays/0007/overlay-{i:04d}.mamaps"] = hashlib.sha256(body).hexdigest()+ os.makedirs(os.path.join(self.dir, ".upload-state"))+ self.bucket_file = os.path.join(self.dir, ".upload-state", "manifest.dryrun-bucket.json")+ save(self.bucket_file, {"uploads": {}, "objects": {+ "maps/planet3.mamaps": {"size": 5000, "meta": {"sha256": "ab" * 32}, "etag": '"x"'}}})++ def tearDown(self):+ self.tmp.cleanup()++ def run_manifest(self, *extra):+ return quiet(up.publish_manifest, [self.dir, "--overlay-seq", "7", "--base-build-id", "0xbeef",+ "--base-len", "5000", "--dry-run", *extra])++ def bucket(self):+ return load(self.bucket_file)["objects"]++ def test_segments_upload_with_their_hash_and_the_manifest_lists_them(self):+ rc, out = self.run_manifest("--base-sha256", "ab" * 32)+ self.assertEqual(rc, 0, out)+ objs = self.bucket()+ for key, sha in self.segs.items():+ self.assertEqual(objs[f"maps/{key}"]["meta"]["sha256"], sha)+ self.assertIn("maps/planet3.manifest.json", objs)++ def test_a_rerun_keeps_segments_the_server_already_holds(self):+ self.run_manifest()+ rc, out = self.run_manifest()+ self.assertEqual(rc, 0, out)+ self.assertEqual(out.count("kept s3://"), 2)+ self.assertNotIn("uploading", out)++ def test_a_server_segment_with_another_hash_is_replaced(self):+ self.run_manifest()+ b = load(self.bucket_file)+ b["objects"]["maps/overlays/0007/overlay-0001.mamaps"]["meta"]["sha256"] = "00" * 32+ save(self.bucket_file, b)+ rc, out = self.run_manifest()+ self.assertEqual(rc, 0, out)+ self.assertIn("replacing s3://maps/overlays/0007/overlay-0001.mamaps", out)+ self.assertEqual(self.bucket()["maps/overlays/0007/overlay-0001.mamaps"]["meta"]["sha256"],+ self.segs["overlays/0007/overlay-0001.mamaps"])++ def test_a_base_with_the_wrong_length_or_hash_blocks_the_manifest(self):+ rc, out = quiet(up.publish_manifest, [self.dir, "--overlay-seq", "7", "--base-build-id", "0xbeef",+ "--base-len", "4999", "--dry-run"])+ self.assertEqual(rc, 2, out)+ self.assertIn("BASE MISMATCH", out)+ rc, out = self.run_manifest("--base-sha256", "cd" * 32)+ self.assertEqual(rc, 2, out)+ self.assertNotIn("maps/planet3.manifest.json", self.bucket())+++if __name__ == "__main__":+ unittest.main()
scripts/maps/upload_planet.py
diff --git a/scripts/maps/upload_planet.py b/scripts/maps/upload_planet.pyindex 2865cd1ce4..3c3e87cfee 100644--- a/scripts/maps/upload_planet.py+++ b/scripts/maps/upload_planet.py@@ -1,109 +1,700 @@ """Upload any local file to R2 as planet3.mamaps (bucket: maps). -Usage: python upload_planet.py <local-file>- python upload_planet.py --manifest <overlay-dir> --overlay-seq N --base-build-id HEX-Verifies server-side size matches, then deletes nothing local. Progress-prints every ~1 GB.+Usage: python upload_planet.py <local-file> [options]+ python upload_planet.py --manifest <overlay-dir> --overlay-seq N --base-build-id HEX [options]+ python upload_planet.py --rebase --overlay-seq N --base-build-id HEX --base-len N [--dry-run] Serves https://data.maproj.org/planet3.mamaps, which the apps' basemap URLs point at (library/map urls.rs, maps MapTileCache.kt). +The base upload is a resumable multipart upload with end-to-end hash checks:++* Parts are uploaded concurrently (--part-size-mb / --concurrency, or the+ R2_PART_SIZE_MB / R2_CONCURRENCY env vars). The default part size is+ 64 MiB, raised automatically so the upload never needs more than 9,000+ parts (R2 allows 10,000, all but the last the same size, 5 MiB..5 GiB):+ a ~80 GB planet is ~1,200 parts. Each part carries Content-MD5, so R2+ rejects a part that was corrupted in flight, and its returned ETag is+ checked against the local MD5.+* The upload id and every completed part (number + ETag) are persisted in a+ state file (default <local-file>.upload-state.json) after each part. A rerun+ after a crash or a dropped link lists the parts R2 already holds, keeps every+ one whose ETag matches the local bytes, and uploads only the rest. The state+ is discarded (and the stale upload aborted) if the file, key or part size+ changed; --no-resume forces a fresh upload.+* The SHA-256 of the whole file is computed while the parts upload (one+ sequential reader beside the part workers) and compared with the+ `sha256sum`-format sidecar <local-file>.sha256 that `mamaps_pack+ --sha256-out` writes, when it is present (or --sha256 HEX). A mismatch means+ the local copy is not the file the build produced (e.g. a bad pull from the+ build box): the upload is aborted before CompleteMultipartUpload, so nothing+ becomes visible. The hash is stored as object metadata (x-amz-meta-sha256)+ and checked after completion with a HEAD, together with the size.+* --dry-run exercises all of the above -- reading, MD5/SHA-256, part planning,+ state file, resume -- against an in-process fake bucket persisted next to the+ state file, and makes no network calls at all.+ The --manifest form publishes a patch-overlay update: it uploads each overlay-*.mamaps segment from the overlay dir under overlays/<seq>/, then writes planet3.manifest.json (base pin + segment list + hashes).-The manifest is the only file the client fetches on a check.+The manifest is the only file the client fetches on a check. Segments that+R2 already holds with the same size and x-amz-meta-sha256 are not re-uploaded;+every segment is HEAD-verified (size + server hash vs local hash) before the+manifest is written, and the base the manifest pins is HEAD-checked too+(its size against --base-len, and its stored hash against --base-sha256 when+given). SECURITY: credentials/config come from ENVIRONMENT VARIABLES, never baked into this file (the analysis/ original carried hardcoded keys and must stay gitignored there). Never paste keys on the command line — `export` them instead, ideally from a secret store. -Required env:+Required env (not for --dry-run): R2_ENDPOINT S3 API endpoint, e.g. https://<ACCOUNT_ID>.r2.cloudflarestorage.com R2_ACCESS_KEY_ID R2 access key id R2_SECRET_ACCESS_KEY R2 secret access key Optional env: R2_BUCKET Default: maps R2_CDN Default: https://data.maproj.org (URL prefix written into the manifest)+ R2_PART_SIZE_MB Default: 64 (raised to keep the part count <= 9,000)+ R2_CONCURRENCY Default: 16 parts in flight (each holds one part in memory) """+import argparse+import base64 import hashlib import json import os+import random import sys import threading--import boto3-from boto3.s3.transfer import TransferConfig+import time+import uuid+from concurrent.futures import ThreadPoolExecutor, as_completed BUCKET = os.environ.get("R2_BUCKET", "maps") KEY = "planet3.mamaps" MANIFEST_KEY = "planet3.manifest.json" CDN = os.environ.get("R2_CDN", "https://data.maproj.org") -PART_SIZE = 100 * 1024 * 1024 # 100 MB parts, 10-way concurrency+MIB = 1024 * 1024+DEFAULT_PART_MB = 64+DEFAULT_CONCURRENCY = 16+MIN_PART = 5 * MIB # S3/R2 floor for every part but the last+MAX_PART = 5 * 1024 * MIB # S3/R2 ceiling+MAX_PARTS = 10_000 # S3/R2 hard limit+TARGET_MAX_PARTS = 9_000 # what auto-sizing aims under, leaving slack+PART_ATTEMPTS = 5 # per-part attempts on top of botocore's own retries+RETRY_BASE_S = 2.0 # first backoff between part attempts (doubles, capped at 60 s)+HASH_CHUNK = 8 * MIB+META_SHA = "sha256" # stored as x-amz-meta-sha256 MANIFEST_VERSION = 1 MIN_CLIENT_COMPAT = 1 -def main() -> int:- if len(sys.argv) >= 2 and sys.argv[1] == "--manifest":- return publish_manifest(sys.argv[2:])- if len(sys.argv) >= 2 and sys.argv[1] == "--rebase":- return publish_rebase(sys.argv[2:])- if len(sys.argv) != 2:- print(f"usage: {os.path.basename(sys.argv[0])} <local-file>", flush=True)- print(f" or: {os.path.basename(sys.argv[0])} --manifest <overlay-dir> --overlay-seq N --base-build-id HEX [--base-len N]", flush=True)- print(f" or: {os.path.basename(sys.argv[0])} --rebase --overlay-seq N --base-build-id HEX --base-len N", flush=True)- return 2+def log(msg: str) -> None:+ print(msg, flush=True)+++# --------------------------------------------------------------------------+# Configuration+# --------------------------------------------------------------------------++def env_int(name: str, default: int) -> int:+ raw = os.environ.get(name)+ if raw is None or raw.strip() == "":+ return default+ try:+ v = int(raw)+ except ValueError:+ raise SystemExit(f"{name}={raw!r} is not an integer")+ if v <= 0:+ raise SystemExit(f"{name} must be positive, not {v}")+ return v+++def plan_part_size(size: int, requested_mb: int) -> int:+ """Part size in bytes: the request, raised so `size` needs <= TARGET_MAX_PARTS+ parts, rounded up to a whole MiB and clamped to the S3/R2 limits."""+ part = max(requested_mb * MIB, MIN_PART)+ need = -(-size // TARGET_MAX_PARTS) # ceil+ if part < need:+ part = -(-need // MIB) * MIB+ if part > MAX_PART:+ raise SystemExit(f"{size} bytes would need parts over 5 GiB; too large for one object")+ if -(-size // part) > MAX_PARTS:+ raise SystemExit(f"{size} bytes in {part}-byte parts is over {MAX_PARTS} parts")+ return part+++def add_transfer_args(ap: argparse.ArgumentParser) -> None:+ ap.add_argument("--part-size-mb", type=int, default=None,+ help=f"multipart part size in MiB (env R2_PART_SIZE_MB, default {DEFAULT_PART_MB}; "+ f"raised automatically to stay under {TARGET_MAX_PARTS} parts)")+ ap.add_argument("--concurrency", type=int, default=None,+ help=f"parts in flight (env R2_CONCURRENCY, default {DEFAULT_CONCURRENCY})")+ ap.add_argument("--dry-run", action="store_true",+ help="do everything except network calls, against a local fake bucket")+++def transfer_settings(args) -> tuple:+ part_mb = args.part_size_mb if args.part_size_mb is not None else env_int("R2_PART_SIZE_MB", DEFAULT_PART_MB)+ conc = args.concurrency if args.concurrency is not None else env_int("R2_CONCURRENCY", DEFAULT_CONCURRENCY)+ if part_mb <= 0 or conc <= 0:+ raise SystemExit("--part-size-mb and --concurrency must be positive")+ return part_mb, conc+++# --------------------------------------------------------------------------+# Clients+# --------------------------------------------------------------------------++def make_client(concurrency: int):+ """A real boto3 client for R2. boto3 is imported here so --dry-run and the+ unit tests run without it.""" missing = [v for v in ("R2_ENDPOINT", "R2_ACCESS_KEY_ID", "R2_SECRET_ACCESS_KEY") if not os.environ.get(v)] if missing:- print(f"missing env: {', '.join(missing)} (see module docstring)", flush=True)- return 2- src = sys.argv[1]- if not os.path.isfile(src):- print(f"no such file: {src}", flush=True)- return 2- size = os.path.getsize(src)- print(f"uploading {src} ({size / 1e9:.1f} GB) to s3://{BUCKET}/{KEY}", flush=True)-- s3 = boto3.client(+ raise SystemExit(f"missing env: {', '.join(missing)} (see module docstring)")+ import boto3+ from botocore.config import Config+ cfg = dict(+ # One pooled connection per part in flight, plus a few for HEAD/list.+ max_pool_connections=concurrency + 4,+ retries={"max_attempts": 10, "mode": "adaptive"},+ connect_timeout=30,+ read_timeout=300,+ )+ # botocore >= 1.36 adds CRC checksums to every upload by default; R2 does+ # not accept all of them. Content-MD5 (set per part below) is the integrity+ # check here, so only compute checksums where an operation requires them.+ try:+ client_cfg = Config(request_checksum_calculation="when_required",+ response_checksum_validation="when_required", **cfg)+ except TypeError:+ client_cfg = Config(**cfg)+ return boto3.client( "s3", endpoint_url=os.environ["R2_ENDPOINT"], aws_access_key_id=os.environ["R2_ACCESS_KEY_ID"], aws_secret_access_key=os.environ["R2_SECRET_ACCESS_KEY"], region_name="auto",+ config=client_cfg, )- config = TransferConfig(- multipart_threshold=PART_SIZE,- multipart_chunksize=PART_SIZE,- max_concurrency=10,- use_threads=True,- ) ++class ClientError(Exception):+ """Raised by FakeS3 with the same `.response["Error"]["Code"]` shape botocore uses."""++ def __init__(self, code: str, msg: str = ""):+ super().__init__(f"{code}: {msg}")+ self.response = {"Error": {"Code": code, "Message": msg}}+++def error_code(e: Exception) -> str:+ return getattr(e, "response", {}).get("Error", {}).get("Code", "")+++class FakeS3:+ """The subset of the S3 API this script uses, in memory, optionally persisted+ to a JSON file so a dry run can be interrupted and resumed like the real+ thing. Object bodies are never stored -- only sizes, MD5s and metadata --+ so a dry run of an 80 GB file costs kilobytes. It validates what R2 would:+ Content-MD5 against the body, part sizes, part order and ETags on complete.++ `fail_after_parts` (tests / UPLOAD_PLANET_FAIL_AFTER_PARTS) raises on the+ Nth successful UploadPart to simulate a dropped link."""++ def __init__(self, persist_path: str = None, fail_after_parts: int = None):+ self.path = persist_path+ self.lock = threading.Lock()+ self.uploads = {} # upload_id -> {"bucket","key","meta","parts":{n: [size, etag]}}+ self.objects = {} # "bucket/key" -> {"size", "meta", "etag"}+ self.calls = [] # (op, detail) log, for tests+ self.fail_after = fail_after_parts+ self.part_ok = 0+ if self.path and os.path.exists(self.path):+ with open(self.path) as f:+ st = json.load(f)+ self.uploads = {k: {**v, "parts": {int(n): p for n, p in v["parts"].items()}}+ for k, v in st.get("uploads", {}).items()}+ self.objects = st.get("objects", {})++ def _save(self):+ if not self.path:+ return+ tmp = self.path + ".tmp"+ with open(tmp, "w") as f:+ json.dump({"uploads": self.uploads, "objects": self.objects}, f)+ os.replace(tmp, self.path)++ def create_multipart_upload(self, Bucket, Key, Metadata=None, **_):+ with self.lock:+ uid = "dryrun-" + uuid.uuid4().hex+ self.uploads[uid] = {"bucket": Bucket, "key": Key, "meta": dict(Metadata or {}), "parts": {}}+ self.calls.append(("create", Key))+ self._save()+ return {"UploadId": uid}++ def upload_part(self, Bucket, Key, UploadId, PartNumber, Body, ContentMD5=None, **_):+ body = bytes(Body)+ md5 = hashlib.md5(body)+ if ContentMD5 is not None and base64.b64encode(md5.digest()).decode() != ContentMD5:+ raise ClientError("BadDigest", f"part {PartNumber}")+ with self.lock:+ if self.fail_after is not None and self.part_ok >= self.fail_after:+ raise ConnectionError("simulated dropped link")+ up = self.uploads.get(UploadId)+ if up is None:+ raise ClientError("NoSuchUpload", UploadId)+ etag = f'"{md5.hexdigest()}"'+ up["parts"][PartNumber] = [len(body), etag]+ self.part_ok += 1+ self.calls.append(("part", PartNumber))+ self._save()+ return {"ETag": etag}++ def list_parts(self, Bucket, Key, UploadId, PartNumberMarker=0, MaxParts=1000, **_):+ with self.lock:+ up = self.uploads.get(UploadId)+ if up is None:+ raise ClientError("NoSuchUpload", UploadId)+ nums = sorted(n for n in up["parts"] if n > PartNumberMarker)+ page = nums[:MaxParts]+ self.calls.append(("list", PartNumberMarker))+ return {+ "Parts": [{"PartNumber": n, "Size": up["parts"][n][0], "ETag": up["parts"][n][1]} for n in page],+ "IsTruncated": len(nums) > MaxParts,+ "NextPartNumberMarker": page[-1] if page else PartNumberMarker,+ }++ def complete_multipart_upload(self, Bucket, Key, UploadId, MultipartUpload):+ with self.lock:+ up = self.uploads.get(UploadId)+ if up is None:+ raise ClientError("NoSuchUpload", UploadId)+ parts = MultipartUpload["Parts"]+ nums = [p["PartNumber"] for p in parts]+ if nums != sorted(nums) or nums != list(range(1, len(nums) + 1)):+ raise ClientError("InvalidPartOrder", str(nums[:5]))+ sizes = []+ for p in parts:+ have = up["parts"].get(p["PartNumber"])+ if have is None or have[1] != p["ETag"]:+ raise ClientError("InvalidPart", str(p["PartNumber"]))+ sizes.append(have[0])+ if any(s < MIN_PART for s in sizes[:-1]) or len(set(sizes[:-1])) > 1:+ raise ClientError("EntityTooSmall", "parts must match and be >= 5 MiB")+ self.objects[f"{Bucket}/{Key}"] = {"size": sum(sizes), "meta": up["meta"],+ "etag": f'"multipart-{len(parts)}"'}+ del self.uploads[UploadId]+ self.calls.append(("complete", Key))+ self._save()+ return {"ETag": self.objects[f"{Bucket}/{Key}"]["etag"]}++ def abort_multipart_upload(self, Bucket, Key, UploadId):+ with self.lock:+ self.uploads.pop(UploadId, None)+ self.calls.append(("abort", Key))+ self._save()+ return {}++ def put_object(self, Bucket, Key, Body, Metadata=None, ContentMD5=None, **_):+ body = bytes(Body)+ md5 = hashlib.md5(body)+ if ContentMD5 is not None and base64.b64encode(md5.digest()).decode() != ContentMD5:+ raise ClientError("BadDigest", Key)+ with self.lock:+ self.objects[f"{Bucket}/{Key}"] = {"size": len(body), "meta": dict(Metadata or {}),+ "etag": f'"{md5.hexdigest()}"'}+ self.calls.append(("put", Key))+ self._save()+ return {"ETag": self.objects[f"{Bucket}/{Key}"]["etag"]}++ def head_object(self, Bucket, Key):+ with self.lock:+ o = self.objects.get(f"{Bucket}/{Key}")+ self.calls.append(("head", Key))+ if o is None:+ raise ClientError("404", Key)+ return {"ContentLength": o["size"], "Metadata": dict(o["meta"]), "ETag": o["etag"]}+++def dry_run_client(state_path: str):+ fail = os.environ.get("UPLOAD_PLANET_FAIL_AFTER_PARTS")+ return FakeS3(persist_path=state_path + ".dryrun-bucket.json",+ fail_after_parts=int(fail) if fail else None)+++# --------------------------------------------------------------------------+# Hashes+# --------------------------------------------------------------------------++def sha256_file(path: str) -> str:+ h = hashlib.sha256()+ with open(path, "rb") as f:+ while True:+ chunk = f.read(HASH_CHUNK)+ if not chunk:+ break+ h.update(chunk)+ return h.hexdigest()+++def read_sidecar(path: str, name: str) -> str:+ """The hash from a `sha256sum`-format line (`<hex> <name>`), or None if the+ file is absent. A malformed line or one naming another file is an error."""+ if not os.path.exists(path):+ return None+ with open(path) as f:+ lines = [l for l in f.read().splitlines() if l.strip()]+ if len(lines) != 1:+ raise SystemExit(f"{path}: expected one `sha256sum` line, found {len(lines)}")+ parts = lines[0].split()+ digest = parts[0].lower()+ if len(digest) != 64 or any(c not in "0123456789abcdef" for c in digest):+ raise SystemExit(f"{path}: {parts[0]!r} is not a SHA-256")+ if len(parts) > 1 and parts[-1].lstrip("*") != name:+ raise SystemExit(f"{path} names {parts[-1]!r}, not {name!r}")+ return digest+++def server_sha(head: dict) -> str:+ meta = {k.lower(): v for k, v in (head.get("Metadata") or {}).items()}+ v = meta.get(META_SHA)+ return v.lower() if v else None+++def head_or_none(s3, bucket: str, key: str):+ try:+ return s3.head_object(Bucket=bucket, Key=key)+ except Exception as e:+ if error_code(e) in ("404", "NoSuchKey", "NotFound"):+ return None+ raise+++# --------------------------------------------------------------------------+# Resumable multipart upload+# --------------------------------------------------------------------------++class State:+ """The resume record: which upload id, which file, and which parts are done.+ Rewritten atomically after every part, so a crash loses at most the parts in+ flight (and those are recovered from ListParts on the next run)."""++ def __init__(self, path: str):+ self.path = path+ self.lock = threading.Lock()+ self.data = None++ def load(self):+ if not os.path.exists(self.path):+ return None+ try:+ with open(self.path) as f:+ self.data = json.load(f)+ except (OSError, ValueError) as e:+ log(f" ignoring unreadable state file {self.path}: {e}")+ self.data = None+ return self.data++ def start(self, **fields):+ self.data = {**fields, "parts": {}}+ self.save()++ def part_done(self, n: int, etag: str):+ with self.lock:+ self.data["parts"][str(n)] = etag+ self._write()++ def save(self):+ with self.lock:+ self._write()++ def _write(self):+ tmp = self.path + ".tmp"+ with open(tmp, "w") as f:+ json.dump(self.data, f)+ os.replace(tmp, self.path)++ def remove(self):+ for p in (self.path, self.path + ".tmp"):+ try:+ os.remove(p)+ except FileNotFoundError:+ pass+++def pread(fd: int, want: int, off: int) -> bytes:+ if hasattr(os, "pread"):+ return os.pread(fd, want, off)+ # No positional read (Windows): a private handle per call, so threads never+ # share a file cursor.+ with open(_fd_paths[fd], "rb") as f:+ f.seek(off)+ return f.read(want)+++_fd_paths = {}+++def open_ro(path: str) -> int:+ fd = os.open(path, os.O_RDONLY | getattr(os, "O_BINARY", 0))+ _fd_paths[fd] = path+ return fd+++def read_part(fd: int, n: int, part_size: int, size: int) -> bytes:+ off = (n - 1) * part_size+ want = min(part_size, size - off)+ buf = pread(fd, want, off)+ if len(buf) != want:+ raise IOError(f"short read of part {n}: {len(buf)} of {want} bytes")+ return buf+++def list_all_parts(s3, bucket: str, key: str, upload_id: str) -> dict:+ out, marker = {}, 0+ while True:+ r = s3.list_parts(Bucket=bucket, Key=key, UploadId=upload_id, PartNumberMarker=marker)+ for p in r.get("Parts", []):+ out[int(p["PartNumber"])] = (int(p["Size"]), p["ETag"])+ if not r.get("IsTruncated"):+ return out+ marker = r["NextPartNumberMarker"]+++def multipart_upload(s3, src: str, bucket: str, key: str, part_mb: int, concurrency: int,+ state_path: str, expected_sha: str = None, resume: bool = True,+ verify_local: bool = True) -> int:+ """Upload `src` to bucket/key; returns a process exit code (0 = verified)."""+ size = os.path.getsize(src)+ mtime_ns = os.stat(src).st_mtime_ns+ part_size = plan_part_size(size, part_mb)+ n_parts = max(1, -(-size // part_size))+ log(f"uploading {src} ({size / 1e9:.2f} GB) to s3://{bucket}/{key}: "+ f"{n_parts} part(s) of {part_size // MIB} MiB, {concurrency} in flight")++ state = State(state_path)+ old = state.load() if resume else None+ identity = {"bucket": bucket, "key": key, "size": size, "mtime_ns": mtime_ns, "part_size": part_size}+ done = {} # part number -> etag already on the server+ upload_id = None+ if old and all(old.get(k) == v for k, v in identity.items()) and old.get("upload_id"):+ upload_id = old["upload_id"]+ try:+ server = list_all_parts(s3, bucket, key, upload_id)+ except Exception as e:+ if error_code(e) != "NoSuchUpload":+ raise+ # Gone: either completed (a crash after CompleteMultipartUpload) or+ # expired/aborted. Completed with the right bytes means nothing to do.+ head = head_or_none(s3, bucket, key)+ want = expected_sha or old.get("sha256")+ if head is not None and int(head["ContentLength"]) == size and want and server_sha(head) == want:+ log(f" previous upload {upload_id} already completed: s3://{bucket}/{key} has "+ f"{size} bytes, sha256 {want}")+ state.remove()+ return 0+ log(f" previous upload {upload_id} no longer exists on the server; starting over")+ upload_id = None+ else:+ # Keep a server part only if its size is right and its ETag is the MD5+ # of the local bytes. Parts the state file recorded are trusted by ETag;+ # parts it missed (crash between upload and save) are re-hashed.+ recorded = old.get("parts", {})+ fd = open_ro(src)+ try:+ for n, (psize, etag) in sorted(server.items()):+ if n > n_parts:+ continue+ want = min(part_size, size - (n - 1) * part_size)+ if psize != want:+ continue+ if recorded.get(str(n)) == etag:+ done[n] = etag+ elif f'"{hashlib.md5(read_part(fd, n, part_size, size)).hexdigest()}"' == etag:+ done[n] = etag+ finally:+ os.close(fd)+ if old.get("sha256") and expected_sha and old["sha256"] != expected_sha:+ log(" expected hash changed since the interrupted run; aborting it and starting over")+ try:+ s3.abort_multipart_upload(Bucket=bucket, Key=key, UploadId=upload_id)+ except Exception as e:+ log(f" (abort failed: {e}; R2 expires it on its own)")+ upload_id, done = None, {}+ else:+ expected_sha = expected_sha or old.get("sha256")+ log(f" resuming upload {upload_id}: {len(done)}/{n_parts} part(s) already on the server")+ elif old and old.get("upload_id"):+ log(f" state file {state_path} is for a different file/key/part size; aborting that upload")+ try:+ s3.abort_multipart_upload(Bucket=old["bucket"], Key=old["key"], UploadId=old["upload_id"])+ except Exception as e:+ log(f" (abort of the stale upload failed: {e}; R2 expires it on its own)")++ if upload_id is None:+ if expected_sha is None and verify_local:+ # Without a sidecar the hash has to be known up front to ride the+ # create call as metadata.+ log(" no .sha256 sidecar: hashing the file first")+ t0 = time.time()+ expected_sha = sha256_file(src)+ log(f" sha256 {expected_sha} ({time.time() - t0:.0f} s)")+ meta = {META_SHA: expected_sha} if expected_sha else {}+ upload_id = s3.create_multipart_upload(Bucket=bucket, Key=key, Metadata=meta)["UploadId"]+ state.start(**identity, upload_id=upload_id, sha256=expected_sha)+ log(f" started multipart upload {upload_id}")+ else:+ state.data = {**old, "parts": {str(n): e for n, e in done.items()}, "sha256": expected_sha}+ state.save()++ # The whole-file SHA-256, read sequentially beside the part workers+ # (hashlib releases the GIL on large updates).+ hash_result = {}++ def hash_whole():+ try:+ hash_result["sha"] = sha256_file(src)+ except Exception as e: # reported at the join+ hash_result["err"] = e++ hasher = threading.Thread(target=hash_whole, daemon=True) if verify_local else None+ if hasher:+ hasher.start()++ todo = [n for n in range(1, n_parts + 1) if n not in done] lock = threading.Lock()- done = [0]- last_mark = [0]+ already = sum(min(part_size, size - (n - 1) * part_size) for n in done)+ sent = [already]+ last_mark = [already]+ t0 = time.time()+ fd = open_ro(src) - def progress(n: int) -> None:+ def send(n: int) -> str:+ body = read_part(fd, n, part_size, size)+ md5 = hashlib.md5(body)+ b64 = base64.b64encode(md5.digest()).decode()+ want_etag = f'"{md5.hexdigest()}"'+ delay = RETRY_BASE_S+ for attempt in range(1, PART_ATTEMPTS + 1):+ try:+ etag = s3.upload_part(Bucket=bucket, Key=key, UploadId=upload_id, PartNumber=n,+ Body=body, ContentMD5=b64)["ETag"]+ if etag.lower() != want_etag:+ raise IOError(f"part {n}: server ETag {etag} is not the local MD5 {want_etag}")+ break+ except Exception as e:+ if attempt == PART_ATTEMPTS or error_code(e) == "NoSuchUpload":+ raise+ log(f" part {n} attempt {attempt} failed ({e}); retrying in {delay:.0f} s")+ time.sleep(delay + random.random() * min(delay, 1.0))+ delay = min(delay * 2, 60)+ state.part_done(n, etag) with lock:- done[0] += n- gb = done[0] / 1e9- if gb - last_mark[0] >= 1.0 or done[0] >= size:- last_mark[0] = gb- pct = 100.0 * done[0] / size- print(f" {gb:.1f}/{size / 1e9:.1f} GB ({pct:.1f}%)", flush=True)+ sent[0] += len(body)+ if sent[0] - last_mark[0] >= 1_000_000_000 or sent[0] >= size:+ last_mark[0] = sent[0]+ rate = (sent[0] - already) / max(time.time() - t0, 1e-6)+ eta = (size - sent[0]) / rate if rate > 0 else 0+ log(f" {sent[0] / 1e9:.1f}/{size / 1e9:.1f} GB ({100.0 * sent[0] / size:.1f}%) "+ f"{rate / 1e6:.0f} MB/s, ETA {eta / 60:.1f} min")+ return etag + failed = None try:- s3.upload_file(src, BUCKET, KEY, Config=config, Callback=progress)- except Exception as e:- print(f"UPLOAD FAILED: {e}", flush=True)+ with ThreadPoolExecutor(max_workers=concurrency) as pool:+ futures = {pool.submit(send, n): n for n in todo}+ for fut in as_completed(futures):+ n = futures[fut]+ try:+ done[n] = fut.result()+ except Exception as e:+ failed = failed or (n, e)+ for other in futures:+ other.cancel()+ finally:+ os.close(fd)+ if failed:+ log(f"UPLOAD FAILED at part {failed[0]}: {failed[1]}")+ log(f" {len(done)}/{n_parts} part(s) are on the server; rerun the same command to resume "+ f"(state: {state_path})") return 1 - remote = s3.head_object(Bucket=BUCKET, Key=KEY)["ContentLength"]- print(f"done. remote size {remote} bytes, local {size} bytes", flush=True)- return 0 if remote == size else 2+ if hasher:+ hasher.join()+ if "err" in hash_result:+ log(f"UPLOAD FAILED: could not hash {src}: {hash_result['err']}")+ return 1+ local_sha = hash_result["sha"]+ if expected_sha and local_sha != expected_sha:+ log(f"HASH MISMATCH: {src} hashes to {local_sha}, but the build recorded {expected_sha}.")+ log(" The local copy is not the file the build produced; aborting the upload so nothing is published.")+ try:+ s3.abort_multipart_upload(Bucket=bucket, Key=key, UploadId=upload_id)+ finally:+ state.remove()+ return 3+ expected_sha = expected_sha or local_sha+ log(f" local sha256 {local_sha} verified")++ parts = [{"PartNumber": n, "ETag": done[n]} for n in range(1, n_parts + 1)]+ s3.complete_multipart_upload(Bucket=bucket, Key=key, UploadId=upload_id,+ MultipartUpload={"Parts": parts})+ # The upload id is consumed either way; the HEAD below decides the exit code.+ state.remove()+ head = s3.head_object(Bucket=bucket, Key=key)+ remote, rsha = int(head["ContentLength"]), server_sha(head)+ log(f"done. remote size {remote} bytes, local {size} bytes; remote sha256 {rsha}")+ if remote != size:+ log("SIZE MISMATCH after upload")+ return 2+ if expected_sha and rsha != expected_sha:+ log(f"HASH MISMATCH after upload: server records {rsha}, expected {expected_sha}")+ return 2+ return 0+++def put_small(s3, src: str, bucket: str, key: str, sha: str) -> None:+ with open(src, "rb") as f:+ body = f.read()+ md5 = base64.b64encode(hashlib.md5(body).digest()).decode()+ s3.put_object(Bucket=bucket, Key=key, Body=body, ContentMD5=md5, Metadata={META_SHA: sha})+++# --------------------------------------------------------------------------+# Entry points+# --------------------------------------------------------------------------++def upload_base(argv: list) -> int:+ ap = argparse.ArgumentParser(prog="upload_planet.py", description="Upload a planet archive to R2.")+ ap.add_argument("src", help="local file, normally world.mamaps")+ ap.add_argument("--key", default=KEY, help=f"object key (default {KEY})")+ ap.add_argument("--sha256", default=None,+ help="expected SHA-256 (default: read <src>.sha256 if present)")+ ap.add_argument("--state-file", default=None,+ help="resume record (default <src>.upload-state.json)")+ ap.add_argument("--no-resume", action="store_true", help="ignore any saved state and start over")+ ap.add_argument("--no-verify-local", action="store_true",+ help="skip the whole-file SHA-256 (the server check then uses --sha256/sidecar only)")+ add_transfer_args(ap)+ args = ap.parse_args(argv)+ if not os.path.isfile(args.src):+ log(f"no such file: {args.src}")+ return 2+ part_mb, conc = transfer_settings(args)+ expected = args.sha256.lower() if args.sha256 else read_sidecar(args.src + ".sha256", os.path.basename(args.src))+ if expected:+ log(f"expected sha256 {expected}" + ("" if args.sha256 else f" (from {args.src}.sha256)"))+ state_path = args.state_file or args.src + ".upload-state.json"+ s3 = dry_run_client(state_path) if args.dry_run else make_client(conc)+ if args.dry_run:+ log("DRY RUN: no network calls; fake bucket at " + state_path + ".dryrun-bucket.json")+ return multipart_upload(s3, args.src, BUCKET, args.key, part_mb, conc, state_path,+ expected_sha=expected, resume=not args.no_resume,+ verify_local=not args.no_verify_local) def publish_manifest(argv: list) -> int:@@ -115,41 +706,56 @@ def publish_manifest(argv: list) -> int: to overlays/<seq>/overlay-SSSS.mamaps; the manifest lists their CDN URLs + lengths + SHA-256. Sections (chunked sidecars) ride the same manifest when present as <kind>.section files (not yet emitted).++ Server-side vs local: a segment R2 already holds with the same size and+ x-amz-meta-sha256 is kept, not re-uploaded; every segment is HEAD-checked+ (size + stored hash == local hash) before the manifest is written; and the+ base the manifest pins is HEAD-checked against --base-len / --base-sha256. """- import argparse- ap = argparse.ArgumentParser()+ ap = argparse.ArgumentParser(prog="upload_planet.py --manifest") ap.add_argument("overlay_dir") ap.add_argument("--overlay-seq", type=int, required=True) ap.add_argument("--base-build-id", required=True, help="hex base build_id the overlay applies to, e.g. 0x0355...") ap.add_argument("--base-len", type=int, default=None, help="base length in bytes (else read from overlay.json sidecar)")+ ap.add_argument("--base-sha256", default=None,+ help="expected SHA-256 of the base on R2 (checked against its x-amz-meta-sha256)")+ add_transfer_args(ap) args = ap.parse_args(argv)- missing = [v for v in ("R2_ENDPOINT", "R2_ACCESS_KEY_ID", "R2_SECRET_ACCESS_KEY")- if not os.environ.get(v)]- if missing:- print(f"missing env: {', '.join(missing)} (see module docstring)", flush=True)- return 2+ part_mb, conc = transfer_settings(args) overlay_dir = args.overlay_dir descriptor_path = os.path.join(overlay_dir, "overlay.json") try:- descriptor = json.load(open(descriptor_path))+ with open(descriptor_path) as f:+ descriptor = json.load(f) except (OSError, ValueError) as e:- print(f"cannot read {descriptor_path}: {e} (run mamaps_overlay first)", flush=True)+ log(f"cannot read {descriptor_path}: {e} (run mamaps_overlay first)") return 2- s3 = boto3.client(- "s3",- endpoint_url=os.environ["R2_ENDPOINT"],- aws_access_key_id=os.environ["R2_ACCESS_KEY_ID"],- aws_secret_access_key=os.environ["R2_SECRET_ACCESS_KEY"],- region_name="auto",- )- config = TransferConfig(- multipart_threshold=PART_SIZE,- multipart_chunksize=PART_SIZE,- max_concurrency=10,- use_threads=True,- )+ state_dir = os.path.join(overlay_dir, ".upload-state")+ if args.dry_run:+ os.makedirs(state_dir, exist_ok=True)+ s3 = dry_run_client(os.path.join(state_dir, "manifest"))+ log("DRY RUN: no network calls")+ else:+ s3 = make_client(conc)++ base_len = args.base_len if args.base_len is not None else descriptor.get("base_len")+ base_head = head_or_none(s3, BUCKET, KEY)+ if base_head is None:+ log(f"WARN: s3://{BUCKET}/{KEY} does not exist; the manifest would pin a missing base")+ if not args.dry_run:+ return 2+ else:+ blen, bsha = int(base_head["ContentLength"]), server_sha(base_head)+ log(f"base on server: {blen} bytes, sha256 {bsha}")+ if base_len and blen != int(base_len):+ log(f"BASE MISMATCH: server base is {blen} bytes, the overlay expects {base_len}")+ return 2+ if args.base_sha256 and bsha != args.base_sha256.lower():+ log(f"BASE MISMATCH: server base sha256 {bsha}, expected {args.base_sha256.lower()}")+ return 2+ segments = [] for name in sorted(os.listdir(overlay_dir)): if not (name.startswith("overlay-") and name.endswith(".mamaps")):@@ -157,17 +763,33 @@ def publish_manifest(argv: list) -> int: local = os.path.join(overlay_dir, name) key = f"overlays/{args.overlay_seq:04d}/{name}" size = os.path.getsize(local)- print(f"uploading {local} ({size / 1e6:.1f} MB) to s3://{BUCKET}/{key}", flush=True)- try:- s3.upload_file(local, BUCKET, key, Config=config)- except Exception as e:- print(f"UPLOAD FAILED: {e}", flush=True)- return 1- remote = s3.head_object(Bucket=BUCKET, Key=key)["ContentLength"]- if remote != size:- print(f"size mismatch for {key}: remote {remote} vs local {size}", flush=True)- return 2 digest = sha256_file(local)+ head = head_or_none(s3, BUCKET, key)+ if head is not None and int(head["ContentLength"]) == size and server_sha(head) == digest:+ log(f"kept s3://{BUCKET}/{key}: server already holds it (size + sha256 match)")+ else:+ if head is not None:+ log(f"replacing s3://{BUCKET}/{key}: server has {head['ContentLength']} bytes, "+ f"sha256 {server_sha(head)}; local {size} bytes, {digest}")+ try:+ if size <= plan_part_size(size, part_mb):+ log(f"uploading {local} ({size / 1e6:.1f} MB) to s3://{BUCKET}/{key}")+ put_small(s3, local, BUCKET, key, digest)+ else:+ rc = multipart_upload(s3, local, BUCKET, key, part_mb, conc,+ os.path.join(state_dir, name + ".json") if args.dry_run+ else local + ".upload-state.json",+ expected_sha=digest)+ if rc != 0:+ return rc+ except Exception as e:+ log(f"UPLOAD FAILED: {e}")+ return 1+ head = s3.head_object(Bucket=BUCKET, Key=key)+ remote, rsha = int(head["ContentLength"]), server_sha(head)+ if remote != size or rsha != digest:+ log(f"MISMATCH for {key}: server {remote} bytes / sha256 {rsha}; local {size} / {digest}")+ return 2 seq = int(name[len("overlay-"):len("overlay-") + 4]) segments.append({ "url": f"{CDN}/{key}",@@ -176,7 +798,7 @@ def publish_manifest(argv: list) -> int: "seq": seq, }) if not segments:- print(f"no overlay-*.mamaps in {overlay_dir}", flush=True)+ log(f"no overlay-*.mamaps in {overlay_dir}") return 2 tombstones = descriptor.get("tombstones", []) manifest = {@@ -196,23 +818,11 @@ def publish_manifest(argv: list) -> int: body = json.dumps(manifest, indent=1).encode() s3.put_object(Bucket=BUCKET, Key=MANIFEST_KEY, Body=body, ContentType="application/json")- print(f"wrote s3://{BUCKET}/{MANIFEST_KEY} "- f"({len(segments)} segments, overlay_build_id={manifest['overlay_build_id']})",- flush=True)+ log(f"wrote s3://{BUCKET}/{MANIFEST_KEY} "+ f"({len(segments)} segments, overlay_build_id={manifest['overlay_build_id']})") return 0 -def sha256_file(path: str) -> str:- h = hashlib.sha256()- with open(path, "rb") as f:- while True:- chunk = f.read(1 << 20)- if not chunk:- break- h.update(chunk)- return h.hexdigest()-- def publish_rebase(argv: list) -> int: """Publish a consolidation: a fresh full base becomes the world, and the overlay stack resets to empty.@@ -224,27 +834,16 @@ def publish_rebase(argv: list) -> int: and drop their old segments. No client-side rewrite pass exists by design (the no-2x guarantee stays trivially true). - Args: --overlay-seq N --base-build-id HEX --base-len N+ Args: --overlay-seq N --base-build-id HEX --base-len N [--dry-run] (N continues the sequence; segments/tombstones/sections are empty.) """- import argparse- ap = argparse.ArgumentParser()+ ap = argparse.ArgumentParser(prog="upload_planet.py --rebase") ap.add_argument("--overlay-seq", type=int, required=True) ap.add_argument("--base-build-id", required=True) ap.add_argument("--base-len", type=int, required=True)+ ap.add_argument("--dry-run", action="store_true") args = ap.parse_args(argv)- missing = [v for v in ("R2_ENDPOINT", "R2_ACCESS_KEY_ID", "R2_SECRET_ACCESS_KEY")- if not os.environ.get(v)]- if missing:- print(f"missing env: {', '.join(missing)} (see module docstring)", flush=True)- return 2- s3 = boto3.client(- "s3",- endpoint_url=os.environ["R2_ENDPOINT"],- aws_access_key_id=os.environ["R2_ACCESS_KEY_ID"],- aws_secret_access_key=os.environ["R2_SECRET_ACCESS_KEY"],- region_name="auto",- )+ s3 = FakeS3() if args.dry_run else make_client(DEFAULT_CONCURRENCY) manifest = { "version": MANIFEST_VERSION, "base": {@@ -262,10 +861,26 @@ def publish_rebase(argv: list) -> int: body = json.dumps(manifest, indent=1).encode() s3.put_object(Bucket=BUCKET, Key=MANIFEST_KEY, Body=body, ContentType="application/json")- print(f"wrote s3://{BUCKET}/{MANIFEST_KEY} rebase seq={args.overlay_seq} "- f"(empty overlay; clients refresh the base once)", flush=True)+ log(f"{'DRY RUN: would write' if args.dry_run else 'wrote'} s3://{BUCKET}/{MANIFEST_KEY} "+ f"rebase seq={args.overlay_seq} (empty overlay; clients refresh the base once)") return 0 +def main() -> int:+ if len(sys.argv) >= 2 and sys.argv[1] == "--manifest":+ return publish_manifest(sys.argv[2:])+ if len(sys.argv) >= 2 and sys.argv[1] == "--rebase":+ return publish_rebase(sys.argv[2:])+ if len(sys.argv) < 2:+ log(f"usage: {os.path.basename(sys.argv[0])} <local-file> [--dry-run] [--part-size-mb N] "+ f"[--concurrency N] [--sha256 HEX] [--state-file PATH] [--no-resume]")+ log(f" or: {os.path.basename(sys.argv[0])} --manifest <overlay-dir> --overlay-seq N "+ f"--base-build-id HEX [--base-len N] [--base-sha256 HEX] [--dry-run]")+ log(f" or: {os.path.basename(sys.argv[0])} --rebase --overlay-seq N --base-build-id HEX "+ f"--base-len N [--dry-run]")+ return 2+ return upload_base(sys.argv[1:])++ if __name__ == "__main__": sys.exit(main())