maps: fan out the layer builds and the GTFS unzip loop
- Commit
3460045287cc936a80ec46601830e4863a5fe180- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-26 16:57 UTC (3 months ago)
- Parents
555dc9d
Diff
scripts/maps/build_all.sh
diff --git a/scripts/maps/build_all.sh b/scripts/maps/build_all.shindex affed97f06..259431c370 100644--- a/scripts/maps/build_all.sh+++ b/scripts/maps/build_all.sh@@ -78,6 +78,13 @@ set -euo pipefail # --skip-graph --skip-pois --skip-transit --skip-tiles # --only STAGE run exactly one of graph|pois|transit|tiles # --force ignore stamps and redo every requested stage+# --jobs N how many layer builds (tiles) and feed unzips (transit) run+# at once. Default 3. Peak memory is roughly N x the largest+# concurrent job, so raise it when the machine's headroom is+# known rather than by default.+# --overlap-graph run the graph stage alongside pois/transit/tiles instead of+# before them. They share nothing, but the graph is the+# hungriest stage in the build, so this is opt-in. # --dry-run print what each stage would run; change nothing # Graph build-time memory (forwarded to road_graph; none change the on-disk # contract, and --rounds does not change the output at all)@@ -89,9 +96,10 @@ set -euo pipefail # --rounds N write the pack in N source-partitioned rounds instead of # one, so the write buffer is edges/N x 48 B rather than # all of it. Planet wants 3.-# --spill-dir DIR put chains.pts somewhere other than --out-dir. It is the-# only file the build reads RANDOMLY, so it wants cheap-# seeks -- keep it off a network or /mnt/c mount even when+# --spill-dir DIR put chains.pts somewhere other than --out-dir, and the roads+# tiler's spill with it. Those are the only files the build+# reads RANDOMLY or re-reads per zoom, so they want cheap+# seeks -- keep them off a network or /mnt/c mount even when # the output lives there. # See osm_ingest/README.md 'Build-time memory'. # Engines, per layer, so a ported layer rolls back with a flag@@ -143,6 +151,19 @@ SKIP_TRANSIT=0 SKIP_TILES=0 ONLY="" FORCE=0+# Fanned out to the tiles stage's layer builds and the transit stage's unzip loop.+# Peak memory is roughly JOBS x the largest concurrent job, so this is a memory dial+# as much as a speed one -- see build_v5_pmtiles.sh's own note.+JOBS=3+# Run the graph stage CONCURRENTLY with pois/transit/tiles.+#+# Off by default, and not because the stages conflict -- they genuinely do not, the+# graph has no consumers here and writes only to $OUT_DIR. It is off because the+# graph is the most memory-hungry stage in the build by a wide margin (61.85 GiB on+# Europe via the reference path, which is why --within-way-chains exists), and+# overlapping it with a fanned-out tiles stage multiplies two large peaks together.+# Turn it on when the machine's headroom is known; the wall-clock win is real.+OVERLAP_GRAPH=0 DRY_RUN=0 PUBLISH=0 PUBLISH_DRY_RUN=0@@ -180,6 +201,8 @@ while [[ $# -gt 0 ]]; do --skip-tiles) SKIP_TILES=1; shift ;; --only) ONLY="$2"; shift 2 ;; --force) FORCE=1; shift ;;+ --jobs) JOBS="$2"; shift 2 ;;+ --overlap-graph) OVERLAP_GRAPH=1; shift ;; --dry-run) DRY_RUN=1; shift ;; --publish) PUBLISH=1; shift ;; --publish-dry-run) PUBLISH_DRY_RUN=1; shift ;;@@ -323,10 +346,21 @@ if want_stage graph; then # measurable and separately deletable. [[ -n "$SPILL_DIR" ]] && GRAPH_ARGS+=(--spill-dir "$SPILL_DIR/graph") # Gated deliberately: road_graph truncates its outputs as it writes, so a- # failure here must not fall through to the manifest or the publish.- run cargo run --release --quiet --manifest-path "$HERE/osm_ingest/Cargo.toml" \- --bin road_graph -- "${GRAPH_ARGS[@]}"- mark_done graph+ # failure here must not fall through to the manifest or the publish. With+ # --overlap-graph that gate moves rather than disappears: the PID is waited+ # on before the manifest below, and a non-zero status still stops the build.+ if [[ "$OVERLAP_GRAPH" == "1" && "$DRY_RUN" != "1" ]]; then+ mkdir -p "$WORK/logs"+ GRAPH_LOG="$WORK/logs/graph.log"+ echo "[all] graph: started in the background (log $GRAPH_LOG)"+ cargo run --release --quiet --manifest-path "$HERE/osm_ingest/Cargo.toml" \+ --bin road_graph -- "${GRAPH_ARGS[@]}" >"$GRAPH_LOG" 2>&1 &+ GRAPH_PID=$!+ else+ run cargo run --release --quiet --manifest-path "$HERE/osm_ingest/Cargo.toml" \+ --bin road_graph -- "${GRAPH_ARGS[@]}"+ mark_done graph+ fi fi fi @@ -368,6 +402,7 @@ if want_stage transit; then WT_ARGS=(--work "$TRANSIT_WORK" --out "$OUT_DIR") [[ -n "$GTFS_MIRROR" ]] && WT_ARGS+=(--mirror-dir "$GTFS_MIRROR") [[ -n "$GTFS_RATE" ]] && WT_ARGS+=(--rate "$GTFS_RATE")+ WT_ARGS+=(--jobs "$JOBS") if [[ -n "$GTFS_MANIFEST" ]]; then WT_ARGS+=(--manifest "$GTFS_MANIFEST") else@@ -386,7 +421,10 @@ if want_stage tiles; then else require_pbf tiles echo "=== tiles -> $OUT ==="- V5_ARGS=(--pbf "$PBF" --out "$OUT" --workdir "$WORK/v5")+ V5_ARGS=(--pbf "$PBF" --out "$OUT" --workdir "$WORK/v5" --jobs "$JOBS")+ # The same mount the graph spill went to: the roads tiler re-reads its spill+ # once per zoom, which is the other access pattern a translation layer punishes.+ [[ -n "$SPILL_DIR" ]] && V5_ARGS+=(--spill-dir "$SPILL_DIR") # Overlay-only unless asked otherwise. The base options only mean anything # when there IS a base, and build_v5_pmtiles.sh rejects them alongside # --no-base rather than ignoring them, so they are passed only here.@@ -420,6 +458,24 @@ if want_stage tiles; then fi fi +# --- the graph barrier ---+# With --overlap-graph the graph stage has been running alongside everything above.+# It must be finished, and finished SUCCESSFULLY, before the manifest hashes its+# outputs -- road_graph truncates them as it writes, so a half-written nodes.bin+# would otherwise be measured and published as if it were whole.+if [[ -n "${GRAPH_PID:-}" ]]; then+ echo "[all] waiting for the background graph stage..."+ if wait "$GRAPH_PID"; then+ cat "$GRAPH_LOG"+ mark_done graph+ else+ echo "===== graph: FAILED =====" >&2+ cat "$GRAPH_LOG" >&2+ echo "ERROR: the graph stage failed; not writing a manifest" >&2+ exit 1+ fi+fi+ # --- manifest.txt: name, size, SHA-256 for all 12 --- ARTIFACTS=(metadata.bin road_names.bin nodes.bin edges.bin lanes.bin intermediate.bin poi_names.bin poi_index.bin poi_attrs.bin poi_spatial.bin poi_name_index.bin
scripts/maps/build_v5_pmtiles.sh
diff --git a/scripts/maps/build_v5_pmtiles.sh b/scripts/maps/build_v5_pmtiles.shindex 8d1af71778..c88962f03c 100644--- a/scripts/maps/build_v5_pmtiles.sh+++ b/scripts/maps/build_v5_pmtiles.sh@@ -74,6 +74,10 @@ set -euo pipefail # base-only options (--base-mode build, --base-jar, # --base-source) rather than ignoring them. # --workdir DIR scratch dir for intermediates (default ./v5_work)+# --spill-dir DIR where the roads tiler spills. It is tens of GB at planet scale+# and read once per zoom, so keep it off a network or /mnt/c+# mount even when the output lives there. Defaults to beside+# the roads archive. # --bbox BOX metro bbox "minlon,minlat,maxlon,maxlat" (dry runs) # --gtfs-manifest F feeds.manifest for the transit_stops layer (`name=dir[=motis_prefix]` # per line, as build_ca_transit.ps1 writes). Without it the@@ -137,6 +141,7 @@ ENGINE_TRANSIT_LINES="rust" ENGINE_ADMIN="legacy" ENGINE_ADMIN_CITY="rust" ADMIN_REUSE=""+SPILL_DIR="" DRY_RUN=0 NO_BASE=0 SKIP_BASE=0@@ -150,12 +155,22 @@ GTFS_MANIFEST="" KEEP_WORK=0 PUBLISH=0 PUBLISH_KEY=""+# How many layer builds run at once.+#+# Peak memory is roughly JOBS x the largest concurrent layer, and the largest is+# the roads extract at 5.00 GiB (California, measured). Three is the default+# because it uses the box without betting the build on it: six concurrent layers+# on a metro extract is comfortable, six on a planet extract is not, and a script+# that OOMs by default on a smaller machine is worse than one that is merely+# slower. Raise it when you know the machine.+JOBS=3 while [[ $# -gt 0 ]]; do case "$1" in --pbf) PBF="$2"; shift 2 ;; --out) OUT="$2"; shift 2 ;; --workdir) WORK="$2"; shift 2 ;;+ --spill-dir) SPILL_DIR="$2"; shift 2 ;; --bbox) BBOX="$2"; shift 2 ;; --base-mode) BASE_MODE="$2"; shift 2 ;; --base-jar) BASE_JAR="$2"; shift 2 ;;@@ -178,6 +193,7 @@ while [[ $# -gt 0 ]]; do --skip-admin) SKIP_ADMIN=1; shift ;; --skip-transit-stops) SKIP_TRANSIT_STOPS=1; shift ;; --gtfs-manifest) GTFS_MANIFEST="$2"; shift 2 ;;+ --jobs) JOBS="$2"; shift 2 ;; --keep-work) KEEP_WORK=1; shift ;; --publish) PUBLISH=1; shift ;; --publish-key) PUBLISH_KEY="$2"; shift 2 ;;@@ -248,6 +264,87 @@ run() { "$@" } +# --- the layer fan-out ------------------------------------------------------+#+# The layer builds are independent: each reads $PBF read-only, each mktemp -d's its+# own scratch, and each writes a distinct name under $WORK (admin gets its own+# subdirectory). So they can run at once -- but three things have to be right, and+# none of them are free.+#+# 1. `set -euo pipefail` does NOT propagate a failure out of a background job. A+# layer that dies would otherwise reach tile_join and yield a short archive that+# looks fine. `wait` per PID is the only way to see each exit code, so every job+# is waited on individually and its status recorded.+# 2. Each job's output goes to its own log and is replayed afterwards. Six builds+# interleaving progress bars on one terminal is unreadable, and the log is what+# makes a failure diagnosable.+# 3. MAPS_THREADS is divided among the jobs. Without it each layer would size its+# own pool from the whole box -- six layers claiming 32 threads each on 32 cores+# is 192 threads, and finishes slower than running in sequence.+[[ "$JOBS" =~ ^[0-9]+$ && "$JOBS" -ge 1 ]] || {+ echo "ERROR: --jobs wants a positive count (got '$JOBS')" >&2; exit 1; }++# Total threads to share out. `nproc` is not on every box, so fall back rather+# than fail; MAPS_THREADS from the caller wins, since an operator who set it meant it.+TOTAL_THREADS="${MAPS_THREADS:-$(nproc 2>/dev/null || echo 4)}"+PER_JOB_THREADS=$(( TOTAL_THREADS / JOBS ))+[[ "$PER_JOB_THREADS" -ge 1 ]] || PER_JOB_THREADS=1++JOB_PIDS=()+JOB_NAMES=()+JOB_LOGS=()+LOG_DIR="$WORK/logs"++# Start a layer build, or run it in the foreground when --jobs 1.+launch() {+ local name="$1"; shift+ if [[ "$DRY_RUN" == "1" ]]; then+ printf '[dry-run]'+ printf ' %q' "$@"+ printf '\n'+ return 0+ fi+ if [[ "$JOBS" -le 1 ]]; then+ echo "[v5] $name"+ MAPS_THREADS="$PER_JOB_THREADS" "$@"+ return+ fi+ mkdir -p "$LOG_DIR"+ local log="$LOG_DIR/$name.log"+ echo "[v5] $name: started (MAPS_THREADS=$PER_JOB_THREADS, log $log)"+ MAPS_THREADS="$PER_JOB_THREADS" "$@" >"$log" 2>&1 &+ JOB_PIDS+=("$!")+ JOB_NAMES+=("$name")+ JOB_LOGS+=("$log")+ # Never more than JOBS in flight, so --jobs really is a memory bound and not+ # just a hint.+ [[ "${#JOB_PIDS[@]}" -lt "$JOBS" ]] || reap+}++# Wait for every outstanding job, replay its log, and fail if any failed.+reap() {+ [[ "${#JOB_PIDS[@]}" -gt 0 ]] || return 0+ local failed=()+ local i+ for i in "${!JOB_PIDS[@]}"; do+ if wait "${JOB_PIDS[$i]}"; then+ echo "===== ${JOB_NAMES[$i]}: ok ====="+ cat "${JOB_LOGS[$i]}"+ else+ echo "===== ${JOB_NAMES[$i]}: FAILED =====" >&2+ cat "${JOB_LOGS[$i]}" >&2+ failed+=("${JOB_NAMES[$i]}")+ fi+ done+ JOB_PIDS=()+ JOB_NAMES=()+ JOB_LOGS=()+ if [[ "${#failed[@]}" -gt 0 ]]; then+ echo "ERROR: layer build(s) failed: ${failed[*]}" >&2+ exit 1+ fi+}+ command -v cargo >/dev/null || { echo "ERROR: cargo not installed (https://rustup.rs)" >&2; exit 1; } mkdir -p "$WORK" @@ -256,7 +353,15 @@ mkdir -p "$WORK" # to share the same guard. OUTDIR="$(cd "$(dirname "$OUT")" && pwd)" +# The merge's argv order is a CONTRACT: a later input wins a layer-name collision,+# so `--extra-layer` must come last and the base must come first. Completion order+# is not argv order once the layers run concurrently, so the list is assembled in+# three fixed pieces rather than appended to as jobs finish. `INPUTS_ADMIN` is+# separate only because its entries are existence-checked, and those files do not+# exist until the admin job has been reaped. INPUTS=()+INPUTS_ADMIN=()+INPUTS_POST=() # --- 1. base --- BASE="$WORK/base.pmtiles"@@ -271,7 +376,7 @@ else [[ -n "$BBOX" ]] && BASE_ARGS+=(--bbox "$BBOX") [[ -n "$BASE_SOURCE" ]] && BASE_ARGS+=(--source "$BASE_SOURCE") [[ "$BASE_MODE" == "build" ]] && BASE_ARGS+=(--jar "$BASE_JAR" --area "$BASE_AREA")- run "$HERE/build_base_layers.sh" "${BASE_ARGS[@]}"+ launch base "$HERE/build_base_layers.sh" "${BASE_ARGS[@]}" INPUTS+=("$BASE") fi @@ -280,7 +385,7 @@ if [[ "$SKIP_SAFETY" == "0" ]]; then [[ -n "$PBF" ]] || { echo "ERROR: --pbf required for safety layer (or --skip-safety)" >&2; exit 1; } SAFETY_ARGS=(--pbf "$PBF" --out "$WORK/safety.pmtiles" --engine "$ENGINE_SAFETY") [[ -n "$BBOX" ]] && SAFETY_ARGS+=(--bbox "$BBOX")- run "$HERE/build_safety_layer.sh" "${SAFETY_ARGS[@]}"+ launch safety "$HERE/build_safety_layer.sh" "${SAFETY_ARGS[@]}" INPUTS+=("$WORK/safety.pmtiles") fi @@ -289,7 +394,11 @@ if [[ "$SKIP_ROADS" == "0" ]]; then [[ -n "$PBF" ]] || { echo "ERROR: --pbf required for roads layer (or --skip-roads)" >&2; exit 1; } ROADS_ARGS=(--pbf "$PBF" --out "$WORK/roads.pmtiles") [[ -n "$BBOX" ]] && ROADS_ARGS+=(--bbox "$BBOX")- run "$HERE/build_roads_layer.sh" "${ROADS_ARGS[@]}"+ # The roads tiler streams, so it needs somewhere to spill. Forwarded rather than+ # left to default beside the output, because at planet scale the spill is tens of+ # GB and the operator is the only one who knows which mount can take it.+ [[ -n "$SPILL_DIR" ]] && ROADS_ARGS+=(--spill-dir "$SPILL_DIR/roads")+ launch roads "$HERE/build_roads_layer.sh" "${ROADS_ARGS[@]}" INPUTS+=("$WORK/roads.pmtiles") fi @@ -298,7 +407,7 @@ if [[ "$SKIP_TRANSIT_LINES" == "0" ]]; then [[ -n "$PBF" ]] || { echo "ERROR: --pbf required for transit_lines layer (or --skip-transit-lines)" >&2; exit 1; } TL_ARGS=(--pbf "$PBF" --out "$WORK/transit_lines.pmtiles" --engine "$ENGINE_TRANSIT_LINES") [[ -n "$BBOX" ]] && TL_ARGS+=(--bbox "$BBOX")- run "$HERE/build_transit_lines_layer.sh" "${TL_ARGS[@]}"+ launch transit_lines "$HERE/build_transit_lines_layer.sh" "${TL_ARGS[@]}" INPUTS+=("$WORK/transit_lines.pmtiles") fi @@ -311,7 +420,7 @@ if [[ "$SKIP_POIS" == "0" ]]; then --spatial-out "$OUTDIR/poi_spatial.bin" \ --name-index-out "$OUTDIR/poi_name_index.bin") [[ -n "$BBOX" ]] && POIS_ARGS+=(--bbox "$BBOX")- run "$HERE/build_pois_layer.sh" "${POIS_ARGS[@]}"+ launch ma_pois "$HERE/build_pois_layer.sh" "${POIS_ARGS[@]}" INPUTS+=("$WORK/ma_pois.pmtiles") fi @@ -325,10 +434,10 @@ if [[ "$SKIP_ADMIN" == "0" ]]; then # archive rather than rebuilt, so build_admin_layers.sh only has to do the one # level OSM can supply -- and therefore needs no ogr2ogr, tippecanoe or python3. [[ -n "$ADMIN_REUSE" ]] && ADMIN_ARGS+=(--only-city)- run "$HERE/build_admin_layers.sh" "${ADMIN_ARGS[@]}"- for l in admin_country admin_region admin_city; do- [[ -f "$WORK/admin/$l.pmtiles" ]] && INPUTS+=("$WORK/admin/$l.pmtiles")- done+ launch admin "$HERE/build_admin_layers.sh" "${ADMIN_ARGS[@]}"+ # Deferred: these files do not exist until the admin job has been reaped, and+ # `INPUTS_ADMIN` keeps their argv position regardless of when that happens.+ ADMIN_WANTED=(admin_country admin_region admin_city) if [[ -n "$ADMIN_REUSE" ]]; then SRC="$ADMIN_REUSE"@@ -349,7 +458,7 @@ if [[ "$SKIP_ADMIN" == "0" ]]; then --bin pmtiles_extract -- "$SRC" \ --out "$WORK/admin/admin_reused.pmtiles" \ --layer admin_country --layer admin_region- INPUTS+=("$WORK/admin/admin_reused.pmtiles")+ ADMIN_REUSED="$WORK/admin/admin_reused.pmtiles" fi fi @@ -366,15 +475,30 @@ if [[ "$SKIP_TRANSIT_STOPS" == "0" ]]; then # source rather than per layer. A stops layer tiled below the union has its # stops disappear above its own maxzoom, because the client then fetches real # tiles that carry no `transit_stops`.- run "$HERE/build_transit_stops_layer.sh" \+ launch transit_stops "$HERE/build_transit_stops_layer.sh" \ --manifest "$GTFS_MANIFEST" \ --out "$WORK/transit_stops.pmtiles" \ --workdir "$WORK/transit_stops" \ --maxzoom 16- INPUTS+=("$WORK/transit_stops.pmtiles")+ INPUTS_POST+=("$WORK/transit_stops.pmtiles") fi fi +# --- 7a. every layer job has to have finished before the merge reads its output ---+#+# This is the barrier the whole fan-out hangs on: `reap` waits on each PID+# individually and exits non-zero if any layer failed, because `set -e` would+# otherwise let a dead layer through to tile_join and produce a short archive that+# looks perfectly valid.+reap++for l in ${ADMIN_WANTED[@]+"${ADMIN_WANTED[@]}"}; do+ [[ -f "$WORK/admin/$l.pmtiles" ]] && INPUTS_ADMIN+=("$WORK/admin/$l.pmtiles")+done+[[ -n "${ADMIN_REUSED:-}" ]] && INPUTS_ADMIN+=("$ADMIN_REUSED")++INPUTS+=(${INPUTS_ADMIN[@]+"${INPUTS_ADMIN[@]}"} ${INPUTS_POST[@]+"${INPUTS_POST[@]}"})+ # --- 7b. layers the caller already built --- # Last in, so an --extra-layer wins a name collision with anything above it. for f in ${EXTRA_LAYERS[@]+"${EXTRA_LAYERS[@]}"}; do
scripts/maps/build_world_transit.sh
diff --git a/scripts/maps/build_world_transit.sh b/scripts/maps/build_world_transit.shindex e5d2b8c0a4..56a6d33755 100644--- a/scripts/maps/build_world_transit.sh+++ b/scripts/maps/build_world_transit.sh@@ -79,6 +79,12 @@ OUT="" REGION="*" MAX_FEEDS=0 MANIFEST=""+# How many feeds to unzip at once.+#+# The unzip loop only; the wget mirror below stays strictly serial on purpose --+# see its own comment. Unzipping is CPU-bound (inflate) and touches only per-feed+# directories, so this is the one place in this script that fans out safely.+JOBS="${MAPS_JOBS:-4}" MIRROR_DIR="" SKIP_MIRROR=0 RATE="30m"@@ -98,6 +104,7 @@ while [[ $# -gt 0 ]]; do --region) REGION="$2"; shift 2 ;; --max-feeds) MAX_FEEDS="$2"; shift 2 ;; --manifest) MANIFEST="$2"; shift 2 ;;+ --jobs) JOBS="$2"; shift 2 ;; --mirror-dir) MIRROR_DIR="$2"; shift 2 ;; --skip-mirror) SKIP_MIRROR=1; shift ;; --rate) RATE="$2"; shift 2 ;;@@ -170,6 +177,12 @@ else # only pulls what changed upstream. The `scripts/` transforms come along # because they name each feed's MOTIS id, which is worth having on disk # next to the zips it identifies.+ #+ # DO NOT fan this out. `--limit-rate` is per-process, so N parallel wgets+ # silently multiply the load on a volunteer host by N -- the rate limit+ # would still read 30m in the arguments while the host saw N x 30m. The+ # unzip loop below is where this script gets its concurrency; overlap the+ # download with CPU work instead of splitting the download. MIRROR_ARGS=( --limit-rate="$RATE" --mirror -l 2 --no-parent --cut-dirs=1 --no-host-directories@@ -199,7 +212,13 @@ else echo "=== ${#ZIPS[@]} zip(s) in the mirror; region filter: $REGION ===" : > "$BUILD_MANIFEST"- used=0++ # --- 3a. Select, serially, in sorted order -------------------------------+ #+ # Cheap (name parsing and a glob match, no I/O), and it has to be serial anyway+ # because `--max-feeds` contractually takes a stable prefix of the name-sorted+ # list. Unzipping is the expensive part and happens below.+ CAND_NAME=(); CAND_DIR=(); CAND_PREFIX=(); CAND_ZIP=() for zip in ${ZIPS[@]+"${ZIPS[@]}"}; do stem="$(basename "$zip" .gtfs.zip)" # `<region>_<Source>`: only the FIRST underscore separates them, and the@@ -217,24 +236,60 @@ else echo " WARN: '$stem' has no <region>_<source> split; skipped" >&2 continue fi- prefix="$region-$source_name"- name="$(safe_name "$stem")"- dir="$GTFS_ROOT/$name"+ CAND_NAME+=("$(safe_name "$stem")")+ CAND_PREFIX+=("$region-$source_name")+ CAND_ZIP+=("$zip")+ CAND_DIR+=("$GTFS_ROOT/$(safe_name "$stem")")+ done+ NCAND="${#CAND_NAME[@]}" - if [[ "$MAX_FEEDS" -gt 0 && "$used" -ge "$MAX_FEEDS" ]]; then break; fi- if [[ "$LIST_ONLY" == "1" ]]; then- printf '%s\t%s\t%s\n' "$name" "$prefix" "$zip"+ if [[ "$LIST_ONLY" == "1" ]]; then+ used=0+ for ((i = 0; i < NCAND; i++)); do+ [[ "$MAX_FEEDS" -gt 0 && "$used" -ge "$MAX_FEEDS" ]] && break+ printf '%s\t%s\t%s\n' "${CAND_NAME[$i]}" "${CAND_PREFIX[$i]}" "${CAND_ZIP[$i]}" used=$((used + 1))- continue- fi+ done+ echo "=== --list-only: $used feed(s) would be used ==="+ exit 0+ fi++ # --- 3b. Unzip, in parallel, in waves ------------------------------------+ #+ # Embarrassingly parallel: each feed is its own zip and its own directory. Two+ # things constrain how:+ #+ # * The manifest must stay in sorted order, so each feed writes its line to+ # its OWN fragment file and the fragments are concatenated by index. A+ # shared append would interleave and make the order completion order.+ # * `--max-feeds` counts feeds that actually unzipped, not feeds attempted, so+ # the work goes out in waves and the count is checked between them. Selecting+ # N candidates up front instead would silently return fewer than N whenever a+ # zip was corrupt.+ #+ # A feed that fails is a warning, not a failure: one bad zip out of hundreds must+ # not lose the build. Failure is signalled by an ABSENT fragment rather than an+ # exit code, so the reap loop needs no error plumbing.+ FRAG_DIR="$WORK/frag"+ rm -rf "$FRAG_DIR"+ mkdir -p "$FRAG_DIR" + unzip_feed() {+ local i="$1"+ local zip="${CAND_ZIP[$i]}" dir="${CAND_DIR[$i]}"+ local name="${CAND_NAME[$i]}" prefix="${CAND_PREFIX[$i]}"+ local stem+ stem="$(basename "$zip" .gtfs.zip)" if [[ ! -f "$dir/stops.txt" ]]; then mkdir -p "$dir" if ! unzip -oq "$zip" -d "$dir" 2>/dev/null; then- echo " WARN: unzip failed: $stem" >&2; discard_feed_dir "$dir"; continue+ echo " WARN: unzip failed: $stem" >&2+ discard_feed_dir "$dir"+ return 0 fi # Some feeds nest the txt files one dir deep; flatten if needed. if [[ ! -f "$dir/stops.txt" ]]; then+ local found inner found="$(find "$dir" -name stops.txt 2>/dev/null | head -n1)" # Test the find RESULT, not the dirname of it. `dirname ""` is `.`, # so a zip with no stops.txt anywhere used to set inner="." and run@@ -250,17 +305,38 @@ else if [[ ! -f "$dir/stops.txt" ]]; then echo " WARN: no stops.txt in the zip: $stem" >&2 discard_feed_dir "$dir"- continue+ return 0 fi fi- printf '%s=%s=%s\n' "$name" "$dir" "$prefix" >> "$BUILD_MANIFEST"- used=$((used + 1))- done+ printf '%s=%s=%s\n' "$name" "$dir" "$prefix" > "$FRAG_DIR/$i.line"+ return 0+ } - if [[ "$LIST_ONLY" == "1" ]]; then- echo "=== --list-only: $used feed(s) would be used ==="- exit 0- fi+ used=0+ next=0+ while [[ "$next" -lt "$NCAND" ]]; do+ [[ "$MAX_FEEDS" -gt 0 && "$used" -ge "$MAX_FEEDS" ]] && break+ wave_pids=(); wave_idx=()+ while [[ "${#wave_pids[@]}" -lt "$JOBS" && "$next" -lt "$NCAND" ]]; do+ unzip_feed "$next" &+ wave_pids+=("$!")+ wave_idx+=("$next")+ next=$((next + 1))+ done+ for p in ${wave_pids[@]+"${wave_pids[@]}"}; do+ wait "$p" || true+ done+ # In index order, so the manifest stays sorted however the wave finished.+ for i in ${wave_idx[@]+"${wave_idx[@]}"}; do+ [[ "$MAX_FEEDS" -gt 0 && "$used" -ge "$MAX_FEEDS" ]] && break+ if [[ -s "$FRAG_DIR/$i.line" ]]; then+ cat "$FRAG_DIR/$i.line" >> "$BUILD_MANIFEST"+ used=$((used + 1))+ fi+ done+ echo " ...$used feed(s) unzipped of $NCAND candidate(s)"+ done+ rm -rf "$FRAG_DIR" fi nfeeds="$(wc -l < "$BUILD_MANIFEST" | tr -d ' ')"