Repository navigation
feat: resume Snowflake jobs by query tag (AWOL plan, phase 5) - #173
Open
prasadlohakpure wants to merge 11 commits into
Open
prasadlohakpure wants to merge 11 commits into
prasadlohakpure wants to merge 11 commits into
Conversation
…in Cleanup (AWOL plan, phase 5)
Contributor
There was a problem hiding this comment.
🟡 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
StreamResumerand 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 Scan Summary
To detect these findings earlier in the dev lifecycle, try the Wiz Code extension for VS Code, JetBrains, or Visual Studio. |
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.


AWOL-on-rollout plan
plugin.Resumerforsparkeksandecs(#162)awol_timeout(#164)CleanupWhy
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_QUERYis set, and the default is off.Cleanupdid nothing, so the orphaned query kept the warehouse busy untilSTATEMENT_TIMEOUT_IN_SECONDS(two days by default), and its result was thrown away.What changes
QUERY_TAG = heimdall-job-<job_id>(sf.WithQueryTag), the same marker style as ECS (startedBy) and Spark (spark-sql-job-<id>).plugin.Resumerinterface, the same onesparkeksandecsuse, so the janitor releases its stale jobs andrunJobcallsResume.plugin.goand the janitor are identical to main; the only framework change is theRuntime.WriteResultfield below.Resume: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 appliesresult_limitbefore thewhereclause (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.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.INSERT,MERGE,COPY) only runs again in this case, so it can't apply twice.Resumestreams the job's result throughRuntime.WriteResult, a new function field onplugin.RuntimethatrunJobsets to heimdall's ownwriteResultToDestination(3 lines inrunJob, 1 field inruntime.go). A resumed job writes<result dir>/result.json(to S3 when the result directory is ans3://path) through exactly the same code as a normal run, replacing anything the dead agent half-wrote. The normalStreamResultpath is unchanged.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.StreamResultis unchanged apart from the query tag on itsQueryContextcall.ResumeandCleanupopen their own connection.Behaviour per scenario (Snowflake)
accepted, query not submittedStreamResult)running, query running in Snowflakerunning, query already finishedrunning, query failed or canceledrunning, query not yet submittedawol_timeout(1 h)Cleanupcancels the queryCleanupcancels the queryTesting
go test ./internal/pkg/janitor/ ./internal/pkg/object/command/... ./pkg/...passes, includingTestStaleJobsagainst a local Postgres.snowflake_test.gochecks the plugin implementsplugin.Resumerandplugin.ResultStreamer, and that the query tag isheimdall-job-<job_id>.snowflake-heimdall-wh-dev):select system$wait(120)) as an async job, kill the agent withkill -9while 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.Cleanupcancels the query.information_schema.query_history_by_userworks forheimdall__app_userwithiceberg_dbas the current database, and accepts the boundend_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_TAGoverrides: the tag replaces any user- or account-levelQUERY_TAGfor these queries. Check nothing relies on it for cost attribution.Cleanupopens a Snowflake connection for every AWOL or canceled Snowflake job.