Skip to content

feat: resume Snowflake jobs by query tag (AWOL plan, phase 5) - #173

Open
prasadlohakpure wants to merge 11 commits into
mainfrom
awol/phase-5-snowflake-resume
Open

prasadlohakpure wants to merge 11 commits into
mainfrom
awol/phase-5-snowflake-resume

Conversation

@prasadlohakpure

@prasadlohakpure prasadlohakpure commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

AWOL-on-rollout plan

  • Phase 1: make SIGTERM reach Heimdall (#161)
  • Phase 2: drain on shutdown (#161)
  • Phase 3: reattach instead of fail, plugin.Resumer for sparkeks and ecs (#162)
  • Phase 4: release resumable jobs after 45 s, fail them as AWOL after awol_timeout (#164)
  • Phase 5 (this PR): resume Snowflake jobs by query tag, cancel orphaned Snowflake queries in Cleanup

Why

A Snowflake job's query lives in the agent's connection. When the agent dies (for example at the drain deadline of a rollout), the job is failed as AWOL after 45 s, but Snowflake keeps running the query: it doesn't abort a query when its client disconnects unless ABORT_DETACHED_QUERY is set, and the default is off. Cleanup did nothing, so the orphaned query kept the warehouse busy until STATEMENT_TIMEOUT_IN_SECONDS (two days by default), and its result was thrown away.

What changes

  • Query tag: every Snowflake query runs with QUERY_TAG = heimdall-job-<job_id> (sf.WithQueryTag), the same marker style as ECS (startedBy) and Spark (spark-sql-job-<id>).
  • Small framework change. Snowflake implements the existing plugin.Resumer interface, the same one sparkeks and ecs use, so the janitor releases its stale jobs and runJob calls Resume. plugin.go and the janitor are identical to main; the only framework change is the Runtime.WriteResult field below.
  • Snowflake Resume:
    1. Finds the job's query with select query_id from table(information_schema.query_history_by_user(end_time_range_start => to_timestamp_ltz(<job created_at>), result_limit => 10000)) where query_tag = ?. The table function only returns queries that ended after the job was created or are still running. It applies result_limit before the where clause (the default is 100), so the limit is set to the maximum and only matters if the user runs more than 10,000 queries during one job.
    2. Found: fetches the rows by query ID (sf.WithFetchResultByID). The driver polls a running query until it finishes; a failed or canceled query returns its error and the job fails with it.
    3. Not found: the agent died before submitting the query, so it runs now (tagged as usual). DML (INSERT, MERGE, COPY) only runs again in this case, so it can't apply twice.
  • Result file: Resume streams the job's result through Runtime.WriteResult, a new function field on plugin.Runtime that runJob sets to heimdall's own writeResultToDestination (3 lines in runJob, 1 field in runtime.go). A resumed job writes <result dir>/result.json (to S3 when the result directory is an s3:// path) through exactly the same code as a normal run, replacing anything the dead agent half-wrote. The normal StreamResult path is unchanged.
  • Snowflake Cleanup: finds the job's query by the same tag, with the window starting now so only a still-running query matches, and cancels it (system$cancel_query). It runs when the janitor fails a job as AWOL or finishes a cancel, and after a failed resume.
  • StreamResult is unchanged apart from the query tag on its QueryContext call. Resume and Cleanup open their own connection.

Behaviour per scenario (Snowflake)

Agent dies while Before After
accepted, query not submitted failed as AWOL after 45 s released after 45 s, next agent runs it from the start (StreamResult)
running, query running in Snowflake failed after 45 s, query keeps running up to 2 days released after 45 s, next agent finds it by tag and waits for its result
running, query already finished failed after 45 s, result lost released, next agent fetches the result by query ID (Snowflake keeps results 24 h)
running, query failed or canceled failed after 45 s released, next agent fails the job with Snowflake's error
running, query not yet submitted failed after 45 s released, next agent runs it
no agent picks it up within awol_timeout (1 h) n/a failed as AWOL; Cleanup cancels the query
job canceled after its agent died canceled, query keeps running canceled; Cleanup cancels the query

Testing

  • go test ./internal/pkg/janitor/ ./internal/pkg/object/command/... ./pkg/... passes, including TestStaleJobs against a local Postgres.
  • snowflake_test.go checks the plugin implements plugin.Resumer and plugin.ResultStreamer, and that the query tag is heimdall-job-<job_id>.
  • Not run against a real Snowflake account (no key locally). Before merging, on the dev cluster (snowflake-heimdall-wh-dev):
    1. Run a slow query (select system$wait(120)) as an async job, kill the agent with kill -9 while it runs, start another agent: the job should be resumed and succeed with the query's result, and query history should show one query with the tag.
    2. Same, but let the query finish before the second agent starts: the result is fetched by query ID, no second query in history.
    3. Kill the agent before any agent can resume and cancel the job: Cleanup cancels the query.
    4. Confirm information_schema.query_history_by_user works for heimdall__app_user with iceberg_db as the current database, and accepts the bound end_time_range_start.

Known gaps and checks

  • ABORT_DETACHED_QUERY: if it is on for the account or the user, Snowflake aborts the query 5 minutes after the client disconnects. The resume then fails with that error instead of the result. Releases happen within 2 minutes, so the next agent usually reattaches before that; check the setting.
  • QUERY_TAG overrides: the tag replaces any user- or account-level QUERY_TAG for these queries. Check nothing relies on it for cost attribution.
  • More than 10,000 queries by the cluster's user since the job started: the lookup can miss the query and run it again.
  • Results older than 24 h can't be fetched by ID; not reachable today because jobs are failed as AWOL after 1 h.
  • Cleanup opens a Snowflake connection for every AWOL or canceled Snowflake job.

Copilot AI balanced review requested due to automatic review settings November 27, 2026 14:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Resumption can replay untagged queries from the previous agent version and duplicate writes, while lock-blocked queries are incorrectly treated as failures and canceled.

2 open issues
What changed in this PR

Extends Heimdall’s rollout recovery to Snowflake with query-tag-based resumption, streamed result recovery, and cleanup of orphaned queries.

Changes:

  • Adds StreamResumer and integrates it with job execution and janitor release decisions.
  • Tags Snowflake queries, retrieves results by query ID, and cancels unfinished queries during cleanup.
  • Adds tests for resumability, query tags, and history-query formatting.
File Description
pkg/​plugin/​plugin.go Defines streaming resumption and shared resumability detection.
internal/​pkg/​object/​command/​snowflake/​snowflake.go Implements tagged queries, result recovery, and orphan cleanup.
internal/​pkg/​object/​command/​snowflake/​snowflake_test.go Checks resumability, tags, and history-query formatting.
internal/​pkg/​janitor/​job.go Recognizes streaming plugins when releasing stale jobs.
internal/​pkg/​janitor/​job_test.go Tests release classification for a streaming resumer.
internal/​pkg/​heimdall/​job.go Routes resumed streams through the result destination writer.

Review effort: Balanced

var queryID string
err = db.QueryRowContext(ctx, fmt.Sprintf(queryHistorySelect, j.CreatedAt-queryHistorySlack), queryTag(j.ID)).Scan(&queryID)
if errors.Is(err, sql.ErrNoRows) {
return streamQuery(ctx, w, db, j.ID, jobContext.Query)
}

// the driver polls a running query until it finishes; a failed or canceled query returns its error
rows, err := db.QueryContext(sf.WithFetchResultByID(ctx, queryID), ``)
@wiz-55ccc8b716

wiz-55ccc8b716 Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Wiz Scan Summary

Scanner Findings
Vulnerability Finding Vulnerabilities -
Data Finding Sensitive Data -
Secret Finding Secrets -
IaC Misconfiguration IaC Misconfigurations -
SAST Finding SAST Findings -
Software Management Finding Software Management Findings -
Total -

View scan details in Wiz

To detect these findings earlier in the dev lifecycle, try the Wiz Code extension for VS Code, JetBrains, or Visual Studio.

Comment thread pkg/plugin/runtime.go Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants