From 0fd3e7f45aa94e3e5f9fb3ed93105b150b85d5d9 Mon Sep 17 00:00:00 2001 From: David Gamez Diaz <1192523+davidgamez@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:03:45 -0500 Subject: [PATCH 1/8] feat: front Cloud Run with the Cloudflare edge and request-based billing --- README.md | 31 ++++---- docs/edge-cutover.md | 95 +++++++++++++++++++++++ gbfs-validator/function_config.json | 7 +- infra/cloud-run-service/cloud-run-main.tf | 50 +++++++++++- infra/cloud-run-service/vars.tf | 13 +++- infra/main.tf | 22 ++++++ infra/outputs.tf | 26 +++++++ infra/vars.tf | 10 +++ scripts/setup-deployer-sa-permissions.sh | 9 ++- scripts/setup-environment.sh | 55 ++----------- 10 files changed, 244 insertions(+), 74 deletions(-) create mode 100644 docs/edge-cutover.md create mode 100644 infra/outputs.tf diff --git a/README.md b/README.md index 531d2ef..4c3fd87 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,16 @@ Deployments are fully automated via GitHub Actions. There is nothing to build or > **Note:** `dev` and `qa` share the same GCP project (`gbfs-validator-staging`). Each environment gets its own Cloud Run service, Artifact Registry path, runtime service account, and Terraform state. `prod` lives in a separate dedicated project. +### Request path + +``` +client -> Cloudflare (mobilitydatabase.org zone) -> Worker mobilitydata-sites -> Cloud Run (run.app, IAM-protected) +``` + +- The public hostnames are served by the `mobilitydata-sites` Cloudflare Worker ([MobilityData/org-cloudflare-workers](https://github.com/MobilityData/org-cloudflare-workers), `sites/gbfs.api.mobilitydatabase.org/`). It accepts only `POST /validate` (plus the CORS preflight), checks the request, rate limits per IP and forwards it with an ID token of the `gbfs-edge-{env}` service account. Everything else gets a 404 at the edge and never starts a Cloud Run instance. +- Cloud Run only accepts requests signed by `gbfs-edge-{env}` (`roles/run.invoker`), uses request-based billing and is capped at 2 instances in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). The validator's built-in Swagger UI is turned off; the API docs are on [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html). +- The GCP load balancer and Cloud Armor are kept only during the cutover (`legacy_load_balancer_enabled`) and removed in a follow-up change. See [docs/edge-cutover.md](docs/edge-cutover.md) for the two-step rollout and the manual steps (Worker secrets, DNS, cleanup). + ### Manual deployment The staging and prod workflows can be triggered manually from the [GitHub Actions UI](https://github.com/MobilityData/gbfs-validator-java-infra/actions). @@ -98,10 +108,9 @@ Create a Firebase project and link it to the GCP project. Create OAuth client credentials via the [APIs & Services > Credentials](https://console.cloud.google.com/apis/credentials) page. These credentials will be passed as Terraform variables. -### 5. Create SSL Certificates +### 5. TLS -Google-managed SSL certificates are created automatically by `scripts/setup-environment.sh` (step 11 below). -They provision once the DNS A/AAAA records are in place and the load balancer is deployed. +Nothing to do: Cloudflare terminates TLS for the public hostname, and Cloud Run serves its run.app URL over TLS. ### 6. Enable and Configure Identity Platform @@ -148,22 +157,10 @@ This script: - Creates the deployer service account and grants IAM roles - Enables required Google APIs - Creates (or verifies) the shared Artifact Registry repository -- Creates global static IPv4 and IPv6 addresses for the load balancer (`{env}-lb-ipv4`, `{env}-lb-ipv6`) -- Creates a Google-managed SSL certificate for `{env}.gbfs.api.mobilitydatabase.org` -At the end it prints the IP addresses you will need for DNS. - -### 11a. Create DNS Records - -After running the setup script, ask your DNS administrator (Cloudflare) to create: - -| Type | Name | Value | -|---|---|---| -| `A` | `{env}.gbfs.api.mobilitydatabase.org` | IPv4 printed by the script | -| `AAAA` | `{env}.gbfs.api.mobilitydatabase.org` | IPv6 printed by the script | +### 11a. Connect the Cloudflare edge -> **Note:** DNS proxy must be **disabled** (grey cloud in Cloudflare) to allow Google-managed cert provisioning. -> The SSL certificate will complete provisioning automatically once DNS resolves to the load balancer IP. +After the first Terraform deploy of the environment, follow [docs/edge-cutover.md](docs/edge-cutover.md): create the edge service account key, add the Worker secrets, and point `{env}.gbfs.api.mobilitydatabase.org` at the Worker (proxied DNS record). ### 12. Generate a Deployer Service Account Key (for CI) diff --git a/docs/edge-cutover.md b/docs/edge-cutover.md new file mode 100644 index 0000000..e5e486c --- /dev/null +++ b/docs/edge-cutover.md @@ -0,0 +1,95 @@ +# Cloudflare edge cutover + +The public hostnames of the GBFS Validator API are served by the `mobilitydata-sites` Cloudflare Worker ([MobilityData/org-cloudflare-workers](https://github.com/MobilityData/org-cloudflare-workers), `sites/gbfs.api.mobilitydatabase.org/`). The Worker answers bots and scanners at the edge and forwards only valid `POST /validate` requests to Cloud Run, signed with the `gbfs-edge-{env}` service account. Cloud Run accepts nothing else. + +This replaces the GCP load balancer and Cloud Armor. Terraform covers everything in GCP; the steps below are the parts that live outside it (Cloudflare dashboard, service account keys, one-off cleanup). + +| Environment | Hostname | GCP project | Cloud Run service | Edge service account | +|---|---|---|---|---| +| `dev` | `dev.gbfs.api.mobilitydatabase.org` | `gbfs-validator-staging` | `dev-gbfs-validator-api` | `gbfs-edge-dev` | +| `qa` | `qa.gbfs.api.mobilitydatabase.org` | `gbfs-validator-staging` | `qa-gbfs-validator-api` | `gbfs-edge-qa` | +| `prod` | `gbfs.api.mobilitydatabase.org` | `gbfs-validator-prod` | `prod-gbfs-validator-api` | `gbfs-edge-prod` | + +## Behavior changes + +- Only `POST /validate` and its CORS preflight are served. Swagger UI (`/swagger-ui/...`) and `/v3/api-docs` are turned off on Cloud Run (`SPRINGDOC_SWAGGERUI_ENABLED=false`, `SPRINGDOC_APIDOCS_ENABLED=false`); the validator keeps them for anyone running it on their own infrastructure. The public API docs move to GitHub Pages: https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html (`docs/SwaggerUI/` in gbfs-validator-java). +- Each client IP gets 5 validate requests per minute (`ratelimits` in the Worker's `wrangler.jsonc`), then a 429 with `Retry-After: 60`. +- `feedUrl` must be an `http`/`https` URL on a public host, and the body must be JSON under 16 KB. +- At most 2 validations run at once per instance, with 2 instances in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). Above that, Cloud Run returns 429. + +## Before the first environment + +1. **Cloudflare, custom rule "Challenge abusive countries"** (zone `mobilitydatabase.org`, Security rules): append `and not (http.host contains "gbfs.api.mobilitydatabase.org")` to its expression. API clients cannot solve a Managed Challenge, so without this, validator users in those countries are blocked. +2. **Worker:** merge the `org-cloudflare-workers` change that adds `sites/gbfs.api.mobilitydatabase.org/`. Its routes do nothing until a hostname's DNS record is proxied (step 4 below), so merging early is safe. Its `verify-live` run checks the new hostnames too, and fails for each environment not yet cut over. +3. Do not turn on Bot Fight Mode or raise the Security Level on the zone: both apply to every hostname and would challenge API clients. +4. **API docs:** in [gbfs-validator-java](https://github.com/MobilityData/gbfs-validator-java/settings/pages) Settings, Pages, deploy from branch `master`, folder `/docs` (same as mobility-feed-api), once its `docs/SwaggerUI/` change is merged. + +## Two-step rollout + +The cutover is split so no hostname goes down: + +1. **This change** (`legacy_load_balancer_enabled = true`, the default): the load balancer, Cloud Armor and the public `allUsers` invoker stay, next to the new edge service account and its invoker. Cloud Run ingress opens to all traffic and switches to request-based billing. The hostnames keep working through the load balancer until their DNS records point at the Worker. During this step the run.app URL is publicly invokable, as the load balancer needs it. +2. **Follow-up change**, once all three environments are cut over: set `legacy_load_balancer_enabled = false` (or delete the `load_balancer` module, its `moved` block, `infra/load-balancer/` and the `allUsers` invoker). Terraform destroys the load balancer, Cloud Armor and the public invoker, and only the edge service account can call Cloud Run. + +## Per environment (dev, then qa, then prod) + +1. **Deploy Terraform** as usual: `dev` from the pull request, `qa` on merge to `main`, `prod` from the manual workflow. The plan creates `gbfs-edge-{env}` and its invoker binding and updates the Cloud Run service. It must not destroy anything: the `moved` blocks keep the existing load balancer and public invoker. + +2. **Edge service account key**, as Worker secret `GBFS_VALIDATOR__SA_KEY` (`DEV`, `QA` or `PROD`). The key never goes into Terraform state or the repository. With Wrangler logged in to the MobilityData account, from a checkout of `org-cloudflare-workers`: + + ```bash + gcloud iam service-accounts keys create /dev/stdout --iam-account=gbfs-edge-dev@gbfs-validator-staging.iam.gserviceaccount.com | npx wrangler secret put GBFS_VALIDATOR_DEV_SA_KEY --name mobilitydata-sites + ``` + + Or create the key file, paste its content in the dashboard (Workers & Pages, `mobilitydata-sites`, Settings, Variables and Secrets, type Secret), then delete the file. + +3. **Cloud Run URL**, as Worker secret `GBFS_VALIDATOR__URL`. It is also the `cloud_run_url` Terraform output. The Worker strips a trailing slash. + + ```bash + gcloud run services describe dev-gbfs-validator-api --region northamerica-northeast1 --project gbfs-validator-staging --format='value(status.url)' | npx wrangler secret put GBFS_VALIDATOR_DEV_URL --name mobilitydata-sites + ``` + + Workers Builds deploys keep secrets, so this is done once per environment. + +4. **DNS** (zone `mobilitydatabase.org`): delete the grey-cloud `A` and `AAAA` records of the hostname, then add `AAAA 100::`, **Proxied**. `100::` is a discard address: the Worker answers every request and never contacts it. Traffic moves from the load balancer to the Worker as the change propagates; both work in the meantime. + +5. **Check:** + + ```bash + curl -s -o /dev/null -w '%{http_code}\n' https://dev.gbfs.api.mobilitydatabase.org/ + curl -s -X POST https://dev.gbfs.api.mobilitydatabase.org/validate -H 'Content-Type: application/json' -d '{"feedUrl":"https://gbfs.lyft.com/gbfs/2.3/bay/gbfs.json"}' | head -c 300; echo + ``` + + Expected: `404` (answered by the Worker), then a validation report. + +## After all three environments + +1. **Follow-up change** with `legacy_load_balancer_enabled = false`, deployed to dev, qa and prod as usual. Then the run.app URL refuses callers without the edge identity: + + ```bash + curl -s -o /dev/null -w '%{http_code}\n' -X POST "$(gcloud run services describe dev-gbfs-validator-api --region northamerica-northeast1 --project gbfs-validator-staging --format='value(status.url)')/validate" + ``` + + Expected: `403`. + +2. **Clean up**, per environment, the static IPs and certificate the old setup script created outside Terraform. Unattached static IPs keep billing. + + ```bash + gcloud compute addresses delete dev-lb-ipv4 dev-lb-ipv6 --global --project gbfs-validator-staging + gcloud compute ssl-certificates delete dev-gbfs-api-mobilitydatabase-org --global --project gbfs-validator-staging + ``` + +3. Remove `roles/compute.loadBalancerAdmin` and `roles/compute.securityAdmin` from both deployer service accounts and from `scripts/setup-deployer-sa-permissions.sh`. + +4. Budget alerts, set on the billing account (the deployer service account has no billing permissions on purpose). The amount must be in the billing account's currency (CAD): + + ```bash + gcloud billing budgets create --billing-account= --display-name="gbfs-validator-prod" --budget-amount=25CAD --filter-projects=projects/gbfs-validator-prod --threshold-rule=percent=0.5 --threshold-rule=percent=1.0 + gcloud billing budgets create --billing-account= --display-name="gbfs-validator-staging" --budget-amount=15CAD --filter-projects=projects/gbfs-validator-staging --threshold-rule=percent=0.5 --threshold-rule=percent=1.0 + ``` + +## Internal addresses and local files + +The Worker only sees the top-level `feedUrl`. The validator also fetches every URL listed in the discovery file, the OAuth token URL and redirect targets, and it used to read `file:` URLs, so a public discovery file could point it at internal services or local files. The validator's loader closes this with `loader.public-network-only` (on by default in the API, from the gbfs-validator-java release that includes it): every host is resolved through a resolver that rejects private, loopback and link-local addresses, the connection uses the checked address, and `file:` URLs are refused. Deploy that release (`app_version`) before or together with this cutover. + +Not covered yet: the size of a downloaded file is not capped. diff --git a/gbfs-validator/function_config.json b/gbfs-validator/function_config.json index 506e53a..6a8eb47 100644 --- a/gbfs-validator/function_config.json +++ b/gbfs-validator/function_config.json @@ -2,12 +2,13 @@ "name_suffix": "gbfs-validator-api", "description": "API containing the GBFS validator", "entry_point": "org.entur.gbfs.validator.api.handler.OpenApiGeneratorApplication", - "timeout": 540, + "timeout": 60, "available_memory": "1Gi", "trigger_http": true, "ingress_settings": "ALLOW_ALL", - "max_instance_request_concurrency": 1, - "max_instance_count": 5, + "max_instance_request_concurrency": 2, + "max_instance_count": 2, + "max_instance_count_non_prod": 1, "min_instance_count": 0, "available_cpu": 1 } diff --git a/infra/cloud-run-service/cloud-run-main.tf b/infra/cloud-run-service/cloud-run-main.tf index 524f993..96b9e14 100644 --- a/infra/cloud-run-service/cloud-run-main.tf +++ b/infra/cloud-run-service/cloud-run-main.tf @@ -30,6 +30,9 @@ terraform { locals { gbfs_validator_config = jsondecode(file("${path.module}/../../gbfs-validator/function_config.json")) artifact_registry_repo = "${var.artifact_registry_repo}/${var.environment}" + # The instance cap is the hard cost ceiling: with request-based billing an instance costs + # nothing while idle, and at most max_instance_count instances can ever be billed at once. + max_instance_count = var.environment == "prod" ? local.gbfs_validator_config.max_instance_count : local.gbfs_validator_config.max_instance_count_non_prod } provider "google" { @@ -47,10 +50,22 @@ data "archive_file" "source_zip" { resource "google_cloud_run_v2_service" "gbfs_validator_api" { name = "${var.environment}-${local.gbfs_validator_config.name_suffix}" location = var.gcp_region - ingress = "INGRESS_TRAFFIC_INTERNAL_LOAD_BALANCER" + # Public traffic reaches the service through the Cloudflare Worker (mobilitydata-sites in + # MobilityData/org-cloudflare-workers), which calls the run.app URL with an ID token of the + # edge service account. Ingress is open, but only that service account may invoke (see below). + # The legacy load balancer also works with this ingress during the cutover. + ingress = "INGRESS_TRAFFIC_ALL" template { - service_account = var.gbfs_validator_service_account_email + service_account = var.gbfs_validator_service_account_email + timeout = "${local.gbfs_validator_config.timeout}s" + max_instance_request_concurrency = local.gbfs_validator_config.max_instance_request_concurrency + + scaling { + min_instance_count = local.gbfs_validator_config.min_instance_count + max_instance_count = local.max_instance_count + } + containers { image = "${var.gcp_region}-docker.pkg.dev/${var.project_id}/${local.artifact_registry_repo}/${var.gbfs_api_service}:${var.gbfs_api_image_version}" resources { @@ -58,6 +73,19 @@ resource "google_cloud_run_v2_service" "gbfs_validator_api" { cpu = local.gbfs_validator_config.available_cpu memory = local.gbfs_validator_config.available_memory } + # Request-based billing: CPU is billed only while a request is processed (and during + # startup). Instance-based billing kept an idle instance billed after every bot hit. + cpu_idle = true + } + # The validator ships Swagger UI (springdoc) for whoever runs it. Our deployment serves + # only POST /validate; the public API docs are on GitHub Pages (MobilityData/gbfs-validator-java). + env { + name = "SPRINGDOC_SWAGGERUI_ENABLED" + value = "false" + } + env { + name = "SPRINGDOC_APIDOCS_ENABLED" + value = "false" } } } @@ -69,10 +97,28 @@ resource "google_cloud_run_v2_service" "gbfs_validator_api" { } +# Public invoker for the legacy load balancer, only during the Cloudflare edge cutover +# (var.allow_unauthenticated). Removed with it in the follow-up change. resource "google_cloud_run_service_iam_member" "gbfs_validator_invoker" { + count = var.allow_unauthenticated ? 1 : 0 project = var.project_id location = var.gcp_region service = google_cloud_run_v2_service.gbfs_validator_api.name role = "roles/run.invoker" member = "allUsers" +} + +moved { + from = google_cloud_run_service_iam_member.gbfs_validator_invoker + to = google_cloud_run_service_iam_member.gbfs_validator_invoker[0] +} + +# Once the cutover is done, only the edge service account may invoke the service. Requests to +# the run.app URL without its ID token are rejected by Cloud Run IAM before reaching the container. +resource "google_cloud_run_service_iam_member" "gbfs_validator_edge_invoker" { + project = var.project_id + location = var.gcp_region + service = google_cloud_run_v2_service.gbfs_validator_api.name + role = "roles/run.invoker" + member = "serviceAccount:${var.edge_service_account_email}" } \ No newline at end of file diff --git a/infra/cloud-run-service/vars.tf b/infra/cloud-run-service/vars.tf index 61d1dbd..00e2532 100644 --- a/infra/cloud-run-service/vars.tf +++ b/infra/cloud-run-service/vars.tf @@ -69,4 +69,15 @@ variable "artifact_registry_repo" { type = string description = "Artifact Registry repository name (e.g. gbfs-validator-staging)" default = "gbfs-validator-staging" -} \ No newline at end of file +} + +variable "edge_service_account_email" { + type = string + description = "Service account the Cloudflare Worker uses to invoke the Cloud Run service" +} + +variable "allow_unauthenticated" { + type = bool + description = "Keep the public (allUsers) invoker, needed by the legacy load balancer during the Cloudflare edge cutover" + default = false +} diff --git a/infra/main.tf b/infra/main.tf index 8bc0082..c9b4dd0 100644 --- a/infra/main.tf +++ b/infra/main.tf @@ -41,12 +41,34 @@ module "cloud_run" { gbfs_validator_service_account_email = google_service_account.gbfs_validator_service_account.email gbfs_api_image_version = var.gbfs_api_image_version artifact_registry_repo = var.artifact_registry_repo + edge_service_account_email = google_service_account.gbfs_edge_service_account.email + allow_unauthenticated = var.legacy_load_balancer_enabled } +# Legacy entry point, kept only until the hostnames point at the Cloudflare Worker +# (var.legacy_load_balancer_enabled, docs/edge-cutover.md). Remove this block, the moved block +# and infra/load-balancer/ in the follow-up change that sets the variable to false. module "load_balancer" { + count = var.legacy_load_balancer_enabled ? 1 : 0 source = "./load-balancer" project_id = var.project_id environment = var.environment cloud_run_service_name = module.cloud_run.cloud_run_service_name } +# The module gained a count: keep the existing load balancer instead of recreating it. +moved { + from = module.load_balancer + to = module.load_balancer[0] +} + +# Identity of the Cloudflare Worker that fronts the API (mobilitydata-sites in +# MobilityData/org-cloudflare-workers). It holds roles/run.invoker on the Cloud Run service +# and nothing else. Its key is created once with gcloud and stored as a Worker secret, so it +# never lands in Terraform state (see docs/edge-cutover.md). +resource "google_service_account" "gbfs_edge_service_account" { + project = var.project_id + account_id = "gbfs-edge-${var.environment}" + display_name = "GBFS Validator Cloudflare edge (${var.environment})" +} + diff --git a/infra/outputs.tf b/infra/outputs.tf new file mode 100644 index 0000000..c47ffde --- /dev/null +++ b/infra/outputs.tf @@ -0,0 +1,26 @@ +# +# MobilityData 2025 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# Values the Cloudflare Worker needs as secrets (see docs/edge-cutover.md). +output "cloud_run_url" { + description = "run.app URL the Cloudflare Worker forwards validation requests to" + value = module.cloud_run.function_url +} + +output "edge_service_account_email" { + description = "Service account whose key the Cloudflare Worker uses to invoke Cloud Run" + value = google_service_account.gbfs_edge_service_account.email +} diff --git a/infra/vars.tf b/infra/vars.tf index cee82e5..431be5c 100644 --- a/infra/vars.tf +++ b/infra/vars.tf @@ -74,3 +74,13 @@ variable "artifact_registry_repo" { description = "Artifact Registry repository name shared across staging environments (e.g. gbfs-validator-staging)" default = "gbfs-validator-staging" } + +# Two-step rollout of the Cloudflare edge (docs/edge-cutover.md). While true, the old load +# balancer, Cloud Armor and the public (allUsers) invoker stay in place next to the edge +# invoker, so the hostnames keep working until their DNS records point at the Worker. +# Set to false in a follow-up change once every environment has been cut over. +variable "legacy_load_balancer_enabled" { + type = bool + description = "Keep the legacy load balancer and public invoker during the Cloudflare edge cutover" + default = true +} diff --git a/scripts/setup-deployer-sa-permissions.sh b/scripts/setup-deployer-sa-permissions.sh index ed5e298..3f5a8e3 100755 --- a/scripts/setup-deployer-sa-permissions.sh +++ b/scripts/setup-deployer-sa-permissions.sh @@ -54,10 +54,11 @@ ROLES=( # Cloud Run — create/update/delete services roles/run.admin - # Compute — load balancer, NEG, URL maps, forwarding rules + # Compute: load balancer and Cloud Armor. No longer used since the Cloudflare edge + # replaced the load balancer, but still needed by the first apply after the cutover, + # which destroys the old load balancer resources. Remove both roles once every + # environment has been cut over (see docs/edge-cutover.md). roles/compute.loadBalancerAdmin - - # Compute — Cloud Armor security policies roles/compute.securityAdmin # IAM — create service accounts (for Cloud Run runtime SA) @@ -69,7 +70,7 @@ ROLES=( # IAM — attach SAs to Cloud Run services roles/iam.serviceAccountUser - # IAM — set IAM policies (e.g. allUsers invoker on Cloud Run) + # IAM - set IAM policies (e.g. the edge service account as Cloud Run invoker) roles/resourcemanager.projectIamAdmin # Storage — read/write Terraform state bucket diff --git a/scripts/setup-environment.sh b/scripts/setup-environment.sh index f4f142b..1349d16 100755 --- a/scripts/setup-environment.sh +++ b/scripts/setup-environment.sh @@ -42,7 +42,9 @@ # - Artifact Registry repository: gbfs-validator- # - Artifact Registry cleanup policies (keep latest 5 images, delete untagged >30d) # - Google APIs enabled: artifactregistry, cloudfunctions, cloudbuild, run, -# certificatemanager, compute +# cloudresourcemanager +# - No load balancer, static IPs or SSL certificates: the public hostname is served by a +# Cloudflare Worker (see docs/edge-cutover.md) # - IAM Policy Binding for deployer to impersonate gbfs-validator-service-account # # Usage: @@ -217,8 +219,6 @@ REQUIRED_SERVICES=( cloudfunctions.googleapis.com cloudbuild.googleapis.com run.googleapis.com - certificatemanager.googleapis.com - compute.googleapis.com cloudresourcemanager.googleapis.com ) @@ -239,57 +239,18 @@ done echo "🎉 Setup complete for project $PROJECT_ID" -# === Global LB IP addresses === -IPV4_NAME="${ENVIRONMENT}-lb-ipv4" -IPV6_NAME="${ENVIRONMENT}-lb-ipv6" - -if gcloud compute addresses describe "${IPV4_NAME}" --project="${PROJECT_ID}" --global &>/dev/null; then - IPV4=$(gcloud compute addresses describe "${IPV4_NAME}" --project="${PROJECT_ID}" --global --format="value(address)") - echo "✅ IPv4 already exists: ${IPV4_NAME} → ${IPV4}" -else - echo "➕ Creating global IPv4: ${IPV4_NAME} ..." - gcloud compute addresses create "${IPV4_NAME}" --project="${PROJECT_ID}" --ip-version=IPV4 --global - IPV4=$(gcloud compute addresses describe "${IPV4_NAME}" --project="${PROJECT_ID}" --global --format="value(address)") - echo "✅ Created: ${IPV4_NAME} → ${IPV4}" -fi - -if gcloud compute addresses describe "${IPV6_NAME}" --project="${PROJECT_ID}" --global &>/dev/null; then - IPV6=$(gcloud compute addresses describe "${IPV6_NAME}" --project="${PROJECT_ID}" --global --format="value(address)") - echo "✅ IPv6 already exists: ${IPV6_NAME} → ${IPV6}" -else - echo "➕ Creating global IPv6: ${IPV6_NAME} ..." - gcloud compute addresses create "${IPV6_NAME}" --project="${PROJECT_ID}" --ip-version=IPV6 --global - IPV6=$(gcloud compute addresses describe "${IPV6_NAME}" --project="${PROJECT_ID}" --global --format="value(address)") - echo "✅ Created: ${IPV6_NAME} → ${IPV6}" -fi - -# === Google-managed SSL certificate === -# Prod uses the bare domain; all other envs use {env}.domain +# === Next steps === if [[ "${ENVIRONMENT}" == "prod" ]]; then DOMAIN="gbfs.api.mobilitydatabase.org" else DOMAIN="${ENVIRONMENT}.gbfs.api.mobilitydatabase.org" fi -CERT_NAME="${ENVIRONMENT}-gbfs-api-mobilitydatabase-org" - -if gcloud compute ssl-certificates describe "${CERT_NAME}" --project="${PROJECT_ID}" --global &>/dev/null; then - echo "✅ SSL certificate already exists: ${CERT_NAME}" -else - echo "➕ Creating Google-managed SSL certificate: ${CERT_NAME} ..." - gcloud compute ssl-certificates create "${CERT_NAME}" \ - --project="${PROJECT_ID}" \ - --domains="${DOMAIN}" \ - --global - echo "✅ Created: ${CERT_NAME} (provisioning pending DNS)" -fi echo "" echo "==========================================" -echo " ✅ Bootstrap complete for: ${ENVIRONMENT}" -echo "" -echo " Next: create DNS records for ${DOMAIN}:" -echo " A ${IPV4}" -echo " AAAA ${IPV6}" +echo " Bootstrap complete for: ${ENVIRONMENT}" echo "" -echo " SSL cert provisions automatically once DNS resolves." +echo " Public traffic for ${DOMAIN} goes through the Cloudflare Worker" +echo " (mobilitydata-sites in MobilityData/org-cloudflare-workers), not a GCP load balancer." +echo " After the first Terraform deploy, follow docs/edge-cutover.md to connect it." echo "==========================================" From 5387fdad22b87f31b0a1caa8f7d29b3434cb62e8 Mon Sep 17 00:00:00 2001 From: David Gamez Diaz <1192523+davidgamez@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:14:40 -0500 Subject: [PATCH 2/8] docs: cut over with Workers Custom Domains and keep legacy LB setup Replace the proxied DNS steps with the Custom Domain cutover, keep the legacy load balancer IPs and certificate in the setup script until the follow-up change, and note the run.app URL stays public until then. Co-Authored-By: Claude Opus 5.5 --- README.md | 5 ++-- docs/edge-cutover.md | 30 ++++++++++---------- scripts/setup-environment.sh | 53 ++++++++++++++++++++++++++++++++---- 3 files changed, 65 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 4c3fd87..2d1e921 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ client -> Cloudflare (mobilitydatabase.org zone) -> Worker mobilitydata-sites -> ``` - The public hostnames are served by the `mobilitydata-sites` Cloudflare Worker ([MobilityData/org-cloudflare-workers](https://github.com/MobilityData/org-cloudflare-workers), `sites/gbfs.api.mobilitydatabase.org/`). It accepts only `POST /validate` (plus the CORS preflight), checks the request, rate limits per IP and forwards it with an ID token of the `gbfs-edge-{env}` service account. Everything else gets a 404 at the edge and never starts a Cloud Run instance. -- Cloud Run only accepts requests signed by `gbfs-edge-{env}` (`roles/run.invoker`), uses request-based billing and is capped at 2 instances in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). The validator's built-in Swagger UI is turned off; the API docs are on [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html). +- Once the legacy load balancer is removed (follow-up change), Cloud Run only accepts requests signed by `gbfs-edge-{env}` (`roles/run.invoker`); until then the public `allUsers` invoker stays and the run.app URL is publicly reachable. Cloud Run uses request-based billing and is capped at 2 instances in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). The validator's built-in Swagger UI is turned off; the API docs are on [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html). - The GCP load balancer and Cloud Armor are kept only during the cutover (`legacy_load_balancer_enabled`) and removed in a follow-up change. See [docs/edge-cutover.md](docs/edge-cutover.md) for the two-step rollout and the manual steps (Worker secrets, DNS, cleanup). ### Manual deployment @@ -157,10 +157,11 @@ This script: - Creates the deployer service account and grants IAM roles - Enables required Google APIs - Creates (or verifies) the shared Artifact Registry repository +- Creates the legacy load balancer's static IPs (`{env}-lb-ipv4`, `{env}-lb-ipv6`) and SSL certificate, needed only while `legacy_load_balancer_enabled` is true ### 11a. Connect the Cloudflare edge -After the first Terraform deploy of the environment, follow [docs/edge-cutover.md](docs/edge-cutover.md): create the edge service account key, add the Worker secrets, and point `{env}.gbfs.api.mobilitydatabase.org` at the Worker (proxied DNS record). +After the first Terraform deploy of the environment, follow [docs/edge-cutover.md](docs/edge-cutover.md): create the edge service account key, add the Worker secrets, and hand `{env}.gbfs.api.mobilitydatabase.org` to the Worker as a Workers Custom Domain (delete its old DNS records; the Custom Domain creates its own record and certificate). ### 12. Generate a Deployer Service Account Key (for CI) diff --git a/docs/edge-cutover.md b/docs/edge-cutover.md index e5e486c..f8b903a 100644 --- a/docs/edge-cutover.md +++ b/docs/edge-cutover.md @@ -17,25 +17,27 @@ This replaces the GCP load balancer and Cloud Armor. Terraform covers everything - `feedUrl` must be an `http`/`https` URL on a public host, and the body must be JSON under 16 KB. - At most 2 validations run at once per instance, with 2 instances in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). Above that, Cloud Run returns 429. -## Before the first environment +## Before the cutover 1. **Cloudflare, custom rule "Challenge abusive countries"** (zone `mobilitydatabase.org`, Security rules): append `and not (http.host contains "gbfs.api.mobilitydatabase.org")` to its expression. API clients cannot solve a Managed Challenge, so without this, validator users in those countries are blocked. -2. **Worker:** merge the `org-cloudflare-workers` change that adds `sites/gbfs.api.mobilitydatabase.org/`. Its routes do nothing until a hostname's DNS record is proxied (step 4 below), so merging early is safe. Its `verify-live` run checks the new hostnames too, and fails for each environment not yet cut over. -3. Do not turn on Bot Fight Mode or raise the Security Level on the zone: both apply to every hostname and would challenge API clients. +2. Do not turn on Bot Fight Mode or raise the Security Level on the zone: both apply to every hostname and would challenge API clients. +3. **Validator release** with `loader.public-network-only` (gbfs-validator-java), deployed with `app_version` in step 1 of the cutover (see "Internal addresses and local files"). 4. **API docs:** in [gbfs-validator-java](https://github.com/MobilityData/gbfs-validator-java/settings/pages) Settings, Pages, deploy from branch `master`, folder `/docs` (same as mobility-feed-api), once its `docs/SwaggerUI/` change is merged. ## Two-step rollout -The cutover is split so no hostname goes down: +The cutover is split so the hostnames keep working while the GCP side changes: -1. **This change** (`legacy_load_balancer_enabled = true`, the default): the load balancer, Cloud Armor and the public `allUsers` invoker stay, next to the new edge service account and its invoker. Cloud Run ingress opens to all traffic and switches to request-based billing. The hostnames keep working through the load balancer until their DNS records point at the Worker. During this step the run.app URL is publicly invokable, as the load balancer needs it. -2. **Follow-up change**, once all three environments are cut over: set `legacy_load_balancer_enabled = false` (or delete the `load_balancer` module, its `moved` block, `infra/load-balancer/` and the `allUsers` invoker). Terraform destroys the load balancer, Cloud Armor and the public invoker, and only the edge service account can call Cloud Run. +1. **This change** (`legacy_load_balancer_enabled = true`, the default): the load balancer, Cloud Armor and the public `allUsers` invoker stay, next to the new edge service account and its invoker. Cloud Run ingress opens to all traffic and switches to request-based billing. The hostnames keep working through the load balancer until the Worker takes them over. During this step the run.app URL is publicly invokable, as the load balancer needs it. +2. **Follow-up change**, once the Worker serves all three hostnames: set `legacy_load_balancer_enabled = false` (or delete the `load_balancer` module, its `moved` block, `infra/load-balancer/` and the `allUsers` invoker). Terraform destroys the load balancer, Cloud Armor and the public invoker, and only the edge service account can call Cloud Run. -## Per environment (dev, then qa, then prod) +## Cutover -1. **Deploy Terraform** as usual: `dev` from the pull request, `qa` on merge to `main`, `prod` from the manual workflow. The plan creates `gbfs-edge-{env}` and its invoker binding and updates the Cloud Run service. It must not destroy anything: the `moved` blocks keep the existing load balancer and public invoker. +The Worker serves the hostnames as **Workers Custom Domains**, not routes on proxied records. Universal SSL on the zone's Free plan only covers one subdomain level (`*.mobilitydatabase.org`), so a proxied `gbfs.api.mobilitydatabase.org` record would fail TLS, and HSTS preload makes browsers refuse it. A Custom Domain gets its own certificate and DNS record from Cloudflare, at no cost. The Worker repository deploys on every merge to `main` (Workers Builds) and declares all three Custom Domains, so the DNS switch happens for all environments at once, in step 4. -2. **Edge service account key**, as Worker secret `GBFS_VALIDATOR__SA_KEY` (`DEV`, `QA` or `PROD`). The key never goes into Terraform state or the repository. With Wrangler logged in to the MobilityData account, from a checkout of `org-cloudflare-workers`: +1. **Deploy Terraform** to each environment as usual: `dev` from the pull request, `qa` on merge to `main`, `prod` from the manual workflow, with `app_version` set to the validator release from "Before the cutover". The plan creates `gbfs-edge-{env}` and its invoker binding and updates the Cloud Run service. It must not destroy anything: the `moved` blocks keep the existing load balancer and public invoker. + +2. **Edge service account keys**, as Worker secrets `GBFS_VALIDATOR_DEV_SA_KEY`, `GBFS_VALIDATOR_QA_SA_KEY` and `GBFS_VALIDATOR_PROD_SA_KEY`. The keys never go into Terraform state or a repository. With Wrangler logged in to the MobilityData account, from a checkout of `org-cloudflare-workers` (shown for `dev`; `qa` uses `gbfs-edge-qa@gbfs-validator-staging`, `prod` uses `gbfs-edge-prod@gbfs-validator-prod`): ```bash gcloud iam service-accounts keys create /dev/stdout --iam-account=gbfs-edge-dev@gbfs-validator-staging.iam.gserviceaccount.com | npx wrangler secret put GBFS_VALIDATOR_DEV_SA_KEY --name mobilitydata-sites @@ -43,24 +45,22 @@ The cutover is split so no hostname goes down: Or create the key file, paste its content in the dashboard (Workers & Pages, `mobilitydata-sites`, Settings, Variables and Secrets, type Secret), then delete the file. -3. **Cloud Run URL**, as Worker secret `GBFS_VALIDATOR__URL`. It is also the `cloud_run_url` Terraform output. The Worker strips a trailing slash. +3. **Cloud Run URLs**, as Worker secrets `GBFS_VALIDATOR_DEV_URL`, `GBFS_VALIDATOR_QA_URL` and `GBFS_VALIDATOR_PROD_URL`. Each is also the `cloud_run_url` Terraform output. Workers Builds deploys keep secrets, so steps 2 and 3 are done once. ```bash gcloud run services describe dev-gbfs-validator-api --region northamerica-northeast1 --project gbfs-validator-staging --format='value(status.url)' | npx wrangler secret put GBFS_VALIDATOR_DEV_URL --name mobilitydata-sites ``` - Workers Builds deploys keep secrets, so this is done once per environment. - -4. **DNS** (zone `mobilitydatabase.org`): delete the grey-cloud `A` and `AAAA` records of the hostname, then add `AAAA 100::`, **Proxied**. `100::` is a discard address: the Worker answers every request and never contacts it. Traffic moves from the load balancer to the Worker as the change propagates; both work in the meantime. +4. **Switch DNS by merging the Worker change.** In the `mobilitydatabase.org` DNS records, delete the grey-cloud `A` and `AAAA` records of `gbfs.api`, `qa.gbfs.api` and `dev.gbfs.api` (six records), then merge the `org-cloudflare-workers` change right away. Workers Builds deploys the Worker and creates the three Custom Domains with their DNS records and certificates; nothing else needs adding in DNS. The deploy fails if any of the six records still exists: delete it and retry the build. The hostnames are unreachable from the record deletion until the certificates are active, usually a few minutes. -5. **Check:** +5. **Check**, for each hostname: ```bash curl -s -o /dev/null -w '%{http_code}\n' https://dev.gbfs.api.mobilitydatabase.org/ curl -s -X POST https://dev.gbfs.api.mobilitydatabase.org/validate -H 'Content-Type: application/json' -d '{"feedUrl":"https://gbfs.lyft.com/gbfs/2.3/bay/gbfs.json"}' | head -c 300; echo ``` - Expected: `404` (answered by the Worker), then a validation report. + Expected: `404` (answered by the Worker), then a validation report. The Worker repository's `verify-live` run after the merge checks the same 404s. ## After all three environments diff --git a/scripts/setup-environment.sh b/scripts/setup-environment.sh index 1349d16..4656a9f 100755 --- a/scripts/setup-environment.sh +++ b/scripts/setup-environment.sh @@ -42,9 +42,7 @@ # - Artifact Registry repository: gbfs-validator- # - Artifact Registry cleanup policies (keep latest 5 images, delete untagged >30d) # - Google APIs enabled: artifactregistry, cloudfunctions, cloudbuild, run, -# cloudresourcemanager -# - No load balancer, static IPs or SSL certificates: the public hostname is served by a -# Cloudflare Worker (see docs/edge-cutover.md) +# certificatemanager, compute # - IAM Policy Binding for deployer to impersonate gbfs-validator-service-account # # Usage: @@ -219,6 +217,8 @@ REQUIRED_SERVICES=( cloudfunctions.googleapis.com cloudbuild.googleapis.com run.googleapis.com + certificatemanager.googleapis.com + compute.googleapis.com cloudresourcemanager.googleapis.com ) @@ -239,18 +239,59 @@ done echo "🎉 Setup complete for project $PROJECT_ID" -# === Next steps === +# === Legacy load balancer: global IP addresses and SSL certificate === +# Still read by the load_balancer Terraform module while legacy_load_balancer_enabled is true +# (Cloudflare edge cutover, docs/edge-cutover.md). Remove this block, and the compute and +# certificatemanager APIs above, in the follow-up change that sets it to false. +IPV4_NAME="${ENVIRONMENT}-lb-ipv4" +IPV6_NAME="${ENVIRONMENT}-lb-ipv6" + +if gcloud compute addresses describe "${IPV4_NAME}" --project="${PROJECT_ID}" --global &>/dev/null; then + IPV4=$(gcloud compute addresses describe "${IPV4_NAME}" --project="${PROJECT_ID}" --global --format="value(address)") + echo "✅ IPv4 already exists: ${IPV4_NAME} → ${IPV4}" +else + echo "➕ Creating global IPv4: ${IPV4_NAME} ..." + gcloud compute addresses create "${IPV4_NAME}" --project="${PROJECT_ID}" --ip-version=IPV4 --global + IPV4=$(gcloud compute addresses describe "${IPV4_NAME}" --project="${PROJECT_ID}" --global --format="value(address)") + echo "✅ Created: ${IPV4_NAME} → ${IPV4}" +fi + +if gcloud compute addresses describe "${IPV6_NAME}" --project="${PROJECT_ID}" --global &>/dev/null; then + IPV6=$(gcloud compute addresses describe "${IPV6_NAME}" --project="${PROJECT_ID}" --global --format="value(address)") + echo "✅ IPv6 already exists: ${IPV6_NAME} → ${IPV6}" +else + echo "➕ Creating global IPv6: ${IPV6_NAME} ..." + gcloud compute addresses create "${IPV6_NAME}" --project="${PROJECT_ID}" --ip-version=IPV6 --global + IPV6=$(gcloud compute addresses describe "${IPV6_NAME}" --project="${PROJECT_ID}" --global --format="value(address)") + echo "✅ Created: ${IPV6_NAME} → ${IPV6}" +fi + +# === Google-managed SSL certificate === +# Prod uses the bare domain; all other envs use {env}.domain if [[ "${ENVIRONMENT}" == "prod" ]]; then DOMAIN="gbfs.api.mobilitydatabase.org" else DOMAIN="${ENVIRONMENT}.gbfs.api.mobilitydatabase.org" fi +CERT_NAME="${ENVIRONMENT}-gbfs-api-mobilitydatabase-org" + +if gcloud compute ssl-certificates describe "${CERT_NAME}" --project="${PROJECT_ID}" --global &>/dev/null; then + echo "✅ SSL certificate already exists: ${CERT_NAME}" +else + echo "➕ Creating Google-managed SSL certificate: ${CERT_NAME} ..." + gcloud compute ssl-certificates create "${CERT_NAME}" \ + --project="${PROJECT_ID}" \ + --domains="${DOMAIN}" \ + --global + echo "✅ Created: ${CERT_NAME} (provisioning pending DNS)" +fi echo "" echo "==========================================" -echo " Bootstrap complete for: ${ENVIRONMENT}" +echo " ✅ Bootstrap complete for: ${ENVIRONMENT}" echo "" echo " Public traffic for ${DOMAIN} goes through the Cloudflare Worker" -echo " (mobilitydata-sites in MobilityData/org-cloudflare-workers), not a GCP load balancer." +echo " (mobilitydata-sites in MobilityData/org-cloudflare-workers) as a Workers Custom Domain." echo " After the first Terraform deploy, follow docs/edge-cutover.md to connect it." +echo " The IPs and certificate above only serve the legacy load balancer during the cutover." echo "==========================================" From bb82bef02f5f0bdbe7c7d08464198bab8bbbba6c Mon Sep 17 00:00:00 2001 From: David Gamez Diaz <1192523+davidgamez@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:40:22 -0500 Subject: [PATCH 3/8] feat: allow 5 prod instances and document limits and capacity Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- docs/edge-cutover.md | 17 +++++++++++++++-- gbfs-validator/function_config.json | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2d1e921..45b3464 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ client -> Cloudflare (mobilitydatabase.org zone) -> Worker mobilitydata-sites -> ``` - The public hostnames are served by the `mobilitydata-sites` Cloudflare Worker ([MobilityData/org-cloudflare-workers](https://github.com/MobilityData/org-cloudflare-workers), `sites/gbfs.api.mobilitydatabase.org/`). It accepts only `POST /validate` (plus the CORS preflight), checks the request, rate limits per IP and forwards it with an ID token of the `gbfs-edge-{env}` service account. Everything else gets a 404 at the edge and never starts a Cloud Run instance. -- Once the legacy load balancer is removed (follow-up change), Cloud Run only accepts requests signed by `gbfs-edge-{env}` (`roles/run.invoker`); until then the public `allUsers` invoker stays and the run.app URL is publicly reachable. Cloud Run uses request-based billing and is capped at 2 instances in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). The validator's built-in Swagger UI is turned off; the API docs are on [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html). +- Once the legacy load balancer is removed (follow-up change), Cloud Run only accepts requests signed by `gbfs-edge-{env}` (`roles/run.invoker`); until then the public `allUsers` invoker stays and the run.app URL is publicly reachable. Cloud Run uses request-based billing and is capped at 5 instances in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). Request limits and capacity: [docs/edge-cutover.md](docs/edge-cutover.md#limits-and-capacity). The validator's built-in Swagger UI is turned off; the API docs are on [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html). - The GCP load balancer and Cloud Armor are kept only during the cutover (`legacy_load_balancer_enabled`) and removed in a follow-up change. See [docs/edge-cutover.md](docs/edge-cutover.md) for the two-step rollout and the manual steps (Worker secrets, DNS, cleanup). ### Manual deployment diff --git a/docs/edge-cutover.md b/docs/edge-cutover.md index f8b903a..b37cb09 100644 --- a/docs/edge-cutover.md +++ b/docs/edge-cutover.md @@ -13,9 +13,22 @@ This replaces the GCP load balancer and Cloud Armor. Terraform covers everything ## Behavior changes - Only `POST /validate` and its CORS preflight are served. Swagger UI (`/swagger-ui/...`) and `/v3/api-docs` are turned off on Cloud Run (`SPRINGDOC_SWAGGERUI_ENABLED=false`, `SPRINGDOC_APIDOCS_ENABLED=false`); the validator keeps them for anyone running it on their own infrastructure. The public API docs move to GitHub Pages: https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html (`docs/SwaggerUI/` in gbfs-validator-java). -- Each client IP gets 5 validate requests per minute (`ratelimits` in the Worker's `wrangler.jsonc`), then a 429 with `Retry-After: 60`. +- Requests are rate limited and capacity is capped; see [Limits and capacity](#limits-and-capacity). - `feedUrl` must be an `http`/`https` URL on a public host, and the body must be JSON under 16 KB. -- At most 2 validations run at once per instance, with 2 instances in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). Above that, Cloud Run returns 429. + +## Limits and capacity + +| Limit | Value | Where | When exceeded | +|---|---|---|---| +| Validate requests per client IP | 5 per minute, counted per Cloudflare location | Worker `ratelimits` (`wrangler.jsonc` in org-cloudflare-workers) | 429, `Retry-After: 60` | +| Request body | 16 KB | Worker `MAX_BODY_BYTES` | 413 | +| Validations per instance | 2 at once | `max_instance_request_concurrency` in `gbfs-validator/function_config.json` | Queued by Cloud Run | +| Instances | 5 in prod, 1 in dev/qa | `max_instance_count`, `max_instance_count_non_prod` in the same file | Cloud Run returns 429 after a short queue | +| Request timeout | 60 s | `timeout` in the same file | 504 | + +Measured on dev (1 vCPU, 1 GiB), a validation takes about 1 to 7 s once the instance is warm, and 5 to 8 s on a cold start. Validations are CPU-bound, so two at once on one instance share its vCPU: 4 parallel validations of a large feed took 16 s in total. Prod therefore absorbs peaks with instances, not concurrency: 10 validations in flight, roughly 50 to 100 per minute depending on feed size. + +Cost follows usage: an idle instance costs nothing, and each validation costs well under 0.01 CAD. The per-IP rate limit is the control on sustained use; the instance cap bounds the worst case (about 64 CAD per instance busy for a whole month). To change a limit, edit the value in the file listed above; the budget alerts below flag any unexpected spend. ## Before the cutover diff --git a/gbfs-validator/function_config.json b/gbfs-validator/function_config.json index 6a8eb47..a43af6f 100644 --- a/gbfs-validator/function_config.json +++ b/gbfs-validator/function_config.json @@ -7,7 +7,7 @@ "trigger_http": true, "ingress_settings": "ALLOW_ALL", "max_instance_request_concurrency": 2, - "max_instance_count": 2, + "max_instance_count": 5, "max_instance_count_non_prod": 1, "min_instance_count": 0, "available_cpu": 1 From 8aef7909f13bb687b539cf279864dbd515b8cbec Mon Sep 17 00:00:00 2001 From: David Gamez Diaz <1192523+davidgamez@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:52:32 -0500 Subject: [PATCH 4/8] feat: size Cloud Run from load tests and cut over dev/qa first Run 5 validations per 2 GiB instance (4 in prod, 1 in dev/qa) with the JVM heap at 75%, document limits and the load test, and switch prod to the Worker after dev and qa. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- docs/edge-cutover.md | 19 +++++++++++-------- gbfs-validator/function_config.json | 9 +++++---- infra/cloud-run-service/cloud-run-main.tf | 6 ++++++ 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 45b3464..a3a0849 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ client -> Cloudflare (mobilitydatabase.org zone) -> Worker mobilitydata-sites -> ``` - The public hostnames are served by the `mobilitydata-sites` Cloudflare Worker ([MobilityData/org-cloudflare-workers](https://github.com/MobilityData/org-cloudflare-workers), `sites/gbfs.api.mobilitydatabase.org/`). It accepts only `POST /validate` (plus the CORS preflight), checks the request, rate limits per IP and forwards it with an ID token of the `gbfs-edge-{env}` service account. Everything else gets a 404 at the edge and never starts a Cloud Run instance. -- Once the legacy load balancer is removed (follow-up change), Cloud Run only accepts requests signed by `gbfs-edge-{env}` (`roles/run.invoker`); until then the public `allUsers` invoker stays and the run.app URL is publicly reachable. Cloud Run uses request-based billing and is capped at 5 instances in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). Request limits and capacity: [docs/edge-cutover.md](docs/edge-cutover.md#limits-and-capacity). The validator's built-in Swagger UI is turned off; the API docs are on [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html). +- Once the legacy load balancer is removed (follow-up change), Cloud Run only accepts requests signed by `gbfs-edge-{env}` (`roles/run.invoker`); until then the public `allUsers` invoker stays and the run.app URL is publicly reachable. Cloud Run uses request-based billing and is capped at 4 instances of 5 concurrent validations in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). Request limits and capacity: [docs/edge-cutover.md](docs/edge-cutover.md#limits-and-capacity). The validator's built-in Swagger UI is turned off; the API docs are on [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html). - The GCP load balancer and Cloud Armor are kept only during the cutover (`legacy_load_balancer_enabled`) and removed in a follow-up change. See [docs/edge-cutover.md](docs/edge-cutover.md) for the two-step rollout and the manual steps (Worker secrets, DNS, cleanup). ### Manual deployment diff --git a/docs/edge-cutover.md b/docs/edge-cutover.md index b37cb09..4bbbde4 100644 --- a/docs/edge-cutover.md +++ b/docs/edge-cutover.md @@ -20,15 +20,16 @@ This replaces the GCP load balancer and Cloud Armor. Terraform covers everything | Limit | Value | Where | When exceeded | |---|---|---|---| -| Validate requests per client IP | 5 per minute, counted per Cloudflare location | Worker `ratelimits` (`wrangler.jsonc` in org-cloudflare-workers) | 429, `Retry-After: 60` | +| Validate requests per client IP | 5 per minute, per Cloudflare location | Worker `ratelimits` (`wrangler.jsonc` in org-cloudflare-workers) | 429, `Retry-After: 60` | | Request body | 16 KB | Worker `MAX_BODY_BYTES` | 413 | -| Validations per instance | 2 at once | `max_instance_request_concurrency` in `gbfs-validator/function_config.json` | Queued by Cloud Run | -| Instances | 5 in prod, 1 in dev/qa | `max_instance_count`, `max_instance_count_non_prod` in the same file | Cloud Run returns 429 after a short queue | +| Validations per instance | 5 at once | `max_instance_request_concurrency` in `gbfs-validator/function_config.json` | Queued by Cloud Run | +| Instances | 4 in prod (20 at once), 1 in dev/qa (5 at once) | `max_instance_count`, `max_instance_count_non_prod` in the same file | 429 after a short queue | +| Instance size | 1 vCPU, 2 GiB, JVM heap 75% (`JAVA_TOOL_OPTIONS=-XX:MaxRAMPercentage=75`) | `available_cpu`, `available_memory`, `java_tool_options` in the same file | Heap: `OutOfMemoryError` (500) | | Request timeout | 60 s | `timeout` in the same file | 504 | -Measured on dev (1 vCPU, 1 GiB), a validation takes about 1 to 7 s once the instance is warm, and 5 to 8 s on a cold start. Validations are CPU-bound, so two at once on one instance share its vCPU: 4 parallel validations of a large feed took 16 s in total. Prod therefore absorbs peaks with instances, not concurrency: 10 validations in flight, roughly 50 to 100 per minute depending on feed size. +**Load test** (2026-11-26, worst case: concurrent validations of 8 to 12 MB feeds, Dott Paris and Bolt). Without the heap setting the JVM uses only 25% of the memory: prod at 1 GiB threw `OutOfMemoryError` from 4 concurrent validations with the container at 47% memory. With heap 75%: at 1 GiB, 4 at once peaked at 986 MiB and 6 were killed; at 2 GiB, 6 peaked at 1.3 GiB and 8 at 1.75 GiB, all successful. 5 per instance at 2 GiB leaves room for larger feeds. Typical feeds (0.2 to 2 MB) validate in 1 to 7 s and need far less. -Cost follows usage: an idle instance costs nothing, and each validation costs well under 0.01 CAD. The per-IP rate limit is the control on sustained use; the instance cap bounds the worst case (about 64 CAD per instance busy for a whole month). To change a limit, edit the value in the file listed above; the budget alerts below flag any unexpected spend. +Cost follows usage: an idle instance costs nothing, and a validation costs well under 0.01 CAD. The per-IP rate limit controls sustained use; the instance cap bounds the worst case (about 70 CAD per 2 GiB instance busy for a whole month). The budget alerts below flag unexpected spend. ## Before the cutover @@ -42,11 +43,11 @@ Cost follows usage: an idle instance costs nothing, and each validation costs we The cutover is split so the hostnames keep working while the GCP side changes: 1. **This change** (`legacy_load_balancer_enabled = true`, the default): the load balancer, Cloud Armor and the public `allUsers` invoker stay, next to the new edge service account and its invoker. Cloud Run ingress opens to all traffic and switches to request-based billing. The hostnames keep working through the load balancer until the Worker takes them over. During this step the run.app URL is publicly invokable, as the load balancer needs it. -2. **Follow-up change**, once the Worker serves all three hostnames: set `legacy_load_balancer_enabled = false` (or delete the `load_balancer` module, its `moved` block, `infra/load-balancer/` and the `allUsers` invoker). Terraform destroys the load balancer, Cloud Armor and the public invoker, and only the edge service account can call Cloud Run. +2. **Follow-up change**, once the Worker serves all three hostnames (step 6 of the cutover): set `legacy_load_balancer_enabled = false` (or delete the `load_balancer` module, its `moved` block, `infra/load-balancer/` and the `allUsers` invoker). Terraform destroys the load balancer, Cloud Armor and the public invoker, and only the edge service account can call Cloud Run. ## Cutover -The Worker serves the hostnames as **Workers Custom Domains**, not routes on proxied records. Universal SSL on the zone's Free plan only covers one subdomain level (`*.mobilitydatabase.org`), so a proxied `gbfs.api.mobilitydatabase.org` record would fail TLS, and HSTS preload makes browsers refuse it. A Custom Domain gets its own certificate and DNS record from Cloudflare, at no cost. The Worker repository deploys on every merge to `main` (Workers Builds) and declares all three Custom Domains, so the DNS switch happens for all environments at once, in step 4. +The Worker serves the hostnames as **Workers Custom Domains**, not routes on proxied records. Universal SSL on the zone's Free plan only covers one subdomain level (`*.mobilitydatabase.org`), so a proxied `gbfs.api.mobilitydatabase.org` record would fail TLS, and HSTS preload makes browsers refuse it. A Custom Domain gets its own certificate and DNS record from Cloudflare, at no cost. The Worker repository deploys on every merge to `main` (Workers Builds), so each merge that adds a Custom Domain is a DNS switch. `dev` and `qa` switch first (step 4); `prod` follows in a separate Worker change once they are verified (step 6). 1. **Deploy Terraform** to each environment as usual: `dev` from the pull request, `qa` on merge to `main`, `prod` from the manual workflow, with `app_version` set to the validator release from "Before the cutover". The plan creates `gbfs-edge-{env}` and its invoker binding and updates the Cloud Run service. It must not destroy anything: the `moved` blocks keep the existing load balancer and public invoker. @@ -64,7 +65,7 @@ The Worker serves the hostnames as **Workers Custom Domains**, not routes on pro gcloud run services describe dev-gbfs-validator-api --region northamerica-northeast1 --project gbfs-validator-staging --format='value(status.url)' | npx wrangler secret put GBFS_VALIDATOR_DEV_URL --name mobilitydata-sites ``` -4. **Switch DNS by merging the Worker change.** In the `mobilitydatabase.org` DNS records, delete the grey-cloud `A` and `AAAA` records of `gbfs.api`, `qa.gbfs.api` and `dev.gbfs.api` (six records), then merge the `org-cloudflare-workers` change right away. Workers Builds deploys the Worker and creates the three Custom Domains with their DNS records and certificates; nothing else needs adding in DNS. The deploy fails if any of the six records still exists: delete it and retry the build. The hostnames are unreachable from the record deletion until the certificates are active, usually a few minutes. +4. **Switch dev and qa by merging the Worker change.** In the `mobilitydatabase.org` DNS records, delete the grey-cloud `A` and `AAAA` records of `qa.gbfs.api` and `dev.gbfs.api` (four records), then merge the `org-cloudflare-workers` change right away. Workers Builds deploys the Worker and creates both Custom Domains with their DNS records and certificates; nothing else needs adding in DNS. The deploy fails, blocking deploys of every site on that Worker, while any of the four records still exists: delete it and retry the build. The hostnames are unreachable from the record deletion until the certificates are active, usually a few minutes. 5. **Check**, for each hostname: @@ -75,6 +76,8 @@ The Worker serves the hostnames as **Workers Custom Domains**, not routes on pro Expected: `404` (answered by the Worker), then a validation report. The Worker repository's `verify-live` run after the merge checks the same 404s. +6. **Switch prod** the same way once dev and qa work: a Worker change adds `gbfs.api.mobilitydatabase.org` to `src/sites.js`, its Custom Domain to `wrangler.jsonc` and its expected results; delete the two grey-cloud records of `gbfs.api`, merge, and check as in step 5. + ## After all three environments 1. **Follow-up change** with `legacy_load_balancer_enabled = false`, deployed to dev, qa and prod as usual. Then the run.app URL refuses callers without the edge identity: diff --git a/gbfs-validator/function_config.json b/gbfs-validator/function_config.json index a43af6f..b4c3a76 100644 --- a/gbfs-validator/function_config.json +++ b/gbfs-validator/function_config.json @@ -3,12 +3,13 @@ "description": "API containing the GBFS validator", "entry_point": "org.entur.gbfs.validator.api.handler.OpenApiGeneratorApplication", "timeout": 60, - "available_memory": "1Gi", + "available_memory": "2Gi", "trigger_http": true, "ingress_settings": "ALLOW_ALL", - "max_instance_request_concurrency": 2, - "max_instance_count": 5, + "max_instance_request_concurrency": 5, + "max_instance_count": 4, "max_instance_count_non_prod": 1, "min_instance_count": 0, - "available_cpu": 1 + "available_cpu": 1, + "java_tool_options": "-XX:MaxRAMPercentage=75" } diff --git a/infra/cloud-run-service/cloud-run-main.tf b/infra/cloud-run-service/cloud-run-main.tf index 96b9e14..8ce607f 100644 --- a/infra/cloud-run-service/cloud-run-main.tf +++ b/infra/cloud-run-service/cloud-run-main.tf @@ -77,6 +77,12 @@ resource "google_cloud_run_v2_service" "gbfs_validator_api" { # startup). Instance-based billing kept an idle instance billed after every bot hit. cpu_idle = true } + # Without it the JVM caps its heap at 25% of the container memory, and large feeds fail with + # OutOfMemoryError while most of the memory is unused (load test in docs/edge-cutover.md). + env { + name = "JAVA_TOOL_OPTIONS" + value = local.gbfs_validator_config.java_tool_options + } # The validator ships Swagger UI (springdoc) for whoever runs it. Our deployment serves # only POST /validate; the public API docs are on GitHub Pages (MobilityData/gbfs-validator-java). env { From 0ecbc3b9682875d6a40ee28d6dd1c949659d3456 Mon Sep 17 00:00:00 2001 From: David Gamez Diaz <1192523+davidgamez@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:55:17 -0500 Subject: [PATCH 5/8] feat: allow 10 prod instances (50 concurrent validations) Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- docs/edge-cutover.md | 2 +- gbfs-validator/function_config.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a3a0849..48d465c 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ client -> Cloudflare (mobilitydatabase.org zone) -> Worker mobilitydata-sites -> ``` - The public hostnames are served by the `mobilitydata-sites` Cloudflare Worker ([MobilityData/org-cloudflare-workers](https://github.com/MobilityData/org-cloudflare-workers), `sites/gbfs.api.mobilitydatabase.org/`). It accepts only `POST /validate` (plus the CORS preflight), checks the request, rate limits per IP and forwards it with an ID token of the `gbfs-edge-{env}` service account. Everything else gets a 404 at the edge and never starts a Cloud Run instance. -- Once the legacy load balancer is removed (follow-up change), Cloud Run only accepts requests signed by `gbfs-edge-{env}` (`roles/run.invoker`); until then the public `allUsers` invoker stays and the run.app URL is publicly reachable. Cloud Run uses request-based billing and is capped at 4 instances of 5 concurrent validations in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). Request limits and capacity: [docs/edge-cutover.md](docs/edge-cutover.md#limits-and-capacity). The validator's built-in Swagger UI is turned off; the API docs are on [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html). +- Once the legacy load balancer is removed (follow-up change), Cloud Run only accepts requests signed by `gbfs-edge-{env}` (`roles/run.invoker`); until then the public `allUsers` invoker stays and the run.app URL is publicly reachable. Cloud Run uses request-based billing and is capped at 10 instances of 5 concurrent validations in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). Request limits and capacity: [docs/edge-cutover.md](docs/edge-cutover.md#limits-and-capacity). The validator's built-in Swagger UI is turned off; the API docs are on [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html). - The GCP load balancer and Cloud Armor are kept only during the cutover (`legacy_load_balancer_enabled`) and removed in a follow-up change. See [docs/edge-cutover.md](docs/edge-cutover.md) for the two-step rollout and the manual steps (Worker secrets, DNS, cleanup). ### Manual deployment diff --git a/docs/edge-cutover.md b/docs/edge-cutover.md index 4bbbde4..60ccc9e 100644 --- a/docs/edge-cutover.md +++ b/docs/edge-cutover.md @@ -23,7 +23,7 @@ This replaces the GCP load balancer and Cloud Armor. Terraform covers everything | Validate requests per client IP | 5 per minute, per Cloudflare location | Worker `ratelimits` (`wrangler.jsonc` in org-cloudflare-workers) | 429, `Retry-After: 60` | | Request body | 16 KB | Worker `MAX_BODY_BYTES` | 413 | | Validations per instance | 5 at once | `max_instance_request_concurrency` in `gbfs-validator/function_config.json` | Queued by Cloud Run | -| Instances | 4 in prod (20 at once), 1 in dev/qa (5 at once) | `max_instance_count`, `max_instance_count_non_prod` in the same file | 429 after a short queue | +| Instances | 10 in prod (50 at once), 1 in dev/qa (5 at once) | `max_instance_count`, `max_instance_count_non_prod` in the same file | 429 after a short queue | | Instance size | 1 vCPU, 2 GiB, JVM heap 75% (`JAVA_TOOL_OPTIONS=-XX:MaxRAMPercentage=75`) | `available_cpu`, `available_memory`, `java_tool_options` in the same file | Heap: `OutOfMemoryError` (500) | | Request timeout | 60 s | `timeout` in the same file | 504 | diff --git a/gbfs-validator/function_config.json b/gbfs-validator/function_config.json index b4c3a76..ec0a8df 100644 --- a/gbfs-validator/function_config.json +++ b/gbfs-validator/function_config.json @@ -7,7 +7,7 @@ "trigger_http": true, "ingress_settings": "ALLOW_ALL", "max_instance_request_concurrency": 5, - "max_instance_count": 4, + "max_instance_count": 10, "max_instance_count_non_prod": 1, "min_instance_count": 0, "available_cpu": 1, From 700e2ed0ee3a1212df531e05649a030b1a3099fe Mon Sep 17 00:00:00 2001 From: David Gamez Diaz <1192523+davidgamez@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:59:13 -0500 Subject: [PATCH 6/8] docs: shorten the edge cutover runbook Co-Authored-By: Claude Opus 5.5 --- docs/edge-cutover.md | 120 +++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 78 deletions(-) diff --git a/docs/edge-cutover.md b/docs/edge-cutover.md index 60ccc9e..a5bfa85 100644 --- a/docs/edge-cutover.md +++ b/docs/edge-cutover.md @@ -1,111 +1,75 @@ # Cloudflare edge cutover -The public hostnames of the GBFS Validator API are served by the `mobilitydata-sites` Cloudflare Worker ([MobilityData/org-cloudflare-workers](https://github.com/MobilityData/org-cloudflare-workers), `sites/gbfs.api.mobilitydatabase.org/`). The Worker answers bots and scanners at the edge and forwards only valid `POST /validate` requests to Cloud Run, signed with the `gbfs-edge-{env}` service account. Cloud Run accepts nothing else. +``` +client -> Cloudflare Worker (mobilitydata-sites) -> Cloud Run +``` -This replaces the GCP load balancer and Cloud Armor. Terraform covers everything in GCP; the steps below are the parts that live outside it (Cloudflare dashboard, service account keys, one-off cleanup). +The Worker ([org-cloudflare-workers](https://github.com/MobilityData/org-cloudflare-workers), `sites/gbfs.api.mobilitydatabase.org/`) only lets `POST /validate` through and calls Cloud Run as the `gbfs-edge-{env}` service account. It replaces the GCP load balancer and Cloud Armor. -| Environment | Hostname | GCP project | Cloud Run service | Edge service account | -|---|---|---|---|---| -| `dev` | `dev.gbfs.api.mobilitydatabase.org` | `gbfs-validator-staging` | `dev-gbfs-validator-api` | `gbfs-edge-dev` | -| `qa` | `qa.gbfs.api.mobilitydatabase.org` | `gbfs-validator-staging` | `qa-gbfs-validator-api` | `gbfs-edge-qa` | -| `prod` | `gbfs.api.mobilitydatabase.org` | `gbfs-validator-prod` | `prod-gbfs-validator-api` | `gbfs-edge-prod` | - -## Behavior changes - -- Only `POST /validate` and its CORS preflight are served. Swagger UI (`/swagger-ui/...`) and `/v3/api-docs` are turned off on Cloud Run (`SPRINGDOC_SWAGGERUI_ENABLED=false`, `SPRINGDOC_APIDOCS_ENABLED=false`); the validator keeps them for anyone running it on their own infrastructure. The public API docs move to GitHub Pages: https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html (`docs/SwaggerUI/` in gbfs-validator-java). -- Requests are rate limited and capacity is capped; see [Limits and capacity](#limits-and-capacity). -- `feedUrl` must be an `http`/`https` URL on a public host, and the body must be JSON under 16 KB. +| Env | Hostname | GCP project | +|---|---|---| +| dev | `dev.gbfs.api.mobilitydatabase.org` | `gbfs-validator-staging` | +| qa | `qa.gbfs.api.mobilitydatabase.org` | `gbfs-validator-staging` | +| prod | `gbfs.api.mobilitydatabase.org` | `gbfs-validator-prod` | ## Limits and capacity -| Limit | Value | Where | When exceeded | -|---|---|---|---| -| Validate requests per client IP | 5 per minute, per Cloudflare location | Worker `ratelimits` (`wrangler.jsonc` in org-cloudflare-workers) | 429, `Retry-After: 60` | -| Request body | 16 KB | Worker `MAX_BODY_BYTES` | 413 | -| Validations per instance | 5 at once | `max_instance_request_concurrency` in `gbfs-validator/function_config.json` | Queued by Cloud Run | -| Instances | 10 in prod (50 at once), 1 in dev/qa (5 at once) | `max_instance_count`, `max_instance_count_non_prod` in the same file | 429 after a short queue | -| Instance size | 1 vCPU, 2 GiB, JVM heap 75% (`JAVA_TOOL_OPTIONS=-XX:MaxRAMPercentage=75`) | `available_cpu`, `available_memory`, `java_tool_options` in the same file | Heap: `OutOfMemoryError` (500) | -| Request timeout | 60 s | `timeout` in the same file | 504 | - -**Load test** (2026-11-26, worst case: concurrent validations of 8 to 12 MB feeds, Dott Paris and Bolt). Without the heap setting the JVM uses only 25% of the memory: prod at 1 GiB threw `OutOfMemoryError` from 4 concurrent validations with the container at 47% memory. With heap 75%: at 1 GiB, 4 at once peaked at 986 MiB and 6 were killed; at 2 GiB, 6 peaked at 1.3 GiB and 8 at 1.75 GiB, all successful. 5 per instance at 2 GiB leaves room for larger feeds. Typical feeds (0.2 to 2 MB) validate in 1 to 7 s and need far less. +| Limit | Value | Change it in | +|---|---|---| +| Requests per IP | 5/min, then 429 | Worker `wrangler.jsonc` | +| Request body | 16 KB, then 413 | Worker `handler.js` | +| Validations per instance | 5 at once | `gbfs-validator/function_config.json` | +| Instances | prod 10, dev/qa 1 | same file | +| Instance size | 1 vCPU, 2 GiB, JVM heap 75% | same file | +| Timeout | 60 s | same file | -Cost follows usage: an idle instance costs nothing, and a validation costs well under 0.01 CAD. The per-IP rate limit controls sustained use; the instance cap bounds the worst case (about 70 CAD per 2 GiB instance busy for a whole month). The budget alerts below flag unexpected spend. +Why 2 GiB and heap 75%: without the heap setting Java uses only 25% of memory and large feeds (8-12 MB) failed with `OutOfMemoryError`. At 2 GiB, 8 large validations at once used 1.75 GiB. -## Before the cutover +Other changes: Swagger UI is off on Cloud Run (docs move to [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html)); `feedUrl` must be a public http(s) URL. -1. **Cloudflare, custom rule "Challenge abusive countries"** (zone `mobilitydatabase.org`, Security rules): append `and not (http.host contains "gbfs.api.mobilitydatabase.org")` to its expression. API clients cannot solve a Managed Challenge, so without this, validator users in those countries are blocked. -2. Do not turn on Bot Fight Mode or raise the Security Level on the zone: both apply to every hostname and would challenge API clients. -3. **Validator release** with `loader.public-network-only` (gbfs-validator-java), deployed with `app_version` in step 1 of the cutover (see "Internal addresses and local files"). -4. **API docs:** in [gbfs-validator-java](https://github.com/MobilityData/gbfs-validator-java/settings/pages) Settings, Pages, deploy from branch `master`, folder `/docs` (same as mobility-feed-api), once its `docs/SwaggerUI/` change is merged. +## Before -## Two-step rollout - -The cutover is split so the hostnames keep working while the GCP side changes: - -1. **This change** (`legacy_load_balancer_enabled = true`, the default): the load balancer, Cloud Armor and the public `allUsers` invoker stay, next to the new edge service account and its invoker. Cloud Run ingress opens to all traffic and switches to request-based billing. The hostnames keep working through the load balancer until the Worker takes them over. During this step the run.app URL is publicly invokable, as the load balancer needs it. -2. **Follow-up change**, once the Worker serves all three hostnames (step 6 of the cutover): set `legacy_load_balancer_enabled = false` (or delete the `load_balancer` module, its `moved` block, `infra/load-balancer/` and the `allUsers` invoker). Terraform destroys the load balancer, Cloud Armor and the public invoker, and only the edge service account can call Cloud Run. +- [ ] Cloudflare, Security rules, "Challenge abusive countries": append `and not (http.host contains "gbfs.api.mobilitydatabase.org")`. +- [ ] Release gbfs-validator-java with `loader.public-network-only` (blocks private addresses and local files). +- [ ] Don't enable Bot Fight Mode on the zone (it would block API clients). ## Cutover -The Worker serves the hostnames as **Workers Custom Domains**, not routes on proxied records. Universal SSL on the zone's Free plan only covers one subdomain level (`*.mobilitydatabase.org`), so a proxied `gbfs.api.mobilitydatabase.org` record would fail TLS, and HSTS preload makes browsers refuse it. A Custom Domain gets its own certificate and DNS record from Cloudflare, at no cost. The Worker repository deploys on every merge to `main` (Workers Builds), so each merge that adds a Custom Domain is a DNS switch. `dev` and `qa` switch first (step 4); `prod` follows in a separate Worker change once they are verified (step 6). - -1. **Deploy Terraform** to each environment as usual: `dev` from the pull request, `qa` on merge to `main`, `prod` from the manual workflow, with `app_version` set to the validator release from "Before the cutover". The plan creates `gbfs-edge-{env}` and its invoker binding and updates the Cloud Run service. It must not destroy anything: the `moved` blocks keep the existing load balancer and public invoker. +1. **Deploy this repo** to dev, qa and prod (prod with the new `app_version`). Nothing goes down: the old load balancer stays while `legacy_load_balancer_enabled = true`. -2. **Edge service account keys**, as Worker secrets `GBFS_VALIDATOR_DEV_SA_KEY`, `GBFS_VALIDATOR_QA_SA_KEY` and `GBFS_VALIDATOR_PROD_SA_KEY`. The keys never go into Terraform state or a repository. With Wrangler logged in to the MobilityData account, from a checkout of `org-cloudflare-workers` (shown for `dev`; `qa` uses `gbfs-edge-qa@gbfs-validator-staging`, `prod` uses `gbfs-edge-prod@gbfs-validator-prod`): +2. **Add Worker secrets** (once per env; `DEV` shown): ```bash gcloud iam service-accounts keys create /dev/stdout --iam-account=gbfs-edge-dev@gbfs-validator-staging.iam.gserviceaccount.com | npx wrangler secret put GBFS_VALIDATOR_DEV_SA_KEY --name mobilitydata-sites - ``` - - Or create the key file, paste its content in the dashboard (Workers & Pages, `mobilitydata-sites`, Settings, Variables and Secrets, type Secret), then delete the file. - -3. **Cloud Run URLs**, as Worker secrets `GBFS_VALIDATOR_DEV_URL`, `GBFS_VALIDATOR_QA_URL` and `GBFS_VALIDATOR_PROD_URL`. Each is also the `cloud_run_url` Terraform output. Workers Builds deploys keep secrets, so steps 2 and 3 are done once. - - ```bash gcloud run services describe dev-gbfs-validator-api --region northamerica-northeast1 --project gbfs-validator-staging --format='value(status.url)' | npx wrangler secret put GBFS_VALIDATOR_DEV_URL --name mobilitydata-sites ``` -4. **Switch dev and qa by merging the Worker change.** In the `mobilitydatabase.org` DNS records, delete the grey-cloud `A` and `AAAA` records of `qa.gbfs.api` and `dev.gbfs.api` (four records), then merge the `org-cloudflare-workers` change right away. Workers Builds deploys the Worker and creates both Custom Domains with their DNS records and certificates; nothing else needs adding in DNS. The deploy fails, blocking deploys of every site on that Worker, while any of the four records still exists: delete it and retry the build. The hostnames are unreachable from the record deletion until the certificates are active, usually a few minutes. +3. **Switch dev and qa:** delete the A/AAAA records of `dev.gbfs.api` and `qa.gbfs.api` in Cloudflare DNS, then merge the Worker PR right away. Its Custom Domains create the new DNS records and certificates (a few minutes of downtime). -5. **Check**, for each hostname: +4. **Check:** ```bash curl -s -o /dev/null -w '%{http_code}\n' https://dev.gbfs.api.mobilitydatabase.org/ - curl -s -X POST https://dev.gbfs.api.mobilitydatabase.org/validate -H 'Content-Type: application/json' -d '{"feedUrl":"https://gbfs.lyft.com/gbfs/2.3/bay/gbfs.json"}' | head -c 300; echo - ``` - - Expected: `404` (answered by the Worker), then a validation report. The Worker repository's `verify-live` run after the merge checks the same 404s. - -6. **Switch prod** the same way once dev and qa work: a Worker change adds `gbfs.api.mobilitydatabase.org` to `src/sites.js`, its Custom Domain to `wrangler.jsonc` and its expected results; delete the two grey-cloud records of `gbfs.api`, merge, and check as in step 5. - -## After all three environments - -1. **Follow-up change** with `legacy_load_balancer_enabled = false`, deployed to dev, qa and prod as usual. Then the run.app URL refuses callers without the edge identity: - - ```bash - curl -s -o /dev/null -w '%{http_code}\n' -X POST "$(gcloud run services describe dev-gbfs-validator-api --region northamerica-northeast1 --project gbfs-validator-staging --format='value(status.url)')/validate" + curl -s -X POST https://dev.gbfs.api.mobilitydatabase.org/validate -H 'Content-Type: application/json' -d '{"feedUrl":"https://gbfs.lyft.com/gbfs/2.3/bay/gbfs.json"}' | head -c 200 ``` - Expected: `403`. + Expect `404`, then a validation report. -2. **Clean up**, per environment, the static IPs and certificate the old setup script created outside Terraform. Unattached static IPs keep billing. +5. **Switch prod** the same way, with a Worker PR that adds `gbfs.api.mobilitydatabase.org`. - ```bash - gcloud compute addresses delete dev-lb-ipv4 dev-lb-ipv6 --global --project gbfs-validator-staging - gcloud compute ssl-certificates delete dev-gbfs-api-mobilitydatabase-org --global --project gbfs-validator-staging - ``` - -3. Remove `roles/compute.loadBalancerAdmin` and `roles/compute.securityAdmin` from both deployer service accounts and from `scripts/setup-deployer-sa-permissions.sh`. +## After -4. Budget alerts, set on the billing account (the deployer service account has no billing permissions on purpose). The amount must be in the billing account's currency (CAD): - - ```bash - gcloud billing budgets create --billing-account= --display-name="gbfs-validator-prod" --budget-amount=25CAD --filter-projects=projects/gbfs-validator-prod --threshold-rule=percent=0.5 --threshold-rule=percent=1.0 - gcloud billing budgets create --billing-account= --display-name="gbfs-validator-staging" --budget-amount=15CAD --filter-projects=projects/gbfs-validator-staging --threshold-rule=percent=0.5 --threshold-rule=percent=1.0 - ``` +- [ ] Set `legacy_load_balancer_enabled = false` and deploy. The run.app URL then returns 403 to anyone but the Worker. +- [ ] Delete the old static IPs and certificates (they keep billing): -## Internal addresses and local files + ```bash + gcloud compute addresses delete dev-lb-ipv4 dev-lb-ipv6 --global --project gbfs-validator-staging + gcloud compute ssl-certificates delete dev-gbfs-api-mobilitydatabase-org --global --project gbfs-validator-staging + ``` -The Worker only sees the top-level `feedUrl`. The validator also fetches every URL listed in the discovery file, the OAuth token URL and redirect targets, and it used to read `file:` URLs, so a public discovery file could point it at internal services or local files. The validator's loader closes this with `loader.public-network-only` (on by default in the API, from the gbfs-validator-java release that includes it): every host is resolved through a resolver that rejects private, loopback and link-local addresses, the connection uses the checked address, and `file:` URLs are refused. Deploy that release (`app_version`) before or together with this cutover. +- [ ] Remove `roles/compute.loadBalancerAdmin` and `roles/compute.securityAdmin` from the deployer service accounts. +- [ ] Add budget alerts (25 CAD prod, 15 CAD staging): -Not covered yet: the size of a downloaded file is not capped. + ```bash + gcloud billing budgets create --billing-account= --display-name="gbfs-validator-prod" --budget-amount=25CAD --filter-projects=projects/gbfs-validator-prod --threshold-rule=percent=0.5 --threshold-rule=percent=1.0 + ``` From 5a5a4e00305b6ab70f5ec53d2ebf1bc465716746 Mon Sep 17 00:00:00 2001 From: David Gamez Diaz <1192523+davidgamez@users.noreply.github.com> Date: Thu, 26 Nov 2026 14:56:00 -0500 Subject: [PATCH 7/8] feat: size Cloud Run for 10 validations per instance and address audit Run 10 validations per 4 vCPU / 4 GiB instance (5 in prod, 1 in dev/qa) with a 90 s timeout and startup CPU boost. Runbook: budget alerts before the cutover, rollback step, key cleanup and rotation, cleanup for all environments. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- docs/edge-cutover.md | 33 ++++++++++++----------- gbfs-validator/function_config.json | 10 +++---- infra/cloud-run-service/cloud-run-main.tf | 6 +++-- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 48d465c..cf8ed50 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ client -> Cloudflare (mobilitydatabase.org zone) -> Worker mobilitydata-sites -> ``` - The public hostnames are served by the `mobilitydata-sites` Cloudflare Worker ([MobilityData/org-cloudflare-workers](https://github.com/MobilityData/org-cloudflare-workers), `sites/gbfs.api.mobilitydatabase.org/`). It accepts only `POST /validate` (plus the CORS preflight), checks the request, rate limits per IP and forwards it with an ID token of the `gbfs-edge-{env}` service account. Everything else gets a 404 at the edge and never starts a Cloud Run instance. -- Once the legacy load balancer is removed (follow-up change), Cloud Run only accepts requests signed by `gbfs-edge-{env}` (`roles/run.invoker`); until then the public `allUsers` invoker stays and the run.app URL is publicly reachable. Cloud Run uses request-based billing and is capped at 10 instances of 5 concurrent validations in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). Request limits and capacity: [docs/edge-cutover.md](docs/edge-cutover.md#limits-and-capacity). The validator's built-in Swagger UI is turned off; the API docs are on [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html). +- Once the legacy load balancer is removed (follow-up change), Cloud Run only accepts requests signed by `gbfs-edge-{env}` (`roles/run.invoker`); until then the public `allUsers` invoker stays and the run.app URL is publicly reachable. Cloud Run uses request-based billing and is capped at 5 instances of 10 concurrent validations (4 vCPU, 4 GiB) in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). Request limits and capacity: [docs/edge-cutover.md](docs/edge-cutover.md#limits-and-capacity). The validator's built-in Swagger UI is turned off; the API docs are on [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html). - The GCP load balancer and Cloud Armor are kept only during the cutover (`legacy_load_balancer_enabled`) and removed in a follow-up change. See [docs/edge-cutover.md](docs/edge-cutover.md) for the two-step rollout and the manual steps (Worker secrets, DNS, cleanup). ### Manual deployment diff --git a/docs/edge-cutover.md b/docs/edge-cutover.md index a5bfa85..bca6745 100644 --- a/docs/edge-cutover.md +++ b/docs/edge-cutover.md @@ -16,14 +16,14 @@ The Worker ([org-cloudflare-workers](https://github.com/MobilityData/org-cloudfl | Limit | Value | Change it in | |---|---|---| -| Requests per IP | 5/min, then 429 | Worker `wrangler.jsonc` | +| Requests per IP | 5/min per Cloudflare location, then 429 | Worker `wrangler.jsonc` | | Request body | 16 KB, then 413 | Worker `handler.js` | -| Validations per instance | 5 at once | `gbfs-validator/function_config.json` | -| Instances | prod 10, dev/qa 1 | same file | -| Instance size | 1 vCPU, 2 GiB, JVM heap 75% | same file | -| Timeout | 60 s | same file | +| Validations per instance | 10 at once | `gbfs-validator/function_config.json` | +| Instances | prod 5 (50 at once), dev/qa 1 (10 at once) | same file | +| Instance size | 4 vCPU, 4 GiB, JVM heap 75% | same file | +| Timeout | 90 s (Cloudflare cuts at 100 s) | same file | -Why 2 GiB and heap 75%: without the heap setting Java uses only 25% of memory and large feeds (8-12 MB) failed with `OutOfMemoryError`. At 2 GiB, 8 large validations at once used 1.75 GiB. +Why: without the heap setting Java uses only 25% of memory and large feeds (8-12 MB) failed with `OutOfMemoryError`; 8 large validations used 1.75 GiB, so 10 need about 2.2 GiB (4 GiB leaves headroom). Validation is CPU-bound: one 12 MB feed takes 12 s on 1 vCPU and 5 at once on 1 vCPU hit 504s, so 10 at once needs 4 vCPU (about 30 s worst case, not yet measured). Other changes: Swagger UI is off on Cloud Run (docs move to [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html)); `feedUrl` must be a public http(s) URL. @@ -32,12 +32,17 @@ Other changes: Swagger UI is off on Cloud Run (docs move to [GitHub Pages](https - [ ] Cloudflare, Security rules, "Challenge abusive countries": append `and not (http.host contains "gbfs.api.mobilitydatabase.org")`. - [ ] Release gbfs-validator-java with `loader.public-network-only` (blocks private addresses and local files). - [ ] Don't enable Bot Fight Mode on the zone (it would block API clients). +- [ ] Add budget alerts (25 CAD prod, 15 CAD staging); the run.app URL is public until the follow-up: + + ```bash + gcloud billing budgets create --billing-account= --display-name="gbfs-validator-prod" --budget-amount=25CAD --filter-projects=projects/gbfs-validator-prod --threshold-rule=percent=0.5 --threshold-rule=percent=1.0 + ``` ## Cutover 1. **Deploy this repo** to dev, qa and prod (prod with the new `app_version`). Nothing goes down: the old load balancer stays while `legacy_load_balancer_enabled = true`. -2. **Add Worker secrets** (once per env; `DEV` shown): +2. **Add Worker secrets** (once per env; `DEV` shown). If `wrangler` fails, delete the key it created (`gcloud iam service-accounts keys list --iam-account=...`). To rotate: new key, update the secret, delete the old key. ```bash gcloud iam service-accounts keys create /dev/stdout --iam-account=gbfs-edge-dev@gbfs-validator-staging.iam.gserviceaccount.com | npx wrangler secret put GBFS_VALIDATOR_DEV_SA_KEY --name mobilitydata-sites @@ -55,21 +60,19 @@ Other changes: Swagger UI is off on Cloud Run (docs move to [GitHub Pages](https Expect `404`, then a validation report. + **Rollback** if the Worker deploy fails: recreate the grey-cloud (DNS only) A/AAAA records with the addresses of `{env}-lb-ipv4` / `{env}-lb-ipv6` (`gcloud compute addresses list --global --project `). + 5. **Switch prod** the same way, with a Worker PR that adds `gbfs.api.mobilitydatabase.org`. ## After - [ ] Set `legacy_load_balancer_enabled = false` and deploy. The run.app URL then returns 403 to anyone but the Worker. -- [ ] Delete the old static IPs and certificates (they keep billing): +- [ ] Delete the old static IPs and certificates (they keep billing), then remove their creation from `scripts/setup-environment.sh` (rerunning it recreates them): ```bash - gcloud compute addresses delete dev-lb-ipv4 dev-lb-ipv6 --global --project gbfs-validator-staging - gcloud compute ssl-certificates delete dev-gbfs-api-mobilitydatabase-org --global --project gbfs-validator-staging + for env in dev qa; do gcloud compute addresses delete $env-lb-ipv4 $env-lb-ipv6 --global --project gbfs-validator-staging; gcloud compute ssl-certificates delete $env-gbfs-api-mobilitydatabase-org --global --project gbfs-validator-staging; done + gcloud compute addresses delete prod-lb-ipv4 prod-lb-ipv6 --global --project gbfs-validator-prod + gcloud compute ssl-certificates delete prod-gbfs-api-mobilitydatabase-org --global --project gbfs-validator-prod ``` - [ ] Remove `roles/compute.loadBalancerAdmin` and `roles/compute.securityAdmin` from the deployer service accounts. -- [ ] Add budget alerts (25 CAD prod, 15 CAD staging): - - ```bash - gcloud billing budgets create --billing-account= --display-name="gbfs-validator-prod" --budget-amount=25CAD --filter-projects=projects/gbfs-validator-prod --threshold-rule=percent=0.5 --threshold-rule=percent=1.0 - ``` diff --git a/gbfs-validator/function_config.json b/gbfs-validator/function_config.json index ec0a8df..d479215 100644 --- a/gbfs-validator/function_config.json +++ b/gbfs-validator/function_config.json @@ -2,14 +2,14 @@ "name_suffix": "gbfs-validator-api", "description": "API containing the GBFS validator", "entry_point": "org.entur.gbfs.validator.api.handler.OpenApiGeneratorApplication", - "timeout": 60, - "available_memory": "2Gi", + "timeout": 90, + "available_memory": "4Gi", "trigger_http": true, "ingress_settings": "ALLOW_ALL", - "max_instance_request_concurrency": 5, - "max_instance_count": 10, + "max_instance_request_concurrency": 10, + "max_instance_count": 5, "max_instance_count_non_prod": 1, "min_instance_count": 0, - "available_cpu": 1, + "available_cpu": 4, "java_tool_options": "-XX:MaxRAMPercentage=75" } diff --git a/infra/cloud-run-service/cloud-run-main.tf b/infra/cloud-run-service/cloud-run-main.tf index 8ce607f..97a765e 100644 --- a/infra/cloud-run-service/cloud-run-main.tf +++ b/infra/cloud-run-service/cloud-run-main.tf @@ -30,8 +30,8 @@ terraform { locals { gbfs_validator_config = jsondecode(file("${path.module}/../../gbfs-validator/function_config.json")) artifact_registry_repo = "${var.artifact_registry_repo}/${var.environment}" - # The instance cap is the hard cost ceiling: with request-based billing an instance costs - # nothing while idle, and at most max_instance_count instances can ever be billed at once. + # The instance cap bounds compute spend: with request-based billing an idle instance costs + # nothing. It does not cap egress or logging, and Cloud Run can briefly exceed it during a rollout. max_instance_count = var.environment == "prod" ? local.gbfs_validator_config.max_instance_count : local.gbfs_validator_config.max_instance_count_non_prod } @@ -76,6 +76,8 @@ resource "google_cloud_run_v2_service" "gbfs_validator_api" { # Request-based billing: CPU is billed only while a request is processed (and during # startup). Instance-based billing kept an idle instance billed after every bot hit. cpu_idle = true + # Faster Spring Boot cold starts when scaling from zero. + startup_cpu_boost = true } # Without it the JVM caps its heap at 25% of the container memory, and large feeds fail with # OutOfMemoryError while most of the memory is unused (load test in docs/edge-cutover.md). From 6e35effc2106b21f177cefc5eb81c36dc51aec2d Mon Sep 17 00:00:00 2001 From: David Gamez Diaz <1192523+davidgamez@users.noreply.github.com> Date: Thu, 26 Nov 2026 15:12:06 -0500 Subject: [PATCH 8/8] feat: run 5 validations per instance and up to 20 prod instances 10 large feeds at once on 4 vCPU took up to 82 s against the 90 s timeout on dev. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- docs/edge-cutover.md | 6 +++--- gbfs-validator/function_config.json | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index cf8ed50..8a6c000 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ client -> Cloudflare (mobilitydatabase.org zone) -> Worker mobilitydata-sites -> ``` - The public hostnames are served by the `mobilitydata-sites` Cloudflare Worker ([MobilityData/org-cloudflare-workers](https://github.com/MobilityData/org-cloudflare-workers), `sites/gbfs.api.mobilitydatabase.org/`). It accepts only `POST /validate` (plus the CORS preflight), checks the request, rate limits per IP and forwards it with an ID token of the `gbfs-edge-{env}` service account. Everything else gets a 404 at the edge and never starts a Cloud Run instance. -- Once the legacy load balancer is removed (follow-up change), Cloud Run only accepts requests signed by `gbfs-edge-{env}` (`roles/run.invoker`); until then the public `allUsers` invoker stays and the run.app URL is publicly reachable. Cloud Run uses request-based billing and is capped at 5 instances of 10 concurrent validations (4 vCPU, 4 GiB) in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). Request limits and capacity: [docs/edge-cutover.md](docs/edge-cutover.md#limits-and-capacity). The validator's built-in Swagger UI is turned off; the API docs are on [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html). +- Once the legacy load balancer is removed (follow-up change), Cloud Run only accepts requests signed by `gbfs-edge-{env}` (`roles/run.invoker`); until then the public `allUsers` invoker stays and the run.app URL is publicly reachable. Cloud Run uses request-based billing and is capped at 20 instances of 5 concurrent validations (4 vCPU, 4 GiB) in prod and 1 in dev/qa (`gbfs-validator/function_config.json`). Request limits and capacity: [docs/edge-cutover.md](docs/edge-cutover.md#limits-and-capacity). The validator's built-in Swagger UI is turned off; the API docs are on [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html). - The GCP load balancer and Cloud Armor are kept only during the cutover (`legacy_load_balancer_enabled`) and removed in a follow-up change. See [docs/edge-cutover.md](docs/edge-cutover.md) for the two-step rollout and the manual steps (Worker secrets, DNS, cleanup). ### Manual deployment diff --git a/docs/edge-cutover.md b/docs/edge-cutover.md index bca6745..4d3df21 100644 --- a/docs/edge-cutover.md +++ b/docs/edge-cutover.md @@ -18,12 +18,12 @@ The Worker ([org-cloudflare-workers](https://github.com/MobilityData/org-cloudfl |---|---|---| | Requests per IP | 5/min per Cloudflare location, then 429 | Worker `wrangler.jsonc` | | Request body | 16 KB, then 413 | Worker `handler.js` | -| Validations per instance | 10 at once | `gbfs-validator/function_config.json` | -| Instances | prod 5 (50 at once), dev/qa 1 (10 at once) | same file | +| Validations per instance | 5 at once | `gbfs-validator/function_config.json` | +| Instances | prod 20 (100 at once), dev/qa 1 (5 at once) | same file | | Instance size | 4 vCPU, 4 GiB, JVM heap 75% | same file | | Timeout | 90 s (Cloudflare cuts at 100 s) | same file | -Why: without the heap setting Java uses only 25% of memory and large feeds (8-12 MB) failed with `OutOfMemoryError`; 8 large validations used 1.75 GiB, so 10 need about 2.2 GiB (4 GiB leaves headroom). Validation is CPU-bound: one 12 MB feed takes 12 s on 1 vCPU and 5 at once on 1 vCPU hit 504s, so 10 at once needs 4 vCPU (about 30 s worst case, not yet measured). +Why: without the heap setting Java uses only 25% of memory and large feeds (8-12 MB) failed with `OutOfMemoryError`; 8 large validations used 1.75 GiB, so 4 GiB leaves headroom. Validation is CPU-bound (about 12 s of CPU per 12 MB feed): on 4 vCPU, 10 large feeds at once took up to 82 s against the 90 s timeout, so 5 per instance. Other changes: Swagger UI is off on Cloud Run (docs move to [GitHub Pages](https://mobilitydata.github.io/gbfs-validator-java/SwaggerUI/index.html)); `feedUrl` must be a public http(s) URL. diff --git a/gbfs-validator/function_config.json b/gbfs-validator/function_config.json index d479215..2c3e10e 100644 --- a/gbfs-validator/function_config.json +++ b/gbfs-validator/function_config.json @@ -6,8 +6,8 @@ "available_memory": "4Gi", "trigger_http": true, "ingress_settings": "ALLOW_ALL", - "max_instance_request_concurrency": 10, - "max_instance_count": 5, + "max_instance_request_concurrency": 5, + "max_instance_count": 20, "max_instance_count_non_prod": 1, "min_instance_count": 0, "available_cpu": 4,