feat(storage): add QueueBatchStateStore and shared batch state helpers - #523
Merged
Conversation
## Summary ### Why? `BatchStore.GetByQueueAndStates` is the storage contract's only query-by-attribute, backed by `idx_queue_state` — the only secondary index in the entire schema. The storage README's key-value contract flags exactly this shape: a genuinely needed reverse lookup should be a first-class mapping store keyed by the lookup attribute, so any backend (SQL, DynamoDB, Bigtable) can serve it as a primary-key read. This PR adds that store and the shared helpers controllers will migrate onto; `speculate/run.go` already carries a TODO for this replacement. ### What? - `entity.QueueBatchState` — an advisory membership record filing an in-queue batch under a (queue, state) bucket, existing from batch creation until the batch exits the queue (through terminal states, hence not "active"). Also adds `entity.AllBatchStates()` for the future conclude-time sweep. - `storage.QueueBatchStateStore` — `List(queue, state)` / `Put` / `Delete`, all idempotent. The MySQL impl is backed by a new `queue_batch_state` table whose PK (queue, state, batch_id) *is* the lookup: listing a state bucket is a PK-prefix scan, no secondary index. - `submitqueue/core/batch` — the shared protocol primitives: `Transition` (batch CAS, then Put the new-bucket record before Deleting the old one, so a batch always has at least one record), `EnsureRecord` (idempotent repair for redelivery skip branches), and `ListByStates` (scan requested buckets, dedupe, hydrate by key with bounded concurrency, classify by the hydrated authoritative state — a stale record can misplace a batch but never misreport it). - Contract-suite coverage for the new store and a README pointer beside the `ChangeRecord` mapping-store example. Scoped to the foundation only: the store has no callers yet, so there is no runtime behavior change. Follow-ups migrate the six batch-transition sites and the two `GetByQueueAndStates` readers onto the helpers, add conclude-time record deletion, and then remove `GetByQueueAndStates` + `idx_queue_state`. ## Test Plan - ✅ `bazel test //submitqueue/entity:all //submitqueue/core/batch:all //submitqueue/extension/storage/...` - ✅ `bazel test //test/integration/submitqueue/extension/storage/mysql:go_default_test` (real MySQL; includes the new `TestStorage_QueueBatchStateRecordLifecycle` contract case) - ✅ `make tidy` / `make gazelle` / `make fmt` / `make mocks` — all stable (re-running produces no diffs) - Note: `make test` failures in `orchestrator/controller/batch` and `controller/cancel` are pre-existing — they reproduce at clean HEAD db51df4 in a fresh worktree.
behinddwalls
force-pushed
the
preetam/active-batch
branch
from
August 6, 2026 01:15
6c449c2 to
1608170
Compare
ubettigole
approved these changes
Aug 6, 2026
ubettigole
left a comment
Contributor
There was a problem hiding this comment.
Looks good, just one small comment
behinddwalls
temporarily deployed
to
stack-rebase
August 6, 2026 14:27 — with
GitHub Actions
Inactive
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.
Summary
Why?
BatchStore.GetByQueueAndStatesis the storage contract's only query-by-attribute, backed byidx_queue_state— the only secondary index in the entire schema. The storage README's key-value contract flags exactly this shape: a genuinely needed reverse lookup should be a first-class mapping store keyed by the lookup attribute, so any backend (SQL, DynamoDB, Bigtable) can serve it as a primary-key read. This PR adds that store and the shared helpers controllers will migrate onto;speculate/run.goalready carries a TODO for this replacement.What?
entity.QueueBatchState— an advisory membership record filing an in-queue batch under a (queue, state) bucket, existing from batch creation until the batch exits the queue (through terminal states, hence not "active"). Also addsentity.AllBatchStates()for the future conclude-time sweep.storage.QueueBatchStateStore—List(queue, state)/Put/Delete, all idempotent. The MySQL impl is backed by a newqueue_batch_statetable whose PK (queue, state, batch_id) is the lookup: listing a state bucket is a PK-prefix scan, no secondary index.submitqueue/core/batch— the shared protocol primitives:Transition(batch CAS, then Put the new-bucket record before Deleting the old one, so a batch always has at least one record),EnsureRecord(idempotent repair for redelivery skip branches), andListByStates(scan requested buckets, dedupe, hydrate by key with bounded concurrency, classify by the hydrated authoritative state — a stale record can misplace a batch but never misreport it).ChangeRecordmapping-store example.Scoped to the foundation only: the store has no callers yet, so there is no runtime behavior change. Follow-ups migrate the six batch-transition sites and the two
GetByQueueAndStatesreaders onto the helpers, add conclude-time record deletion, and then removeGetByQueueAndStates+idx_queue_state.Test Plan
bazel test //submitqueue/entity:all //submitqueue/core/batch:all //submitqueue/extension/storage/...bazel test //test/integration/submitqueue/extension/storage/mysql:go_default_test(real MySQL; includes the newTestStorage_QueueBatchStateRecordLifecyclecontract case)make tidy/make gazelle/make fmt/make mocks— all stable (re-running produces no diffs)make testfailures inorchestrator/controller/batchandcontroller/cancelare pre-existing — they reproduce at clean HEAD db51df4 in a fresh worktree.