Repository navigation
Conversation
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.
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
force-pushed
the
list-running-native-queries
branch
from
November 26, 2026 08:31
1888148 to
0bd2b95
Compare
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
force-pushed
the
list-running-native-queries
branch
from
November 26, 2026 08:57
e55553b to
ec1910c
Compare
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.
The list running queries API now returns only query metadata. When
druid.server.http.enableRunningQueryText is enabled, the text of a running query
can be fetched by its sqlQueryId or queryId from the existing query report API,
GET /druid/v2/sql/queries/{queryId}/reports, which already serves Dart reports.
NativeSqlEngine implements getQueryReport by looking the query up in the running
query registry, with the same STATE READ rules as the list API.
Look up the query and check whether the caller may see it in the same loop, so that a collision on a user-provided ID does not return another query or hide the caller's own query. DartSqlEngine now checks the type of the query info in a peer response, since the native engine can also answer for the same query ID. Document that Dart entries include SQL text in the list, and that native entries do not include query text in sys.queries.
The endpoint lives under /druid/v2/sql/ because it is served by SqlResource and shared by all engines, but the native engine also reports native queries submitted directly to /druid/v2/. Say so in the API docs, in the native query API docs, and in the SqlResource javadoc, so that the endpoint is not mistaken for a SQL-only API.
Record that the registry is used by query execution threads and HTTP threads, that all state lives in a ConcurrentHashMap, and that the returned entries are immutable. This is what the repository's concurrency checklist asks for.
CodeQL flagged the previous shape, where the ownership check and the redacted view were only computed for some queries depending on the caller's permission. Compute both for every query and only make the selection conditional, so the visibility decision does not depend on which queries the caller owns.
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.
Related to #18047.
Description
GET /druid/v2/sql/queriesand thesys.queriestable already report currently running queries, but only for the Dart engine (#17140, #18923). The native engine has no registry of in-flight queries, soSqlEngine#getQueriesreturns an empty list for it.This patch adds native engine support.
Added
RunningQueryRegistryA 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
QueryLifecycleQueries are added in
execute()and removed inemitLogsAndMetrics(). Every completion path goes throughemitLogsAndMetrics: the streaming response pusher (success, error, and 304) and therunSimplesequence wrapper. Queries that fail authorization or blocklisting never start, so they are never registered.Query text is fetched per query, not in the list
The list API returns only query metadata. When
druid.server.http.enableRunningQueryTextis enabled, the text of a running query (its SQL text when it came in through SQL, and its native query) can be fetched bysqlQueryIdorqueryIdfrom the existing query report API,GET /druid/v2/sql/queries/{queryId}/reports. That endpoint already serves Dart reports and is engine-pluggable throughSqlEngine#getQueryReport, so native support is a matter of implementing that method. The setting is disabled by default because query text can include sensitive information, and the native query is serialized only when a report is requested.Implemented
NativeSqlEngine#getQueriesRegistry entries are mapped to
NativeQueryInfo. Users with READ permission on theSTATEresource see all running queries, including identity details. Other users see only their own queries, with identity and authenticator omitted. This follows the same rules asDartSqlEngine#isOwnQuery.SqlModuleregisters theNativeQueryInfoJackson subtype ("engine": "native").The list is per-process: the native engine does not contact other servers, so
selfOnlyhas no effect, and completed queries are not retained, soincludeCompletehas no effect. Both behaviors are documented.Scope and API location
The list and report APIs live under
/druid/v2/sql/because they are served bySqlResourceand shared by all queryengines (
SqlEngine#getQueries,SqlEngine#getQueryReport). The native engine reports the native queries that executeSQL statements and the native queries submitted directly to
/druid/v2/; those entries have aqueryIdbut nosqlQueryId. The docs now say this explicitly, the native query API docs link to these endpoints, and theSqlResourcejavadoc notes that the API is not limited to SQL. Keeping the existing paths avoids duplicating or moving an API that
the Dart engine and the console already use.
Public API, configuration, and endpoints
GET /druid/v2/sql/querieskeeps the same response shape (GetQueriesResponse) and now alsocontains entries from the native engine. Native entries use the existing
QueryInfoshape with a new subtype(
NativeQueryInfo) and a newenginevalue,native.GET /druid/v2/sql/queries/{queryId}/reportsis an existing endpoint that now also answers fornative engine queries. For native queries,
{queryId}can be thesqlQueryIdor the nativequeryId, and theresponse is
{"query": {...}, "report": {}}.druid.server.http.enableRunningQueryText, defaultfalse. Whentrue, theper-query report includes
sqlandnativeQueryfor native queries. The list API never includes query text.@PublicApior@ExtensionPoint.SqlEngine#getQueryReportis implemented forthe native engine; the default implementation and the Dart implementation are unchanged except for a type check on
peer responses.
Compatibility and rolling updates
report endpoint keeps returning
404for native queries there, since the previous behavior was to return the Dartengine's answer only.
Concurrency
RunningQueryRegistryis used by two kinds of threads: query execution threads callregisterwhen a query starts andunregisterwhen it finishes, and HTTP threads callgetAllto build the API response. All state lives in a singleConcurrentHashMap; there is no additional locking, no shared mutable state outside the map, and no orderingrequirement between readers and writers.
getAlltakes a snapshot of the map and returns immutable entries, so a slowAPI call never blocks query execution, and a query that finishes while the list is being built simply disappears from
the next call.
NativeSqlEngine#getQueryReportserializes a query only when a report is requested; the query object itreads is not mutated after execution starts.
Design notes
QueryScheduler? It already tracks query IDs inqueryFuturesandqueryDatasources, but those maps are documented as racy, are only populated on some code paths (CachingClusteredClientregisters 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 byQueryLifecyclegives one complete lifecycle for every query that actually starts executing.STATEresource, other users' queries return 404.Release note
GET /druid/v2/sql/queriesandsys.queriesnow include queries that are running on the native engine, with their query type, datasources, identity, and start time. Previously, both only reported Dart queries. Operators who enable the newdruid.server.http.enableRunningQueryTextproperty can fetch the SQL text and the native query of a running native query fromGET /druid/v2/sql/queries/{queryId}/reports.Testing
RunningQueryRegistryTest(including query text capture and serialization failure),NativeSqlEngineTest(mapping, authorization filtering, JSON serialization throughSqlModule),QueryLifecycleTest#testRunSimple_registersRunningQuery.SqlResourceTest#testListRunningQueries(STATE READ users see all queries; other users see only their own).NativeSqlEngineRunningQueriesTestruns a real SQL query throughSqlResourcewith the real planner and segment walker, and verifies that the query is listed without text while it executes, that its SQL text and native query are available from the report API, and that it is gone afterwards.druid-server:RunningQueryRegistryTest,QueryLifecycleTest,QueryResourceTest,PerSegmentTimeoutInjectionTest,ServerConfigTest(82 tests).druid-sql:NativeSqlEngineTest,NativeSqlEngineRunningQueriesTest,SqlResourceTest(79 tests).druid-msq:DartSqlEngineTest,DartSqlResourceTest(22 tests).-DstrictCompile(ErrorProne) are clean for the changed modules./druid/v2/appears inGET /druid/v2/sql/querieswithoutsqlQueryId, its text is returned by the report API whendruid.server.http.enableRunningQueryTextis enabled, and it appears insys.queries; unknown query IDs return404.Reviewer notes
Design Reviewlabel. It also has user-visible impact, so it likely needs the
Release Noteslabel. The author does not have labelpermissions, so a committer will need to add them.
not planned. If a new issue is required for the feature/change process, I canfile one and link it here.
Key changed/added classes in this PR
server/src/main/java/org/apache/druid/server/RunningQueryRegistry.java(new)server/src/main/java/org/apache/druid/server/QueryLifecycle.javaserver/src/main/java/org/apache/druid/server/QueryLifecycleFactory.javaserver/src/main/java/org/apache/druid/server/initialization/ServerConfig.javasql/src/main/java/org/apache/druid/sql/calcite/run/NativeSqlEngine.javasql/src/main/java/org/apache/druid/sql/http/NativeQueryInfo.java(new)Self-review checklist
model, and readers never block writers.
were added or changed.
reader.
code coverage is met.
NativeSqlEngineRunningQueriesTestruns a real SQL query throughSqlResourcewith the real planner and segment walker, which covers the API behavior without a separate integrationtest module.
to
/druid/v2/appears in the list withoutsqlQueryId, its text is returned by the report API, and it appears insys.queries.