Poll of a worker below the secrets version floor scans every queued task that uses stored secrets

Open
#72 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
fastapi, postgresql, python

Research direction

Start with the claim picks and _TASK_USES_STORED_SECRETS in server/orcaster_server/api/poll.py, then read the query in server/orcaster_server/task_secrets/gate.py. First add a reproducible benchmark for current and below-floor workers that records poll time and EXPLAIN (ANALYZE, BUFFERS); use its results to decide whether to add a schema option. Done means the benchmark can be rerun, slow polls are logged and tested, and any chosen schema option meets the compatibility and migration criteria with a CHANGELOG entry.

Written by the indexing model from the issue text.

Description

enhancement P2 question server

Problem

A worker whose version is below the task secrets floor must not receive tasks that use stored secrets. The three claim picks in server/orcaster_server/api/poll.py enforce this with a predicate under :exclude_stored_secrets (:235-236, :296-297, :401-402).

That predicate, _TASK_USES_STORED_SECRETS (server/orcaster_server/api/poll.py:185), expands to an EXISTS subquery on task_secrets for every candidate row (server/orcaster_server/task_secrets/gate.py:110-139). It looks inside the JSONB payload.task_env of each task, so no index can serve it.

A current worker stops at the first candidate in priority, run_at order. A below-floor worker rejects every queued task that uses stored secrets, one row at a time, before it finds a task it can take or returns no task. The cost of each idle poll of such a worker therefore grows in a straight line with the number of queued tasks that use stored secrets. Every old worker pays it on every idle poll.

The same subquery also runs for every push pick in a busy poll (server/orcaster_server/api/poll.py:1066-1068).

Proposed change

  1. Measure first. Add a reproducible benchmark (test or script) that fills a test database with a large backlog of queued tasks using stored secrets and records EXPLAIN (ANALYZE, BUFFERS) and the poll time for a current worker and a below-floor worker.
  2. Make poll cost visible. Log the poll duration at DEBUG, and log a WARNING when a single poll takes longer than a threshold, with the worker id and whether exclude_stored_secrets was set.
  3. Only if the measurement shows the scan dominates, pick one of the schema options in Open questions.

Compatibility

Steps 1 and 2 do not change the poll result, the API, the database schema or any setting. Existing deployments and workers see no difference.

If a schema option is chosen:

  • The migration is additive: a new nullable column or a new index. Old code ignores it.
  • A NULL column means "unknown", never "declares nothing". Tasks queued before the migration, or created by an old server after a rollback, have NULL. For them a below-floor worker still runs the EXISTS predicate, so the gate (server/orcaster_server/api/poll.py:235-236) is never bypassed. A backfill in the migration is optional and does not remove this rule.
  • Migrations run in one transaction under an advisory lock (server/orcaster_server/migrations/env.py:55-63), and by default they run at server startup (MIGRATE_ON_STARTUP, server/orcaster_server/config.py:151). CREATE INDEX CONCURRENTLY cannot run there. The index uses plain CREATE INDEX IF NOT EXISTS with SET LOCAL lock_timeout, one statement per op.execute, like server/orcaster_server/migrations/versions/0061_te_worker_assigned_idx.py.
  • Lock cost: the index build takes a SHARE lock on tasks. It blocks writes, not reads, for the length of the build, at startup before the server serves traffic. The benchmark from step 1 records the build time on a large table, and the release note states it. A backfill, if any, runs in the same transaction, so its row locks and its time are measured and stated the same way.
  • The poll result is the same before and after for every worker (same tasks offered, same order).

Acceptance criteria

  • A benchmark test or script measures the open claim query for a current worker and a below-floor worker with a large backlog, and it can be re-run.
  • Poll duration is logged, with a WARNING above a documented threshold (test for the WARNING).
  • If a schema option ships: an Alembic migration that is additive, builds its index with plain CREATE INDEX IF NOT EXISTS and SET LOCAL lock_timeout, and runs under MIGRATE_ON_STARTUP at server startup (test), plus a test that a below-floor worker and a current worker get the same tasks as before, and a test that a queued task with a NULL column that uses stored secrets is still withheld from a below-floor worker.
  • Existing deployments keep working without config changes, and the poll response format is unchanged.
  • CHANGELOG entry under Unreleased.

Open questions

Which schema option, if any, after the measurement?

  • Option 1: static column. At task creation, store whether payload.task_env declares at least one name (the same rule as payload_declares_task_env_sql, server/orcaster_server/task_secrets/gate.py:142). Add a partial index on queued tasks for it. A below-floor worker can skip tasks that declare nothing without the EXISTS. The value never changes once set, so it cannot drift. NULL rows fall back to the EXISTS (see Compatibility). The scan stays linear in the number of tasks that declare a name.
  • Option 2: maintained flag. Store uses_stored_secrets and recompute it for queued tasks when a secret is set or deleted. Fastest poll, but it adds writes to the secret paths and the flag can drift, so it needs a repair step.
  • Option 3: no schema change. Keep only steps 1 and 2.

Option 3 changes nothing for existing deployments. Option 1 is the safer schema option, because the column is set once, an old server ignores it, and NULL falls back to today's check.

Dominant language
Python
Stars
0
Forks
0
Avg merge
1d 1h
Merged PRs (30d)
10

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from AInvirion/orcaster

All issues in AInvirion/orcaster

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.