Repository navigation
[FEATURE] Language-neutral query job lifecycle - #5818
Conversation
…s only Introduces org.opensearch.sql.job — a five-state query lifecycle (PENDING, RUNNING, SUCCEEDED, FAILED, CANCELLED) — in response to review of opensearch-project#5809. Design-only PR: adds the neutral SPI, the state machine, the registry, the OpenSearch-side lifecycle service, and their tests. No existing API is touched. PPL / SQL / transport wiring lands in a follow-up. Addresses @dai-chen's three review comments: 1. Lifecycle layer is language-neutral, Livy-style. The job package does not import PPL; each engine implements the QueryRunner SPI. 2. Execution boundary is engine-neutral. QueryRunner returns a neutral QueryResult (schema/rows/cursor/warnings/tookMillis); the same shape fits SQL and AE. 3. Execution state is decoupled from retention. State stays RUNNING regardless of whether a caller is still waiting. Service responsibilities split into QueryJobService (public interface), QueryJobStore (in-memory registry SPI), and QueryJob (state machine active object). SecurityAdapter SPI keeps OpenSearch types out of core. Module layout: core/job/ neutral interface + value types + reference in-memory store + engine SPI opensearch/job/ OpenSearchQueryJobService — symmetric with the existing OpenSearchQueryManager Public surface (all in core/job/): QueryJobService submit(runner, submitter) / get / cancel QueryJob state machine active object (5-state) QueryJobId opaque, node-routable, URL-safe QueryJobState PENDING / RUNNING / SUCCEEDED / FAILED / CANCELLED QueryJobStatus immutable snapshot with invariant checks QueryJobStore registry SPI + InMemoryQueryJobStore QueryRunner engine SPI: run() / cancel() QueryResult neutral final result QueryFailure neutral failure descriptor Principal caller identity for owner authorization SecurityAdapter SPI: runtime context -> Principal Verification (rerun against the reduced scope): ./gradlew :core:test :opensearch:test -> 39 job tests, 0 failures MVP scope intentionally excludes wait_for_completion_timeout, keep_alive, retention, admission control, and REST wiring. Every deferred feature lands behind an existing interface without touching QueryJob. Design doc: docs/dev/query-job-mvp-design.md (design/query-job-mvp branch). Signed-off-by: Peng Huo <[email protected]>
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
Codecov Report❌ Patch coverage is ❌ Your project check has failed because the head coverage (63.31%) is below the target coverage (99.00%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #5818 +/- ##
============================================
+ Coverage 63.23% 63.31% +0.07%
- Complexity 8823 8892 +69
============================================
Files 938 950 +12
Lines 40236 40464 +228
Branches 4537 4569 +32
============================================
+ Hits 25445 25620 +175
- Misses 13966 13999 +33
- Partials 825 845 +20
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
dai-chen
left a comment
There was a problem hiding this comment.
LGTM. Thanks!
Minor: there seems a race condition between register() and close().
Ack, will address in API hookup PR. |
Description
Introduces
org.opensearch.sql.job— a language-neutral, engine-neutral query job lifecycle that PPL, SQL, and analytics-engine can all sit behind. This PR does not touch existing API. REST / transport wiring lands in a follow-up.Scope
core/job/*package (interface + value types + state machine + engine SPI + in-memory store + exceptions), the OpenSearch-side impl inopensearch/, unit tests.QueryJob): REST endpoints, transport wiring, PPL/SQL runner adapters, Guice bindings, retention timers, admission control, persistent store.Class diagram
classDiagram class QueryJobService { <<interface>> +submit(QueryRunner, Principal) QueryJob +get(QueryJobId, Principal) QueryJobStatus +cancel(QueryJobId, Principal) QueryJobStatus } class OpenSearchQueryJobService { -QueryJobStore store -ClusterService clusterService -Clock clock } class QueryJob { -QueryJobId id -Principal owner -QueryRunner runner -QueryJobState state +id() QueryJobId +owner() Principal +status() QueryJobStatus +completion() CompletionStage~QueryResult~ +cancel() void ~startRunner() void } class QueryJobStore { <<interface>> +register(QueryJob) QueryJob +find(QueryJobId) Optional~QueryJob~ +remove(QueryJobId, QueryJob) boolean +jobs() Collection~QueryJob~ +close() void } class InMemoryQueryJobStore class QueryRunner { <<interface>> +run() CompletionStage~QueryResult~ +cancel() void } class QueryJobId { <<record>> +ownerNodeId String +contextId String +encode() String +parse(String)$ QueryJobId } class QueryJobStatus { <<record>> +id QueryJobId +state QueryJobState +submittedAtMillis long +startedAtMillis OptionalLong +completedAtMillis OptionalLong +failure Optional~QueryFailure~ +result Optional~QueryResult~ } class QueryJobState { <<enum>> PENDING RUNNING SUCCEEDED FAILED CANCELLED +isTerminal() boolean } class Principal { <<record>> +name String +tenant String +backendRoles List~String~ +allows(Principal) boolean } class SecurityAdapter { <<interface>> +current() Principal } class QueryResult { <<record>> +schema Schema +rows List~ExprValue~ +cursor Cursor +warnings List~Warning~ +tookMillis long } class QueryFailure { <<record>> +type String +reason String +of(Throwable)$ QueryFailure } OpenSearchQueryJobService ..|> QueryJobService OpenSearchQueryJobService --> QueryJobStore OpenSearchQueryJobService --> QueryJob : creates QueryJob --> QueryRunner : delegates QueryJob --> QueryJobId QueryJob --> Principal : owner QueryJob --> QueryJobStatus : projects QueryJobStore --> QueryJob : holds InMemoryQueryJobStore ..|> QueryJobStore QueryJobStatus --> QueryJobState QueryJobStatus --> QueryFailure QueryJobStatus --> QueryResultState machine
Terminal transitions are idempotent.
cancel()fromPENDINGorRUNNINGmoves toCANCELLED; from a terminal state it is a no-op.Sequence diagrams
submit — happy path
sequenceDiagram autonumber participant Caller as Caller (transport) participant Service as OpenSearchQueryJobService participant Store as QueryJobStore participant Job as QueryJob participant Runner as QueryRunner participant Engine as PPL / SQL / AE Caller->>Service: submit(runner, principal) Service->>Store: register(new QueryJob) [state=PENDING] Service->>Job: startRunner() Job->>Job: state PENDING → RUNNING Job->>Runner: run() Runner->>Engine: (engine-specific execution) Service-->>Caller: QueryJob Note over Caller,Job: Caller thread free. Runner completes on worker thread. Engine-->>Runner: result Runner-->>Job: future.complete(QueryResult) Job->>Job: state RUNNING → SUCCEEDED, record result + completedAt Note over Job: completion() listeners fire on the completing threadget — authorization + snapshot
sequenceDiagram autonumber participant Caller participant Service as OpenSearchQueryJobService participant Store participant Job Caller->>Service: get(jobId, principal) Service->>Store: find(jobId) alt not found Store-->>Service: Optional.empty Service-->>Caller: QueryJobNotFoundException else found Store-->>Service: QueryJob Service->>Job: owner().allows(principal) ? alt forbidden Service-->>Caller: QueryJobForbiddenException else allowed Service->>Job: status() Job-->>Service: QueryJobStatus (immutable snapshot) Service-->>Caller: QueryJobStatus end endcancel — idempotent transition
sequenceDiagram autonumber participant Caller participant Service participant Job participant Runner Caller->>Service: cancel(jobId, principal) Service->>Job: owner().allows(principal) ? (see get flow) Service->>Job: cancel() alt state was PENDING / RUNNING Job->>Job: state → CANCELLED, record completedAt Job->>Runner: cancel() [best-effort] Job->>Job: completion.cancel(false) else state already terminal Note over Job: no-op (idempotent) end Service-->>Caller: QueryJobStatusFollow-ups (deferred behind existing interfaces)
wait_for_completion_timeout(BigQuery-style submit-response wait)completion().orTimeout(...); job unchangedkeep_alive/ retentionRetentionPolicycomposed intoQueryJobService; job unchangedAdmissionControllerinvoked beforestore.register; job unchangedQueryJobStoreimplementationQueryRunnerimplementations/_plugins/_async_queryAsyncQueryExecutorService; no new REST endpointRelated Issues
Redesign of #5809.
Check List
--signoffor-s.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.