Skip to content

[FEATURE] Report cumulative CPU and memory for live queries #710

Open

Description

@dzane17

Is your feature request related to a problem?

The CPU and memory that Live Queries reports for a running query go up and down while the query runs, but they should only grow. The totals come from the tasks that _tasks lists at the moment of the call, which are the coordinator task plus the child tasks still running. When a shard task or a child search finishes it leaves the listing, so its usage drops out of the total.

  • For a DSL search the totals dip as shard tasks finish within a phase and again between phases.
  • For a PPL query (feat: Show running PPL queries in Live Queries #699) it is much more visible. A query that pages through a composite aggregation runs dozens of searches one after another, and its row only ever counts the one search running right now. On a local 2-node cluster, a 10 second PPL stats ... by query showed total_cpu_nanos jumping between about 0.1 s and 1.7 s and dropping to 0 between searches.

As a result the CPU and memory columns and sorts in Live Queries understate long queries and can rank them below short ones.

What solution would you like?

Report cumulative usage for a live query by also counting the tasks that have already finished. Core already captures what is needed.

  1. DSL searches. Each shard response carries the shard task's final usage in the TASK_RESOURCE_USAGE response header, and the coordinator records it as each shard result arrives (AbstractSearchAsyncAction.onShardResult and FetchSearchPhase call setPhaseResourceUsages, which adds to SearchRequestContext.getPhaseResourceUsage()). Today QueryInsightsListener reads that list only in onRequestEnd for Top N.
    • Keep a reference to each running search's SearchRequestContext in a node-local map keyed by the coordinator task key, added in onRequestStart and removed in onRequestEnd / onRequestFailure, the same way live query user info is kept today.
    • A row's total becomes the coordinator's own usage plus, for each child task, its final usage if it has finished or its running snapshot if not. A finished value is never lower than a running snapshot of the same task, so the total only grows.
  2. PPL queries. When a search that has a parent task ends, the listener already computes its full total for Top N. Also add that total to a node-local accumulator keyed by the parent task. A PPL row's total becomes the PPL task's own usage plus its finished child searches plus the running child's live total from step 1. Drop the entry when the parent task is gone, so memory stays at one entry per running PPL query. This relies on the SQL plugin setting the PPL task as the parent of its searches (fix: Register background search tasks as children of parent sql#5888).
  3. Transport. A query's state lives on the node that coordinates it. TransportLiveQueriesAction already fans out LiveQueriesUserInfo to exactly the nodes that own the listed task keys, so the finished usage per task key can come back in the same node response as a new, version-gated field. Rows from nodes that do not send it keep today's numbers.

What alternatives have you considered?

  • Changing core so _tasks includes the usage of finished children in the parent's resource stats. That changes _tasks semantics for every caller, including search backpressure, and is a much larger change than keeping the state in Query Insights.
  • Leaving live numbers as snapshots and documenting it. This keeps the PPL rows misleading, since most of a PPL query's work is always in searches that have already finished.

Do you have any additional context?

  • The PPL task's own CPU and memory stay at 0 until the SQL plugin tracks its engine threads on the PPL task (feat(ppl): Report completed PPL queries to Query Insights Top N sql#5760). This proposal covers the usage of its child searches.
  • memory_bytes from task resource tracking is allocated bytes, not resident memory, so adding finished tasks to it matches what Top N already reports for completed queries.

Scale considerations

  • No new per-shard hook. Core already reads every shard task's final usage for every search. Each shard phase writes it to the TASK_RESOURCE_USAGE header in a finally block in SearchService, and the coordinator records it on every shard result. Query Insights would only keep a reference to the SearchRequestContext that already holds it, with one map put when a search starts and one remove when it ends. A PPL query adds one accumulator update per child search, not per shard.
  • Pull, not push. Nothing is summed until someone calls the Live Queries API, and each owning node sends back two numbers per query, never per-shard lists. The fan-out reuses the existing LiveQueriesUserInfo node request, so there are no extra requests.
  • Read cost. getPhaseResourceUsage() copies the whole list before it can be summed, so each API call costs time proportional to the finished shard phases of all running searches (for example 100 searches on 1,000 shards each is about 200k entries). A small core change makes this O(1) per query, with SearchRequestContext keeping running CPU and memory totals updated inside recordPhaseResourceUsage. That adds two increments per shard result next to the header decode core already does.
  • Zero cost when unused. Track search contexts and PPL totals only while Live Queries is in use. Turn tracking on at the first API call and off after an idle period, the same way the finished queries cache works.
  • Bounded memory. Cap the context map and the PPL accumulators by size and age. A query that misses tracking falls back to today's snapshot numbers.

Activity

  1. added
    enhancement
    on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions