refactor(bigquery): route via SET @@reservation instead of multi-project#185
Draft
MaxHalford wants to merge 1 commit intomainfrom
Draft
refactor(bigquery): route via SET @@reservation instead of multi-project#185MaxHalford wants to merge 1 commit intomainfrom
MaxHalford wants to merge 1 commit intomainfrom
Conversation
BigQuery now supports picking a reservation per query via the `@@reservation` system variable (see https://docs.cloud.google.com/bigquery/docs/reservations-assignments#sql_3). Previously we juggled several `bigquery.Client` instances to route queries to different compute projects that had reservation assignments. With this change there's a single client and a single compute project; reservations are chosen via SQL. - Replace `LEA_BQ_SCRIPT_SPECIFIC_COMPUTE_PROJECT_IDS` with `LEA_BQ_SCRIPT_SPECIFIC_RESERVATIONS` (value: `"none"` or a full `projects/.../reservations/...` path). - Replace Big Blue Pick API's two on-demand/reservation project-id vars with a single `LEA_BQ_BIG_BLUE_PICK_API_RESERVATION`. On-demand is hardcoded as `"none"` (the only legal value for @@reservation). - Static `LEA_BQ_SCRIPT_SPECIFIC_RESERVATIONS` takes priority over Pick API, so deliberately routed scripts aren't re-routed behind the user's back. - Drop the `self.clients` dict and `default_client` property; drop `BigQueryClient`'s inheritance from `BigBluePickAPI` (composition). - Fix `BigQueryJob.metadata`: it was reading the non-populated `statistics.reservationUsage[0].name` field; actual BQ jobs expose `statistics.reservation_id`. The log line now correctly reports `reservation billing (<id>)` / `on-demand billing`. - Also fix the `REVERVATION` typo in the Big Blue env var. Caveat: `SET @@reservation = 'none'` to force on-demand requires the project's `reservation_override_mode` to be configured; the valid enum value is not publicly documented. Routing to a specific reservation path works out of the box. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
BigQuery now supports setting a reservation per query via the
@@reservationsystem variable:lea previously routed queries between compute projects with different reservation assignments, so
BigQueryClientjuggled a dict ofbigquery.Clientinstances. This PR replaces that with a single client and picks reservations via SQL.Changes
BigQueryClient: singleself.client. Replacesscript_specific_compute_project_idswithscript_specific_reservations. Addsdetermine_reservation_for_scriptand a_open_session/_attach_sessionpair that emitsSET @@reservation = '<value>'inside a BQ session and carries it to the main query viasession_id. Skips the SET when a script has no override (the compute project's GCP-assigned reservation applies) and in dry-run.BigBluePickAPI: one constructor arg (reservation). ON-DEMAND picks map to'none', RESERVATION picks map to that string.BigQueryClientno longer inherits from it β composition only.LEA_BQ_SCRIPT_SPECIFIC_RESERVATIONS> Pick API > project's GCP assignment. Static overrides deliberately win so routed scripts aren't re-routed behind the user's back.LEA_BQ_SCRIPT_SPECIFIC_COMPUTE_PROJECT_IDSβLEA_BQ_SCRIPT_SPECIFIC_RESERVATIONS(value:"none"orprojects/.../reservations/...; parsed with validation so typos surface at startup).LEA_BQ_BIG_BLUE_PICK_API_{ON_DEMAND,REVERVATION}_PROJECT_IDβLEA_BQ_BIG_BLUE_PICK_API_RESERVATION(single var; fixes theREVERVATIONtypo).BigQueryJob.metadatawas readingstatistics.reservationUsage[0].name, which is not populated on real jobs β switched tostatistics.reservation_id. The log line now correctly reportsreservation billing (<id>)/on-demand billing.Caveat β overriding to on-demand is gated
During validation,
SET @@reservation = 'none'(or equivalentlybq query --reservation_id=none) onint-data-kaya-prodwas rejected:The referenced doc does not list this option or its enum; ~25 brute-force guesses all failed. Routing to a reservation path works fine out of the box; a GCP support ticket is probably needed to unlock the on-demand override. This refactor is still strictly better regardless (single client, single compute project, typo fix, fixed billing-model log line).
Test plan
uv run pytest lea/β 75/75 pass (4 new tests for reservation resolution precedence).lea run --scripts ../carbonfact/kaya/scripts --select core.accounts --dry-run --productionwithLEA_BQ_SCRIPT_SPECIFIC_RESERVATIONS={"kaya.core__accounts":"projects/int-data-kaya-prod/locations/EU/reservations/default"}β logsUsing reservation '<path>' for materializing ...andSUCCESS ... (reservation billing (int-data-kaya-prod:EU.default)).--dry-runonce the repo owner is ready.reservation_override_modeis enabled at the project/org level.Migration
Existing users must:
LEA_BQ_SCRIPT_SPECIFIC_COMPUTE_PROJECT_IDS={...}withLEA_BQ_SCRIPT_SPECIFIC_RESERVATIONS={...}whose values are"none"orprojects/.../reservations/....LEA_BQ_BIG_BLUE_PICK_API_ON_DEMAND_PROJECT_ID/LEA_BQ_BIG_BLUE_PICK_API_REVERVATION_PROJECT_IDwith a singleLEA_BQ_BIG_BLUE_PICK_API_RESERVATION=projects/.../reservations/....π€ Generated with Claude Code
Summary by cubic
Route BigQuery jobs via per-query reservations using
SET @@reservationinstead of switching compute projects. This simplifies config to a single client and improves billing visibility.Refactors
BigQueryClientand set reservations in a session (SET @@reservation), carried to the main query viasession_id.script_specific_reservations. Precedence:LEA_BQ_SCRIPT_SPECIFIC_RESERVATIONS> Big Blue Pick API > project's assignment.ON-DEMANDβ'none',RESERVATIONβLEA_BQ_BIG_BLUE_PICK_API_RESERVATION. Composition only.statistics.reservation_id; now reportsreservation billing (<id>)oron-demand billing.'none') may be disabled by your project'sreservation_override_mode.Migration
LEA_BQ_SCRIPT_SPECIFIC_COMPUTE_PROJECT_IDSwithLEA_BQ_SCRIPT_SPECIFIC_RESERVATIONSvalues:"none"orprojects/.../reservations/....LEA_BQ_BIG_BLUE_PICK_API_ON_DEMAND_PROJECT_IDandLEA_BQ_BIG_BLUE_PICK_API_REVERVATION_PROJECT_IDwithLEA_BQ_BIG_BLUE_PICK_API_RESERVATION.Written for commit 47a3a44. Summary will update on new commits.