Skip to content

feat: add native engine queries to the running queries API - #20973

Draft
dkhwangbo wants to merge 5 commits into
apache:masterfrom
dkhwangbo:list-running-native-queries
Draft

dkhwangbo wants to merge 5 commits into
apache:masterfrom
dkhwangbo:list-running-native-queries

Conversation

@dkhwangbo

@dkhwangbo dkhwangbo commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Related to #18047.

Description

GET /druid/v2/sql/queries and the sys.queries table already report currently running queries, but only for the Dart engine (#17140, #18923). The native engine has no registry of in-flight queries, so SqlEngine#getQueries returns an empty list for it.

This patch adds native engine support.

Added RunningQueryRegistry

A new server-module singleton tracks the queries that are executing on a process: query ID, sqlQueryId (when the query came in through SQL), query type, datasources, identity, authenticator, and start time.

Registered and unregistered by QueryLifecycle

Queries are added in execute() and removed in emitLogsAndMetrics(). Every completion path goes through emitLogsAndMetrics: the streaming response pusher (success, error, and 304) and the runSimple sequence wrapper. Queries that fail authorization or blocklisting never start, so they are never registered.

Captured query text behind a flag

Setting druid.server.http.enableRunningQueryText makes the registry keep the SQL text of SQL queries and a reference to the native query, and return them as sql and nativeQuery in the API and in sys.queries. The native query is serialized when the list is read, so processes that do not serve the API do no extra work. The setting is disabled by default because query text can include sensitive information.

Implemented NativeSqlEngine#getQueries

Registry entries are mapped to NativeQueryInfo. Users with READ permission on the STATE resource see all running queries, including identity details. Other users see only their own queries, with identity and authenticator omitted. This follows the same rules as DartSqlEngine#isOwnQuery.

SqlModule registers the NativeQueryInfo Jackson subtype ("engine": "native").

The list is per-process: the native engine does not contact other servers, so selfOnly has no effect, and completed queries are not retained, so includeComplete has no effect. Both behaviors are documented.

Design notes

  • Why not use QueryScheduler? It already tracks query IDs in queryFutures and queryDatasources, but those maps are documented as racy, are only populated on some code paths (CachingClusteredClient registers only in the parallel-merge branch), and are cleared when the last result future completes, which can happen before the response has been fully written. They also carry no identity or start time. A registry driven by QueryLifecycle gives one complete lifecycle for every query that actually starts executing.
  • Why is query text behind a flag? Query text can include sensitive information, and it is not needed by every deployment. When the flag is off, the registry stores no text and the API returns no sql or nativeQuery fields. When it is on, the SQL layer passes the SQL text through QueryLifecycle, and the registry serializes the native query. Users still need permission to see the queries themselves: without READ permission on the STATE resource, users only see their own queries.

Release note

GET /druid/v2/sql/queries and sys.queries now include queries that are running on the native engine, with their query type, datasources, identity, and start time. Previously, both only reported Dart queries.

Testing

  • Unit tests: RunningQueryRegistryTest (including query text capture and serialization failure), NativeSqlEngineTest (mapping, authorization filtering, JSON serialization through SqlModule), QueryLifecycleTest#testRunSimple_registersRunningQuery.
  • Resource-level test: SqlResourceTest#testListRunningQueries (STATE READ users see all queries; other users see only their own).
  • End-to-end test: NativeSqlEngineRunningQueriesTest runs a real SQL query through SqlResource with the real planner and segment walker, and verifies that the query, its SQL text, and its native query are reported while it executes, and that it is gone afterwards.
  • Full affected test sets pass:
    • druid-server: RunningQueryRegistryTest, QueryLifecycleTest, QueryResourceTest, PerSegmentTimeoutInjectionTest (76 tests).
    • druid-sql: NativeSqlEngineTest, NativeSqlEngineRunningQueriesTest, SqlResourceTest, SystemSchemaTest#testQueriesTable (72 tests).
  • Checkstyle, forbidden-apis, and spotbugs are clean for druid-server and druid-sql.

Add RunningQueryRegistry, which tracks the queries that are executing on a
process along with their identity, start time, and datasources. Queries are
registered when execution starts, and removed when logs and metrics are
emitted, in QueryLifecycle.

Implement SqlEngine#getQueries for the native engine, so that
GET /druid/v2/sql/queries and sys.queries report queries that are running on
the native engine. Users without READ permission on the STATE resource only
see their own queries, without identity details.

Register the NativeQueryInfo Jackson subtype in SqlModule, and document the
GET /druid/v2/sql/queries endpoint and the native engine rows of sys.queries.
@dkhwangbo dkhwangbo changed the title Add native engine queries to the running queries API feat: add native engine queries to the running queries API Nov 26, 2026
QueryLifecycle-driven identity filtering now follows the same shape as
DartSqlEngine: build the full list, then filter it for callers without READ
permission on the STATE resource.
Remove equals, hashCode, and toString from RunningQueryRegistry.Entry and
NativeQueryInfo, which are not needed by the API, and use a Comparator for
the registry snapshot. Add tests for a query without an ID, for unregistering
unknown IDs, and for callers that have no identity or no authenticator.
@dkhwangbo
dkhwangbo force-pushed the list-running-native-queries branch from 1888148 to 0bd2b95 Compare November 26, 2026 08:31
Add druid.server.http.enableRunningQueryText, which makes the running query
registry keep the SQL text and the native query of each running query, and
return them from the running queries API and sys.queries. The setting is
disabled by default because query text can include sensitive information.

The SQL layer passes the SQL text to QueryLifecycle, and the native query is
serialized when the query is registered. Use generic test identities instead
of personal ones.
@dkhwangbo
dkhwangbo force-pushed the list-running-native-queries branch from e55553b to ec1910c Compare November 26, 2026 08:57
Include enableRunningQueryText in ServerConfig equals, hashCode, and toString,
and thread it through the all-args constructor and the copies that
CliIndexerServerModule makes, so that EqualsVerifier and config dumps see it.

Stop serializing native queries when they are registered. The registry now
keeps a reference to the running query and NativeSqlEngine serializes it when
the list is read, so processes that do not serve the API do no extra work.

Clarify in the configuration docs that the list is served by the Broker and
that SQL text is captured only for queries submitted through the SQL layer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants