mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-26 20:54:08 +00:00
* test(promql): feed the label-rewrite tests through an explicit input slot The runtime takes an input position now, so the tests that a label rewrite brought in name the leaf they push at, and the map import no longer carries the operation type the runtime reads through `MapOp`. Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): feed the newly added native tests through input slots The histogram-function and range-modifier tests that landed on the branch use the execution entry points as they were before the runtime took an input position, so they name the leaf they push at. Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * fix(query): preserve legacy semantics at native admission Signed-off-by: discord9 <discord9@163.com> * test(query): iterate captured delta result batches Signed-off-by: discord9 <discord9@163.com> * test(query): cover fused Columnar Float64 execution Signed-off-by: discord9 <discord9@163.com> * refactor(promql): keep runtime trees owned by their executions Register each execution's node metric handles once in the physical plan's metrics set. Reading metrics no longer retains or locks runtime topology. Keep historical counters alive after execution drop and cover repeated reads and executions. Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): register binary metrics before execution Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * fix(mito2): read raw series when columnar indexes are not ready Keep native queries available for unflushed memtables and SSTs with incomplete columnar series or samples coverage by selecting the ordinary raw-series reader. Preserve structural validation and propagate artifact read errors instead of retrying them through legacy execution. Cover all four incomplete-readiness states in public PromQL queries, both storage scan modes and ordering, complete prepared-source execution, and artifact error propagation. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): keep fused POC execution partition-local Remove plan-global label-set occupancy and the speculative split-series tree guard. Keep the native evaluator, finality, memory budgets, errors and ordinary RecordBatch output local to each scan partition. Narrow native admission to selectors and the small unary POC subset. Document that cross-partition final-label conflicts and semantic merging are outside this POC, and cover real rate/abs(rate) execution across dense and sparse storage spanning SST and memtable data. Signed-off-by: discord9 <discord9@163.com> * test(promql): use an instant-vector subquery boundary case Signed-off-by: discord9 <discord9@163.com> * test(promql): restore the shared pool fixture for metric lifecycle tests Signed-off-by: discord9 <discord9@163.com> * feat(promql): bind selector occurrences without node paths Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): cover selector bounds and recursive binding contracts Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): avoid cloning a selector binding for validation Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * feat(promql): resolve runtime input ids from source bindings Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * feat(promql): lower independently bound vector binary operands Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): correct distinct-input arithmetic expectations Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * feat(promql): fan out a shared raw scan to bound binary inputs Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): align frame ordering and SQL result sorting Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): align native display and shared-source scan expectations Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): account for counter-zero range extrapolation Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): record shared-selector SQL regression results Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): read bounded scan metrics through cooperative wrapper Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * feat(promql): execute independent selector scans in fused binary trees Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * style(promql): satisfy strict fused adapter lints Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): regenerate independent matcher and range results Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * feat(promql): adapt complete aggregate wiring to recursive native runtime Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * fix(promql): preserve partial prefix rejection diagnostic Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * fix(promql): align aggregate row adapters with current native APIs Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * fix(promql): align aggregate admission with boxed parser inputs Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): admit complete aggregate in native shape coverage Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * style(promql): attach native predicate rustdoc directly Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * feat(promql): execute zero-input scalar roots through fused children Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): verify scalar routes and four-partition broadcasts Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): disambiguate the frontend SQL test helper Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): prove each scalar-route partition produces output Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): import partition properties for output assertions Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): let query encoding use auto-deref Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): retain and verify empty vector result schemas Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): cover scalar aggregate composition and fallback Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): scope partition proof to unfiltered scans Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): quote region label in Mito fixture SQL Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): fix scalar schema ownership and rebuild API Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): align admission expectations with literal entry Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): use fixed scalar batch fixture Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * feat(promql): bind instant anchors and execute native timestamps Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): cover the multi-value timestamp refusal Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * refactor(series): unify raw and evaluated SeriesFrame codec SeriesFrame is the internal transport of the Narthex native PromQL path, and it could only carry raw samples: an evaluated result had to fall back to a plain row table. This makes one self-describing Arrow format carry both, so a receiver decodes a frame from the frame alone, without the scanner, region, or planning context its producer had. What is unified is the physical representation and its codec, not the runtime data structures. RawSeriesChunk, Series, and EvaluatedChunk stay as they are; only the adapter boundaries branch on the sample shape. Three combinations are legal and every other one is refused in both directions: raw + key + timestamps a storage raw-series scan raw + explicit + timestamps a raw input whose labels are decoded evaluated + explicit + grid_spans a compute node's output An evaluated frame cannot claim a storage key, because the computation may rewrite its labels; raw samples have no grid to be addressed on. The schema builder and the declaration parser both enforce that, so a frame cannot be written or read in a shape no producer could mean. An evaluated frame declares its GridSpec once in schema metadata and carries absolute grid positions per row as spans, so a sparse grid costs its runs and not its holes: nothing is expanded, and no position a chunk does not hold is filled in. A chunk with no position is legal exactly when it closes its chain, and it is kept as one frame row, because dropping it would drop the finality it exists to carry. Layout is v2 and the writer and reader of the reserved greptime.series.* metadata live in one place each, so a producer and a consumer cannot spell one frame two ways. The unreleased v1 format is not read: the in-tree producers, consumers, tests, and the two frozen wire fixtures all move to v2 together. Signed-off-by: discord9 <discord9@users.noreply.github.com> Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> (cherry picked from commit176f6f6479) * fix(series): validate v2 ordering and repair scan binding fixtures Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(series): pin literal ordering declarations and decoder validation Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(series): remove unused decoder fixture binding Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * fix(test): port MinIO conditional-write test fix to narthex (#9930) fix(test): avoid MinIO connection reuse after conditional writes (#9877) Signed-off-by: WenyXu <wenymedia@gmail.com> Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> Co-authored-by: Weny Xu <wenymedia@gmail.com> * feat(promql): add a series frame output to the fused native node The fused node answers one way: a row table with the grid timestamps, one column per series field and one per declared tag. That is what a query returns, but a consumer of the native path needs to tell series and positions apart, and the row shape flattens both away - which series a value belongs to, and which grid positions that series actually holds. This adds a second output format, fixed when the plan is built rather than chosen at run time: PlainRows the existing row table, unchanged and still the only one a query reaches SeriesFrame the same evaluated chunks handed over as the unified series frame The format is part of the plan because it decides the node's output schema, so it takes part in the node's equality, its EXPLAIN text and its rebuild over new children. A row-table plan prints exactly what it printed before; a frame plan says so on its own line. A frame is encoded straight from each evaluated chunk, never by flattening it into rows and packing it again, so a sparse series keeps its runs and a closing chunk with no position is still one row. The output keeps one partition per input partition: a frame is not a reason to coalesce, and no exchange is inserted in either format. The partial-state output that a complete aggregate reads is a third thing again, and the format does not reach it. When a node exports partition-local state, the format is `PlainRows` by construction, `convert` hands every chunk to the fold before any format is consulted, and the EXPLAIN line keeps its `partial state:` suffix. Nothing in the aggregate path is rewritten to make room for frames. A tree whose root is not evaluated samples is refused while the plan is built, because aggregate state is a contribution to a later merge and a scalar has no series to name. So is a frame output over more than one raw source, which this node does not run in parallel yet. Signed-off-by: discord9 <discord9@users.noreply.github.com> Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): pin the frame output's boundaries and its pending hand-over The frame output was covered for its happy path and for the plan-level decisions, but three things the spec asks about had no test of their own at this boundary, and one test did not prove what it claimed. The pending test is the one that mattered. It awaited the stream, and `StagedStream` wakes itself right after reporting pending, so the await ran straight through the boundary: every later assertion held whether or not the buffered rows were handed over at the pending poll. It now polls that single poll by hand and requires it to answer the rows it already encoded. Deleting the `flush_frames` call from the pending branch makes it fail, which it did not before. The three boundaries now have their own tests: a chain that closes with no position survives as one row where the row table answers nothing at all; a sparse series carries exactly the positions the row table answers and no filled hole; and two chains that close on one label set are both kept, with values that differ so a lost chain would show. Also drops the `as_str` helper, which only asked whether to show a format and then spelled the same string twice, and corrects two comments: `convert` produces three outputs rather than two, and a built plan is not a promise that execution cannot fail. Signed-off-by: discord9 <discord9@users.noreply.github.com> Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * fix(promql): stream evaluated frames with batch-level backpressure Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): keep the frame output tests lint-clean The workspace denies the default Clippy warnings, and the new tests tripped three of them without changing what any of them proves: - an expected value was built through `scale as f64` even though the tuple already carries an `f64`, so the conversion is spelled in the literal and the cast is gone; - the standalone replay printed the executed plan and the per-partition counts even though the surrounding assertions already require four nonempty partitions, 24 rows in total and eight closing frames, so the diagnostics are gone rather than exempted from the workspace lint. No production code, assertion or fixture changes. Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * feat(promql): finalize topk over globally merged aggregates Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * feat(promql): compile and run set operators natively Bind `and`, `or` and `unless` (with `on`/`ignoring`) to a new `NativeExpr::Set` node instead of refusing them. The node runs the existing MATCH-SET kernel through the binary runtime, which retains both operands until the input ends, so a set operation reads every series of both sides in one execution. The set kernel now reports its workspace like the binary kernel does: kept rows, the filter side's match groups, the sort and the merge are charged to the node's reservation. The compiler refuses a fill value on a set operator, which Prometheus rejects and the parser accepts, and sides with different field counts. Same-table set operations over two scans take the existing two-scan native route. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(query): keep two-selector native plans off partitioned tables A two-scan FusedSeries over a table split into several regions has no placement: DistPlannerAnalyzer rejects it with "only accept plan with at most one child", so the query failed instead of falling back. This hit the existing arithmetic pair and, since set operators now compile, set operators too. Decline the native route for two selectors when the table has partition columns. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): leave multi-field set operations to the legacy plan The legacy planner refuses `and`, `or` and `unless` over several fields (MultiFieldsNotSupported), and Prometheus has one value per sample, so per-field presence has no reference semantics. The compiler now declines a set operation unless each side has exactly one field, and the query keeps the legacy plan and its error. Errors raised by the shared two-operand runtime now name the set node instead of calling every such node "the binary node". Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * perf(promql): decide set membership from a per-group presence bitmap The set kernel checked every filter series of a kept series' match group at every kept point, so a coarse `on()` / `on(...)` cost O(kept series x points x group size). Prometheus decides a step from the set of signatures present on the filter side. Build one bitmap per filter match group, one bit per grid point, from the filter side's rows, and look each kept sample up in O(1). Each bitmap is charged to the walk phase when its group is first met and is released with the group table before the sort phase. Results are unchanged. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * feat(promql): fuse independent operands of hybrid binary queries Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * fix(promql): hold the labels of queued results until they are handed on A node that reads its whole input (binary, set, vector) queues the series its kernel produced and settles its reservation on the queue. Each queued result was held for `evaluated_bytes`, which covers the chunk and its fields but not the label set the chunk owns, so once the operands were released the reservation dropped below what the queue actually held. A set over many long-labelled one-point series made the gap large. Hold each result that does not share the input for `series_bytes`, the complete owned estimate including labels. The figure stored with the entry is still the one released when it is handed on. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * test(promql): pin per-step or matching through the native entry A range query `a or on(host) b` where one host's left series covers only the middle steps: native must keep the left sample where it exists and take the right sample with its own labels at the other steps, matching the explicit rows and the legacy plan. The set test's row reader moves to module level so both cases share it. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * test(promql): narrow global topk partition recording Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * refactor(promql): reuse prepared native operand context Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): address global topk review feedback Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): strengthen hybrid binary review regressions Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * feat(promql): run cross-table selector pairs natively A binary or set operation between selectors of two tables, such as `rate(a[5m]) / rate(b[5m])`, fell back to the existing plan: the native route required every input to share one metric, time index and tag list, and a fused node is placed inside the one region it reads. Two tables are two regions, so such a pair now runs on the frontend. Each input becomes a `RawSeriesSource` that rides in its own merge scan: the region binds its scan to that table's metric, tags and fields, decodes the labels and answers one explicit-label raw frame stream, reading its scan partitions one after another so each series stays contiguous. The fused node consumes those frames directly. Pairs on one table keep running in the region; partitioned tables keep the existing plan. A point-wise wrapper around an admitted pair, such as `100 * (1 - a / b)`, is admitted too, on one table and across two. The output labels are now derived from the whole tree with each input's own tags, and the physical node takes them from the output schema instead of narrowing by the root alone. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): decline cross-table outputs the native node cannot name Under `or` the right table's tags join the left side's columns, and one of them can share a name with the left side's field or time index; building the output schema then failed instead of leaving the query to the existing plan. Such a union is now declined. The output-label walk also declines an aggregate or whole-vector node instead of passing its input's labels through, which would describe the wrong schema if one were ever reached. Adds an end-to-end case over two tables whose tags, field and time index are all named differently, partly flushed and partly in the memtable, under both series scan modes. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * perf(promql): read native inputs and scan partitions concurrently A multi-input FusedSeriesExec drained every partition of its first input before it touched the second, and RawSeriesSourceExec drained its scan partitions one after another. The series distributor feeding those partitions then waited on the one partition being read, the two inputs' scans never overlapped, and all decoding ran on the one consuming task. Read every partition through SeriesGather instead. It polls all of them and hands a chunk on unless another partition of the same input has a series open; such a chunk is held until that series closes, so each input still sees contiguous series. A partition holding a closed series is not polled again until it is handed over, which bounds what is held to one series per partition, and that is charged to a memory reservation. Inputs do not constrain each other. When there is more than one partition, each is driven on its own query task with a two-chunk channel, so the scan conversion, frame codec and label decode of different partitions run in parallel. Dropping the stream aborts those tasks, and a task that panics ends its partition in an error rather than a silent end. On the o11ybench vmlite/vmlong data (standalone, native ON, median of 10 alternating runs; results byte-equal to the sequential reader): sequential* this change vmbench-lite-05 4.4ms 3.0ms vmbench-lite-06 7.3ms 4.2ms vmbench-lite-08 5.0ms 3.3ms vmbench-long-range-03 54.4ms 39.6ms cpu same-table pair 27.6ms 15.3ms long fs same-table pair 104.2ms 83.6ms * with the series distributor fix; without it the sequential reader stalled for up to 12.8s on these queries, and this change does not need that fix. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): delegate fused frame output to the shared packer (#9968) Reuse pack_evaluated_series for fused Frame output, preserving the single native driver and PlainRows/partial timing. Reject mismatched logical frame schemas and use the reset scan plan in integration tests. Add evaluated-packer lifecycle and invalid-sequence regressions, and address the review follow-ups. Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * feat(promql): run vector-vector comparisons natively A one-to-one comparison between two selectors, such as `a > on(host) b` or `a == bool b`, kept the existing plan: the pair admission accepted only arithmetic operators. The kernel already pairs comparisons by the same matching rules and only differs in the value a pair keeps (the left sample when the comparison holds, or 1/0 with `bool`), so the operator check is dropped. This covers one table and two, and the point-wise wrappers around a pair, such as `(a > b) * 100`. Adds an end-to-end case over two tables comparing explicit rows, the legacy answer and the native placement for a filter and a `bool` comparison. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): charge the chunks a spawned partition reads ahead A spawned partition queues up to two chunks and its task holds a third while the queue is full. None of them was charged to a reservation, and since they carry materialized labels their size is unbounded even though their count is not. Each chunk is now charged to the query's memory pool when the task reads it, and the charge travels with the chunk: it is released when the chunk is handed to the gather, which charges what it holds back itself, or when the chunk is dropped in the queue or in an aborted task. A pool that cannot hold the next chunk ends the partition in an error. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(mito2): stop the series distributor from stalling on unread partitions (#9991) When every partition channel is full, the distributor walked the senders round-robin with a 100us send timeout each. Tokio timers fire on a 1ms tick, so a consumer that leaves some partitions unpolled costs about one millisecond per unpolled partition for every series batch. SeriesScan::build_stream and scan_all_partitions drain partitions one after another, and SortPreservingMergeExec above a multi-partition SeriesScan only pulls the partition holding the current minimum key. With 16 partitions and one batch per series after a merge, a 40-series scan read partition by partition took ~480ms (measured on the native PromQL two-input route). Wait on all senders at once and send to the first one that frees a slot. The send-timeout metric goes away with the timeout. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> (cherry picked from commite6b60644b3) Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * feat(promql): add explicit local canonical merge operator (#9969) * feat(promql): add explicit local canonical merge operator Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * fix(promql): tighten canonical merge accounting and validation Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * refactor(promql): remove redundant merge tree whitelist Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * fix(promql): retain canonical merge output label reservations Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * refactor(promql): finish canonical merge review simplifications Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): pin the parent barrier over canonical merge Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): assert the two-selector binary stays unfused Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> --------- Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * fix(promql): keep the right side's labels for a filled left side across tables Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): repair native execution contracts and remove obsolete paths (#9980) * fix(promql): restore native scan statistics and remove unused raw merger Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * fix(promql): repair native execution contracts and remove obsolete paths Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * refactor(promql): leave fused frame output repairs to separate PR Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * docs: restore native contracts and regenerate config reference Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * fix: slice nullable sparse frame fields after span selection Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * fix(promql): intersect anchored timestamp selection windows Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(query): name anchored timestamp boundary case type Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * test(promql): align partitioned pair checks with hybrid operands Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> --------- Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> Signed-off-by: discord9 <discord9@163.com> * feat(promql): run native statements over any number of selectors (#10004) * feat(promql): run native trees with more than two selectors Lift the two-selector cap of the native PromQL route. A tree of one-to-one vector matches and set operators, nested under point-wise wrappers, is now admitted for any number of selectors: on one table the node runs in the region over every scan, across tables it runs on the frontend over one RawSeriesSource per selector. Partitioned tables, aggregates, whole-vector operations and many-to-one matches still keep the existing plan. The dist analyzer and commutativity checks accept a same-table node with more than two scans; without that such a plan fails in the analyzer. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * test(promql): cover nested set operators and metric-engine multi-selector decline Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * test(promql): wait for aborted partition tasks to release their inputs Signed-off-by: Dennis Zhuang <killme2008@gmail.com> (cherry picked from commit 3f8699782c3116c4063ec70a3522d1f5f3bff897) * docs(promql): say spawned partitions are released after an error, not before Signed-off-by: Dennis Zhuang <killme2008@gmail.com> --------- Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * perf(promql): resolve the vector match result row once per pairing (#10007) * perf(promql): resolve the result row once per pairing in vector match Signed-off-by: discord9 <discord9@163.com> * docs(promql): state the native node metric naming contract Signed-off-by: discord9 <discord9@163.com> * bench(promql): make the vector match timing reproducible Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * feat(promql): run info() natively (#10005) * feat(promql): run info() natively info(v, {selector}) fell back to the existing plan, which has no info and refuses the query ("Unsupported expr type: info", or "Cannot accept multiple vector as function input" when a selector is given). Compile it into the native tree and run it on the info kernel: - Plan: NativeExpr::Info { base, info, selector }. The info side is an instant selection reporting each selected sample's own time in milliseconds (InstantValue::InfoTimestamp, which replaces the BoundInstant.timestamp flag); validation refuses any other info side. - Compiler: info_input derives the info selector the way Prometheus does: target_info or one __name__="..." matcher, the data matchers, and the @ and offset every selector of the first argument shares (subquery offsets added until an @ anchors them, none when they differ). Regex or negative name matchers, which can select several tables, __field__/__schema__/__database__ matchers and multi-field info tables are refused. The planner's selector walk uses the same helper, so both bind the info table as the last input. - Runtime: BinaryRuntime runs the kernel as PairKernel::Info, with the retention and pool accounting of binary and set nodes; the kernel reports its workspace through info_with_reservation. - Planner: the info table is a second table, so it is read through its own RawSeriesSource and the node runs on the frontend. The output adds the info table's tags, or only the data labels the selector names. Partitioned or non-Mito tables and a missing info table keep the existing plan. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * test(promql): pin region-local placement of info over its own table info(rate(target_info[5m])) reads the info table twice. Check that the planner binds both as direct scans under one FusedSeries node bound to the table, that the node is placed in the region, and the columns it reports. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): keep sample-less base series out of info's identifying patterns Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): share identifying label strings in info() instead of copying them Signed-off-by: Dennis Zhuang <killme2008@gmail.com> --------- Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): keep a group's points together when decoding aggregate state (#10008) * fix(promql): keep a group's points together when decoding aggregate state The state encoder writes a group's points in ascending order, one row per (point, field). The decoder opened a group whenever the row's point differed from the group's first point, so every point of every group became its own group: each row re-allocated the label set's `Arc<str>` strings, and the merge then re-hashed those labels and repeated a map lookup for each of them. Open a group only when the labels change. A later point of the same group is appended to it and a repeated point is that point's next field, which keeps the arrival order of every (point, field) and therefore the floating-point merge order unchanged. A point that goes backwards within a group is refused. Appended points and their state vectors are admitted before they allocate. Measured with paired alternating A/B on one prepared vmbench-lite database, 30 measured requests per run, opposite order each round, 6 rounds: Q02 avg without(cpu) 5m p50 A/B 1.60, CPU A/B 1.25, 6/6 rounds faster Q11 sum by 4 tags 5m p50 A/B 1.57, CPU A/B 1.49, 6/6 Q01 100-(avg(...)*100) p50 A/B 1.40, CPU A/B 1.25, 6/6 Q10 count by p50 A/B 1.04, CPU A/B 0.99, 4/6 (neutral) Q03 sum by region,az p50 A/B 0.92, CPU A/B 0.87, 2/6 (slightly slower) `cargo nextest run -p promql` 987 passed, 0 skipped. Signed-off-by: discord9 <discord9@163.com> * fix(promql): accept concatenated aggregate state instead of rejecting it Review of #10008 found three problems. `decode_state` refused a row whose point went backwards within its group, and refused a repeated `(point, field)` pair. Both are real inputs once state crosses Flight: `CoalescingBatcher` merges consecutive small batches (`servers/src/grpc/flight/stream.rs:313`), so two partitions' state for one group arrives back to back and the second restarts the group at its first point. Accept them instead: the merge addresses points by index (`aggregate/state.rs:396-423`) and folds groups by label key, so a second entry for the same point lands in the same slot, exactly as merging the two batches separately would. Out-of-grid point and field indices are still errors. The doc comment on `decode_state` still described the old rule (a change of label or point opens a group); only a label change does now. `decode_admits_the_points_it_appends` did not test what it claimed: the fixed layout charge already exceeded its `tallest / 2` threshold, so the first admission was refused whether or not appended points were charged, and the test passed with the append-time `budget(total)?` removed. Derive the threshold from a one-point batch of the same group so only appended points can exceed it. Both files' tests updated to pin the new behaviour: a concatenated batch of two encoders' output decodes like the two batches merged, a repeated field sums, and the budget test now fails if the append-time charge is dropped. Signed-off-by: discord9 <discord9@users.noreply.github.com> Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> Signed-off-by: discord9 <discord9@users.noreply.github.com> * perf(mito2): coalesce raw series records before residual predicates (#10017) * perf(mito2): coalesce raw series records before residual predicates `raw::make_stream` evaluated every residual predicate against one record per `ScanBatch`, and a `ScanBatch` carries the rows of a single series. A predicate pass is dominated by per-call setup rather than by per-row comparison, so the cost was paid once per series: profiling the lite-02 aggregate query put `common_recordbatch::filter::batch_filter` at ~20% of CPU across 1280 calls for 640 series, and 70% of that was per-call overhead. Buffer records until 4096 rows, concatenate them with `concat_batches`, and evaluate the predicates once per group. The grouping loop, the `is_last` marking, the strictly-increasing checks and the error messages are unchanged. Three guards keep the behaviour identical: - A record whose first primary key continues the series the buffer ends with is held back, because concatenation would fuse two chunk boundaries of one series. Two existing tests pin those boundaries. - With no predicate there is nothing to amortize, so the threshold drops to one record and the stream keeps its per-record shape. - Buffered records are reported one at a time when an upstream error arrives. Merging them would put rows into the chunk withheld as `pending`, dropping a prefix the per-record stream emitted before the error. All 12 vmbench-lite queries return identical series, timestamps and values. Paired A/B against the previous head: CPU is lower in 30 of 30 rounds, median cut 13.8%-29.4% across five queries. Signed-off-by: discord9 <discord9@users.noreply.github.com> Signed-off-by: discord9 <discord9@163.com> * perf(mito2): release coalesced source records before filtering The concatenated arrays own their buffers through `Arc`, so the source `RecordBatch`es are dead once `concat_batches` returns. Releasing them keeps the original arrays from staying alive across the predicate pass and the per-chunk yields of the same round. Raised in review of #10017; no behaviour change. Signed-off-by: discord9 <discord9@163.com> * perf(mito2): start a group at a record that fills a batch, drop the error split Two review comments on #10017. `make_stream` processed the records buffered before an upstream error one at a time, so the chunks of the earlier records came out before the error. Nothing reads that prefix: the error fails the query, and the `RawSeriesStream` contract promises per-series ordering and finality, not a prefix on error. The predicate path already withholds its `pending` chunk on failure, so the split was also inconsistent with the rest of the stream. Removed, along with the half of `filtered_boundary_fuses_chunks_on_eof_but_an_upstream_error_still_withholds_one_record` that pinned it; the remaining fusing assertion keeps its own test. A record that reaches `coalesce_rows` rows by itself now heads its own group. Concatenating it with whatever is buffered copies it, and its predicate pass is paid once either way; on its own it takes the single-record path unchanged. The threshold's doc comment no longer claims a record larger than the bound is isolated, which was never true. Neutral for the measured workload: a vmbench-lite series is 61 rows, so the threshold is never met by one record. `cargo nextest -p mito2` series-scan selection: 53 passed. Signed-off-by: discord9 <discord9@users.noreply.github.com> Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@users.noreply.github.com> Signed-off-by: discord9 <discord9@163.com> * perf(mito): push predicates into columnar series discovery (#10014) * perf(mito): push predicates into columnar series discovery Signed-off-by: evenyag <realevenyag@gmail.com> * perf(mito): resolve series tags once per discovery batch Signed-off-by: evenyag <realevenyag@gmail.com> --------- Signed-off-by: evenyag <realevenyag@gmail.com> * perf(mito): batch samples reads and reuse part readers (#10016) * perf(mito): batch samples reads and reuse part readers Signed-off-by: evenyag <realevenyag@gmail.com> * fix(mito): validate boundary reads and report scan memory exhaustion Signed-off-by: evenyag <realevenyag@gmail.com> * docs: keep samples specification focused on the file format Signed-off-by: evenyag <realevenyag@gmail.com> * style: remove trailing blank line in samples format doc Signed-off-by: evenyag <realevenyag@gmail.com> --------- Signed-off-by: evenyag <realevenyag@gmail.com> * feat(promql): run time(), vector(s) and data-free scalar broadcasts natively (#9988) * feat(promql): run time(), scalar(v), vector(s) and dynamic scalar broadcasts natively Bind time() and zero-argument date functions as EvalTime, scalar(v) as ScalarOf and vector(s) as VectorOf, and run binary operations whose scalar side varies per grid point: the scalar is read whole, vector chunks that arrive before it are held and charged, and both operand orders and bool comparisons follow Prometheus. A tree holding scalar(v) runs as one execution over every scan partition, and the planner admits it only over an unpartitioned Mito table. Data-free statements plan without a scan. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): hold the labels of chunks waiting for a varying scalar Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * feat(promql): read round and clamp parameters from varying scalars natively Bind round, clamp, clamp_min and clamp_max whose parameters are not all constants as a ParameterizedMap with scalar parameter nodes, run through map_math_with_parameters. The parameters are read whole first; vector chunks that arrive before them are held and charged, through the same hold the varying-scalar broadcast now shares. Constant parameters keep the MathOp map. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * test(promql): pin a parameterized map's empty closing chunk Also correct the held-chunk docs, use same-table examples for varying map parameters, and assert the native decline instead of the existing planner's failure. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(query): plan vector(s) natively whatever the table's field is named The fused route declined a root field that is not a raw field, which is every root over vector(s): it names its result `value`. Take the compiled root's fields as they are and decline only a name the output already gives a tag or the time index. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): reserve a constant map parameter before building it Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * refactor(promql): leave scalar(v) to the existing planner until it has its own execution boundary Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): leave data-free statements that generate labels to the existing plan Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): reserve the output point-wise kernels build before running them Signed-off-by: Dennis Zhuang <killme2008@gmail.com> --------- Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * ci: backport scoped heavy CI to narthex (#10053) * ci: backport scoped heavy CI to narthex Backport #9927 and the CI-only changes from #10043. Adapt script references to narthex, preserve manual platform checks, and document the default-branch schedule limitation. Upstream-Commit:ebb7e22566Upstream-Commit:e87766d7efSigned-off-by: WenyXu <wenymedia@gmail.com> * ci: refresh narthex compatibility release window Signed-off-by: WenyXu <wenymedia@gmail.com> --------- Signed-off-by: WenyXu <wenymedia@gmail.com> * chore(ci): slide the compatibility window to v1.3.0 (#10054) Signed-off-by: discord9 <discord9@163.com> * perf(mito): cache native reader metadata (#10015) * perf(mito): cache native reader metadata in the decoded SST tier Signed-off-by: evenyag <realevenyag@gmail.com> * fix(mito): account for partially occupied metadata tree nodes Signed-off-by: evenyag <realevenyag@gmail.com> * style(mito): use absolute cache imports Signed-off-by: evenyag <realevenyag@gmail.com> * perf(mito): trim native metadata cache lookup overhead Signed-off-by: evenyag <realevenyag@gmail.com> * test(mito): bound wide metadata benchmark setup Signed-off-by: evenyag <realevenyag@gmail.com> * test(mito): allow manual benchmark measurement output Signed-off-by: evenyag <realevenyag@gmail.com> * docs: keep series specification focused on the file format Signed-off-by: evenyag <realevenyag@gmail.com> --------- Signed-off-by: evenyag <realevenyag@gmail.com> * feat(promql): route evaluated fragments to their canonical owner (#10038) * feat(promql): route evaluated chains independently by canonical key Signed-off-by: discord9 <discord9@163.com> * test(promql): verify canonical routing key semantics Signed-off-by: discord9 <discord9@163.com> * fix(promql): release closed-output chains and yield routing producers Signed-off-by: discord9 <discord9@163.com> * test(promql): check closed-chain budget before releasing the child Signed-off-by: discord9 <discord9@163.com> * fix(promql): account histogram slices and verify routing completion Signed-off-by: discord9 <discord9@163.com> * fix(promql): finalize contiguous series at repartition owners Wait for successful producer EOF before publishing exact complete metric-name/tag identities. Reuse the existing sample merge for position ordering, complementary fields, and conflicting-cell errors; recompute one final close per owner identity. Keep N producer tasks and M outputs. Admit nonwaiting queue payloads through the query pool, reserve merge workspace, and retain cancellation, failure, memory-release and unread-owner coverage. Signed-off-by: discord9 <discord9@163.com> * feat(promql): publish completed repartition identities incrementally Signed-off-by: discord9 <discord9@163.com> * test(promql): expose histogram amplification in public repartition Signed-off-by: discord9 <discord9@163.com> * fix(datatypes): encode only referenced histogram table rows Signed-off-by: discord9 <discord9@163.com> * test(promql): wait for queued retention before draining a failed read Signed-off-by: discord9 <discord9@163.com> * perf(promql): bound identity rescans and share repeated histogram rows Signed-off-by: discord9 <discord9@163.com> * docs(query): scope the evaluated validator to one chain Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * fix(mito2): close a raw series when its package completes The raw producer only ever marked `is_last` when the next chunk of another series arrived or the input ended, so a series that a scanned package held completely stayed open until that later event. A consumer could not tell a finished series from one still being written, which is why the frontend's raw fan-in has to buffer a whole series before it can merge anything. `FlatSeriesBatchDivider` already emits a package only once it holds every row of the series in it, so the record that ends such a package also ends the last series in it. `raw::make_stream` flattened those packages into a plain record sequence and dropped that fact. Records now carry it, survive being held back a round, and a flagged record ends its group: the group's end and the package's end are the same record, so a record of the next package can never be closed by the wrong boundary. Two things had to follow. The tail primary key is taken before the residual predicates run, because a predicate may drop the run that would have closed the series while the package still holds all of its rows. And closing a package clears `pending`, so the ordering check keeps its own baseline instead of losing the key the next package must exceed. A series that continues past the package that completed it would be split into two runs that both claim to be final, so that is rejected rather than yielded. The prepared columnar source lost the same fact a second way: its per-range readers cut complete series out, but the row-level merge behind them re-cut the stream by batch size. Dividing that stream again restores packages, reusing the divider the legacy producer already uses. Both flat producers declare `KeyThenTime`, which is what lets the closed-key comparison stay a single key; the code states that dependency. Signed-off-by: discord9 <discord9@163.com> * fix(mito2): retain streaming and batching around raw package completion Signed-off-by: discord9 <discord9@163.com> * feat(promql): add a versioned wire format for the native prefix Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * feat(promql): serialize fused native prefixes through substrait Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> * fix(promql): make the native prefix codec agree with itself A payload the encoder accepts has to be one this version can decode. Three places disagreed. An `Input`'s field count was bounded only on the way in, so a 129-field `Input → Instant` encoded to 983 bytes and then failed to decode. The decoder's limit was `MAX_COUNT`, a constant that also bounds nesting depth and `Map` operation count, which is why the missing half was easy to miss; each limit now has its own name and both sides check it. `FusedSeries::serialize` only checked that the tag count fit in a `u32` while `deserialize` bounded it at 1024. Both sides now share `MAX_TAG_COUNT`. `FusedSeries::deserialize` passed an empty schema to `try_new_with_format`, which checks a frame node's declared schema against the layout derived from its tree rather than overwriting it. Every serialized frame node was therefore impossible to rebuild. The schema is derived from the decoded tree now, which is where it comes from in the first place, so nothing is added to the wire. Encoding refuses an over-limit count instead of writing a payload it cannot read back, and a field-count overflow no longer reports "operation count exceeds maximum". `rejects_excess_operation_count` hand-built a payload whose operation count was read from the wrong offset: it placed an `Instant` where the decoder expects the count, so it exercised the limit with 258 while claiming 129. It now starts from a tree the codec accepts, asserts the bytes it edits are the count it means, and checks the error names both the operation limit and the value. The same is added for field count, tag count and nesting. The integration test moves to its own file: it was written against `tests-integration/tests/raw_series.rs`, which the raw-fan-in line creates and this one does not carry. Signed-off-by: discord9 <discord9@163.com> * feat(promql): carry nested math maps in the native prefix The wire format accepted at most one Math map above the selection, so abs(ceil(x)) - which compiles to one map over another - could not be transported even though a map is transparent to shape: it keeps its input samples type and grid, and only the operation kinds are restricted. Walk every map before the selection check instead, and keep rejecting non-math operations at any depth. The tests cover a nested round trip that would notice flattening, a non-math operation under a valid outer map, three distinct levels, and the depth boundary in both directions. Signed-off-by: discord9 <discord9@163.com> * feat(promql): answer whole-vector and multi-metric statements natively (#10025) * feat(promql): name a fused tree's tags from the existing plan A fused node declares exactly the tags the planner gives it, and reads each one back off the series, so a label set this route cannot derive is a label set the answer would lose. The walk over the tree derives labels for a match, a set operation and the point-wise wrappers over them, and names none for an aggregate's groups or a whole-vector operation - which today declines every such statement. Take those columns from the existing planner instead, the way the aggregate split already does: its tag columns are what it projects for the same statement, and they are now shared through one helper rather than written out twice. Only the tag columns are copied, because the node names its field columns after its own tree while the existing planner can name a computed column after its expression. A label rewrite is a shape the walk cannot derive at all: label_replace adds its destination label and an empty replacement removes it, so passing the input's labels through is an inference the rewrite contradicts. Report no label set there. The rewrite itself keeps the existing plan, and this does not change that: the native runtime refuses two series a rewrite gives one label set, where that plan answers them, so admitting the shape would turn an answer into an error. The test that holds this now covers the two-occurrence shape as well. An aggregate or whole-vector root does not reach this branch today: both read across series, so they are planned by the aggregate split or fall back. It is the prerequisite for the placement that runs such a tree in one execution, and reaches the branch when that lands. Signed-off-by: discord9 <discord9@163.com> * feat(promql): run a classic histogram function above the aggregate cut The split accepts a suffix that runs once over the fully merged series, which is why TopK/BottomK were already admitted: the merge has folded every partition's contribution to each group, so the series the suffix reads are the ones the statement's input would have produced in one execution. A classic histogram function is that kind of operation. It reads each histogram off the bucket series that share its other labels and drops le, and the merge has already folded each (..., le) group into one complete series on the evaluated grid, so the buckets it reads are exactly the ones the input would have had. Reading le off those merged series is the same computation, not an approximation. LimitK still declines: it depends on the storage input order, which the merge does not preserve. This is what the o11ybench query `histogram_quantile(0.99, sum(rate(...[5m])) by (instance, le))` needs: it was planned by the existing path only because the split refused a whole-vector operation above the cut. The acceptance test runs the shape over real data and compares the rows two executions report: the fixture spreads six bucket series over several files and a memtable range, so the partial sums genuinely have partitions to merge, and the native answer must equal the complete input's for quantiles at both ends of the interpolation and for histogram_fraction. It also asserts the native run really merged state, so a silent fall back cannot pass it. Signed-off-by: discord9 <discord9@163.com> * feat(promql): read two metric logical tables as two inputs A two-selector statement whose two sides are aggregates kept the existing plan, even where both sides read a metric-engine logical table. The o11ybench query `sum(max by (...) (a)) by (namespace) / sum(b) by (namespace)` is that shape, and it fell back for three separate reasons. First, the engine gate refused every multi-selector metric-engine statement. The clause after it already requires each such selector to be a recognized logical rewrite, so the removed clause refused only the case this change handles. Second, a metric logical table is rewritten to a scan of the physical table restricted by `__table_id = <id>`, so two logical tables over one physical table prepare two scans of the same table. The check deciding whether the node binds one metric or one input per occurrence compared table names only, so the two metrics read as one and both inputs would carry the first one's name - a wrong answer, not a fall back. The id the rewrite already requires is now part of that identity. Third, a tree over two aggregates reads across series, so it is not answered per partition, and the branch for such trees was the aggregate split, which takes one child. A node with more than one input needs no split: it declares one partition and reads all of every input's partitions in one execution, so every group is folded in one place. Such a tree now falls through to the node. The one-selector path is unchanged: it still requires `can_split` and still declines a tree the split cannot cut. So is the multi-selector case whose inputs stay direct scans of one table - such a node is not region-local, as `Categorizer::check_extension_plan` refuses a cross-series plan over more than one scan - which is why the condition is the metric-logical-table one. The acceptance test runs both shapes over two logical tables of one physical table, with values that make a swapped input visibly wrong, and compares the rows the two routes report. Signed-off-by: discord9 <discord9@163.com> * test(promql): log the metric pair's rows through telemetry, not stdout The acceptance test printed its evidence with println!, which the crate lints as an error under -D warnings, and echoed a Display value through to_string inside the format arguments. Every other test in the file logs through common_telemetry::info!, so this one does too. Signed-off-by: discord9 <discord9@163.com> * fix(promql): keep the existing plan for a tree the runtime cannot execute The multi-input route skipped the aggregate split, and the split was what refused a tree the runtime cannot execute: `check_cut` declines a `quantile` and a grouping by a value field. Skipping it turned queries the existing plan answers into errors, because the node is chosen while planning the logical plan and a refusal the physical layer raises later has nowhere to fall back to. `quantile(0.5, a) / sum(b)` over two metric logical tables reported `NotImplemented: the native runtime does not execute the Quantile { .. } aggregate yet` where the existing plan answers it, and `count(count by (val) (a)) / sum(b)` failed on the grouping by a value field, which the outer `count` had hidden from the column check. Ask the runtime before taking the route, with the same construction the physical layer validates by, and keep the existing plan for whatever it refuses. Asking it rather than listing the unsupported kinds is what makes the check cover refusals this planner does not know about. Signed-off-by: discord9 <discord9@163.com> * fix(promql): keep the existing plan for a grouping and a selection the node cannot answer A multi-input node skips the aggregate split, so the refusals the split makes for reasons that are not about what the runtime computes were skipped with it. Two of them matter here. A grouping by the metric name keeps a label no column of the existing plan can name. The native aggregate folds its groups by `__name__` as by any other label, but the label set the node declares comes from the existing planner's columns, and `__name__` is the name a raw frame carries rather than a column of any scan - so the existing planner's `by(__name__)` finds nothing and reports no label. The schema would then report one label set for two groups the tree keeps apart: `sum by (__name__) (a or b)` over two metrics would answer two rows a reader cannot tell apart, which is a series identity lost rather than an answer given. A selection that depends on the order its input arrives in is the other. `limitk` keeps the first `k` series of each group in input order, while a node with more than one input reads its inputs' partitions through a gather that hands them over in the order they became ready: the schedule would decide which series a query keeps. Prometheus defines the selection over a series set. The split declines `LimitK` for the same reason. Every other vector operation orders its input itself - `topk` by label, `sort` by value, `limit_ratio` by a label hash - which is why only this one is asked about. Both shapes keep the existing plan. `limitk` is not answerable by either route today, since the existing planner has no kernel for the operator; the point is that the native route is not the first to answer a selection the schedule would decide. Each guard is checked with the guard removed, so both tests are known to fail without it. Signed-off-by: discord9 <discord9@163.com> * fix(promql): decline unsupported count_values groupings before execution Signed-off-by: discord9 <discord9@163.com> * docs(promql): scope the native capability probe guarantee Signed-off-by: discord9 <discord9@163.com> * test(promql): pin the same-table pair the multi-selector exclusion used to keep out State that a Mito input joins a multi-input tree only when a second metric logical id makes the tree multi-input, and that only LimitK's selection depends on arrival order. Signed-off-by: discord9 <discord9@163.com> * fix(promql): decline ranking grouped by a value field before native execution topk, bottomk and limitk key their groups by series labels, while the existing plan also accepts a value field there and forms one group per sample value. Admitted multi-input trees could therefore keep one row where the existing plan keeps one per group. Validate the grouping when the vector runtime is built so the statement keeps the existing plan. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * fix(promql): bound the prefix depth and name what a refusal names Lower MAX_DEPTH to 32. The old 128 was validated recursively, and a debug build overflows a default test-thread stack well before reaching it, so the depth guard itself could abort instead of refusing. Report the refused node by its own name instead of "unsupported", which is what every kind used to say. Add the multi-partition datanode case. It asserts the failure the review reproduced - a series chain cut by CoalescePartitionsExec no longer decodes - so the limitation stays visible and turns red when the region merge becomes chain-atomic. Reconcile the retained-rate comment with the values the test asserts. Signed-off-by: discord9 <discord9@163.com> * fix(mito2): keep one predicate pass across a brief upstream pause The fill loop awaited upstream only while nothing was buffered, so one Pending split a predicate pass even when the group held no package tail that could close anything. Gate the wait on the package tail instead: without one, breaking out cannot settle a series, so waiting is free. One hundred small record batches separated by Pending now cost one pass instead of one hundred. Package completion is also only usable under KeyThenTime, which is what the one real flat producer declares. Drop the Contiguous package path and its closed-key state: it has no producer, and reaching it would close a series twice. A reopened key is still refused by the ordered-key comparison, now covered under KeyThenTime. Signed-off-by: discord9 <discord9@163.com> * perf(mito2): compare raw series keys without copying them The ordered-key guard built a Buffer for every record before checking whether a previous package key existed to compare against. Read the slices instead, so the common case allocates nothing. Signed-off-by: discord9 <discord9@163.com> * test(promql): pin cross-producer topk to fixed partitions `native_aggregate_topk_over_global_partitions` also asserted that a selected hostname had state from two distinct original producers. That follows from the storage scheduler, not from the merge: a scan dispatches on a channel of capacity one and independent flushes do not fix their output partition, so a legal schedule can leave one partition holding every series but two. The assertion failed intermittently - `[25, 1, 1, 1]` - while the answer itself stayed correct. Move the requirement to a fixture that fixes which partition each contribution arrives on, so it holds on every run. `topk_above_the_cut_sees_every_producer_ contribution` folds `topk(1, sum by (hostname)(...))` over four producers whose contributions make the winner differ between the two grid points and require both major producers to be summed: at point 0 producer 0 holds A=6/B=10 and producer 1 holds A=6/C=10, so A wins on 12; at point 1 the same producers give B=12. Two further producers contribute a small hostname each so all four partitions carry state. Dropping a producer or taking topk inside a producer before the merge both answer a different winner, which the fixture catches - checked by mutating the fixture each way and watching it fail. The integration test keeps everything that follows from this build: the native/legacy comparison, the hand-computed twenty rows, the two points selecting different topk members, the producer and finalize plan shapes, and the four non-empty producer partitions. Its now-unused partial-state recorder is removed with it. Signed-off-by: discord9 <discord9@163.com> * feat(promql): carry the native plan with serde instead of a hand-written codec `native/wire.rs` encoded `NativeTree` by hand: fixed node tags, fixed field layouts, both directions written out, and a `check_shape` that narrowed the transport to `Input -> Instant|Window -> Map(Math*)`. The serialization layer therefore decided which plans could exist. `abs(ceil(x))` compiled but could not be transported, and every new node kind cost a second codec path plus a new whitelist entry. Widening the whitelist to nested maps maintained that interpreter rather than removing it. The file is deleted. The plan types derive serde and the codec is postcard 1.1.3 -- bincode is unusable here because RUSTSEC-2025-0141 marks the whole crate unmaintained with no patched version. postcard is maintained, has a documented stable wire format since 1.0, and writes an f64 as its bit pattern in eight little-endian bytes, so NaN payloads, signed zero, infinities and subnormals survive exactly. What remains hand-written is framing only: magic `NPRQ`, a version, a payload length. The version is rejected before the payload is deserialized, so an unknown version is a clean error instead of a silent misparse, and oversize, trailing bytes and truncation are separate errors. Decoding ends at `NativeTree::try_new`, so a tree the compiler would not build cannot be decoded. Three types keep written impls because they hold invariants. `EvalGrid` serializes its `GridSpec` and rebuilds through `try_deferred` with an empty cache -- the grid is never serialized and never materialized on decode. `SeriesLabels` rebuilds through `try_new`, keeping the sorting and the duplicate / `__name__`-as-a-tag rejection. `LabelRewrite` travels as its parameters with the regex recompiled, and fails to decode if the pattern no longer compiles; compiled internals never travel. `FusedSeries` now carries its output schema and accepts `PlainRows` and `partial_fold`, which it used to refuse. A carried schema is only honest if it is checked, so decode validates every column role, the exact time type and nullability, missing or extra value columns, and the scalar-root tag rule; a `SeriesFrame` schema must equal the frame layout the tree derives, and a partial export must match `state_schema(binding.tags)` including tag order. Identity stops being display text. `FusedSeries` compared `tree.to_string()` with a field patch, `AggregateFinalize` hashed the rendering, and `LabelRewrite` compared its own output; all three are structural now, with floats compared by `to_bits()` so that signed zero and NaN payloads stay reflexive under `Eq`. `same_binding` compared `Arc::ptr_eq`, which put an allocation address into a transportable plan; it compares the ordered fields and the optional grid spec now, distinguishing raw from evaluated. Tests assert the properties rather than the byte layout: a non-canonical NaN payload round-trips by bits, an unknown version is rejected before decode even when the payload is garbage, non-prefix trees (binary, set, aggregate, scalar, evaluated input) round-trip, thirteen malformed schemas are each rejected with their own message, and a multi-chunk chain over a real region is checked against its exact sample count and contiguous timestamps. The multi-partition integration test is split in two and labelled honestly. It used to assert a failure, on the theory that coalescing interleaved multi-chunk chains; its fixture's grid has one position, so no chain has more than one chunk and that assertion was already failing at the base commit. One test now covers the single-position shape and says it does not exercise interleaving; the other covers a 8192-position grid that does. Deserialization is bounded by depth, not only by bytes. The derived recursion walks boxed nodes, and a payload under the 1 MiB limit can nest far deeper than the stack allows, which aborts the process instead of returning an error. A thread-local budget guards `NativeNode`'s deserializer, so a hostile payload is rejected before a complete tree exists. `NativeTree::Deserialize` also routes through `try_new`, so no path can materialize a tree the constructor would refuse. Measured under postcard with no `RUST_MIN_STACK` override: 128 nodes decode, 129 return a decode error. Postcard collapses custom serde errors to its unit variant, so that rejection does not name the nesting limit; the other envelope failures (version, size, trailing bytes, truncation) each keep their own message. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): carry the fused plan as one derived payload The node still framed its fields by hand: the grid spec's integers, a 0/1 marker for the metric name, a length prefix before every string, the tag count, a format tag, and separate length-prefixed blobs for the partial fold and the output schema, with a matching Reader on the way in. That is per-field serialization sitting inside a change whose point was to stop writing per-field serialization, and it kept the maintenance cost: any new field touched both directions, and a test located a byte by computing 8 + tree.len() + 8 + 8 + 8. One derived payload replaces it. The envelope is now a version, a payload length, and a single Postcard-encoded `FusedSeriesPayload` holding the tree, the grid spec, the binding's name/time index/tags, the output format, the statement text, the optional partial fold and the output schema. Substrait still carries the children, which are exactly the fields this format should not be inventing. `text`, `Reader` and `MAX_TAG_COUNT` are gone; the tag bound existed only because a hand-written length prefix needs one. Every carried-schema check stays: the tag-order-sensitive comparison against `state_schema(binding.tags)` for a partial export, exact equality with `series_frame_output_schema` for a frame node, and the full column-role validation for plain rows. Decode also requires the payload to be consumed exactly, so a byte appended after a valid payload is rejected rather than ignored -- `postcard::from_bytes` would have accepted it, which is why the sub-payload framing had that hole. Two tests were reading the format rather than the behaviour: The depth-limit test asserted that `RUST_MIN_STACK` was unset, but CI sets it to 8 MiB, so the assertion failed before the decoder ran and the test could never do its job there. It now runs on an explicit 2 MiB worker stack, which is the stack a normal worker has, and asserts from inside that thread that 128 nodes decode and 129 are rejected. The integration test located the embedded version by assuming an offset and comparing raw bytes. The framing change made that offset stale and it failed. It now finds the envelope, decodes the version word and asserts the number, checks the length word against the payload, and prints the observed bytes when either assertion trips. Signed-off-by: discord9 <discord9@163.com> * fix(promql): bound tree depth on both sides and validate merge inputs The depth limit was decode-only and set to a number nobody had measured. A 129-deep tree could therefore be built, serialized and shipped, while the receiver refused it; and in CI the boundary test itself aborted with a stack overflow, because a 2 MiB thread was asked to build, serialize, decode and drop a chain of that depth, and the recursive drop of `Box<NativeNode>` is not covered by a guard that only wraps `Deserialize`. Measured instead of guessed. A probe walked build, serialize, decode, validate and drop at increasing depth inside a named 2 MiB thread: 32 through 128 completed, 160 aborted. The cap is now 64, half the highest passing depth, and the same number is enforced in `NativeTree::try_new` and in the deserializer. Verified through the real constructor: a 64-node tree round-trips, and a 65-node tree is refused when it is built as well as when it is decoded, so sender and receiver accept the same set. The boundary test runs on a named thread, which is how a future failure will identify itself. The merge node accepted any single input. `with_exprs_and_inputs` rebuilt `AggregateFinalize` over whatever it was handed, on the reasoning that a rewritten input only changes where the state comes from. Nothing checked that it was state at all -- and until now nothing could produce a counterexample, because a values-producing producer could not be serialized. The serializer was standing in for a validation that belonged to the node. `with_exprs_and_inputs` now rejects an input whose schema is not `state_schema(binding.tags)`, so substituting a computed-values producer fails at the logical node with a message naming the expected schema, rather than depending on an encoding limitation. `AggregateFinalizeExec` already had the same check at physical planning; the serializer stays permissive. The three placement tests in `commutativity.rs` asserted the old refusal as their mechanism -- an ordinary fused node "fails to encode", a partial producer "cannot be pushed down". Those expectations were the encoding limitation wearing a test's clothes. They now assert the responsibilities that remain: an ordinary fused node round-trips through the encoder, a partial producer rides to the region while its finalize stays on the frontend, and a values producer handed to a merge is refused by the schema contract. The test-only `NPRQ` envelope is gone. It framed trees with its own version, a 1 MiB cap and its own rejections, none of which production had, so `native_tree_envelope_rejects_version_size_trailing_and_truncation` was proving a helper's protocol while the PR description claimed a production guarantee. Trees are tested through plain postcard, transport through the real `FusedSeries` interface. The redundant clone-and-revalidate in `FusedSeries::deserialize` is also gone: `NativeTree::Deserialize` already routes through `try_new`. Signed-off-by: discord9 <discord9@163.com> * fix(promql): stop carrying an Arrow schema in the fused payload The payload carried a full `arrow::Schema`, which dragged in `arrow_schema::DataType` -- whose `List`, `LargeList`, `FixedSizeList`, `Struct` and map variants recurse without bound through a derived `Deserialize` that cannot be hooked from outside. The tree has a measured, symmetric depth limit; the schema had none on either side, so a small payload nesting `List` a few thousand deep would exhaust the stack during decode, before any post-decode validation could run. Review reproduced that with an 80 KB payload and 8000 levels. The schema did not need to travel. `fused_output_schema` builds every column from three flat inputs and hardcodes every type -- the time column is `Timestamp(Millisecond, None)` non-nullable, values are `Float64` nullable, tags are `Utf8` nullable -- and a frame node's schema comes from `series_frame_output_schema` over the tree's root schema. The only genuinely irreducible part is which columns are labels. So `output_tags: Vec<String>` replaces `output_schema: Schema`, and decode reconstructs the schema instead of trusting one. That is strictly stronger than validating carried data: a mismatched schema can no longer be expressed at all, where before it could be carried and merely checked. The three formats differ, and the difference is now meaningful rather than decorative: - a frame node carries no tags, because its label columns come from the tree; decode rejects a non-empty field instead of comparing a copied value to itself, which is what the previous revision did; - a partial fold carries `binding.tags` and decode requires the exact order, because the state layout is position-sensitive and the binding is the ordering authority; - a plain-rows node carries its output tags, the one case where the labels cannot be derived from the tree. `skip_encodability_gate` in the district analyzer was one flag doing two unrelated jobs: skipping the Substrait encodability check, and bypassing multi-input column requirements on the strength of a placement proof. Making ordinary and partial producers serializable removed the premise of the first while leaving the second intact, so they are two fields now -- `skip_in_process_encodability` and `in_process_fused_root` -- and the placement bypass reads as `has_fused_pair_placement_proof` rather than as "we skipped the serialization check". Signed-off-by: discord9 <discord9@163.com> * fix(promql): let a too-deep tree fall back instead of failing the query The depth limit was reported as `DataFusionError::Internal`, the error a compiler defect gets. The planner only keeps the legacy plan on `NotImplemented`, so enabling the native route turned an answer into an error: a statement compiling to more than 64 nodes -- 70 `time()` terms compile to 139 -- failed outright, where the legacy planner answers it. Switching a feature flag on must never make a query stop working. The two rejections now say what they mean. Exceeding the depth limit is "this statement is beyond what this route handles", which is `NotImplemented` and makes the planner fall back, from both the constructor and the deserializer. Every other rejection in `validate()` -- a mismatched binding, a map over a scalar, a window with the wrong grid -- stays `Internal`, because those mean the compiler built something inconsistent and reclassifying them would hide real defects as unsupported queries. A fallback test pins it: 70 `time()` terms must plan exactly as the legacy route does, with the node count in the message so a future change to the cap explains itself. `postcard` moves into the external-dependencies block. It is declared with `default-features = false`, and that block exists precisely for inherited dependencies whose features are narrowed. Signed-off-by: discord9 <discord9@163.com> * test(promql): restore the scalar-root refusal and drop a duplicated derivation `a_samples_root_without_an_input_is_refused` was rewritten in place earlier in this branch, and the rewrite changed what it covers: it used to build a constant root that reports samples and assert the refusal names the contract ("does not produce a scalar"); it now builds a samples tree with no input and asserts "source indexes". Those are two different contracts, and the original one went uncovered. That gap matters here in particular, because this branch broke exactly that path: deriving output labels from the schema classified a scalar root's value column as a label, which two physical-plan tests caught. The original assertion is restored as `a_scalar_root_claiming_samples_is_refused`, and the samples-without-input case keeps its own test under its own name. `FusedSeries::serialize` also open-coded the same call `output_tags()` already makes, differing only in that the frame arm carries nothing. It now calls the method, so there is one derivation and one place for the format rule. Signed-off-by: discord9 <discord9@163.com> * style: let taplo order the workspace dependency entry `taplo format --check` runs over the whole workspace in CI, not over the two manifests I had checked. It sorts the `[workspace.dependencies]` table, so `postcard` belongs in alphabetical position inside the external-dependencies block rather than immediately after its comment. Local checking was narrower than the CI command, which is why this only surfaced there. Signed-off-by: discord9 <discord9@163.com> * style: place postcard in the sorted position the review asked for Inside the external-dependencies block, `taplo` was willing to leave the entry before the `axum` comment, because a comment line ends a reorder group. The review asked for the alphabetical position, which `taplo format --check` also accepts, so use it. Signed-off-by: discord9 <discord9@163.com> * refactor(mito2): flatten samples artifacts and reset incompatible index caches (#10041) * refactor(mito2): simplify samples paths and version index cache Signed-off-by: evenyag <realevenyag@gmail.com> * fix(mito2): preserve index cache root during reset Signed-off-by: evenyag <realevenyag@gmail.com> * ci: refresh compatibility release window Signed-off-by: evenyag <realevenyag@gmail.com> --------- Signed-off-by: evenyag <realevenyag@gmail.com> * feat(promql): own raw series identities at the repartition boundary (#10058) * feat(promql): own raw series identities at the repartition boundary A raw frame's is_last closes one producer's stream, not the series: the same stored primary key may still arrive from another input. Routing such frames through the evaluated path therefore emits several partial series where a consumer expects one. Teach SeriesRepartitionExec to group raw identities. SeriesRouting selects the grouping key and try_new_with_routing takes an explicit policy, rejecting the combinations a frame's content cannot honour: an evaluated frame may not use RawPrimaryKey, a raw frame may not use CanonicalLabels, and a raw frame must declare explicit labels. Raw routing keys on the complete stored primary key rather than on labels so two pieces of one physical key always meet in one owner, where a disagreement about labels is rejected instead of being split across owners that would each look consistent on its own. A raw identity is published only after every input reaches normal EOF, unlike an evaluated identity, which is complete once each input either contributed it or ended. merge_raw_identity orders the pieces by timestamp, collapses equal duplicates, rejects conflicting ones and emits one closing chunk. The reconstruction's pool charge covers the two parallel row vectors, the timestamps and the worst-case per-row sample slices that alternating contributions produce. This adds the capability only; no node is constructed with raw routing yet. Signed-off-by: discord9 <discord9@163.com> * test(promql): cover raw identity merging and routing source/merge covers the pure merge: interleaved ordering, duplicate collapse versus bit-level conflict, key and label rejection, histogram property comparison and shape mismatch. The router needs its own proof, because the readiness rule and the identity key are what a consumer depends on. A raw child whose partitions are scripted delivers them: one gated input that has already handed over every chunk but whose stream is still open blocks publication until it ends; identical labels with different primary keys stay apart; pieces of one key that disagree on labels fail; and one key with conflicting samples at one timestamp surfaces an error rather than dropping data. The gate holds back only the end of the stream, so the first test distinguishes "every input contributed" from "every input ended". Swapping the raw readiness rule for the evaluated one makes it fail. Signed-off-by: discord9 <discord9@163.com> * docs(promql): correct the cross-partition merge boundary The guide said native execution does not merge one logical series split across partitions. That stopped being true when the repartition owner began finalizing an exact identity's fragments: the merge now happens at that boundary, and the remaining limit is a logical series split across regions. Signed-off-by: discord9 <discord9@163.com> * fix(promql): charge raw chunks their slice and publish complete identities Two problems in the raw path, both about holding memory longer than the data requires. A frame batch decodes into many chunks that are slices of the same Arrow buffers. Charging each chunk the whole allocation charged one batch's timestamps once per chunk: 64 series of 64 rows retained 4.2 MiB for a 32 KiB timestamp buffer, 129 times the real size, and the reservations live until the identity is published. raw_chunk_slice_bytes charges the slice each chunk actually owns, using the same slice-aware accounting the histogram path already used. The same fixture now retains 95 KiB. A raw identity also waited for every input to reach EOF even when every input had already handed over its complete chain for it. read_raw_chains hands a run of chunks over atomically once is_last arrives, and an input cannot contribute the same key twice, so a contribution is final for that input. Readiness is now the same rule for both contents: every input has contributed the identity or ended. The previous test asserted that a fully contributed identity must still wait for an unrelated input's EOF, which pinned the conservative rule as if it were required. It is replaced by two tests, one for each direction: an identity whose inputs all contributed publishes without waiting, and an identity with an input that has contributed nothing waits. Reverting the readiness rule to the old one fails the first and leaves the second passing. Signed-off-by: discord9 <discord9@163.com> * fix(promql): charge histogram indices and steady the raw accounting tests A histogram field is an index array naming rows of a shared table. Charging only the table missed the indices, and a query over few distinct histograms holds mostly indices: 4096 positions naming one row is 16 KiB of indices against a table of a few hundred bytes. Both the histogram and the mixed branch now charge the index array's slice, as the evaluated path already does. The two accounting tests waited by sampling a fixed number of idle ticks. Under load the input task may not have run yet, so the sample reads zero and the test fails for a reason that has nothing to do with what it measures. Withdrawing the index charge reproduces the failure, so the test still discriminates after the wait was made deterministic. Signed-off-by: discord9 <discord9@163.com> * test(promql): gate the producer so the failure cannot drain unclaimed queues The retention this test reads is not guaranteed across `start` and `claim`. `start` spawns the producer before it returns, and a failure drains every queue nobody has claimed yet, releasing the charges those queues held. A producer that reaches its error first therefore leaves the test claiming two empty queues, and "queued output chains retain memory before drain" can never become true. That is the shape the suite now hits on this machine: the baseline commit fails this test in about ten seconds when the suite runs concurrently with another one, while the same test written with the gate passes every time. The producer now waits on a switch before its error, and the test claims both queues, waits for the retained charge, and only then opens the switch. The assertion keeps its meaning because nothing can drain the queues before the test owns them. This is a test fix. The production cleanup it exposed is correct behaviour: a queue no consumer claimed should not keep holding memory. Signed-off-by: discord9 <discord9@163.com> * feat(promql): route raw frames by a match key A vector operation between two raw sources needs the series of one match key in one partition, and the node could not give it that: raw frames were restricted to key routing, so `100 * (1 - a / b)` could not be planned at all. The restriction rested on an inference the code made when it started a read: the routing policy named the frame content, so only one policy could mean raw. That inference stops holding once a raw frame may be routed by a match key, and the content is now read from the child's own declaration instead. Choosing a destination and deciding a fragment's identity are separate jobs. A match key picks the partition by hashing the labels the frame already carries -- raw frames are required to declare them -- while the stored key still decides which identity a fragment belongs to. Both keep one key's fragments in one partition, because one key always decodes to one label set, so merging is unaffected. Canonical labels stay refused for raw frames: they include the metric name, which is not a match key. Signed-off-by: discord9 <discord9@163.com> * fix(promql): report a raw chain that cannot pick a destination Two paths hid a destination they could not compute. A matching-routed chunk without labels built an error and then discarded it through `unwrap_or_else`, routing the chunk by its stored key instead; and `output_labels` answered the stored-key policy with nothing at all, so every chain would have gone to one partition silently. Neither is reachable today -- a raw frame is required to carry explicit labels, and a stored-key-routed frame never asks for a label hash -- but a silent fallback turns a defect into a wrong answer. Both now say what went wrong. Signed-off-by: discord9 <discord9@163.com> * test(promql): cover the histogram properties a duplicate check has to compare Merging two raw pieces treats a sample seen twice at one timestamp as a duplicate only when every property of the two matches. The comparison already covered all of them, but its test changed only the float ones, so dropping `reset_hint`, `start_timestamp` or `custom_values` from the comparison left every test passing: conflicting samples would have been discarded as duplicates with no failure. Each of the three now has a case that differs in exactly that property. Removing any of the three comparisons fails the test. The bucket side is deliberately not covered: a legal sample must satisfy `span_lengths` summing to the bucket count with an in-range cursor, so a sample differing in one bucket field is a different shape rather than a different value. Signed-off-by: discord9 <discord9@163.com> * perf(promql): publish an ordered identity as its inputs pass it, and charge what a merge uses A key-then-time identity could only be published after every input ended, so the node buffered a whole region even though the ordering it was protecting is exactly what lets it publish early. An input emits its keys in order and each chain goes to one owner, so the keys one owner sees from one input are non-decreasing too: a greater key from an input proves that input will not contribute a smaller one. The owner records the greatest key seen per input and publishes the pending identity with the smallest key once every input has contributed it, ended, or been seen past it. Readiness is checked in ascending key order, so the output stays ordered. An identity carried by a single piece needs no reconstruction at all: it is marked final and published as it is, which is what the evaluated path already did. A multi-piece identity charges each allocation the reconstruction actually makes — the two row vectors, the timestamps, and one descriptor per surviving run — instead of reserving a worst case per row per field that a well-ordered identity never approaches. The two tests for the ordered path were written against a fixture where the key they expected to be held was already complete, so they asserted the opposite of the rule. They now use fixtures where the held key really has not been passed, and they collect rows per identity because a frame may split one identity across chunks. Signed-off-by: discord9 <discord9@163.com> * fix(promql): drop the uncharged merge wrapper the charging path replaced Routing a raw identity charges each allocation before it is made, so `merge_raw_identity` had no caller left outside the tests and `cargo clippy -p promql --all-targets -- -D warnings` refused it as dead code. Its body is now the test helper that calls the charging form with a no-op reserve, and the production name is gone. Signed-off-by: discord9 <discord9@163.com> * fix(promql): stop scanning settled identities, and keep merge errors typed Three faults in the raw owner, each small: A key-then-time frame publishes only in key order, so if the smallest pending identity is not ready no larger one can be: an input reaches a later key only by passing the earlier one. The rescan applied that predicate to every pending group anyway, so an input that fell behind could have the owner walk the whole map after each arriving chain it could not yet settle. Ask the first entry only. `reserve` returning `ResourcesExhausted` came back through `invalid(...)`, so a memory-pool rejection reached the caller as a malformed-series error with the wrong status. A reservation failure is not a claim about the data; hand the `DataFusionError` through untouched. The merge reserved its row and selection scratch up front and kept holding both after `rows` and `selected` were dropped, since the reservation outlives them until the packer takes the output. Release the pair once the merge is done. The release amount matches the two reservations, so it can only fail if the reservation was already reduced elsewhere, which is why the assertion checks it but the query still succeeds. Signed-off-by: discord9 <discord9@163.com> * test(promql): assert the decode a chain-atomic merge produces This case asserted that unpacking fails with "evaluated series identity changed before its chain closed". It was written to keep that boundary honest until the region merge became chain-atomic, and its own comment asks for this change once that landed - which it has: unpacking now succeeds. The success it asserts is the same property read the other way. Sixteen hosts, one chain each, each closing exactly once, each reporting 1.0 - the rate its own samples produce, since the fixture's value rises one per second. A merge that started interleaving chains again would cut them, and a cut chain cannot report one correct value per host. The case's evaluation grid holds one step, so it says nothing about how a chain spanning several chunks behaves; that is a property of the grid, not of the merge, and the comment now says so instead of claiming four batches per chain. Signed-off-by: discord9 <discord9@163.com> * test(promql): cover the gaps the review named, and drop two false claims Four independent gaps in the raw repartition tests, plus one comment that asserted something the code does not do. `RawScript` reported `SeriesOrdering::Contiguous` regardless of what its plan declared. `pack_raw_series` derives both the frame metadata and the `SequenceValidator` from that one value, so every `KeyThenTime` test packed `Contiguous` batches and validated them under the `Contiguous` contract — `SequenceValidator`'s "raw series keys are not ordered" check never ran. Carry the declared ordering through `RawScripted` and hand it to the stream, so the batches and the check agree with the plan. `raw_matching_routing_groups_by_match_key` asserted only that both identities shared an output, which holds under primary-key routing too because those two keys collide in a three-output layout. Assert the owner against `output_labels` of the match key instead, so routing by the wrong key fails. The test named for waiting asserted early publication through the watermark. Rename it, and cover the other side deterministically with `gate_chunks`: an identity is withheld while another input has delivered nothing, then publishes once the gate opens. The two lifecycle tests set `gate_chunks: true`, which blocks the *first* chunk, so nothing was ever charged and their wait for a reservation could not be satisfied. Let the chunks through and keep the gate closed at EOF: the chain is delivered, charged, parked, and then dropped. `histogram_duplicates_compare_every_property` claimed the bucket side was unreachable because a legal sample must satisfy layout constraints. Only the bucket *count* is constrained; bucket values are free. Add a case per property — schema, count, zero_count, sum, zero_threshold, either side's bucket value, either side's span offset and span length with equal counts, plus the existing reset_hint, start_timestamp and custom_values — so dropping any comparison from `histograms_equal` fails. `native_prefix_multi_partition_series_chains` said the region merges chain-atomically. It does not: `CoalescePartitionsExec` still interleaves partitions batch by batch. The decode passes because #10023 closes a region's pending raw chunk at package completion, so a chain's value and its close are no longer latched apart. Say that, drop the unsupported "four batches per chain" count, and note that a one-point grid gives one value plus a possible empty close. Signed-off-by: discord9 <discord9@163.com> * perf(promql): merge ordered raw pieces without a row index `merge_raw_identity_charging` rebuilt a `(timestamp, chunk, row)` entry for every row of every piece and sorted the lot, then walked it again to drop duplicate timestamps. Reconstructing one series therefore cost `24 * rows` bytes of transient workspace and an `O(n log n)` sort, even when the pieces already arrived in order. The validation above that point has already refused a piece whose own timestamps are not strictly increasing, so a piece never needs sorting internally. What is left to order is only the relation between pieces. When every adjacent pair satisfies `previous.last() < next.first()` the concatenation is already the merged sequence, and no timestamp can repeat across pieces, so neither the index nor the duplicate walk is needed. Keep the general path unchanged for the interleaved and touching cases - touching pieces share a timestamp, so they still need the duplicate check - and shrink only what the path actually reserved. Signed-off-by: discord9 <discord9@163.com> * test(promql): vary histogram spans one dimension at a time `histogram_duplicates_compare_every_property` varied `span_offsets` and `span_lengths` together in the two length cases: the baseline was `offsets: [0], lengths: [2]` and the cases used `offsets: [0, 0], lengths: [1, 1]`. The offsets comparison rejected them first, so removing the lengths comparison left the test green and those two cases pinned nothing. The baseline is now two spans in both sides (`offsets: [0, 0]`, `lengths: [1, 1]`). The offset cases change only the cursors (`[1, 0]`) and the length cases only the lengths (`[2, 0]`), keeping the cursor list, the span count and the bucket count fixed, with a zero-length span as the only difference. Ablating any one of the four comparisons - positive and negative offsets, positive and negative lengths - now fails the case that covers it. `single_chunk_uses_ordered_fast_path` is removed. It passed with the fast path forced off, so it did not pin path selection, and the owner never reaches the merge with one chunk: `finish_raw_group` returns the chunk after setting `is_last`. The fast path stays pinned by `ordered_disjoint_pieces_concatenate_without_row_workspace`, which asserts the exact reservation and zero shrink for disjoint chunks, and by `touching_ordered_chunks_use_general_duplicate_path` on the other side. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * fix(promql): keep a plain-rows schema in the column order it was built with Decoding rebuilt a PlainRows schema in a fixed order — time, then value fields, then tags — so a legal node whose schema was `[value, host, __name__, time]` came back as `[time, value, host, __name__]`, and a scalar root's customised value column came back as `value`. The planner happens to emit that fixed order today, so no ordinary query hits it yet, but `MergeScan` compares schemas by position, which makes a reordered schema a different schema. The wire now carries `output_columns`: the flat, ordered output column names. Decode reconstructs in that order and still derives each type from the role the name plays, so no recursive Arrow schema comes back — that was the previous blocker, and the point of dropping it was that only the ordering is irreducible. Before rebuilding, the carried list is checked as a set against the tree's value fields, the tags and the time index, so an order that names something absent, omits something present or duplicates a name is refused rather than quietly repaired. The test that used to catch this had been replaced by per-index assertions written against the new order, which pinned the defect instead of the contract. `assert_eq!(decoded.schema(), node.schema())` is back, and a fixture uses `[value, time, host]` so the assertion is load-bearing: reverting the reconstruction to the fixed order makes it fail. A frame node is unaffected. Its schema comes from `series_frame_output_schema` over the tree's evaluated root, so it never had a flat list to reconstruct out of order; the frame layout is what that derivation defines. Signed-off-by: discord9 <discord9@163.com> * fix(promql): carry fused output schema without reconstruction Signed-off-by: discord9 <discord9@163.com> * perf(query): reuse root plan encodability result Signed-off-by: discord9 <discord9@163.com> * test(promql): exercise depth fallback with a legacy-plannable query Signed-off-by: discord9 <discord9@163.com> * feat(promql): decide a duplicate match group across partitions (#10081) * refactor(promql): report every duplicate point from overlap `overlap` returned only the first point where two members of a group are present and a caller-supplied predicate held, so the caller could not tell which other points had a duplicate. Turn the predicate into a callback and invoke it for every such point in ascending order; the caller decides what a duplicate point means. The one caller reproduces its previous behaviour exactly: it ignores points its gate excludes and keeps the smallest point per group, which is what the group fold already took. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): number the binary runtimes of a tree `RuntimeNode::build` walks the compiled tree in pre-order, so a counter threaded alongside the input ids numbers each binary node deterministically: the same tree yields the same index on every partition, because every partition builds from the same compiled tree. That gives per-node execution state a key without depending on pointer identity. Signed-off-by: discord9 <discord9@163.com> * feat(promql): reduce duplicate findings across partitions A partitioned match cannot tell whether the other operand has a sample at a point, because each partition holds only its own slice. This adds the reducer that makes that decision possible without deciding anything yet: per binary node and field, it merges the operands' presence as partitions publish it and keeps the duplicate findings they report. Findings are retained regardless of what the gate says at the moment they arrive. A partition that publishes its presence late must not cause an earlier partition's finding to be discarded, so the gate is applied only when the merged presence is asked for a decision, with the same formula the kernel uses locally: `lhs && rhs`, or `lhs || rhs` where a fill value makes a point live with only one side. Presence and the retained findings are charged to the pool as they are added, by their real owned bytes, and a refusal is reported rather than swallowed. Storage is bounded by the grid length: one presence flag per point per operand and at most one finding per point, replaced only by one with a lower rank. Nothing constructs this yet. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): carry duplicate evidence down to the kernel Plumbing only: `FusedSeriesExec` holds a plan-instance slot for the execution's duplicate evidence, `NativeExecutionState` and every `BinaryRuntime` carry a handle to it, and the three vector-match layers that reach `binary_field` accept it. Nothing reads or writes the evidence yet — every call site passes `None` or forwards what it was given — so the crate behaves exactly as before. The slot starts empty in `build` rather than in `execute`: the partitions of one execution are separate `execute` calls on the same plan instance, so a slot created per call would give each partition its own state, while a rebuilt plan is a different execution and gets a fresh empty slot from its own `build`. `BinaryRuntime` gains a `slot(field)` accessor for the index the compiler already assigns it; the next change is its first caller. Signed-off-by: discord9 <discord9@163.com> * feat(promql): publish what a field's match saw, without deciding yet `binary_field` now publishes each side's per-point presence into the execution's evidence and records every duplicate point `overlap` reports, including the ones the local gate excludes. Recording them is the point: a partition whose finding arrives before the other side's presence is known must still be able to fire once that presence does arrive, so the gate is applied when a decision is asked for, not when the finding is made. Each published field carries its node and field index, which is what keeps two binary nodes or two fields from sharing presence and findings. The message a recorded candidate carries comes from the same helper the immediate error uses, so a deferred report and a direct one say the same thing. Nothing decides anything yet: `evaluated` is still computed from the local presence vectors exactly as before, and `settled` has no caller outside the tests. `FieldEvidence` groups node, field and handle so the kernel signatures gained one parameter rather than three. Signed-off-by: discord9 <discord9@163.com> * feat(promql): report a duplicate match group found on another partition A partitioned match decides whether to check a duplicate group from the presence of the other operand, and a partition only holds its own slice of that operand. So the partition holding `job="other"` had no left-hand data, skipped every point, and answered where Prometheus refuses the query. The findings a partition makes are now decided once the whole execution has reported. Each partition publishes its operand presence and records every duplicate point it sees, including points its own gate excludes; the partition that finishes last holds complete evidence and asks for the earliest point that is eligible once all the presence is merged. Earlier partitions return their rows and their stream ends normally, so nothing waits on a partition that is never polled — an abandoned execution simply never reaches a verdict, and a failing one aborts its claim rather than being counted as having contributed. Presence is merged per binary node and field, so two nodes or two fields cannot share a verdict, and `filling` is recorded once per slot: a second publisher that disagrees is an error rather than a silent pick. The gate itself is unchanged, which is why a point whose other operand is genuinely absent still answers instead of erroring: the merged presence still says that point is not live. Signed-off-by: discord9 <discord9@163.com> * fix(promql): size duplicate evidence by the grid, not by partition count `DuplicateEvidence::new` stored the output partition count as its grid length. Those are unrelated numbers, and the caller passed `partition_count`: `publish` kept the operand presence for at most that many points and `record_candidate` returned success for anything past it. With two partitions and a five-point grid, a duplicate group at point four was discarded by the very check that exists to find it, and the query answered. Take the point count from the tree's evaluation grid and the partition count separately. Every claim now leaves the counter exactly once, whether it completes or aborts, and the exit that closes the last claim releases what the evidence holds: the verdict is kept only when the run was not aborted, then the slots are cleared and the reservation released. Previously the last completion shrank the reservation while the vectors and strings stayed allocated, and an aborted claim could leave another claim's exit unaccounted, so the charge outlived the run. Candidate storage becomes a sparse map keyed by point instead of a grid-length vector, charged before it grows: the group's strings, its vector capacity, the candidate itself and a per-entry estimate. A grid thousand points long with one duplicate no longer pays for a thousand empty slots, and the charge now covers the containers it claims to cover. The execution test evaluates a five-point range at parallelism two — more points than partitions — with the duplicate at the last point, which is exactly the boundary the partition-sized array dropped. Its control runs the same duplicate against a left side that is absent at every point and still answers. Signed-off-by: discord9 <discord9@163.com> * fix(promql): decide a partitioned duplicate from the whole execution's presence A partitioned match decided whether to check a duplicate match group from the presence of the other operand, and a partition only ever holds its own slice of that operand. `left{job="api"} / on(job) right` with two `job="other"` series on the right returned `5.0`, because the partition holding `other` had no left-hand data and its gate was false at every point, while Prometheus checks every group of the one side whenever the other side is non-empty at that point. Each binary evaluation now publishes both operands' presence and records every duplicate point it sees, including the points its own gate excludes, because a finding that arrives before the other side's presence is known has to be able to fire once that presence does arrive. The partition that finishes last reduces the evidence and asks for the earliest eligible point. Presence and findings are keyed by binary node and field: a tree can hold several binary nodes, and the kernel evaluates fields separately with presence read from that field's own operand. No partition waits on another. A slot takes its length from the operands it is published with rather than from the statement's grid, because a binary inside a subquery runs on that subquery's own grid, which can be longer. A partition that publishes a different length for the same slot is an error instead of a truncation. The evidence exists only when the node has more than one output partition and the tree holds a binary with two vector operands; otherwise the local gate already sees every operand and there is no verdict to reach. A claim for a partition this generation already issued, or one arriving after it finished, starts a new generation, so a second execution of the same plan does not join an accumulator whose claims were spent. A retained finding holds its match group behind an `Arc` with the side and timestamp, and the error text is built only when a verdict is reported, so recording a duplicate point costs no formatting. Signed-off-by: discord9 <discord9@163.com> * perf(promql): record a duplicate only where this partition cannot decide it A point whose merged-in-partition presence makes it evaluated already aborts the execution from the partition that sees it, so storing it in the shared evidence took the lock to record a verdict nobody would ask for. Only the points whose local gate is false are recorded now, which is exactly the set whose eligibility depends on presence this partition does not hold. Both readings are in the comment at the call site, because the previous round's test depended on recording every point and the next reader should see why that changed. Signed-off-by: discord9 <discord9@163.com> * fix(promql): decide an evidence generation from its state, not from call order `claim_evidence` inferred an execution boundary from "this partition has not claimed yet". Cancellation breaks that inference: an execution that claimed partition 0 and was dropped leaves an unfinished generation behind, and the next execution claiming partition 1 joins it; only when that same execution reaches partition 0 does a new generation start. The two partitions of one execution then sit in different accumulators, and a duplicate that only the merged presence can decide is missed. No two executions have to be alive at once for this. A generation is now dead when it has finished or when any of its claims was aborted, and a claim for a partition that a live generation already issued is an error that names the cause instead of silently starting a new one. The previous review allowed refusing re-execution outright, and this is that: any inference from call order is a guess that cancellation can invalidate. The consequence is stated where it is caused: a plan instance whose first execution claimed some partitions and never claimed the rest has an unfinished generation that no later claim can join. That is the price of refusing re-execution; a legitimate second execution would need an explicit execution lifetime rather than a heuristic over partition indices. Signed-off-by: discord9 <discord9@163.com> * fix(promql): keep duplicate evidence single execution Signed-off-by: discord9 <discord9@163.com> * test(promql): pin disjoint duplicate groups and trim unused ranking Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * fix(prometheus): report the metric name only where the aggregation keeps it (#10108) * fix(prometheus): report the metric name only where the aggregation keeps it An aggregate response carried a `__name__` label the aggregate does not have. The response layer derived the name from the selector and attached it whatever the aggregation did, so `sum(alpha_total{job="api"})` answered with `{"__name__":"alpha_total"}` where Prometheus answers with no labels at all, and `sum without (host)` likewise kept a name the aggregation discards. Prometheus keeps the metric name on an aggregate only when the aggregation groups by it: `by (__name__)`, or a `by` list containing `__name__`. Every `without` drops it, including `without ()` and exclusions of labels that do not exist, and an aggregate with no grouping modifier drops it too. The aggregate function does not matter, and neither does whether the selector names a metric. The rule is stated once now: keep the name only for a `by` whose labels include `__name__`, and clear otherwise. A chain of guards left the ungrouped case to fall through to the selector, which is how the name leaked in. The previous behaviour had a test asserting it, so the suite agreed with the code and neither agreed with Prometheus. The expectations here were measured against Prometheus 2.53.0 across all four modifier shapes and five aggregate functions before being written down. Signed-off-by: discord9 <discord9@163.com> * fix(prometheus): keep the metric name for the aggregates that select series The naming rule added for aggregates was applied to every operator. It holds for the ones that fold a group's values into one: those drop the metric name unless the grouping names it. But `topk`, `bottomk`, `limitk` and `limit_ratio` return some of the input's series with their labels intact, so the name survives them, and the grouping modifier only decides which bucket to select from. Applying the folding rule to those four stripped `__name__` from a response that used to carry it - on the default path, not only under the native route, so every deployment saw it. Read the operator before the modifier: the four selectors recurse into their input and keep whatever name it contributes, and every other aggregate keeps the measured rule. Recursing rather than synthesising also keeps a name the inner expression already dropped, so `topk(1, sum(cpu_usage))` stays nameless. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * fix(promql): let native compilation and execution decide admission (#10071) Replace legacy output probing and selector allowlisting with native capability checks. Signed-off-by: discord9 <discord9@163.com> * feat(promql): scan every metric a nameless selector matches (#10106) feat(promql): answer a selector that names no metric by scanning each it matches A PromQL selector with no metric name used to be a planning error. It is now answered by enumerating the schema's logical metric tables, compiling one source per match under one union input, so the fused producer still reads what the compiler prepared: one compiler source per selector, not per metric. Each source names the metric it reads, in its own tag list. That is what makes this work at all: two metrics whose ordinary labels are equal are told apart by name alone, and nothing else in the response can carry that identity. Where the name survives is then a property of the operator rather than of discovery, so the same tag walk decides it for every query - the walk now drops the name where the runtime drops it, for a range function, a math or histogram kernel, a parameterized map, a timestamp selection and an arithmetic or `bool` binary operation, and keeps it for a filtering comparison. `sum by (__name__)` keeps it by grouping. The walk already had that same divergence without discovery: no metric-engine logical table carries `__name__` as a column, so a named selector's tags did not include it and the walk's answer was incomplete in the same way. Discovery only made the column real. The metric engine checks that two logical metrics agree on their time column's unit, not on its name, so each source's bound is built from the column that source actually carries, and a source without one is refused rather than read unbounded. Two upstream contracts assumed one table per selector and are widened rather than bypassed: the time-bound pushdown walks a union's sources, and a fused node may declare `__name__` when its inputs each name their own metric instead of binding one name itself. Declining still means the query goes to the existing planner; shapes this route cannot derive keep the plan they had. Signed-off-by: discord9 <discord9@163.com> * feat(promql): bound metric discovery by the caller's table grants (#10107) Discovery enumerates the schema's logical metric tables, which is a wider read than a caller's grants necessarily allow. The frontend now answers "which of these may this request see" before the planner touches the catalog, and the planner filters on that answer rather than on its own enumeration. The scope is process-local state on the query context, skipped by the builder the same way the other local-only fields are: a remote node must not receive a scope that widens what it may read. It defaults to `Unavailable`, so a path that did not prepare authorization cannot discover, and the validator turns "should have been set and was not" into an internal error instead of a silent unfiltered read. The named tables of a mixed query are checked too. The complete extractor answers "no tables at all" when any selector is unresolvable, and that was exactly the selector that made the query enter the discovery branch - so `beta_total{job="api"} or {job="api"}` used to lose `beta_total` from the check. Both the allowed and the unrestricted arms now take the partial answer. A denied metric is skipped rather than failing the query, which is what makes discovery mean "every metric you can see". The split route rejects a query whose name matchers reach a denied table, so the same query answers differently depending on the request header; that difference is stated where the skip is made. The planner's retain runs before any source is prepared, so a denied table's name reaches neither the plan nor an error message. It filters with borrowed keys, one pass to build the set and one lookup per metric. Signed-off-by: discord9 <discord9@163.com> * feat(promql): preserve scan parallelism through native frame boundaries (#10055) * fix(promql): let a closed chain keep its charge until the packer is done Four places released the frame reservation: the early-publish path handed it to the packer, but the queue drain and the end-of-input path freed it in place. Those two run while the packer still holds rows it has not turned into batches, so a series spanning several batches went unbilled once its last piece was handed over. Every emit path now hands the charge over, and the packed stream frees what it holds: what is left when it is polled and pending is the one identity it is turning into batches, and nothing survives its end or its drop. The identity guard is also widened: within one source every series shares its metric name, so a node that drops `__name__`/`__type__`/`__unit__` -- which is what a range function, a math map and the timestamp shape do -- still maps distinct series to distinct labels. Only rewriting an ordinary label can make two equal, and that is what the guard refuses. Ordinary `rate` and arithmetic frame output therefore publish as their chains close instead of buffering a partition. Signed-off-by: discord9 <discord9@163.com> * fix(promql): keep a cross-source tree's partitions, and gather a raw one only when it must Three defects in how this node decided a tree's execution layout. A match whose two operands derive from the same source still needs its match keys co-located, but only the two-source case was recognized. `x - 2*x` with `on(host)` therefore fell through to single-partition execution and lost the parallelism the scan gave it. A raw input never received a matching router: the normalization branch returned it unchanged unless the whole tree was global, while the builder requires a direct matching router for any multi-partition matching-local input. A two-source raw query was rejected at planning. Nothing reconciled the partition counts of different sources either, though the builder requires one common positive count. A single count is now computed for the matching-local case and every router under it uses it, gathering when that count is one. The window restriction also ran before normalization decided the layout, so a tree about to be gathered could be refused for a limit that only applies to partitioned windows. Two tests changed with the behaviour rather than around it: hash routing does not promise which partition holds a given series, so that test compares results instead of placement while keeping its partition count, per-partition execution and operand-order assertions; and a test that pinned the old refusal of a multi-source frame now pins its acceptance. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): keep a raw source's scan partitions instead of gathering them `RawSeriesSourceExec` declared one partition and read every child partition inside `execute(0)`, so a cross-table node lost the scan's parallelism before its inputs reached it. It now declares the child's own count, executes exactly the partition it is asked for, and re-labels that partition's frames. That leaves `SeriesGather` with no consumer: it existed to interleave several partitions into one stream. The module is gone rather than kept as a hidden N-to-one path a later plan could quietly fall back on. Signed-off-by: discord9 <discord9@163.com> * fix(promql): publish a raw source's frames under the ordering it declared `pack_raw_series` derives the frame schema from the stream's own ordering, and `RawSeriesSourceExec` fixes that schema at plan time from a constant. Passing the scan's stream through unchanged broke that agreement: a columnar scan promises key-then-time, so the region published key-then-time frames while the frontend still expected the schema it had built, and `MergeScan` refused them. The forwarding adapter restates the ordering this node declares. It is the weaker promise, so a scan that keeps a finer one still satisfies it, and every other stream property is forwarded unchanged. Signed-off-by: discord9 <discord9@163.com> * fix(promql): route a raw frame that names its labels by its key A region cannot hand out complete labels: it has no metric-name binding, so `prepare_raw_scan` refuses an explicit binding outright. What it hands out is the stored key plus the description a decoder needs, which is the normal input to this node rather than a case to refuse — every single-table selector is planned that way. The node's constructor refused exactly those frames, so any plan that inserted a router under a single-table selector failed before it ran: the matching policies this branch adds, and the one-partition normalization of an evaluated tree. The node publishes labels, because that is what it groups and closes identities by: `finish_raw_group` reads `chunk.metric` and the merge needs it too. So the input is decoded once, at the unpack boundary this node already has, and an already-labelled stream passes through it untouched. The frame it declares is rebuilt from the input's own declaration instead of being compared against it, which keeps the check that a frame this node cannot re-declare is refused while letting input and output differ in how they name the same series. `frame_schema_with` becomes public for that rebuild, since it is the one place that decides what a declaration is. `FusedSeries` now describes the router's output rather than the scan's when it wraps a raw input, because the binding it hands `build` has to match the frame the router actually publishes. Tested through the node directly: a key-labelled input routes at all; matching puts both pieces of one label set in one owner and closes it once; and `RawPrimaryKey` still hashes the key rather than the decoded labels. Signed-off-by: discord9 <discord9@163.com> * test(promql): measure a routed pair by the width it routes to Four assertions still described the layout this branch replaced: a node with two inputs used to read every partition of both into one and answer one partition, and that gather is gone. The pair is now matched in the partitions it was scanned in, so the fused node answers as many partitions as its inputs route to. Each of these fixtures scans with four partitions, and on the base of this branch all five tests passed, so this is a behaviour change this branch made and did not re-verify rather than a defect: the value assertions behind these shape assertions pass unchanged, in both the native and the legacy comparison. The width is read from the routed children instead of being written as four, and the scan is found by walking down through the nodes that only pass its frames along, since a routed input is no longer a bare `SeriesScanExec`. Signed-off-by: discord9 <discord9@163.com> * fix(query): normalize a multi-partition series-frame root A query engine hands its plan to one stream, and for a plan with several partitions that means `CoalescePartitionsExec` - ordinary batch coalescing. A series frame cannot survive it: one stored series may be split across partitions, and a chunk's `is_last` closes that producer rather than the series, so interleaving two partitions' batches produces a stream whose decoder refuses it (`raw series key changed before final chunk`). Evaluated frames have the same shape of problem: their validator keeps one open chain, and a batch from another partition interrupts it. The engine's root is also a region's root, which is how a cross-table native statement reaches this: its per-region sub-plan is rooted at a partitioned `RawSeriesSourceExec`. So the rule belongs at the physical optimizer's end, before the final sanity check, and it normalizes the root and nothing else - `FusedSeries` already inserts its own owner wherever a consumer needs one. A single-partition root is left alone: one producer is the whole answer. A key-labelled raw root is refused rather than coalesced, because the router checks that one stored key decodes to one label set and it needs the labels to do that. No production plan reaches that rejection today - the only producer of a key-labelled frame is the region's series scan, and every plan that executes it puts `RawSeriesSourceExec` or `FusedSeries` above it, both of which publish explicit labels - and the rejection is what keeps it that way. `schema_check` stays enabled: for the roots this rule accepts, the router re-declares its input's frame with the same content, label mode, decode description and ordering, so the schema it publishes is the one it was given. Signed-off-by: discord9 <discord9@163.com> * feat(promql): admit an evaluated frame as a fused input The extension planner hands a child straight to the fused executor when that child already publishes a series frame, and until now it only recognised a raw one. A segment's output is an evaluated frame, so a statement planned as two segments could not be bound above the first one. The check it replaces asked for a raw frame; the property that matters is that the frame names its own series, which is what `labels() == Explicit` says - evaluated frames always declare it and carry no decode description. Everything else stays with `FusedSeriesExec::build`, which already refuses a mixed-content node, an evaluated input that is not contiguous, and a frame whose declaration does not match the binding it was given. Signed-off-by: discord9 <discord9@163.com> * feat(query): plan a global topk under an aggregate as two segments `sum(topk(1, m))` needs the whole vector for its ranking, which is a property of the tree rather than of the statement, and the routes that existed could only answer a whole-vector tree in one partition: the statement fell back to the existing planner. Splitting it lets the per-series prefix keep the scan's partitions while the ranking sees every candidate. The cut goes below the `topk`, not below the `sum`. A segment holding a `topk` reads across series, so it would gather its own input before evaluating the prefix - correct numbers, no parallelism, which is the thing the split is for. Below the `topk`, the prefix runs on the scan's partitions and the ranking and the fold above it run once, on the child's evaluated frame. Both segments are built from one compiled tree: the child is the prefix subtree, the parent is the original tree with that subtree replaced by an input bound to the child's frame. Nothing is re-parsed, so the grid, the matching rules and the bound scans are the ones the compiler produced. The parent needs its output columns without asking the existing planner to name a computed column after its expression, so `fused_output_tags` now derives the grouping labels of an `Aggregate` and passes a selecting `Vector`'s input labels through. It derives output *columns*, not a complete runtime label set: the metric name is bound separately from the tag columns, and a grouping by `__name__` keeps that name in the labels while contributing no column. `fused_root_schema` is that derivation, shared with the ordinary route. The segmenter is its own method, boxed at the call site. Inlined, its future made every frame of the planner's recursion large enough to overflow the stack on a deeply nested statement - `test_parse_or_operator` aborted before this. Signed-off-by: discord9 <discord9@163.com> * test(promql): pin the two-segment topk plan and what it keeps The statement's two segments do not land in one physical tree: the prefix has one scan for a child, so the categorizer classifies it commutative and pushes it into the region, while the aggregate above it stays at the frontend. The observer therefore records every plan a query engine wraps, not only the first, so the test can look at the region's own root. What the assertions pin, and why each one is worth its brittleness: - the answer is the global maximum, not a sum of per-partition winners; - the frontend segment holds the complete sum and the global topk over an evaluated input, with no instant evaluation left in it; - the region's segment holds the prefix and its output is an evaluated, explicitly labelled, contiguous frame; - that segment keeps more than one output partition, which is the whole point of cutting below the topk; - the region's root is the series-aware router, one output, and it reads that segment. Column names are not compared: this route names a computed column after its compiled root while the existing planner names it after the expression, which is older than this split and not what this test is about. Signed-off-by: discord9 <discord9@163.com> * feat(promql): share the input-locality classification A tree's distribution requirement is one property, and two layers now ask it. The physical planner already decided with `input_locality` what a tree needs from its inputs; the logical planner has to ask the same question when it splits a statement, or the two can disagree about what a subtree requires. `InputLocality` and `input_locality` are therefore public within the workspace. The classification describes the external inputs a tree needs - not its output distribution, and not which neighbouring operators may share a node - so a caller that reads it as either of those will cut where it should not. Signed-off-by: discord9 <discord9@163.com> * feat(query): segment a statement where a gather would swallow its prefixes The previous commit planned `sum(topk(k, prefix))` by matching that shape. A planner that grows one matcher per query shape grows one per benchmark entry, and the shape is not what decides where a statement divides: the tree already says what each subtree needs from its inputs, and `input_locality` answers it. So a segment is now a maximal subtree with one distribution requirement. Inside a tree that must gather, any child that (a) can cross a boundary - it produces evaluated samples rather than raw samples, aggregate state or a scalar - and (b) does not itself need a gather, becomes a segment of its own and keeps the partitions it was scanned with. Nothing else is cut, and a tree with nothing to cut keeps the plan it had. Every segment rebinds its own inputs: the scans it reads directly first, in the order it first names them, then the segments cut out of it, because `FusedSeries` requires dense source indices over its children. `fused_output_tags` derives the parent's columns from those inputs rather than from the statement's first selector. Three gates keep this from answering what the routes before it would not. Each one is a failure this change produced before it was added: - the existing planner must still be able to plan the statement. Deriving tags locally had cut the route off from that refusal, so `sum(clamp(m, 2, 1))` - an empty range, which the existing planner rejects - was answered instead of failing; - every segment's tree must construct a `NativeExecutionState`, because nothing below can fall back once physical planning starts, and a grouping by a value field is a shape the runtime refuses; - the tree must contain a prefix that a boundary could carry, so a whole-vector tree with no per-series work inside it is not split for nothing. The scope is one selector. A statement over two tables binds each scan as a `RawSeriesSource`, and a segment whose child is that wrapper cannot be placed in a region, so its prefix would not gain the partitions the cut is for; those statements keep the plan they had, which the acceptance test pins. Signed-off-by: discord9 <discord9@163.com> * test(promql): pin the segment boundaries and their scope One statement per shape the cut can meet, asserting what the rule says rather than what one query happens to produce: a per-series tree stays one node, a matching tree routes instead of gathering, a global fold over per-series work cuts that work out, a constant stays embedded because a boundary cannot carry one, and a statement over two tables keeps the plan it had because a segment over `RawSeriesSource` cannot be placed in a region. Answers are compared as sets of rows with the value column renamed. Three things differ between the routes and none of them is the answer: this one names a value column after its compiled root while the existing planner names it after the expression, the two emit columns in a different order, and this one is partitioned so its rows arrive by partition. Comparing them as what they are leaves the values as the only thing an assertion can fail on. Signed-off-by: discord9 <discord9@163.com> * fix(query): keep a selected segment whole A segment is chosen because its subtree needs one distribution; cutting inside it again contradicts the choice. The inner builder was still applying the cut predicate, and that predicate is "exportable and not `SinglePartition`" - which every node inside a per-series subtree satisfies, since a per-series tree has no `SinglePartition` node in it to begin with. `sum(topk(1, abs(m)))` showed it: the prefix was cut out of the `topk` as one segment, then `abs` was cut out of that segment, leaving a middle segment whose child is a node rather than a scan. That middle segment cannot be placed in a region, so it answered one partition while the scan had several - the prefix lost exactly the parallelism the cut is for. The inner builder now collects the segment's `Input` leaves and nothing else. Signed-off-by: discord9 <discord9@163.com> * refactor(query): bind a segment to its own scan A cut segment carries one source's identity: its scan, its metric name, its time index, and its labels. Building it from the statement's first selector only happens to be the same thing while a statement has one selector. `build_inner_segment` takes the two lists the planner already has - each occurrence's filtered scan, and each occurrence's metric, time index and tags - and binds the segment's single source from them. The parent keeps the wrapped children and the statement-level binding it has now, because a source that reaches the parent directly still has to cross a region as a raw frame. A segment that collects more than one source is declined. `MatchingLocal` satisfies the cut predicate while reading two sources, and one segment cannot carry both sources' identity. No behaviour changes: the entry is still one selector, so no scan is wrapped and the statement-level values are already that selector's. Signed-off-by: discord9 <discord9@163.com> * feat(query): segment a statement that reads more than one table A whole-vector statement over two sources is now cut into one per-series prefix segment per source, with the fold above them in the frontend. Each segment binds its own source's scan, metric, time index and labels, so it can be placed in the region that holds that source, and each prefix keeps the scan partitions it was read with. Three things had to change for the existing segmenter to reach that shape: The cut sat inside `!per_partition && !multi_input`, so a statement over two metric tables - which is `multi_input` - never reached it. The cut now sits under `!per_partition` alone. The aggregate split keeps its own conditions, including `!multi_input`, so a multi-input tree is still not split that way. The single-selector gate was doing two jobs. A statement naming several selectors declines unless those selectors read more than one table: the count alone says nothing about whether the tree has more than one input to place, and the shapes it would otherwise admit - one metric named twice under a fold, such as `sum(m) / m` or `m + sum(m)` - are a plan the existing path already answers. `cross_table` rather than `multi_input` decides this, because two Mito tables carry no metric logical id and are still two sources. A segment that collects more than one source is declined. A matching subtree over two sources is a cut candidate, and one segment cannot carry both sources' identity, so `sum(topk(1, m / n))` keeps the plan it had. The segmented parent reports the value column the compiled tree names, not the one the existing planner names; `sum(rate(a[5m])) / sum(b)` reports `v` where the existing plan reports an expression-shaped name. The values are identical and both names are pinned in the test, so a change to either rule shows up there. Restoring the existing name needs a rename above the parent - the node's own schema must keep the compiled names its output columns are classified by - and is left to a follow-up. Signed-off-by: discord9 <discord9@163.com> * test(query): pin that two sources keep their own scan bounds A segmented statement binds each prefix to its own source's scan, and A1 made that binding: `bare_scans` is derived from the prepared scans after each selector's time filter has been pushed into its own `TableScan`. Nothing tested that the two segments actually read `their` bounds rather than one window applied twice - the existing two-source case has no offset, so both sides read the same range and a crossed binding would answer the same numbers. `sum(off_a) / sum(off_b offset 60s)` differs from `sum(off_a) / sum(off_b)` only in the right side's window, so the two queries must disagree: 20/1 and 30/2 against 20/2 and 30/3. The test asserts they do, which is what fails if both segments were given the left scan's bounds. The shape is read from `MergeScanExec`'s `Debug`, because its `DisplayAs` prints only the peers: the debug form carries the table each segment reads and the embedded `FusedSeries` segment. An in-process region hands the plan observer no separately wrapped region tree, so the per-partition evidence for segments is measured in `native_aggregate_test.rs::check_two_source_segregation` instead. Signed-off-by: discord9 <discord9@163.com> * fix(query): decline a multi-source tree that groups by the metric name The gate that lets a multi-source statement reach the segmenter is `selectors.len() != 1 && !cross_table`, but the metric-name grouping guard above it is keyed on `multi_input`, which is `selectors.len() > 1 && logical_ids_differ`. Two ordinary Mito tables differ in table name and carry no metric logical id, so they are `cross_table && !multi_input`: the guard did not apply and the statement reached the segmenter. `fused_output_tags` keeps a grouping by `__name__` out of the declared columns on purpose - the name travels in the chunk's labels, not as a tag column - so `sum by (__name__)` over two sources derives no tag column while the runtime holds two groups that differ only by metric name. Both are then reported under one label set, as two rows a reader cannot tell apart. That is the failure the metric-logical-table test already documents; it could not catch this path because its tables make `multi_input` true. The check is scoped to a tree that reads more than one source. One source folds one metric, so its groups carry the same name and naming that name or dropping it cannot merge two of them. Signed-off-by: discord9 <discord9@163.com> * test(query): cut a range function on each side of a division The two-source cases so far cut an `Instant` prefix: one sample per grid point. A window function's prefix computes its window inside the region over that source's own samples instead, so the segment carries a `Window` rather than an `Instant` and nothing tested that such a prefix is still a cut candidate. `sum(increase(a[2m])) / sum(increase(b[2m]))` over two tables: at the first step the increases are 60 and 30, at the second 120 and 90, so the ratio is 2 and then 4/3. Two different step values are what let a shared window bound change an answer, which is asserted before the result is compared. The shape is read from `MergeScanExec`'s `Debug` for the same reason as the offset case: an in-process region hands the plan observer no separately wrapped region tree, so the per-partition evidence lives in `native_aggregate_test.rs`. This is the shape of the throttling query in the benchmark corpus: a window under a fold, on each side of a division. Signed-off-by: discord9 <discord9@163.com> * test(query): plan the benchmark's throttling and namespace-ratio shapes Every two-source case so far divides two ungrouped scalars. The corpus queries that motivate this work group by a label on each side, so each segment reports several groups and the parent matches the two sides by that label. Nothing tested that path. `container_cpu_cfs_throttled_periods_total` over `container_cpu_cfs_periods_total` is the corpus query verbatim: two metric logical tables on one physical table, `sum(increase(...)) by (container)` on each side. It plans as one frontend `FusedSeries` over two `MergeScan` children, each holding a segment bound to its own metric, and reports the same labelled rows as the existing plan. The namespace-CPU-usage-over-requests shape differs from the corpus text only in the metric names, because the corpus names carry recording-rule colons that a table name cannot: it exercises the nested `sum(max by (...) (...)) by (...)` form and is asserted as that shape rather than as the query verbatim. Both tests assert the native route answered, so a decline fails here instead of passing on the legacy rows. The value column still differs from the existing plan's - the same known gap recorded for the ungrouped case - and both names are pinned. The fixture helper records the three setup traps the hard way: `INSERT` needs explicit column lists because the physical table's column order differs from the logical table's, SQL keywords used as tags need quoting, and no sample may sit at timestamp zero, which both the scan's lower bound and PromQL staleness drop. Signed-off-by: discord9 <discord9@163.com> * test(query): record which native route every corpus query takes The corpus is 29 queries and nothing said which of them the native route answers, or by which of its plans. One parameterised test now runs all of them over generated fixtures and records, per query, the route and the numbers. Four routes, and the frontend tree alone cannot tell them apart: - `segmented` - the frontend runs a `FusedSeries` over `MergeScan` children, one per source; the prefix work happens in the regions. - `aggregate-split` - the frontend runs `AggregateFinalizeExec` over a region `FusedSeries` that reports `partial state:`, so what crosses the boundary is aggregate state. - `region-local` - the frontend holds no native node at all: a per-series tree is commutative, so the whole `FusedSeries` is pushed into the region. A classifier that only looks at the frontend calls these legacy, which is what happened before this test existed. - `legacy` - no native node on either side. All 29 take a native route: sixteen `aggregate-split`, nine `segmented`, two `region-local`, none legacy. The two `region-local` ones are the corpus queries that fold nothing - a bare selector and a `rate` - which is also why pushing the whole tree into the region costs nothing. The test asserts only that the existing plan answers each query with rows, and that a native route's labelled rows agree to within `1e-10` relative. It records the route rather than asserting it, because which route a shape takes is a property of the planner that this test exists to report, not to legislate. Ten queries differ from the existing plan in the last bits; the largest relative difference is 2.9e-16, which is summation order, not arithmetic. The table prints that figure per query so a future equality failure can be told apart from rounding without re-measuring. Every fixture here is generated: explicit `INSERT` column lists, quoted SQL keywords, and no sample at timestamp zero. They establish the plans and the values; they say nothing about speed, memory, or how many partitions actually produced rows. Signed-off-by: discord9 <discord9@163.com> * test(query): record how many partitions each corpus query's nodes used The route matrix said which plan answers each query. It did not say whether that plan produced anything, so a node that ran and emitted nothing looked the same as one that worked. `output_values_per_partition` reads `native.root.output_values` off a `FusedSeriesExec` and returns the counts per partition, and the matrix prints them as `partitions used`. `assert_four_native_partitions` keeps its own assertions and now builds on the same helper. What the column does not establish, and says so where it is printed: these fixtures hold a handful of series each, so a scan puts them all in one partition - every vector here is `[n,0,0,0]`. That is a property of the fixture, not of the plan. The one place a fixture is built to fill the scan's partitions is `native_aggregate_test.rs::check_two_source_segregation`, which asserts all four produce values for both segments of a two-source statement. This matrix records a single assertion instead: at least one partition produced values, so a node that ran and emitted nothing fails. Four cases report `no metric` - L05, L06, L08 and R03, the same-table pointwise divisions. Their region side has no `FusedSeriesExec` at all, because both scans are matched inside one region: that is the route, not a missing measurement, and the table says `no metric` rather than implying zero. Signed-off-by: discord9 <discord9@163.com> * test(query): record the new rule in the explain stages `TQL EXPLAIN` lists the plan after every physical optimizer rule, so registering `SeriesFrameOutput` adds one line to that listing whether or not the rule changes the plan. The three `TQL EXPLAIN VERBOSE` statements in this case use ordinary tables, whose schemas carry no series frame declaration, so the rule returns the plan untouched - the new line says `SAME TEXT AS ABOVE`, which is what the existing lines say for the same reason. Regenerated by the sqlness runner; the diff is those three lines and nothing else. Signed-off-by: discord9 <discord9@163.com> * test(query): make the corpus matrix prove what it claims Three gaps, all in the test the "every corpus query answers natively" claim rests on. The partition helper returned early on a plan that reported no `native.root.output_values`, so a producer that registered nothing passed on the declared partition count alone. It now expects the metric: the point of the assertion is that every partition executed, and an absent counter says nothing about that. `segmented` was decided by the frontend tree containing a `MergeScan`, which also matches a frontend native node consuming raw frames from its regions - a different placement that says nothing about a prefix running in a region. The merge scan's `Debug` carries the plan its region runs, so the check now reads that for a segment; the raw case gets its own `frontend-raw` route. Four queries move there: L05, L06, L08 and R03. The matrix held 23 of the 29 queries: Q05, Q08, Q09 and Q10 had only ever been probed in a throwaway run. They are entries now, with the join labels Q08 and Q09 match on and real `le` buckets for Q10's histogram. A fallback is no longer a route to record: a case that reaches the existing plan fails, because that is the regression this table exists to catch. Twenty-seven cases here plus the separate Q04 and Q07 tests cover all 29. Signed-off-by: discord9 <discord9@163.com> * refactor(query): let the compiler decide which calls this route answers A second list of function names stood beside the compiler's own capability check, and the two disagreed. `admitted_selector` named five calls; the compiler folds `ceil`, `floor`, `sqrt`, `irate`, `avg_over_time`, `sort`, `absent` and more. So `ceil(m)` was refused while `sum(ceil(m))` was admitted, and the same call meant two different things depending on what wrapped it. The compiler already answers this question: `compile_call` refuses a name it has no kernel for with `NotImplemented`, and the planner turns that into a decline. With the list gone there is one place that decides, and a call the compiler folds reaches the route whether or not it is wrapped in an aggregate. `sort` and `absent` read across a vector rather than one series at a time, which is why the old list named them out. They compile, so they are planned; the tests that asserted the refusal now assert the admission. Only the shapes the compiler genuinely cannot express keep the existing plan - a subquery evaluates its body on a grid of its own, which this route does not offer. Signed-off-by: discord9 <discord9@163.com> * refactor(query): admit an operand by its shape, not by call name An operand of a hybrid binary was admitted only if it was a selector under one of five named calls. The compiler folds many more than those five, so `ceil(m)`, `irate(m[5m])`, `sort(m)` and `absent(m)` were refused while the same call under an aggregate was admitted - the name list disagreed with the compiler about what this route can answer. What the list was really protecting is a layout assumption, and the shape states it directly: `try_plan_native_operand` hands back a selector's planner context, and the hybrid caller reads field columns and tags off that context, so an operand whose output layout is not its selector's cannot be one. `sum(A) / B` is refused for that reason, not because `sum` is a name. `single_selector_spine` states it on the expression: a selector, parentheses, unary, a call carrying exactly one vector-or-matrix argument, or a binary whose other side is a compile-time constant. Calls are followed by the type of their arguments rather than by `constant_value`, because a call's string parameters are constants that `constant_value` does not fold and would otherwise look like data. Which calls this route answers is now the compiler's answer alone: a name it has no kernel for comes back as `NotImplemented`, which the planner turns into a decline. Signed-off-by: discord9 <discord9@163.com> * feat(promql): mix raw and evaluated inputs in one fused tree `build` refused a tree whose children were not all raw or all evaluated, but nothing below that point assumed uniformity: `execute` picks the decoder from each source's own schema, `FusedFeed` carries the source index, and `collect_source_schemas` already validates a repeated source against its own grid. A frame stream has one format per stream, not one per consumer, so one segment can decode the raw samples of a rate and compare the result against a vector a previous stage already evaluated. Remove the refusal and make the remaining per-input decisions per source: `evaluated_global` now asks about the source it orders, and `window_sources` keeps only the evaluated sources, since a raw source is answered by the raw path it always used. Cover it by executing `rate(x[5m]) + y` where `x` arrives raw and `y` arrives evaluated on the statement grid. The test asserts all ten (host, timestamp) rows and fails on the removed refusal if the refusal is restored. Signed-off-by: discord9 <discord9@163.com> * feat(promql): bind a segment to every source its subtree reads `cut_as_segment` accepts any exportable subtree that is not `SinglePartition`, so a matching subtree over two sources - `m / n` - is a legal cut point. `build_inner_segment` then required exactly one source and returned `None` for it, and `segment_tree` propagates that `None` as "the statement keeps the plan it had", so `sum(topk(1, m / n))` fell back to the legacy path even though its cut was legal. Build the segment from every source the subtree reads. Each child gets the `RawSeriesSource` boundary already used for separate metric tables, which carries that source's own metric, tags and fields across the boundary and makes the child placeable; a segment over two tables cannot sit inside either region, so it runs above them. The single-source case keeps its existing direct-scan path unchanged, which is what lets a one-table segment still be pushed into its region. Field names come from `tree_source_fields` on the rewritten tree, whose source indexes are already dense in the order the children are built. That helper also validates a repeated source against its own schema and refuses a non-dense index set, which a second traversal would have to re-derive. It becomes `pub` and is re-exported for the query crate to call. Take the segment's time column from its own first source rather than from the statement's first: a cut whose sources are `[1, 2]` would otherwise name the time column of a table it does not read. Cover the construction with a planner test and with a real-Mito integration test that asserts both the exact values and the route: one segment over the two tables, each child reading its own table through its own raw series source. Without the route assertion a silent fallback would pass on equal values. Signed-off-by: discord9 <discord9@163.com> * feat(promql): run a matching tree at the configured width A matching tree's width came from its inputs: `matching_partitions` took the selected source's count, or the maximum of every input's. In standalone a cross-table statement arrives as one `MergeScanExec` per table with one partition each, so `m / n` matched on a single worker while the scans under it ran in parallel. The engine's `execution.target_partitions` was reachable - `plan_fused_series` already uses it for `series_scan` - but never reached this decision. Add `to_execution_plan_with_parallelism(inputs, target_partitions)`, which resolves a matching tree's width as `target_partitions.max(1)` for both the same-source and the multi-input case. `to_execution_plan` keeps its signature and its input-derived width, so a caller that has no configured parallelism is unchanged. `PartitionLocal` and `SinglePartition` trees do not consult the value at all: a per-series tree runs on whatever partitions it was scanned with, and asking for a router there would only add a shuffle. Forward the value through `FusedSeriesExec` because `with_new_children` rebuilds from the node's own children, which are the ones below the router; rebuilding without it would resolve the width again from the pre-router count and silently fall back to one. Verified on a real standalone: the two-table segment now reports four partitions with a `SeriesRepartitionExec` under each one-partition region scan, where it reported one before, and the rows are identical. Signed-off-by: discord9 <discord9@163.com> * test(promql): declare a keyed test frame's ordering, and order its keys `plan_keyed` built its schema with `datatypes::series::frame::series_frame_schema`, which hardcodes the frame metadata `ORDERING` to `contiguous`. The stream it returns packs what `RawScript::ordering` reports, which is now the key-then-time order this helper models. The node reads the ordering off the *plan* schema, so the two disagreeing meant the router scheduled these fixtures as `Contiguous` while unpacking validated them under the stricter ordered contract. Build the schema through `frame_schema_with` with the ordering the stream actually produces, so the plan and the runtime frames state the same contract. That mismatch was invisible until `RawScript` stopped hardcoding `Contiguous`: with both sides claiming `contiguous`, the ordered validation never ran. Running it showed the matching fixture feeding key `b` before `a` within one partition, which its declared ordering forbids. Reorder the partition; the two pieces of `host="a"` still arrive in different partitions, which is what the test is about. Signed-off-by: discord9 <discord9@163.com> * fix(mito2): declare the key order a two-phase raw scan actually keeps `SeriesScan::raw_ordering` reported `SeriesOrdering::Contiguous` for a `TwoPhase` scan that is not reading columnar artifacts. The data that path produces is ordered by primary key: - candidates are merged and sorted ascending by the encoded primary-key column, then decoded to ids by walking that ordered stream without reordering; - partition assignment is a deterministic TSID-to-partition map that appends in arrival order, so one series never spans two partitions; - each assignment is drained completely before the next is taken, rather than emitted in completion order; - the raw adapter's predicate buffering preserves input order and cannot let a later key overtake a held one, and two-phase records always report `ends_package: false`, so it never engages the package-tail machinery that legacy relies on. Declaring the weaker order costs two things that are available: the frame codec's `SequenceValidator` never ran its increasing-key check, and a repartition owner could not use a key watermark to release an identity before another input ended. It also contradicts the scanner's own documented contract, which states the output is always ordered by (primary key, time index) within a partition. Report the order the path actually keeps, and reword the two ordering errors that said "legacy" now that both modes can reach them. Cover it with a test that forces two-phase mode and the non-prepared fallback, asserts which of the two it ran, and then checks per-partition key monotonicity, per-series timestamp monotonicity, single-partition ownership, and the ordering the scan reports. The fixture writes three rounds, uses two output partitions and a residual predicate, and keeps one series above a batch so the multi-batch path is exercised. The three existing two-phase raw tests asserted the old declaration. They check that a series stays in one run and never recurs non-adjacently, which the stronger order still guarantees; only their expected declaration and their names said "contiguous", so those change and the substantive assertions stay. Signed-off-by: discord9 <discord9@163.com> * fix(promql): route a raw match only by a key the computation keeps `same_source` answers "this subtree reads one source", and a `MatchingLocal` classification was taking that answer as licence to insert the router below the fused computations, hashing the labels as they are at the raw stage. Those are different facts. A math map drops the metric name and the `__type__`/`__unit__` metadata, `timestamp` does the same, and every range function drops them except `last_over_time` and `first_over_time` — so `on(__name__)` over `abs(...)` routes by a key the very next node deletes. Concretely, `abs(x) + on(__name__) abs(y)` with one series on each side routes `x` and `y` to different owners; both become the empty key after `abs`, nothing re-partitions between the map and the match, and the pair is silently lost. Decide preservation separately from source identity. A node keeps the key when it cannot remove one of the labels the key is built from; a `Window` consults `RangeFunction::drops_metric_name` rather than assuming, so the two functions that keep the name still route early. Anything not proven to keep the key — a label rewrite, a histogram map, an unmodelled node — does not establish preservation, and the tree is classified `SinglePartition`. That is not a fallback that trades parallelism for correctness. When the computation removes the labels the key is built from, every series on one side reduces to the same empty key, so every left series matches every right one: the match is whole-vector by construction and has no partitioned form that preserves its result. One partition is the only placement that answers it. Where the key does survive, the match stays partitioned. Signed-off-by: discord9 <discord9@163.com> * fix(query): describe an admitted operand by what it produces `try_plan_native_operand` handed the binary matcher the context saved during scanning, which describes the operand's *input* layout. An admitted call can change that layout: `histogram_quantile` folds a classic histogram and drops `le`, so the scan context still names a tag column the operand no longer produces, and default matching then builds a join key over a column that is not there. Concretely, `histogram_quantile(0.9, m) / histogram_quantile(0.5, m)` declines whole-tree planning (two selectors over one table) and is then admitted operand by operand; each operand drops `le`, and planning fails building the join with `FieldNotFound { field: "le" }` against a schema of `timestamp, field_0, host`. Publish only the tag and field columns that the operand's own plan schema actually contains, and drop `use_tsid`, which the produced output does not carry. The check is against the schema the node returns rather than against a list of functions that happen to shrink it, so a call added later cannot reintroduce the gap by not being on a list. Covered by a classic-histogram fixture with `host` and `le` and a float bucket field, asserting each operand's context reports `["host"]` and that the planned join key omits `le`. Reverting the change fails that test with the `FieldNotFound` above. Signed-off-by: discord9 <discord9@163.com> * test(promql): pin the partitioned duplicate-group answer, with the gap named `native_hybrid_binary_05_06_08` asserted that the right side's two `job="other"` series are refused as a duplicate match group. On the partitioned route they are not: the check is gated on `lhs.presence && rhs.presence` computed from the partition's own slices, so the partition holding `other` sees no left-hand data and filters every point out. Pin what the route actually answers, and state the three things that constrain the fix, because each has already been tried and rejected here: - the gate cannot be dropped. `prometheus_cases.txt:1065-1075` is a Prometheus-generated case where the left is absent at one point and the right carries three samples of one group there, and the answer is a value with no error; - the scan cannot run unconditionally, for the same reason; - the match cannot be collapsed to one partition to make both operands whole, because that gives up the parallelism this route exists for. What remains is to let the verdict see whole-operand presence while the partitions keep answering — a deferred check rather than a different placement. This test now makes any change to the current answer visible instead of silent. Signed-off-by: discord9 <discord9@163.com> * feat(query): segment a nested match instead of gathering it A nested one-to-one match — `x + y + z`, or `(x - y) / on(host) z` — passes the logical admission check, because `independent_scan_pair` recurses through `scan_operand`. The physical locality classification does not go that far: it recognizes a match only when both operands read one source each, so `same_source` fails on an operand that is itself a match and the whole tree is classified `SinglePartition`. The fused computation then ran on one partition while the scans under it stayed parallel, which the existing planner does not do: it builds ordinary equijoins that DataFusion can execute as partitioned hash joins. Admission and placement are different questions, so they now get different predicates. The tree is segmented when it is cross-table, not multi-input, classified `SinglePartition`, and its root is a vector match with a nested vector match directly under it. The inner match becomes a segment and the outer one repartitions the evaluated operands by its own key, which is what the segmenter already does for any exportable evaluated prefix; a nested match whose two levels use different keys is routed twice, once per key, rather than pretending one routing serves both. The predicate stays narrow on purpose. Three-selector trees over distinct metric logical ids are `multi_input` and keep their existing construction, including the parent binding that construction produces. A same-table nested match does not enter this branch either, so its prior admission is unchanged. The plan test's collector walks each fused node's native tree in expression-child order and resolves each input's `source_index` against that node's inputs, so a segmented plan is checked for the same property as before — every selector occurrence bound exactly once, in expression order — rather than being checked more weakly. It panics on a shape it does not model instead of returning an empty list. Signed-off-by: discord9 <discord9@163.com> * fix(promql): prove a raw match key survives the computation `preserves_matching_key` decides whether a subtree keeps the labels a vector match routes by. Two arms answered yes without looking at the field that decides it. A binary with a scalar operand recursed into the vector side alone, but arithmetic and a `bool` comparison drop the metric name while a filtering comparison keeps it — the distinction execution already makes. And an `Instant` recursed unconditionally, while `timestamp(x)` compiles to `InstantValue::Timestamp`, which drops the name too; the check written for the separate `Timestamp` node never covered that shape. Both now read the field that decides: the operator together with `return_bool`, and `InstantValue`. The labels checked are the whole computed set — name, type and unit — because `drop_metric_name` removes all three and default matching does not ignore the last two by itself. Without this, `(x * 2) + on(__name__) (y * 2)` was classified matching-local, the router hashed the raw names before the maps dropped them, and a valid pair could land in different partitions with nothing re-partitioning afterwards. The regression asserts the value is 10 and that the tree is `SinglePartition`, computing the destinations the router would have used (`x0` to partition 0, `y0` to partition 1 of two). With the old arm restored the value assertion fails with `left: [10.0], right: []`. Signed-off-by: discord9 <discord9@163.com> * feat(query): find a nested match through the wrappers around it Segmentation was gated on the root itself being a vector match with a nested match beneath it, so `abs(x + y + z)` and `2 * (x + y + z)` never entered: the wrapper or the scalar broadcast sat where the match was expected. Meanwhile the segmenter already walks children and leaves a wrapper in the parent while cutting what it can carry, so no per-wrapper case is needed — the search just has to look through them. Replacing the root check with `carries_prefix` was too wide: almost any multi-source tree contains something a boundary could carry, so ordinary shapes like `some_metric unless on(tag_0) other_metric` were cut for no gain and their inputs stopped being raw sources. So keep requiring a nested vector match, and search for it anywhere in the tree rather than at the root. Wrappers and scalar broadcasts are transparent because the walk visits every child; the nested match itself is still required, which is what keeps the plain multi-source shapes on the plan they had. `cross_table`, `!multi_input` and the `SinglePartition` locality requirement stay, as before: they are what keeps distinct metric logical ids and the same-table shapes out of this branch. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): let a router publish an identity whose chain is complete `SeriesRepartitionExec` published an identity only once every input had contributed it or ended, because an identity may be split across inputs. A per-series region scan assigns each identity to one partition, so on that shape the rule holds every identity until the other partitions reach EOF: a region's whole raw selection is retained before the first series leaves. The node now carries an `IdentityCompletion`, defaulting to the existing merge. `CompleteDisjoint` publishes an identity as soon as its chain closes and keeps the published identities charged in the pool, so a second chain for the same identity fails the execution instead of emitting a partial series as final. The declaration is the producer's, not something a consumer infers from a node's type: publishing early against a producer that does not own its identities would be wrong, so the conservative mode stays the default and every existing constructor keeps it. Signed-off-by: discord9 <discord9@163.com> * fix(query): choose a series-frame root's router from declared provenance `SeriesFrameOutput` normalized every multi-partition frame root through a router that merges identities, which is wrong for a root whose identities are already disjoint: it waits for every scan partition to end before the first series leaves. The rule now selects `CompleteDisjoint` only for a root whose producer declared that property. A raw root qualifies when `RawSeriesSourceExec` was built through `to_per_series_execution_plan` - which also checks the child is a `SeriesScanExec`, so the type alone is not taken as proof - and a direct `SeriesRepartitionExec` root qualifies because it merged the identities itself. Everything else, including a fused frame root, keeps the merging router: a native label transformation can map two source series onto one output label set, so its output identities are not exclusive. The rejection of a key-only raw frame is unchanged and its reason is restated: coalescing cannot produce such a frame, and a router needs explicit labels to check that one key has one binding. Signed-off-by: discord9 <discord9@163.com> * fix(promql): report the labels `absent` names, not the table's columns `fused_output_tags` answered `None` for `absent`, so the caller fell back to the existing planner's column names. Those are the table's tag columns, which is not what `absent` reports: `absent(m{host="x"})` carries the labels its matchers name, and the compiled tree already knows them in `BoundVectorOp::Absent { labels }`. The label set is now read from that field: the named labels are kept in the input's column order, so `absent(native_shapes{host="missing"})` over a table with `host` and `le` reports `host` rather than `ts, val, host, le`. A label the matchers name that is not an input column still answers `None`, because a frame has no column to carry it; `count_values` and `histogram` are unchanged. Caught by the native-versus-legacy value comparison: the extra column made `absent` differ from the existing plan while every value matched. Signed-off-by: discord9 <discord9@163.com> * fix(promql): describe a frame root's emission from what it does `FusedSeriesExec` declared `EmissionType::Final` for a frame output, but `poll_output` hands a frame's evaluated chunk over while its input is still arriving, so the declaration overstated the node to the optimizer. Only a partial aggregate's state is emitted after EOF; every other output keeps its input's emission type. Signed-off-by: discord9 <discord9@163.com> * test(promql): compare the shapes the native route now admits with the existing plan Removing the shapes a name list used to refuse put single-selector `sort` and `sort_desc`, `absent`, and a bare `histogram_quantile` on the segmented route. Nothing compared their answers with the existing plan, so the widening was unverified. The new case runs each shape both ways over one metric table. `sort`, `sort_desc` and both `absent` cases compare the whole printed result, which covers column names and values together. `histogram_quantile` compares values row by row and asserts the one known difference instead of hiding it: a segmented statement names its value column from the compiled tree (`val`) where the existing plan names it after the expression that produced it. That case is given a window long enough to produce a row, so it cannot pass by comparing two empty results. The `absent` cases caught a real difference: the native route reported the table's tag columns rather than the labels the matchers name, which is fixed in the preceding commit. Signed-off-by: discord9 <discord9@163.com> * fix(query): publish a fused root's identities early when the tree keeps them The root rule publishes an identity as soon as its chain closes only for a root whose producer declared that each identity lives in one partition, and it knew two such producers: a per-series raw scan, and a router that had already merged its identities. A fused root was always treated as needing a merge, so a region whose segment is a plain instant selection over one per-series scan still held every identity until the other scan partitions reached EOF - `sum(topk(1, m))` is that shape, and its segment is already multi-partition with no label rewrite. `FusedSeriesExec` now answers whether its output identities are disjoint, and the rule asks it. The answer is narrow on purpose: one input, and a tree of nothing but inputs and sample instant selections, over a per-series scan or another fused node that already answered yes. Anything that computes labels is excluded, because a timestamp or a pointwise map can map two source series onto one output identity, and publishing early would then hand over one identity's pieces as if they were different ones. A vector-to-vector binary reads across series and is excluded too. Signed-off-by: discord9 <discord9@163.com> * fix(query): segment a computed matching key's operands instead of gathering them A vector match whose operands compute away one of the matched labels was classified `SinglePartition` and executed on one partition. That is correct for the bare tree - the raw names cannot route a match whose operands drop them - but it also gave up the parallelism the operands still have: `(x * 2) + on(__name__, host) (y * 2)` loses `__name__` and keeps `host`, so the computed sides can be routed by what they actually carry. The planner now cuts those operands into segments of their own and lets the parent route their evaluated outputs by the unchanged match key. The key itself is not rewritten: `group_key` reads the labels present at that point, so `On(__name__, host)` degrades to the `host` a computed operand still has. Which shapes take this route is decided by one predicate shared with `input_locality`, so the admission rule cannot drift from the locality judgement: a matching site whose two operands are each proven single-source computations, at least one of which does not preserve the key. A failed preservation proof on its own is not enough - it can also mean the proof gave up, as with a parameter that is a compound expression or an operand that is itself a vector match - and the shared doc says so. The same table read twice is admitted for this shape only. The rule that declines several selectors on one table exists to keep folds such as `sum(m) / m` off this route, not because one table cannot be read twice; the compiler already gives each occurrence its own source index, and each cut gets its own input. Distinct partitions are what the two operands need, and they get them. The regression executes both the same-table and the cross-table shape over a four-host fixture and asserts that at least two output partitions produce rows, so the result is not silently produced by one of them. Signed-off-by: discord9 <discord9@163.com> * fix(query): take the early-publication guarantee from the scan's binding The root rule publishes an identity as soon as its chain closes only when the producer can promise that each identity lives in one partition, and the fused answer to that question asked whether its input was a `SeriesScanExec`. The node's type is not that promise: a scan binds a set of logical tags and decodes those into the series' labels, and `try_to_named_series_scan` requires the caller to pass complete tag names but does not check it. A binding that names only some primary-key columns maps two stored series onto one output identity, and early publication would hand one identity's pieces over as different ones. The answer now reads the binding: the label source has to be `PrimaryKey`, and every primary-key column has to be among the bound tags. A source that carries its labels explicitly is not this shape and answers no, as before. Covered by a new `bound_tags_cover_primary_key` unit test - partial coverage is false, full coverage is true, an explicit source is false - and by tests for the predicate's two other parts: a tree that computes labels and a tree that is not one instant selection over one input both answer no, and a gated execution shows a completed identity leaving a fused node while the other input is still held. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): drop the checks the types and decoders already guarantee Two re-checks ran inside the repartition node over facts its inputs had already established, and both cost real work on every query. A raw merge scanned each chunk's timestamps for strict increase, but `RawTimestamps` cannot hold an unordered buffer: its field is private, its only constructor validates, and the accessors are read-only or order-preserving, so no safe caller can build the value this check defends against. The check is gone; the identity, label and shape checks around it stay, because those compare different producers' pieces rather than one chunk's own timestamps. An owner rebuilt each chunk's identity to prove a chain does not change it, but the decoder that hands the chain over already refuses that: `SequenceValidator` holds one open key and rejects a key or metric change before the chain closes, `EvaluatedSequenceValidator` does the same for labels, and the input task that assembles chains only reads from those decoders. What stays is the identity the owner needs anyway to pick a destination, now built by the named functions rather than inline. Where a schema was needed, the node built an empty stream of its own, wrapped it in a packer and read the packer's schema back. The frame module already answers that question from the declaration, so the fake stream is deleted along with its half of the roundtrip. Signed-off-by: discord9 <discord9@163.com> * fix(promql): recursively segment native matching plans Signed-off-by: discord9 <discord9@163.com> * fix(promql): derive native schemas without legacy probes Signed-off-by: discord9 <discord9@163.com> * fix(promql): recognize complete metric scan identities Signed-off-by: discord9 <discord9@163.com> (cherry picked from commit 8f752a02dbb9dc7858618c1d5cc0c41e2d4efb23) * fix(promql): release completed disjoint identity history Signed-off-by: discord9 <discord9@163.com> (cherry picked from commit 9975fa6c5c68786e18af230f62041d2aa5e71f6c) * test(promql): assert native aggregate schemas independently Signed-off-by: discord9 <discord9@163.com> * fix(promql): identify internal metric keys by reserved id Signed-off-by: discord9 <discord9@163.com> * fix(promql): keep parallel sources and derive group match labels Signed-off-by: discord9 <discord9@163.com> * test(promql): observe the frontend fused root in scan-parallelism checks Signed-off-by: discord9 <discord9@163.com> * fix(promql): release duplicate evidence immediately on abort Signed-off-by: discord9 <discord9@163.com> * test(promql): preserve operand identity across matching repartition Signed-off-by: discord9 <discord9@163.com> * fix(promql): charge the final duplicate verdict until it is reported Signed-off-by: discord9 <discord9@163.com> * test(promql): observe native scans across frontend and region plans Signed-off-by: discord9 <discord9@163.com> * chore(promql): drop dead test helper and clippy lints Signed-off-by: discord9 <discord9@163.com> * fix(promql): select limitk by canonical label order Signed-off-by: discord9 <discord9@163.com> * fix(promql): keep data-free subtrees in the parent segment Signed-off-by: discord9 <discord9@163.com> * test(promql): prove the duplicate error crosses matching partitions Signed-off-by: discord9 <discord9@163.com> * test(promql): drop debug prints from the segmentation test Signed-off-by: discord9 <discord9@163.com> * revert(promql): keep limitk's Prometheus input-order semantics Signed-off-by: discord9 <discord9@163.com> * chore(promql): satisfy clippy in fused root schema Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * feat(promql): admit label rewrites and decide canonical collection once (#10097) * fix(promql): let a closed chain keep its charge until the packer is done Four places released the frame reservation: the early-publish path handed it to the packer, but the queue drain and the end-of-input path freed it in place. Those two run while the packer still holds rows it has not turned into batches, so a series spanning several batches went unbilled once its last piece was handed over. Every emit path now hands the charge over, and the packed stream frees what it holds: what is left when it is polled and pending is the one identity it is turning into batches, and nothing survives its end or its drop. The identity guard is also widened: within one source every series shares its metric name, so a node that drops `__name__`/`__type__`/`__unit__` -- which is what a range function, a math map and the timestamp shape do -- still maps distinct series to distinct labels. Only rewriting an ordinary label can make two equal, and that is what the guard refuses. Ordinary `rate` and arithmetic frame output therefore publish as their chains close instead of buffering a partition. Signed-off-by: discord9 <discord9@163.com> * fix(promql): keep a cross-source tree's partitions, and gather a raw one only when it must Three defects in how this node decided a tree's execution layout. A match whose two operands derive from the same source still needs its match keys co-located, but only the two-source case was recognized. `x - 2*x` with `on(host)` therefore fell through to single-partition execution and lost the parallelism the scan gave it. A raw input never received a matching router: the normalization branch returned it unchanged unless the whole tree was global, while the builder requires a direct matching router for any multi-partition matching-local input. A two-source raw query was rejected at planning. Nothing reconciled the partition counts of different sources either, though the builder requires one common positive count. A single count is now computed for the matching-local case and every router under it uses it, gathering when that count is one. The window restriction also ran before normalization decided the layout, so a tree about to be gathered could be refused for a limit that only applies to partitioned windows. Two tests changed with the behaviour rather than around it: hash routing does not promise which partition holds a given series, so that test compares results instead of placement while keeping its partition count, per-partition execution and operand-order assertions; and a test that pinned the old refusal of a multi-source frame now pins its acceptance. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): keep a raw source's scan partitions instead of gathering them `RawSeriesSourceExec` declared one partition and read every child partition inside `execute(0)`, so a cross-table node lost the scan's parallelism before its inputs reached it. It now declares the child's own count, executes exactly the partition it is asked for, and re-labels that partition's frames. That leaves `SeriesGather` with no consumer: it existed to interleave several partitions into one stream. The module is gone rather than kept as a hidden N-to-one path a later plan could quietly fall back on. Signed-off-by: discord9 <discord9@163.com> * fix(promql): publish a raw source's frames under the ordering it declared `pack_raw_series` derives the frame schema from the stream's own ordering, and `RawSeriesSourceExec` fixes that schema at plan time from a constant. Passing the scan's stream through unchanged broke that agreement: a columnar scan promises key-then-time, so the region published key-then-time frames while the frontend still expected the schema it had built, and `MergeScan` refused them. The forwarding adapter restates the ordering this node declares. It is the weaker promise, so a scan that keeps a finer one still satisfies it, and every other stream property is forwarded unchanged. Signed-off-by: discord9 <discord9@163.com> * fix(promql): route a raw frame that names its labels by its key A region cannot hand out complete labels: it has no metric-name binding, so `prepare_raw_scan` refuses an explicit binding outright. What it hands out is the stored key plus the description a decoder needs, which is the normal input to this node rather than a case to refuse — every single-table selector is planned that way. The node's constructor refused exactly those frames, so any plan that inserted a router under a single-table selector failed before it ran: the matching policies this branch adds, and the one-partition normalization of an evaluated tree. The node publishes labels, because that is what it groups and closes identities by: `finish_raw_group` reads `chunk.metric` and the merge needs it too. So the input is decoded once, at the unpack boundary this node already has, and an already-labelled stream passes through it untouched. The frame it declares is rebuilt from the input's own declaration instead of being compared against it, which keeps the check that a frame this node cannot re-declare is refused while letting input and output differ in how they name the same series. `frame_schema_with` becomes public for that rebuild, since it is the one place that decides what a declaration is. `FusedSeries` now describes the router's output rather than the scan's when it wraps a raw input, because the binding it hands `build` has to match the frame the router actually publishes. Tested through the node directly: a key-labelled input routes at all; matching puts both pieces of one label set in one owner and closes it once; and `RawPrimaryKey` still hashes the key rather than the decoded labels. Signed-off-by: discord9 <discord9@163.com> * test(promql): measure a routed pair by the width it routes to Four assertions still described the layout this branch replaced: a node with two inputs used to read every partition of both into one and answer one partition, and that gather is gone. The pair is now matched in the partitions it was scanned in, so the fused node answers as many partitions as its inputs route to. Each of these fixtures scans with four partitions, and on the base of this branch all five tests passed, so this is a behaviour change this branch made and did not re-verify rather than a defect: the value assertions behind these shape assertions pass unchanged, in both the native and the legacy comparison. The width is read from the routed children instead of being written as four, and the scan is found by walking down through the nodes that only pass its frames along, since a routed input is no longer a bare `SeriesScanExec`. Signed-off-by: discord9 <discord9@163.com> * fix(query): normalize a multi-partition series-frame root A query engine hands its plan to one stream, and for a plan with several partitions that means `CoalescePartitionsExec` - ordinary batch coalescing. A series frame cannot survive it: one stored series may be split across partitions, and a chunk's `is_last` closes that producer rather than the series, so interleaving two partitions' batches produces a stream whose decoder refuses it (`raw series key changed before final chunk`). Evaluated frames have the same shape of problem: their validator keeps one open chain, and a batch from another partition interrupts it. The engine's root is also a region's root, which is how a cross-table native statement reaches this: its per-region sub-plan is rooted at a partitioned `RawSeriesSourceExec`. So the rule belongs at the physical optimizer's end, before the final sanity check, and it normalizes the root and nothing else - `FusedSeries` already inserts its own owner wherever a consumer needs one. A single-partition root is left alone: one producer is the whole answer. A key-labelled raw root is refused rather than coalesced, because the router checks that one stored key decodes to one label set and it needs the labels to do that. No production plan reaches that rejection today - the only producer of a key-labelled frame is the region's series scan, and every plan that executes it puts `RawSeriesSourceExec` or `FusedSeries` above it, both of which publish explicit labels - and the rejection is what keeps it that way. `schema_check` stays enabled: for the roots this rule accepts, the router re-declares its input's frame with the same content, label mode, decode description and ordering, so the schema it publishes is the one it was given. Signed-off-by: discord9 <discord9@163.com> * feat(promql): admit an evaluated frame as a fused input The extension planner hands a child straight to the fused executor when that child already publishes a series frame, and until now it only recognised a raw one. A segment's output is an evaluated frame, so a statement planned as two segments could not be bound above the first one. The check it replaces asked for a raw frame; the property that matters is that the frame names its own series, which is what `labels() == Explicit` says - evaluated frames always declare it and carry no decode description. Everything else stays with `FusedSeriesExec::build`, which already refuses a mixed-content node, an evaluated input that is not contiguous, and a frame whose declaration does not match the binding it was given. Signed-off-by: discord9 <discord9@163.com> * feat(query): plan a global topk under an aggregate as two segments `sum(topk(1, m))` needs the whole vector for its ranking, which is a property of the tree rather than of the statement, and the routes that existed could only answer a whole-vector tree in one partition: the statement fell back to the existing planner. Splitting it lets the per-series prefix keep the scan's partitions while the ranking sees every candidate. The cut goes below the `topk`, not below the `sum`. A segment holding a `topk` reads across series, so it would gather its own input before evaluating the prefix - correct numbers, no parallelism, which is the thing the split is for. Below the `topk`, the prefix runs on the scan's partitions and the ranking and the fold above it run once, on the child's evaluated frame. Both segments are built from one compiled tree: the child is the prefix subtree, the parent is the original tree with that subtree replaced by an input bound to the child's frame. Nothing is re-parsed, so the grid, the matching rules and the bound scans are the ones the compiler produced. The parent needs its output columns without asking the existing planner to name a computed column after its expression, so `fused_output_tags` now derives the grouping labels of an `Aggregate` and passes a selecting `Vector`'s input labels through. It derives output *columns*, not a complete runtime label set: the metric name is bound separately from the tag columns, and a grouping by `__name__` keeps that name in the labels while contributing no column. `fused_root_schema` is that derivation, shared with the ordinary route. The segmenter is its own method, boxed at the call site. Inlined, its future made every frame of the planner's recursion large enough to overflow the stack on a deeply nested statement - `test_parse_or_operator` aborted before this. Signed-off-by: discord9 <discord9@163.com> * test(promql): pin the two-segment topk plan and what it keeps The statement's two segments do not land in one physical tree: the prefix has one scan for a child, so the categorizer classifies it commutative and pushes it into the region, while the aggregate above it stays at the frontend. The observer therefore records every plan a query engine wraps, not only the first, so the test can look at the region's own root. What the assertions pin, and why each one is worth its brittleness: - the answer is the global maximum, not a sum of per-partition winners; - the frontend segment holds the complete sum and the global topk over an evaluated input, with no instant evaluation left in it; - the region's segment holds the prefix and its output is an evaluated, explicitly labelled, contiguous frame; - that segment keeps more than one output partition, which is the whole point of cutting below the topk; - the region's root is the series-aware router, one output, and it reads that segment. Column names are not compared: this route names a computed column after its compiled root while the existing planner names it after the expression, which is older than this split and not what this test is about. Signed-off-by: discord9 <discord9@163.com> * feat(promql): share the input-locality classification A tree's distribution requirement is one property, and two layers now ask it. The physical planner already decided with `input_locality` what a tree needs from its inputs; the logical planner has to ask the same question when it splits a statement, or the two can disagree about what a subtree requires. `InputLocality` and `input_locality` are therefore public within the workspace. The classification describes the external inputs a tree needs - not its output distribution, and not which neighbouring operators may share a node - so a caller that reads it as either of those will cut where it should not. Signed-off-by: discord9 <discord9@163.com> * feat(query): segment a statement where a gather would swallow its prefixes The previous commit planned `sum(topk(k, prefix))` by matching that shape. A planner that grows one matcher per query shape grows one per benchmark entry, and the shape is not what decides where a statement divides: the tree already says what each subtree needs from its inputs, and `input_locality` answers it. So a segment is now a maximal subtree with one distribution requirement. Inside a tree that must gather, any child that (a) can cross a boundary - it produces evaluated samples rather than raw samples, aggregate state or a scalar - and (b) does not itself need a gather, becomes a segment of its own and keeps the partitions it was scanned with. Nothing else is cut, and a tree with nothing to cut keeps the plan it had. Every segment rebinds its own inputs: the scans it reads directly first, in the order it first names them, then the segments cut out of it, because `FusedSeries` requires dense source indices over its children. `fused_output_tags` derives the parent's columns from those inputs rather than from the statement's first selector. Three gates keep this from answering what the routes before it would not. Each one is a failure this change produced before it was added: - the existing planner must still be able to plan the statement. Deriving tags locally had cut the route off from that refusal, so `sum(clamp(m, 2, 1))` - an empty range, which the existing planner rejects - was answered instead of failing; - every segment's tree must construct a `NativeExecutionState`, because nothing below can fall back once physical planning starts, and a grouping by a value field is a shape the runtime refuses; - the tree must contain a prefix that a boundary could carry, so a whole-vector tree with no per-series work inside it is not split for nothing. The scope is one selector. A statement over two tables binds each scan as a `RawSeriesSource`, and a segment whose child is that wrapper cannot be placed in a region, so its prefix would not gain the partitions the cut is for; those statements keep the plan they had, which the acceptance test pins. Signed-off-by: discord9 <discord9@163.com> * test(promql): pin the segment boundaries and their scope One statement per shape the cut can meet, asserting what the rule says rather than what one query happens to produce: a per-series tree stays one node, a matching tree routes instead of gathering, a global fold over per-series work cuts that work out, a constant stays embedded because a boundary cannot carry one, and a statement over two tables keeps the plan it had because a segment over `RawSeriesSource` cannot be placed in a region. Answers are compared as sets of rows with the value column renamed. Three things differ between the routes and none of them is the answer: this one names a value column after its compiled root while the existing planner names it after the expression, the two emit columns in a different order, and this one is partitioned so its rows arrive by partition. Comparing them as what they are leaves the values as the only thing an assertion can fail on. Signed-off-by: discord9 <discord9@163.com> * fix(query): keep a selected segment whole A segment is chosen because its subtree needs one distribution; cutting inside it again contradicts the choice. The inner builder was still applying the cut predicate, and that predicate is "exportable and not `SinglePartition`" - which every node inside a per-series subtree satisfies, since a per-series tree has no `SinglePartition` node in it to begin with. `sum(topk(1, abs(m)))` showed it: the prefix was cut out of the `topk` as one segment, then `abs` was cut out of that segment, leaving a middle segment whose child is a node rather than a scan. That middle segment cannot be placed in a region, so it answered one partition while the scan had several - the prefix lost exactly the parallelism the cut is for. The inner builder now collects the segment's `Input` leaves and nothing else. Signed-off-by: discord9 <discord9@163.com> * refactor(query): bind a segment to its own scan A cut segment carries one source's identity: its scan, its metric name, its time index, and its labels. Building it from the statement's first selector only happens to be the same thing while a statement has one selector. `build_inner_segment` takes the two lists the planner already has - each occurrence's filtered scan, and each occurrence's metric, time index and tags - and binds the segment's single source from them. The parent keeps the wrapped children and the statement-level binding it has now, because a source that reaches the parent directly still has to cross a region as a raw frame. A segment that collects more than one source is declined. `MatchingLocal` satisfies the cut predicate while reading two sources, and one segment cannot carry both sources' identity. No behaviour changes: the entry is still one selector, so no scan is wrapped and the statement-level values are already that selector's. Signed-off-by: discord9 <discord9@163.com> * feat(query): segment a statement that reads more than one table A whole-vector statement over two sources is now cut into one per-series prefix segment per source, with the fold above them in the frontend. Each segment binds its own source's scan, metric, time index and labels, so it can be placed in the region that holds that source, and each prefix keeps the scan partitions it was read with. Three things had to change for the existing segmenter to reach that shape: The cut sat inside `!per_partition && !multi_input`, so a statement over two metric tables - which is `multi_input` - never reached it. The cut now sits under `!per_partition` alone. The aggregate split keeps its own conditions, including `!multi_input`, so a multi-input tree is still not split that way. The single-selector gate was doing two jobs. A statement naming several selectors declines unless those selectors read more than one table: the count alone says nothing about whether the tree has more than one input to place, and the shapes it would otherwise admit - one metric named twice under a fold, such as `sum(m) / m` or `m + sum(m)` - are a plan the existing path already answers. `cross_table` rather than `multi_input` decides this, because two Mito tables carry no metric logical id and are still two sources. A segment that collects more than one source is declined. A matching subtree over two sources is a cut candidate, and one segment cannot carry both sources' identity, so `sum(topk(1, m / n))` keeps the plan it had. The segmented parent reports the value column the compiled tree names, not the one the existing planner names; `sum(rate(a[5m])) / sum(b)` reports `v` where the existing plan reports an expression-shaped name. The values are identical and both names are pinned in the test, so a change to either rule shows up there. Restoring the existing name needs a rename above the parent - the node's own schema must keep the compiled names its output columns are classified by - and is left to a follow-up. Signed-off-by: discord9 <discord9@163.com> * test(query): pin that two sources keep their own scan bounds A segmented statement binds each prefix to its own source's scan, and A1 made that binding: `bare_scans` is derived from the prepared scans after each selector's time filter has been pushed into its own `TableScan`. Nothing tested that the two segments actually read `their` bounds rather than one window applied twice - the existing two-source case has no offset, so both sides read the same range and a crossed binding would answer the same numbers. `sum(off_a) / sum(off_b offset 60s)` differs from `sum(off_a) / sum(off_b)` only in the right side's window, so the two queries must disagree: 20/1 and 30/2 against 20/2 and 30/3. The test asserts they do, which is what fails if both segments were given the left scan's bounds. The shape is read from `MergeScanExec`'s `Debug`, because its `DisplayAs` prints only the peers: the debug form carries the table each segment reads and the embedded `FusedSeries` segment. An in-process region hands the plan observer no separately wrapped region tree, so the per-partition evidence for segments is measured in `native_aggregate_test.rs::check_two_source_segregation` instead. Signed-off-by: discord9 <discord9@163.com> * fix(query): decline a multi-source tree that groups by the metric name The gate that lets a multi-source statement reach the segmenter is `selectors.len() != 1 && !cross_table`, but the metric-name grouping guard above it is keyed on `multi_input`, which is `selectors.len() > 1 && logical_ids_differ`. Two ordinary Mito tables differ in table name and carry no metric logical id, so they are `cross_table && !multi_input`: the guard did not apply and the statement reached the segmenter. `fused_output_tags` keeps a grouping by `__name__` out of the declared columns on purpose - the name travels in the chunk's labels, not as a tag column - so `sum by (__name__)` over two sources derives no tag column while the runtime holds two groups that differ only by metric name. Both are then reported under one label set, as two rows a reader cannot tell apart. That is the failure the metric-logical-table test already documents; it could not catch this path because its tables make `multi_input` true. The check is scoped to a tree that reads more than one source. One source folds one metric, so its groups carry the same name and naming that name or dropping it cannot merge two of them. Signed-off-by: discord9 <discord9@163.com> * test(query): cut a range function on each side of a division The two-source cases so far cut an `Instant` prefix: one sample per grid point. A window function's prefix computes its window inside the region over that source's own samples instead, so the segment carries a `Window` rather than an `Instant` and nothing tested that such a prefix is still a cut candidate. `sum(increase(a[2m])) / sum(increase(b[2m]))` over two tables: at the first step the increases are 60 and 30, at the second 120 and 90, so the ratio is 2 and then 4/3. Two different step values are what let a shared window bound change an answer, which is asserted before the result is compared. The shape is read from `MergeScanExec`'s `Debug` for the same reason as the offset case: an in-process region hands the plan observer no separately wrapped region tree, so the per-partition evidence lives in `native_aggregate_test.rs`. This is the shape of the throttling query in the benchmark corpus: a window under a fold, on each side of a division. Signed-off-by: discord9 <discord9@163.com> * test(query): plan the benchmark's throttling and namespace-ratio shapes Every two-source case so far divides two ungrouped scalars. The corpus queries that motivate this work group by a label on each side, so each segment reports several groups and the parent matches the two sides by that label. Nothing tested that path. `container_cpu_cfs_throttled_periods_total` over `container_cpu_cfs_periods_total` is the corpus query verbatim: two metric logical tables on one physical table, `sum(increase(...)) by (container)` on each side. It plans as one frontend `FusedSeries` over two `MergeScan` children, each holding a segment bound to its own metric, and reports the same labelled rows as the existing plan. The namespace-CPU-usage-over-requests shape differs from the corpus text only in the metric names, because the corpus names carry recording-rule colons that a table name cannot: it exercises the nested `sum(max by (...) (...)) by (...)` form and is asserted as that shape rather than as the query verbatim. Both tests assert the native route answered, so a decline fails here instead of passing on the legacy rows. The value column still differs from the existing plan's - the same known gap recorded for the ungrouped case - and both names are pinned. The fixture helper records the three setup traps the hard way: `INSERT` needs explicit column lists because the physical table's column order differs from the logical table's, SQL keywords used as tags need quoting, and no sample may sit at timestamp zero, which both the scan's lower bound and PromQL staleness drop. Signed-off-by: discord9 <discord9@163.com> * test(query): record which native route every corpus query takes The corpus is 29 queries and nothing said which of them the native route answers, or by which of its plans. One parameterised test now runs all of them over generated fixtures and records, per query, the route and the numbers. Four routes, and the frontend tree alone cannot tell them apart: - `segmented` - the frontend runs a `FusedSeries` over `MergeScan` children, one per source; the prefix work happens in the regions. - `aggregate-split` - the frontend runs `AggregateFinalizeExec` over a region `FusedSeries` that reports `partial state:`, so what crosses the boundary is aggregate state. - `region-local` - the frontend holds no native node at all: a per-series tree is commutative, so the whole `FusedSeries` is pushed into the region. A classifier that only looks at the frontend calls these legacy, which is what happened before this test existed. - `legacy` - no native node on either side. All 29 take a native route: sixteen `aggregate-split`, nine `segmented`, two `region-local`, none legacy. The two `region-local` ones are the corpus queries that fold nothing - a bare selector and a `rate` - which is also why pushing the whole tree into the region costs nothing. The test asserts only that the existing plan answers each query with rows, and that a native route's labelled rows agree to within `1e-10` relative. It records the route rather than asserting it, because which route a shape takes is a property of the planner that this test exists to report, not to legislate. Ten queries differ from the existing plan in the last bits; the largest relative difference is 2.9e-16, which is summation order, not arithmetic. The table prints that figure per query so a future equality failure can be told apart from rounding without re-measuring. Every fixture here is generated: explicit `INSERT` column lists, quoted SQL keywords, and no sample at timestamp zero. They establish the plans and the values; they say nothing about speed, memory, or how many partitions actually produced rows. Signed-off-by: discord9 <discord9@163.com> * test(query): record how many partitions each corpus query's nodes used The route matrix said which plan answers each query. It did not say whether that plan produced anything, so a node that ran and emitted nothing looked the same as one that worked. `output_values_per_partition` reads `native.root.output_values` off a `FusedSeriesExec` and returns the counts per partition, and the matrix prints them as `partitions used`. `assert_four_native_partitions` keeps its own assertions and now builds on the same helper. What the column does not establish, and says so where it is printed: these fixtures hold a handful of series each, so a scan puts them all in one partition - every vector here is `[n,0,0,0]`. That is a property of the fixture, not of the plan. The one place a fixture is built to fill the scan's partitions is `native_aggregate_test.rs::check_two_source_segregation`, which asserts all four produce values for both segments of a two-source statement. This matrix records a single assertion instead: at least one partition produced values, so a node that ran and emitted nothing fails. Four cases report `no metric` - L05, L06, L08 and R03, the same-table pointwise divisions. Their region side has no `FusedSeriesExec` at all, because both scans are matched inside one region: that is the route, not a missing measurement, and the table says `no metric` rather than implying zero. Signed-off-by: discord9 <discord9@163.com> * test(query): record the new rule in the explain stages `TQL EXPLAIN` lists the plan after every physical optimizer rule, so registering `SeriesFrameOutput` adds one line to that listing whether or not the rule changes the plan. The three `TQL EXPLAIN VERBOSE` statements in this case use ordinary tables, whose schemas carry no series frame declaration, so the rule returns the plan untouched - the new line says `SAME TEXT AS ABOVE`, which is what the existing lines say for the same reason. Regenerated by the sqlness runner; the diff is those three lines and nothing else. Signed-off-by: discord9 <discord9@163.com> * test(query): make the corpus matrix prove what it claims Three gaps, all in the test the "every corpus query answers natively" claim rests on. The partition helper returned early on a plan that reported no `native.root.output_values`, so a producer that registered nothing passed on the declared partition count alone. It now expects the metric: the point of the assertion is that every partition executed, and an absent counter says nothing about that. `segmented` was decided by the frontend tree containing a `MergeScan`, which also matches a frontend native node consuming raw frames from its regions - a different placement that says nothing about a prefix running in a region. The merge scan's `Debug` carries the plan its region runs, so the check now reads that for a segment; the raw case gets its own `frontend-raw` route. Four queries move there: L05, L06, L08 and R03. The matrix held 23 of the 29 queries: Q05, Q08, Q09 and Q10 had only ever been probed in a throwaway run. They are entries now, with the join labels Q08 and Q09 match on and real `le` buckets for Q10's histogram. A fallback is no longer a route to record: a case that reaches the existing plan fails, because that is the regression this table exists to catch. Twenty-seven cases here plus the separate Q04 and Q07 tests cover all 29. Signed-off-by: discord9 <discord9@163.com> * refactor(query): let the compiler decide which calls this route answers A second list of function names stood beside the compiler's own capability check, and the two disagreed. `admitted_selector` named five calls; the compiler folds `ceil`, `floor`, `sqrt`, `irate`, `avg_over_time`, `sort`, `absent` and more. So `ceil(m)` was refused while `sum(ceil(m))` was admitted, and the same call meant two different things depending on what wrapped it. The compiler already answers this question: `compile_call` refuses a name it has no kernel for with `NotImplemented`, and the planner turns that into a decline. With the list gone there is one place that decides, and a call the compiler folds reaches the route whether or not it is wrapped in an aggregate. `sort` and `absent` read across a vector rather than one series at a time, which is why the old list named them out. They compile, so they are planned; the tests that asserted the refusal now assert the admission. Only the shapes the compiler genuinely cannot express keep the existing plan - a subquery evaluates its body on a grid of its own, which this route does not offer. Signed-off-by: discord9 <discord9@163.com> * refactor(query): admit an operand by its shape, not by call name An operand of a hybrid binary was admitted only if it was a selector under one of five named calls. The compiler folds many more than those five, so `ceil(m)`, `irate(m[5m])`, `sort(m)` and `absent(m)` were refused while the same call under an aggregate was admitted - the name list disagreed with the compiler about what this route can answer. What the list was really protecting is a layout assumption, and the shape states it directly: `try_plan_native_operand` hands back a selector's planner context, and the hybrid caller reads field columns and tags off that context, so an operand whose output layout is not its selector's cannot be one. `sum(A) / B` is refused for that reason, not because `sum` is a name. `single_selector_spine` states it on the expression: a selector, parentheses, unary, a call carrying exactly one vector-or-matrix argument, or a binary whose other side is a compile-time constant. Calls are followed by the type of their arguments rather than by `constant_value`, because a call's string parameters are constants that `constant_value` does not fold and would otherwise look like data. Which calls this route answers is now the compiler's answer alone: a name it has no kernel for comes back as `NotImplemented`, which the planner turns into a decline. Signed-off-by: discord9 <discord9@163.com> * feat(promql): mix raw and evaluated inputs in one fused tree `build` refused a tree whose children were not all raw or all evaluated, but nothing below that point assumed uniformity: `execute` picks the decoder from each source's own schema, `FusedFeed` carries the source index, and `collect_source_schemas` already validates a repeated source against its own grid. A frame stream has one format per stream, not one per consumer, so one segment can decode the raw samples of a rate and compare the result against a vector a previous stage already evaluated. Remove the refusal and make the remaining per-input decisions per source: `evaluated_global` now asks about the source it orders, and `window_sources` keeps only the evaluated sources, since a raw source is answered by the raw path it always used. Cover it by executing `rate(x[5m]) + y` where `x` arrives raw and `y` arrives evaluated on the statement grid. The test asserts all ten (host, timestamp) rows and fails on the removed refusal if the refusal is restored. Signed-off-by: discord9 <discord9@163.com> * feat(promql): bind a segment to every source its subtree reads `cut_as_segment` accepts any exportable subtree that is not `SinglePartition`, so a matching subtree over two sources - `m / n` - is a legal cut point. `build_inner_segment` then required exactly one source and returned `None` for it, and `segment_tree` propagates that `None` as "the statement keeps the plan it had", so `sum(topk(1, m / n))` fell back to the legacy path even though its cut was legal. Build the segment from every source the subtree reads. Each child gets the `RawSeriesSource` boundary already used for separate metric tables, which carries that source's own metric, tags and fields across the boundary and makes the child placeable; a segment over two tables cannot sit inside either region, so it runs above them. The single-source case keeps its existing direct-scan path unchanged, which is what lets a one-table segment still be pushed into its region. Field names come from `tree_source_fields` on the rewritten tree, whose source indexes are already dense in the order the children are built. That helper also validates a repeated source against its own schema and refuses a non-dense index set, which a second traversal would have to re-derive. It becomes `pub` and is re-exported for the query crate to call. Take the segment's time column from its own first source rather than from the statement's first: a cut whose sources are `[1, 2]` would otherwise name the time column of a table it does not read. Cover the construction with a planner test and with a real-Mito integration test that asserts both the exact values and the route: one segment over the two tables, each child reading its own table through its own raw series source. Without the route assertion a silent fallback would pass on equal values. Signed-off-by: discord9 <discord9@163.com> * feat(promql): run a matching tree at the configured width A matching tree's width came from its inputs: `matching_partitions` took the selected source's count, or the maximum of every input's. In standalone a cross-table statement arrives as one `MergeScanExec` per table with one partition each, so `m / n` matched on a single worker while the scans under it ran in parallel. The engine's `execution.target_partitions` was reachable - `plan_fused_series` already uses it for `series_scan` - but never reached this decision. Add `to_execution_plan_with_parallelism(inputs, target_partitions)`, which resolves a matching tree's width as `target_partitions.max(1)` for both the same-source and the multi-input case. `to_execution_plan` keeps its signature and its input-derived width, so a caller that has no configured parallelism is unchanged. `PartitionLocal` and `SinglePartition` trees do not consult the value at all: a per-series tree runs on whatever partitions it was scanned with, and asking for a router there would only add a shuffle. Forward the value through `FusedSeriesExec` because `with_new_children` rebuilds from the node's own children, which are the ones below the router; rebuilding without it would resolve the width again from the pre-router count and silently fall back to one. Verified on a real standalone: the two-table segment now reports four partitions with a `SeriesRepartitionExec` under each one-partition region scan, where it reported one before, and the rows are identical. Signed-off-by: discord9 <discord9@163.com> * test(promql): declare a keyed test frame's ordering, and order its keys `plan_keyed` built its schema with `datatypes::series::frame::series_frame_schema`, which hardcodes the frame metadata `ORDERING` to `contiguous`. The stream it returns packs what `RawScript::ordering` reports, which is now the key-then-time order this helper models. The node reads the ordering off the *plan* schema, so the two disagreeing meant the router scheduled these fixtures as `Contiguous` while unpacking validated them under the stricter ordered contract. Build the schema through `frame_schema_with` with the ordering the stream actually produces, so the plan and the runtime frames state the same contract. That mismatch was invisible until `RawScript` stopped hardcoding `Contiguous`: with both sides claiming `contiguous`, the ordered validation never ran. Running it showed the matching fixture feeding key `b` before `a` within one partition, which its declared ordering forbids. Reorder the partition; the two pieces of `host="a"` still arrive in different partitions, which is what the test is about. Signed-off-by: discord9 <discord9@163.com> * fix(mito2): declare the key order a two-phase raw scan actually keeps `SeriesScan::raw_ordering` reported `SeriesOrdering::Contiguous` for a `TwoPhase` scan that is not reading columnar artifacts. The data that path produces is ordered by primary key: - candidates are merged and sorted ascending by the encoded primary-key column, then decoded to ids by walking that ordered stream without reordering; - partition assignment is a deterministic TSID-to-partition map that appends in arrival order, so one series never spans two partitions; - each assignment is drained completely before the next is taken, rather than emitted in completion order; - the raw adapter's predicate buffering preserves input order and cannot let a later key overtake a held one, and two-phase records always report `ends_package: false`, so it never engages the package-tail machinery that legacy relies on. Declaring the weaker order costs two things that are available: the frame codec's `SequenceValidator` never ran its increasing-key check, and a repartition owner could not use a key watermark to release an identity before another input ended. It also contradicts the scanner's own documented contract, which states the output is always ordered by (primary key, time index) within a partition. Report the order the path actually keeps, and reword the two ordering errors that said "legacy" now that both modes can reach them. Cover it with a test that forces two-phase mode and the non-prepared fallback, asserts which of the two it ran, and then checks per-partition key monotonicity, per-series timestamp monotonicity, single-partition ownership, and the ordering the scan reports. The fixture writes three rounds, uses two output partitions and a residual predicate, and keeps one series above a batch so the multi-batch path is exercised. The three existing two-phase raw tests asserted the old declaration. They check that a series stays in one run and never recurs non-adjacently, which the stronger order still guarantees; only their expected declaration and their names said "contiguous", so those change and the substantive assertions stay. Signed-off-by: discord9 <discord9@163.com> * fix(promql): route a raw match only by a key the computation keeps `same_source` answers "this subtree reads one source", and a `MatchingLocal` classification was taking that answer as licence to insert the router below the fused computations, hashing the labels as they are at the raw stage. Those are different facts. A math map drops the metric name and the `__type__`/`__unit__` metadata, `timestamp` does the same, and every range function drops them except `last_over_time` and `first_over_time` — so `on(__name__)` over `abs(...)` routes by a key the very next node deletes. Concretely, `abs(x) + on(__name__) abs(y)` with one series on each side routes `x` and `y` to different owners; both become the empty key after `abs`, nothing re-partitions between the map and the match, and the pair is silently lost. Decide preservation separately from source identity. A node keeps the key when it cannot remove one of the labels the key is built from; a `Window` consults `RangeFunction::drops_metric_name` rather than assuming, so the two functions that keep the name still route early. Anything not proven to keep the key — a label rewrite, a histogram map, an unmodelled node — does not establish preservation, and the tree is classified `SinglePartition`. That is not a fallback that trades parallelism for correctness. When the computation removes the labels the key is built from, every series on one side reduces to the same empty key, so every left series matches every right one: the match is whole-vector by construction and has no partitioned form that preserves its result. One partition is the only placement that answers it. Where the key does survive, the match stays partitioned. Signed-off-by: discord9 <discord9@163.com> * fix(query): describe an admitted operand by what it produces `try_plan_native_operand` handed the binary matcher the context saved during scanning, which describes the operand's *input* layout. An admitted call can change that layout: `histogram_quantile` folds a classic histogram and drops `le`, so the scan context still names a tag column the operand no longer produces, and default matching then builds a join key over a column that is not there. Concretely, `histogram_quantile(0.9, m) / histogram_quantile(0.5, m)` declines whole-tree planning (two selectors over one table) and is then admitted operand by operand; each operand drops `le`, and planning fails building the join with `FieldNotFound { field: "le" }` against a schema of `timestamp, field_0, host`. Publish only the tag and field columns that the operand's own plan schema actually contains, and drop `use_tsid`, which the produced output does not carry. The check is against the schema the node returns rather than against a list of functions that happen to shrink it, so a call added later cannot reintroduce the gap by not being on a list. Covered by a classic-histogram fixture with `host` and `le` and a float bucket field, asserting each operand's context reports `["host"]` and that the planned join key omits `le`. Reverting the change fails that test with the `FieldNotFound` above. Signed-off-by: discord9 <discord9@163.com> * test(promql): pin the partitioned duplicate-group answer, with the gap named `native_hybrid_binary_05_06_08` asserted that the right side's two `job="other"` series are refused as a duplicate match group. On the partitioned route they are not: the check is gated on `lhs.presence && rhs.presence` computed from the partition's own slices, so the partition holding `other` sees no left-hand data and filters every point out. Pin what the route actually answers, and state the three things that constrain the fix, because each has already been tried and rejected here: - the gate cannot be dropped. `prometheus_cases.txt:1065-1075` is a Prometheus-generated case where the left is absent at one point and the right carries three samples of one group there, and the answer is a value with no error; - the scan cannot run unconditionally, for the same reason; - the match cannot be collapsed to one partition to make both operands whole, because that gives up the parallelism this route exists for. What remains is to let the verdict see whole-operand presence while the partitions keep answering — a deferred check rather than a different placement. This test now makes any change to the current answer visible instead of silent. Signed-off-by: discord9 <discord9@163.com> * feat(query): segment a nested match instead of gathering it A nested one-to-one match — `x + y + z`, or `(x - y) / on(host) z` — passes the logical admission check, because `independent_scan_pair` recurses through `scan_operand`. The physical locality classification does not go that far: it recognizes a match only when both operands read one source each, so `same_source` fails on an operand that is itself a match and the whole tree is classified `SinglePartition`. The fused computation then ran on one partition while the scans under it stayed parallel, which the existing planner does not do: it builds ordinary equijoins that DataFusion can execute as partitioned hash joins. Admission and placement are different questions, so they now get different predicates. The tree is segmented when it is cross-table, not multi-input, classified `SinglePartition`, and its root is a vector match with a nested vector match directly under it. The inner match becomes a segment and the outer one repartitions the evaluated operands by its own key, which is what the segmenter already does for any exportable evaluated prefix; a nested match whose two levels use different keys is routed twice, once per key, rather than pretending one routing serves both. The predicate stays narrow on purpose. Three-selector trees over distinct metric logical ids are `multi_input` and keep their existing construction, including the parent binding that construction produces. A same-table nested match does not enter this branch either, so its prior admission is unchanged. The plan test's collector walks each fused node's native tree in expression-child order and resolves each input's `source_index` against that node's inputs, so a segmented plan is checked for the same property as before — every selector occurrence bound exactly once, in expression order — rather than being checked more weakly. It panics on a shape it does not model instead of returning an empty list. Signed-off-by: discord9 <discord9@163.com> * fix(promql): prove a raw match key survives the computation `preserves_matching_key` decides whether a subtree keeps the labels a vector match routes by. Two arms answered yes without looking at the field that decides it. A binary with a scalar operand recursed into the vector side alone, but arithmetic and a `bool` comparison drop the metric name while a filtering comparison keeps it — the distinction execution already makes. And an `Instant` recursed unconditionally, while `timestamp(x)` compiles to `InstantValue::Timestamp`, which drops the name too; the check written for the separate `Timestamp` node never covered that shape. Both now read the field that decides: the operator together with `return_bool`, and `InstantValue`. The labels checked are the whole computed set — name, type and unit — because `drop_metric_name` removes all three and default matching does not ignore the last two by itself. Without this, `(x * 2) + on(__name__) (y * 2)` was classified matching-local, the router hashed the raw names before the maps dropped them, and a valid pair could land in different partitions with nothing re-partitioning afterwards. The regression asserts the value is 10 and that the tree is `SinglePartition`, computing the destinations the router would have used (`x0` to partition 0, `y0` to partition 1 of two). With the old arm restored the value assertion fails with `left: [10.0], right: []`. Signed-off-by: discord9 <discord9@163.com> * feat(query): find a nested match through the wrappers around it Segmentation was gated on the root itself being a vector match with a nested match beneath it, so `abs(x + y + z)` and `2 * (x + y + z)` never entered: the wrapper or the scalar broadcast sat where the match was expected. Meanwhile the segmenter already walks children and leaves a wrapper in the parent while cutting what it can carry, so no per-wrapper case is needed — the search just has to look through them. Replacing the root check with `carries_prefix` was too wide: almost any multi-source tree contains something a boundary could carry, so ordinary shapes like `some_metric unless on(tag_0) other_metric` were cut for no gain and their inputs stopped being raw sources. So keep requiring a nested vector match, and search for it anywhere in the tree rather than at the root. Wrappers and scalar broadcasts are transparent because the walk visits every child; the nested match itself is still required, which is what keeps the plain multi-source shapes on the plan they had. `cross_table`, `!multi_input` and the `SinglePartition` locality requirement stay, as before: they are what keeps distinct metric logical ids and the same-table shapes out of this branch. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): let a router publish an identity whose chain is complete `SeriesRepartitionExec` published an identity only once every input had contributed it or ended, because an identity may be split across inputs. A per-series region scan assigns each identity to one partition, so on that shape the rule holds every identity until the other partitions reach EOF: a region's whole raw selection is retained before the first series leaves. The node now carries an `IdentityCompletion`, defaulting to the existing merge. `CompleteDisjoint` publishes an identity as soon as its chain closes and keeps the published identities charged in the pool, so a second chain for the same identity fails the execution instead of emitting a partial series as final. The declaration is the producer's, not something a consumer infers from a node's type: publishing early against a producer that does not own its identities would be wrong, so the conservative mode stays the default and every existing constructor keeps it. Signed-off-by: discord9 <discord9@163.com> * fix(query): choose a series-frame root's router from declared provenance `SeriesFrameOutput` normalized every multi-partition frame root through a router that merges identities, which is wrong for a root whose identities are already disjoint: it waits for every scan partition to end before the first series leaves. The rule now selects `CompleteDisjoint` only for a root whose producer declared that property. A raw root qualifies when `RawSeriesSourceExec` was built through `to_per_series_execution_plan` - which also checks the child is a `SeriesScanExec`, so the type alone is not taken as proof - and a direct `SeriesRepartitionExec` root qualifies because it merged the identities itself. Everything else, including a fused frame root, keeps the merging router: a native label transformation can map two source series onto one output label set, so its output identities are not exclusive. The rejection of a key-only raw frame is unchanged and its reason is restated: coalescing cannot produce such a frame, and a router needs explicit labels to check that one key has one binding. Signed-off-by: discord9 <discord9@163.com> * fix(promql): report the labels `absent` names, not the table's columns `fused_output_tags` answered `None` for `absent`, so the caller fell back to the existing planner's column names. Those are the table's tag columns, which is not what `absent` reports: `absent(m{host="x"})` carries the labels its matchers name, and the compiled tree already knows them in `BoundVectorOp::Absent { labels }`. The label set is now read from that field: the named labels are kept in the input's column order, so `absent(native_shapes{host="missing"})` over a table with `host` and `le` reports `host` rather than `ts, val, host, le`. A label the matchers name that is not an input column still answers `None`, because a frame has no column to carry it; `count_values` and `histogram` are unchanged. Caught by the native-versus-legacy value comparison: the extra column made `absent` differ from the existing plan while every value matched. Signed-off-by: discord9 <discord9@163.com> * fix(promql): describe a frame root's emission from what it does `FusedSeriesExec` declared `EmissionType::Final` for a frame output, but `poll_output` hands a frame's evaluated chunk over while its input is still arriving, so the declaration overstated the node to the optimizer. Only a partial aggregate's state is emitted after EOF; every other output keeps its input's emission type. Signed-off-by: discord9 <discord9@163.com> * test(promql): compare the shapes the native route now admits with the existing plan Removing the shapes a name list used to refuse put single-selector `sort` and `sort_desc`, `absent`, and a bare `histogram_quantile` on the segmented route. Nothing compared their answers with the existing plan, so the widening was unverified. The new case runs each shape both ways over one metric table. `sort`, `sort_desc` and both `absent` cases compare the whole printed result, which covers column names and values together. `histogram_quantile` compares values row by row and asserts the one known difference instead of hiding it: a segmented statement names its value column from the compiled tree (`val`) where the existing plan names it after the expression that produced it. That case is given a window long enough to produce a row, so it cannot pass by comparing two empty results. The `absent` cases caught a real difference: the native route reported the table's tag columns rather than the labels the matchers name, which is fixed in the preceding commit. Signed-off-by: discord9 <discord9@163.com> * fix(query): publish a fused root's identities early when the tree keeps them The root rule publishes an identity as soon as its chain closes only for a root whose producer declared that each identity lives in one partition, and it knew two such producers: a per-series raw scan, and a router that had already merged its identities. A fused root was always treated as needing a merge, so a region whose segment is a plain instant selection over one per-series scan still held every identity until the other scan partitions reached EOF - `sum(topk(1, m))` is that shape, and its segment is already multi-partition with no label rewrite. `FusedSeriesExec` now answers whether its output identities are disjoint, and the rule asks it. The answer is narrow on purpose: one input, and a tree of nothing but inputs and sample instant selections, over a per-series scan or another fused node that already answered yes. Anything that computes labels is excluded, because a timestamp or a pointwise map can map two source series onto one output identity, and publishing early would then hand over one identity's pieces as if they were different ones. A vector-to-vector binary reads across series and is excluded too. Signed-off-by: discord9 <discord9@163.com> * fix(query): segment a computed matching key's operands instead of gathering them A vector match whose operands compute away one of the matched labels was classified `SinglePartition` and executed on one partition. That is correct for the bare tree - the raw names cannot route a match whose operands drop them - but it also gave up the parallelism the operands still have: `(x * 2) + on(__name__, host) (y * 2)` loses `__name__` and keeps `host`, so the computed sides can be routed by what they actually carry. The planner now cuts those operands into segments of their own and lets the parent route their evaluated outputs by the unchanged match key. The key itself is not rewritten: `group_key` reads the labels present at that point, so `On(__name__, host)` degrades to the `host` a computed operand still has. Which shapes take this route is decided by one predicate shared with `input_locality`, so the admission rule cannot drift from the locality judgement: a matching site whose two operands are each proven single-source computations, at least one of which does not preserve the key. A failed preservation proof on its own is not enough - it can also mean the proof gave up, as with a parameter that is a compound expression or an operand that is itself a vector match - and the shared doc says so. The same table read twice is admitted for this shape only. The rule that declines several selectors on one table exists to keep folds such as `sum(m) / m` off this route, not because one table cannot be read twice; the compiler already gives each occurrence its own source index, and each cut gets its own input. Distinct partitions are what the two operands need, and they get them. The regression executes both the same-table and the cross-table shape over a four-host fixture and asserts that at least two output partitions produce rows, so the result is not silently produced by one of them. Signed-off-by: discord9 <discord9@163.com> * fix(query): take the early-publication guarantee from the scan's binding The root rule publishes an identity as soon as its chain closes only when the producer can promise that each identity lives in one partition, and the fused answer to that question asked whether its input was a `SeriesScanExec`. The node's type is not that promise: a scan binds a set of logical tags and decodes those into the series' labels, and `try_to_named_series_scan` requires the caller to pass complete tag names but does not check it. A binding that names only some primary-key columns maps two stored series onto one output identity, and early publication would hand one identity's pieces over as different ones. The answer now reads the binding: the label source has to be `PrimaryKey`, and every primary-key column has to be among the bound tags. A source that carries its labels explicitly is not this shape and answers no, as before. Covered by a new `bound_tags_cover_primary_key` unit test - partial coverage is false, full coverage is true, an explicit source is false - and by tests for the predicate's two other parts: a tree that computes labels and a tree that is not one instant selection over one input both answer no, and a gated execution shows a completed identity leaving a fused node while the other input is still held. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): drop the checks the types and decoders already guarantee Two re-checks ran inside the repartition node over facts its inputs had already established, and both cost real work on every query. A raw merge scanned each chunk's timestamps for strict increase, but `RawTimestamps` cannot hold an unordered buffer: its field is private, its only constructor validates, and the accessors are read-only or order-preserving, so no safe caller can build the value this check defends against. The check is gone; the identity, label and shape checks around it stay, because those compare different producers' pieces rather than one chunk's own timestamps. An owner rebuilt each chunk's identity to prove a chain does not change it, but the decoder that hands the chain over already refuses that: `SequenceValidator` holds one open key and rejects a key or metric change before the chain closes, `EvaluatedSequenceValidator` does the same for labels, and the input task that assembles chains only reads from those decoders. What stays is the identity the owner needs anyway to pick a destination, now built by the named functions rather than inline. Where a schema was needed, the node built an empty stream of its own, wrapped it in a packer and read the packer's schema back. The frame module already answers that question from the declaration, so the fake stream is deleted along with its half of the roundtrip. Signed-off-by: discord9 <discord9@163.com> * fix(promql): recursively segment native matching plans Signed-off-by: discord9 <discord9@163.com> * fix(promql): derive native schemas without legacy probes Signed-off-by: discord9 <discord9@163.com> * fix(promql): recognize complete metric scan identities Signed-off-by: discord9 <discord9@163.com> (cherry picked from commit 8f752a02dbb9dc7858618c1d5cc0c41e2d4efb23) * fix(promql): release completed disjoint identity history Signed-off-by: discord9 <discord9@163.com> (cherry picked from commit 9975fa6c5c68786e18af230f62041d2aa5e71f6c) * test(promql): assert native aggregate schemas independently Signed-off-by: discord9 <discord9@163.com> * fix(promql): identify internal metric keys by reserved id Signed-off-by: discord9 <discord9@163.com> * fix(promql): keep parallel sources and derive group match labels Signed-off-by: discord9 <discord9@163.com> * test(promql): observe the frontend fused root in scan-parallelism checks Signed-off-by: discord9 <discord9@163.com> * fix(promql): release duplicate evidence immediately on abort Signed-off-by: discord9 <discord9@163.com> * test(promql): preserve operand identity across matching repartition Signed-off-by: discord9 <discord9@163.com> * fix(promql): charge the final duplicate verdict until it is reported Signed-off-by: discord9 <discord9@163.com> * test(promql): observe native scans across frontend and region plans Signed-off-by: discord9 <discord9@163.com> * chore(promql): drop dead test helper and clippy lints Signed-off-by: discord9 <discord9@163.com> * fix(promql): select limitk by canonical label order Signed-off-by: discord9 <discord9@163.com> * fix(promql): keep data-free subtrees in the parent segment Signed-off-by: discord9 <discord9@163.com> * test(promql): prove the duplicate error crosses matching partitions Signed-off-by: discord9 <discord9@163.com> * test(promql): drop debug prints from the segmentation test Signed-off-by: discord9 <discord9@163.com> * revert(promql): keep limitk's Prometheus input-order semantics Signed-off-by: discord9 <discord9@163.com> * feat(promql): admit label rewrites on the native route The native route declined label_replace and label_join outright, because the runtime refuses two series that a rewrite gives one canonical label set where the existing plan answered them. That refusal is what Prometheus does, so the existing answer was the wrong one, and on the current parent the refusal already reaches a rewrite that folds series together: the collapse errors at the root, under sum and under topk. Admit both functions: only the first argument is a vector expression and contributes selectors, and each function's arity is checked before its selectors are collected. Signed-off-by: discord9 <discord9@163.com> * feat(promql): derive canonical ownership and insert the merge consumers need Stacked on the frame-segment route so the ownership this pass proves is the ownership the physical plan actually gives it: a merge is inserted only where its input is co-located by canonical key, which the routing boundary establishes. The pass answers one series per canonical label set at the consumers that read series whole, and leaves the point-wise, kernel-filed and aggregate paths alone - their own duplicate checks already refuse two values of one field at one point. (cherry picked from commitd0bbfc6893) Signed-off-by: discord9 <discord9@163.com> * test(promql): pin the label rewrite shapes the native route now admits The rewrite test still asserted that every label_replace and label_join keeps the existing plan, which was the admission this branch removed. Assert what the route does now: the shape is admitted, its rewritten label is declared, and the plan differs from the one built with the flag off. Signed-off-by: discord9 <discord9@163.com> * refactor(prom… * perf(promql): resolve a partial fold's labels once per group (#10145) `PartialFold::encode` writes one row per (point, field) of a group, and it looked every output tag up with `SeriesLabels::get` for each of those rows. That lookup binary-searches the group's tags, so the loop paid `rows * tags * log(tags)` string comparisons for a value that cannot change inside a group. Resolve the tags once per group and push the resolved values. Measured with `perf` on a `step=1s` range query over 28.8M grid points (the native PromQL path). Before the change `SeriesLabels::get` held 12.8% of samples (self) and `__memcmp_avx2_movbe` 14.5% (self); both frames are gone from the profile after it, and the remaining native-side hot spots are the sample iterators, `select_instants`, `seen_points` and `push_samples`. Wall clock on that query moved from 6.8s to 6.1s, from a single paired run rather than a repeated measurement. Behavior is unchanged: the same tag values are pushed in the same order. `cargo nextest run -p promql` passes 1115/1115, including `aggregate::tests::partials_merge_to_the_serial_result`. Signed-off-by: discord9 <discord9@163.com> * perf(promql): walk a batch's window ends instead of searching each one (#10113) * perf(promql): walk a batch's window ends instead of searching each one A range function's windows advance with the grid, so the samples one window covers are a prefix of the next window's: the end only moves forward. The batch path nevertheless binary-searched the retained timestamps once per point, per field, to find where each window ends. On a profile of `rate(...[1m]) / rate(...[1m])` over 1801 grid points, that search was the largest single flat entry: `accumulate` accounted for 5.39% of sampled CPU and `-peek` showed all of it inside `partition_point` -> `binary_search_by`. A batch now carries one cursor per field, reset when the batch is cleared, and `accumulate` advances it while the timestamp is at or before the evaluation timestamp. The predicate is the same and the array is ordered, so the span is identical; the cost drops from O(points x log samples) to O(samples). On that query each batch went from ~16,800 comparisons per series to ~216. `clear_points` resets the cursors because `FieldSamples::advance` drains the retained arrays, which the existing debug assertion already requires to happen only after the batch is flushed. The cursors are charged in `bytes()` with the rest of the batch. Measured with the server's own CPU time (utime+stime from /proc), alternating run order over 11 paired rounds on the same fixture: the bare-rate query, where the evaluator is a large share of the work, used 1.10-1.12x less CPU (8 of 11 rounds faster); the rate-over-aggregate query, where the aggregate split dilutes it, 1.02x. No wall-clock claim: the host ran at a load average above its core count and wall time swung 20-47% between rounds. Correctness: `cargo nextest run -p promql` 1099/1099, including the brute-force evaluator reference and the random-input batch-versus-per-window comparison. The HTTP responses of both queries hash identically before and after the change. Signed-off-by: discord9 <discord9@163.com> * perf(promql): keep the window cursor across batch flushes The cursor added in the previous commit was reset on every batch flush, but a flush does not drain the retained arrays: `FieldSamples::advance` does, once at the end of `evaluate`. So after each 512-point batch the cursor restarted at index 0 while `head` sat mid-array, and the next batch rescanned the samples already behind every remaining window. Measured in timestamp comparisons for 100,000 points at ~6 samples per window: the pre-optimisation binary search cost 1,536,267, resetting on every flush cost 9,985,289, and keeping the cursor costs 200,005. A single point over a 100,000-sample window costs 18 comparisons, so a large window is still located by search rather than walked, and 600 points across one flush boundary cost 1,216. The cursor is now an index into the retained arrays, valid across flushes and invalidated only where they are drained: `evaluate` resets it next to `advance`, and only when a field actually drained. Before use it is clamped to `field.head`, so samples behind the window are never revisited. A `positioned` flag marks a cursor that has no valid index yet, and the first point after a reset locates the end with `partition_point`; the rest of the batch walks forward from there. The comparison counts are pinned by a test, and it was checked against both broken shapes: resetting in `clear_points` fails it at 203,125 (the binary search still saves it), and the pre-fix shape of resetting plus walking from zero fails it at 9,985,289. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * feat(promql): execute split native aggregates as partial state over a state router (#10147) * feat(datatypes): add the shared aggregate-state representation A frame that carries partial aggregate state has to say which aggregate it is state of and which accumulator components it carries, or a consumer can read a sum state as an average one. These types are that statement: the kind, the ordered component list, one position's values, and the schema that validates the two against each other. Nothing uses them yet; the frame content that carries them follows. Signed-off-by: discord9 <discord9@163.com> * feat(datatypes): carry aggregate state as a third frame content A state frame keeps the series-major columns a frame already has — the key, finality, grid spans and labels — and puts each aggregate field's accumulators in one list of component structs under `fields`, the shape a classic histogram payload already uses. An absent list item is that field having no contribution at that position, so a hole stays a hole instead of becoming a zero, and a group state — which carries no component at all — still says per field where it was folded. The reader keeps typed handles into the batch rather than expanding the state into rows and rebuilding it, so the merge above it reads the arrays where they lie. Signed-off-by: discord9 <discord9@163.com> * feat(common-query): pack and unpack aggregate-state series frames The packer takes what a partial aggregate produces — one group, its carried positions and its per-field state — and the unpacker hands back the row with typed handles into the batch. That asymmetry is deliberate: export columnarises once, import must not rebuild the state to read it. Validation mirrors the evaluated pair: the declaration is parsed, contiguity required, the empty batch decoded before the stream is returned, and kind, component order, grid and field count are each checked with the part that disagreed named. Signed-off-by: discord9 <discord9@163.com> * feat(promql): run native partial and final aggregates An aggregate node used to compute only `Complete`, refusing the stages a split aggregate needs. It now folds evaluated chunks into kernel state for `Partial` and merges that state into values for `Final`, so the compiler's `Partial -> Final` plan has a runtime behind it. The executor gains a third chunk kind: an input leaf accepts raw, evaluated or state chunks according to what its binding produces, never guessing from a schema, and a state chunk cannot be handed to a node that reads values. The state binding now carries the aggregate kind and its ordered components, derived from the kernel's own state fields, so a sum state cannot be merged as an average one. The sibling router refuses aggregate state explicitly for now; the node that routes it follows in its own change. Signed-off-by: discord9 <discord9@163.com> * feat(promql): route aggregate state by its group labels A state row is one group, complete as one producer wrote it: there is nothing to chain and nothing to merge, so the existing repartition node — whose invariants are all about assembling a key's chain and waiting for every input to close — is the wrong place for it. This node does one thing: send each row to the output its canonical labels name, so every contribution of a group reaches one owner and the merge above sees all of them. The read belongs to the outputs that were claimed, not to the plan, so dropping every output stops it and returns what it reserved. Signed-off-by: discord9 <discord9@163.com> * feat(promql): execute partial aggregates as state frames A fused node now reads its inputs by what they produce — raw samples, evaluated values, or aggregate state — instead of inferring the kind from a schema, and a partial root answers state rather than rows: the executor's state chunks are packed into the aggregate-state frame the merge above consumes. The temporary partial-fold bridge goes with it: no marker field, no side channel fold, no compatibility accessor. A tree that folds on the side is exactly what this change exists to remove, and the aggregate merge that replaces it is a separate node. The state path derives its mode and its binding from one reading of the root, so the two cannot disagree, and no production path panics on a malformed tree. Signed-off-by: discord9 <discord9@163.com> * feat(promql): split native aggregates into partial and final trees The previous commit made a `Partial` aggregate export its state, but left `AggregateFinalize` in place: it still called `FusedSeries::try_new_partial` and `partial_fold`, which that commit deleted, so the crate did not compile. Replace the bridge with the two trees that run a split aggregate: - `native/split.rs` owns the cut. It moves out of `aggregate_finalize.rs` unchanged in structure, and `Split::into_trees` binds the two halves: the prefix under a `Partial` aggregate answering aggregate state, and a `Final` aggregate consuming that state with the suffix above it. - The suffix is narrower than the old merge admitted. Only point-wise maps and a binary operation with a constant side may sit above the cut; `topk`, the classic-histogram functions, a second aggregate and a vector matching are declined, because the groups a `Final` aggregate owns are split across partitions and each of those reads a whole vector. - `FusedSeries` gains `try_new_state_export` (a `Partial` node declares its own state frame schema rather than taking a caller's), `split_aggregate` (declining shapes become `None`, not an error), `reads_aggregate_state` and the state-input path of `to_execution_plan_with_parallelism_option`, which routes state through `StateRepartitionExec` and lets a cross-series tree read state from more than one partition. - The runtime executes the `Final` stage: the guard that refused it is gone. `AggregateFinalize`, `aggregate_exchange` and their tests are deleted; nothing in the crate referenced the exchange module. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): route aggregate state through the series repartitioner Replace the dedicated state-repartition node with the existing series repartition execution: state batches are routed by their canonical labels without being rebuilt as StateSeries, and each row slice is charged for the Arrow wrappers it newly allocates while the parent buffers stay charged once. Carry a producer's state charge with the chunk it hands over, so the exporter that packs rows across polls remains accountable for the batch it still holds instead of releasing it at handoff. Read a Final aggregate's value and tag columns positionally, which is the order the split's output schema is built in, and name them through the legacy expression only at the outer boundary; a bare sum over a metric whose value column is named after the whole expression no longer fails planning. Pack aggregate-state frames in one pass over positions and validity instead of scanning the contributing rows for every position. Cover the changed behaviour with execution tests for bare global and grouped sums over one- and two-value-field tables, for the handed-over state charge, and for the export lifecycle on drain and early drop. Signed-off-by: discord9 <discord9@163.com> * fix(promql): bound tree depth on both sides and validate merge inputs The depth limit was decode-only and set to a number nobody had measured. A 129-deep tree could therefore be built, serialized and shipped, while the receiver refused it; and in CI the boundary test itself aborted with a stack overflow, because a 2 MiB thread was asked to build, serialize, decode and drop a chain of that depth, and the recursive drop of `Box<NativeNode>` is not covered by a guard that only wraps `Deserialize`. Measured instead of guessed. A probe walked build, serialize, decode, validate and drop at increasing depth inside a named 2 MiB thread: 32 through 128 completed, 160 aborted. The cap is now 64, half the highest passing depth, and the same number is enforced in `NativeTree::try_new` and in the deserializer. Verified through the real constructor: a 64-node tree round-trips, and a 65-node tree is refused when it is built as well as when it is decoded, so sender and receiver accept the same set. The boundary test runs on a named thread, which is how a future failure will identify itself. The merge node accepted any single input. `with_exprs_and_inputs` rebuilt `AggregateFinalize` over whatever it was handed, on the reasoning that a rewritten input only changes where the state comes from. Nothing checked that it was state at all -- and until now nothing could produce a counterexample, because a values-producing producer could not be serialized. The serializer was standing in for a validation that belonged to the node. `with_exprs_and_inputs` now rejects an input whose schema is not `state_schema(binding.tags)`, so substituting a computed-values producer fails at the logical node with a message naming the expected schema, rather than depending on an encoding limitation. `AggregateFinalizeExec` already had the same check at physical planning; the serializer stays permissive. The three placement tests in `commutativity.rs` asserted the old refusal as their mechanism -- an ordinary fused node "fails to encode", a partial producer "cannot be pushed down". Those expectations were the encoding limitation wearing a test's clothes. They now assert the responsibilities that remain: an ordinary fused node round-trips through the encoder, a partial producer rides to the region while its finalize stays on the frontend, and a values producer handed to a merge is refused by the schema contract. The test-only `NPRQ` envelope is gone. It framed trees with its own version, a 1 MiB cap and its own rejections, none of which production had, so `native_tree_envelope_rejects_version_size_trailing_and_truncation` was proving a helper's protocol while the PR description claimed a production guarantee. Trees are tested through plain postcard, transport through the real `FusedSeries` interface. The redundant clone-and-revalidate in `FusedSeries::deserialize` is also gone: `NativeTree::Deserialize` already routes through `try_new`. Signed-off-by: discord9 <discord9@163.com> * style: let taplo order the workspace dependency entry `taplo format --check` runs over the whole workspace in CI, not over the two manifests I had checked. It sorts the `[workspace.dependencies]` table, so `postcard` belongs in alphabetical position inside the external-dependencies block rather than immediately after its comment. Local checking was narrower than the CI command, which is why this only surfaced there. Signed-off-by: discord9 <discord9@163.com> * style: place postcard in the sorted position the review asked for Inside the external-dependencies block, `taplo` was willing to leave the entry before the `axum` comment, because a comment line ends a reorder group. The review asked for the alphabetical position, which `taplo format --check` also accepts, so use it. Signed-off-by: discord9 <discord9@163.com> * docs(promql): name the real reason a multi-source name grouping declines The comments still said the fused node's declared columns cannot name `__name__`. Since #10106 a discovered source declares it, so a nameless selector's `sum by (__name__)` survives; what actually declines is a tree reading more than one *named* source, where the table name is the metric name and no source declares the tag. Same conditions, accurate reason. Signed-off-by: discord9 <discord9@163.com> * test(promql): find the topk producer by behaviour, not by node count `native_aggregate_topk_over_global_partitions` asserted that the whole plan holds exactly one fused node. Once native frames cross the region boundary a plan legitimately holds the frontend root and the region producers at the same time, so that count is no longer an invariant. The test now selects the segmented producer and the ranking node by what their plans do: at least one producer contains `segment` and none of them runs `topk`, and exactly one node across the fused and final captures ranks. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * fix(mito2): release metadata cache locks before asynchronous loads (#10125) * fix(mito2): avoid holding metadata cache locks across loads Signed-off-by: evenyag <realevenyag@gmail.com> * fix(mito2): atomically admit loaded columnar metadata Signed-off-by: evenyag <realevenyag@gmail.com> * test(mito2): verify concurrent loaders share admitted metadata Signed-off-by: evenyag <realevenyag@gmail.com> --------- Signed-off-by: evenyag <realevenyag@gmail.com> * test(compat): exercise the bloom-token case only below v1.3.0 (#10170) `fulltext_bloom_tokens` reads a fulltext bloom SST in the first format and asserts that rows written by the older binary stay visible next to a current one. Its `from_range` started at v1.0.0, but the released v1.3.0 binary already writes `bloom_version: 2`, so the setup produces two current-format SSTs and the expectation ("one old, one new") can never hold: expected: | false | 1 | actual: | true | 2 | expected: | true | 1 | Narrow the range to the releases that still write the first format. The constraint language has `>=`, `<=`, `==` and `=` only, and several entries are alternatives rather than a conjunction, so `<=v1.2.99` is the bound that excludes v1.3.0 while keeping v1.2.x and older - including the nightly window. Verified with the compat runner: `--from-version v1.2.1 --dry-run` selects the case, `--from-version v1.3.0 --dry-run` skips it with `from_range ["<=v1.2.99"] does not match version 'v1.3.0'`, and `update-compat-versions.py --check --published-only --check-anchors` passes. Signed-off-by: discord9 <discord9@163.com> * perf(mito2): buffer sample artifact writes across blocks (#10105) * perf(mito2): buffer sample artifact writes across blocks Signed-off-by: evenyag <realevenyag@gmail.com> * test(mito2): use telemetry for sample writer benchmark output Signed-off-by: evenyag <realevenyag@gmail.com> --------- Signed-off-by: evenyag <realevenyag@gmail.com> * perf(mito2): merge columnar samples using identity-only keys (#10126) * perf(mito2): merge columnar samples with identity-only keys Signed-off-by: evenyag <realevenyag@gmail.com> * perf(mito2): instrument columnar sample read stages Signed-off-by: evenyag <realevenyag@gmail.com> * refactor(mito2): clarify reader timing and focus merge regression coverage Signed-off-by: evenyag <realevenyag@gmail.com> * test(mito2): cover first-field null recovery in identity merge Signed-off-by: evenyag <realevenyag@gmail.com> --------- Signed-off-by: evenyag <realevenyag@gmail.com> * perf(mito2): encode fallback candidate keys from borrowed labels (#10133) perf(mito2): encode candidate keys from borrowed labels Signed-off-by: evenyag <realevenyag@gmail.com> * feat(promql): query native histograms through the native PromQL route Admission reads the field's kind, the output column is typed by what the compiled tree answers, the reader follows the field kind, and the response writes the canonical histogram column. PromQL warnings reach the native kernels. Tracks #8887. * fix(promql): answer a nameless selector that matches no metric with the empty vector (#10159) fix(promql): answer a nameless selector with no metric by the empty vector A nameless selector that matches no metric table returned nothing to the native route, which fell back to the existing planner; there the missing metric name surfaced as a `TableNameNotFound` error instead of the empty result Prometheus returns. Plan a typed empty input instead: an empty relation whose schema is the raw explicit-label series frame a scan would declare, with one field and no evaluation grid. Declaring the frame is what tells the physical planner this is a producer, so no region scan is looked for below it, and every operator above keeps its own answer for zero series - an ungrouped fold reports no group rather than 0, `absent` still derives its labels from the matchers, and `or` keeps the vector operand's own row. Two shapes keep the old fallback deliberately: a statement holding a second selector occurrence (a real scan's partition count and the empty frame's one cannot share the node's partition contract) and a `__field__` matcher (a field projection, not ordinary label filtering). Authorization that is unavailable still fails closed, and the metric cap still errors, both before this branch is reached. The discovery route had no value-level test: every existing one asserts a schema or a source name. Add one that runs the Prometheus handler against two metric tables with identical ordinary labels, so only `__name__` tells them apart, and asserts each row's name and sample; disabling the `__name__` tag injection leaves the values and empties the names. Signed-off-by: discord9 <discord9@163.com> * fix(promql): carry each row's metric name through to the response (#10162) A PromQL response could carry only one metric name for the whole result, derived from the statement. That is wrong whenever the rows of one result come from different metrics: a set operator reports the series of both its operands, so `left_metric or right_metric` returned the right-hand series under the left metric's name, with their own labels and samples intact. Carry the name as a column and declare what that column is: the column metadata key `greptime:metric_name`, the same place `greptime:time_index` declares a column's role. A consumer reads the declaration, not the column name, so a label an operation generates may itself be called `__name__` without being mistaken for one. The native planner adds `__name__` to the output tags of `or` -- the only set operator whose output mixes both operands -- and marks that column. Every other shape that keeps a per-row name already carries it the same way, since a native `__name__` tag always reads the series' own metric-name slot; arithmetic and aggregates drop it, as Prometheus does. The response layer reads the declared column per row instead of prepending the statement's name, keeps it out of the ordinary label set, and treats an empty value as no name, since Prometheus drops empty label values. A result with no declared column behaves exactly as before. Scope: the native route only. The legacy planner carries no per-row name and keeps naming rows from the statement, so its set-operator results are unchanged here. Signed-off-by: discord9 <discord9@163.com> * feat(query): execute native raw promql over real datanodes (#10150) * feat(promql): serialize raw series source bindings through Substrait Signed-off-by: discord9 <discord9@163.com> * feat(query): transport interleaved raw series fragments Signed-off-by: discord9 <discord9@163.com> * feat(promql): reconstruct interleaved raw series at repartition Signed-off-by: discord9 <discord9@163.com> * feat(query): plan distributed native raw series Signed-off-by: discord9 <discord9@163.com> * chore: satisfy clippy for the raw series transport The workspace clippy run rejects a redundant rebinding, a constant-size `chunks_exact`, and an implicit `String` clone in the tests these commits carry. No behavior changes. Signed-off-by: discord9 <discord9@163.com> * feat(query): execute native raw promql over real datanodes Plan a whole native statement as one fused node over raw series sources when the frontend reaches regions over the wire, including single-selector statements, while keeping the in-process split and segment shapes unchanged. Validate the whole tree with the runtime constructor before committing to that plan, so unsupported shapes still fall back to the legacy route. Bound the region reads of that topology with a query-wide permit budget of target_partitions, held until each region body is drained; other plan shapes keep unbounded reads rather than risk a body budget they could deadlock. Acceptance on a real two-datanode cluster with TCP frontend clients: time-partitioned series are reconciled before the fused node and match the unpartitioned control, with the DoGet peak at the target bound; series that no region shares keep contiguous per-series order and reach the fused node without a reconciling router. Signed-off-by: discord9 <discord9@163.com> * chore(deps): pin greptime-proto to the raw series transport PR The raw series Substrait message lives in greptime-proto PR #349, which is not merged, so pin its head commit instead of carrying a local path patch. Move the rev to the merge commit once #349 lands. https://github.com/GreptimeTeam/greptime-proto/pull/349 Signed-off-by: discord9 <discord9@163.com> * fix(promql): reconcile a discovered union of raw series A nameless selector reads its matches as one `Union`, and `UnionExec` reports the frame metadata of its last child. A time-partitioned metric sorted before a host-partitioned one therefore looked contiguous, and the fused node skipped the reconciliation of the fragmented member's series, silently folding region fragments as separate series. Lower the declaration on the fused edge to the weakest ordering any member declares, before the fused node reads it. The union itself is unchanged; the fused node's existing interleaved-input path inserts the router. A projection carries the lowered declaration because the pinned physical planner ignores a union's own schema. Cover it with a planner regression for both member orders and an all-complete control, and with a real two-datanode acceptance case that mixes a time-partitioned and a host-partitioned metric and checks the extrapolated increase per series. Signed-off-by: discord9 <discord9@163.com> * test(promql): cover aggregates over discovered metrics A nameless selector's metric comes from its own frames, so a folding aggregate is a multi-input tree even with one selector: apply the same preflight and order-dependent refusals on the remote route. Cover `sum(increase(...))` and grouping by the metric name over the mixed discovery fixture. A grouping that names only `__name__` stays declined, since the output schema cannot name that group: here the metrics also differ by `site`, so grouping by the name and `site` exercises the two groups. Signed-off-by: discord9 <discord9@163.com> * fix(query): keep remote multi-source refusals the split made Skipping the aggregate split and `segment_tree` over the wire must not drop their admission refusals. Two sources that fold groups by metric name would report groups that differ only by name under one label set, and an order-dependent operation over a discovered selector's union would answer in arrival order. Scope both refusals to the actual multi-source inputs rather than to the logical-table-id heuristic: a discovered selector reads several metrics through one selector, and a Mito table beside a metric one carries no logical id. Cover the Mito-plus-metric grouping refusal on a real two-datanode cluster, and cover a discovered `sum by (__name__)` over a raw selector (the name survives it) and a folding `sum(increase(...))`. Signed-off-by: discord9 <discord9@163.com> * docs: drop the local proto patch prerequisite The dependency is pinned to greptime-proto PR #349's commit, so the wire fixture no longer needs a local path patch. Signed-off-by: discord9 <discord9@163.com> * chore: finish the narthex rebase resolution Keep the upstream aggregate split (`FusedSeries::split_aggregate`) and pass the engine state through the native planner's callers, while the remote route still declines the split it cannot carry. Signed-off-by: discord9 <discord9@163.com> * test: pass the engine state to the native operand test The native operand planner now takes the engine state, which decides whether a statement is planned for remote regions. Keep the histogram operand contract test on that signature. Signed-off-by: discord9 <discord9@163.com> * fix(query): keep the split and segment plans over the wire Remote planning disabled the aggregate split and segmentation because the split's producer read a bare scan. The producer reads whatever the statement prepared, and over the wire that is the raw series source: the partial half runs per partition on the frontend, the final half folds the exported states, and no aggregate state crosses the wire. Keep the split and let segmentation build evaluated segments from the raw sources instead of bare scans, so per-series window work keeps the scan width. Admit the resulting fused chain to the query-wide read budget, since each fused node polls its input while it is drained. Remove the recovery-projection wrapper: the analyzer no longer inserts that projection, and the physical planner consumes the frame boundary as it is. Serialization is now only the node's encoding: the codec no longer decides placement, and the metric-name binding is checked against the declared frames rather than the node types below. Signed-off-by: discord9 <discord9@163.com> * refactor(query): drop the in-process encodability exception A split aggregate encodes now, so the analyzer no longer needs a special case that skipped the encoding gate for it. The gate itself stays: every plan a region receives still has to encode. Signed-off-by: discord9 <discord9@163.com> * test: cover a repeated selector over one partitioned table Two occurrences of one time-partitioned metric keep their own raw source, time bounds and identity over the wire, so a ratio of two windows and a binary expression over two identical ones stay native and reconcile each occurrence's fragments before the fused node reads them. Compare both against the unpartitioned control. Signed-off-by: discord9 <discord9@163.com> * fix(table): refuse a scan that plans interleaved ordering A scan's planned ordering is a guarantee its own partitions keep. Interleaved is the transport annotation for fragments several producers contribute, so a scanner that planned it has no raw-series read whose frame would honour that declaration. Reject it in the scan conversion, while the frame codec keeps accepting interleaved raw frames from the router that reconciles them. Signed-off-by: discord9 <discord9@163.com> * test(promql): assert the metric name beside the value `sum by (__name__)` over a discovered selector was asserted as a bare sorted list of values, so a plan that grouped by the wrong metric name - or dropped the name and folded everything into one series - could produce those same numbers. Read `__name__` and `val` by column name (the result puts `__name__` last, and a fold that drops the name drops the column rather than emptying it) and compare the `(name, value)` pairs against the fixture's own metric names. Signed-off-by: discord9 <discord9@163.com> * fix(query): keep the hybrid route for a partitioned pair A multi-selector tree over a partitioned table was admitted as one fused node for any pair of tables, not only for scans of one table. That preempted the frontend hybrid route, which copies the matcher's labels into each operand's scan and checks uniqueness above the join. `native_hybrid_binary_05_06_08` depends on exactly that plan: its partitioned RHS holds a duplicated `job="other"` group, so the whole-native binary reports the duplicate instead of the value the hybrid route returns. Refuse the whole-fused plan for that shape in process again; the remote raw-source admission is unchanged, since a remote multi-selector tree over a partitioned table is the point of this branch and the distributed acceptance tests read it. The hybrid route then has to behave the same way it does without native operands: `matching_filters::propagate` now runs for them too, and a rewritten operand is re-planned through `try_plan_native_operand` with its original `selector_matcher` preserved, falling back to the ordinary plan when the native route declines. The unpartitioned case, whose duplicate group the whole-native binary is expected to report, is unchanged. The two new re-plan call sites put the native route's large future inside the recursive planner frame at every nesting level (~178 KB became ~209 KB), which overflowed the 2 MiB test-thread stack in `test_parse_or_operator` even where the new branch never ran. Box the future in a non-async helper and move the re-plan into `replan_rewritten_operand`, which brings the frame back to the old size without changing the behaviour. The partitioned-pair unit test asserted the removed shape. It now asks for the remote engine state, where a cross-table pair really is one fused node over two raw sources, and asserts that the same pair in process keeps the hybrid route. Signed-off-by: discord9 <discord9@163.com> * chore(deps): pin greptime-proto to the merged raw series transport GreptimeTeam/greptime-proto#349 has been merged into `narthex-proto` as `f51cb5312bf7`. Move the rev from that PR's head commit to its merge commit, in `Cargo.toml` and `Cargo.lock` together, so the pin no longer points at an unmerged ref. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * test(query): cover series-frame completion policy selection (#10151) * test(query): pin the completion-policy choice for each eligible root Signed-off-by: discord9 <discord9@163.com> * test(query): pin fused roots without disjoint identities Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * perf(promql): keep the grid position in i64 instead of dividing in i128 (#10175) `grid_position` runs once per reported point and computed the position with `i128` arithmetic: `div_euclid` and `rem_euclid` on the same operands, which become two `__divti3` calls for a timestamp and a start that are both `i64`. Stay in `i64` when the subtraction does not overflow and the offset is not negative: `div` and `rem` on the same operands become one machine divide. Off-lattice timestamps, positions past the end, negative offsets and overflowing subtractions fall through to the `i128` path, which answers the same as before, error message included. The fast path is a strict subset of what the `i128` code accepted, so the answer cannot change; a wrapped subtraction is the one way it could, and a test pins that an overflowing offset still goes to the wide path. Another test pins that a lattice whose offsets do not fit in an `i64` at all still answers, so the fast path cannot quietly narrow what the function accepts, and a third pins the lattice itself. Measured on a step=1s range query over 28.8M grid points on the native PromQL path, retired instructions per query, alternating the two binaries: session A (3 rounds) before 51,233.33 / 50,677.21 / 50,795.70 M after 47,174.07 / 46,378.66 / 46,473.54 M session B (2 rounds) before 51,343.56 / 51,232.13 M after 45,803.16 / 45,942.42 M That is 8.5% to 10.6% fewer instructions, with every paired round moving in the same direction and no overlap between the two ranges. The two sessions do not share a baseline to better than about 1%, so the range is reported rather than one figure. The `perf` samples put `grid_position` at 3.99% of the profile and the divide at 3.6%, which understates this: `__divti3` is dozens of instructions per call, so a sampling share reads low where an instruction count does not. `cargo nextest run -p promql` passes 1123/1123. Signed-off-by: discord9 <discord9@163.com> * feat(query): place a native label-set merge above the regions (#10191) * feat(query): place the canonical merge above the regions A canonical producer merges the pieces of one label set that its own input partitions hold, and the canonical router FusedSeries::to_physical_plan inserts above it collects them from exactly those partitions. In process the producer reads the region's own scans, so the region holds every piece its router has to see and that placement is the only one it has. Over the wire the producer reads RawSeriesSource children instead - the wrapper that reconciles fragments several regions hold. Classifying that shape commutative shipped producer -> router as the region sub-plan, one producer and one router per region, each collecting only its own region's pieces, so a label set a time partition spread over several regions was answered from one region's share. Classify a canonical producer by its own contract only: commutative when every child is a bare scan, non-commutative otherwise, and never fall through to the per-series rules, which describe what a node computes and say nothing about where the pieces of one label set are. The analyzer then cuts at the raw source, so the frontend holds consumer -> SeriesRepartitionExec(CanonicalLabels) -> producer -> [raw router] -> MergeScan(RawSeriesSource(TableScan)) and the canonical router collects each label set from the producers of every region, while the fragments of each series are still reconciled below the producer by the raw-primary-key router the interleaved raw input asks for. Admission follows the placement: the in-process route still declines a canonical merge over a partitioned table, because a region-local producer is the only placement it has, and the remote route now admits it. Acceptance is a real two-datanode test over a three-partition metric table: the rows equal an unpartitioned control's, the producer and its canonical router are frontend nodes above the merge scans, and a collision two regions hold reports the Prometheus duplicate-labelset error that no single region can see. Signed-off-by: discord9 <discord9@163.com> * test(promql): partition the canonical acceptance table on a tag GreptimeDB has no implicit time partitioning: a table has more than one region only from a declared partition, and metric tables are normally partitioned on a tag. The acceptance fixture partitioned on the time index instead, which made the test read as if time partitioning were the everyday case. Partition on `host` in three ranges - `early`/`first`/`late`/`second` still land in different regions, so the merge pair and the collision pair keep their cross-region geometry - and rename the test accordingly. A partition on a primary-key column that excludes the time index also proves the regions hold disjoint series, so the two assertions that described fragmented series state what this fixture actually proves instead: the merge scan publishes whole series (`contiguous`), and no raw-key repartition sits below the frontend producer because only the label set crosses regions. Signed-off-by: discord9 <discord9@163.com> * refactor(query): ask the transport where a canonical merge runs `canonical_boundary_region_node` inferred "this execution reads the whole table" from the node's children: a canonical producer whose children were all bare scans was admitted inside a region sub-plan. Over the wire every selector input is a `RawSeriesSource`, so the inference happened to agree with the rule that matters, but a wire plan that ever held a bare scan under a producer would have been admitted silently and answered a label set from one region's share. The analyzer already holds the fact. `in_process_region_transport` is what it was built with, and it is exactly the condition under which a region read is the whole table. Pass it down to `Categorizer::check_plan` and `check_extension_plan` and require it instead of inspecting child shapes. Guard the hole the inference hid: a canonical producer over a bare table scan classifies `Commutative` in process and `NonCommutative` on the wire. Signed-off-by: discord9 <discord9@163.com> * test(promql): cover a fragmented canonical merge too A partition on a primary-key column proves the regions hold disjoint series, so the tag-partitioned fixture could not exercise the other geometry the frontend merge has to read: fragments. In a metric table a tag is always a primary-key column and a field must be Float64, so the only declared partition that proves nothing about where a series lives is the time index. Run the same two fixture rows over both geometries in one loop. The tag case still publishes whole series and has no router below the producer; the time case declares interleaved and asserts exactly one RawPrimaryKey reconciling router between the merge scans and the frontend producer. Both keep the unpartitioned control's rows, the frontend placement of the producer and its canonical router, and the cross-region duplicate-labelset error. Signed-off-by: discord9 <discord9@163.com> * Revert "test(promql): cover a fragmented canonical merge too" This reverts commit8ff9f43031. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * feat(mito2): expose columnar preparation and reader stage metrics (#10132) * perf(mito2): merge columnar samples with identity-only keys Signed-off-by: evenyag <realevenyag@gmail.com> * perf(mito2): instrument columnar sample read stages Signed-off-by: evenyag <realevenyag@gmail.com> * refactor(mito2): clarify reader timing and focus merge regression coverage Signed-off-by: evenyag <realevenyag@gmail.com> * perf(mito2): measure columnar discovery preparation stages Signed-off-by: evenyag <realevenyag@gmail.com> * perf(mito2): expose series discovery reader costs Signed-off-by: evenyag <realevenyag@gmail.com> * test(mito2): cover first-field null recovery in identity merge Signed-off-by: evenyag <realevenyag@gmail.com> * perf(mito2): aggregate preparation counters once per batch Signed-off-by: evenyag <realevenyag@gmail.com> * fix(mito2): retain stage timing on failed and cancelled reads Signed-off-by: evenyag <realevenyag@gmail.com> --------- Signed-off-by: evenyag <realevenyag@gmail.com> * perf(mito2): coalesce bounded columnar sample reads (#10127) * perf(mito2): merge columnar samples with identity-only keys Signed-off-by: evenyag <realevenyag@gmail.com> * perf(mito2): instrument columnar sample read stages Signed-off-by: evenyag <realevenyag@gmail.com> * refactor(mito2): clarify reader timing and focus merge regression coverage Signed-off-by: evenyag <realevenyag@gmail.com> * perf(mito2): coalesce bounded sample reads across blocks Signed-off-by: evenyag <realevenyag@gmail.com> * test(mito2): cover first-field null recovery in identity merge Signed-off-by: evenyag <realevenyag@gmail.com> * fix(mito2): fall back when sample read-ahead exhausts memory Signed-off-by: evenyag <realevenyag@gmail.com> --------- Signed-off-by: evenyag <realevenyag@gmail.com> * feat(mito2): consolidate columnar samples into bounded daily generations (#10129) * perf(mito2): consolidate columnar samples into daily windows Signed-off-by: evenyag <realevenyag@gmail.com> * fix(mito2): isolate daily source coverage and version the local cache Signed-off-by: evenyag <realevenyag@gmail.com> * test(mito2): use the exported index purge channel Signed-off-by: evenyag <realevenyag@gmail.com> * test(mito2): exercise daily source groups across the merge limit Signed-off-by: evenyag <realevenyag@gmail.com> * fix(mito2): build stable daily groups from populated source windows Signed-off-by: evenyag <realevenyag@gmail.com> * fix(mito2): anchor daily coverage once per source group Signed-off-by: evenyag <realevenyag@gmail.com> * test(mito2): validate partial-day reads and grouped rebuild cleanup Signed-off-by: evenyag <realevenyag@gmail.com> * fix(mito2): skip daily sample readers outside query bounds Signed-off-by: evenyag <realevenyag@gmail.com> --------- Signed-off-by: evenyag <realevenyag@gmail.com> * feat(promql): match an empty on() and grouped cardinalities natively (#10223) The compiler already carries an empty `on()` and both grouped cardinalities, and the matcher kernel implements them; only the planner's narrow region-local allowlist refused them, so those queries fell back. Admit them: the pair is planned as one native binary over the two operands, matching above their sources. Admitting them exposed an output-schema bug the allowlist was hiding: both tag walks treated an empty `on()` as "keep every tag", so the output carried labels the kernel had dropped. They now keep only the specified labels, and a grouped match keeps the many side's labels and copies the included ones from the one side. Tests: the empty-`on()` subtraction and the `group_left()` form assert native execution with their original rows and labels, and four cases assert the kernel's cardinality errors - duplicate right under `on()` and `group_left()`, an implicit many-to-one under `on()`, and duplicate left under `group_right()`. The subquery case in the same loop keeps its fallback. Signed-off-by: discord9 <discord9@163.com> * fix(query): plan a data-free statement and a zero-match discovery natively (#10224) Three refusals kept statements off the native route in a distributed deployment, and one of them raised an Internal error rather than falling back: - `remote_native_raw_plan_supported` required a `RawSeriesSource` somewhere in the plan, so a statement that reads no table was re-planned without native execution and answered with legacy column names. The gate now also admits a `FusedSeries` chain whose leaves are all the typed empty input - followed through fused nodes only, so a scan, a raw source or any other extension still rejects - and the exact `strip_tsid_column` projection over such a chain. - A nameless selector that matches no metric produced a typed empty input, and on the distributed route that input fell through to "a prepared selector has no metric name". It now stays that input on both routes: never wrapped in a raw source, never bound to a metric. - A segmented shape such as `sort({job="api"})` did not recognize the empty child as an already-prepared explicit-frame input, so it declined. It is recognized next to a raw source now. The empty predicate checks the declared frame rather than only the `EmptyRelation` variant, so the guard's narrowness rests on a typed check. Tests cover `time()`, `vector(1)`, label rewrites including one that synthesizes `__tsid`, and `{job="api"}` / `sort` / `sum` / `absent` with no authorized metric, asserting native admission, the declared frame schema, `metric_name: None` and the actual rows and labels - including a two-datanode distributed case. Signed-off-by: discord9 <discord9@163.com> * feat(query): read a split-series table through a raw source (#10227) A table partitioned on a column that is not a tag splits one series across regions, so a bare scan inside one region holds a fragment: a window there evaluates over part of the series and a range that straddles the boundary produces no value at all. The single-selector route declined that shape in process, so the existing planner answered it. Read it through a raw source instead: `prepare_one_scan` reports whether the declared partition splits a series, that occurrence is wrapped like any other source-backed input, and the segment cut keeps the source so the prefix runs above the reconciliation. A partition on tags keeps every series whole in one region and keeps the bare-scan path. Signed-off-by: discord9 <discord9@163.com> * feat(query): match a partitioned multi-selector tree above its sources (#10226) * fix(query): match the whole operand instead of copying matchers A native binary must report what Prometheus reports: when one side holds two series under one matching label set, the query fails with "found duplicate series for the match group ... on the right hand-side". The legacy route answers a value instead, because `matching_filters::propagate` copies the matcher into the other operand's scan and the groups without a partner are dropped before the join - its own documentation says that hides the duplicate. This branch had started applying that rewrite to native operands as well, and had refused multi-selector trees over partitioned tables in process to force the hybrid topology back. Both kept a legacy behaviour the native route does not need: a native binary reads independent raw sources and checks the whole one-side groups itself. Drop the rewrite for native operands, and keep the refusal only for several scans of one partitioned table, whose node cannot run inside a region. The partitioned acceptance case now asserts the duplicate error and that the pair really runs as one native binary; the legacy value stays in the test as the contrast that documents the difference. Signed-off-by: discord9 <discord9@163.com> * test(promql): assert the native binary, not the hybrid join The hybrid cases were written when a cross-table pair over a partitioned table could only run as two region-local producers joined on the frontend. The pair now runs as one native binary over independent raw sources, so those shape assertions describe a topology that no longer exists: two fused nodes, a frontend join, direct region scans under it and a projection above it. Assert what the native plan actually promises instead: the binary is one fused node with more than one input, every operand reads its own source, and the duplicate-group and value assertions stay as they are. The per-scan-partition value check now applies to operand producers only - a matching binary routes by label set, so an input partition may contribute nothing to a given output one. Signed-off-by: discord9 <discord9@163.com> * test(promql): keep the operand metrics for a native binary The per-scan-partition value check ran over every fused node, which now means the binary itself: it routes by label set, so an output partition may legitimately hold nothing from a given input partition. Check the two operand metrics instead - the same assertion the unpartitioned pair already makes - so each operand still proves it registered its values on every partition it reads. Signed-off-by: discord9 <discord9@163.com> * feat(query): admit multi-selector trees the native binary can match Three refusals were left over from when a multi-selector tree could only run inside one region over bare scans, and each of them now only costs coverage: - The aggregate split and the segmentation were admission authority: a tree that needed placement and did not fit the single-selector split fell back, even though each selector occurrence can read its own raw source and the consumer can run above the collection. They are optimizations now. - `multi_input` meant "the metric logical ids differ", so ordinary tables, repeated selectors and mixed engines were treated as a single input. It now means what the code needs: more than one independent input, one per selector occurrence, each bound to its own source. - Several scans of one partitioned table were refused in process. That node cannot run inside a region, but the same tree has a frontend placement over per-occurrence raw sources, so only the cross-table refusal that keeps the hybrid route is left. Tests that asserted the fallback now assert the native plan and the per-source inputs; the value, label and duplicate-group assertions are unchanged, and the shapes the compiler genuinely cannot express stay refused. Signed-off-by: discord9 <discord9@163.com> * fix(query): keep the raw sources under an in-process cut A table partitioned on a column that is not a tag splits one series across regions. When such a tree is cut into segments in process, the cut could fall back to the bare scan, and then a per-region prefix evaluated a window over one region's fragments: `rate` never saw the samples the other region holds, so a window that straddles the boundary produced no value at all. Remote execution already kept the raw source under the cut; in process the flag said otherwise. Pass the boundary fact that matters - the cut must keep each source when the table is split into regions - so the evaluated segment runs the prefix above the reconciliation. An unpartitioned table's scans are whole in one region, so its cuts keep the bare-scan form and the two-aggregate pairs still plan as one execution. The regression puts the two samples of one series on either side of a time-partition boundary and compares the native answer with the existing plan's for a two- and a three-selector window; without this change both lose the value. Signed-off-by: discord9 <discord9@163.com> * test(promql): prove the window runs natively The time-boundary regression compared rows with the existing plan, which a fallback also satisfies. Require a fused node in the frontend tree as well, so the test fails if the planner quietly declines the shape. Signed-off-by: discord9 <discord9@163.com> * test(query): trim redundant gates and assertions A review of this branch found duplications rather than missing coverage: two identical metric-name gates, a `!multi_input` term that now implies its own `selector.len() == 1` partner, a duplicate aggregate-admission test, a redundant cooperative-decorator special case, a tautological region-input assertion, an exact-child-type assertion that pins an intermediate shape, and a window regression that accepted a region-side fused node as proof of frontend admission. Remove them; the behaviour assertions stay. Signed-off-by: discord9 <discord9@163.com> * test(promql): require the native binary for a partitioned pair The values equal the legacy route's, so a legacy frontend join reading the same regions could satisfy the assertions on their own. Require the fused node in the frontend tree as well, and drop a duplicated rustdoc paragraph. Signed-off-by: discord9 <discord9@163.com> * fix(query): drop the stale unconditional canonical refusal A rebase left both the old refusal (`any_partitioned && contains_canonical_merge`) and the transport-scoped one, so the older block declined every canonical merge over a partitioned table on the wire and the acceptance case fell back to the existing planner. Keep the scoped refusal only. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * perf(mito2): cache aligned sample payload blocks in the page cache (#10128) Cache aligned sample payload blocks within the shared page-cache budget with generation isolation and owned buffer accounting. Fall back to uncached block reads when aligned reservations exceed the scan budget. Validated bounded reads, cache isolation, cancellation and reservation release. * perf(mito2): retain batch-backed labels for native samples (#10134) Retain compacted metadata rows for new series candidates and materialize owned labels lazily. Preserve region-qualified identities, full physical-tag conflict checks and mixed-predicate fallback. Separate scan bindings from output labels and validate execution-time schemas. Validated cross-bucket retention and conflicts, filtered PromQL label binding, affected crate tests and strict Clippy. Sample-to-flat, merge and frame conversions remain. * perf(mito2): reuse sample decoders within a block (#10214) Reuse decoder context and output scratch during synchronous sample-block decoding, and borrow validated uncompressed payloads. Share checksum validation and release decoder scratch before boundary-read I/O waits. Preserve bounded decoding, corruption handling, timestamp-first pruning and block memory reservations. * perf(mito2): retain all-visible sample batches (#10215) Return the original sample batch when null-free sequence and timestamp bounds prove every row visible. Preserve schema validation and the mask path for partial visibility and null-containing batches. Cover snapshot, sequence and time boundaries, unordered timestamps and retained buffer identity. * feat(promql): evaluate dynamic scalar parameters natively (#10093) Evaluate scalar expressions per step in native topk/bottomk and histogram functions, including scalar(vector) parameters that read independent selectors. Reuse the existing kernels, preserve whole-vector placement and selector binding order, account for retained parameter values, and append expression wire tags without renumbering existing variants. Expression-column naming is intentionally outside this change. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): reuse canonical and segment trees (#10238) Signed-off-by: discord9 <discord9@163.com> * refactor(promql): remove duplicated planner facts (#10239) Signed-off-by: discord9 <discord9@163.com> * refactor(promql): share instant selection bounds (#10240) Signed-off-by: discord9 <discord9@163.com> * perf(mito2): reuse raw scan preparation per partition (#10241) Signed-off-by: discord9 <discord9@163.com> * refactor(promql): drop native runtime state at terminal (#10242) Signed-off-by: discord9 <discord9@163.com> * feat(promql): run subqueries through the native prefix (#10085) * feat(promql): run subqueries through the native prefix A subquery evaluates its inner expression on its own grid, so the native route now plans it instead of sending every statement containing one to the existing planner. - `collect_selectors` recurses into a subquery's expression, so its selectors are prepared like any other and each gets its scan bounds from `raw_scan_bounds`. - `calls_rate_or_increase` recurses too, so a delta-temporality table still keeps a rate or an increase inside a subquery on the existing planner. - A window whose input is already evaluated - a subquery - is admitted above the split aggregate when its declared grid equals its direct input's grid, and the suffix is rebuilt with it. A raw-sample window still cannot sit above the cut. - A `FusedSeries` whose root is such a window publishes on that window's output grid, which is not necessarily the statement's; the binding follows it at every construction site, and the split's partial state keeps the cut aggregate's own grid. An evaluated-input window does not need an owner router for its sources, and a segment whose root is one is cut on its own grid. Verified with `-p promql`, `-p query`, `tests-integration --lib` (native_promql) and clippy. Signed-off-by: discord9 <discord9@163.com> * fix(promql): keep a subquery's aggregate operand and gate an evaluated cut Two shapes reached the native route and failed while planning instead of keeping the plan they had. A hybrid-binary operand folds a group one subquery down (`avg_over_time((sum by (job, host) (pm))[5m:1m]) + on(job, host) pn`): `single_selector_operand` counts the subquery's selector, so the operand looked admissible, while `contains_aggregate_expression` stopped at `PromExpr::Subquery` and never saw the aggregate. The admission then built a `FusedSeries` the physical planner could not convert (`No installed planner was able to convert the custom node to an execution plan ...`). The walk descends into a subquery again, and both statements keep the existing plan with the same rows. A window over an evaluated input was cut as a segment regardless of locality. For any such window whose input is not a bare `Input` - a raw window, a `label_replace` - `reads_across_series` is true and `pointwise_operand` false, so the `FusedSeries` arm of the dist analyzer answers `NonCommutative`: the segment stays above the `MergeScan` and nothing converts it (`sum by (job) (max_over_time(rate(pm[2m])[5m:1m]))` and two neighbours failed that way at instant and range evaluation). `region_local_segment` mirrors that arm's decision from the tree, and `cut_as_segment` declines an evaluated-input window it refuses, cutting the per-series prefix inside the window instead; where there is none, the statement keeps the plan it had. The analyzer is not extended: every range function but `last_over_time`/`first_over_time` drops the metric name, so such a window is genuinely cross-series and a region-local segment would publish two series under one label set. `build_evaluated_segment` stays, and so does its caller. The canonical pass inserts a merge under a label rewrite, and that boundary is only ever built from the frames the loop has published, so the cut needs the fallback; deleting it took every rewritten fold off the route, which is what `sum(label_replace(...))` and the histogram-over-rewrite boundary caught. The frames-bound segment itself is not the defect - the gate is: the evaluated-input window is declined before any cut is taken, so the fallback only sees cuts the analyzer places. Tests that asserted the older rejection are decided on evidence: `sum_over_time(rate(m[5m])[1h:])` is admitted now and its rows match the existing plan, so the plan-level test asserts the route and the integration case proves the rows; the aggregate-subquery case in `native_aggregate_complete_over_four_nonempty_mito_partitions` is answered by the split (one `Partial` producer, one frontend merge) and returns the same 15.0 on both routes, so it moved to the split cases rather than being dropped. Two fold-over-subquery shapes in `native_promql_subqueries_match_legacy` (`sum by (job) (max_over_time(rate(...)[5m:1m]))` and the fold above it) are answered natively for the same reason - the fallback builds their cut - and their rows equal the existing plan's, so they assert the route instead of the fallback; the rewrite shape still declines, because its window reads across series and there is no per-series prefix inside it to take instead. Signed-off-by: discord9 <discord9@163.com> * test(promql): assert anchored subqueries do not re-fold across steps Signed-off-by: discord9 <discord9@163.com> * test(promql): drop the unused fallback-case helper Signed-off-by: discord9 <discord9@163.com> * test(promql): update subquery expectations for the native route Signed-off-by: discord9 <discord9@163.com> * fix(promql): publish subquery segments on their declared grid Signed-off-by: discord9 <discord9@163.com> * refactor(promql): drop unreachable segment arms and pin subquery routes Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * feat(promql): answer subquery matrix roots natively (#10095) * feat(promql): answer a subquery matrix root on the native route A matrix-valued instant query whose root is a subquery (`m[5m:1m]`, with `offset` and `@`) now runs natively: the planner recognises the root, derives the subquery's child grid and compiles the inner expression on it, while the statement's own bounds stay where `@ start()` and `@ end()` resolve against them. No window node is added - the tree is the inner expression, and its rows carry the evaluation timestamps, which is what the HTTP encoder reports as `resultType: matrix`. A raw range selector still declines (raw samples have no evaluation grid to bind), and a range query with a matrix root declines so the existing planner's "range vector" rejection is what the caller sees. A pre-epoch statement (`TQL EVAL ('-60', '-60', '1m')`) rebuilds its bounds on the side of the epoch their sign names, and a statement whose grid cannot be built keeps the existing plan. `compile_statement_with_grid` / `compile_selectors_with_grid` take the grid explicitly; the grid is still derived lazily inside the compiler so an unsupported statement reports the reason it has no tree, not a grid complaint. Verified with `-p query`, `-p promql`, `tests-integration --lib` (the matrix-root cases) and the file-backed HTTP route test. Signed-off-by: discord9 <discord9@163.com> * fix(promql): bind the subquery's child grid on every lowering path `compile_statement_with_grid` compiled a subquery root's tree on the child grid and the plain `FusedSeries::try_new` path bound that grid, but the split-aggregate, canonical-boundary and segment paths still received the statement grid. A matrix root whose inner expression folds as an aggregate therefore built a final node publishing on the statement grid over a tree compiled on the child grid, and `plan_split_aggregate` refused it with `FusedSeries output is not on the statement grid`: `sum(pm)[5m:1m]`, `(sum by (job) (rate(pm[2m])))[5m:1m]`, its `@ 600` form and its `* 2` form all failed instead of answering the five child-grid points. The effective grid is now chosen once, right after the statement grid is known - the child grid for a subquery root, the statement grid otherwise - and every lowering path binds that one. The per-site `unwrap_or_else` workarounds and `fused_publish_grid` go away with it: they were compensating for two halves that disagreed, and the binding is now the grid the tree was compiled on. The child-grid derivation itself is back in one function: `subquery_child_grid` in `native_compile.rs` feeds both the compiler (which binds it into the window node) and the planner (which binds the rows to it), using the checked `range_millis`/`millis` conversions and the compiler's zero-step refusal. The planner's inline copy used unchecked `as i64` and its own default step, so an overflowing range wrapped there while the compiler reported it. `native_promql_matrix_root_matches_legacy` covers the four shapes, asserting both the route and the child-grid timestamps for the split case. Signed-off-by: discord9 <discord9@163.com> * test(promql): cover pre-epoch non-matrix routing Signed-off-by: discord9 <discord9@163.com> * fix(promql): publish recursive segments on their declared grid Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * fix(promql): plan a zero-match selector beside another input (#10243) * fix(promql): plan a zero-match selector beside another input Signed-off-by: discord9 <discord9@163.com> * test(promql): cover a zero-match selector beside a real one in distributed Signed-off-by: discord9 <discord9@163.com> * fix(promql): bind a zero-match selector to its set-operand layout Signed-off-by: discord9 <discord9@163.com> * fix(promql): propagate empty set layouts through nested operands Signed-off-by: discord9 <discord9@163.com> * test(promql): cover empty operands beside arithmetic and non-bare sets Signed-off-by: discord9 <discord9@163.com> * fix(promql): record set layouts through subqueries Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * fix(promql): replace the residual hybrid binaries and drop the 64-level depth cap (#10254) Remove the last hybrid composition: when a native statement is refused as a whole but each side is independently admissible, the sides are now compiled through the same native frame preparation, compiler and placement path the whole-native route uses and meet at a shallow native Binary, instead of the native operands being composed by the legacy planner's Join. Admission is decided by the composition itself - equal grids, the binder's matching/cardinality/fill support, the output column's kind, and the runtime execution state - rather than by a predicate that copied what the removed hybrid route used to accept. The artificial 64-level native depth cap is gone on both the construction and the decoding side, with no replacement cap, no depth-based splitting and no product stack configuration; the recursive walks that actually need it grow their stack on demand (validate, runtime build, runtime pull). Histogram binary results are preserved and their diagnostics aligned with Prometheus: an unsupported pairing synthesized by a fill now reports the same Info, and an incompatible bucket layout reports only the schema Warning instead of also reporting a reset-hint contradiction. The legacy histogram UDFs follow the same order so the two routes stay in step. Signed-off-by: discord9 <discord9@163.com> * feat(promql): compile absent_over_time on the native route (#10198) `absent_over_time(<selector>[<range>])` had no native kernel, so the statement declined to the existing planner; there a nameless selector has no metric name to look up, and the query failed instead of answering the label set Prometheus reports. Bind it to the whole-vector operation `absent` already uses, over a window read of the selector's range. The fold keeps the metric name: a presence fold that drops it would fold two metrics which differ only by name into one series and refuse the second value, while `absent` only asks whether the window held anything at all. Labels come from the same derivation `absent` uses, now also accepting a matrix selector. A statement holding a nameless selector that discovery expands into more than one metric table keeps declining: keeping the name would declare `__name__` without a bound metric name, and dropping it would merge the metrics. The typed empty input of a selector that matched no metric table is excluded from that decline, so a zero-match query plans natively and answers from it. The legacy planner is untouched. Signed-off-by: discord9 <discord9@163.com> * feat(query): run an embedded TQL statement on the native PromQL route (#10288) * refactor(query): enforce column requirements per node, not per plan root Signed-off-by: discord9 <discord9@163.com> * feat(query): run an embedded TQL statement on the native route Signed-off-by: discord9 <discord9@163.com> * fix(query): publish one CTE schema on both TQL routes Signed-off-by: discord9 <discord9@163.com> * fix(query): publish the CTE schema durably, including its metadata Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * test(promql): cover discovery under an aggregate on the standalone route (#10255) Signed-off-by: discord9 <discord9@163.com> * refactor(series): hold the frame layout marker at one value The series frame is the native PromQL path's internal transport, and that path lives on `narthex`, an experimental branch: it may break its own formats between revisions, so a frame revision number has no compatibility promise to protect. The marker becomes movable once the path reaches a release branch, where a producer and a consumer can legitimately be different revisions. The reader still refuses a frame that declares any other value, which turns a mismatched producer into an error instead of values decoded under the wrong columns. The two wire fixtures are renamed to `v1_metadata.json`, and the drift cases now use the value that is not current. * feat(promql): let `count` and `group` fold native histograms `count` and `group` fold native histogram samples in the native aggregate: neither reads a value, so a histogram is one present sample. `min`, `max`, `stddev` and `stdvar` read a float and now skip histogram samples the way Prometheus does, reporting what they skipped; `sum` and `avg` keep the existing plan until #10199. A group holding only histograms answers nothing under a skipping aggregate, and `quantile` keeps the existing plan because this runtime has no kernel for it. * test(promql): qualify native corpus partition parallelism (#10181) Use disjoint flushed ranges in the corpus and real-query fixtures, then assert every real SeriesScanExec reads on all four partitions. Assert every region-side fused prefix and partial aggregate publishes on all four partitions; require width-four frontend-raw nodes to do the same while leaving final combine nodes reported only. Signed-off-by: discord9 <discord9@163.com> * perf(promql): reduce repeated work in the native query path (#10212) * perf(promql): elide redundant tracking after name-dropping windows Signed-off-by: discord9 <discord9@163.com> * perf(promql): fold aggregate samples without staging buffers Signed-off-by: discord9 <discord9@163.com> * perf(promql): reuse clean labels when dropping metric names Signed-off-by: discord9 <discord9@163.com> * test(promql): cover literal arithmetic over histogram windows Signed-off-by: discord9 <discord9@163.com> * test(promql): resolve all-target clippy lints Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * feat(promql): support metric-name rewrites in native labels (#10176) Signed-off-by: discord9 <discord9@163.com> * refactor: remove dead native analysis and duplicate columnar filtering (#10314) * refactor(promql): drop unused native build sharing flag Signed-off-by: discord9 <discord9@163.com> * fix(promql): keep retained-label windows unified Signed-off-by: discord9 <discord9@163.com> * perf(mito2): skip exact columnar residual filters Signed-off-by: discord9 <discord9@163.com> * test(promql): pin nested window placement Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * fix(mito2): identify series by their exact sparse key, not by tsid (#10330) * fix(mito2): identify series by their exact sparse key, not by tsid `(table_id, tsid)` is a hash, so distinct series can share it: 2000 label values already produce 20 colliding pairs and a 36k-series SST keeps only 27856 distinct tsids. The columnar series writer silently merged such pairs (first label set kept, row counts added) and the samples writer either reported time_regressed or, with increasing timestamps, attached one series' samples to another's labels. Every one of those decisions used the hash as identity. The SST already stores the exact sparse primary key, so nothing on the storage side changes: `tsid`, the hash function and the key layout are untouched. What changes is that the artifacts, their readers and the prepared read path now decide identity on the canonical sparse key bytes, and `(table_id, tsid)` is only the coarse, order-preserving prefix used for ordering, pruning and bucketing. - `ExactSeriesKey` is the new identity type: equality, ordering and hashing are defined on the key bytes alone, `id()` is documented as a derived prefix that is never compared. `from_sparse_key` validates the entire key, not just the prefix, and bounds it at 8 KiB. - Series format v2 keeps one row per exact key; the descriptor layout and the existing per-row tags already express this once the writer aggregates by key. Coarse page/block/part bounds may now touch, so readers prune conservatively. - Samples format v2 records the owning key per block, appends a `__primary_key` column, and lets a key change restart timestamps and ordinals while the same key keeps the monotonic rules. Exact selection travels in `SamplesReadRequest.keys` next to the coarse `ids`. - The prepared path keys candidates by `ExactSeriesKey`, re-encodes identity from the artifact's own tags, associates samples through `__primary_key`, and uses the exact key for the merge key and the direct-label path. - Both manifests carry a mandatory `identity_encoding` discriminator. Artifacts without it are structurally valid and perfectly ordered yet may contain an already merged row, so they are rejected and rebuilt from SSTs; maintenance rotates them out instead of reusing them, and the local cache version drops the rebuildable store. There is no dual reader and no migration. Verified: `cargo nextest run -p mito2` 1824 passed, 2 skipped (three times, identical); `cargo nextest run -p mito-codec` 57 passed; clippy `-D warnings` and rustfmt clean. Tests pin the real captured pair (`host-020078` / `host-029838`, both retained with their own labels), the silent-loss variant with globally increasing timestamps, sixteen distinct keys under one forced tsid in both PK encodings, candidate isolation end to end, and rejection of a frozen v1 fixture. Signed-off-by: discord9 <discord9@163.com> * fix(mito2): close the exact-identity review findings - The two-phase series scan assumed candidate discovery had already enforced tag predicates through an "exact assigned-series set". An assignment is a `(table_id, tsid)` hash, so a colliding partner shares it without matching the predicate, and skipping tag filters returned its rows. Apply the predicates the precise filter already holds instead of assuming them. - `MAX_KEY_BYTES` was checked when reconstructing a key from an artifact but not when a writer built one, so the writer could publish a key the reader refused; a single 8 KiB label already encodes past the cap. Length is bounded by the containers that store a key (one key per block, descriptor, page and column limits), and long labels are real data, so drop the cap instead of enforcing it on both sides. - The `__primary_key` column copies one key per output row, but the read reservation budgeted a fixed 32 bytes per column and metadata pages did not count the per-block key heap: 8192 rows of 8 KiB keys allocated ~64 MiB against a ~1 MiB estimate. Budget the real bytes and count the heap. - `SamplesReadRequest` carried both `keys` and `ids`, which the only caller derived from the same candidates; selection is key-only now and the reader derives its coarse pruning set, so the two selectors cannot disagree. - Restore the unknown-field-codec projection coverage that the frozen v1 fixture used to carry, against a generated v2 artifact, and assert v1 rejection through the compatibility property rather than a field name. - Drop the redundant columnar increasing-timestamp test (that writer never reads timestamps) and correct the samples format doc: the five internal columns and `__primary_key` are non-null, value fields stay nullable. Verified: `cargo nextest run -p mito2` 1830 passed, 2 skipped; clippy `-D warnings` and rustfmt clean. Each new test was run with its fix reverted first: the two-phase test returned the colliding partner's rows, the long-key tests failed with "series key exceeds 8192 bytes", and the 4 MiB pool test read 8192 rows of 1 KiB keys while allocating ~8 MiB. Signed-off-by: discord9 <discord9@163.com> * fix(mito2): budget the exact key column exactly and drop stale skip-tag claims The read reservation charged the exact-key column its logical bytes, but the column was built with `BinaryArray::from_iter_values`, whose value buffer grows geometrically: measured on a 2135-row fixture it retained 4,989,432 bytes for 2,551,325 bytes of key data (1.956x), so a pool sized from the reservation was oversubscribed. `BinaryArray::new_repeated` allocates the offsets once and reserves the key bytes once (1.003x on the same fixture), which is what the reservation already assumes. The new test asserts the pool covers what the yielded batch retains, that the column keeps no growth slack, and that the reservation is released on drop; 2135 rows sits just past a doubling step so a growing builder fails it. Also make the documentation match the mechanism and the encoder: - `read/pruner.rs` still told readers the data phase skips tag filtering because candidate discovery had applied the predicates exactly once. Assignments are `(table_id, tsid)` hashes, so a colliding partner shares the identifier without matching the predicate, and the data phase does apply tag predicates. - The initial-window fallback test anchored on `reads[5]`, which is the second payload window; it now identifies the initial window by its payload-header offset and asserts the window spans 128 blocks, so a traversal change cannot quietly point it at another read. - The samples format descriptor sizes are recomputed from the encoder (`48 + 44 * C + key_length`): `224`, `45,236`, and with the leaf prefix `45,248`, each `+ key_length`; the page-fit sentence now states the enforced constraint (descriptor and leaf prefix inside one page) instead of claiming the widest descriptor always fits. Verified: `cargo nextest run -p mito2` 1831 passed, 2 skipped; clippy `-D warnings` and rustfmt clean. Signed-off-by: discord9 <discord9@163.com> * fix(mito2): budget each projected sample array's fixed overhead The read reservation charged per-row bytes plus the payload sizes, but not the fixed cost of the arrays it builds: Arrow's `get_array_memory_size` is the array struct plus its buffer capacities, so a one-row, key-only projection reserved `key_length + 72` for the 1344 bytes it actually retained. Add `ARRAY_HEADER_BYTES` (`size_of::<BinaryArray>()`) plus `BUFFER_ALIGNMENT` per projected column, as their own term, and cover the one-row key-only case in `exact_key_output_reservation_covers_the_retained_array`: the pool must cover the batch's reported memory size while it is alive and be empty after drop. Verified: `cargo nextest run -p mito2` 1831 passed, 2 skipped; clippy `-D warnings` and rustfmt clean. Signed-off-by: discord9 <discord9@163.com> * chore(mito2): pin the series-index cache token to 1 The branch inherits `2` from an earlier layout change. This branch is experimental and has no deployments, so it pins the token at `1`: whatever a different build wrote into the rebuildable local store is discarded on startup and rebuilt from the SSTs. The token is not the compatibility boundary on its own. Artifacts are rejected on open when they lack the mandatory `identity_encoding` discriminator, so a hash-only artifact is never served even when the reset has not run yet (a read that precedes it, or an artifact outside this cache). Signed-off-by: discord9 <discord9@163.com> * feat(mito2): keep exact series keys out of the samples descriptor page A block descriptor used to embed the whole exact key, and a descriptor shares a 65,536-byte metadata page with its leaf prefix. That put an accidental ceiling on key length: 65,344 bytes with no value fields, 65,300 with one, and 20,288 with 1,024 fields. Valid SST primary keys can be longer, so their samples builds failed with "sample descriptor exceeds leaf limit" even though the key itself was accepted. The key now lives in a per-part key area and the descriptor carries a reference: part = [header 16][column payloads][key area][leaf pages / root][trailer 120] trailer = root ref 72 | depth 4 | version 4 | file_length 8 | payload_end 8 | key_end 8 (body 104), magic 8, XXH64 over the preceding 112 bytes 8 block = table_id u32 | tsid u64 | key offset u64 | key length u32 | min i64 | max i64 | ordinal u64 | rows u32 | columns u32 (56 bytes, fixed) Descriptor size no longer depends on key length (232 bytes for one present field, 45,244 for 1,024), so the ceilings are gone. Keys are written once per series, back to back in block order and without padding, so an equal reference means "same exact series" and a key may exceed any page limit; a reference is validated against `key_end` with a checked add, an overlap between consecutive new series is rejected, and a resolved key whose `(table_id, tsid)` disagrees with its descriptor is refused before any row is emitted. Reads stay lazy: only blocks that survive coarse selection fetch a key, at most once per series, merged per page and bounded by `MAX_READ_BYTES`, reused from the part's aligned cache when one is configured. Measured on the 260-series fixture: 260 per-series key reads before, 1 merged read after, with the fetched byte count asserted to be exactly the key bytes. The read contract also stops materializing one key per row: `SamplesBatch { batch, key }` carries the block's key once, the exact-key column is refused in projections, and `prepared` attributes a whole batch with a single binary search. For an 8,192-row block with a 1 KiB key the reservation drops from 8,388,608 B to 1,120 B (~7,490x), which is what lets a 4 MiB pool read it; `Candidate` keeps one owned copy of the key instead of two, and `retained_rows` is gone in favour of the `prepare_retained_rows` metric. No version bump: the layout changes in place. Artifacts written by an earlier shape of this branch (112-byte trailer, or keys inside descriptors) are unreadable and must be rebuilt, which the cache token and the required `identity_encoding` discriminator already bring about. One user-visible cost: keys retained by a scan are charged to its memory pool, so a scan that used to fit in 16 KiB needs ~14 KB more for 128 series. The fixture that pinned 16 KiB is raised to 64 KiB with the arithmetic in a comment. Verified: `cargo test -p mito2 --lib -- series_index::samples series_scan::prepared series_index::identity` 72 passed / 0 failed / 2 ignored on six consecutive runs; clippy `-D warnings` and rustfmt clean. Failing-before evidence, both produced by reverting the relevant hunk: - restoring `48 + 44 * C + key_len` fails the >64 KiB round-trip with "sample descriptor exceeds leaf limit"; - the 260-series coalescing assertion fails against per-series reads. Signed-off-by: discord9 <discord9@163.com> * test(mito2): assert the duplicate-drop on the preparation's own rows `overlapping_buckets_retain_only_new_label_rows` compared the deltas of two process-global counters (`prepare_unique_series`, `prepare_retained_rows`). Every preparation in the process increments both, so under parallel test execution the two deltas do not pair up and the test failed intermittently - it always passed in isolation. Assert the property the pairing was standing in for, on this preparation's own rows: read every series of the prepared source and require that no series keeps two rows at the same timestamp. A surviving duplicate from the overlapping bucket would repeat a timestamp; the counters can no longer make the result depend on what else the test binary happens to be running. Signed-off-by: discord9 <discord9@163.com> * test(mito2): drop the now-unused preparation counter helper The duplicate-drop assertion no longer reads the process-global counters, so `prepared_count` has no callers and `-D warnings` rejects the dead function in `cargo check` and `cargo clippy` CI. Remove it. Signed-off-by: discord9 <discord9@163.com> * fix(mito2): scope key references per part, bound key reads, checksum keys Review of the key-area change found three defects that ordinary data can hit and three smaller ones. All six are fixed with a test that fails beforehand. Key references are file-local offsets, and the ordering state kept them across parts: a series continuing into another part looked like an overlapping reference (or a new series with a non-zero ordinal), and two different series in structurally identical parts could be mistaken for one because their offsets and lengths coincided. Reference ordering is now scoped to a part; continuation across a boundary is decided on the exact key bytes plus time/ordinal, and the loaded keys must be nondecreasing, a repeated key must continue its series. A key prefetch fetched the whole window and only then grew the reservation, and the per-key fallback charged after its read, so a small pool could read and assemble a page of long keys before reporting exhaustion. The peak (fetched entry + owned key + alignment/assembly) is now reserved before the read; on refusal the reader drops alignment first, then halves the window, and a returned key is cloned only after the block reserved for it. A test asserts that an unpayable prefetch issues no key read at all. The key area had no integrity check, so flipping one byte of a label - leaving framing and `(table_id, tsid)` intact - could silently change which series a block belongs to. Every stored key now carries its XXH64, excluded from the reference length and verified when fetched. The manifest discriminator now names the layout (`exact-sparse-key-area` for samples; the series index keeps `exact-sparse-key`), so an artifact with inline keys is refused at open instead of being marked reusable by maintenance and failing later at a part read. No version number changes anywhere. A key copy is also counted correctly when a series spans a metadata rollover: the "first copy of this part" decision is taken after the rollover, not before. Verified: `series_index::samples` 51 passed, `series_scan::prepared` 27 passed, `series_index::identity` 4 passed; clippy `-D warnings` and rustfmt clean. Signed-off-by: discord9 <discord9@163.com> * fix(mito2): charge the key peak on every path and chain the order check Four follow-ups from the confirmation review of the key-area change. The per-key fallback reserved only the retained and stored bytes and then fetched through the aligned payload cache, whose aligned/assembly peak it never charged, so a leftover key could read a whole aligned block the pool had not paid for. It now reserves the same peak as the page prefetch: aligned budget first, and on refusal an explicitly cache-less reader with the unaligned peak, before any read. The key carried across a part boundary was a deep copy taken while the per-part reservation was being released, so it lived on uncharged until the next part. It is now held by its own pool consumer, charged before the copy and released when it is replaced or when the boundary falls behind the scan. The content-order check was chained over the last *traversed* block, which an unloaded block overwrote, so a block pruned by selection could hide reversed or restarted keys - and could drop the boundary key entirely when a part ended on a pruned block. It is now chained over the last *loaded* key, with adjacency taken from "the last traversed block is the last loaded one", so a gap still requires increasing order and a strictly greater ordinal but not exactly +1. Finally, the key-area read counter in the tests only saw reads fully inside the key area, missing aligned reads that start before it; it now counts reads that intersect, and the cached-read test is documented as a cache-behaviour guard rather than a failing-before proof for the budget work. Verified: `series_index::samples` 55 passed, `series_scan::prepared` 27 passed, `series_index::identity` 4 passed; clippy `-D warnings` and rustfmt clean. Failing-before evidence: a 16384-byte key-area read issued with only 15080 reserved; a boundary key with 0 of its 1195 bytes charged; reversed keys hidden behind a skipped block; and a pruned block hiding the boundary key. Signed-off-by: discord9 <discord9@163.com> * docs(mito2): qualify the ordinal rule to one exact key The samples format said a loaded key's ordinal must still grow, which reads as if ordinals had to increase across different series too. They do not: only an equal exact key continues, an adjacent block of that key requires +1, and a different key starts at zero. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * fix(mito2): keep main's cache split unless the columnar format is enabled The merge reserved a quarter of `sst_meta_cache_size` for the native-reader metadata tier unconditionally, which silently halved main's decoded tier (1/2 -> 1/4) on every deployment, including the ones that never enable the columnar series-index format. Gate that reservation on `experimental_series_index_format == Columnar`, so the default split stays exactly what main ships and `columnar_meta_cache` is not even constructed. Tests that exercise the columnar tier now opt in explicitly. Signed-off-by: discord9 <discord9@163.com> * fix(mito2): drop a needless borrow in series-index reconciliation `files` is already a reference after the merge, so `&files` tripped clippy::needless_borrow and failed CI's `-D warnings`. Signed-off-by: discord9 <discord9@163.com> * fix(mito-codec): register reserved columns in the sparse label fixtures main enforces (`fix!: enforce sparse primary key schema`) that a sparse primary key starts with `(__table_id, __tsid)` and that every primary-key column id exists in the schema. The label-decoding fixtures built a sparse key that satisfied neither, so `RegionMetadataBuilder::build()` panicked and the workspace `test` job failed. Same adaptation as the promql source fixtures. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): read binary operators' kernel operations from one table (#10393) * refactor(promql): drop the Go math port and use the standard library The pointwise kernels called a port of Go's `math` functions as they behave on amd64. Results are compared within a tolerance, and the port reproduced amd64 assembly quirks that Go on arm64 does not share. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * refactor(promql): read every binary operator's kernel operation from one table `BinaryOp::kind` maps an operator to the arithmetic or comparison the map kernels implement. The vector-vector kernel, the scalar broadcast and the compiler's constant fold use it instead of their own tables and their own copies of the arithmetic. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * refactor(promql): use BinaryOp::is_comparison for the hand-written comparison checks Signed-off-by: Dennis Zhuang <killme2008@gmail.com> --------- Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): info() ACL targets, per-series routing, reset collisions (#10385) * fix(sql): include the table PromQL info() reads in extracted tables `info(v)` and `info(v, {label="x"})` read `target_info`, which is not written as a selector in the expression, so table-level permission targets missed it. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): keep per-series provenance behind CooperativeExec `EnsureCooperative` wraps the `SeriesScanExec` leaf and rebuilds `RawSeriesSourceExec` over the wrapper, which cleared `per_series_scan` and left `SeriesFrameOutput` on the merge-inputs router. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(promql): drop histogram pairs whose counter reset hints contradict Prometheus returns the collision as the error of `+`/`-` and skips the pair before counting its match. The native path kept the sample and counted it. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(frontend): keep info() label selectors out of metric discovery The table extractor now claims `target_info` only for the selector shape the planner reads it with; other shapes stay unresolved. `MetricDiscoveryVisitor` skips the label selector of `info`, which the planner never discovers with, so an unresolved one is no longer answered by the caller's discoverable tables. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * test(promql): expect the dropped pair on a histogram reset hint collision Signed-off-by: Dennis Zhuang <killme2008@gmail.com> --------- Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * refactor(promql): share NativeNode child traversal across crates (#10391) Move `node_children` and `with_node_children` from the query planner to `NativeExpr::children` and `NativeExpr::with_children`, add `NativeNode::{any, inputs, is_data_free}`, and replace the hand-written structural walks over `NativeExpr` with them. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * refactor(promql): derive native output columns and run-time labels with one walk (#10394) The planner's `native_output_tags` was a copy of `fused_output_tags` in `promql::native::plan` read over tag columns instead of run-time label sets. The planner calls `fused_output_tags`, which takes over the one rule the copy had on its own: `or` reports the metric name of each row. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * refactor(promql): drop the Go math port and use the standard library (#10392) * refactor(promql): drop the Go math port and use the standard library The pointwise kernels called a port of Go's `math` functions as they behave on amd64. Results are compared within a tolerance, and the port reproduced amd64 assembly quirks that Go on arm64 does not share. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * docs(promql): stop claiming map_arithmetic follows Go's math functions Signed-off-by: Dennis Zhuang <killme2008@gmail.com> --------- Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * perf(mito-codec): index sparse label columns by id once at bind time (#10419) The sparse-label audit walked the physical primary key once per label: column() and position() were linear scans over the described columns, so decoding a key with N labels over M primary-key columns cost O(N·M) per chunk. Build an id→encoding-position index once in bind() and use it for both lookups, so the cost drops to O(N). The full structural audit still runs before any value is read, and error text and error-selection order are unchanged. Signed-off-by: discord9 <discord9@163.com> * fix(promql): declare the per-row name column on every native rows answer A native rows result now always declares `__name__`, a nullable `Utf8` column appended after the labels and marked as the metric name, and each row carries the name of the series it was read from. A row whose value is computed rather than read (a math kernel, a folding aggregate, a range function) carries null there. The name is part of what the statement answers, not of how a response is displayed: the walk that derives a node's tags reports the name of a discovered source and of an `or` unconditionally, so `sum by (__name__)` groups on the identity of the series rather than collapsing every series into one group. `with_row_name_column` appends the column when the root is a samples root. It moves an existing name column to the end rather than adding a second one, because neither a set operation nor a discovered source leaves an existing name last: `or` keeps the order of the left operand and appends only the tags of the right side it has not seen, while a discovered source sorts its tags, which puts the name wherever its bytes fall. Every rows layout then answers one order - the time index, the value fields, the labels, then the name. A `label_replace(..., "__name__", ...)` result is therefore not declared twice, and a scalar root keeps its two columns. The collision check runs after the column is appended, so a name that would clash with a label still declines the layout, and a split's terminal schema declares the column while the frame producers keep the tags they were compiled with. A CTE answered natively publishes the column too, after the existing planner's columns and only when the answer declares one: `publish_legacy_schema` keeps the metadata-carrying name column out of its type-based pairing, so a label cannot claim it, and validates the published schema column by column. `abs(a or b)` now reports no `__name__` at all instead of the statement's name on every row: the operator drops the name, so the declared column's values are null. Shapes that keep the name (plain selection, filtering comparison, `or`, `topk`/`sort`, `first/last_over_time`, `sum by (__name__)`) carry it per row. Signed-off-by: discord9 <discord9@163.com> * chore(deps): point greptime-proto back at the upstream repository (#10370) `narthex` was pinned to a personal fork branch because the `RawSeriesSource` Substrait extension only existed there: GreptimeTeam/greptime-proto#355 was still open when the back-merge landed. #355 has since been merged as `5fc04135b`, so point the pin at that upstream commit. The generated bindings are identical; only the source repository changes, and the branch no longer depends on a fork staying available. `Cargo.lock` regenerated with `cargo metadata`. Verified: `cargo check --workspace` passes. Signed-off-by: discord9 <discord9@163.com> * perf(promql): avoid two copies on the native binary path (#10338) * perf(promql): write single-field binary results in one pass A merge of one field has nothing to merge: the field filed every label set's rows already sorted by point, and already refused two rows at one point. Writing them straight into the builder drops the per-label-set map, the triples table, its sort and the row buffer that the multi-field merge needs, and bills the same columns that merge bills. Profile shares of one 20s workload on a six-thousand-series shape put merge_fields at 6.1% of samples before and 3.2% after; the query result is byte-identical. Wall-clock comparison on that host could not resolve the difference: session drift there is larger than the effect. Signed-off-by: discord9 <discord9@163.com> * perf(promql): move a chunk into its only runtime input One source feeding one runtime input is the common shape, and the chunk was cloned for it and then dropped. Moving it removes that copy. Several inputs still clone per input, and a producer state batch stays refused either way: it carries the charge for its whole payload and belongs to a different handover. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * test(query): pin the native PromQL option default to off (#10371) The native route is gated by two halves: the engine-wide `experimental_native_promql` option and the per-request `query.experimental_native_promql` extension. The extension half is already covered by the `native_promql_requested_*` tests in `query::promql::planner::native`, and the plan-shape no-op is covered by `SeriesFrameOutput`'s `a_plan_without_a_frame_declaration_is_untouched`. What was not pinned is the default of the engine option itself. Flipping that `false` to `true` would expose the native route to every query that sets the extension, and no test would fail. Assert it directly so the opt-in property cannot regress silently. Signed-off-by: discord9 <discord9@163.com> * perf(promql): route proven per-series chains disjointly and forward explicit child frames (#10413) perf(promql): keep per-series provenance and let identity-preserving windows route disjointly RawSeriesSourceExec re-derived per_series_scan by downcasting the child directly, so EnsureCooperative's wrapper silently dropped the proof and the root router fell back to MergeInputs. Peel CooperativeExec before identifying SeriesScanExec, and check the child's bound tags cover the primary key the same way a bare scan is checked. FusedSeriesExec now accepts a RawSeriesSourceExec input, and a Window whose function keeps the metric name and whose child preserves partition identity, so last_over_time/first_over_time over a per-series scan can publish each identity as soon as its chain closes. When the child already publishes this node's frame, RawSeriesSourceExec forwards it 1:1 instead of unpack→relabel→pack. Signed-off-by: discord9 <discord9@163.com> * fix(promql): ignore histogram samples in scalar() and share one kernel (#10426) * fix(promql): ignore histogram samples in scalar() and share one kernel Prometheus' funcScalar counts only float samples: histogram samples are ignored silently, so they neither supply a value nor make a point hold more than one element. The select kernel rejected them instead, while the native ScalarOf runtime kept its own inline loop that ignored them, so scalar(histogram_metric) answered differently depending on the route. The kernel now ignores histogram samples and the runtime calls it, deleting the inline copy. A point with one float answers that float, with only histograms or with several floats answers the Go NaN. The kernel builds its answer as one dense run over the grid: a Vec<f64> seeded with the Go NaN, where the first float at a point writes its value and a second writes NaN back, plus the counts the caller already reserves. That buffer becomes the result's Float64Array by move, so the per-point peak is the 8 bytes of values plus 1 of counts the reservation already counts. Staging through Vec<Option<f64>> and GridSeriesBuilder would have held about 33 bytes per point against a 9-byte reservation. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): resolve the evaluation grid once in scalar() grid_points already resolves and validates the grid, so asking for the schema's grid again right after could only disagree with what was just checked. Signed-off-by: discord9 <discord9@163.com> * chore(compat): move the from-versions window to v1.3.1 v1.3.1 has published release assets, so the compatibility check asks for it and fails on every branch that still names v1.3.0. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * refactor: drop two helpers whose production callers are gone (#10427) * refactor(promql): drop the uncalled merge_exact_series helper The helper carried #[allow(dead_code)] and a comment saying the fused series stack would call it once that caller landed on this branch. No caller exists: no references in src, tests-integration, tests-fuzz or tests, and no re-export. Its only dependency, merge_same_labelset, is a shared grid_series helper with other users and stays. Signed-off-by: discord9 <discord9@163.com> * refactor(datatypes): drop StateFrameRow::to_state_series Only three promql tests called it, and each asserted frame content -- labels, grid length, per-point state values and presence -- after the conversion rather than the conversion itself. They now read the row's fields() and time axis directly, so the unpack and declaration-validation path is still exercised and no assertion was dropped. The private state_float helper went with it; StateSeries itself stays, still produced by pack_aggregate_state and the repartition fixtures. Signed-off-by: discord9 <discord9@163.com> * chore(compat): move the from-versions window to v1.3.1 v1.3.1 has published release assets, so the compatibility check asks for it and fails on every branch that still names v1.3.0. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * refactor(promql): drop four kernel entries the native runtime replaced (#10429) * refactor(promql): drop four kernel entries the native runtime replaced Each of these had no caller left outside tests, and each has a live replacement the runtime already uses. The golden cases move to that replacement rather than being deleted with the entry, so the inputs they covered are still covered. FloatMerger::merge/merge_budgeted/merge_series -> merge_row, which reads a StateRow instead of a PartialAggregate. The merge tests now encode partials into rows and drive merge_row, keeping the fold order, the shuffled partition orders, the NaN and signed-zero cases and the admission-before-insert assertions. One test goes away: the spare capacity of an input partial's nested Vec has no counterpart in a row, whose memory is charged as Arrow buffers by the caller and whose shared parent buffer the router charges separately. map_negate -> -1 * x, which is how the compiler already lowers unary minus. NaN bit patterns differ between negation and multiplication (the sign and quiet bits are not specified by IEEE), so the assertion is that the answer is still a NaN carrying its payload; staleness is decided at instant evaluation and a Map's holes are validity bits, so neither reads that payload. Prometheus's neg cases pass unchanged. map_compare -> map_compare_literal/map_compare_series, which is what production already called. The histogram cases now assert that kernel's existing drop-and-report behaviour instead of the dead entry's whole-query error; production behaviour is unchanged. scalar_arithmetic/scalar_compare -> the compile-time constant folding the compiler performs, asserted through constant_series. ScalarFunction and map_scalar_function stay: removing them depends on the parameterized math variants another change adds. Signed-off-by: discord9 <discord9@163.com> * chore(compat): move the from-versions window to v1.3.1 v1.3.1 has published release assets, so the compatibility check asks for it and fails on every branch that still names v1.3.0. Signed-off-by: discord9 <discord9@163.com> * test(promql): spell the state kind out in the merge-row encoder The encoder stands in for the executor that hands merge_row its input, and it was calling native::state::state_kind to name the kind. #10408 deletes that helper along with the other production-dead items in its scope, so depending on it makes these two changes fail to compile when they land together. The mapping is eight lines and belongs to the encoder anyway: it is the one place that has to agree with what the runtime would derive. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * feat(promql): answer max_of and min_of over two scalars natively (#10431) * feat(promql): answer max_of and min_of over two scalars natively Prometheus defines both as two scalars in, one scalar out (experimental), and the pinned promql-parser carries the same signature, so an instant-vector argument is already a parse error. The ScalarFunction kernel implementing Go's math.Max/math.Min -- an infinity of the function's own sign beats NaN, NaN beats any other value, +0 beats -0 -- existed with its golden cases but was never reachable from a query. The call now binds through the existing parameterized-map tower: compile_call -> scalar_function_call -> ParameterizedOp::MaxOf/MinOf -> ParameterizedMath -> PointMath::Scalar -> ScalarFunction::apply, executed by map_math_with_parameters. Only MaxOf/MinOf take the scalar-in/scalar-out shape; round and clamp* keep their vector contract, and clamp's max < min emptiness rule moved into PointMath verbatim. A constant first argument is folded and materialized as the one chunk it stands for, so max_of(1, 2) answers 2 without reading input, and two scalars on different grids are refused rather than answered wrongly. The 64-case golden now runs through NativeExecutor for both functions instead of the kernel alone, and the by-position pairing test was un-ignored and pointed at the parameterized path. map_scalar_function, whose only caller was that test, is removed. The legacy planner is untouched: neither name was implemented there. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): keep the call dispatch's comment beside the check it describes Signed-off-by: discord9 <discord9@163.com> * chore(compat): move the from-versions window to v1.3.1 v1.3.1 has published release assets, so the compatibility check asks for it and fails on every branch that still names v1.3.0. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * refactor(promql): let the legacy histogram path read the native kernel (#10420) * refactor(promql): read the legacy histogram estimators from the native kernel `prom_native_histogram_quantile` and `prom_native_histogram_fraction` estimated from `common_query`'s `NativeHistogram` bucket walk: exponential bucket bounds computed as `2^x`, and a forward walk from the lowest bucket only. The native kernel in `histogram_eval` walks the same histogram the way Prometheus does -- the bound table in `bounds`, and a walk from the highest bucket down once a finite-sum quantile reaches the median -- so one histogram had two answers that differ in their last places. Read the `NativeHistogram` through the borrowed `HistogramView` both representations share and return the kernel's answer, mapping its outcome back onto the NaN-observation notes the legacy UDFs record. Their signatures and the notes are unchanged; only the arithmetic behind them moves. The `2^x` estimator in `common_query::native_histogram` stays for that crate's public API and is annotated as such: no PromQL path reaches it any more. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): delegate the legacy histogram arithmetic to the native kernel `prom_native_histogram_add`, `_sub`, `_mul_scalar`, `_scalar_mul`, `_div_scalar`, `_neg`, the sum/avg accumulators, `delta`/`idelta`/`rate`/`increase`, and the weighted mean that merges partial states all scaled and combined `NativeHistogram`s with that type's own reconciliation: its own zero-bucket widening, schema reduction and custom-bound intersection, over `2^x` bounds. `FloatHistogram::add`/`sub`/`mul`/`div` already implement Prometheus's rules (`addBuckets`, `reconcileZeroBuckets`, `CopyToSchema`, `intersectCustomBucketBounds`) and the native kernel's golden tests cover them, so this keeps one implementation of the semantics. `AddOutcome` folds into the existing semantics rather than being dropped: `custom_bounds_reconciled` is the intersection the kernel keeps in `custom_values`, which the callers already announce from the operands, and `counter_reset_collision` is the contradiction the kernel answers with the unknown hint the legacy arithmetic derived from two hints that disagree; both are asserted in debug builds. The public `NativeHistogram` signatures are unchanged, the sums are compacted as before so `promql_eq` and the serialized spans keep their answers, and unary minus goes through the kernel's multiplication like the other scalings. Signed-off-by: discord9 <discord9@163.com> * test(promql): pin the legacy delegation's observable answers Comparing the bridge against the kernel it calls only shows they agree today; it cannot show what either answer means. These tests pin the answers themselves. - quantile with a NaN sum over several buckets answers 32.0 with NaNSkew, because the NaN walk leaves the last bucket for the interpolation; below the rank it answers NaN with NaNResult. - an exact tie answers 1.0 through the legacy walk and 2.0 through the kernel, on a fixture with an empty bucket between the two, and neither records a note. - assert_same_payload now compares every field, including the start timestamp policy, custom values, the complete span segmentation and the bucket bits. - round-tripping preserves reset hints, custom values, zero-length spans and gaps. - add/sub reconcile the reset hint and the custom bounds, compact cancelled buckets, and keep promql_eq's expanded-index equality. - scale/divide_by keep the field-by-field fallback for an unreadable layout -- mismatched counts, a backwards span, an overflowing index, a negative length -- and preserve its start timestamp; add/sub answer None for the same layouts. Zero, negative and NaN factors are pinned. - rate/increase/delta/idelta/irate clear the start timestamp of a derived histogram, while a single-sample sum aggregate keeps its stored one. A layout whose later span steps backwards is refused before the kernel sees it: the legacy bucket_indices rejected it, while side_from_view would have accepted repeated indices. Signed-off-by: discord9 <discord9@163.com> * test(http): pin the legacy histogram route's rate, NaN quantile and derived windows The delegation changes what three kinds of well-formed data answer, so assert it where a user sees it. Every request runs without the native hint and asserts !executes_fused_series on the observed plan, which proves the legacy route rather than assuming it from the missing hint. - rate and increase over a raw sample with a start timestamp inside the window still extrapolate from the synthetic zero: count 8/3 and sum 10/3 for a rate, 8 and 10 for an increase, with no infos or warnings. - histogram_quantile(0.5) over schema 0, buckets [8, 1], count 10 and a NaN sum answers 32.0 and reports the NaN-observation info, matching Prometheus's walk that leaves the last bucket for the interpolation. The previous estimator answered 2^-0.375. - rate over a raw window answers one series while the same window over h*1 answers none: the derived sample carries no start timestamp, so it has no synthetic zero to extrapolate from. Both phases are covered, memtable and flushed SST. The native-histogram test now installs the route interceptor the way its sibling tests do, so the plan can be observed; all of its original assertions are unchanged. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * test(promql): stop deriving budget expectations from the estimator (#10409) About fourteen accounting/reservation tests computed their expected figure, pool limit, or 'exactly fits' budget from operand_chunk_bytes / evaluated_bytes or a copied row-size constant, so a wrong formula stayed green. They now assert an independent behavioural property instead: a charge appears and is released to zero, an oversized operand is refused by the pool, a refused chunk leaves nothing reserved, or a bound stated in the bytes the fixture's own data holds. Ablation proof: temporarily doubling operand_chunk_bytes now fails 9 tests instead of 3, so these tests can actually catch a wrong formula. The ablation was reverted; promql 1209/1209 green, clippy clean. Signed-off-by: discord9 <discord9@163.com> * chore: drop a PR description that was committed by accident (#10463) `pr-histshared-body.md` is the body draft for #10420 and was committed along with that change. The repository root only carries the shared docs (AGENTS, CONTRIBUTING, README, SECURITY, AUTHOR), so this file has no reason to be here. Signed-off-by: discord9 <discord9@163.com> * chore(promql): drop production-dead state and sample helpers (#10408) - native/state.rs: remove ensure_bound_state and its only call site, plus the now-dead state_kind/state_components helpers (about 60 lines). - instant_eval.rs: remove the strictly-increasing re-check in select_instants; RawTimestamps::try_new already enforces the order. - native/split.rs: the Window arm declines on input_grid.is_none(); the declared-vs-input grid equality is already enforced when the window is bound (native/plan.rs). - datatypes/series/samples.rs: remove Samples::into_dense and its arrow_result helper; only datatypes tests called it. The eight densification tests existed only to exercise it and go with it. Verified: promql 1209/1209, datatypes 402/402, clippy -D warnings clean. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): move the segmentation engine into the promql crate (#10418) Lands the segmentation-engine move together with the uniform native-PromQL topology it builds on (#10321), which this branch carries as its base. * refactor(promql): share the extrapolation factor between float and histogram windows (#10425) * refactor(promql): share the extrapolation factor between float and histogram windows A native histogram counter can carry a start timestamp inside its window, which the histogram range path turned into a synthetic zero and extrapolated from with its own inline millisecond-domain formula. The float path already computed the same factor in extrapolation_factor, so the two routes could drift apart: the histogram branch counted the synthetic zero as an extra sample interval and extrapolated towards zero from a negative first count. The factor now takes an ExtrapolationSpan with two anchors -- sampled_from, where the increase was sampled, and average_from, where the average sample distance is measured. A float window sets both to its first sample; a histogram window starts the sampled interval at the synthetic zero and keeps the average on the real samples. A synthetic zero is the counter's zero point, so it is expressed as zero_point = Some(0.0), which reproduces the removed duration_to_start = 0.0 exactly. The sampled_interval == 0 divergence is deliberate and documented: a float window keeps its increase with a factor of 1, while the histogram window answers no value, since its buckets are all zero and an Inf or NaN rate would only poison its consumer. Signed-off-by: discord9 <discord9@163.com> * test(promql): make the two-anchor extrapolation tests able to fail Each of these could only pass: the route-equality table gave both fillings the same anchors, and the synthetic-zero geometry kept its ends under the threshold with the average anchor taken either way, so an anchor swap answered the same 1.375. The table now also carries a window whose anchors differ, pinned to 1.5 for the histogram filling and 1.75 for the float one, so collapsing the sampled anchor onto the average fails. The synthetic-zero test uses zero 1000, samples 2000/3000, eval 4500, range 4000: correct is 1.25, and measuring the average from the zero gives 1.75. Both numbers are asserted, and a temporary anchor swap was confirmed to fail the test before it was reverted. Signed-off-by: discord9 <discord9@163.com> * test(promql): give the synthetic-zero range test anchors it can tell apart The 1.375 geometry answered the same with the average anchor measured from the synthetic zero, so the end-to-end test agreed with itself either way. It now uses zero 1000, samples 2000/3000, eval 4500, range 4000: the count is (4 - 2) + 2 = 4, the factor 1.25, so an increase answers 5.0 and a rate 1.25, while a swapped anchor answers 7.0 and 1.75. The swap was applied to the production call site temporarily and failed the test with "Increase over two samples: 7 vs 5" before it was reverted. Signed-off-by: discord9 <discord9@163.com> * test(promql): give the two-anchor window geometry that can fail both ways The previous geometry had its end inside both thresholds, so an average interval read from the sampled anchor answered the same 1.5 as the correct one; only a sampled interval read from the average anchor changed the answer. With zero point 2000, average 2500 and last 4500 the end is past the correct threshold (2.2s) and inside the wrong one (2.75s), so it truncates one way and not the other: the correct answer is 1.4, an average anchor read from sampled gives 2.0, and a sampled anchor read from average gives 1.5. Both mutants are pinned as assertions, and both were applied to the production formula to confirm this test fails on them. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * refactor(mito2): drop the unreachable samples builder and the unused prepared-series contract (#10430) * refactor(mito2): drop the unreachable per-SST samples builder Production only ever constructs the columnar samples format: SeriesFormats::from_config is reached from with_format with experimental_series_index_format, where Legacy yields no samples backend and Columnar yields ColumnarSamplesFormat. The other SamplesFormat impl is cfg(test). So the per-SST branch of the maintenance task could never run, and build_samples had no caller but its own test. The daily columnar body is now unconditional, and the former branch position holds an explicit UnsupportedOperation error so an unknown backend fails loudly instead of building nothing. The two fake-format lifecycle tests were duplicates of real_samples_reconcile_reopen_and_retire, which drives the same build/reuse/reopen/ catalog-failure/purge scenario against the real format; the abort test moved to build_sample_windows, and a new test pins the refusal. The compatibility fixtures are untouched and still pass. Signed-off-by: discord9 <discord9@163.com> * test(mito2): cover retirement after a successful samples rebuild format_skeleton_build_reopen_and_retire was the only test of the retirement that follows a successful rebuild; real_samples_reconcile_reopen_and_retire covered the failed-publish path only. Removing the samples loop's mark_deleted() from publish_index_version left every other series_index and series_scan test green, which is what made the gap invisible. The real-format test now also rebuilds a day by deleting one of its objects, checks that the superseded generation is no longer in the published snapshot, that nothing is purged while the old snapshot and its handle are alive, and that its objects are purged once they are dropped. With mark_deleted() commented out, all four cases fail on that last assertion. Signed-off-by: discord9 <discord9@163.com> * test(mito2): pin the purge to the last reader handle, not the snapshot Dropping the published snapshot was allowed to release a superseded generation as long as one of its handles was still held elsewhere, which no test covered: the retirement assertions only checked that nothing was purged while the snapshot lived, then dropped every handle at once. The test now holds a reader handle on every generation the superseded snapshot published before releasing it, so that release must purge nothing, and asserts that between the two drops. A rebuild replaces several generations of a source group at once, so holding only the one the test tracks would let its siblings be released legitimately and the assertion would fail for a reason unrelated to the lifecycle under test. A mutation that purges on snapshot drop -- ignoring live reader handles -- fails this new assertion in all four cases and none of the later ones. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * docs(promql): trim design-narration comments in native planner and runtime (#10421) Comment-only cleanup on top of the segmentation move (#10418) and the topology it carries (#10321). * test(query): pin the plan shapes the uniform topology builds (#10466) * test(query): pin the aggregate plan shapes the uniform topology builds Four native-aggregate integration tests have failed on `narthex` since the uniform native PromQL topology landed (#10418): their plan-shape helpers demanded the pre-topology chains, panicked before the native-vs-legacy comparison ran, and nextest's fail-fast then left most of the suite unexecuted. Both shapes the helpers now see are the topology's own: - a Partial producer the analyzer keeps above the regions reads the `MergeScanExec` through one `SeriesRepartitionExec` that widens the region stream to `target_partitions` with `RawPrimaryKey`, so each stored key still reaches exactly one owner; - a single-owner Final reads the Partial producer itself through its state router, instead of a region merge scan, because both halves are planned in the frontend tree now. Pin each chain exactly rather than accepting any exchange, and run the legacy comparison before the shape assertions so a shape regression can no longer hide the result check. Verified: native_aggregate 6/6, native_promql 90/94 (the four `integration_http_s3*` cases abort for environment reasons), clippy and fmt clean. Signed-off-by: discord9 <discord9@163.com> * test(query): read one raw source per selector occurrence in the CTE case `cte_native_statement_runs_the_fused_plan` asserted that one logical table keeps both occurrences of a repeated selector region-local, so no raw source appears in the plan. The uniform topology reads every selector through its own raw source, so the statement now names two. Pin that instead: both occurrences still read their own region scan, and each does it through a raw source of its own. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * fix(test): stop requiring every scan partition to be non-empty (#10469) `SeriesRepartitionExec` routes a series by hashing its label set, so the eight series this test writes over four partitions can legitimately leave one empty: the fixture happened to hash evenly before, and it no longer does (`{0: 0, 1: 3, 2: 3, 3: 2}` on `narthex`). Requiring every partition to be non-empty asserts a property of the hash, not of the topology. What the test actually needs is that the series are spread over more than one partition - otherwise the merge it exists for is never exercised - and that routing loses none of them. Assert those instead, and keep the existing "all four partitions reported" check. Signed-off-by: discord9 <discord9@163.com> * test(promql): cover unequal-width owner routing and scalar broadcasts (#10039) Signed-off-by: discord9 <discord9@163.com> * test(query): prove a default engine option keeps native requests on the legacy route (#10459) The engine-wide `experimental_native_promql` option is the outer gate. #10371 pinned its default, but nothing showed what that default does to a request that explicitly asks for the native route — which is the case that actually matters when the branch is merged back: a client can always send the per-request extension, and the engine option is the only thing left deciding. `plan_native_promql_with_query_options` always sets `query.experimental_native_promql=true` on the request, so passing `QueryOptions::default()` covers exactly that combination. The assertion looks at the rendered plan for both `FusedSeries` and `RawSeriesSource`, so a future change that lets the request alone admit the route fails here. Signed-off-by: discord9 <discord9@163.com> * feat(promql): name native result columns from expressions (#10231) Use the statement expression for native value column names, resolve legacy value columns by type rather than by name, and reject label-function destination collisions instead of dropping a value. Keep matrix roots named after the whole statement, preserve an Int64 __tsid value, and propagate schema-collision errors from canonical boundaries. Signed-off-by: discord9 <discord9@163.com> * refactor(promql): decide native admission with an enumerable predicate (#10424) Admission to the native PromQL route was decided by trial-building the whole runtime tree and discarding it, at five call sites of NativeExecutionState::try_new(...).is_err(). That is where the refusal lived, not anywhere enumerable. Replace those calls with native_tree_supported(&tree), a predicate on the compiled tree's shapes that builds no runtime: it declines the nine shape classes RuntimeNode::build rejects (raw-sample roots, interleaved inputs, scalar() over a multi-field vector, non-performable broadcasts, quantile aggregates, value-field groupings, vector ops over multi-field vectors, top-k variants, and unsupported window modifiers). try_new keeps its construction-time validation for the refusals that are not shapes. Which queries reach the native route is unchanged; the new tests assert both the predicate's verdict and that the runtime agrees. Verified: promql 1211/1211, query 1130/1130, native_promql 93/93, clippy -D warnings clean. Signed-off-by: discord9 <discord9@163.com> * feat(promql): fold pi() and answer quantile as a complete aggregate (#10433) * feat(promql): fold pi() and answer quantile as a complete aggregate pi() folds through constant_value like any other constant, so pi() * x, clamp_max(x, pi()) and quantile(pi()/4, x) all compile natively. The value is std::f64::consts::PI on both routes. Note this is wider than the legacy planner, which does not treat a call as a literal and so rejects those parameter positions; it is an extension of constant support, not a narrowing. quantile now binds a Complete fold that retains the group's input and answers with the existing aggregate_quantile kernel. A quantile has no partial kernel, so split.rs and the state binding are untouched and a Partial/Final quantile cannot exist: validate refuses it and check_cut declines the cut. A partitioned input therefore gathers above per-series prefixes instead of folding per partition, which is the only correct distributed shape for a non-associative aggregate -- the raw-source boundary keeps fragments from being read as whole series, and the router merges a raw identity once every input reaches EOF. The retained input is not a fixed-width state: a quantile keeps every group until EOF and cannot spill, so it is bounded by pool admission, which the module doc now states. Its parameter is sparse, covering the grid positions the input actually holds rather than the window they span. Two points 100k positions apart used to allocate an 800KB dense parameter before any admission; now the parameter and the kernel's position/value copy are charged by row bound before allocation, and retaining a series reserves the Vec capacity it is about to grow to rather than one slot. A test runs two distant points under a 64KiB pool and finishes with nothing left reserved. quantile over a histogram no longer falls back: the legacy planner groups it with min, max, stddev and stdvar, skipping histogram samples and reporting an info, so the admission check now refuses only sum and avg, which fold the payload. The native runtime asserts the same info as min over a histogram-only chunk. Signed-off-by: discord9 <discord9@163.com> * fix(promql): bill a quantile's retained chunks the way a binary operand is billed series_bytes charges the whole Arrow parent buffer a slice points into, and every histogram table it references, so a series handed over as many slices of one buffer was charged once per chunk: two thousand one-row slices of a 16 KB array came to ~32 MB against a 4 MiB pool and were refused around the 260th, while min over the same input answered. Retained chunks are now billed as a binary operand bills its operand: operand_chunk_bytes measures what each chunk holds on its own, with the parent buffer counted once across its slices, and unbilled_tables bills each histogram table once, remembered by allocation address. QuantileInput::bytes() reports real residency -- its own size, the vector's capacity and the folded held bytes -- instead of a per-chunk sum, so every resize settles on what the node actually keeps. The capacity pre-booking stays and is kept out of held, so a vector slot is not charged twice. The regression hands one array over as 2000 single-row slices under a 4 MiB pool and asserts min and quantile answer the same and both return their reservation. Restoring the old billing makes it fail with ResourcesExhausted, which is the failure it exists to catch. Signed-off-by: discord9 <discord9@163.com> * fix(promql): bill a quantile's retained input once per allocation The previous change stopped charging every chunk for the whole parent buffer, but the figure was still not self-consistent in three ways. operand_chunk_bytes subtracted the whole of fields_bytes() and added back only the row bytes of the slice, so a small slice of a big buffer was billed as small while the whole allocation stayed resident, and a validity bitmap was not counted at all. Retained chunks are now billed per Arrow allocation, keyed on the buffer's data pointer and charged at capacity, the same way unbilled_tables bills histogram tables. The no-identity alternative -- bill every chunk for the whole buffer -- over-estimates and would refuse a legal query, which is the reported bug mirrored. series_bytes includes size_of::<Series>(), and QuantileInput::bytes() also counted every vector slot, so each used slot paid for its Series twice. The chunk figure now carries only what sits beside the samples and the struct is left to the vector. pending() returned the incoming chunk's figure for every fold, but the quantile retainer books its own figure before the push, so that backing was reserved twice and could refuse a query before any memory was actually held. finish_quantile also settled on the output while its input was still alive by value; it now drops the input first. Billing only; no answer changes. A 2000-chunk input goes from 1,757,960 to 1,181,968 bytes (the 2000 x 288 of double-counted structs plus one 16 KB allocation billed once), and every quantile billing test ends with the pool empty. Five mutations -- rows-only figure, no buffer dedup, per-chunk series_bytes, grid-wide parameter, struct added back per chunk -- each fail the test that covers them. Signed-off-by: discord9 <discord9@163.com> * test(promql): cover a quantile over no input and drop its redundant guards A quantile whose input holds no sample in range has no positions to build a parameter from, so it hands the kernel an empty scalar and answers nothing. No test reached that: replacing the fallback with an error left every quantile and aggregate test green. The new one runs the node without pushing a chunk at all and asserts an empty answer with the pool back to zero. new_quantile also re-checked the stage, the kind and the grid. validate already refuses a Partial or Final quantile -- aggregate_state_binding has no state for one -- and the node's schema is the tree's validated output, so those checks could not fire. The quantile is now passed in from the arm that destructured it. The billing test's comment described the operand_chunk_bytes model; the quantile bills chunk_bytes_beside_samples plus the Arrow allocations its slices point into. Signed-off-by: discord9 <discord9@163.com> * test(promql): answer a quantile over a multi-input tree instead of declining it quantile stopped being a kind the runtime refuses, so the query that pinned that refusal now answers natively. It moved out of the declines test into its own case, which asserts the existing plan answers 3/16, that the route plans a fused node for it, and that the native rows equal the legacy ones. The grouping-by-a-value-field case stays where it was. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * feat(promql): show the native tree as plan lines with per-node metrics (#10202) feat(promql): draw the native tree as plan lines with per-node metrics The fused node prints its own compiled tree instead of echoing the PromQL text it was built from: one node per line inside a `native_tree=[ ... ]` block, with each node's figures on its own line when the statement ran. A split aggregate's two halves therefore show the trees they compute rather than the same statement, and EXPLAIN/ANALYZE of a native statement reads like the plan it runs. The statement text stays on the node as data: it is what names the value columns (`expression_field_names`), so it has to survive to execution and over the wire where a node is rebuilt from its envelope. What changes is that it is no longer what the node prints. The tree's registrations sit in the node's own metric set, so a reader that keys by name reads that set; `ExecutionPlan::metrics` leaves them out because a flat set cannot say which node recorded what. Signed-off-by: discord9 <discord9@163.com> * perf(promql): cut per-series fixed costs in decoding, packing and accounting (#10416) Signed-off-by: discord9 <discord9@163.com> * feat(promql): take the native route's switch from the query options (#10262) The unified query option mechanism is already in this branch's base, so what is left is the native route reading it: the switch resolves once in apply_query_options as request over session SET over the engine default, and both the planner and SHOW read that resolved value instead of combining the engine option with the request extension themselves. The engine-wide option therefore stops being a gate a request also has to pass and becomes the default a request or a session states over, which is what the other query options in this crate already do. What that costs is stated rather than implied: an operator can no longer switch the experimental route off for clients that set query options, and none of the entry points (HTTP hints, gRPC context, SET) carries an authorization check of its own. Data permissions are unaffected - the statement still plans through the same scanners and the same per-table checks. #10459 asserted the old gate; its test is rewritten here to pin the new contract in both directions instead of being dropped. Metric discovery stops keying off the request extension for the same reason: a session SET or the engine default can turn the route on without one. It reads the switch through the same resolver planning reads, so a query that leaves the route off is not bounded for discovery and pays no per-table grant for it. Signed-off-by: discord9 <discord9@163.com> * fix(test): render the schema view in the quantile fold test (#10479) #10202 changed what `display_indent` shows: plan lines with per-node metrics, without the statement. `native_fused_quantile_is_never_folded_per_partition` asserts the statement is still named by the quantile it wrote, so it now reads `display_indent_schema`, which keeps rendering it. This is the only assertion in the file that depends on the statement text; the other `display_indent` call sites assert plan structure. Every PR based on `narthex` currently fails this test. Signed-off-by: discord9 <discord9@163.com> * feat(promql): fold and average native histograms in the native aggregate (#10199) * feat(promql): carry a native histogram in aggregate state `sum` and `avg` over native histograms need a partial state whose payload is variable width, which the numeric state family cannot express. Add two kinds, `histogram_sum` and `histogram_avg`, whose components are a live histogram, a layout-carrying compensation, and the evidence a merge records. A position's payload is one `StatePayload`, either the numeric components a float kind carries or a boxed histogram state, so absence stays a single `None` and a field never holds both families. The numeric encoding is byte for byte what it was, and a histogram component nests the same struct a histogram sample frame already uses. The compensation must carry the live value's bucket layout: the kernel indexes it by the value's positions, so a placeholder without bucket indices would be an out-of-bounds read rather than a loss of precision. Both the producer and the decoder check the schema, the zero threshold, the custom bounds and the expanded logical bucket indices, which accepts two spellings of the same buckets and refuses two different layouts. State accounting charges the payload's real width instead of a fixed slot, so a wide bucket layout is paid for. `HistogramSum` cannot declare `incremental_mean`, which is checked structurally: a sum has no mean mode. No admission, split or runtime wiring yet; nothing produces these states until the aggregate kernels land. Signed-off-by: discord9 <discord9@163.com> * feat(promql): fold native histograms into a `sum` aggregate `sum` over a native histogram now folds in the native aggregate: the first sample at a grid position establishes the accumulator from a layout-carrying zero compensation, and every later one is added with a compensated add whose receiver is the live value. The evidence the final answer needs is recorded as it appears: set when a merge reconciled custom bucket bounds, and both reset-hint bits when samples reported a reset and reported that none happened. Two layouts that cannot be added - exponential and custom buckets in one position - poison the point, which the final answer drops instead of answering a partial sum. A numeric overflow is not a fault and does not poison anything: `sum` answers `Inf` when its cumulative sum saturates, exactly as the kernel's own sum does. `avg` keeps refusing a histogram: its mean mode needs the merge rules, which are not part of this change. A group that kept histogram payloads now refuses to export a partial numeric state rather than exporting an empty one. Signed-off-by: discord9 <discord9@163.com> * feat(promql): average native histograms in the native aggregate `avg` over a native histogram folds the same way the v3.15.0 kernel averages a range: sum with compensation while the sum fits, then switch to a running mean once the next add would overflow. The switch keeps the answer finite where the sum would not be: two histograms whose scalar sum is `1e308` each average to `1e308`, not to `Inf`. It starts the mean from the sum accumulated so far divided by the samples that produced it, and weights every later sample by the fraction of samples it represents, so the compensation is scaled by the same weight as the value it belongs to. The complete answer divides both the sum and its compensation by the sample count, or answers the mean with its compensation. `start_timestamp` is cleared and the payload compacted, as every other derived histogram is. No operation refuses a histogram sample any more, so the refusal variant is gone rather than left unused: what a fold accepts is decided by its arithmetic, not by the sample's type. Signed-off-by: discord9 <discord9@163.com> * feat(promql): merge native histogram state across a split A split aggregate exports its partial state and folds it somewhere else, so a histogram fold has to travel. The binding now says which layout a node carries: `aggregate_state_binding(kind, field_kind)` picks the histogram family for a histogram input and the numeric family otherwise, and `BoundAggregate` records the kind its direct input answers with, decided where the node is bound. The partial half exports the live histogram, the compensation that carries its bucket layout, and the evidence the merge needs; a sum cannot claim a mean mode because its kind has no such component. The final half merges two partials the way the v3.15.0 kernel merges contributions: two compensated adds for a sum, and a mean path that converts each side, turns its count into a weight, and scales before adding so that a pair of `1e308` histograms still averages to `1e308`. A sum/sum pair first runs the same trial the complete fold runs, and only keeps it when it does not overflow. An unreconcilable layout poisons the point in either partial: the merge keeps it poisoned and never revives it, and the answer drops that point. Evidence is merged bit by bit, including from a poisoned side, so which annotation fires does not depend on the merge order. The node that finishes the answer is the only one that reports: `Complete` and `Final` speak after their output is queued, `Partial` stays silent because its evidence travels in the state. A poisoned point suppresses its reconciliation info, which is what makes the message set order-independent. Signed-off-by: discord9 <discord9@163.com> * feat(promql): admit a native `sum` and `avg` over histograms A payload-folding aggregate is now admitted exactly when it binds histogram state: `sum` and `avg` over a native histogram fold and merge their own state, so the gate that refused every such input now asks what the node will build rather than what its input carries. Their answer column is the canonical histogram struct; `count`, `group`, `min`, `max`, `stddev` and `stdvar` keep answering floats whatever they read. The split may carry a histogram answer when the cut aggregate exports histogram state. That is also what makes the state schema's value column count follow the kind, which a histogram split otherwise failed against. A histogram fold the split declines is not segmented. Segmentation answers a group by folding each segment and appending the pieces, so a point whose contributions are spread over partitions would be folded once per segment - a sum would merge histograms that never met, an average would average averages. The whole statement therefore keeps the plan it had, which also covers an aggregate above an aggregate and a whole-vector ranking above a fold: neither can cross the split boundary, and cutting them would answer a piece no segment owns. Multi-input histograms and a histogram behind a canonical merge keep their refusals: the state crossing a canonical boundary has no execution coverage. Signed-off-by: discord9 <discord9@163.com> * test(promql): read a native histogram sum and average over HTTP Three cases write native histograms through Remote Write 2.0 and read the answer back through the Prometheus query API. Each one asserts the native route really executed: the observer records the split's two fused producers, and the one publishing a histogram column is the final consumer, so a statement that fell back to the existing plan cannot satisfy the test. `sum` and `avg` answer the values the v3.15.0 kernels produce, and both are compared against the same query without the native hint. That comparison uses a relative tolerance for `avg` and an exact one for `sum`, because the existing plan averages by weighting each sample (`12 * (1/3)` is one ulp below `12 / 3`) while the native fold divides at the end - the difference this change documents rather than hides. The partitioned case flushes after every write so the scan really splits, then asserts the partial producer reported output in more than one partition and that the same query at parallelism one answers the same JSON. Two layouts in one metric remove the group and report it: the answer is empty and the response's warnings name the incompatible layouts, asserted after the native nodes are known to have run. Signed-off-by: discord9 <discord9@163.com> * fix(promql): decide a reset collision per point and pin the avg merge weights The contradictory-reset warning was decided from the two evidence bits OR-ed across every group and every point, so sum by (job) (h) with CounterReset only in job a and NotCounterReset only in job b warned, as did one series crossing its reset over a range. Prometheus checks counterResetSeen && notCounterResetSeen per group per step, so the bit is now decided per accumulator and only the decision is OR-ed across the answer. The state layout is untouched: it still carries the two raw bits, and the collision is settled when the answer is. Nothing caught a swapped pair of avg merge weights: the existing overflowing-pair test gives both partitions one sample and the same mean, so both weightings answer 1e308. The new case merges a two-sample partition that switched to a mean with a six-sample one that still sums, weights 2/8 and 6/8, and asserts 7 * 2^1019; swapping the weights answers 13 * 2^1019 and fails. Signed-off-by: discord9 <discord9@163.com> * test(promql): merge histogram partials through the accumulator, not a row encoder The eight histogram merge tests drove their partials through a test-side encoder that writes numeric state rows, so they panicked the moment a state carried a histogram payload. That encoder never produced a histogram row for anyone: the merge they want to check is the one HistogramAccumulator::merge_state performs, and the row path is the executor's. They now merge the payloads directly -- from_payload for the first, merge_state for the rest -- and read the answer through finished_histogram and the evidence through the production summary. Every assertion keeps its value and strength: the unequal-count case still asserts 7 * 2^1019, so swapping the merge weights answers 13 * 2^1019 and fails, and the evidence case still fails when the merge order decides the result. Signed-off-by: discord9 <discord9@163.com> * test(promql): drive histogram state rows through merge_row The cross-partition merge reads each partition's state frame as a StateRow, and merge_histogram_row is the code that decodes a histogram payload out of one. No test reached it: the numeric row encoder panics on a histogram, and the histogram merge cases go through the accumulator instead, so a bug in that decode -- a swapped component, a poison bit lost, a field index off by one -- would have left every test green. These construct StateFieldsBatch rows directly and merge them. Sum and the average's mean path assert the same bits the accumulator answers for the same payloads, so the two paths cannot drift; a poisoned row is asserted to poison the point; a row missing a component and a row whose compensation carries another bucket layout are asserted to be refused rather than answer something. Dropping the poison bit, not decoding the mean mode, and reversing the field index each fail one of them. Signed-off-by: discord9 <discord9@163.com> * fix(promql): bind an aggregate's state from the statement's own field kind The rebase replayed the compiler call with a literal Some(FieldKind::Float) for the statement's source kind, so every aggregate bound float state: sum over a native-histogram column asked the gate whether it folds a payload, answered yes, and the statement fell back to the legacy plan with prom_native_histogram_agg_sum in it. The call now passes statement_kind, the kind the prepared scans report for the statement's fields. The duplicate guard on the segmentation path -- which refused every histogram fold rather than only the ones the split declined -- is removed, and the mutation scripts the earlier commits picked up are dropped. Signed-off-by: discord9 <discord9@163.com> * test(http): register the three native histogram aggregate cases Splicing the range test and this branch's cases together during the rebase kept the function bodies but dropped their http_tests! entries, so the three cases existed without ever running. Signed-off-by: discord9 <discord9@163.com> * test(promql): identify the histogram column by type, not by name Native result columns are named after their expressions now, so the three admission tests that looked for a column called greptime_native_histogram found none in a plan that answered exactly the histogram they meant to check. They look for the histogram value type instead, which is what the assertions were about. Signed-off-by: discord9 <discord9@163.com> * test(http): find the histogram answer by type and allow an empty scan partition `answer_consumer` looked for a column named `greptime_native_histogram`, which the answer is no longer called now that result columns take their expression's name (#10231). It reads the histogram value type instead, and only the schema's own fields: a state frame keeps its histogram inside `fields`, so a top-level type check still finds the consumer and not the producer. The partition case also required every scan partition to hold rows. Routing hashes each series' label set, so eight series over four partitions can leave one empty; #10469 stopped asking for non-empty partitions elsewhere and `test_remote_write_v2_histogram_native_range_function` already reads the same fixture this way. The producer still has to run on more than one partition, and the answer still has to count every series once. Signed-off-by: discord9 <discord9@163.com> * fix(promql): carry the histogram fold onto #10433's bound aggregate Rebasing onto #10433 left three things behind. `BoundAggregate` gained `field_kind`, which the quantile tests construct without; the value is `Float` because those fixtures hand the fold float samples, and the one call to `aggregate_state_binding` now names the kind it asks about. `native_composed_binary_admits_complex_operands` also expected `hist * on(tag_0) sum by (tag_0) (hist)` to fall back, for a reason this branch made false: it said `sum` has no histogram state to fold into. It has one now, so the pair composes. `histogram * histogram` is not an error in Prometheus - only `+` and `-` apply and any other operator removes the element with an info annotation - so the composed pair is admitted and keeps the legacy float layout for the answer no sample can fill. The test asserts that shape instead, and the refusal it still checks (`sum(sum(hist))`) is named for what refuses it: the split cannot carry an aggregate above a split aggregate. Signed-off-by: discord9 <discord9@163.com> * fix(promql): carry the histogram fold onto #10202's plan-line display `BoundAggregate.field_kind` again: the display test's `sum(rate(...))` builds one without the field, and its input is a histogram, so the field says so. `native_fused_quantile_is_never_folded_per_partition` looked for the statement's text in the display to show the fold is still named by the quantile it wrote. #10202 renders the tree as plan lines instead, so the quantile is named by its own line - `Aggregate: quantile 0.5` - and that is what the assertion reads now. Signed-off-by: discord9 <discord9@163.com> * style(http): keep the answer-column doc off a list item The first line of the histogram sentence ended on a hyphen, so the next line began with one and clippy read it as an unindented list item. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * test(promql): pin the existing route when a fixture defaults to native (#10484) The three native-route tests turn `experimental_native_promql` on in the standalone's query options, so the "legacy" request that follows the native one also takes the native route. The comparison then checks the native answer against itself, and the observer collects both runs: `sum_and_avg` expects the split's two nodes and saw four. Ask for the existing route explicitly with `x-greptime-hints: query.experimental_native_promql=false`. Signed-off-by: discord9 <discord9@163.com> * perf(promql): stop building label-set tables where they cannot collide (#10412) * perf(promql): stop building label-set tables where they cannot collide A `Window` node used to hold an Emitted table of the label sets it had already published, so that a statement dropping the metric name could still keep two series apart when their remaining labels were identical. Every window paid for that table whether or not it could ever collide: a plain `rate()` over a table whose tag columns are nullable had to collect and merge for its whole life, because the table could not prove two series would not fold into one. The table is gone. The proof moved to plan time: a drop that cannot collide - one metric, and no `__type__`/`__unit__` tag to lose - needs no `Merge`, and any other shape gets a canonical `Merge` inserted where the drop happens. The canonical merge kernel already answers the collision, so the runtime no longer carries an occupancy set to detect one. A decline now records why it declined, which is what tells the two paths apart in metrics. Signed-off-by: discord9 <discord9@163.com> * test(promql): pin that a drop above a name-dropping kernel needs no merge `rate(x[5m]) * 2` plans one window and one broadcast, and `abs(abs(rate(x[5m])))` one window and two maps; neither plans a merge, because the metadata labels are all the drop can lose and the labels proof covers them. The review asked for these two shapes by name: with the labels proof forced to answer `false` the test reports `left: 0, right: 2` for the first, which is the count it planned before. Signed-off-by: discord9 <discord9@163.com> * fix(promql): make the metadata drop's proof ask the storage keys `drop_metric_name` also drops an empty label value (`map.rs:367-368`), so two stored series that differ only in `{zone=""}` versus no `zone` become one label set after a name-dropping kernel. The labels alone cannot see that pair, so the proof was incomplete: a nullable key column let the pass insert no `Merge`, and the runtime then emitted two series of one label set instead of reporting `vector cannot contain metrics with the same labelset`. The proof now asks `keys_survive_metadata_drop` - the rule the physical scan already reads - and a `Merge` at or below the drop answers its empty-value half, so a chain of drops over a nullable table pays one collection at the bottom rather than one per drop. Every GreptimeDB tag column is nullable today, so that boundary is what a real table gets. A canonical merge carrying histogram frames was the one pairing nothing exercised, and the extra boundary made it reachable: the gate that declined it is gone. A histogram statement now crosses the boundary - a producer per partition, a consumer above the router - on the merge kernel's own `SampleRef::Histogram` arm, which is where the duplicate this fix is about is reported. That crossing is covered by an HTTP case over a nullable tag, and by `Merge` coverage for complementary positions, overlapping positions, missing samples and sliced payloads, the evaluated frame's histogram and mixed encodings through producer, router and consumer, a float-producing histogram function below a nested boundary, and the segmented plan. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <discord9@163.com> * revert(promql): revert #10412, merged without approval (#10495) Revert "perf(promql): stop building label-set tables where they cannot collide (#10412)" This reverts commit1fd9069418. * revert(promql): restore #10412, undoing the revert (#10497) Revert "revert(promql): revert #10412, merged without approval (#10495)" This reverts commit3f8d718a30. * revert(promql): revert #10412 again, unreviewed changes included (#10500) Revert "revert(promql): restore #10412, undoing the revert (#10497)" This reverts commit405c2f9016. * test(promql): read the partition producer's node metrics from the plan's own set (#10501) `test_remote_write_v2_histogram_avg_native_partitions` read the producer's per-partition `native.root.output_values` through `ExecutionPlan::metrics`. `FusedSeriesExec::metrics` drops node metrics on purpose - `is_node_metric` covers `native.root.*`, so the operator's set reports only its own figures - and the map stayed empty. The assertion then saw zero partitions where the statement asks for four. Read the plan's own set through `node_metric_set`, which is how the other node-metric assertions in this file already do it. Signed-off-by: discord9 <discord9@163.com> --------- Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com> Signed-off-by: discord9 <discord9@163.com> Signed-off-by: discord9 <discord9@users.noreply.github.com> Signed-off-by: WenyXu <wenymedia@gmail.com> Signed-off-by: Dennis Zhuang <killme2008@gmail.com> Signed-off-by: evenyag <realevenyag@gmail.com> Co-authored-by: Weny Xu <wenymedia@gmail.com> Co-authored-by: Dennis Zhuang <killme2008@gmail.com> Co-authored-by: Yingwen <realevenyag@gmail.com>
18859 lines
700 KiB
Rust
18859 lines
700 KiB
Rust
// Copyright 2023 Greptime Team
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
use std::collections::BTreeMap;
|
|
use std::io::Write;
|
|
use std::str::FromStr;
|
|
use std::sync::{Arc, Mutex};
|
|
use std::time::Duration;
|
|
|
|
use api::greptime_proto::io::prometheus::write::v2::histogram::{Count, ZeroCount};
|
|
use api::greptime_proto::io::prometheus::write::v2::metadata::MetricType as RemoteWriteV2MetricType;
|
|
use api::greptime_proto::io::prometheus::write::v2::{
|
|
BucketSpan, Histogram, Metadata as RemoteWriteV2Metadata, Request as RemoteWriteV2Request,
|
|
Sample as RemoteWriteV2Sample, TimeSeries as RemoteWriteV2TimeSeries,
|
|
};
|
|
use api::prom_store::remote::label_matcher::Type as MatcherType;
|
|
use api::prom_store::remote::{
|
|
Label, LabelMatcher, Query, ReadRequest, ReadResponse, Sample, TimeSeries, WriteRequest,
|
|
};
|
|
use api::v1::RowInsertRequests;
|
|
use auth::{UserProviderRef, user_provider_from_option};
|
|
use axum::http::{HeaderMap, HeaderName, HeaderValue, StatusCode};
|
|
use base64::prelude::{BASE64_STANDARD, Engine as _};
|
|
use chrono::Utc;
|
|
use cmd::options::GreptimeOptions;
|
|
use common_base::Plugins;
|
|
use common_catalog::consts::{
|
|
DEFAULT_PRIVATE_SCHEMA_NAME, TRACE_TABLE_NAME, trace_operations_table_name,
|
|
trace_services_table_name,
|
|
};
|
|
use common_config::Configurable;
|
|
use common_error::status_code::StatusCode as ErrorCode;
|
|
use common_frontend::slow_query_event::{
|
|
SLOW_QUERY_TABLE_NAME, SLOW_QUERY_TABLE_QUERY_COLUMN_NAME,
|
|
};
|
|
use common_memory_manager::OnExhaustedPolicy;
|
|
use common_options::plugin_options::StandaloneFlag;
|
|
use datafusion::common::{DFSchema, ToDFSchema};
|
|
use datafusion::logical_expr::{EmptyRelation, LogicalPlan};
|
|
use datafusion::physical_plan::metrics::MetricValue;
|
|
use datafusion::physical_plan::{ExecutionPlan, ExecutionPlanProperties};
|
|
use datatypes::arrow::array::{
|
|
Array as _, Float64Array, StringArray, StringViewArray, TimestampMillisecondArray,
|
|
};
|
|
use datatypes::arrow::datatypes::{DataType, SchemaRef, TimeUnit};
|
|
use datatypes::series::samples::{SampleRef, SeriesSchema};
|
|
use datatypes::series::time::{EvalGrid, GridSpec};
|
|
use flate2::Compression;
|
|
use flate2::write::GzEncoder;
|
|
use frontend::instance::Instance;
|
|
use futures::TryStreamExt;
|
|
use log_query::{AggFunc, Context, Limit, LogExpr, LogQuery, TimeFilter};
|
|
use loki_proto::logproto::{EntryAdapter, LabelPairAdapter, PushRequest, StreamAdapter};
|
|
use loki_proto::prost_types::Timestamp;
|
|
use opentelemetry_proto::tonic::collector::logs::v1::ExportLogsServiceRequest;
|
|
use opentelemetry_proto::tonic::collector::metrics::v1::{
|
|
ExportMetricsServiceRequest, ExportMetricsServiceResponse,
|
|
};
|
|
use opentelemetry_proto::tonic::collector::trace::v1::{
|
|
ExportTraceServiceRequest, ExportTraceServiceResponse,
|
|
};
|
|
use pipeline::{
|
|
GREPTIME_INTERNAL_TRACE_PIPELINE_V1_NAME, GREPTIME_INTERNAL_TRACE_PIPELINE_V2_NAME,
|
|
};
|
|
use prost::Message;
|
|
use query::options::QueryOptions;
|
|
use query::parser::PromQuery;
|
|
use query::physical_wrapper::{PhysicalPlanWrapper, PhysicalPlanWrapperRef};
|
|
use rstest_reuse::apply;
|
|
use serde_json::{Value, json};
|
|
use servers::http::handler::HealthResponse;
|
|
use servers::http::header::constants::{
|
|
GREPTIME_LOG_EXTRACT_KEYS_HEADER_NAME, GREPTIME_LOG_TABLE_NAME_HEADER_NAME,
|
|
GREPTIME_PIPELINE_NAME_HEADER_NAME,
|
|
};
|
|
use servers::http::header::{GREPTIME_DB_HEADER_NAME, GREPTIME_TIMEZONE_HEADER_NAME};
|
|
use servers::http::otlp::GoogleRpcStatus;
|
|
use servers::http::prometheus::{
|
|
Column, PromQueryResult, PrometheusJsonResponse, PrometheusResponse,
|
|
};
|
|
use servers::http::result::error_result::ErrorResponse;
|
|
use servers::http::result::greptime_result_v1::GreptimedbV1Response;
|
|
use servers::http::result::influxdb_result_v1::{InfluxdbOutput, InfluxdbV1Response};
|
|
use servers::http::test_helpers::{TestClient, TestResponse};
|
|
use servers::http::{GreptimeQueryOutput, HttpOptions, HttpServerBuilder};
|
|
use servers::interceptor::{PromQueryInterceptor, PromQueryInterceptorRef};
|
|
use servers::prom_remote_write::v2::test_util as remote_write_v2;
|
|
use servers::prom_remote_write::validation::PromValidationMode;
|
|
use servers::prom_store::{self, mock_timeseries_new_label};
|
|
use servers::query_handler::sql::SqlQueryHandler;
|
|
use servers::request_memory_limiter::ServerMemoryLimiter;
|
|
use session::context::{QueryContextBuilder, QueryContextRef};
|
|
use standalone::options::StandaloneOptions;
|
|
use table::table_name::TableName;
|
|
use tests_integration::test_util::{
|
|
MockInstanceImpl, StorageType, TestGuard, assert_wal_delta, build_test_prom_server,
|
|
execute_sql_and_expect, setup_test_http_app, setup_test_http_app_with_frontend,
|
|
setup_test_http_app_with_frontend_and_custom_options,
|
|
setup_test_http_app_with_frontend_and_slow_query_threshold,
|
|
setup_test_http_app_with_frontend_and_user_provider, setup_test_prom_app_with_frontend,
|
|
setup_test_prom_app_with_frontend_and_memory_limiter,
|
|
setup_test_prom_app_with_frontend_batched,
|
|
};
|
|
use urlencoding::encode;
|
|
use yaml_rust::YamlLoader;
|
|
|
|
use crate::both_deployment_cases;
|
|
use crate::event_recorder_test_util::assert_procedure_actor_by_table;
|
|
|
|
/// Registers tests that exercise nothing below the HTTP layer, so running them
|
|
/// against a remote object store only pays bucket setup and teardown.
|
|
#[macro_export]
|
|
macro_rules! http_local_tests {
|
|
($($(#[$meta:meta])* $test:ident),*,) => {
|
|
mod integration_http_local_test {
|
|
$(
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
$(
|
|
#[$meta]
|
|
)*
|
|
async fn $test() {
|
|
let store_type = tests_integration::test_util::StorageType::File;
|
|
// Support both unit tests and fallible tests without discarding errors.
|
|
let result = $crate::http::$test(store_type).await;
|
|
assert_eq!(
|
|
std::process::Termination::report(result),
|
|
std::process::ExitCode::SUCCESS,
|
|
);
|
|
}
|
|
)*
|
|
}
|
|
};
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! http_test {
|
|
($service:ident, $($(#[$meta:meta])* $test:ident),*,) => {
|
|
paste::item! {
|
|
mod [<integration_http_ $service:lower _test>] {
|
|
$(
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
$(
|
|
#[$meta]
|
|
)*
|
|
async fn [< $test >]() {
|
|
let store_type = tests_integration::test_util::StorageType::$service;
|
|
if store_type.test_on() {
|
|
// Support both unit tests and fallible tests without discarding errors.
|
|
let result = $crate::http::$test(store_type).await;
|
|
assert_eq!(
|
|
std::process::Termination::report(result),
|
|
std::process::ExitCode::SUCCESS,
|
|
);
|
|
}
|
|
}
|
|
)*
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! http_tests {
|
|
($($service:ident),*) => {
|
|
$(
|
|
http_test!(
|
|
$service,
|
|
|
|
test_http_auth,
|
|
test_sql_api,
|
|
test_http_analyze_stream_tql,
|
|
test_http_analyze_stream_scan_diagnostics,
|
|
test_http_sql_slow_query,
|
|
test_prometheus_promql_api,
|
|
test_prometheus_native_promql_route,
|
|
test_native_promql_route_plans_the_query,
|
|
test_promql_over_non_millisecond_physical_tables,
|
|
test_prometheus_label_replace_response,
|
|
test_prom_http_api,
|
|
test_prom_remote_compression_bomb_rejected,
|
|
test_prom_remote_write_decoded_body_charged,
|
|
test_influxdb_write_decoded_body_charged,
|
|
test_config_api,
|
|
test_dashboard_api,
|
|
test_prometheus_remote_write,
|
|
test_prometheus_remote_write_v2,
|
|
test_prometheus_remote_write_v2_native_histogram,
|
|
test_remote_write_v2_histogram_prometheus_native_route,
|
|
test_remote_write_v2_histogram_float_prefix_native_route,
|
|
test_remote_write_v2_histogram_count_and_group_native_route,
|
|
test_remote_write_v2_histogram_sum_over_range,
|
|
test_remote_write_v2_histogram_sum_and_avg_native_route,
|
|
test_remote_write_v2_histogram_avg_native_partitions,
|
|
test_remote_write_v2_histogram_sum_incompatible_layouts_warns,
|
|
test_remote_write_v2_histogram_native_annotations,
|
|
test_remote_write_v2_histogram_native_arithmetic,
|
|
test_remote_write_v2_histogram_native_composition,
|
|
test_remote_write_v2_histogram_flush_native_consistency,
|
|
test_remote_write_v2_histogram_native_partitions,
|
|
test_remote_write_v2_histogram_native_range_function,
|
|
test_prometheus_remote_write_batched,
|
|
test_prometheus_remote_write_batched_mixed_time_index_units,
|
|
test_prometheus_remote_write_batched_interceptor_time_index_units,
|
|
test_prometheus_remote_write_v2_batched_interceptor_time_index_units,
|
|
test_prometheus_remote_write_batched_microsecond_physical_table,
|
|
test_prometheus_remote_write_batched_conflicting_physical_selections,
|
|
test_prometheus_remote_special_labels,
|
|
test_prometheus_remote_schema_labels,
|
|
test_prometheus_remote_write_with_pipeline,
|
|
test_vm_proto_remote_write,
|
|
|
|
test_pipeline_api,
|
|
test_pipeline_name_in_header,
|
|
test_json_stream_ingestion,
|
|
test_plain_text_ingestion,
|
|
test_pipeline_auto_transform,
|
|
test_pipeline_auto_transform_with_select,
|
|
test_identity_pipeline,
|
|
test_identity_pipeline_with_null_column,
|
|
test_identity_pipeline_with_flatten,
|
|
test_identity_pipeline_with_custom_ts,
|
|
test_pipeline_dispatcher,
|
|
test_pipeline_suffix_template,
|
|
test_pipeline_context,
|
|
test_pipeline_with_vrl,
|
|
test_pipeline_vrl_host_access,
|
|
test_pipeline_ignore_errors_config,
|
|
test_pipeline_with_hint_vrl,
|
|
test_pipeline_one_to_many_vrl,
|
|
test_pipeline_2,
|
|
test_pipeline_skip_error,
|
|
test_pipeline_filter,
|
|
test_pipeline_create_table,
|
|
test_pipeline_index_options,
|
|
|
|
test_otlp_metrics_new,
|
|
test_otlp_exponential_histogram,
|
|
test_otlp_metric_translation_strategies,
|
|
test_otlp_metrics_resource_info_conflicts,
|
|
test_otlp_traces_v0,
|
|
test_otlp_traces_v1,
|
|
test_otlp_traces_v2,
|
|
test_otlp_traces_v1_entity_graph,
|
|
test_otlp_logs,
|
|
test_loki_pb_logs,
|
|
test_loki_pb_logs_with_pipeline,
|
|
test_loki_json_logs,
|
|
test_loki_json_logs_with_pipeline,
|
|
test_elasticsearch_logs,
|
|
test_elasticsearch_logs_with_index,
|
|
test_elasticsearch_pipeline_version,
|
|
test_splunk_logs,
|
|
test_splunk_raw,
|
|
test_log_query,
|
|
test_jaeger_query_api,
|
|
test_jaeger_v3_query_api,
|
|
test_jaeger_query_api_for_trace_v1,
|
|
test_jaeger_query_api_for_trace_v2,
|
|
test_jaeger_search_limit_returns_latest_traces,
|
|
|
|
test_influxdb_write,
|
|
test_influxdb_write_with_hints,
|
|
test_influxdb_write_with_append_mode_hint,
|
|
test_http_memory_limit,
|
|
);
|
|
)*
|
|
};
|
|
}
|
|
|
|
pub async fn test_http_auth(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
|
|
let user_provider = user_provider_from_option(
|
|
"static_user_provider:cmd:greptime_user=greptime_pwd,readonly_user:ro=readonly_pwd,writeonly_user:wo=writeonly_pwd",
|
|
)
|
|
.unwrap();
|
|
|
|
let (app, mut guard) = setup_test_http_app_with_frontend_and_user_provider(
|
|
store_type,
|
|
"sql_api",
|
|
Some(user_provider),
|
|
)
|
|
.await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// 1. no auth
|
|
let res = client
|
|
.get("/v1/sql?db=public&sql=show tables;")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
|
|
|
|
// 2. wrong auth
|
|
let res = client
|
|
.get("/v1/sql?db=public&sql=show tables;")
|
|
.header("Authorization", basic_auth("greptime_user", "wrong_pwd"))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
|
|
|
|
// 3. right auth
|
|
let res = client
|
|
.get("/v1/sql?db=public&sql=show tables;")
|
|
.header("Authorization", basic_auth("greptime_user", "greptime_pwd"))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 4. readonly user cannot write
|
|
let res = client
|
|
.get("/v1/sql?db=public&sql=show tables;")
|
|
.header("Authorization", basic_auth("readonly_user", "readonly_pwd"))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let res = client
|
|
.get("/v1/sql?db=public&sql=create table auth_test(ts timestamp time index);")
|
|
.header("Authorization", basic_auth("readonly_user", "readonly_pwd"))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::FORBIDDEN);
|
|
|
|
// 5. writeonly user cannot read
|
|
let res = client
|
|
.get("/v1/sql?db=public&sql=show tables;")
|
|
.header(
|
|
"Authorization",
|
|
basic_auth("writeonly_user", "writeonly_pwd"),
|
|
)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::FORBIDDEN);
|
|
let res = client
|
|
.get("/v1/sql?db=public&sql=create table auth_test(ts timestamp time index);")
|
|
.header(
|
|
"Authorization",
|
|
basic_auth("writeonly_user", "writeonly_pwd"),
|
|
)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let actor_query_client = &client;
|
|
assert_procedure_actor_by_table(
|
|
"create_table",
|
|
"auth_test",
|
|
"writeonly_user",
|
|
|query| async move {
|
|
let res = actor_query_client
|
|
.get(&format!("/v1/sql?db=public&sql={}", encode(&query)))
|
|
.header("Authorization", basic_auth("greptime_user", "greptime_pwd"))
|
|
.send()
|
|
.await;
|
|
let Ok(body) = serde_json::from_str::<GreptimedbV1Response>(&res.text().await) else {
|
|
return false;
|
|
};
|
|
matches!(
|
|
body.output().first(),
|
|
Some(GreptimeQueryOutput::Records(records))
|
|
if records.rows().first().and_then(|row| row.first()).and_then(Value::as_bool)
|
|
== Some(true)
|
|
)
|
|
},
|
|
)
|
|
.await;
|
|
let res = client
|
|
.get("/v1/sql?db=public&sql=insert into auth_test values(1);")
|
|
.header(
|
|
"Authorization",
|
|
basic_auth("writeonly_user", "writeonly_pwd"),
|
|
)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let res = client
|
|
.get("/v1/sql?db=public&sql=select * from auth_test;")
|
|
.header(
|
|
"Authorization",
|
|
basic_auth("writeonly_user", "writeonly_pwd"),
|
|
)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::FORBIDDEN);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
fn basic_auth(username: &str, password: &str) -> String {
|
|
format!(
|
|
"basic {}",
|
|
BASE64_STANDARD.encode(format!("{username}:{password}"))
|
|
)
|
|
}
|
|
|
|
fn assert_remote_write_v2_written_headers(headers: &HeaderMap, samples: &str) {
|
|
assert_remote_write_v2_written_headers_with_histograms(headers, samples, "0")
|
|
}
|
|
|
|
fn assert_remote_write_v2_written_headers_with_histograms(
|
|
headers: &HeaderMap,
|
|
samples: &str,
|
|
histograms: &str,
|
|
) {
|
|
assert_eq!(
|
|
Some(samples),
|
|
headers
|
|
.get("X-Prometheus-Remote-Write-Samples-Written")
|
|
.map(|x| x.to_str().unwrap())
|
|
);
|
|
assert_eq!(
|
|
Some(histograms),
|
|
headers
|
|
.get("X-Prometheus-Remote-Write-Histograms-Written")
|
|
.map(|x| x.to_str().unwrap())
|
|
);
|
|
assert_eq!(
|
|
Some("0"),
|
|
headers
|
|
.get("X-Prometheus-Remote-Write-Exemplars-Written")
|
|
.map(|x| x.to_str().unwrap())
|
|
);
|
|
}
|
|
|
|
pub async fn test_http_auth_from_standalone_user_provider_config() {
|
|
common_telemetry::init_default_ut_logging();
|
|
|
|
let config = tempfile::NamedTempFile::new().unwrap();
|
|
let user_provider = "static_user_provider:cmd:greptime_user=greptime_pwd";
|
|
std::fs::write(
|
|
config.path(),
|
|
format!("user_provider = \"{user_provider}\"\n"),
|
|
)
|
|
.unwrap();
|
|
|
|
let options =
|
|
GreptimeOptions::<StandaloneOptions>::load_layered_options(config.path().to_str(), "")
|
|
.unwrap();
|
|
let fe_opts = options.component.frontend_options();
|
|
|
|
let mut plugins = Plugins::new();
|
|
plugins.insert(StandaloneFlag);
|
|
plugins::setup_frontend_plugins_pre_build(&mut plugins, &[], &fe_opts, None)
|
|
.await
|
|
.unwrap();
|
|
let user_provider = plugins.get::<UserProviderRef>();
|
|
|
|
let (app, mut guard) = setup_test_http_app_with_frontend_and_user_provider(
|
|
StorageType::File,
|
|
"sql_api_user_provider_config",
|
|
user_provider,
|
|
)
|
|
.await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let res = client
|
|
.get("/v1/sql?db=public&sql=show tables;")
|
|
.send()
|
|
.await;
|
|
let status = res.status();
|
|
let body = res.text().await;
|
|
assert_eq!(
|
|
status,
|
|
StatusCode::UNAUTHORIZED,
|
|
"unexpected response body: {body}"
|
|
);
|
|
|
|
let res = client
|
|
.get("/v1/sql?db=public&sql=show tables;")
|
|
.header("Authorization", basic_auth("greptime_user", "greptime_pwd"))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
pub async fn test_cors() {
|
|
let (app, mut guard) = setup_test_http_app_with_frontend(StorageType::File, "test_cors").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let res = client.get("/health").send().await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
assert_eq!(
|
|
res.headers()
|
|
.get(http::header::ACCESS_CONTROL_ALLOW_ORIGIN)
|
|
.expect("expect cors header origin"),
|
|
"*"
|
|
);
|
|
|
|
let res = client
|
|
.options("/health")
|
|
.header("Access-Control-Request-Headers", "x-greptime-auth")
|
|
.header("Access-Control-Request-Method", "DELETE")
|
|
.header("Origin", "https://example.com")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
assert_eq!(
|
|
res.headers()
|
|
.get(http::header::ACCESS_CONTROL_ALLOW_ORIGIN)
|
|
.expect("expect cors header origin"),
|
|
"*"
|
|
);
|
|
assert_eq!(
|
|
res.headers()
|
|
.get(http::header::ACCESS_CONTROL_ALLOW_HEADERS)
|
|
.expect("expect cors header headers"),
|
|
"*"
|
|
);
|
|
assert_eq!(
|
|
res.headers()
|
|
.get(http::header::ACCESS_CONTROL_ALLOW_METHODS)
|
|
.expect("expect cors header methods"),
|
|
"GET,POST,PUT,DELETE,HEAD"
|
|
);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
#[apply(both_deployment_cases)]
|
|
async fn test_sql_skip_wal(distributed: bool) {
|
|
let mut cases = vec![HttpWalCase::new(
|
|
"SQL",
|
|
"/v1/sql",
|
|
"application/x-www-form-urlencoded",
|
|
"sql=INSERT+INTO+wal_sql+VALUES+(1%2C-1)%2C(2%2C1)",
|
|
)];
|
|
let mut sql_get = HttpWalCase::new(
|
|
"SQL GET",
|
|
"/v1/sql?sql=INSERT%20INTO%20wal_sql%20VALUES%20(1%2C-1)%2C(2%2C1)",
|
|
"application/x-www-form-urlencoded",
|
|
Vec::new(),
|
|
);
|
|
sql_get.get = true;
|
|
cases.push(sql_get);
|
|
for case in &mut cases {
|
|
case.create_table = Some(if distributed {
|
|
"CREATE TABLE IF NOT EXISTS wal_sql (ts TIMESTAMP TIME INDEX, v INT) PARTITION ON COLUMNS (v) (v < 0, v >= 0)"
|
|
} else {
|
|
"CREATE TABLE IF NOT EXISTS wal_sql (ts TIMESTAMP TIME INDEX, v INT)"
|
|
});
|
|
case.expected_written_nodes = distributed.then_some(2);
|
|
}
|
|
check_http_skip_wal("sql", &cases, distributed).await;
|
|
}
|
|
|
|
fn logical_ddl(name: &str) -> String {
|
|
format!(
|
|
"CREATE TABLE IF NOT EXISTS {name} (ts TIMESTAMP TIME INDEX, host STRING PRIMARY KEY, val DOUBLE) ENGINE=metric WITH (on_physical_table='phy.dot')"
|
|
)
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn logical_ddl_http_standalone() {
|
|
check_logical_ddl_http(false).await;
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn logical_ddl_http_distributed() {
|
|
check_logical_ddl_http(true).await;
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
#[ignore = "manual logical DDL throughput measurement"]
|
|
async fn logical_ddl_http_benchmark() {
|
|
common_telemetry::init_default_ut_logging();
|
|
let count: usize = std::env::var("GT_LOGICAL_DDL_BENCH_TABLES")
|
|
.unwrap_or_else(|_| "1000".to_string())
|
|
.parse()
|
|
.unwrap();
|
|
for (name, endpoint, batch_size) in [
|
|
("single", "/v1/sql", 1),
|
|
("multi_sql", "/v1/sql", 128),
|
|
("batch", "/v1/ddl/logical-tables", 128),
|
|
] {
|
|
let mut instance =
|
|
MockInstanceImpl::Standalone(
|
|
tests_integration::standalone::GreptimeDbStandaloneBuilder::new(&format!(
|
|
"logical_ddl_bench_{name}"
|
|
))
|
|
.with_event_recorder_options(
|
|
tests_integration::test_util::test_event_recorder_options(),
|
|
)
|
|
.build()
|
|
.await,
|
|
);
|
|
let server = HttpServerBuilder::new(HttpOptions::default())
|
|
.with_sql_handler(instance.frontend())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
let response = execute_sql(&client, "CREATE TABLE \"phy.dot\" (ts TIMESTAMP TIME INDEX, host STRING PRIMARY KEY, val DOUBLE) ENGINE=metric WITH (physical_metric_table='')").await;
|
|
assert!(response.status().is_success(), "{}", response.text().await);
|
|
let statements = (0..count)
|
|
.map(|i| logical_ddl(&format!("bench_{i}")))
|
|
.collect::<Vec<_>>();
|
|
let start = std::time::Instant::now();
|
|
for chunk in statements.chunks(batch_size) {
|
|
let response = client
|
|
.post(endpoint)
|
|
.form(&[("sql", chunk.join(";"))])
|
|
.send()
|
|
.await;
|
|
assert!(response.status().is_success(), "{}", response.text().await);
|
|
assert_eq!(
|
|
response.json::<Value>().await["output"]
|
|
.as_array()
|
|
.unwrap()
|
|
.len(),
|
|
chunk.len()
|
|
);
|
|
}
|
|
let elapsed = start.elapsed();
|
|
let requests = count.div_ceil(batch_size);
|
|
let procedures = if name == "batch" { requests } else { count };
|
|
wait_for_event_data(&client,
|
|
"SELECT count(DISTINCT procedure_id), count(DISTINCT table_name) FROM greptime_private.events WHERE type='create_logical_tables' AND json_get_string(procedure_trigger, 'type')='Submitted' AND table_name LIKE 'bench_%'",
|
|
&format!("[[{procedures},{count}]]")).await;
|
|
common_telemetry::info!(
|
|
"Logical DDL: mode={name}, tables={count}, concurrency=1, HTTP requests={requests}, procedures={procedures}, elapsed={elapsed:?}"
|
|
);
|
|
instance.shutdown().await;
|
|
}
|
|
}
|
|
|
|
async fn check_logical_ddl_http(distributed: bool) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let events = tests_integration::test_util::test_event_recorder_options();
|
|
let mut instance = if distributed {
|
|
MockInstanceImpl::Distributed(
|
|
tests_integration::cluster::GreptimeDbClusterBuilder::new("batch_logical_ddl")
|
|
.await
|
|
.with_event_recorder_options(events)
|
|
.with_datanodes(3)
|
|
.build(true)
|
|
.await,
|
|
)
|
|
} else {
|
|
MockInstanceImpl::Standalone(
|
|
tests_integration::standalone::GreptimeDbStandaloneBuilder::new("batch_logical_ddl")
|
|
.with_event_recorder_options(events)
|
|
.build()
|
|
.await,
|
|
)
|
|
};
|
|
let frontend = instance.frontend();
|
|
let server = HttpServerBuilder::new(HttpOptions::default())
|
|
.with_sql_handler(frontend.clone())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
assert_eq!(
|
|
client
|
|
.get("/v1/capabilities")
|
|
.send()
|
|
.await
|
|
.json::<Value>()
|
|
.await["metric_batch_ddl"],
|
|
1
|
|
);
|
|
let response = execute_sql(&client, "CREATE TABLE \"phy.dot\" (ts TIMESTAMP TIME INDEX, host STRING PRIMARY KEY, val DOUBLE) ENGINE=metric WITH (physical_metric_table='')").await;
|
|
assert!(response.status().is_success(), "{}", response.text().await);
|
|
|
|
for sql in [
|
|
String::new(),
|
|
format!("{}; SELECT 1", logical_ddl("rejected")),
|
|
format!("{}; CREATE VIEW v AS SELECT 1", logical_ddl("rejected")),
|
|
format!(
|
|
"{}; CREATE TABLE copy LIKE batch_0",
|
|
logical_ddl("rejected")
|
|
),
|
|
format!("{}; CREATE TABLE copy AS SELECT 1", logical_ddl("rejected")),
|
|
logical_ddl("rejected").replace("WITH (", "WITH (physical_metric_table='', "),
|
|
format!(
|
|
"{}; CREATE TABLE ordinary (ts TIMESTAMP TIME INDEX)",
|
|
logical_ddl("rejected")
|
|
),
|
|
format!(
|
|
"{};{}",
|
|
logical_ddl("rejected"),
|
|
logical_ddl("public.rejected")
|
|
),
|
|
format!("{};{}", logical_ddl("rejected"), logical_ddl("other.t")),
|
|
format!(
|
|
"{};{}",
|
|
logical_ddl("rejected"),
|
|
logical_ddl("other_physical").replace("phy.dot", "other")
|
|
),
|
|
(0..129)
|
|
.map(|i| logical_ddl(&format!("rejected_{i}")))
|
|
.collect::<Vec<_>>()
|
|
.join(";"),
|
|
] {
|
|
let response = client
|
|
.post("/v1/ddl/logical-tables")
|
|
.form(&[("sql", sql)])
|
|
.send()
|
|
.await;
|
|
assert!(!response.status().is_success(), "{}", response.text().await);
|
|
}
|
|
validate_data(
|
|
"batch_rejected",
|
|
&client,
|
|
"SELECT count(*) FROM information_schema.tables WHERE table_name LIKE 'rejected%'",
|
|
"[[0]]",
|
|
)
|
|
.await;
|
|
|
|
let sql = (0..128)
|
|
.map(|i| logical_ddl(&format!("batch_{i}")))
|
|
.collect::<Vec<_>>()
|
|
.join(";");
|
|
let response = client
|
|
.post("/v1/ddl/logical-tables")
|
|
.form(&[("sql", sql)])
|
|
.send()
|
|
.await;
|
|
assert!(response.status().is_success(), "{}", response.text().await);
|
|
assert_eq!(
|
|
response.json::<Value>().await["output"],
|
|
json!(vec![json!({"affectedrows": 0}); 128])
|
|
);
|
|
wait_for_event_data(&client,
|
|
"SELECT count(DISTINCT procedure_id), count(DISTINCT table_name) FROM greptime_private.events WHERE type='create_logical_tables' AND json_get_string(procedure_trigger, 'type')='Submitted' AND table_name LIKE 'batch_%'",
|
|
"[[1,128]]").await;
|
|
|
|
let physical_id = frontend
|
|
.catalog_manager()
|
|
.table("greptime", "public", "phy.dot", None)
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.table_info()
|
|
.table_id();
|
|
let mut ids = std::collections::HashSet::new();
|
|
for i in 0..128 {
|
|
let table = frontend
|
|
.catalog_manager()
|
|
.table("greptime", "public", &format!("batch_{i}"), None)
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
let id = table.table_info().table_id();
|
|
assert!(ids.insert(id));
|
|
assert_ne!(id, physical_id);
|
|
let bound_physical_id = frontend
|
|
.table_metadata_manager()
|
|
.table_route_manager()
|
|
.get_physical_table_id(id)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(bound_physical_id, physical_id);
|
|
assert_eq!(
|
|
table
|
|
.table_info()
|
|
.meta
|
|
.options
|
|
.extra_options
|
|
.get("on_physical_table")
|
|
.map(String::as_str),
|
|
Some("phy.dot")
|
|
);
|
|
assert_eq!(table.schema().column_schemas().len(), 3);
|
|
}
|
|
let sql = logical_ddl("one_at_byte_limit");
|
|
let padded = format!("{sql}{}", " ".repeat(1024 * 1024 - sql.len()));
|
|
let response = client
|
|
.post("/v1/ddl/logical-tables")
|
|
.form(&[("sql", padded)])
|
|
.send()
|
|
.await;
|
|
assert!(response.status().is_success(), "{}", response.text().await);
|
|
|
|
let response = execute_sql(&client, "CREATE DATABASE inherited WITH ('skip_wal'='true', ttl='1h', 'compaction.type'='twcs'); CREATE TABLE inherited.phy (ts TIMESTAMP TIME INDEX, host STRING PRIMARY KEY, val DOUBLE) ENGINE=metric WITH (physical_metric_table='')").await;
|
|
assert!(response.status().is_success(), "{}", response.text().await);
|
|
let inherited = logical_ddl("inherited.ordinary").replace("phy.dot", "phy");
|
|
let response = execute_sql(&client, &inherited).await;
|
|
assert!(response.status().is_success(), "{}", response.text().await);
|
|
let sql = format!(
|
|
"{};{}",
|
|
logical_ddl("batched").replace("phy.dot", "phy"),
|
|
logical_ddl("explicit")
|
|
.replace("phy.dot", "phy")
|
|
.replace("WITH (", "WITH (skip_wal='false', ")
|
|
);
|
|
let response = client
|
|
.post("/v1/ddl/logical-tables")
|
|
.form(&[("sql", sql.as_str()), ("db", "greptime-inherited")])
|
|
.send()
|
|
.await;
|
|
assert!(response.status().is_success(), "{}", response.text().await);
|
|
let catalog = frontend.catalog_manager();
|
|
let ordinary = catalog
|
|
.table("greptime", "inherited", "ordinary", None)
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
let batched = catalog
|
|
.table("greptime", "inherited", "batched", None)
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
let explicit = catalog
|
|
.table("greptime", "inherited", "explicit", None)
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
assert_eq!(
|
|
ordinary.table_info().meta.options,
|
|
batched.table_info().meta.options
|
|
);
|
|
assert!(batched.table_info().meta.options.skip_wal);
|
|
assert!(!explicit.table_info().meta.options.skip_wal);
|
|
assert!(
|
|
!batched
|
|
.table_info()
|
|
.meta
|
|
.options
|
|
.extra_options
|
|
.contains_key("compaction.type")
|
|
);
|
|
|
|
let response = execute_sql(&client, "CREATE TABLE partitioned (ts TIMESTAMP TIME INDEX, host STRING PRIMARY KEY, val DOUBLE) PARTITION ON COLUMNS (host) (host < 'm', host >= 'm') ENGINE=metric WITH (physical_metric_table='')").await;
|
|
assert!(response.status().is_success(), "{}", response.text().await);
|
|
let partitioned = logical_ddl("bad_partition")
|
|
.replace("phy.dot", "partitioned")
|
|
.replace(
|
|
"ENGINE=",
|
|
"PARTITION ON COLUMNS (host) (host < 'n', host >= 'n') ENGINE=",
|
|
);
|
|
let response = execute_sql(&client, &partitioned).await;
|
|
assert!(!response.status().is_success());
|
|
let sql = format!(
|
|
"{};{partitioned}",
|
|
logical_ddl("before_bad_partition").replace("phy.dot", "partitioned")
|
|
);
|
|
let response = client
|
|
.post("/v1/ddl/logical-tables")
|
|
.form(&[("sql", sql)])
|
|
.send()
|
|
.await;
|
|
assert!(!response.status().is_success());
|
|
assert!(
|
|
catalog
|
|
.table("greptime", "public", "before_bad_partition", None)
|
|
.await
|
|
.unwrap()
|
|
.is_none()
|
|
);
|
|
|
|
let matching = partitioned
|
|
.replace("bad_partition", "matching_partition")
|
|
.replace("'n'", "'m'");
|
|
let response = execute_sql(&client, &matching).await;
|
|
assert!(response.status().is_success(), "{}", response.text().await);
|
|
let sql = format!(
|
|
"{};{}",
|
|
matching.replace("matching_partition", "batch_matching_partition"),
|
|
logical_ddl("batch_inherited_partition").replace("phy.dot", "partitioned")
|
|
);
|
|
let response = client
|
|
.post("/v1/ddl/logical-tables")
|
|
.form(&[("sql", sql)])
|
|
.send()
|
|
.await;
|
|
assert!(response.status().is_success(), "{}", response.text().await);
|
|
|
|
let original_id = frontend
|
|
.catalog_manager()
|
|
.table("greptime", "public", "batch_0", None)
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.table_info()
|
|
.table_id();
|
|
let sql = format!("{};{}", logical_ddl("batch_0"), logical_ddl("batch_new"));
|
|
for _ in 0..2 {
|
|
let response = client
|
|
.post("/v1/ddl/logical-tables")
|
|
.form(&[("sql", &sql)])
|
|
.send()
|
|
.await;
|
|
assert!(response.status().is_success(), "{}", response.text().await);
|
|
}
|
|
let old = frontend
|
|
.catalog_manager()
|
|
.table("greptime", "public", "batch_0", None)
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
assert_eq!(original_id, old.table_info().table_id());
|
|
let response = execute_sql(
|
|
&client,
|
|
"INSERT INTO batch_0 (ts, host, val) VALUES (1, 'a', 2.0); INSERT INTO batch_new (ts, host, val) VALUES (2, 'b', 3.0)",
|
|
)
|
|
.await;
|
|
assert!(response.status().is_success(), "{}", response.text().await);
|
|
validate_data(
|
|
"batch_values",
|
|
&client,
|
|
"SELECT val FROM batch_0 UNION ALL SELECT val FROM batch_new ORDER BY val",
|
|
"[[2.0],[3.0]]",
|
|
)
|
|
.await;
|
|
let ctx = session::context::QueryContext::arc();
|
|
let provider =
|
|
auth::static_user_provider_from_option("static_user_provider:cmd:readonly:ro=password")
|
|
.unwrap();
|
|
let user = auth::UserProvider::authenticate(
|
|
&provider,
|
|
auth::Identity::UserId("readonly", None),
|
|
auth::Password::PlainText("password".to_string().into()),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
ctx.set_current_user(user);
|
|
frontend
|
|
.plugins()
|
|
.get_or_insert::<auth::PermissionCheckerRef, _>(auth::DefaultPermissionChecker::arc);
|
|
let result = servers::query_handler::sql::SqlQueryHandler::create_logical_tables(
|
|
frontend.as_ref(),
|
|
&logical_ddl("read_only_denied"),
|
|
ctx,
|
|
)
|
|
.await;
|
|
assert!(result.is_err());
|
|
assert!(
|
|
frontend
|
|
.catalog_manager()
|
|
.table("greptime", "public", "read_only_denied", None)
|
|
.await
|
|
.unwrap()
|
|
.is_none()
|
|
);
|
|
|
|
struct FailAfterExecution(&'static str);
|
|
impl servers::interceptor::SqlQueryInterceptor for FailAfterExecution {
|
|
type Error = frontend::error::Error;
|
|
fn post_execute(
|
|
&self,
|
|
_: common_query::Output,
|
|
_: QueryContextRef,
|
|
) -> frontend::error::Result<common_query::Output> {
|
|
use common_query::Output;
|
|
match self.0 {
|
|
"rows" => Ok(Output::new_with_affected_rows(1)),
|
|
"records" => Ok(Output::new_with_record_batches(
|
|
common_recordbatch::RecordBatches::empty(),
|
|
)),
|
|
"stream" => Ok(Output::new_with_stream(Box::pin(
|
|
common_recordbatch::RecordBatchStreamWrapper::new(
|
|
Arc::new(datatypes::schema::Schema::new(vec![])),
|
|
futures::stream::iter([Err::<common_recordbatch::RecordBatch, _>(
|
|
common_recordbatch::error::CreateRecordBatchesSnafu {
|
|
reason: "sensitive-option-value",
|
|
}
|
|
.build(),
|
|
)]),
|
|
),
|
|
))),
|
|
_ => frontend::error::InvalidSqlSnafu {
|
|
err_msg: "sensitive-option-value",
|
|
}
|
|
.fail(),
|
|
}
|
|
}
|
|
}
|
|
frontend
|
|
.plugins()
|
|
.insert::<servers::interceptor::SqlQueryInterceptorRef<frontend::error::Error>>(Arc::new(
|
|
FailAfterExecution("error"),
|
|
));
|
|
for mode in ["error", "rows", "records", "stream"] {
|
|
frontend
|
|
.plugins()
|
|
.map_mut::<servers::interceptor::SqlQueryInterceptorRef<frontend::error::Error>, _, _>(
|
|
|plugin| *plugin.unwrap() = Arc::new(FailAfterExecution(mode)),
|
|
);
|
|
let name = format!("created_before_error_{mode}");
|
|
let response = client
|
|
.post("/v1/ddl/logical-tables")
|
|
.form(&[("sql", logical_ddl(&name))])
|
|
.send()
|
|
.await;
|
|
assert!(!response.status().is_success(), "{mode}");
|
|
assert!(!format!("{:?}", response.headers()).contains("sensitive-option-value"));
|
|
let body = response.json::<Value>().await;
|
|
assert!(body.get("output").is_none());
|
|
assert_eq!(body["error"], "logical-table batch failed");
|
|
assert!(
|
|
frontend
|
|
.catalog_manager()
|
|
.table("greptime", "public", &name, None)
|
|
.await
|
|
.unwrap()
|
|
.is_some()
|
|
);
|
|
}
|
|
instance.shutdown().await;
|
|
}
|
|
|
|
pub async fn test_sql_api(store_type: StorageType) {
|
|
let (app, mut guard) = setup_test_http_app_with_frontend(store_type, "sql_api").await;
|
|
let client = TestClient::new(app).await;
|
|
let res = client.get("/v1/sql").send().await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
|
|
let body = serde_json::from_str::<ErrorResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.code(), 1004);
|
|
assert_eq!(body.error(), "sql parameter is required.");
|
|
|
|
let res = client
|
|
.get("/v1/sql?sql=select * from numbers limit 10")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let body = serde_json::from_str::<GreptimedbV1Response>(&res.text().await).unwrap();
|
|
let output = body.output();
|
|
assert_eq!(output.len(), 1);
|
|
assert_eq!(
|
|
output[0],
|
|
serde_json::from_value::<GreptimeQueryOutput>(json!({
|
|
"records" :{"schema":{"column_schemas":[{"name":"number","data_type":"UInt32"}]},"rows":[[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]],"total_rows":10}
|
|
})).unwrap()
|
|
);
|
|
|
|
// test influxdb_v1 result format
|
|
let res = client
|
|
.get("/v1/sql?format=influxdb_v1&sql=select * from numbers limit 10")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let body = serde_json::from_str::<InfluxdbV1Response>(&res.text().await).unwrap();
|
|
let output = body.results();
|
|
assert_eq!(output.len(), 1);
|
|
assert_eq!(
|
|
output[0],
|
|
serde_json::from_value::<InfluxdbOutput>(json!({
|
|
"statement_id":0,"series":[{"name":"","columns":["number"],"values":[[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]]}]
|
|
})).unwrap()
|
|
);
|
|
|
|
// test json result format
|
|
let res = client
|
|
.get("/v1/sql?format=json&sql=select * from numbers limit 10")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let body = res.json::<Value>().await;
|
|
let data = body.get("data").expect("Missing 'data' field in response");
|
|
|
|
let expected = json!([
|
|
{"number": 0}, {"number": 1}, {"number": 2}, {"number": 3}, {"number": 4},
|
|
{"number": 5}, {"number": 6}, {"number": 7}, {"number": 8}, {"number": 9}
|
|
]);
|
|
assert_eq!(data, &expected);
|
|
|
|
// test insert and select
|
|
let res = client
|
|
.get("/v1/sql?sql=insert into demo values('host, \"name', 66.6, 1024, 0)")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// select *
|
|
let res = client
|
|
.get("/v1/sql?sql=select * from demo limit 10")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let body = serde_json::from_str::<GreptimedbV1Response>(&res.text().await).unwrap();
|
|
let output = body.output();
|
|
assert_eq!(output.len(), 1);
|
|
|
|
assert_eq!(
|
|
output[0],
|
|
serde_json::from_value::<GreptimeQueryOutput>(json!({
|
|
"records":{"schema":{"column_schemas":[{"name":"host","data_type":"String"},{"name":"cpu","data_type":"Float64"},{"name":"memory","data_type":"Float64"},{"name":"ts","data_type":"TimestampMillisecond"}]},"rows":[["host, \"name",66.6,1024.0,0]],"total_rows":1}
|
|
})).unwrap()
|
|
);
|
|
|
|
// select with projections
|
|
let res = client
|
|
.get("/v1/sql?sql=select cpu, ts from demo limit 10")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let body = serde_json::from_str::<GreptimedbV1Response>(&res.text().await).unwrap();
|
|
let output = body.output();
|
|
assert_eq!(output.len(), 1);
|
|
|
|
assert_eq!(
|
|
output[0],
|
|
serde_json::from_value::<GreptimeQueryOutput>(json!({
|
|
"records":{"schema":{"column_schemas":[{"name":"cpu","data_type":"Float64"},{"name":"ts","data_type":"TimestampMillisecond"}]},"rows":[[66.6,0]],"total_rows":1}
|
|
})).unwrap()
|
|
);
|
|
|
|
// select with column alias
|
|
let res = client
|
|
.get("/v1/sql?sql=select cpu as c, ts as time from demo limit 10")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let body = serde_json::from_str::<GreptimedbV1Response>(&res.text().await).unwrap();
|
|
let output = body.output();
|
|
assert_eq!(output.len(), 1);
|
|
assert_eq!(
|
|
output[0],
|
|
serde_json::from_value::<GreptimeQueryOutput>(json!({
|
|
"records":{"schema":{"column_schemas":[{"name":"c","data_type":"Float64"},{"name":"time","data_type":"TimestampMillisecond"}]},"rows":[[66.6,0]],"total_rows":1}
|
|
})).unwrap()
|
|
);
|
|
|
|
// test multi-statement
|
|
let res = client
|
|
.get("/v1/sql?sql=select cpu, ts from demo limit 1;select cpu, ts from demo where ts > 0;")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let body = serde_json::from_str::<GreptimedbV1Response>(&res.text().await).unwrap();
|
|
let outputs = body.output();
|
|
assert_eq!(outputs.len(), 2);
|
|
assert_eq!(
|
|
outputs[0],
|
|
serde_json::from_value::<GreptimeQueryOutput>(json!({
|
|
"records":{"schema":{"column_schemas":[{"name":"cpu","data_type":"Float64"},{"name":"ts","data_type":"TimestampMillisecond"}]},"rows":[[66.6,0]],"total_rows":1}
|
|
})).unwrap()
|
|
);
|
|
assert_eq!(
|
|
outputs[1],
|
|
serde_json::from_value::<GreptimeQueryOutput>(json!({
|
|
"records":{"rows":[], "schema":{"column_schemas":[{"name":"cpu","data_type":"Float64"},{"name":"ts","data_type":"TimestampMillisecond"}]}, "total_rows":0}
|
|
}))
|
|
.unwrap()
|
|
);
|
|
|
|
// test multi-statement with error
|
|
let res = client
|
|
.get("/v1/sql?sql=select cpu, ts from demo limit 1;select cpu, ts from demo2 where ts > 0;")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
|
|
let body = serde_json::from_str::<ErrorResponse>(&res.text().await).unwrap();
|
|
assert!(body.error().contains("Table not found"));
|
|
assert_eq!(body.code(), ErrorCode::TableNotFound as u32);
|
|
|
|
// test database given
|
|
let res = client
|
|
.get("/v1/sql?db=public&sql=select cpu, ts from demo limit 1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let body = serde_json::from_str::<GreptimedbV1Response>(&res.text().await).unwrap();
|
|
let outputs = body.output();
|
|
assert_eq!(outputs.len(), 1);
|
|
assert_eq!(
|
|
outputs[0],
|
|
serde_json::from_value::<GreptimeQueryOutput>(json!({
|
|
"records":{"schema":{"column_schemas":[{"name":"cpu","data_type":"Float64"},{"name":"ts","data_type":"TimestampMillisecond"}]},"rows":[[66.6,0]],"total_rows":1}
|
|
})).unwrap()
|
|
);
|
|
|
|
// test database not found
|
|
let res = client
|
|
.get("/v1/sql?db=notfound&sql=select cpu, ts from demo limit 1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
let body = serde_json::from_str::<ErrorResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.code(), ErrorCode::DatabaseNotFound as u32);
|
|
|
|
// test catalog-schema given
|
|
let res = client
|
|
.get("/v1/sql?db=greptime-public&sql=select cpu, ts from demo limit 1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let body = serde_json::from_str::<GreptimedbV1Response>(&res.text().await).unwrap();
|
|
let outputs = body.output();
|
|
assert_eq!(outputs.len(), 1);
|
|
assert_eq!(
|
|
outputs[0],
|
|
serde_json::from_value::<GreptimeQueryOutput>(json!({
|
|
"records":{"schema":{"column_schemas":[{"name":"cpu","data_type":"Float64"},{"name":"ts","data_type":"TimestampMillisecond"}]},"rows":[[66.6,0]],"total_rows":1}
|
|
})).unwrap()
|
|
);
|
|
|
|
// test invalid catalog
|
|
let res = client
|
|
.get("/v1/sql?db=notfound2-schema&sql=select cpu, ts from demo limit 1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
let body = serde_json::from_str::<ErrorResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.code(), ErrorCode::DatabaseNotFound as u32);
|
|
|
|
// test invalid schema
|
|
let res = client
|
|
.get("/v1/sql?db=greptime-schema&sql=select cpu, ts from demo limit 1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
let body = serde_json::from_str::<ErrorResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.code(), ErrorCode::DatabaseNotFound as u32);
|
|
|
|
// test analyze format
|
|
let res = client
|
|
.get("/v1/sql?sql=explain analyze format json select cpu, ts from demo limit 1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<GreptimedbV1Response>(&res.text().await).unwrap();
|
|
let output = body.output();
|
|
assert_eq!(output.len(), 1);
|
|
// this is something only json format can show
|
|
assert!(format!("{:?}", output[0]).contains("\\\"param\\\""));
|
|
|
|
// test csv format
|
|
let res = client
|
|
.get("/v1/sql?format=csv&sql=select cpu,ts,host from demo limit 1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = &res.text().await;
|
|
// Must be escaped correctly: 66.6,0,"host, ""name"
|
|
assert_eq!(body, "66.6,0,\"host, \"\"name\"\r\n");
|
|
|
|
// csv with names
|
|
let res = client
|
|
.get("/v1/sql?format=csvWithNames&sql=select cpu,ts,host from demo limit 1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = &res.text().await;
|
|
assert_eq!(body, "cpu,ts,host\r\n66.6,0,\"host, \"\"name\"\r\n");
|
|
|
|
// csv with names and types
|
|
let res = client
|
|
.get("/v1/sql?format=csvWithNamesAndTypes&sql=select cpu,ts,host from demo limit 1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = &res.text().await;
|
|
assert_eq!(
|
|
body,
|
|
"cpu,ts,host\r\nFloat64,TimestampMillisecond,String\r\n66.6,0,\"host, \"\"name\"\r\n"
|
|
);
|
|
// test null format
|
|
let res = client
|
|
.get("/v1/sql?format=null&sql=select cpu,ts,host from demo limit 1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = &res.text().await;
|
|
assert!(body.contains("1 rows in set."));
|
|
assert!(body.ends_with("sec.\n"));
|
|
|
|
// test parse method
|
|
let res = client.get("/v1/sql/parse?sql=desc table t").send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
assert_eq!(
|
|
res.text().await,
|
|
r#"[{"DescribeTable":{"name":[{"Identifier":{"value":"t","quote_style":null,"span":{"start":{"line":0,"column":0},"end":{"line":0,"column":0}}}}]}}]"#,
|
|
);
|
|
|
|
// test timezone header
|
|
let res = client
|
|
.get("/v1/sql?&sql=show variables system_time_zone")
|
|
.header(
|
|
TryInto::<HeaderName>::try_into(GREPTIME_TIMEZONE_HEADER_NAME.to_string()).unwrap(),
|
|
"Asia/Shanghai",
|
|
)
|
|
.send()
|
|
.await
|
|
.text()
|
|
.await;
|
|
assert!(res.contains("SYSTEM_TIME_ZONE") && res.contains("UTC"));
|
|
let res = client
|
|
.get("/v1/sql?&sql=show variables time_zone")
|
|
.header(
|
|
TryInto::<HeaderName>::try_into(GREPTIME_TIMEZONE_HEADER_NAME.to_string()).unwrap(),
|
|
"Asia/Shanghai",
|
|
)
|
|
.send()
|
|
.await
|
|
.text()
|
|
.await;
|
|
assert!(res.contains("TIME_ZONE") && res.contains("Asia/Shanghai"));
|
|
let res = client
|
|
.get("/v1/sql?&sql=show variables system_time_zone")
|
|
.send()
|
|
.await
|
|
.text()
|
|
.await;
|
|
assert!(res.contains("SYSTEM_TIME_ZONE") && res.contains("UTC"));
|
|
let res = client
|
|
.get("/v1/sql?&sql=show variables time_zone")
|
|
.send()
|
|
.await
|
|
.text()
|
|
.await;
|
|
assert!(res.contains("TIME_ZONE") && res.contains("UTC"));
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_sql_format_api() {
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(StorageType::File, "sql_format_api").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// missing sql
|
|
let res = client.get("/v1/sql/format").send().await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
|
|
let body = serde_json::from_str::<ErrorResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.code(), 1004);
|
|
assert_eq!(body.error(), "sql parameter is required.");
|
|
|
|
// with sql
|
|
let res = client
|
|
.get("/v1/sql/format?sql=select%201%20as%20x")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let json: serde_json::Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let formatted = json
|
|
.get("formatted")
|
|
.and_then(|v| v.as_str())
|
|
.unwrap_or_default();
|
|
assert_eq!(formatted, "SELECT 1 AS x;");
|
|
|
|
// complex query
|
|
let complex_query = "WITH RECURSIVE slow_cte AS (SELECT 1 AS n, md5(CAST(random() AS STRING)) AS hash UNION ALL SELECT n + 1, md5(concat(hash, n)) FROM slow_cte WHERE n < 4500) SELECT COUNT(*) FROM slow_cte";
|
|
let encoded_complex_query = encode(complex_query);
|
|
|
|
let query_params = format!("/v1/sql/format?sql={encoded_complex_query}");
|
|
let res = client.get(&query_params).send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let json: serde_json::Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let formatted = json
|
|
.get("formatted")
|
|
.and_then(|v| v.as_str())
|
|
.unwrap_or_default();
|
|
assert_eq!(
|
|
formatted,
|
|
"WITH RECURSIVE slow_cte AS (SELECT 1 AS n, md5(CAST(random() AS STRING)) AS hash UNION ALL SELECT n + 1, md5(concat(hash, n)) FROM slow_cte WHERE n < 4500) SELECT COUNT(*) FROM slow_cte;"
|
|
);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_http_analyze_stream_scan_diagnostics(store_type: StorageType) {
|
|
// Check both levels of JSON: the analyze node schema and the verbose scan
|
|
// object inside its string-valued `param`.
|
|
fn scan_details(node: &Value, scans: &mut Vec<Value>) {
|
|
assert!(node["name"].is_string());
|
|
assert!(node["param"].is_string());
|
|
assert!(node["output_rows"].is_u64());
|
|
assert!(node["elapsed_compute"].is_u64());
|
|
assert!(node["metrics"].is_object());
|
|
if matches!(
|
|
node["name"].as_str(),
|
|
Some("SeqScan" | "UnorderedScan" | "SeriesScan")
|
|
) {
|
|
let (_, fields) = node["param"].as_str().unwrap().split_once("), ").unwrap();
|
|
let fields = if fields.starts_with('{') {
|
|
fields.to_string()
|
|
} else {
|
|
format!("{{{fields}}}")
|
|
};
|
|
let details: Value = serde_json::from_str(&fields).unwrap();
|
|
assert_eq!(details["projection"], json!(["reading", "ts"]));
|
|
assert!(
|
|
details["filters"]
|
|
.as_array()
|
|
.is_some_and(|filters| !filters.is_empty())
|
|
);
|
|
assert_eq!(details["time_range"], "TimestampRange{[0ms,6000ms)}");
|
|
assert_eq!(details["file_summary"]["files"], 2);
|
|
assert_eq!(details["file_summary"]["rows"], 4);
|
|
assert_eq!(details["file_summary"]["row_groups"], 2);
|
|
assert_eq!(details["max_part_range"]["files"], 2);
|
|
assert!(details["max_part_range"].get("identifier").is_none());
|
|
scans.push(details);
|
|
}
|
|
for child in node["children"].as_array().unwrap() {
|
|
scan_details(child, scans);
|
|
}
|
|
}
|
|
fn assert_verbose(scans: &[Value]) {
|
|
assert!(!scans.is_empty());
|
|
for scan in scans {
|
|
let files = scan["files"].as_array().unwrap();
|
|
assert_eq!(files.len(), 2);
|
|
for key in ["rows", "size", "index_size"] {
|
|
assert_eq!(
|
|
scan["file_summary"][key],
|
|
files
|
|
.iter()
|
|
.map(|file| file[key].as_u64().unwrap())
|
|
.sum::<u64>()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
let (app, mut guard) = setup_test_http_app_with_frontend(store_type, "scan_diagnostics").await;
|
|
let client = TestClient::new(app).await;
|
|
for sql in [
|
|
"CREATE TABLE scan_diagnostics (host STRING PRIMARY KEY, reading DOUBLE, ts TIMESTAMP TIME INDEX)",
|
|
"INSERT INTO scan_diagnostics VALUES ('a', 1, 0), ('b', 2, 3000)",
|
|
"ADMIN FLUSH_TABLE('scan_diagnostics')",
|
|
"INSERT INTO scan_diagnostics VALUES ('c', 3, 2000), ('d', 4, 5000)",
|
|
"ADMIN FLUSH_TABLE('scan_diagnostics')",
|
|
"INSERT INTO scan_diagnostics VALUES ('outside', 5, 10000)",
|
|
"ADMIN FLUSH_TABLE('scan_diagnostics')",
|
|
"INSERT INTO scan_diagnostics VALUES ('mem', 6, 1000)",
|
|
] {
|
|
let response = execute_sql(&client, sql).await;
|
|
let status = response.status();
|
|
let body = response.text().await;
|
|
assert_eq!(status, StatusCode::OK, "{sql}: {body}");
|
|
let body: Value = serde_json::from_str(&body).unwrap();
|
|
assert!(body.get("error").is_none(), "{body}");
|
|
}
|
|
let query = "SELECT reading, ts FROM scan_diagnostics WHERE ts >= 0 AND ts < 6000";
|
|
let sql = format!("EXPLAIN ANALYZE FORMAT JSON {query}");
|
|
let response = execute_sql(&client, &sql).await;
|
|
let status = response.status();
|
|
let body = response.text().await;
|
|
assert_eq!(status, StatusCode::OK, "{body}");
|
|
let body: Value = serde_json::from_str(&body).unwrap();
|
|
let mut explain_scans = vec![];
|
|
for row in body["output"][0]["records"]["rows"].as_array().unwrap() {
|
|
if row[0].is_null() {
|
|
assert_eq!(row[2], "Total rows: 5");
|
|
continue;
|
|
}
|
|
let plan: Value = serde_json::from_str(row[2].as_str().unwrap()).unwrap();
|
|
scan_details(&plan, &mut explain_scans);
|
|
}
|
|
assert!(!explain_scans.is_empty());
|
|
assert!(explain_scans.iter().all(|scan| scan.get("files").is_none()));
|
|
|
|
let sql = format!("EXPLAIN ANALYZE VERBOSE FORMAT JSON {query}");
|
|
let response = client
|
|
.post("/v1/sql/analyze/stream?snapshot_interval_ms=100")
|
|
.header("Accept", "text/event-stream")
|
|
.form(&BTreeMap::from([("sql", sql)]))
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let body = response.text().await;
|
|
let mut first_scan_seen = false;
|
|
let mut final_seen = false;
|
|
for event in body.split("\n\n") {
|
|
let Some(data) = event.lines().find_map(|line| line.strip_prefix("data: ")) else {
|
|
continue;
|
|
};
|
|
let payload: Value = serde_json::from_str(data).unwrap();
|
|
let mut scans = vec![];
|
|
for stage in payload["metrics"].as_array().unwrap() {
|
|
assert!(stage["stage"].is_u64());
|
|
assert!(stage["node"].is_u64());
|
|
scan_details(&stage["plan"], &mut scans);
|
|
}
|
|
// Every available scan is checked, including its first appearance. A fast
|
|
// query may finish before the first periodic snapshot.
|
|
first_scan_seen |= !scans.is_empty();
|
|
if payload["state"] == "final" {
|
|
assert_verbose(&scans);
|
|
final_seen = true;
|
|
}
|
|
}
|
|
assert!(first_scan_seen && final_seen, "{body}");
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_http_analyze_stream_tql(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_prom_app_with_frontend(store_type, "analyze_stream_tql").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let res = client
|
|
.post("/v1/sql/analyze/stream")
|
|
.header("Accept", "text/event-stream")
|
|
.form(&BTreeMap::from([(
|
|
"sql".to_string(),
|
|
"TQL ANALYZE VERBOSE (0, 10, '5s') demo".to_string(),
|
|
)]))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
assert!(
|
|
res.headers()
|
|
.get("content-type")
|
|
.and_then(|value| value.to_str().ok())
|
|
.is_some_and(|value| value.starts_with("text/event-stream"))
|
|
);
|
|
let body = res.text().await;
|
|
let final_event = body
|
|
.split("\n\n")
|
|
.find(|event| event.lines().any(|line| line == "event: final"))
|
|
.expect(&body);
|
|
let payload = final_event
|
|
.lines()
|
|
.find_map(|line| line.strip_prefix("data: "))
|
|
.map(|data| serde_json::from_str::<Value>(data).unwrap())
|
|
.unwrap();
|
|
assert_eq!(payload["state"], "final");
|
|
assert!(payload["metrics"].as_array().is_some_and(|v| !v.is_empty()));
|
|
assert!(
|
|
payload["output"]["records"]["rows"]
|
|
.as_array()
|
|
.is_some_and(|v| !v.is_empty())
|
|
);
|
|
|
|
let res = client
|
|
.post("/v1/sql/analyze/stream")
|
|
.header("Accept", "text/event-stream")
|
|
.form(&BTreeMap::from([(
|
|
"sql".to_string(),
|
|
"TQL ANALYZE VERBOSE FORMAT JSON (0, 10, '5s') demo".to_string(),
|
|
)]))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
assert!(res.text().await.contains("event: final"));
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_http_sql_slow_query(store_type: StorageType) {
|
|
let (app, mut guard) = setup_test_http_app_with_frontend_and_slow_query_threshold(
|
|
store_type,
|
|
"sql_api",
|
|
Duration::from_millis(100),
|
|
)
|
|
.await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let slow_query = "SELECT count(*) FROM generate_series(1, 50000000)";
|
|
let encoded_slow_query = encode(slow_query);
|
|
|
|
let query_params = format!("/v1/sql?sql={encoded_slow_query}");
|
|
let res = client.get(&query_params).send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let table = format!("{}.{}", DEFAULT_PRIVATE_SCHEMA_NAME, SLOW_QUERY_TABLE_NAME);
|
|
let query = format!(
|
|
"SELECT {} FROM {table} WHERE {} = '{slow_query}'",
|
|
SLOW_QUERY_TABLE_QUERY_COLUMN_NAME, SLOW_QUERY_TABLE_QUERY_COLUMN_NAME
|
|
);
|
|
|
|
let expected = format!(r#"[["{}"]]"#, slow_query);
|
|
wait_for_data(&client, &query, &expected).await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_prometheus_promql_api(store_type: StorageType) {
|
|
let (app, mut guard) = setup_test_http_app_with_frontend(store_type, "promql_api").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let res = client
|
|
.get("/v1/promql?query=abs(demo{host=\"Hangzhou\"})&start=0&end=100&step=5s")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let json_text = res.text().await;
|
|
assert!(serde_json::from_str::<GreptimedbV1Response>(&json_text).is_ok());
|
|
|
|
let res = client
|
|
.get("/v1/promql?query=1&start=0&end=100&step=5s&format=csv")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let csv_body = &res.text().await;
|
|
assert_eq!(
|
|
"0,1.0\r\n5000,1.0\r\n10000,1.0\r\n15000,1.0\r\n20000,1.0\r\n25000,1.0\r\n30000,1.0\r\n35000,1.0\r\n40000,1.0\r\n45000,1.0\r\n50000,1.0\r\n55000,1.0\r\n60000,1.0\r\n65000,1.0\r\n70000,1.0\r\n75000,1.0\r\n80000,1.0\r\n85000,1.0\r\n90000,1.0\r\n95000,1.0\r\n100000,1.0\r\n",
|
|
csv_body
|
|
);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
/// Keeps the plan of the last PromQL request so a test can prove which route answered it.
|
|
///
|
|
/// The plan is only complete once the response body has been drained: a frontend plan keeps its
|
|
/// region stages behind `MergeScanExec`, whose `children()` is empty, and those stages fill in their
|
|
/// metrics while the body streams out. `take_plan` therefore runs after the body is consumed and
|
|
/// leaves nothing behind, so no request can read the plan of another one.
|
|
#[derive(Default)]
|
|
struct PromRouteObserver {
|
|
last_plan: Mutex<Option<Arc<dyn ExecutionPlan>>>,
|
|
}
|
|
|
|
impl PromQueryInterceptor for PromRouteObserver {
|
|
type Error = servers::error::Error;
|
|
|
|
fn post_execute(
|
|
&self,
|
|
output: common_query::Output,
|
|
_query_ctx: QueryContextRef,
|
|
) -> servers::error::Result<common_query::Output> {
|
|
*self.last_plan.lock().unwrap() = output.meta.plan.clone();
|
|
Ok(output)
|
|
}
|
|
}
|
|
|
|
impl PromRouteObserver {
|
|
/// Takes the plan of the request that just finished.
|
|
fn take_plan(&self) -> Arc<dyn ExecutionPlan> {
|
|
self.last_plan
|
|
.lock()
|
|
.unwrap()
|
|
.take()
|
|
.expect("the request finished without the observer seeing the plan it executed")
|
|
}
|
|
}
|
|
|
|
/// Every plan node of every stage of an executed plan, in plan order.
|
|
fn plan_nodes(plan: &Arc<dyn ExecutionPlan>) -> Vec<Value> {
|
|
fn push(node: &Value, nodes: &mut Vec<Value>) {
|
|
nodes.push(node.clone());
|
|
if let Some(children) = node["children"].as_array() {
|
|
for child in children {
|
|
push(child, nodes);
|
|
}
|
|
}
|
|
}
|
|
|
|
let stages = query::analyze_plan_metrics_to_json_value(plan, true)
|
|
.expect("an executed plan's metrics serialize to JSON");
|
|
let mut nodes = Vec::new();
|
|
for stage in stages
|
|
.as_array()
|
|
.expect("plan metrics are a list of stages")
|
|
{
|
|
push(&stage["plan"], &mut nodes);
|
|
}
|
|
nodes
|
|
}
|
|
|
|
/// The nodes of `plan` named `name`.
|
|
fn plan_nodes_named(plan: &Arc<dyn ExecutionPlan>, name: &str) -> Vec<Value> {
|
|
plan_nodes(plan)
|
|
.into_iter()
|
|
.filter(|node| node["name"] == name)
|
|
.collect()
|
|
}
|
|
|
|
/// Whether the executed plan answered on the native fused-series route.
|
|
///
|
|
/// Every stage is searched: on a vector query the fused node runs inside the region stage behind
|
|
/// `MergeScanExec`, while on a constant it is the frontend plan itself.
|
|
fn executes_fused_series(plan: &Arc<dyn ExecutionPlan>) -> bool {
|
|
!plan_nodes_named(plan, "FusedSeriesExec").is_empty()
|
|
}
|
|
|
|
/// One row of a PromQL result: its tag columns in schema order, its timestamp and its value.
|
|
type PromqlRow = (Vec<(String, String)>, i64, f64);
|
|
|
|
/// Checks that a native result plans one fused node, that a constant plans no scan and a vector one
|
|
/// scan exposing `scan_partitions` partitions, and returns the plan's view of the fused node.
|
|
///
|
|
/// `exec` is the fused node that really ran. For a vector it is the region-side node, which the JSON
|
|
/// view reports as a separate stage, so it is passed in rather than found in `plan`.
|
|
fn assert_native_plan(
|
|
plan: &Arc<dyn ExecutionPlan>,
|
|
exec: &Arc<dyn ExecutionPlan>,
|
|
scan_partitions: Option<usize>,
|
|
context: &str,
|
|
) -> Value {
|
|
let fused = {
|
|
let mut nodes = plan_nodes_named(plan, "FusedSeriesExec");
|
|
assert_eq!(
|
|
nodes.len(),
|
|
1,
|
|
"{context}: expected one fused node: {nodes:?}"
|
|
);
|
|
nodes.remove(0)
|
|
};
|
|
// The tree the node ran, as its own line renders it: `native_tree=[...]`. The JSON view reports
|
|
// each node of that tree as its own child, next to the plans the operator reads, so this is the
|
|
// one place the tree is still a single text.
|
|
let display = datafusion::physical_plan::displayable(exec.as_ref())
|
|
.indent(false)
|
|
.to_string();
|
|
let tree = display
|
|
.split_once("native_tree=[")
|
|
.map(|(_, tree)| tree.to_string())
|
|
.unwrap_or_else(|| panic!("{context}: the fused node renders no tree: {display}"));
|
|
let scans = plan_nodes_named(plan, "SeriesScanExec");
|
|
let Some(partitions) = scan_partitions else {
|
|
// A constant reads no frame: its one value per grid point comes from the tree alone, so the
|
|
// node has no child, plans no scan and binds no source.
|
|
assert!(
|
|
scans.is_empty(),
|
|
"{context}: a constant planned a scan: {scans:?}"
|
|
);
|
|
assert!(
|
|
exec.children().is_empty(),
|
|
"{context}: a constant's fused node reads a child: {fused}"
|
|
);
|
|
assert!(
|
|
!tree.contains("Input"),
|
|
"{context}: a constant's tree binds an input: {tree}"
|
|
);
|
|
return fused;
|
|
};
|
|
assert_eq!(
|
|
scans.len(),
|
|
1,
|
|
"{context}: a vector must read exactly one scan: {scans:?}"
|
|
);
|
|
// A fused node can read its region through a router and a merge scan, whose region half is
|
|
// dispatched rather than nested, so the scan it reads is the one this statement observed.
|
|
let scan = scans[0].clone();
|
|
assert_eq!(
|
|
scan["name"], "SeriesScanExec",
|
|
"{context}: the statement's scan is not a series scan: {scan}"
|
|
);
|
|
let scan_description = scan["param"].as_str().unwrap_or_default();
|
|
assert!(
|
|
scan_description.contains(&format!("partitions={partitions}")),
|
|
"{context}: the scan does not expose {partitions} partitions: {scan_description}"
|
|
);
|
|
// Both inputs of one selector name the one source, so a tree that reads two sources or none
|
|
// would have had to pair or gather them.
|
|
assert_eq!(
|
|
tree.matches("source=0").count(),
|
|
1,
|
|
"{context}: the tree does not read the one source: {tree}"
|
|
);
|
|
assert!(
|
|
!tree.contains("source=1"),
|
|
"{context}: the tree reads a second source: {tree}"
|
|
);
|
|
// What the node did is in its own set: the registrations name the partition they ran on, and the
|
|
// samples and values are the counts the tree recorded. Which partition did that work is the
|
|
// canonical router's hash over the series it was delivered, so these are "some partition"
|
|
// claims; the figure the statement fixes - how many values the partitions published between them
|
|
// - is asserted against the result by [`assert_native_partition_output`].
|
|
let metrics = node_metric_set(exec);
|
|
let published = metrics
|
|
.iter()
|
|
.filter(|metric| metric.value().name() == "native.root.output_values")
|
|
.map(|metric| metric.value().as_usize())
|
|
.sum::<usize>();
|
|
assert!(
|
|
published > 0,
|
|
"no partition of `{context}` published a value: {display}"
|
|
);
|
|
let read = metrics
|
|
.iter()
|
|
.filter(|metric| metric.value().name() == "native.root.input_samples")
|
|
.map(|metric| metric.value().as_usize())
|
|
.sum::<usize>();
|
|
assert!(
|
|
read > 0,
|
|
"no partition of `{context}` read any input sample: {display}"
|
|
);
|
|
fused
|
|
}
|
|
|
|
/// Checks that the fused node that actually ran answered the whole result, and that the values its
|
|
/// partitions published add up to the rows the result holds.
|
|
///
|
|
/// The JSON drops `metric.partition()`, so the per-partition figures are only on the captured node.
|
|
/// Which partition produced which value is the canonical router's hash over the series it was
|
|
/// delivered, so the total is the assertion; the split is reported in the failure messages, never
|
|
/// asserted.
|
|
fn assert_native_partition_output(
|
|
fused: &Arc<dyn ExecutionPlan>,
|
|
scan_partitions: Option<usize>,
|
|
expected_rows: usize,
|
|
context: &str,
|
|
) {
|
|
// A constant answers on the one partition it has.
|
|
let partitions = scan_partitions.unwrap_or(1);
|
|
let output_partitions = fused.output_partitioning().partition_count();
|
|
assert_eq!(
|
|
output_partitions, partitions,
|
|
"{context}: the fused node runs {output_partitions} partitions, not {partitions}"
|
|
);
|
|
if scan_partitions.is_some() {
|
|
let children = fused.children();
|
|
assert_eq!(
|
|
children.len(),
|
|
1,
|
|
"{context}: the fused node reads {} children, not one",
|
|
children.len()
|
|
);
|
|
let child_partitions = children[0].output_partitioning().partition_count();
|
|
assert_eq!(
|
|
child_partitions, partitions,
|
|
"{context}: the fused node's one child runs {child_partitions} partitions, not \
|
|
{partitions}"
|
|
);
|
|
}
|
|
let metrics = node_metric_set(fused);
|
|
let mut values_by_partition = vec![0_usize; partitions];
|
|
for metric in metrics.iter() {
|
|
if metric.value().name() != "native.root.output_values" {
|
|
continue;
|
|
}
|
|
let MetricValue::Count { count, .. } = metric.value() else {
|
|
continue;
|
|
};
|
|
let partition = metric.partition().unwrap_or_else(|| {
|
|
panic!("{context}: the fused node reports output values for no partition")
|
|
});
|
|
let values = values_by_partition.get_mut(partition).unwrap_or_else(|| {
|
|
panic!("{context}: partition {partition} of the fused node is not one of {partitions}")
|
|
});
|
|
*values += count.value();
|
|
}
|
|
// A partition-local tree publishes on the owners the canonical router hashed each series onto,
|
|
// so which partition folds what - and how many partitions fold anything at all - is the router's
|
|
// hash of the series it was delivered, not a property of the statement: a small fixture can leave
|
|
// some owners empty, which is not evidence of lost work. The total below is the assertion that
|
|
// every value was produced.
|
|
assert!(
|
|
values_by_partition.iter().any(|values| *values > 0),
|
|
"{context}: no native execution partition reported values: {values_by_partition:?}"
|
|
);
|
|
assert_eq!(
|
|
values_by_partition.iter().sum::<usize>(),
|
|
expected_rows,
|
|
"{context}: the partitions reported a different number of values than the result holds"
|
|
);
|
|
}
|
|
|
|
/// The registrations the fused node's tree made, exactly as they were recorded.
|
|
///
|
|
/// [`datafusion::physical_plan::ExecutionPlan::metrics`] leaves them out: a flat set cannot say
|
|
/// which tree node recorded what, so the node keeps its own set for readers that key by name.
|
|
fn node_metric_set(
|
|
plan: &Arc<dyn datafusion::physical_plan::ExecutionPlan>,
|
|
) -> datafusion::physical_plan::metrics::MetricsSet {
|
|
plan.downcast_ref::<promql::extension_plan::FusedSeriesExec>()
|
|
.unwrap_or_else(|| panic!("the plan is a FusedSeriesExec, not {}", plan.name()))
|
|
.node_metric_set()
|
|
}
|
|
|
|
/// The columns of a result as (name, type, nullable).
|
|
fn result_columns(schema: &SchemaRef) -> Vec<(String, DataType, bool)> {
|
|
schema
|
|
.fields()
|
|
.iter()
|
|
.map(|field| {
|
|
(
|
|
field.name().clone(),
|
|
field.data_type().clone(),
|
|
field.is_nullable(),
|
|
)
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// Checks the schema the native route reports: the time index, then one nullable `Float64` per
|
|
/// series field, then one nullable `Utf8` per tag.
|
|
///
|
|
/// A constant reports no tags and no series fields: its one value per grid point is `value` on the
|
|
/// `time` grid.
|
|
///
|
|
/// The tags are stated by the caller rather than read off the result: a vector that matches no
|
|
/// series reports no row to read them from, and still answers with its tag columns.
|
|
fn assert_native_result_schema(schema: &SchemaRef, tags: &[&str], value_name: &str, context: &str) {
|
|
// The time index and the one field the tree reports, then one `Utf8` column per tag. The
|
|
// native route names the value column after the expression, so the caller states that name;
|
|
// `value`/`val` are the legacy route's names.
|
|
let mut columns = vec![
|
|
(
|
|
if tags.is_empty() { "time" } else { "ts" }.to_string(),
|
|
DataType::Timestamp(TimeUnit::Millisecond, None),
|
|
false,
|
|
),
|
|
(value_name.to_string(), DataType::Float64, true),
|
|
];
|
|
for name in tags {
|
|
columns.push(((*name).to_string(), DataType::Utf8, true));
|
|
}
|
|
// A rows layout declares the per-row name column after the labels; a constant has no rows and
|
|
// no labels, and stays the two columns it always was.
|
|
if !tags.is_empty() {
|
|
columns.push(("__name__".to_string(), DataType::Utf8, true));
|
|
}
|
|
assert_eq!(
|
|
result_columns(schema),
|
|
columns,
|
|
"{context}: native result schema"
|
|
);
|
|
}
|
|
|
|
/// Checks the schema the legacy route reports by role rather than by position or field name, which
|
|
/// it does not share with the native route: one millisecond time column, one `Float64` value column
|
|
/// whose name contains the field it derives from, and one `Utf8` column per tag, named exactly as
|
|
/// that tag.
|
|
///
|
|
/// The value column's name is checked loosely because a computed legacy vector column may be named
|
|
/// after the expression that computes it; only constants are compared field for field.
|
|
///
|
|
/// As on the native route the tags are stated by the caller, so a vector that matches no series is
|
|
/// still expected to answer with its tag columns.
|
|
fn assert_legacy_result_schema(schema: &SchemaRef, tags: &[&str], context: &str) {
|
|
let time_column = if tags.is_empty() { "time" } else { "ts" };
|
|
let columns = result_columns(schema);
|
|
assert_eq!(
|
|
columns.len(),
|
|
2 + tags.len(),
|
|
"{context}: legacy result columns: {columns:?}"
|
|
);
|
|
let times = columns
|
|
.iter()
|
|
.filter(|column| matches!(column.1, DataType::Timestamp(TimeUnit::Millisecond, None)))
|
|
.collect::<Vec<_>>();
|
|
assert_eq!(
|
|
times.len(),
|
|
1,
|
|
"{context}: legacy result reports one millisecond time column: {columns:?}"
|
|
);
|
|
assert_eq!(
|
|
times[0].0, time_column,
|
|
"{context}: legacy time column name: {columns:?}"
|
|
);
|
|
let values = columns
|
|
.iter()
|
|
.filter(|column| column.1 == DataType::Float64)
|
|
.collect::<Vec<_>>();
|
|
assert_eq!(
|
|
values.len(),
|
|
1,
|
|
"{context}: legacy result reports one Float64 value column: {columns:?}"
|
|
);
|
|
assert!(
|
|
values[0].0.contains("val"),
|
|
"{context}: legacy value column does not name the field it derives from: {columns:?}"
|
|
);
|
|
let tag_names = columns
|
|
.iter()
|
|
.filter(|column| column.1 == DataType::Utf8)
|
|
.map(|column| column.0.as_str())
|
|
.collect::<Vec<_>>();
|
|
assert_eq!(
|
|
tag_names, tags,
|
|
"{context}: legacy tag columns: {columns:?}"
|
|
);
|
|
}
|
|
|
|
/// Compares two complete row lists: the same series at the same timestamps, and values that agree to
|
|
/// a relative `1e-9`. Comparing the keys row by row is what rejects a prefix, a duplicate or a
|
|
/// missing series; a row count alone would accept the first two.
|
|
fn assert_rows_eq(actual: &[PromqlRow], expected: &[PromqlRow], context: &str) {
|
|
let keys = |rows: &[PromqlRow]| {
|
|
rows.iter()
|
|
.map(|(tags, timestamp, _)| (tags.clone(), *timestamp))
|
|
.collect::<Vec<_>>()
|
|
};
|
|
assert_eq!(
|
|
keys(actual),
|
|
keys(expected),
|
|
"{context}: the result does not name the expected series and times"
|
|
);
|
|
for (actual, expected) in actual.iter().zip(expected) {
|
|
let tolerance = 1e-9 * expected.2.abs().max(1.0);
|
|
assert!(
|
|
(actual.2 - expected.2).abs() <= tolerance,
|
|
"{context}: series {:?} at {} must report {}, found {}",
|
|
expected.0,
|
|
expected.1,
|
|
expected.2,
|
|
actual.2
|
|
);
|
|
}
|
|
}
|
|
|
|
/// The three grid points of every fixture host: its two stored samples, then the last of them carried
|
|
/// forward to the last point. `value` turns a stored sample into the queried value.
|
|
fn fixture_rows(value: impl Fn(f64) -> f64) -> Vec<PromqlRow> {
|
|
(0..8)
|
|
.flat_map(|host| {
|
|
let tags = vec![("host".to_string(), format!("h{host}"))];
|
|
[
|
|
(0, value((host + 1) as f64)),
|
|
(1_000, value((host + 2) as f64)),
|
|
(2_000, value((host + 2) as f64)),
|
|
]
|
|
.into_iter()
|
|
.map(move |(timestamp, value)| (tags.clone(), timestamp, value))
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// Replays the captured raw scan through both public FusedSeries output formats.
|
|
async fn assert_raw_scan_fused_formats(observer: &NativeFusedObserver, frontend: &Arc<Instance>) {
|
|
let table_id = frontend
|
|
.catalog_manager()
|
|
.table("greptime", "public", "native_demo", None)
|
|
.await
|
|
.unwrap()
|
|
.expect("fixture table exists")
|
|
.table_info()
|
|
.table_id();
|
|
let (_, route) = frontend
|
|
.table_metadata_manager()
|
|
.table_route_manager()
|
|
.get_physical_table_route(table_id)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
route.region_routes.len(),
|
|
1,
|
|
"fixture has exactly one physical region"
|
|
);
|
|
|
|
observer.clear();
|
|
let (_, row_schema, _) = promql_output(frontend, "native_demo", Some(true), "0", "2").await;
|
|
let origin = observer.take("native_demo");
|
|
let children = origin.children();
|
|
assert_eq!(children.len(), 1, "the fused origin has exactly one child");
|
|
// The fused node's child is the router over the statement's dispatched raw read, so the raw
|
|
// scan is the one the region planned: it is observed on its own rather than nested here.
|
|
let scan_plan = observer.take_scan("native_demo");
|
|
let scan = scan_plan
|
|
.downcast_ref::<table::table::scan::SeriesScanExec>()
|
|
.expect("the observed raw input is a real SeriesScanExec");
|
|
let raw_schema = Arc::clone(scan.series_schema());
|
|
let label_source = scan.label_source().clone();
|
|
let ordering = scan.raw_series_ordering();
|
|
let grid = Arc::new(EvalGrid::try_new(GridSpec::try_new(0, 1_000, 3).unwrap()).unwrap());
|
|
let output_schema = Arc::new(SeriesSchema {
|
|
fields: Arc::clone(&raw_schema.fields),
|
|
eval_grid: Some(Arc::clone(&grid)),
|
|
});
|
|
let raw_type = promql::native::plan::NativeType::Samples(Arc::clone(&raw_schema));
|
|
let root = promql::native::plan::NativeNode {
|
|
expr: promql::native::plan::NativeExpr::Instant {
|
|
input: Box::new(promql::native::plan::NativeNode {
|
|
expr: promql::native::plan::NativeExpr::Input(promql::native::plan::BoundInput {
|
|
source_index: 0,
|
|
output: raw_type.clone(),
|
|
ordering,
|
|
}),
|
|
output: raw_type,
|
|
}),
|
|
op: promql::native::plan::BoundInstant {
|
|
lookback_ms: 300_000,
|
|
offset_ms: 0,
|
|
at_ms: None,
|
|
value: promql::native::plan::InstantValue::Sample,
|
|
output_grid: Arc::clone(&grid),
|
|
},
|
|
},
|
|
output: promql::native::plan::NativeType::Samples(Arc::clone(&output_schema)),
|
|
};
|
|
let tree = promql::native::plan::NativeTree::try_new(root).unwrap();
|
|
let binding = promql::extension_plan::FusedSeriesBinding {
|
|
grid,
|
|
metric_name: Some(Arc::from("native_demo")),
|
|
time_index: row_schema
|
|
.fields()
|
|
.iter()
|
|
.find(|field| {
|
|
matches!(
|
|
field.data_type(),
|
|
DataType::Timestamp(TimeUnit::Millisecond, _)
|
|
)
|
|
})
|
|
.expect("PromQL result has a millisecond timestamp field")
|
|
.name()
|
|
.clone(),
|
|
tags: vec!["host".to_string()],
|
|
};
|
|
let logical_child = LogicalPlan::EmptyRelation(EmptyRelation {
|
|
produce_one_row: false,
|
|
schema: DFSchema::try_from(scan.schema()).unwrap().into(),
|
|
});
|
|
let expected = fixture_rows(|value| value);
|
|
for format in [
|
|
promql::extension_plan::FusedOutputFormat::PlainRows,
|
|
promql::extension_plan::FusedOutputFormat::SeriesFrame,
|
|
] {
|
|
let scan_plan = scan_plan.clone().reset_state().unwrap();
|
|
let exec = promql::extension_plan::FusedSeries::try_new_with_format(
|
|
Arc::from("native_demo"),
|
|
tree.clone(),
|
|
binding.clone(),
|
|
vec![logical_child.clone()],
|
|
if format == promql::extension_plan::FusedOutputFormat::SeriesFrame {
|
|
let packed = common_query::series_frame::pack_evaluated_series(
|
|
Box::pin(futures::stream::empty()),
|
|
Arc::clone(&output_schema),
|
|
)
|
|
.unwrap();
|
|
Arc::new(DFSchema::try_from(packed.schema()).unwrap())
|
|
} else {
|
|
row_schema.clone().to_dfschema_ref().unwrap()
|
|
},
|
|
format,
|
|
)
|
|
.unwrap()
|
|
.to_execution_plan(vec![(scan_plan.clone(), label_source.clone(), ordering)])
|
|
.unwrap();
|
|
let context = "standalone raw SeriesScanExec replay";
|
|
let mut actual = Vec::new();
|
|
let mut closed = 0;
|
|
let mut partition_counts = Vec::new();
|
|
for partition in 0..4 {
|
|
let stream = exec
|
|
.execute(
|
|
partition,
|
|
datafusion::prelude::SessionContext::new().task_ctx(),
|
|
)
|
|
.unwrap();
|
|
if format == promql::extension_plan::FusedOutputFormat::SeriesFrame {
|
|
let series = common_query::series_frame::unpack_evaluated_series(stream, None)
|
|
.unwrap()
|
|
.try_collect::<Vec<_>>()
|
|
.await
|
|
.unwrap();
|
|
assert!(
|
|
!series.is_empty(),
|
|
"partition {partition} emitted no series frames"
|
|
);
|
|
partition_counts.push(
|
|
series
|
|
.iter()
|
|
.map(|(s, _)| s.time.timestamps().unwrap().len())
|
|
.sum::<usize>(),
|
|
);
|
|
for (series, last) in series {
|
|
assert_eq!(series.metric.name(), Some("native_demo"));
|
|
assert_eq!(series.metric.tags().len(), 1);
|
|
assert_eq!(series.fields.len(), 1);
|
|
let host = series.metric.get("host").unwrap().to_string();
|
|
let timestamps = series.time.timestamps().unwrap();
|
|
let samples = series.fields.get(0).unwrap();
|
|
for (i, timestamp) in timestamps.iter().enumerate() {
|
|
let SampleRef::Float(value) = samples.iter().nth(i).unwrap() else {
|
|
panic!("expected float sample")
|
|
};
|
|
actual.push((vec![("host".to_string(), host.clone())], *timestamp, value));
|
|
}
|
|
closed += usize::from(last);
|
|
}
|
|
} else {
|
|
// The rows layout declares the per-row metric-name column after the labels, and this
|
|
// replay binds `native_demo` as the metric the tree evaluates: every row reports it.
|
|
assert_eq!(row_schema.fields().len(), 4);
|
|
let name_field = row_schema
|
|
.fields()
|
|
.iter()
|
|
.rposition(|field| datatypes::schema::is_metric_name_field(field))
|
|
.expect("the rows layout declares the metric-name column");
|
|
assert_eq!(
|
|
name_field,
|
|
row_schema.fields().len() - 1,
|
|
"the name column is appended after the labels: {name_field}"
|
|
);
|
|
let batches = stream.try_collect::<Vec<_>>().await.unwrap();
|
|
let mut count = 0;
|
|
for batch in batches {
|
|
let ts = batch
|
|
.column_by_name(&binding.time_index)
|
|
.unwrap()
|
|
.as_any()
|
|
.downcast_ref::<TimestampMillisecondArray>()
|
|
.unwrap();
|
|
let host = batch
|
|
.column_by_name("host")
|
|
.unwrap()
|
|
.as_any()
|
|
.downcast_ref::<StringArray>()
|
|
.unwrap();
|
|
// The native route names its value column after the expression, so the
|
|
// type is what identifies it; `val` is only the legacy route's name.
|
|
let values = batch
|
|
.columns()
|
|
.iter()
|
|
.find(|column| column.data_type() == &DataType::Float64)
|
|
.expect("one Float64 value column")
|
|
.as_any()
|
|
.downcast_ref::<Float64Array>()
|
|
.unwrap();
|
|
let names = batch
|
|
.column_by_name("__name__")
|
|
.expect("the rows carry the name column")
|
|
.as_any()
|
|
.downcast_ref::<StringArray>()
|
|
.unwrap();
|
|
assert_eq!(names.null_count(), 0, "every row reports its metric name");
|
|
for i in 0..batch.num_rows() {
|
|
assert_eq!(
|
|
"native_demo",
|
|
names.value(i),
|
|
"a row names the metric the bound tree evaluates"
|
|
);
|
|
actual.push((
|
|
vec![("host".to_string(), host.value(i).to_string())],
|
|
ts.value(i),
|
|
values.value(i),
|
|
));
|
|
count += 1;
|
|
}
|
|
}
|
|
assert!(count > 0, "partition {partition} emitted no rows");
|
|
partition_counts.push(count);
|
|
}
|
|
}
|
|
assert_eq!(partition_counts.len(), 4);
|
|
assert!(
|
|
partition_counts.iter().all(|count| *count > 0),
|
|
"all four partitions emitted output: {partition_counts:?}"
|
|
);
|
|
assert_eq!(partition_counts.iter().sum::<usize>(), 24);
|
|
if format == promql::extension_plan::FusedOutputFormat::SeriesFrame {
|
|
assert_eq!(closed, 8, "one closing frame per host series");
|
|
}
|
|
actual.sort_unstable_by(|left, right| left.0.cmp(&right.0).then(left.1.cmp(&right.1)));
|
|
assert_rows_eq(&actual, &expected, context);
|
|
assert_native_partition_output(&exec, Some(4), 24, context);
|
|
}
|
|
}
|
|
|
|
/// Runs one PromQL query through the public frontend handler and returns the plan it executed, the
|
|
/// schema it answered with and every row it reported.
|
|
async fn promql_output(
|
|
frontend: &Arc<Instance>,
|
|
query: &str,
|
|
native: Option<bool>,
|
|
start: &str,
|
|
end: &str,
|
|
) -> (Arc<dyn ExecutionPlan>, SchemaRef, Vec<PromqlRow>) {
|
|
let mut query_ctx = QueryContextBuilder::default();
|
|
// `None` states nothing, so the session and the engine-wide default answer; `Some(false)` asks
|
|
// for the existing route explicitly, which the engine default cannot say on its own.
|
|
if let Some(native) = native {
|
|
query_ctx = query_ctx.set_extension(
|
|
"query.experimental_native_promql".to_string(),
|
|
native.to_string(),
|
|
);
|
|
}
|
|
let prom_query = PromQuery {
|
|
query: query.to_string(),
|
|
start: start.to_string(),
|
|
end: end.to_string(),
|
|
step: "1s".to_string(),
|
|
..Default::default()
|
|
};
|
|
let outputs = SqlQueryHandler::do_promql_query(
|
|
frontend.as_ref(),
|
|
&prom_query,
|
|
Arc::new(query_ctx.build()),
|
|
)
|
|
.await;
|
|
let output = outputs
|
|
.into_iter()
|
|
.next()
|
|
.expect("a PromQL query answers once")
|
|
.unwrap_or_else(|error| panic!("`{query}` failed: {error}"));
|
|
let plan = output
|
|
.meta
|
|
.plan
|
|
.clone()
|
|
.expect("a query reports the plan it executed");
|
|
let output_data = match output.data {
|
|
common_query::OutputData::RecordBatches(batches) => batches,
|
|
common_query::OutputData::Stream(stream) => {
|
|
common_recordbatch::RecordBatches::try_collect(stream)
|
|
.await
|
|
.unwrap_or_else(|error| panic!("`{query}` result collection failed: {error}"))
|
|
}
|
|
common_query::OutputData::AffectedRows(rows) => {
|
|
panic!("`{query}` returned affected rows: {rows}")
|
|
}
|
|
};
|
|
// The collected batches keep the schema the stream reported, so a result that holds no row still
|
|
// names the columns its route answers with.
|
|
let schema = output_data.schema().arrow_schema().clone();
|
|
let mut rows = Vec::new();
|
|
for batch in output_data.iter() {
|
|
let batch = batch.df_record_batch();
|
|
let arrow_schema = batch.schema();
|
|
let timestamps = batch
|
|
.columns()
|
|
.iter()
|
|
.find_map(|column| column.as_any().downcast_ref::<TimestampMillisecondArray>())
|
|
.unwrap_or_else(|| panic!("`{query}` reports no millisecond timestamp column"));
|
|
let values = batch
|
|
.columns()
|
|
.iter()
|
|
.find_map(|column| column.as_any().downcast_ref::<Float64Array>())
|
|
.unwrap_or_else(|| panic!("`{query}` reports no Float64 value column"));
|
|
// The tags are the result's string columns, in schema order: a column the schema names but
|
|
// no row carries would be caught by the row comparison below.
|
|
let tag_columns = arrow_schema
|
|
.fields()
|
|
.iter()
|
|
.enumerate()
|
|
.filter(|(_, field)| matches!(field.data_type(), DataType::Utf8 | DataType::Utf8View))
|
|
.map(|(index, field)| {
|
|
(
|
|
index,
|
|
field.name().clone(),
|
|
datatypes::schema::is_metric_name_field(field),
|
|
)
|
|
})
|
|
.collect::<Vec<_>>();
|
|
for row in 0..batch.num_rows() {
|
|
assert!(
|
|
timestamps.is_valid(row),
|
|
"`{query}`: null timestamp at row {row}"
|
|
);
|
|
assert!(values.is_valid(row), "`{query}`: null value at row {row}");
|
|
// The name column is declared on every rows layout and is null for a row whose value is
|
|
// computed rather than read from a series; the labels themselves are never null.
|
|
let tags = tag_columns
|
|
.iter()
|
|
.filter_map(|(index, name, is_name)| {
|
|
let column = batch.column(*index);
|
|
let value = match column.data_type() {
|
|
DataType::Utf8 => {
|
|
let array = column.as_any().downcast_ref::<StringArray>().unwrap();
|
|
if !array.is_valid(row) {
|
|
assert!(*is_name, "`{query}`: null tag {name} at row {row}");
|
|
return None;
|
|
}
|
|
array.value(row).to_string()
|
|
}
|
|
DataType::Utf8View => {
|
|
let array = column.as_any().downcast_ref::<StringViewArray>().unwrap();
|
|
if !array.is_valid(row) {
|
|
assert!(*is_name, "`{query}`: null tag {name} at row {row}");
|
|
return None;
|
|
}
|
|
array.value(row).to_string()
|
|
}
|
|
other => panic!("`{query}`: tag column {name} is not a string: {other}"),
|
|
};
|
|
Some((name.clone(), value))
|
|
})
|
|
.collect::<Vec<_>>();
|
|
rows.push((tags, timestamps.value(row), values.value(row)));
|
|
}
|
|
}
|
|
rows.sort_unstable_by(|left, right| left.0.cmp(&right.0).then(left.1.cmp(&right.1)));
|
|
(plan, schema, rows)
|
|
}
|
|
|
|
/// Checks public PromQL results and the executed plan of every request on both routes.
|
|
///
|
|
/// One standalone frontend serves both routes: the engine-wide option is on, so each request's own
|
|
/// hint decides its route, and the test-local observer proves which one answered. Every response is
|
|
/// compared whole - result type, labels, timestamp and value - and never by prefix.
|
|
pub async fn test_prometheus_native_promql_route(store_type: StorageType) {
|
|
let plugins = Plugins::default();
|
|
let observer = Arc::new(PromRouteObserver::default());
|
|
plugins.insert::<PromQueryInterceptorRef<servers::error::Error>>(observer.clone());
|
|
let mut instance =
|
|
tests_integration::standalone::GreptimeDbStandaloneBuilder::new("promql_native_route")
|
|
.with_default_store_type(store_type)
|
|
.with_query_options(QueryOptions {
|
|
experimental_native_promql: true,
|
|
..Default::default()
|
|
})
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(instance.fe_instance().clone(), false, None)
|
|
.with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
let native_hint = ("x-greptime-hints", "query.experimental_native_promql=true");
|
|
|
|
for sql in [
|
|
// One series to compute over, one whose only value is zero, and one table that keeps no row.
|
|
"CREATE TABLE native_route (ts TIMESTAMP TIME INDEX, val DOUBLE, host STRING, PRIMARY KEY (host))",
|
|
"CREATE TABLE native_zero (ts TIMESTAMP TIME INDEX, val DOUBLE, host STRING, PRIMARY KEY (host))",
|
|
"CREATE TABLE native_empty (ts TIMESTAMP TIME INDEX, val DOUBLE, host STRING, PRIMARY KEY (host))",
|
|
"INSERT INTO native_route(host, val, ts) VALUES ('h1', 1.0, 0), ('h1', 2.0, 1000)",
|
|
"INSERT INTO native_zero(host, val, ts) VALUES ('z', 0.0, 0), ('z', 0.0, 1000)",
|
|
] {
|
|
let res = client
|
|
.get(&format!("/v1/sql?db=public&sql={}", encode(sql)))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{sql}");
|
|
}
|
|
|
|
// One expression per shape a statement root can take: a folded constant, a negated constant, a
|
|
// broadcast on either side of the input, a negation compiled as one, and two divisions by zero.
|
|
// `100 / 0` is `+Inf` and `0 / 0` is `NaN`: both are reported as the values they are, not as a
|
|
// missing sample, so each series keeps its row.
|
|
let instants = [
|
|
("1 + 2", json!({"resultType":"scalar","result":[1.0,"3.0"]})),
|
|
("-1", json!({"resultType":"scalar","result":[1.0,"-1.0"]})),
|
|
(
|
|
"native_route * 100",
|
|
json!({"resultType":"vector","result":[{"metric":{"host":"h1"},"value":[1.0,"200.0"]}]}),
|
|
),
|
|
(
|
|
"100 / native_route",
|
|
json!({"resultType":"vector","result":[{"metric":{"host":"h1"},"value":[1.0,"50.0"]}]}),
|
|
),
|
|
(
|
|
"-native_route",
|
|
json!({"resultType":"vector","result":[{"metric":{"host":"h1"},"value":[1.0,"-2.0"]}]}),
|
|
),
|
|
(
|
|
"100 / native_zero",
|
|
json!({"resultType":"vector","result":[{"metric":{"host":"z"},"value":[1.0,"inf"]}]}),
|
|
),
|
|
(
|
|
"native_zero / 0",
|
|
json!({"resultType":"vector","result":[{"metric":{"host":"z"},"value":[1.0,"NaN"]}]}),
|
|
),
|
|
// A selector that matches nothing broadcasts over no series, and a table that holds no row
|
|
// reports no series either; a constant answers on the grid it was asked about in both cases.
|
|
(
|
|
"native_route{host=\"absent\"} * 100",
|
|
json!({"resultType":"vector","result":[]}),
|
|
),
|
|
("native_empty", json!({"resultType":"vector","result":[]})),
|
|
];
|
|
for native in [false, true] {
|
|
for (query, expected) in &instants {
|
|
let mut request = client.get(&format!(
|
|
"/v1/prometheus/api/v1/query?query={}&time=1",
|
|
encode(query)
|
|
));
|
|
// The engine-wide default is on here, so the request states the route it wants in both
|
|
// directions: a missing value would leave the choice to the default, which is native.
|
|
request = request.header(
|
|
native_hint.0,
|
|
if native {
|
|
"query.experimental_native_promql=true"
|
|
} else {
|
|
"query.experimental_native_promql=false"
|
|
},
|
|
);
|
|
let res = request.send().await;
|
|
assert_eq!(res.status(), StatusCode::OK, "`{query}`, native={native}");
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success", "`{query}`, native={native}");
|
|
let actual = serde_json::to_value(&body.data).unwrap();
|
|
assert_eq!(&actual, expected, "`{query}`, native={native}");
|
|
assert_eq!(
|
|
executes_fused_series(&observer.take_plan()),
|
|
native,
|
|
"`{query}` asked native={native} but executed the other route"
|
|
);
|
|
}
|
|
}
|
|
|
|
// A subquery root is a matrix natively, while query_range rejects a range vector.
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/query?query=native_route[5m:1m]&time=300")
|
|
.header(native_hint.0, native_hint.1)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(
|
|
serde_json::to_value(&body.data).unwrap()["resultType"],
|
|
"matrix"
|
|
);
|
|
assert_eq!(
|
|
serde_json::to_value(&body.data).unwrap()["result"][0]["values"],
|
|
json!([
|
|
[60.0, "2.0"],
|
|
[120.0, "2.0"],
|
|
[180.0, "2.0"],
|
|
[240.0, "2.0"],
|
|
[300.0, "2.0"]
|
|
])
|
|
);
|
|
let res = client
|
|
.get(
|
|
"/v1/prometheus/api/v1/query_range?query=native_route[5m:1m]&start=300&end=300&step=1s",
|
|
)
|
|
.header(native_hint.0, native_hint.1)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
assert!(res.text().await.contains("range vector"));
|
|
|
|
// The same expression over a range: a matrix carries every grid point of every series, with the
|
|
// last sample carried forward, and both routes answer it with the same one.
|
|
let query = encode("native_route * 100");
|
|
let expected = json!({"resultType":"matrix","result":[{"metric":{"host":"h1"},"values":[[0.0,"100.0"],[1.0,"200.0"],[2.0,"200.0"]]}]});
|
|
for native in [false, true] {
|
|
let mut request = client.get(&format!(
|
|
"/v1/prometheus/api/v1/query_range?query={query}&start=0&end=2&step=1s"
|
|
));
|
|
// Both routes are asked for explicitly: the engine default is on, so leaving the value out
|
|
// would ask for neither.
|
|
request = request.header(
|
|
native_hint.0,
|
|
if native {
|
|
"query.experimental_native_promql=true"
|
|
} else {
|
|
"query.experimental_native_promql=false"
|
|
},
|
|
);
|
|
let res = request.send().await;
|
|
assert_eq!(res.status(), StatusCode::OK, "range query, native={native}");
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success", "range query, native={native}");
|
|
let actual = serde_json::to_value(&body.data).unwrap();
|
|
assert_eq!(&actual, &expected, "range query, native={native}");
|
|
assert_eq!(
|
|
executes_fused_series(&observer.take_plan()),
|
|
native,
|
|
"the hinted range query executed the other route"
|
|
);
|
|
}
|
|
|
|
instance.guard.remove_all().await;
|
|
}
|
|
|
|
/// Keeps the native fused node and the raw scan the request executed, for the figures only they
|
|
/// record.
|
|
///
|
|
/// The two are observed from different plans: the fused node is the frontend's, while the raw scan
|
|
/// is the one the region planned for the statement's raw read. Uses the existing
|
|
/// [`PhysicalPlanWrapper`] hook, which runs per plan; `clear` before a query and `take`/`take_scan`
|
|
/// after its body is drained keep each query reading only the nodes it ran.
|
|
#[derive(Default)]
|
|
struct NativeFusedObserver(
|
|
Mutex<Vec<Arc<dyn ExecutionPlan>>>,
|
|
Mutex<Vec<Arc<dyn ExecutionPlan>>>,
|
|
);
|
|
|
|
impl NativeFusedObserver {
|
|
/// Drops both what an earlier request captured: its fused node and its raw scan.
|
|
fn clear(&self) {
|
|
self.0.lock().unwrap().clear();
|
|
self.1.lock().unwrap().clear();
|
|
}
|
|
|
|
/// The one fused node the request just executed.
|
|
fn take(&self, context: &str) -> Arc<dyn ExecutionPlan> {
|
|
let mut captured = std::mem::take(&mut *self.0.lock().unwrap());
|
|
assert_eq!(
|
|
captured.len(),
|
|
1,
|
|
"`{context}`: the request executed {} fused nodes: {captured:?}",
|
|
captured.len()
|
|
);
|
|
captured.remove(0)
|
|
}
|
|
|
|
/// The one raw scan the request just executed.
|
|
///
|
|
/// The fused node reads its region through a router over a merge scan, whose region half is
|
|
/// dispatched rather than nested: the plan below the fused node is the router and the merge
|
|
/// scan, so the raw scan the statement read is taken from the plan the region planned for it
|
|
/// instead of by walking down from the fused node.
|
|
fn take_scan(&self, context: &str) -> Arc<dyn ExecutionPlan> {
|
|
let mut captured = std::mem::take(&mut *self.1.lock().unwrap());
|
|
assert_eq!(
|
|
captured.len(),
|
|
1,
|
|
"`{context}`: the request executed {} series scans: {captured:?}",
|
|
captured.len()
|
|
);
|
|
captured.remove(0)
|
|
}
|
|
|
|
/// Every fused node the request just executed.
|
|
///
|
|
/// A statement the route answers with one node yields one; a split aggregate yields the
|
|
/// partial producer and the final consumer, which are two separate executions.
|
|
fn take_all(&self, context: &str) -> Vec<Arc<dyn ExecutionPlan>> {
|
|
let captured = std::mem::take(&mut *self.0.lock().unwrap());
|
|
assert!(
|
|
!captured.is_empty(),
|
|
"`{context}`: the request executed no fused node, so it left the native route"
|
|
);
|
|
captured
|
|
}
|
|
}
|
|
|
|
impl PhysicalPlanWrapper for NativeFusedObserver {
|
|
fn wrap(
|
|
&self,
|
|
origin: Arc<dyn ExecutionPlan>,
|
|
_ctx: QueryContextRef,
|
|
) -> Arc<dyn ExecutionPlan> {
|
|
let mut stack = vec![origin.clone()];
|
|
while let Some(node) = stack.pop() {
|
|
if node.name() == "FusedSeriesExec" {
|
|
self.0.lock().unwrap().push(Arc::clone(&node));
|
|
} else if node
|
|
.downcast_ref::<table::table::scan::SeriesScanExec>()
|
|
.is_some()
|
|
{
|
|
self.1.lock().unwrap().push(Arc::clone(&node));
|
|
}
|
|
stack.extend(node.children().into_iter().cloned());
|
|
}
|
|
origin
|
|
}
|
|
}
|
|
|
|
/// Verifies the native route's plan and complete result against the legacy one.
|
|
///
|
|
/// The fixture is four pk-disjoint insert batches, each persisted by its own flush, so the scan has
|
|
/// four series batches to distribute over the four partitions the query asks for.
|
|
pub async fn test_native_promql_route_plans_the_query(store_type: StorageType) {
|
|
let observer = Arc::new(NativeFusedObserver::default());
|
|
let plugins = Plugins::default();
|
|
let wrapper: PhysicalPlanWrapperRef = observer.clone();
|
|
plugins.insert(wrapper);
|
|
let mut instance =
|
|
tests_integration::standalone::GreptimeDbStandaloneBuilder::new("promql_native_route_plan")
|
|
.with_default_store_type(store_type)
|
|
.with_query_options(QueryOptions {
|
|
experimental_native_promql: true,
|
|
parallelism: 4,
|
|
..Default::default()
|
|
})
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let frontend = instance.fe_instance();
|
|
|
|
// The relaxed TWCS thresholds keep the four flushed files from being compacted back into one
|
|
// unit of work, so four file ranges reach the scan.
|
|
for sql in [
|
|
"CREATE TABLE native_demo (ts TIMESTAMP TIME INDEX, val DOUBLE, host STRING, PRIMARY KEY (host)) WITH ('compaction.type' = 'twcs', 'compaction.twcs.active_window.trigger_file_num' = '100', 'compaction.twcs.inactive_window.trigger_file_num' = '100')",
|
|
"CREATE TABLE native_rate (ts TIMESTAMP TIME INDEX, val DOUBLE, host STRING, PRIMARY KEY (host)) WITH ('compaction.type' = 'twcs', 'compaction.twcs.active_window.trigger_file_num' = '100', 'compaction.twcs.inactive_window.trigger_file_num' = '100')",
|
|
] {
|
|
execute_sql_and_expect(frontend, sql, "Affected Rows: 0").await;
|
|
}
|
|
for (first, second) in [(0, 1), (2, 3), (4, 5), (6, 7)] {
|
|
let values = [first, second]
|
|
.into_iter()
|
|
.map(|host| {
|
|
format!(
|
|
"('h{host}', {}.0, 0), ('h{host}', {}.0, 1000)",
|
|
host + 1,
|
|
host + 2
|
|
)
|
|
})
|
|
.collect::<Vec<_>>()
|
|
.join(", ");
|
|
execute_sql_and_expect(
|
|
frontend,
|
|
&format!("INSERT INTO native_demo(host, val, ts) VALUES {values}"),
|
|
"Affected Rows: 4",
|
|
)
|
|
.await;
|
|
// One 0 -> 3 increment between the two samples.
|
|
let values = [first, second]
|
|
.into_iter()
|
|
.map(|host| format!("('h{host}', 0.0, 0), ('h{host}', 3.0, 1000)"))
|
|
.collect::<Vec<_>>()
|
|
.join(", ");
|
|
execute_sql_and_expect(
|
|
frontend,
|
|
&format!("INSERT INTO native_rate(host, val, ts) VALUES {values}"),
|
|
"Affected Rows: 4",
|
|
)
|
|
.await;
|
|
// Flushing before the next pair keeps every round a separate persisted batch.
|
|
tests_integration::test_util::execute_sql(frontend, "ADMIN FLUSH_TABLE('native_demo')")
|
|
.await;
|
|
tests_integration::test_util::execute_sql(frontend, "ADMIN FLUSH_TABLE('native_rate')")
|
|
.await;
|
|
}
|
|
|
|
assert_raw_scan_fused_formats(&observer, frontend).await;
|
|
|
|
for (query, start, end, expected, scan_partitions) in [
|
|
(
|
|
"1 + 2",
|
|
"0",
|
|
"2",
|
|
vec![
|
|
(Vec::new(), 0_i64, 3.0),
|
|
(Vec::new(), 1_000, 3.0),
|
|
(Vec::new(), 2_000, 3.0),
|
|
],
|
|
None,
|
|
),
|
|
(
|
|
"-1",
|
|
"0",
|
|
"2",
|
|
vec![
|
|
(Vec::new(), 0_i64, -1.0),
|
|
(Vec::new(), 1_000, -1.0),
|
|
(Vec::new(), 2_000, -1.0),
|
|
],
|
|
None,
|
|
),
|
|
(
|
|
"native_demo * 100",
|
|
"0",
|
|
"2",
|
|
fixture_rows(|value| value * 100.0),
|
|
Some(4),
|
|
),
|
|
(
|
|
"100 / native_demo",
|
|
"0",
|
|
"2",
|
|
fixture_rows(|value| 100.0 / value),
|
|
Some(4),
|
|
),
|
|
(
|
|
"-native_demo",
|
|
"0",
|
|
"2",
|
|
fixture_rows(|value| -value),
|
|
Some(4),
|
|
),
|
|
// The counter rises 0 -> 3 between ts 0 and 1s and is evaluated at 1s: the inferred zero
|
|
// point and the evaluation instant both sit on a sample, so neither edge is extrapolated and
|
|
// (3 / 300s) * 100 = 1.
|
|
(
|
|
"rate(native_rate[5m]) * 100",
|
|
"1",
|
|
"1",
|
|
(0..8)
|
|
.map(|host| (vec![("host".to_string(), format!("h{host}"))], 1_000, 1.0))
|
|
.collect::<Vec<PromqlRow>>(),
|
|
Some(4),
|
|
),
|
|
] {
|
|
// Only the node this query ran is read: the previous request's capture is dropped first.
|
|
observer.clear();
|
|
// Every fixture series carries one tag; a constant carries none.
|
|
let tags: &[&str] = if scan_partitions.is_some() {
|
|
&["host"]
|
|
} else {
|
|
&[]
|
|
};
|
|
let (plan, schema, rows) = promql_output(frontend, query, Some(true), start, end).await;
|
|
assert_rows_eq(&rows, &expected, query);
|
|
assert_native_result_schema(&schema, tags, query, query);
|
|
// The body is drained, so every partition has finished and the node this statement ran is
|
|
// complete; its own registrations - not the flat set the JSON view reads - carry the
|
|
// per-partition figures.
|
|
let exec = observer.take(query);
|
|
assert_native_plan(&plan, &exec, scan_partitions, query);
|
|
assert_native_partition_output(&exec, scan_partitions, expected.len(), query);
|
|
|
|
// A request that turns the route off runs the legacy pipeline, which must answer with the
|
|
// same rows. The engine default is on here, so the request value is what selects the route.
|
|
let (legacy_plan, legacy_schema, legacy_rows) =
|
|
promql_output(frontend, query, Some(false), start, end).await;
|
|
assert!(
|
|
!executes_fused_series(&legacy_plan),
|
|
"`{query}` with the route turned off executed the fused plan"
|
|
);
|
|
assert_rows_eq(&legacy_rows, &expected, query);
|
|
assert_legacy_result_schema(&legacy_schema, tags, query);
|
|
// Both routes report the time column and one Float64 value. The native route names that
|
|
// value after the expression, so the constant cases agree by role, not by header name.
|
|
if scan_partitions.is_none() {
|
|
assert_eq!(
|
|
2,
|
|
schema.fields().len(),
|
|
"`{query}`: native constant schema"
|
|
);
|
|
assert_eq!(
|
|
2,
|
|
legacy_schema.fields().len(),
|
|
"`{query}`: legacy constant schema"
|
|
);
|
|
assert_eq!(
|
|
schema.field(0).data_type(),
|
|
legacy_schema.field(0).data_type(),
|
|
"`{query}`: time column type"
|
|
);
|
|
assert_eq!(
|
|
&DataType::Float64,
|
|
schema.field(1).data_type(),
|
|
"`{query}`: native value type"
|
|
);
|
|
assert_eq!(
|
|
&DataType::Float64,
|
|
legacy_schema.field(1).data_type(),
|
|
"`{query}`: legacy value type"
|
|
);
|
|
}
|
|
}
|
|
|
|
// A vector that matches no series is a typed empty result, not a constant: both routes still
|
|
// report the time, value and tag columns, and the native route still plans its one scan. What
|
|
// such a plan reports per partition is deliberately not read: an empty input may be pruned to a
|
|
// different partition layout than the four non-empty queries above.
|
|
let query = "native_demo{host=\"absent\"} * 100";
|
|
observer.clear();
|
|
let (plan, schema, rows) = promql_output(frontend, query, Some(true), "0", "2").await;
|
|
assert!(rows.is_empty(), "`{query}` reported {} rows", rows.len());
|
|
assert_native_result_schema(&schema, &["host"], query, query);
|
|
assert!(
|
|
executes_fused_series(&plan),
|
|
"`{query}` did not execute the fused route"
|
|
);
|
|
let fused_nodes = plan_nodes_named(&plan, "FusedSeriesExec");
|
|
assert_eq!(
|
|
fused_nodes.len(),
|
|
1,
|
|
"`{query}`: expected one fused node: {fused_nodes:?}"
|
|
);
|
|
let scans = plan_nodes_named(&plan, "SeriesScanExec");
|
|
assert_eq!(
|
|
scans.len(),
|
|
1,
|
|
"`{query}`: an empty vector must read exactly one scan: {scans:?}"
|
|
);
|
|
// The body is drained, so the one fused node the request ran has finished. Its own child list is
|
|
// what says how many plans it reads, and its own line is where the tree it ran is still one text:
|
|
// the JSON view lists that tree's nodes as children of the operator, next to the plans it reads.
|
|
let exec = observer.take(query);
|
|
assert_eq!(
|
|
exec.children().len(),
|
|
1,
|
|
"`{query}`: an empty vector must read exactly one child"
|
|
);
|
|
let display = datafusion::physical_plan::displayable(exec.as_ref())
|
|
.indent(false)
|
|
.to_string();
|
|
let tree = display
|
|
.split_once("native_tree=[")
|
|
.map(|(_, tree)| tree.to_string())
|
|
.unwrap_or_else(|| panic!("`{query}`: the fused node renders no tree: {display}"));
|
|
// The tree binds the one source instead of answering as a constant, whose tree binds no input.
|
|
assert!(
|
|
tree.contains("Input"),
|
|
"`{query}`: the empty vector planned no input: {tree}"
|
|
);
|
|
assert_eq!(
|
|
tree.matches("source=0").count(),
|
|
1,
|
|
"`{query}`: the tree does not read the one source: {tree}"
|
|
);
|
|
assert!(
|
|
!tree.contains("source=1"),
|
|
"`{query}`: the tree reads a second source: {tree}"
|
|
);
|
|
|
|
// Without the per-query hint the same statement runs the legacy pipeline, which must answer
|
|
// with the same empty typed result.
|
|
let (legacy_plan, legacy_schema, legacy_rows) =
|
|
promql_output(frontend, query, Some(false), "0", "2").await;
|
|
assert!(
|
|
!executes_fused_series(&legacy_plan),
|
|
"`{query}` with the route turned off executed the fused plan"
|
|
);
|
|
assert!(
|
|
legacy_rows.is_empty(),
|
|
"`{query}` with the route turned off reported {} rows",
|
|
legacy_rows.len()
|
|
);
|
|
assert_legacy_result_schema(&legacy_schema, &["host"], query);
|
|
|
|
instance.guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_prometheus_label_replace_response(store_type: StorageType) {
|
|
let (app, mut guard) =
|
|
setup_test_prom_app_with_frontend(store_type, "prometheus_label_replace_response").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let query = encode(r#"label_replace(demo, "host_copy", "$1", "host", "(.*)")"#);
|
|
let res = client
|
|
.get(&format!("/v1/prometheus/api/v1/query?query={query}&time=0"))
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!({
|
|
"resultType": "vector",
|
|
"result": [{
|
|
"metric": {
|
|
"__name__": "demo",
|
|
"host": "host1",
|
|
"host_copy": "host1"
|
|
},
|
|
"value": [0.0, "1.1"]
|
|
}]
|
|
}))
|
|
.unwrap()
|
|
);
|
|
|
|
// A series whose source value does not match keeps its labels, without `host_copy`.
|
|
let query = encode(r#"label_replace(demo, "host_copy", "$1", "host", "other(.*)")"#);
|
|
let res = client
|
|
.get(&format!("/v1/prometheus/api/v1/query?query={query}&time=0"))
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!({
|
|
"resultType": "vector",
|
|
"result": [{
|
|
"metric": {
|
|
"__name__": "demo",
|
|
"host": "host1"
|
|
},
|
|
"value": [0.0, "1.1"]
|
|
}]
|
|
}))
|
|
.unwrap()
|
|
);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_prom_http_api(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_prom_app_with_frontend(store_type, "promql_api").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// format_query
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/format_query?query=foo/bar")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
assert_eq!(
|
|
res.text().await.as_str(),
|
|
r#"{"status":"success","data":"foo / bar"}"#
|
|
);
|
|
|
|
// instant query
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/query?query=up&time=1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let res = client
|
|
.post("/v1/prometheus/api/v1/query?query=up&time=1")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// instant query 1+1
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/query?query=1%2B1&time=1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(
|
|
json!({"resultType":"scalar","result":[1.0,"2.0"]})
|
|
)
|
|
.unwrap()
|
|
);
|
|
|
|
// range query
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/query_range?query=up&start=1&end=100&step=5")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let res = client
|
|
.post("/v1/prometheus/api/v1/query_range?query=up&start=1&end=100&step=5")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let res = client
|
|
.post("/v1/prometheus/api/v1/query_range?query=count(count(up))&start=1&end=100&step=5")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
// query a metric table backed by a physical table with nanosecond precision to ensure
|
|
// time cast and __tsid path work with logical table aliasing.
|
|
let res = client
|
|
.get(
|
|
"/v1/prometheus/api/v1/query_range?query=demo_metrics_with_nanos&start=0&end=10&step=1",
|
|
)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/query_range?query=up&start=1&end=100&step=0.5")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let res = client
|
|
.post("/v1/prometheus/api/v1/query_range?query=up&start=1&end=100&step=0.5")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// labels
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/labels?match[]=demo")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let res = client
|
|
.post("/v1/prometheus/api/v1/labels?match[]=up")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/labels?match[]=demo&start=0")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["__name__", "host",])).unwrap()
|
|
);
|
|
|
|
// labels without match[] param
|
|
let res = client.get("/v1/prometheus/api/v1/labels").send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!([
|
|
"__name__", "env", "host", "idc", "number",
|
|
]))
|
|
.unwrap()
|
|
);
|
|
|
|
// labels query with multiple match[] params
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/labels?match[]=up&match[]=down")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let res = client
|
|
.post("/v1/prometheus/api/v1/labels?match[]=up&match[]=down")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// series
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/series?match[]=demo{}&start=0&end=0")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
|
|
let PrometheusResponse::Series(mut series) = body.data else {
|
|
unreachable!()
|
|
};
|
|
let actual = series
|
|
.remove(0)
|
|
.into_iter()
|
|
.collect::<BTreeMap<Column, String>>();
|
|
let expected = BTreeMap::from([
|
|
("__name__".into(), "demo".to_string()),
|
|
("host".into(), "host1".to_string()),
|
|
]);
|
|
assert_eq!(actual, expected);
|
|
|
|
let res = client
|
|
.post("/v1/prometheus/api/v1/series?match[]=up&match[]=down")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
// An empty array will be deserialized into PrometheusResponse::Labels.
|
|
// So here we compare the text directly.
|
|
assert_eq!(res.text().await, r#"{"status":"success","data":[]}"#);
|
|
|
|
// label values
|
|
// should return error if there is no match[]
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/instance/values")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
let prom_resp = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(prom_resp.status, "error");
|
|
assert!(
|
|
prom_resp
|
|
.error
|
|
.is_some_and(|err| err.eq_ignore_ascii_case("match[] parameter is required"))
|
|
);
|
|
assert!(
|
|
prom_resp
|
|
.error_type
|
|
.is_some_and(|err| err.eq_ignore_ascii_case("InvalidArguments"))
|
|
);
|
|
|
|
// single match[]
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/host/values?match[]=demo&start=0&end=600")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["host1", "host2"])).unwrap()
|
|
);
|
|
|
|
// single match[]
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/host/values?match[]=demo&start=0&end=300")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["host1"])).unwrap()
|
|
);
|
|
|
|
// single match[] with __name__
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/host/values?match[]={__name__%3D%22demo%22}&start=0&end=300")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["host1"])).unwrap()
|
|
);
|
|
|
|
// single match[]
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/idc/values?match[]=demo_metrics_with_nanos&start=0&end=600")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["idc1"])).unwrap()
|
|
);
|
|
|
|
// match labels.
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/host/values?match[]=multi_labels{idc=\"idc1\", env=\"dev\"}&start=0&end=600")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["host1", "host2"])).unwrap()
|
|
);
|
|
|
|
// special labels
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__schema__/values?start=0&end=600")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!([
|
|
"greptime_private",
|
|
"information_schema",
|
|
"public"
|
|
]))
|
|
.unwrap()
|
|
);
|
|
|
|
// special labels
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__schema__/values?match[]=demo&start=0&end=600")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["public"])).unwrap()
|
|
);
|
|
|
|
// special labels
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__database__/values?match[]=demo&start=0&end=600")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["public"])).unwrap()
|
|
);
|
|
|
|
// special labels
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__database__/values?match[]=multi_labels{idc=\"idc1\", env=\"dev\"}&start=0&end=600")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["public"])).unwrap()
|
|
);
|
|
|
|
// match special labels.
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/host/values?match[]=multi_labels{__schema__=\"public\", idc=\"idc1\", env=\"dev\"}&start=0&end=600")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["host1", "host2"])).unwrap()
|
|
);
|
|
|
|
// match special labels.
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/host/values?match[]=multi_labels{__schema__=\"information_schema\", idc=\"idc1\", env=\"dev\"}&start=0&end=600")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!([])).unwrap()
|
|
);
|
|
|
|
// search field name
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__field__/values?match[]=demo")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["val"])).unwrap()
|
|
);
|
|
|
|
// limit
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/host/values?match[]=demo&start=0&end=600&limit=1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["host1"])).unwrap()
|
|
);
|
|
|
|
// limit 0
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/host/values?match[]=demo&start=0&end=600&limit=0")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["host1", "host2"])).unwrap()
|
|
);
|
|
|
|
// limit 10
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/host/values?match[]=demo&start=0&end=600&limit=10")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["host1", "host2"])).unwrap()
|
|
);
|
|
|
|
// special labels limit
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__schema__/values?start=0&end=600&limit=2")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!([
|
|
"greptime_private",
|
|
"information_schema",
|
|
]))
|
|
.unwrap()
|
|
);
|
|
|
|
// query an empty database should return nothing
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/host/values?match[]=demo&start=0&end=600")
|
|
.header(GREPTIME_DB_HEADER_NAME.clone(), "nonexistent")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!([])).unwrap()
|
|
);
|
|
|
|
// db header will be overrode by `db` in param
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/host/values?match[]=demo&start=0&end=600&db=public")
|
|
.header(GREPTIME_DB_HEADER_NAME.clone(), "nonexistent")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["host1", "host2"])).unwrap()
|
|
);
|
|
|
|
// multiple match[]
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/instance/values?match[]=up&match[]=system_metrics")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let prom_resp = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(prom_resp.status, "success");
|
|
assert!(prom_resp.error.is_none());
|
|
assert!(prom_resp.error_type.is_none());
|
|
|
|
// query non-string value
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/host/values?match[]=mito")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// query non-exist label in metric table
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/not_exist_label/values?match[]=demo&start=0&end=600")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let prom_resp = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(prom_resp.status, "success");
|
|
assert!(prom_resp.error.is_none() && prom_resp.error_type.is_none());
|
|
assert_eq!(
|
|
prom_resp.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!([])).unwrap()
|
|
);
|
|
|
|
// query `__name__` without match[]
|
|
// create a physical table and a logical table
|
|
let res = client
|
|
.get("/v1/sql?sql=create table physical_table (`ts` timestamp time index, `message` string) with ('physical_metric_table' = 'true');")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
let res = client
|
|
.get("/v1/sql?sql=create table logic_table (`ts` timestamp time index, `message` string) with ('on_physical_table' = 'physical_table');")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
// query `__name__`
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__name__/values")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let prom_resp = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(prom_resp.status, "success");
|
|
assert!(prom_resp.error.is_none());
|
|
assert!(prom_resp.error_type.is_none());
|
|
assert_eq!(
|
|
prom_resp.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!([
|
|
"demo",
|
|
"demo_metrics",
|
|
"demo_metrics_with_nanos",
|
|
"logic_table",
|
|
"multi_labels",
|
|
]))
|
|
.unwrap()
|
|
);
|
|
|
|
// query `__name__`
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__name__/values?match[]={__name__=\"demo\"}")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let prom_resp = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(prom_resp.status, "success");
|
|
assert!(prom_resp.error.is_none());
|
|
assert!(prom_resp.error_type.is_none());
|
|
assert_eq!(
|
|
prom_resp.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["demo",])).unwrap()
|
|
);
|
|
|
|
// query `__name__`
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__name__/values?match[]={__name__=~\".*demo.*\"}")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let prom_resp = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(prom_resp.status, "success");
|
|
assert!(prom_resp.error.is_none());
|
|
assert!(prom_resp.error_type.is_none());
|
|
assert_eq!(
|
|
prom_resp.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!([
|
|
"demo",
|
|
"demo_metrics",
|
|
"demo_metrics_with_nanos",
|
|
]))
|
|
.unwrap()
|
|
);
|
|
|
|
// query `__name__` by a matcher on an ordinary label: the metric engine
|
|
// physical tables are scanned, so only metrics carrying the label value are
|
|
// returned. `demo_metrics` shares `phy` with `demo` but has no `host` value.
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__name__/values?match[]={host=\"host1\"}&start=0&end=600")
|
|
.send()
|
|
.await;
|
|
let status = res.status();
|
|
let text = res.text().await;
|
|
assert_eq!(status, StatusCode::OK, "{text}");
|
|
let prom_resp = serde_json::from_str::<PrometheusJsonResponse>(&text).unwrap();
|
|
assert_eq!(prom_resp.status, "success");
|
|
assert!(prom_resp.error.is_none());
|
|
assert_eq!(
|
|
prom_resp.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["demo", "multi_labels"])).unwrap()
|
|
);
|
|
|
|
// `__name__` matchers narrow the names the data resolved: `multi_labels`
|
|
// also carries `idc="idc1"` but its name does not match.
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__name__/values?match[]={__name__=~\"demo.*\", idc=\"idc1\"}&start=0&end=600")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let prom_resp = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(prom_resp.status, "success");
|
|
assert!(prom_resp.error.is_none());
|
|
assert_eq!(
|
|
prom_resp.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!([
|
|
"demo_metrics",
|
|
"demo_metrics_with_nanos",
|
|
]))
|
|
.unwrap()
|
|
);
|
|
|
|
// The time range selects the series: `demo` carries `host="host2"` only at
|
|
// t=600, so narrowing the range drops it while `multi_labels` at t=0 stays.
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__name__/values?match[]={host=\"host2\"}&start=0&end=600")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let prom_resp = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(
|
|
prom_resp.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["demo", "multi_labels"])).unwrap()
|
|
);
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__name__/values?match[]={host=\"host2\"}&start=0&end=300")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let prom_resp = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(
|
|
prom_resp.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["multi_labels"])).unwrap()
|
|
);
|
|
|
|
// Logical metrics sharing a physical table have NULL in the label columns
|
|
// they don't use. Prometheus reads a label a series doesn't carry as the
|
|
// empty string, so `demo_metrics` and `demo_metrics_with_nanos` — neither of
|
|
// which has a `host` label — match both of these.
|
|
//
|
|
// `.%2B` is `.+`; a bare `+` decodes to a space in a query string. Grafana
|
|
// encodes it the same way.
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__name__/values?match[]={__name__=~\".%2B\", host=\"\"}&start=0&end=600")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let prom_resp = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(
|
|
prom_resp.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!([
|
|
"demo_metrics",
|
|
"demo_metrics_with_nanos",
|
|
]))
|
|
.unwrap()
|
|
);
|
|
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__name__/values?match[]={__name__=~\".%2B\", host!=\"host1\"}&start=0&end=600")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let prom_resp = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(
|
|
prom_resp.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!([
|
|
"demo",
|
|
"demo_metrics",
|
|
"demo_metrics_with_nanos",
|
|
"multi_labels",
|
|
]))
|
|
.unwrap()
|
|
);
|
|
|
|
// A pre-epoch RFC3339 bound is a valid range, not a panic.
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/label/__name__/values?match[]={host=\"host1\"}&start=1969-12-31T23:59:59Z&end=600")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let prom_resp = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(
|
|
prom_resp.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!(["demo", "multi_labels"])).unwrap()
|
|
);
|
|
|
|
// buildinfo
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/status/buildinfo")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = serde_json::from_str::<PrometheusJsonResponse>(&res.text().await).unwrap();
|
|
assert_eq!(body.status, "success");
|
|
|
|
// parse_query
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/parse_query?query=http_requests")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let data = res.text().await;
|
|
// we don't have deserialization for ast so we keep test simple and compare
|
|
// the json output directly.
|
|
// the correctness should be covered by parser. In this test we only check
|
|
// response format.
|
|
let expected = "{\"status\":\"success\",\"data\":{\"type\":\"vectorSelector\",\"name\":\"http_requests\",\"matchers\":[],\"offset\":0,\"startOrEnd\":null,\"timestamp\":null}}";
|
|
assert_eq!(expected, data);
|
|
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/parse_query?query=not http_requests")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
let data = res.text().await;
|
|
let expected = "{\"status\":\"error\",\"error\":\"invalid promql query\",\"errorType\":\"InvalidArguments\"}";
|
|
assert_eq!(expected, data);
|
|
|
|
// range_query with __name__ not-equal matcher
|
|
let res = client
|
|
.post("/v1/prometheus/api/v1/query_range?query=count by(__name__)({__name__=~'demo.*'})&start=1&end=100&step=5")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let data = res.text().await;
|
|
assert!(
|
|
data.contains("{\"__name__\":\"demo_metrics\"}")
|
|
&& data.contains("{\"__name__\":\"demo\"}")
|
|
);
|
|
|
|
let res = client
|
|
.post("/v1/prometheus/api/v1/query_range?query=count by(__name__)({__name__=~'demo_metrics'})&start=1&end=100&step=5")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let data = res.text().await;
|
|
assert!(
|
|
data.contains("{\"__name__\":\"demo_metrics\"}")
|
|
&& !data.contains("{\"__name__\":\"demo\"}")
|
|
);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
/// A tiny compressed body must not be able to demand an unbounded decoded
|
|
/// allocation on the Prometheus remote-storage endpoints. The five-byte raw
|
|
/// Snappy varint (`ff ff ff ff 0f`) declares a ~4 GiB decoded length and must
|
|
/// be rejected by the decoded-size cap.
|
|
pub async fn test_prom_remote_compression_bomb_rejected(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_prom_app_with_frontend(store_type, "prom_remote_bomb").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// Remote read with the five-byte varint bomb.
|
|
let res = client
|
|
.post("/v1/prometheus/read")
|
|
.body(Vec::from([0xffu8, 0xff, 0xff, 0xff, 0x0f]))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
|
|
// Remote write v1 with the same bomb, via both codecs (exercising the
|
|
// fallback: the zstd attempt fails on malformed input and the snappy
|
|
// fallback must reject the declared size).
|
|
for encoding in ["snappy", "zstd"] {
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", encoding)
|
|
.body(Vec::from([0xffu8, 0xff, 0xff, 0xff, 0x0f]))
|
|
.send()
|
|
.await;
|
|
assert_eq!(
|
|
res.status(),
|
|
StatusCode::BAD_REQUEST,
|
|
"bomb with Content-Encoding {encoding} must be rejected"
|
|
);
|
|
}
|
|
|
|
// The server must still serve valid requests afterwards.
|
|
let write_request = WriteRequest {
|
|
timeseries: prom_store::mock_timeseries(),
|
|
..Default::default()
|
|
};
|
|
let compressed = prom_store::snappy_compress(&write_request.encode_to_vec()).unwrap();
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(compressed)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
/// The decoded (not encoded) size of a Prometheus remote write body must be
|
|
/// charged against the aggregate request-memory quota.
|
|
pub async fn test_prom_remote_write_decoded_body_charged(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
// 4 KiB aggregate quota with fail-fast policy.
|
|
let memory_limiter = ServerMemoryLimiter::new(4096, OnExhaustedPolicy::Fail);
|
|
let (app, mut guard) = setup_test_prom_app_with_frontend_and_memory_limiter(
|
|
store_type,
|
|
"prom_remote_write_quota",
|
|
Some(memory_limiter),
|
|
)
|
|
.await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// A valid write request whose decoded protobuf clearly exceeds the quota
|
|
// while its Snappy wire body stays far below it.
|
|
let write_request = WriteRequest {
|
|
timeseries: vec![TimeSeries {
|
|
labels: vec![
|
|
Label {
|
|
name: "__name__".to_string(),
|
|
value: "quota_metric".to_string(),
|
|
},
|
|
Label {
|
|
name: "payload".to_string(),
|
|
// ~8 KiB decoded; compresses to a few hundred bytes.
|
|
value: "p".repeat(8 * 1024),
|
|
},
|
|
],
|
|
samples: vec![Sample {
|
|
value: 1.0,
|
|
timestamp: 1000,
|
|
}],
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
};
|
|
let compressed = prom_store::snappy_compress(&write_request.encode_to_vec()).unwrap();
|
|
assert!(
|
|
compressed.len() < 2048,
|
|
"wire body must stay under the quota"
|
|
);
|
|
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(compressed)
|
|
.send()
|
|
.await;
|
|
let status = res.status();
|
|
let body = res.text().await;
|
|
assert_eq!(
|
|
status,
|
|
StatusCode::TOO_MANY_REQUESTS,
|
|
"decoded body larger than the aggregate quota must be rejected, got body: {body}"
|
|
);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
/// Decompressed ingestion bodies (routes using `RequestDecompressionLayer`)
|
|
/// must be charged against the aggregate request-memory quota as they are
|
|
/// decoded, not only their compressed wire size.
|
|
pub async fn test_influxdb_write_decoded_body_charged(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
// 4 KiB aggregate quota with fail-fast policy.
|
|
let memory_limiter = ServerMemoryLimiter::new(4096, OnExhaustedPolicy::Fail);
|
|
let http_opts = servers::http::HttpOptions {
|
|
addr: format!("127.0.0.1:{}", common_test_util::ports::get_port()),
|
|
..Default::default()
|
|
};
|
|
let (app, mut guard) = setup_test_http_app_with_frontend_and_custom_options(
|
|
store_type,
|
|
"test_influxdb_decoded_quota",
|
|
None,
|
|
Some(http_opts),
|
|
Some(memory_limiter),
|
|
)
|
|
.await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// A gzip member expanding to ~8 KiB of line protocol from a tiny wire body.
|
|
let lines: Vec<String> = (0..200)
|
|
.map(|i| format!("decoded_quota,host=h{} v=1.0,ts={}", i % 20, 1700000000 + i))
|
|
.collect();
|
|
let decoded = lines.join("\n");
|
|
assert!(decoded.len() > 4096);
|
|
let compressed = zstd::stream::encode_all(decoded.as_bytes(), 3).unwrap();
|
|
assert!(
|
|
compressed.len() < 2048,
|
|
"wire body must stay under the quota"
|
|
);
|
|
|
|
// Compressed write whose decoded body exceeds the quota: rejected while
|
|
// streaming (the extractor surfaces the aborted body as 400).
|
|
let res = client
|
|
.post("/v1/influxdb/write?db=public")
|
|
.header("Content-Type", "text/plain")
|
|
.header("Content-Encoding", "zstd")
|
|
.body(compressed)
|
|
.send()
|
|
.await;
|
|
assert_eq!(
|
|
res.status(),
|
|
StatusCode::TOO_MANY_REQUESTS,
|
|
"decoded body larger than the aggregate quota must be rejected with 429"
|
|
);
|
|
|
|
// The server still serves a small compressed write within the quota.
|
|
let small = "decoded_quota,host=ok v=1.0,ts=1700000000";
|
|
let compressed_small = zstd::stream::encode_all(small.as_bytes(), 3).unwrap();
|
|
let res = client
|
|
.post("/v1/influxdb/write?db=public")
|
|
.header("Content-Type", "text/plain")
|
|
.header("Content-Encoding", "zstd")
|
|
.body(compressed_small)
|
|
.send()
|
|
.await;
|
|
assert_eq!(
|
|
res.status(),
|
|
StatusCode::NO_CONTENT,
|
|
"small compressed write must succeed, got: {}",
|
|
res.text().await
|
|
);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
/// PromQL must behave identically regardless of the physical metric table's
|
|
/// time index unit: the same samples are remote-written into physical tables
|
|
/// pre-created with second/micro/nano time indexes (plus a millisecond
|
|
/// baseline), and every PromQL response must match the baseline exactly.
|
|
/// Regression test for <https://github.com/GreptimeTeam/greptimedb/issues/9231>.
|
|
pub async fn test_promql_over_non_millisecond_physical_tables(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_prom_app_with_frontend(store_type, "promql_non_ms_units").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let units = [
|
|
("ms", "timestamp(3)"),
|
|
("second", "timestamp(0)"),
|
|
("micro", "timestamp(6)"),
|
|
("nano", "timestamp(9)"),
|
|
];
|
|
|
|
for (suffix, ts_type) in units {
|
|
let db = format!("promql_units_{suffix}");
|
|
let res = client
|
|
.get(&format!("/v1/sql?db=public&sql=create database {db}"))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "create database {db}");
|
|
|
|
let res = client
|
|
.get(&format!(
|
|
"/v1/sql?db={db}&sql=CREATE TABLE greptime_physical_table \
|
|
(greptime_timestamp {ts_type} NOT NULL, greptime_value DOUBLE NULL, \
|
|
TIME INDEX (greptime_timestamp)) \
|
|
ENGINE = metric WITH ('physical_metric_table' = 'true')"
|
|
))
|
|
.send()
|
|
.await;
|
|
assert_eq!(
|
|
res.status(),
|
|
StatusCode::OK,
|
|
"create physical table with {ts_type} in {db}"
|
|
);
|
|
|
|
// unit_gauge{job="demo"} = 1.0 @ 1000ms, 2.0 @ 2000ms
|
|
let write_request = WriteRequest {
|
|
timeseries: vec![TimeSeries {
|
|
labels: vec![
|
|
Label {
|
|
name: prom_store::METRIC_NAME_LABEL.to_string(),
|
|
value: "unit_gauge".to_string(),
|
|
},
|
|
Label {
|
|
name: "job".to_string(),
|
|
value: "demo".to_string(),
|
|
},
|
|
],
|
|
samples: vec![
|
|
Sample {
|
|
value: 1.0,
|
|
timestamp: 1000,
|
|
},
|
|
Sample {
|
|
value: 2.0,
|
|
timestamp: 2000,
|
|
},
|
|
],
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
};
|
|
let compressed = prom_store::snappy_compress(&write_request.encode_to_vec()).unwrap();
|
|
let write_url = format!("/v1/prometheus/write?db={db}");
|
|
let res = client
|
|
.post(write_url.as_str())
|
|
.header("Content-Encoding", "snappy")
|
|
.body(compressed)
|
|
.send()
|
|
.await;
|
|
assert_eq!(
|
|
res.status(),
|
|
StatusCode::NO_CONTENT,
|
|
"remote write into {db}"
|
|
);
|
|
}
|
|
|
|
let query_paths = [
|
|
"query?query=unit_gauge&time=2",
|
|
"query_range?query=unit_gauge&start=0&end=5&step=1",
|
|
"query?query=avg_over_time(unit_gauge[1m])&time=2",
|
|
"query?query=rate(unit_gauge[1m])&time=2",
|
|
"query?query=unit_gauge[1m]&time=2",
|
|
];
|
|
for path in query_paths {
|
|
let mut baseline: Option<(String, PrometheusResponse)> = None;
|
|
for (suffix, _) in units {
|
|
let db = format!("promql_units_{suffix}");
|
|
let res = client
|
|
.get(&format!("/v1/prometheus/api/v1/{path}&db={db}"))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "promql `{path}` on {db}");
|
|
let body = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(body.status, "success", "promql `{path}` on {db}");
|
|
match &baseline {
|
|
None => baseline = Some((suffix.to_string(), body.data)),
|
|
Some((baseline_suffix, baseline_data)) => {
|
|
assert_eq!(
|
|
&body.data, baseline_data,
|
|
"promql `{path}` on {db} differs from the {baseline_suffix} baseline"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Pin the baseline responses so the cross-unit equality above cannot pass
|
|
// vacuously on wrong data: the samples are 1.0@1s and 2.0@2s, and
|
|
// `rate(unit_gauge[1m])@2s` follows Prometheus's extrapolated rate:
|
|
// Δv=1 over a 1s sampled interval extrapolated to 1.5s, divided by the
|
|
// 60s window -> 1.5/60 = 0.025 per second.
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/query?db=promql_units_ms&query=unit_gauge&time=2")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!({
|
|
"resultType": "vector",
|
|
"result": [{
|
|
"metric": {"__name__": "unit_gauge", "job": "demo"},
|
|
"value": [2.0, "2.0"]
|
|
}]
|
|
}))
|
|
.unwrap()
|
|
);
|
|
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/query_range?db=promql_units_ms&query=unit_gauge&start=0&end=5&step=1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!({
|
|
"resultType": "matrix",
|
|
"result": [{
|
|
"metric": {"__name__": "unit_gauge", "job": "demo"},
|
|
"values": [
|
|
[1.0, "1.0"], [2.0, "2.0"], [3.0, "2.0"], [4.0, "2.0"], [5.0, "2.0"]
|
|
]
|
|
}]
|
|
}))
|
|
.unwrap()
|
|
);
|
|
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/query?db=promql_units_ms&query=unit_gauge[1m]&time=2")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!({
|
|
"resultType": "matrix",
|
|
"result": [{
|
|
"metric": {"__name__": "unit_gauge", "job": "demo"},
|
|
"values": [[1.0, "1.0"], [2.0, "2.0"]]
|
|
}]
|
|
}))
|
|
.unwrap()
|
|
);
|
|
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/query?db=promql_units_ms&query=avg_over_time(unit_gauge[1m])&time=2")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(
|
|
body.data,
|
|
serde_json::from_value::<PrometheusResponse>(json!({
|
|
"resultType": "vector",
|
|
"result": [{
|
|
"metric": {"__name__": "unit_gauge", "job": "demo"},
|
|
"value": [2.0, "1.5"]
|
|
}]
|
|
}))
|
|
.unwrap()
|
|
);
|
|
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/query?db=promql_units_ms&query=rate(unit_gauge[1m])&time=2")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = res.json::<PrometheusJsonResponse>().await;
|
|
let rate = match body.data {
|
|
PrometheusResponse::PromData(data) => match data.result {
|
|
PromQueryResult::Vector(v) => v[0].value.as_ref().unwrap().1.clone(),
|
|
other => panic!("expected vector, got {other:?}"),
|
|
},
|
|
other => panic!("expected prom data, got {other:?}"),
|
|
};
|
|
assert_eq!(rate, "0.025");
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_metrics_api(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_http_app(store_type, "metrics_api").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// Send a sql
|
|
let res = client
|
|
.get("/v1/sql?sql=select * from numbers limit 10")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// Call metrics api
|
|
let res = client.get("/metrics").send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = res.text().await;
|
|
// Comment in the metrics text.
|
|
assert!(body.contains("# HELP"));
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_splunk_health(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_splunk_health").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// The HEC health endpoint returns 200 with the HEC health body.
|
|
let res = client
|
|
.get("/v1/splunk/services/collector/health")
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let json: serde_json::Value = serde_json::from_str(&res.text().await).unwrap();
|
|
assert_eq!(json["text"], "HEC is healthy");
|
|
assert_eq!(json["code"], 17);
|
|
|
|
// The versioned alias `/health/1.0` serves the same handler.
|
|
let res = client
|
|
.get("/v1/splunk/services/collector/health/1.0")
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let json: serde_json::Value = serde_json::from_str(&res.text().await).unwrap();
|
|
assert_eq!(json["text"], "HEC is healthy");
|
|
assert_eq!(json["code"], 17);
|
|
|
|
// Query parameters (e.g. `ack`, `token`) are tolerated and ignored rather
|
|
// than rejected
|
|
for path in [
|
|
"/v1/splunk/services/collector/health?ack=true&token=xyz",
|
|
"/v1/splunk/services/collector/health/1.0?ack=true&token=xyz",
|
|
] {
|
|
let res = client.get(path).send().await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
}
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_splunk_health_is_public(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
|
|
let user_provider =
|
|
user_provider_from_option("static_user_provider:cmd:greptime_user=greptime_pwd").unwrap();
|
|
let (app, _guard) = setup_test_http_app_with_frontend_and_user_provider(
|
|
store_type,
|
|
"test_splunk_health_is_public",
|
|
Some(user_provider),
|
|
)
|
|
.await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// The health endpoint is registered in `PUBLIC_API_PREFIX`, so it must
|
|
// succeed without credentials even when a user provider is configured.
|
|
let res = client
|
|
.get("/v1/splunk/services/collector/health")
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
}
|
|
|
|
#[apply(both_deployment_cases)]
|
|
async fn test_splunk_skip_wal(distributed: bool) {
|
|
let cases = vec![
|
|
HttpWalCase::new(
|
|
"Splunk event",
|
|
"/v1/splunk/services/collector/event",
|
|
"application/json",
|
|
r#"{"event":"wal test","time":1700000000,"index":"wal_splunk"}"#,
|
|
),
|
|
HttpWalCase::new(
|
|
"Splunk raw",
|
|
"/v1/splunk/services/collector/raw?index=wal_splunk_raw&time=1700000000",
|
|
"text/plain",
|
|
"wal test",
|
|
),
|
|
];
|
|
check_http_skip_wal("splunk", &cases, distributed).await;
|
|
}
|
|
|
|
pub async fn test_splunk_logs(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
|
|
let user_provider =
|
|
user_provider_from_option("static_user_provider:cmd:greptime_user=greptime_pwd").unwrap();
|
|
let (app, mut guard) = setup_test_http_app_with_frontend_and_user_provider(
|
|
store_type,
|
|
"test_splunk_logs",
|
|
Some(user_provider),
|
|
)
|
|
.await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// Authenticated SQL statement (the user-provider harness requires auth on
|
|
// /v1/sql). Posted as a form so quotes and parentheses survive.
|
|
async fn query(client: &TestClient, sql: &str) -> String {
|
|
let res = client
|
|
.post("/v1/sql")
|
|
.header("Authorization", basic_auth("greptime_user", "greptime_pwd"))
|
|
.form(&[("sql", sql)])
|
|
.send()
|
|
.await;
|
|
let status = res.status();
|
|
let body = res.text().await;
|
|
assert_eq!(status, StatusCode::OK, "query failed: {sql}: {body}");
|
|
// A failing statement still comes back as 200 with an `error` envelope.
|
|
assert!(!body.contains("\"error\""), "query failed: {sql}: {body}");
|
|
body
|
|
}
|
|
|
|
// HEC `Authorization: Splunk <user:pass>` + JSON content type.
|
|
let splunk_headers = || {
|
|
vec![
|
|
(
|
|
HeaderName::from_static("authorization"),
|
|
HeaderValue::from_static("Splunk greptime_user:greptime_pwd"),
|
|
),
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/json"),
|
|
),
|
|
]
|
|
};
|
|
let event_path = "/v1/splunk/services/collector/event";
|
|
|
|
// 1. Ingest a HEC batch (no `index` -> default `splunk_logs` table).
|
|
let body = concat!(
|
|
r#"{"event":"login ok","time":1700000000,"host":"web-01","source":"auth.log","sourcetype":"syslog","fields":{"region":"us-east"}}"#,
|
|
"\n",
|
|
r#"{"event":"login fail","time":1700000001,"host":"web-02","source":"auth.log","sourcetype":"syslog","fields":{"region":"us-west"}}"#,
|
|
);
|
|
let res = send_req(
|
|
&client,
|
|
splunk_headers(),
|
|
event_path,
|
|
body.as_bytes().to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
assert!(res.text().await.contains("\"code\":0"));
|
|
|
|
// 2. Rows landed with the right values (proves route + auth + parse + insert,
|
|
// and that the default table name is `splunk_logs`).
|
|
let rows = get_rows_from_output(
|
|
&query(
|
|
&client,
|
|
"select host, region, event, greptime_timestamp from splunk_logs order by host",
|
|
)
|
|
.await,
|
|
);
|
|
// `time` (epoch seconds) maps to the nanosecond timestamp column.
|
|
assert_eq!(
|
|
rows,
|
|
r#"[["web-01","us-east","login ok",1700000000000000000],["web-02","us-west","login fail",1700000001000000000]]"#
|
|
);
|
|
|
|
// 3. host/source/sourcetype + `fields` keys are tags (i.e. primary key).
|
|
let create = query(&client, "show create table splunk_logs").await;
|
|
let pk = create
|
|
.split("PRIMARY KEY")
|
|
.nth(1)
|
|
.expect("splunk_logs should have a PRIMARY KEY");
|
|
for col in ["host", "source", "sourcetype", "region"] {
|
|
assert!(
|
|
pk.contains(col),
|
|
"expected `{col}` in primary key: {create}"
|
|
);
|
|
}
|
|
// host/source/sourcetype lead the primary key (ahead of the `fields` tag `region`),
|
|
let pos = |col: &str| pk.find(col).expect("column missing from primary key");
|
|
assert!(
|
|
pos("host") < pos("source")
|
|
&& pos("source") < pos("sourcetype")
|
|
&& pos("sourcetype") < pos("region"),
|
|
"expected host/source/sourcetype to lead the primary key: {create}"
|
|
);
|
|
|
|
// 4. A brand-new `fields` key on a later write also becomes a tag (dynamic
|
|
// tag column added to the existing table's primary key).
|
|
let body2 = r#"{"event":"deploy","time":1700000002,"host":"web-03","source":"deploy.log","sourcetype":"syslog","fields":{"datacenter":"dc1"}}"#;
|
|
let res = send_req(
|
|
&client,
|
|
splunk_headers(),
|
|
event_path,
|
|
body2.as_bytes().to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let create = query(&client, "show create table splunk_logs").await;
|
|
let pk = create.split("PRIMARY KEY").nth(1).unwrap();
|
|
assert!(
|
|
pk.contains("datacenter"),
|
|
"expected dynamically-added `datacenter` in primary key: {create}"
|
|
);
|
|
|
|
// 5. An invalid `?table=` override returns HEC code 7 ("incorrect index").
|
|
let res = send_req(
|
|
&client,
|
|
splunk_headers(),
|
|
"/v1/splunk/services/collector/event?table=bad%20name",
|
|
br#"{"event":"x","time":1700000003}"#.to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::BAD_REQUEST, res.status());
|
|
assert!(res.text().await.contains("\"code\":7"));
|
|
|
|
// 6. A custom pipeline (via `x-greptime-pipeline-name`) overrides identity and
|
|
// owns the schema: this one keeps host/event/timestamp and drops source/sourcetype.
|
|
let pipeline_yaml = r#"
|
|
transform:
|
|
- field: host
|
|
type: string
|
|
index: tag
|
|
- field: event
|
|
type: string
|
|
- field: greptime_timestamp
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
let res = client
|
|
.post("/v1/pipelines/splunk_custom")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.header("Authorization", basic_auth("greptime_user", "greptime_pwd"))
|
|
.body(pipeline_yaml)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "create pipeline failed");
|
|
|
|
let mut headers = splunk_headers();
|
|
headers.push((
|
|
HeaderName::from_static("x-greptime-pipeline-name"),
|
|
HeaderValue::from_static("splunk_custom"),
|
|
));
|
|
let res = send_req(
|
|
&client,
|
|
headers,
|
|
"/v1/splunk/services/collector/event?table=splunk_custom_tbl",
|
|
br#"{"event":"hi","time":1700000010,"host":"web-09","source":"s","sourcetype":"st"}"#
|
|
.to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let create = query(&client, "show create table splunk_custom_tbl").await;
|
|
assert!(
|
|
create.contains("host"),
|
|
"custom pipeline kept host: {create}"
|
|
);
|
|
assert!(
|
|
!create.contains("sourcetype"),
|
|
"custom pipeline should have dropped sourcetype (identity would keep it): {create}"
|
|
);
|
|
|
|
// 6b. Ingest into a pre-created table with a fulltext index, then flush: the
|
|
// index file is only written on flush, so this is the one path that reads it
|
|
// back. `matches_term` is the predicate the index applier recognises.
|
|
query(
|
|
&client,
|
|
"create table splunk_indexed (greptime_timestamp timestamp(9) time index, \
|
|
\"event\" string fulltext index with(backend='bloom'))",
|
|
)
|
|
.await;
|
|
let res = send_req(
|
|
&client,
|
|
splunk_headers(),
|
|
"/v1/splunk/services/collector/event?table=splunk_indexed",
|
|
br#"{"event":"disk pressure on web-01","time":1700000020}"#.to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
query(&client, "ADMIN FLUSH_TABLE('splunk_indexed')").await;
|
|
let rows = get_rows_from_output(
|
|
&query(
|
|
&client,
|
|
"select \"event\" from splunk_indexed where matches_term(\"event\", 'pressure')",
|
|
)
|
|
.await,
|
|
);
|
|
assert_eq!(rows, r#"[["disk pressure on web-01"]]"#);
|
|
|
|
// 7. Auth failures return HEC codes: missing token -> 2 (401), bad token -> 4 (403).
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/json"),
|
|
)],
|
|
event_path,
|
|
br#"{"event":"x","time":1700000020}"#.to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::UNAUTHORIZED, res.status());
|
|
assert!(res.text().await.contains("\"code\":2"));
|
|
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("authorization"),
|
|
HeaderValue::from_static("Splunk baduser:badpass"),
|
|
),
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/json"),
|
|
),
|
|
],
|
|
event_path,
|
|
br#"{"event":"x","time":1700000021}"#.to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::FORBIDDEN, res.status());
|
|
assert!(res.text().await.contains("\"code\":4"));
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_splunk_raw(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
|
|
let user_provider =
|
|
user_provider_from_option("static_user_provider:cmd:greptime_user=greptime_pwd").unwrap();
|
|
let (app, mut guard) = setup_test_http_app_with_frontend_and_user_provider(
|
|
store_type,
|
|
"test_splunk_raw",
|
|
Some(user_provider),
|
|
)
|
|
.await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
async fn query(client: &TestClient, sql: &str) -> String {
|
|
let res = client
|
|
.get(format!("/v1/sql?sql={sql}").as_str())
|
|
.header("Authorization", basic_auth("greptime_user", "greptime_pwd"))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "query failed: {sql}");
|
|
res.text().await
|
|
}
|
|
|
|
// HEC `Authorization: Splunk <user:pass>` + plain-text content type (raw bodies).
|
|
let splunk_headers = || {
|
|
vec![
|
|
(
|
|
HeaderName::from_static("authorization"),
|
|
HeaderValue::from_static("Splunk greptime_user:greptime_pwd"),
|
|
),
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("text/plain"),
|
|
),
|
|
]
|
|
};
|
|
let raw_path = "/v1/splunk/services/collector/raw";
|
|
|
|
// 1. Explicit `?linebreaker=%0A` ("\n") splits the body into one event per
|
|
// line, with request-level metadata; `channel` (param AND header) is
|
|
// accepted and ignored (ack protocol not implemented); `?time=` sets the
|
|
// timestamp for every event; `index` routes the table. Blank lines are
|
|
// skipped; indentation inside a line is preserved verbatim.
|
|
let mut headers = splunk_headers();
|
|
headers.push((
|
|
HeaderName::from_static("x-splunk-request-channel"),
|
|
HeaderValue::from_static("FE0ECFAD-13D5-401B-847D-77833BD77131"),
|
|
));
|
|
let body = "line one\nline two\n\n indented line";
|
|
let res = send_req(
|
|
&client,
|
|
headers,
|
|
&format!(
|
|
"{raw_path}?channel=FE0ECFAD-13D5-401B-847D-77833BD77131\
|
|
&host=web-01&source=nginx&sourcetype=access&index=raw_main&time=1700000100\
|
|
&linebreaker=%0A"
|
|
),
|
|
body.as_bytes().to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
assert!(res.text().await.contains("\"code\":0"));
|
|
|
|
// 2. Rows landed: `message` holds each line verbatim, metadata columns carry the
|
|
// query-param values, `?time=` (epoch seconds) became the timestamp.
|
|
let rows = get_rows_from_output(
|
|
&query(
|
|
&client,
|
|
"select host, source, sourcetype, message, greptime_timestamp from raw_main order by message",
|
|
)
|
|
.await,
|
|
);
|
|
assert_eq!(
|
|
rows,
|
|
concat!(
|
|
r#"[["web-01","nginx","access"," indented line",1700000100000000000],"#,
|
|
r#"["web-01","nginx","access","line one",1700000100000000000],"#,
|
|
r#"["web-01","nginx","access","line two",1700000100000000000]]"#
|
|
)
|
|
);
|
|
|
|
// 3. host/source/sourcetype are tags (primary key); `message` is not.
|
|
let create = query(&client, "show create table raw_main").await;
|
|
let pk = create
|
|
.split("PRIMARY KEY")
|
|
.nth(1)
|
|
.expect("raw_main should have a PRIMARY KEY");
|
|
for col in ["host", "source", "sourcetype"] {
|
|
assert!(
|
|
pk.contains(col),
|
|
"expected `{col}` in primary key: {create}"
|
|
);
|
|
}
|
|
assert!(
|
|
!pk.contains("message"),
|
|
"`message` must not be a tag: {create}"
|
|
);
|
|
|
|
// 4. No query params at all: default table (`splunk_logs`), only timestamp +
|
|
// `message` columns, ingest-time timestamp.
|
|
let res = send_req(
|
|
&client,
|
|
splunk_headers(),
|
|
raw_path,
|
|
b"Hello World".to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let rows = get_rows_from_output(&query(&client, "select message from splunk_logs").await);
|
|
assert_eq!(rows, r#"[["Hello World"]]"#);
|
|
|
|
// 4b. Without `?linebreaker=`, a multiline body (e.g. a stack trace) is stored
|
|
// as it is.
|
|
let stack_trace = "java.lang.NullPointerException: boom\n\tat com.example.Foo.bar(Foo.java:42)\n\tat com.example.Main.main(Main.java:7)";
|
|
let res = send_req(
|
|
&client,
|
|
splunk_headers(),
|
|
&format!("{raw_path}?table=raw_multiline"),
|
|
stack_trace.as_bytes().to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let rows = get_rows_from_output(&query(&client, "select message from raw_multiline").await);
|
|
assert_eq!(
|
|
rows,
|
|
r#"[["java.lang.NullPointerException: boom\n\tat com.example.Foo.bar(Foo.java:42)\n\tat com.example.Main.main(Main.java:7)"]]"#
|
|
);
|
|
|
|
// 5. gzip-compressed raw body on the versioned alias (exercises the
|
|
// decompression layer and the `/raw/1.0` route).
|
|
let res = send_req(
|
|
&client,
|
|
splunk_headers(),
|
|
"/v1/splunk/services/collector/raw/1.0?table=raw_gzip",
|
|
b"compressed line".to_vec(),
|
|
true,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let rows = get_rows_from_output(&query(&client, "select message from raw_gzip").await);
|
|
assert_eq!(rows, r#"[["compressed line"]]"#);
|
|
|
|
// 6. Error paths: empty body -> 5 ("No data"); unparsable `?time=` -> 6;
|
|
// invalid `?table=` -> 7 ("incorrect index").
|
|
let res = send_req(
|
|
&client,
|
|
splunk_headers(),
|
|
raw_path,
|
|
b" \n ".to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::BAD_REQUEST, res.status());
|
|
assert!(res.text().await.contains("\"code\":5"));
|
|
|
|
let res = send_req(
|
|
&client,
|
|
splunk_headers(),
|
|
&format!("{raw_path}?time=not-a-time"),
|
|
b"x".to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::BAD_REQUEST, res.status());
|
|
assert!(res.text().await.contains("\"code\":6"));
|
|
|
|
let res = send_req(
|
|
&client,
|
|
splunk_headers(),
|
|
&format!("{raw_path}?table=bad%20name"),
|
|
b"x".to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::BAD_REQUEST, res.status());
|
|
assert!(res.text().await.contains("\"code\":7"));
|
|
|
|
// 7. Replay of a real Vector `splunk_hec_logs` (endpoint_target = "raw") request,
|
|
// path and headers verbatim from a wire capture (body trimmed):
|
|
let mut headers = splunk_headers();
|
|
headers.push((
|
|
HeaderName::from_static("x-splunk-request-channel"),
|
|
HeaderValue::from_static("b408271e-51af-43c1-a99f-9c21f78df0cf"),
|
|
));
|
|
let res = send_req(
|
|
&client,
|
|
headers,
|
|
"/v1/splunk/services/collector/raw?source=vector%2Dsrc&sourcetype=vector%5Fdemo&index=vector_raw&host=localhost",
|
|
b"245.158.191.1 - AnthraX [13/Jul/2026:05:10:26 +0000] \"GET /wp-admin HTTP/1.0\" 300 17922".to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let rows = get_rows_from_output(
|
|
&query(
|
|
&client,
|
|
"select host, source, sourcetype, message from vector_raw",
|
|
)
|
|
.await,
|
|
);
|
|
// the percent-encoded params (`vector%2Dsrc` etc.) decode to the plain values.
|
|
assert_eq!(
|
|
rows,
|
|
r#"[["localhost","vector-src","vector_demo","245.158.191.1 - AnthraX [13/Jul/2026:05:10:26 +0000] \"GET /wp-admin HTTP/1.0\" 300 17922"]]"#
|
|
);
|
|
|
|
// 8. An unknown `Content-Encoding` passes through the decompression layer
|
|
// still compressed (`pass_through_unaccepted(true)`); the handler must reject
|
|
// it (code 6) instead of ingesting compressed bytes. `identity` is fine.
|
|
// (Known-but-broken encodings, e.g. `zstd` over gzip bytes, already fail in
|
|
// the layer itself.)
|
|
// Invalid UTF-8 must also be rejected with code 6, not lossily replaced.
|
|
let mut headers = splunk_headers();
|
|
headers.push((
|
|
HeaderName::from_static("content-encoding"),
|
|
HeaderValue::from_static("snappy"),
|
|
));
|
|
let res = send_req(
|
|
&client,
|
|
headers,
|
|
&format!("{raw_path}?table=raw_encoding"),
|
|
compress_vec_with_gzip(b"still compressed".to_vec()),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::BAD_REQUEST, res.status());
|
|
assert!(res.text().await.contains("\"code\":6"));
|
|
|
|
let mut headers = splunk_headers();
|
|
headers.push((
|
|
HeaderName::from_static("content-encoding"),
|
|
HeaderValue::from_static("identity"),
|
|
));
|
|
let res = send_req(
|
|
&client,
|
|
headers,
|
|
&format!("{raw_path}?table=raw_encoding"),
|
|
b"identity line".to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let rows = get_rows_from_output(&query(&client, "select message from raw_encoding").await);
|
|
assert_eq!(rows, r#"[["identity line"]]"#);
|
|
|
|
let res = send_req(
|
|
&client,
|
|
splunk_headers(),
|
|
&format!("{raw_path}?table=raw_bad_utf8"),
|
|
vec![b'h', b'i', 0xff, 0xfe],
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::BAD_REQUEST, res.status());
|
|
assert!(res.text().await.contains("\"code\":6"));
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_health_api(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, _guard) = setup_test_http_app_with_frontend(store_type, "health_api").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
async fn health_api(client: &TestClient, endpoint: &str) {
|
|
// we can call health api with both `GET` and `POST` method.
|
|
let res_post = client.post(endpoint).send().await;
|
|
assert_eq!(res_post.status(), StatusCode::OK);
|
|
let res_get = client.get(endpoint).send().await;
|
|
assert_eq!(res_get.status(), StatusCode::OK);
|
|
|
|
// both `GET` and `POST` method return same result
|
|
let body_text = res_post.text().await;
|
|
assert_eq!(body_text, res_get.text().await);
|
|
|
|
// currently health api simply returns an empty json `{}`, which can be deserialized to an empty `HealthResponse`
|
|
assert_eq!(body_text, "{}");
|
|
|
|
let body = serde_json::from_str::<HealthResponse>(&body_text).unwrap();
|
|
assert_eq!(body, HealthResponse {});
|
|
}
|
|
|
|
health_api(&client, "/health").await;
|
|
health_api(&client, "/ready").await;
|
|
}
|
|
|
|
pub async fn test_status_api(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, _guard) = setup_test_http_app_with_frontend(store_type, "status_api").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let res_get = client.get("/status").send().await;
|
|
assert_eq!(res_get.status(), StatusCode::OK);
|
|
|
|
let res_body = res_get.text().await;
|
|
assert!(res_body.contains("\"commit\":"));
|
|
assert!(res_body.contains("\"branch\":"));
|
|
assert!(res_body.contains("\"rustc_version\":"));
|
|
assert!(res_body.contains("\"hostname\":"));
|
|
assert!(res_body.contains("\"version\":"));
|
|
}
|
|
|
|
pub async fn test_config_api(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, _guard) = setup_test_http_app_with_frontend(store_type, "config_api").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let res_get = client.get("/config").send().await;
|
|
assert_eq!(res_get.status(), StatusCode::OK);
|
|
|
|
let storage = if store_type != StorageType::File {
|
|
format!(
|
|
r#"[storage]
|
|
probe_on_startup = false
|
|
type = "{}"
|
|
providers = []
|
|
|
|
[storage.http_client]
|
|
pool_max_idle_per_host = 1024
|
|
connect_timeout = "30s"
|
|
timeout = "30s"
|
|
pool_idle_timeout = "1m 30s"
|
|
skip_ssl_validation = false"#,
|
|
store_type
|
|
)
|
|
} else {
|
|
format!(
|
|
r#"[storage]
|
|
probe_on_startup = false
|
|
type = "{}"
|
|
providers = []"#,
|
|
store_type
|
|
)
|
|
};
|
|
|
|
let expected_toml_str = format!(
|
|
r#"
|
|
enable_telemetry = true
|
|
auto_create_table = true
|
|
auto_create_physical_table_timestamp_unit = "Millisecond"
|
|
max_in_flight_write_bytes = "0KiB"
|
|
write_bytes_exhausted_policy = "wait"
|
|
init_regions_in_background = false
|
|
init_regions_parallelism = 16
|
|
max_concurrent_queries = 0
|
|
concurrent_query_limiter_timeout = "100ms"
|
|
heartbeat_env_vars = []
|
|
|
|
[http]
|
|
addr = "127.0.0.1:4000"
|
|
timeout = "0s"
|
|
body_limit = "64MiB"
|
|
cors_allowed_origins = []
|
|
enable_cors = true
|
|
enable_api_server = false
|
|
api_server_addr = "127.0.0.1:4006"
|
|
|
|
[grpc]
|
|
bind_addr = "127.0.0.1:4001"
|
|
server_addr = "127.0.0.1:4001"
|
|
max_recv_message_size = "512MiB"
|
|
max_send_message_size = "512MiB"
|
|
flight_compression = "arrow_ipc"
|
|
runtime_size = 8
|
|
http2_keep_alive_interval = "10s"
|
|
http2_keep_alive_timeout = "3s"
|
|
enable_cors = false
|
|
cors_allowed_origins = []
|
|
|
|
[grpc.tls]
|
|
mode = "disable"
|
|
cert_path = ""
|
|
key_path = ""
|
|
ca_cert_path = ""
|
|
watch = false
|
|
|
|
[mysql]
|
|
enable = true
|
|
addr = "127.0.0.1:4002"
|
|
runtime_size = 2
|
|
keep_alive = "0s"
|
|
prepared_stmt_cache_size = 10000
|
|
|
|
[mysql.tls]
|
|
mode = "disable"
|
|
cert_path = ""
|
|
key_path = ""
|
|
ca_cert_path = ""
|
|
watch = false
|
|
|
|
[postgres]
|
|
enable = true
|
|
addr = "127.0.0.1:4003"
|
|
runtime_size = 2
|
|
keep_alive = "0s"
|
|
|
|
[postgres.tls]
|
|
mode = "disable"
|
|
cert_path = ""
|
|
key_path = ""
|
|
ca_cert_path = ""
|
|
watch = false
|
|
|
|
[opentsdb]
|
|
enable = true
|
|
|
|
[influxdb]
|
|
enable = true
|
|
default_merge_mode = "last_non_null"
|
|
|
|
[pending_rows_batcher]
|
|
protocols = []
|
|
pending_rows_flush_interval = "0s"
|
|
max_batch_rows = 100000
|
|
max_concurrent_flushes = 256
|
|
worker_channel_capacity = 65536
|
|
max_inflight_requests = 3000
|
|
flow_notification_queue_capacity = 1024
|
|
|
|
[jaeger]
|
|
enable = true
|
|
|
|
[otlp]
|
|
enable = true
|
|
trace_ingest_chunk_size = 512
|
|
experimental_enable_resource_info = true
|
|
|
|
[prom_store]
|
|
enable = true
|
|
with_metric_engine = true
|
|
prom_validation_mode = "strict"
|
|
pending_rows_flush_interval = "0s"
|
|
max_batch_rows = 100000
|
|
max_concurrent_flushes = 256
|
|
worker_channel_capacity = 65536
|
|
max_inflight_requests = 3000
|
|
flow_notification_queue_capacity = 1024
|
|
|
|
[wal]
|
|
provider = "raft_engine"
|
|
file_size = "128MiB"
|
|
purge_threshold = "1GiB"
|
|
purge_interval = "1m"
|
|
read_batch_size = 128
|
|
sync_write = false
|
|
enable_log_recycle = true
|
|
prefill_log_files = false
|
|
|
|
{storage}
|
|
|
|
[metadata_store]
|
|
file_size = "64MiB"
|
|
purge_threshold = "256MiB"
|
|
purge_interval = "1m"
|
|
|
|
[procedure]
|
|
max_retry_times = 3
|
|
retry_delay = "500ms"
|
|
max_running_procedures = 128
|
|
|
|
[flow]
|
|
|
|
[flow.batching_mode]
|
|
query_timeout = "10m"
|
|
slow_query_threshold = "1m"
|
|
experimental_min_refresh_duration = "5s"
|
|
grpc_conn_timeout = "5s"
|
|
experimental_flight_do_get_timeout = "10s"
|
|
experimental_grpc_max_retries = 3
|
|
experimental_frontend_scan_timeout = "30s"
|
|
experimental_max_filter_num_per_query = 20
|
|
experimental_time_window_merge_threshold = 3
|
|
experimental_enable_incremental_read = false
|
|
read_preference = "Leader"
|
|
|
|
[logging]
|
|
max_log_files = 720
|
|
max_log_dir_size = "0KiB"
|
|
append_stdout = true
|
|
enable_file_logging = true
|
|
enable_otlp_tracing = false
|
|
enable_per_region_metrics = false
|
|
|
|
[[region_engine]]
|
|
|
|
[region_engine.mito]
|
|
worker_channel_size = 128
|
|
worker_request_batch_size = 64
|
|
manifest_checkpoint_distance = 10
|
|
experimental_manifest_keep_removed_file_count = 256
|
|
experimental_manifest_keep_removed_file_ttl = "1h"
|
|
compress_manifest = false
|
|
experimental_enable_series_index = false
|
|
experimental_series_index_format = "legacy"
|
|
experimental_series_index_max_size = "5GiB"
|
|
experimental_enable_range_index = false
|
|
experimental_series_index_maintenance_interval = "5m"
|
|
experimental_series_index_bucket_width = "5days"
|
|
experimental_compaction_memory_limit = "unlimited"
|
|
experimental_compaction_on_exhausted = "wait"
|
|
experimental_compaction_output_parallelism = 1
|
|
auto_flush_interval = "10m"
|
|
default_region_write_buffer_size = "0KiB"
|
|
enable_write_cache = false
|
|
write_cache_path = ""
|
|
write_cache_size = "5GiB"
|
|
preload_index_cache = true
|
|
index_cache_percent = 20
|
|
enable_refill_cache_on_read = true
|
|
manifest_cache_size = "256MiB"
|
|
sst_write_buffer_size = "8MiB"
|
|
max_concurrent_scan_files = 384
|
|
allow_stale_entries = false
|
|
scan_memory_limit = "25%"
|
|
scan_memory_mode = "basic"
|
|
scan_memory_on_exhausted = "fail"
|
|
min_compaction_interval = "0s"
|
|
schedule_compaction_after_edit = true
|
|
default_flat_format = true
|
|
experimental_series_scan_v2 = false
|
|
|
|
[region_engine.mito.index]
|
|
aux_path = ""
|
|
staging_size = "2GiB"
|
|
staging_ttl = "7days"
|
|
build_mode = "sync"
|
|
write_buffer_size = "8MiB"
|
|
content_cache_page_size = "64KiB"
|
|
|
|
[region_engine.mito.inverted_index]
|
|
create_on_flush = "auto"
|
|
create_on_compaction = "auto"
|
|
apply_on_query = "auto"
|
|
mem_threshold_on_create = "auto"
|
|
|
|
[region_engine.mito.fulltext_index]
|
|
create_on_flush = "auto"
|
|
create_on_compaction = "auto"
|
|
apply_on_query = "auto"
|
|
mem_threshold_on_create = "auto"
|
|
compress = true
|
|
|
|
[region_engine.mito.bloom_filter_index]
|
|
create_on_flush = "auto"
|
|
create_on_compaction = "auto"
|
|
apply_on_query = "auto"
|
|
mem_threshold_on_create = "auto"
|
|
|
|
[region_engine.mito.gc]
|
|
enable = false
|
|
lingering_time = "1h"
|
|
unknown_file_lingering_time = "1day"
|
|
max_concurrent_lister_per_gc_job = 32
|
|
max_concurrent_gc_job = 4
|
|
|
|
[[region_engine]]
|
|
|
|
[region_engine.file]
|
|
|
|
[tracing]
|
|
|
|
[slow_query]
|
|
enable = true
|
|
record_type = "system_table"
|
|
threshold = "1s"
|
|
sample_ratio = 1.0
|
|
ttl = "2months 29days 2h 52m 48s"
|
|
|
|
[query]
|
|
parallelism = 0
|
|
allow_query_fallback = false
|
|
experimental_spill_mode = "default"
|
|
experimental_spill_max_temp_directory_size = "1GiB"
|
|
experimental_spill_compression = "uncompressed"
|
|
experimental_memory_pool_policy = "greedy"
|
|
experimental_native_promql = false
|
|
|
|
[memory]
|
|
enable_heap_profiling = true
|
|
|
|
[pipeline]
|
|
cache_ttl = "10s"
|
|
allow_vrl_host_access = false
|
|
ignore_errors = false
|
|
|
|
[event_recorder]
|
|
ttl = "2months 29days 2h 52m 48s"
|
|
"#,
|
|
)
|
|
.trim()
|
|
.to_string();
|
|
let body_text = drop_lines_with_inconsistent_results(res_get.text().await);
|
|
similar_asserts::assert_eq!(expected_toml_str, body_text);
|
|
}
|
|
|
|
fn drop_lines_with_inconsistent_results(input: String) -> String {
|
|
let inconsistent_results = [
|
|
"dir =",
|
|
"log_format =",
|
|
"data_home =",
|
|
"bucket =",
|
|
"root =",
|
|
"endpoint =",
|
|
"region =",
|
|
"enable_virtual_host_style =",
|
|
"disable_ec2_metadata =",
|
|
"cache_path =",
|
|
"cache_capacity =",
|
|
"trace_aux_cache_size =",
|
|
"memory_pool_size =",
|
|
"sas_token =",
|
|
"scope =",
|
|
"num_workers =",
|
|
"scan_parallelism =",
|
|
"global_write_buffer_size =",
|
|
"global_write_buffer_reject_size =",
|
|
"sst_meta_cache_size =",
|
|
"vector_cache_size =",
|
|
"page_cache_size =",
|
|
"selector_result_cache_size =",
|
|
"range_result_cache_size =",
|
|
"prefilter_result_cache_size =",
|
|
"metadata_cache_size =",
|
|
"content_cache_size =",
|
|
"result_cache_size =",
|
|
"name =",
|
|
"recovery_parallelism =",
|
|
"max_background_index_builds =",
|
|
"max_background_flushes =",
|
|
"max_background_compactions =",
|
|
"max_background_purges =",
|
|
"enable_read_cache =",
|
|
"max_total_body_memory =",
|
|
"max_total_message_memory =",
|
|
];
|
|
|
|
input
|
|
.lines()
|
|
.filter(|line| {
|
|
// ignores
|
|
let line = line.trim();
|
|
for prefix in inconsistent_results {
|
|
if line.starts_with(prefix) {
|
|
return false;
|
|
}
|
|
}
|
|
true
|
|
})
|
|
.collect::<Vec<&str>>()
|
|
.join(
|
|
"
|
|
",
|
|
)
|
|
}
|
|
|
|
pub async fn test_dynamic_tracer_toggle(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
|
|
let (app, mut guard) = setup_test_http_app(store_type, "test_dynamic_tracer_toggle").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let disable_resp = client
|
|
.post("/debug/enable_trace")
|
|
.body("false")
|
|
.send()
|
|
.await;
|
|
assert_eq!(disable_resp.status(), StatusCode::OK);
|
|
assert_eq!(disable_resp.text().await, "trace disabled");
|
|
|
|
let enable_resp = client.post("/debug/enable_trace").body("true").send().await;
|
|
assert_eq!(enable_resp.status(), StatusCode::OK);
|
|
assert_eq!(enable_resp.text().await, "trace enabled");
|
|
|
|
let cleanup_resp = client
|
|
.post("/debug/enable_trace")
|
|
.body("false")
|
|
.send()
|
|
.await;
|
|
assert_eq!(cleanup_resp.status(), StatusCode::OK);
|
|
assert_eq!(cleanup_resp.text().await, "trace disabled");
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
#[cfg(feature = "dashboard")]
|
|
pub async fn test_dashboard_path(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, _guard) = setup_test_http_app_with_frontend(store_type, "dashboard_path").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let res_get = client.get("/dashboard").send().await;
|
|
assert_eq!(res_get.status(), StatusCode::PERMANENT_REDIRECT);
|
|
let res_post = client.post("/dashboard/").send().await;
|
|
assert_eq!(res_post.status(), StatusCode::OK);
|
|
let res_get = client.get("/dashboard/").send().await;
|
|
assert_eq!(res_get.status(), StatusCode::OK);
|
|
|
|
// both `GET` and `POST` method return same result
|
|
let body_text = res_post.text().await;
|
|
assert_eq!(body_text, res_get.text().await);
|
|
}
|
|
|
|
#[cfg(not(feature = "dashboard"))]
|
|
pub async fn test_dashboard_path(_: StorageType) {}
|
|
|
|
#[cfg(feature = "dashboard")]
|
|
pub async fn test_dashboard_api(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_http_app_with_frontend(store_type, "dashboard_api").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// 1. List dashboards - should be empty initially
|
|
let res = client.get("/v1/dashboards").send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body: Value = res.json().await;
|
|
let dashboards = body.get("dashboards").unwrap().as_array().unwrap();
|
|
assert!(dashboards.is_empty());
|
|
|
|
// 2. Save a dashboard
|
|
let dashboard_definition = r#"{"title": "My Dashboard", "panels": []}"#;
|
|
let res = client
|
|
.post("/v1/dashboards/test_dashboard")
|
|
.body(dashboard_definition)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body: Value = res.json().await;
|
|
let dashboards = body.get("dashboards").unwrap().as_array().unwrap();
|
|
assert_eq!(dashboards.len(), 1);
|
|
assert_eq!(dashboards[0].get("name").unwrap(), "test_dashboard");
|
|
|
|
// 3. Save another dashboard
|
|
let res = client
|
|
.post("/v1/dashboards/another_dashboard")
|
|
.body(r#"{"title": "Another Dashboard"}"#)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 4. List dashboards - should have 2
|
|
let res = client.get("/v1/dashboards").send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body: Value = res.json().await;
|
|
let dashboards = body.get("dashboards").unwrap().as_array().unwrap();
|
|
assert_eq!(dashboards.len(), 2);
|
|
|
|
let names: Vec<&str> = dashboards
|
|
.iter()
|
|
.map(|d| d.get("name").unwrap().as_str().unwrap())
|
|
.collect();
|
|
assert!(names.contains(&"test_dashboard"));
|
|
assert!(names.contains(&"another_dashboard"));
|
|
|
|
// 5. Update a dashboard by posting again with new definition
|
|
let updated_definition = r#"{"title": "Updated Dashboard", "panels": [{"id": 1}]}"#;
|
|
let res = client
|
|
.post("/v1/dashboards/test_dashboard")
|
|
.body(updated_definition)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let body: Value = res.json().await;
|
|
let dashboards = body.get("dashboards").unwrap().as_array().unwrap();
|
|
assert_eq!(dashboards.len(), 1);
|
|
assert_eq!(dashboards[0].get("name").unwrap(), "test_dashboard");
|
|
|
|
// Verify the definition was updated by listing again
|
|
let res = client.get("/v1/dashboards").send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body: Value = res.json().await;
|
|
let dashboards = body.get("dashboards").unwrap().as_array().unwrap();
|
|
assert_eq!(dashboards.len(), 2);
|
|
|
|
// Find test_dashboard and verify it has updated definition
|
|
let test_db = dashboards
|
|
.iter()
|
|
.find(|d| d.get("name").unwrap() == "test_dashboard")
|
|
.unwrap();
|
|
assert_eq!(
|
|
test_db.get("definition").unwrap(),
|
|
r#"{"title": "Updated Dashboard", "panels": [{"id": 1}]}"#
|
|
);
|
|
|
|
// 6. Delete one dashboard
|
|
let res = client.delete("/v1/dashboards/test_dashboard").send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body: Value = res.json().await;
|
|
let dashboards = body.get("dashboards").unwrap().as_array().unwrap();
|
|
assert_eq!(dashboards.len(), 1);
|
|
assert_eq!(dashboards[0].get("name").unwrap(), "test_dashboard");
|
|
|
|
// 7. List dashboards - should have 1
|
|
let res = client.get("/v1/dashboards").send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body: Value = res.json().await;
|
|
let dashboards = body.get("dashboards").unwrap().as_array().unwrap();
|
|
assert_eq!(dashboards.len(), 1);
|
|
assert_eq!(dashboards[0].get("name").unwrap(), "another_dashboard");
|
|
|
|
// 8. Delete the remaining dashboard
|
|
let res = client
|
|
.delete("/v1/dashboards/another_dashboard")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 9. List dashboards - should be empty
|
|
let res = client.get("/v1/dashboards").send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body: Value = res.json().await;
|
|
let dashboards = body.get("dashboards").unwrap().as_array().unwrap();
|
|
assert!(dashboards.is_empty());
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
#[cfg(not(feature = "dashboard"))]
|
|
pub async fn test_dashboard_api(_: StorageType) {}
|
|
|
|
#[apply(both_deployment_cases)]
|
|
async fn test_prometheus_skip_wal(distributed: bool) {
|
|
let mut cases = Vec::new();
|
|
let prom = api::prom_store::remote::WriteRequest {
|
|
timeseries: prom_store::mock_timeseries(),
|
|
..Default::default()
|
|
};
|
|
let mut prom_case = HttpWalCase::new(
|
|
"Prometheus v1",
|
|
"/v1/prometheus/write",
|
|
"application/x-protobuf",
|
|
prom_store::snappy_compress(&prom.encode_to_vec()).unwrap(),
|
|
);
|
|
prom_case.headers.push(("content-encoding", "snappy"));
|
|
cases.push(prom_case);
|
|
let prom_v2 = servers::prom_remote_write::v2::test_util::request_with_labels_and_samples(
|
|
vec![
|
|
(prom_store::METRIC_NAME_LABEL, "wal_prom_v2"),
|
|
("host", "a"),
|
|
],
|
|
vec![api::greptime_proto::io::prometheus::write::v2::Sample {
|
|
value: 1.0,
|
|
timestamp: 1700000000000,
|
|
start_timestamp: 0,
|
|
}],
|
|
);
|
|
let mut prom_v2_case = HttpWalCase::new(
|
|
"Prometheus v2",
|
|
"/v1/prometheus/write",
|
|
"application/x-protobuf;proto=io.prometheus.write.v2.Request",
|
|
prom_store::snappy_compress(&prom_v2.encode_to_vec()).unwrap(),
|
|
);
|
|
prom_v2_case.headers.push(("content-encoding", "snappy"));
|
|
cases.push(prom_v2_case);
|
|
check_http_skip_wal("prometheus", &cases, distributed).await;
|
|
}
|
|
|
|
pub async fn test_prometheus_remote_write(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_prom_app_with_frontend(store_type, "prometheus_remote_write").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// write snappy encoded data
|
|
let write_request = WriteRequest {
|
|
timeseries: prom_store::mock_timeseries(),
|
|
..Default::default()
|
|
};
|
|
let serialized_request = write_request.encode_to_vec();
|
|
let compressed_request =
|
|
prom_store::snappy_compress(&serialized_request).expect("failed to encode snappy");
|
|
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
// The direct Prom remote-write handler creates tables without going through
|
|
// the generic inserter. Its submitted events must retain this origin.
|
|
wait_for_event_data(
|
|
&client,
|
|
"SELECT DISTINCT json_to_string(event_context) FROM greptime_private.events WHERE type IN ('create_table', 'create_logical_tables') AND json_get_string(procedure_trigger, 'type') = 'Submitted' AND json_get_string(event_context, 'protocol') = 'prometheus' AND json_get_string(event_context, 'reason') = 'auto_create'",
|
|
r#"[["{\"protocol\":\"prometheus\",\"reason\":\"auto_create\"}"]]"#,
|
|
)
|
|
.await;
|
|
|
|
let expected = "[[\"demo\"],[\"demo_metrics\"],[\"demo_metrics_with_nanos\"],[\"greptime_physical_table\"],[\"metric1\"],[\"metric2\"],[\"metric3\"],[\"mito\"],[\"multi_labels\"],[\"numbers\"],[\"phy\"],[\"phy2\"],[\"phy_ns\"]]";
|
|
validate_data("prometheus_remote_write", &client, "show tables;", expected).await;
|
|
|
|
let table_val = "[[1000,3.0,\"z001\",\"test_host1\"],[2000,4.0,\"z001\",\"test_host1\"]]";
|
|
validate_data(
|
|
"prometheus_remote_write",
|
|
&client,
|
|
"select * from metric2",
|
|
table_val,
|
|
)
|
|
.await;
|
|
|
|
// A metric-engine logical table stores its rows in the shared physical
|
|
// table, so reading them back from SST files goes through a different
|
|
// column mapping than the memtable read does.
|
|
flush_table(&client, "metric2").await;
|
|
validate_data(
|
|
"prometheus_remote_write_after_flush",
|
|
&client,
|
|
"select * from metric2",
|
|
table_val,
|
|
)
|
|
.await;
|
|
|
|
// Prom RW tables carry the metric identity; type is inferred from naming.
|
|
validate_data(
|
|
"prometheus_remote_write_semantic_identity",
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name = 'metric2' \
|
|
and create_options like '%greptime.semantic.signal_type=metric%' \
|
|
and create_options like '%greptime.semantic.source=prometheus%' \
|
|
and create_options like '%greptime.semantic.metric.metadata_quality=inferred%';",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
|
|
// Write snappy encoded data with new labels
|
|
let write_request = WriteRequest {
|
|
timeseries: mock_timeseries_new_label(),
|
|
..Default::default()
|
|
};
|
|
let serialized_request = write_request.encode_to_vec();
|
|
let compressed_request =
|
|
prom_store::snappy_compress(&serialized_request).expect("failed to encode snappy");
|
|
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
// Adding a label is handled by PendingRowsSchemaAlterer, which is another
|
|
// direct DDL path outside the generic inserter.
|
|
wait_for_event_data(
|
|
&client,
|
|
"SELECT DISTINCT json_to_string(event_context) FROM greptime_private.events WHERE type IN ('alter_table', 'alter_logical_tables') AND json_get_string(procedure_trigger, 'type') = 'Submitted' AND json_get_string(event_context, 'protocol') = 'prometheus' AND json_get_string(event_context, 'reason') = 'auto_alter'",
|
|
r#"[["{\"protocol\":\"prometheus\",\"reason\":\"auto_alter\"}"]]"#,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_prometheus_remote_write_v2(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_prom_app_with_frontend(store_type, "prometheus_remote_write_v2").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let write_request = remote_write_v2::request_with_labels_and_samples(
|
|
vec![
|
|
(prom_store::METRIC_NAME_LABEL, "remote_write_v2_total"),
|
|
("job", "api"),
|
|
("instance", "localhost:9090"),
|
|
],
|
|
vec![
|
|
RemoteWriteV2Sample {
|
|
value: 42.0,
|
|
timestamp: 1000,
|
|
start_timestamp: 0,
|
|
},
|
|
RemoteWriteV2Sample {
|
|
value: 43.0,
|
|
timestamp: 2000,
|
|
start_timestamp: 0,
|
|
},
|
|
],
|
|
);
|
|
let serialized_request = write_request.encode_to_vec();
|
|
let compressed_request =
|
|
prom_store::snappy_compress(&serialized_request).expect("failed to encode snappy");
|
|
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.header(
|
|
"Content-Type",
|
|
"application/x-protobuf;proto=io.prometheus.write.v2.Request",
|
|
)
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
let headers = res.headers();
|
|
assert_remote_write_v2_written_headers(&headers, "2");
|
|
assert!(res.text().await.is_empty());
|
|
|
|
validate_data(
|
|
"prometheus_remote_write_v2_rows",
|
|
&client,
|
|
"select greptime_timestamp, greptime_value, job, instance from remote_write_v2_total order by greptime_timestamp;",
|
|
"[[1000,42.0,\"api\",\"localhost:9090\"],[2000,43.0,\"api\",\"localhost:9090\"]]",
|
|
)
|
|
.await;
|
|
|
|
validate_data(
|
|
"prometheus_remote_write_v2_semantic_identity",
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name = 'remote_write_v2_total' \
|
|
and create_options like '%greptime.semantic.signal_type=metric%' \
|
|
and create_options like '%greptime.semantic.source=prometheus%' \
|
|
and create_options like '%greptime.semantic.metric.metadata_quality=inferred%';",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
|
|
// A series carrying inline metadata (v2 senders SHOULD) upgrades its
|
|
// table's quality to declared, with the metric type and the unit
|
|
// canonicalised from the OpenMetrics word to UCUM.
|
|
let mut write_request = remote_write_v2::request_with_labels_and_samples(
|
|
vec![
|
|
(prom_store::METRIC_NAME_LABEL, "remote_write_v2_typed_total"),
|
|
("job", "api"),
|
|
],
|
|
vec![RemoteWriteV2Sample {
|
|
value: 1.0,
|
|
timestamp: 1000,
|
|
start_timestamp: 0,
|
|
}],
|
|
);
|
|
let unit_ref = write_request.symbols.len() as u32;
|
|
write_request.symbols.push("seconds".to_string());
|
|
write_request.timeseries[0].metadata = Some(RemoteWriteV2Metadata {
|
|
r#type: RemoteWriteV2MetricType::Counter as i32,
|
|
help_ref: 0,
|
|
unit_ref,
|
|
});
|
|
let compressed_request = prom_store::snappy_compress(&write_request.encode_to_vec())
|
|
.expect("failed to encode snappy");
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.header(
|
|
"Content-Type",
|
|
"application/x-protobuf;proto=io.prometheus.write.v2.Request",
|
|
)
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
validate_data(
|
|
"prometheus_remote_write_v2_declared_metadata",
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name = 'remote_write_v2_typed_total' \
|
|
and create_options like '%greptime.semantic.metric.type=counter%' \
|
|
and create_options like '%greptime.semantic.metric.unit=s%' \
|
|
and create_options like '%greptime.semantic.metric.metadata_quality=declared%';",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/metadata?metric=remote_write_v2_typed_total")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(
|
|
serde_json::to_string_pretty(&body).unwrap(),
|
|
r#"{
|
|
"status": "success",
|
|
"data": {
|
|
"remote_write_v2_typed_total": [
|
|
{
|
|
"type": "counter",
|
|
"unit": "seconds",
|
|
"help": ""
|
|
}
|
|
]
|
|
}
|
|
}"#
|
|
);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
/// The Prometheus release these native-histogram expectations follow: **v3.15.0**.
|
|
///
|
|
/// That is the release the checked-in golden fixtures name in their own headers - the aggregate,
|
|
/// range and histogram case files under `src/promql/src/` were all generated by a v3.15.0 engine -
|
|
/// so a reader can look up the behaviour this route reproduces. Each expectation states the value
|
|
/// it wants rather than comparing against a running Prometheus.
|
|
const PROMETHEUS_REFERENCE: &str = "v3.15.0";
|
|
|
|
/// Writes one native histogram through Remote Write 2.0.
|
|
///
|
|
/// `positive_deltas` holds *increments*, not bucket counts, so the two entries are derived from
|
|
/// `count`: decoding accumulates them and the server rejects a payload whose bucket total disagrees
|
|
/// with its count.
|
|
async fn write_native_histogram(
|
|
client: &TestClient,
|
|
labels: &[(&str, &str)],
|
|
timestamp: i64,
|
|
count: u64,
|
|
sum: f64,
|
|
) {
|
|
let mut request = remote_write_v2::request_with_labels_and_samples(labels.to_vec(), vec![]);
|
|
let label_refs = labels
|
|
.iter()
|
|
.flat_map(|(name, value)| {
|
|
let name_ref = request.symbols.iter().position(|s| s == name).unwrap() as u32;
|
|
let value_ref = request.symbols.iter().position(|s| s == value).unwrap() as u32;
|
|
[name_ref, value_ref]
|
|
})
|
|
.collect();
|
|
// The buckets have to add up to `count`, and `positive_deltas` holds *increments*, so the
|
|
// second entry is the difference between the two bucket counts rather than the second count.
|
|
// The server rejects a payload whose totals disagree, which is what caught the earlier
|
|
// attempt at writing the two independently.
|
|
let first = count / 2;
|
|
let second = count - first;
|
|
request.timeseries.push(RemoteWriteV2TimeSeries {
|
|
labels_refs: label_refs,
|
|
histograms: vec![Histogram {
|
|
count: Some(Count::CountInt(count)),
|
|
sum,
|
|
schema: 1,
|
|
positive_spans: vec![BucketSpan {
|
|
offset: 0,
|
|
length: 2,
|
|
}],
|
|
positive_deltas: vec![
|
|
i64::try_from(first).expect("count fits a bucket delta"),
|
|
i64::try_from(second - first).expect("count fits a bucket delta"),
|
|
],
|
|
reset_hint: 2,
|
|
timestamp,
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
});
|
|
let payload = prom_store::snappy_compress(&request.encode_to_vec()).unwrap();
|
|
let response = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.header(
|
|
"Content-Type",
|
|
"application/x-protobuf;proto=io.prometheus.write.v2.Request",
|
|
)
|
|
.body(payload)
|
|
.send()
|
|
.await;
|
|
// The body carries the server's reason, which is what a rejected write has to report.
|
|
let status = response.status();
|
|
let body = response.text().await;
|
|
assert_eq!(status, StatusCode::NO_CONTENT, "write rejected: {body}");
|
|
}
|
|
|
|
pub async fn test_remote_write_v2_histogram_flush_native_consistency(store_type: StorageType) {
|
|
let observer = Arc::new(NativeFusedObserver::default());
|
|
let plugins = Plugins::default();
|
|
let wrapper: PhysicalPlanWrapperRef = observer.clone();
|
|
plugins.insert(wrapper);
|
|
let instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"remote_write_v2_histogram_flush_native_consistency",
|
|
)
|
|
.with_default_store_type(store_type)
|
|
.with_query_options(QueryOptions {
|
|
experimental_native_promql: true,
|
|
..Default::default()
|
|
})
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(instance.fe_instance().clone(), false, None)
|
|
.with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
write_native_histogram(
|
|
&client,
|
|
&[
|
|
(prom_store::METRIC_NAME_LABEL, "native_histogram_flush"),
|
|
("job", "api"),
|
|
],
|
|
3000,
|
|
5,
|
|
10.0,
|
|
)
|
|
.await;
|
|
observer.clear();
|
|
let first = client
|
|
.get("/v1/prometheus/api/v1/query?query=native_histogram_flush&time=3")
|
|
.header("x-greptime-hints", "query.experimental_native_promql=true")
|
|
.send()
|
|
.await;
|
|
assert_eq!(first.status(), StatusCode::OK);
|
|
let first: Value = first.json().await;
|
|
assert_eq!(first["data"]["result"][0]["histogram"][1]["count"], "5");
|
|
assert_eq!(first["data"]["result"][0]["histogram"][1]["sum"], "10");
|
|
assert_eq!(
|
|
observer.take("histogram before flush").name(),
|
|
"FusedSeriesExec"
|
|
);
|
|
tests_integration::test_util::execute_sql(
|
|
instance.fe_instance(),
|
|
"ADMIN FLUSH_TABLE('native_histogram_flush')",
|
|
)
|
|
.await;
|
|
write_native_histogram(
|
|
&client,
|
|
&[
|
|
(prom_store::METRIC_NAME_LABEL, "native_histogram_flush"),
|
|
("job", "api"),
|
|
],
|
|
6000,
|
|
8,
|
|
20.0,
|
|
)
|
|
.await;
|
|
tests_integration::test_util::execute_sql(
|
|
instance.fe_instance(),
|
|
"ADMIN FLUSH_TABLE('native_histogram_flush')",
|
|
)
|
|
.await;
|
|
observer.clear();
|
|
let second = client
|
|
.get("/v1/prometheus/api/v1/query?query=native_histogram_flush&time=6")
|
|
.header("x-greptime-hints", "query.experimental_native_promql=true")
|
|
.send()
|
|
.await;
|
|
assert_eq!(second.status(), StatusCode::OK);
|
|
let second: Value = second.json().await;
|
|
assert_eq!(second["data"]["result"][0]["histogram"][1]["count"], "8");
|
|
assert_eq!(second["data"]["result"][0]["histogram"][1]["sum"], "20");
|
|
assert_eq!(
|
|
observer.take("histogram after flush").name(),
|
|
"FusedSeriesExec"
|
|
);
|
|
}
|
|
|
|
pub async fn test_remote_write_v2_histogram_native_partitions(store_type: StorageType) {
|
|
let observer = Arc::new(NativeFusedObserver::default());
|
|
let plugins = Plugins::default();
|
|
let wrapper: PhysicalPlanWrapperRef = observer.clone();
|
|
plugins.insert(wrapper);
|
|
let instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"remote_write_v2_histogram_native_partitions",
|
|
)
|
|
.with_default_store_type(store_type)
|
|
.with_query_options(QueryOptions {
|
|
experimental_native_promql: true,
|
|
parallelism: 4,
|
|
..Default::default()
|
|
})
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(instance.fe_instance().clone(), false, None)
|
|
.with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
for index in 0..8 {
|
|
let job = format!("job{index}");
|
|
let host = format!("host{index}");
|
|
write_native_histogram(
|
|
&client,
|
|
&[
|
|
(prom_store::METRIC_NAME_LABEL, "native_histogram_parts"),
|
|
("job", &job),
|
|
("instance", &host),
|
|
],
|
|
index * 1000,
|
|
5 + index as u64,
|
|
10.0 + index as f64,
|
|
)
|
|
.await;
|
|
tests_integration::test_util::execute_sql(
|
|
instance.fe_instance(),
|
|
"ADMIN FLUSH_TABLE('native_histogram_parts')",
|
|
)
|
|
.await;
|
|
}
|
|
observer.clear();
|
|
let response = client
|
|
.get("/v1/prometheus/api/v1/query?query=native_histogram_parts&time=7")
|
|
.header(
|
|
"x-greptime-hints",
|
|
"query.experimental_native_promql=true,query_parallelism=4",
|
|
)
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let body: Value = response.json().await;
|
|
let results = body["data"]["result"].as_array().unwrap();
|
|
assert_eq!(results.len(), 8);
|
|
// Every series has to come back carrying the histogram it was written with: the counts differ
|
|
// per series, so a route that dropped or reordered them would not reproduce this map.
|
|
let mut written: BTreeMap<String, String> = BTreeMap::new();
|
|
for index in 0..8u64 {
|
|
written.insert(format!("job{index}"), (5 + index).to_string());
|
|
}
|
|
for result in results {
|
|
let job = result["metric"]["job"].as_str().expect("a job label");
|
|
// Take the entry rather than reading it: a duplicated `job` would then fail as unexpected,
|
|
// and a missing one is caught by the emptiness check below.
|
|
let expected = written
|
|
.remove(job)
|
|
.unwrap_or_else(|| panic!("unexpected or duplicate series: `{job}`"));
|
|
assert_eq!(
|
|
result["histogram"][1]["count"].as_str(),
|
|
Some(expected.as_str()),
|
|
"the histogram of `{job}` as {PROMETHEUS_REFERENCE} renders it"
|
|
);
|
|
}
|
|
assert!(written.is_empty(), "missing series: {written:?}");
|
|
let fused = observer.take("histogram partitions");
|
|
assert_eq!(fused.name(), "FusedSeriesExec");
|
|
let metrics = node_metric_set(&fused);
|
|
let mut partitions = BTreeMap::new();
|
|
let mut samples = BTreeMap::new();
|
|
for metric in metrics.iter() {
|
|
let Some(partition) = metric.partition() else {
|
|
continue;
|
|
};
|
|
let MetricValue::Count { count, .. } = metric.value() else {
|
|
continue;
|
|
};
|
|
let by_partition = match metric.value().name() {
|
|
"native.root.output_values" => &mut partitions,
|
|
"native.root.input_samples" => &mut samples,
|
|
_ => continue,
|
|
};
|
|
*by_partition.entry(partition).or_insert(0usize) += count.value();
|
|
}
|
|
// A partition-local tree publishes on the owners the canonical router hashed the series onto,
|
|
// so which partition reports a given histogram - and how many partitions report at all - is the
|
|
// router's hash of the series it was delivered, not a property of the statement. The total is:
|
|
// every series answers once at the single instant this query evaluates at, so the partitions
|
|
// together report exactly as many values as the result holds.
|
|
assert_eq!(
|
|
results.len(),
|
|
partitions.values().sum::<usize>(),
|
|
"the native partitions reported a different number of values than the result holds: \
|
|
{partitions:?}"
|
|
);
|
|
// Routing hashes each series' label set, so eight series over four owners can legitimately leave
|
|
// one of them empty. What the topology has to guarantee is that the series are spread over more
|
|
// than one owner - otherwise the merge this test exists for is never exercised - and that the
|
|
// routing loses none of them.
|
|
let non_empty = partitions.values().filter(|count| **count > 0).count();
|
|
assert!(
|
|
non_empty >= 2,
|
|
"the fused producer must run on more than one partition: {partitions:?}"
|
|
);
|
|
// An owner that published values is one that read the samples they were computed from; an owner
|
|
// that was handed nothing reports neither.
|
|
for (partition, values) in &partitions {
|
|
assert_eq!(
|
|
*values > 0,
|
|
samples.get(partition).is_some_and(|samples| *samples > 0),
|
|
"partition {partition} must read samples exactly when it publishes values; values: \
|
|
{partitions:?}, samples: {samples:?}"
|
|
);
|
|
}
|
|
}
|
|
|
|
pub async fn test_remote_write_v2_histogram_native_range_function(store_type: StorageType) {
|
|
let observer = Arc::new(NativeFusedObserver::default());
|
|
let plugins = Plugins::default();
|
|
let wrapper: PhysicalPlanWrapperRef = observer.clone();
|
|
plugins.insert(wrapper);
|
|
let instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"remote_write_v2_histogram_native_range_function",
|
|
)
|
|
.with_default_store_type(store_type)
|
|
.with_query_options(QueryOptions {
|
|
experimental_native_promql: true,
|
|
..Default::default()
|
|
})
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(instance.fe_instance().clone(), false, None)
|
|
.with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
write_native_histogram(
|
|
&client,
|
|
&[
|
|
(prom_store::METRIC_NAME_LABEL, "native_histogram_range"),
|
|
("job", "api"),
|
|
],
|
|
1000,
|
|
5,
|
|
10.0,
|
|
)
|
|
.await;
|
|
write_native_histogram(
|
|
&client,
|
|
&[
|
|
(prom_store::METRIC_NAME_LABEL, "native_histogram_range"),
|
|
("job", "api"),
|
|
],
|
|
3000,
|
|
8,
|
|
20.0,
|
|
)
|
|
.await;
|
|
observer.clear();
|
|
let response = client.get("/v1/prometheus/api/v1/query?query=sum_over_time(native_histogram_range%5B1m%5D)&time=3")
|
|
.header("x-greptime-hints", "query.experimental_native_promql=true").send().await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let body: Value = response.json().await;
|
|
assert_eq!(body["data"]["resultType"], "vector");
|
|
assert_eq!(body["data"]["result"][0]["histogram"][0], 3.0);
|
|
assert_eq!(
|
|
body["data"]["result"][0]["histogram"][1]["count"], "13",
|
|
"`sum_over_time` adds the counts as {PROMETHEUS_REFERENCE} does"
|
|
);
|
|
assert_eq!(
|
|
body["data"]["result"][0]["histogram"][1]["sum"], "30",
|
|
"`sum_over_time` adds the sums as {PROMETHEUS_REFERENCE} does"
|
|
);
|
|
assert_eq!(
|
|
observer.take("histogram sum_over_time").name(),
|
|
"FusedSeriesExec"
|
|
);
|
|
}
|
|
|
|
/// Scalar arithmetic over native histograms stays on the native route and scales the payload.
|
|
///
|
|
/// `h * 2` and `h / 2` scale every bucket. `-h` is the same multiply by a negative factor, which
|
|
/// also turns the counter into a gauge, as Prometheus' own `Histogram.Mul` does. `2 / h` has no
|
|
/// histogram answer, so that position is dropped rather than failing the query - the scalar
|
|
/// division arithmetic of Prometheus returns a float only for a float input.
|
|
///
|
|
/// None of these reach the float-only arithmetic the range kernels use, so a query that passed
|
|
/// admission could still fail at execution; these results are what proves it does not.
|
|
pub async fn test_remote_write_v2_histogram_native_arithmetic(store_type: StorageType) {
|
|
let observer = Arc::new(NativeFusedObserver::default());
|
|
let plugins = Plugins::default();
|
|
let wrapper: PhysicalPlanWrapperRef = observer.clone();
|
|
plugins.insert(wrapper);
|
|
let instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"remote_write_v2_histogram_native_arithmetic",
|
|
)
|
|
.with_default_store_type(store_type)
|
|
.with_query_options(QueryOptions {
|
|
experimental_native_promql: true,
|
|
..Default::default()
|
|
})
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(instance.fe_instance().clone(), false, None)
|
|
.with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
write_native_histogram(
|
|
&client,
|
|
&[
|
|
(prom_store::METRIC_NAME_LABEL, "native_histogram_arithmetic"),
|
|
("job", "api"),
|
|
],
|
|
1000,
|
|
10,
|
|
20.0,
|
|
)
|
|
.await;
|
|
|
|
// `count` and `sum` of the one returned histogram, as the response writes them.
|
|
let histogram_of = |body: &Value| -> (String, String) {
|
|
(
|
|
body["data"]["result"][0]["histogram"][1]["count"]
|
|
.as_str()
|
|
.unwrap_or_else(|| panic!("no histogram count in {body}"))
|
|
.to_string(),
|
|
body["data"]["result"][0]["histogram"][1]["sum"]
|
|
.as_str()
|
|
.unwrap_or_else(|| panic!("no histogram sum in {body}"))
|
|
.to_string(),
|
|
)
|
|
};
|
|
|
|
for (query, expected_count, expected_sum) in [
|
|
("native_histogram_arithmetic%20%2A%202", "20", "40"),
|
|
("native_histogram_arithmetic%20%2F%202", "5", "10"),
|
|
("-native_histogram_arithmetic", "-10", "-20"),
|
|
] {
|
|
observer.clear();
|
|
let response = client
|
|
.get(&format!("/v1/prometheus/api/v1/query?query={query}&time=1"))
|
|
.header("x-greptime-hints", "query.experimental_native_promql=true")
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK, "{query}");
|
|
let body: Value = response.json().await;
|
|
assert_eq!(body["data"]["resultType"], "vector", "{query}: {body}");
|
|
assert_eq!(
|
|
histogram_of(&body),
|
|
(expected_count.to_string(), expected_sum.to_string()),
|
|
"{query}: {body}"
|
|
);
|
|
assert_eq!(
|
|
observer.take("histogram arithmetic").name(),
|
|
"FusedSeriesExec",
|
|
"{query} left the native route"
|
|
);
|
|
}
|
|
|
|
// `2 / h` admits but has no histogram answer: the position is dropped, so the query succeeds
|
|
// with an empty result rather than failing at execution or falling back to the float path.
|
|
observer.clear();
|
|
let response = client
|
|
.get("/v1/prometheus/api/v1/query?query=2%20%2F%20native_histogram_arithmetic&time=1")
|
|
.header("x-greptime-hints", "query.experimental_native_promql=true")
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let body: Value = response.json().await;
|
|
assert_eq!(
|
|
body["data"]["result"].as_array().map(Vec::len),
|
|
Some(0),
|
|
"`2 / h` should drop the histogram position: {body}"
|
|
);
|
|
assert_eq!(
|
|
observer
|
|
.take("histogram arithmetic: scalar over histogram")
|
|
.name(),
|
|
"FusedSeriesExec",
|
|
"`2 / h` left the native route"
|
|
);
|
|
}
|
|
|
|
/// Executes a public PromQL request and checks the binary node actually used, without assuming how
|
|
/// many fused producers or children the physical plan contains.
|
|
async fn native_histogram_composition_query(
|
|
client: &TestClient,
|
|
observer: &NativeFusedObserver,
|
|
route: &PromRouteObserver,
|
|
query: &str,
|
|
parameters: &str,
|
|
) -> (StatusCode, Value) {
|
|
observer.clear();
|
|
let response = client
|
|
.get(&format!(
|
|
"/v1/prometheus/api/v1/{parameters}&query={}",
|
|
encode(query)
|
|
))
|
|
.header("x-greptime-hints", "query.experimental_native_promql=true")
|
|
.send()
|
|
.await;
|
|
let status = response.status();
|
|
let body: Value = response.json().await;
|
|
|
|
let descriptions = observer
|
|
.take_all(query)
|
|
.iter()
|
|
.map(|node| {
|
|
datafusion::physical_plan::displayable(node.as_ref())
|
|
.one_line()
|
|
.to_string()
|
|
})
|
|
.collect::<Vec<_>>();
|
|
// The tree a fused node prints names its own nodes, indented by depth inside the
|
|
// `native_tree=[ ... ]` block, so the binary is found by its node line rather than by the
|
|
// operator line (which prints the tree, not the statement).
|
|
assert!(
|
|
descriptions.iter().any(|description| {
|
|
description
|
|
.lines()
|
|
.any(|line| line.trim_start().starts_with("Binary:"))
|
|
}),
|
|
"`{query}` did not execute a native Binary: {descriptions:?}; response: {body}"
|
|
);
|
|
let nodes = plan_nodes(&route.take_plan());
|
|
assert!(
|
|
nodes
|
|
.iter()
|
|
.all(|node| { !node["name"].as_str().unwrap_or_default().contains("Join") }),
|
|
"`{query}` executed a legacy join: {nodes:?}"
|
|
);
|
|
(status, body)
|
|
}
|
|
|
|
/// Composed histogram frames retain their values, annotations and matching semantics over HTTP.
|
|
pub async fn test_remote_write_v2_histogram_native_composition(store_type: StorageType) {
|
|
let observer = Arc::new(NativeFusedObserver::default());
|
|
let route = Arc::new(PromRouteObserver::default());
|
|
let plugins = Plugins::default();
|
|
let wrapper: PhysicalPlanWrapperRef = observer.clone();
|
|
plugins.insert(wrapper);
|
|
plugins.insert::<PromQueryInterceptorRef<servers::error::Error>>(route.clone());
|
|
let instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"remote_write_v2_histogram_native_composition",
|
|
)
|
|
.with_default_store_type(store_type)
|
|
.with_query_options(QueryOptions {
|
|
experimental_native_promql: true,
|
|
..Default::default()
|
|
})
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(instance.fe_instance().clone(), false, None)
|
|
.with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
|
|
for (metric, job, instance_label, timestamp, count, sum) in [
|
|
("native_comp_h", "api", "a", 1_000, 10, 20.0),
|
|
// A later count of 1 makes losing the offset observable at the second range step.
|
|
("native_comp_h", "api", "a", 121_000, 1, 2.0),
|
|
// For this other job, losing @ end() instead makes the first step return 3 rather than 4.
|
|
("native_comp_h", "anchor", "a", 1_000, 1, 2.0),
|
|
("native_comp_h", "anchor", "a", 61_000, 10, 20.0),
|
|
("native_comp_h", "anchor", "a", 121_000, 1, 2.0),
|
|
("native_comp_h2", "api", "a", 1_000, 4, 8.0),
|
|
("native_comp_h2", "other", "a", 1_000, 4, 8.0),
|
|
("native_comp_dups", "duplicate", "a", 1_000, 4, 8.0),
|
|
("native_comp_dups", "duplicate", "b", 1_000, 4, 8.0),
|
|
] {
|
|
write_native_histogram(
|
|
&client,
|
|
&[
|
|
(prom_store::METRIC_NAME_LABEL, metric),
|
|
("job", job),
|
|
("instance", instance_label),
|
|
],
|
|
timestamp,
|
|
count,
|
|
sum,
|
|
)
|
|
.await;
|
|
}
|
|
for sql in [
|
|
"CREATE TABLE native_comp_f (ts TIMESTAMP TIME INDEX, val DOUBLE, job STRING, PRIMARY KEY (job))",
|
|
"INSERT INTO native_comp_f(job, val, ts) VALUES ('api', 2.0, 1000), ('duplicate', 2.0, 1000), ('api', 2.0, 61000), ('api', 9.0, 121000), ('anchor', 2.0, 61000), ('anchor', 9.0, 121000)",
|
|
] {
|
|
tests_integration::test_util::execute_sql(instance.fe_instance(), sql).await;
|
|
}
|
|
|
|
// A counter histogram with a start timestamp strictly inside `(0, 3000]`, its window's only
|
|
// sample: `rate` can answer that window through the start timestamp alone.
|
|
let start_request = remote_write_v2::request_with_labels_and_histograms(
|
|
vec![
|
|
(prom_store::METRIC_NAME_LABEL, "native_comp_start"),
|
|
("job", "api"),
|
|
],
|
|
vec![Histogram {
|
|
count: Some(Count::CountInt(8)),
|
|
sum: 10.0,
|
|
schema: 1,
|
|
positive_spans: vec![BucketSpan {
|
|
offset: 0,
|
|
length: 2,
|
|
}],
|
|
positive_deltas: vec![4, 0],
|
|
reset_hint: 2,
|
|
start_timestamp: 1_500,
|
|
timestamp: 3_000,
|
|
..Default::default()
|
|
}],
|
|
);
|
|
let payload = prom_store::snappy_compress(&start_request.encode_to_vec()).unwrap();
|
|
let written = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.header(
|
|
"Content-Type",
|
|
"application/x-protobuf;proto=io.prometheus.write.v2.Request",
|
|
)
|
|
.body(payload)
|
|
.send()
|
|
.await;
|
|
let status = written.status();
|
|
let body = written.text().await;
|
|
assert_eq!(status, StatusCode::NO_CONTENT, "write rejected: {body}");
|
|
|
|
// The first sample has two equal buckets of 5; h2 has two equal buckets of 2.
|
|
// Repeat the same public requests after flushing so the source can be memtable or SST.
|
|
for phase in ["memtable", "flushed"] {
|
|
if phase == "flushed" {
|
|
for table in [
|
|
"native_comp_h",
|
|
"native_comp_h2",
|
|
"native_comp_dups",
|
|
"native_comp_f",
|
|
"native_comp_start",
|
|
] {
|
|
tests_integration::test_util::execute_sql(
|
|
instance.fe_instance(),
|
|
&format!("ADMIN FLUSH_TABLE('{table}')"),
|
|
)
|
|
.await;
|
|
}
|
|
}
|
|
|
|
for (query, count, sum, bucket) in [
|
|
(
|
|
"native_comp_f{job=\"api\"} * on(job) native_comp_h",
|
|
"20",
|
|
"40",
|
|
"10",
|
|
),
|
|
(
|
|
"native_comp_h / on(job) native_comp_f{job=\"api\"}",
|
|
"5",
|
|
"10",
|
|
"2.5",
|
|
),
|
|
(
|
|
"native_comp_h + on(job) native_comp_h2{job=\"api\"}",
|
|
"14",
|
|
"28",
|
|
"7",
|
|
),
|
|
(
|
|
"native_comp_h - on(job) native_comp_h2{job=\"api\"}",
|
|
"6",
|
|
"12",
|
|
"3",
|
|
),
|
|
] {
|
|
let (status, body) = native_histogram_composition_query(
|
|
&client,
|
|
&observer,
|
|
&route,
|
|
query,
|
|
"query?time=1",
|
|
)
|
|
.await;
|
|
assert_eq!(status, StatusCode::OK, "{phase}: {query}: {body}");
|
|
assert_eq!(
|
|
body,
|
|
json!({
|
|
"status": "success",
|
|
"data": {"resultType": "vector", "result": [{
|
|
"metric": {"job": "api"},
|
|
"histogram": [1.0, {"count": count, "sum": sum, "buckets": [
|
|
[0, "0.7071067811865476", "1", bucket],
|
|
[0, "1", "1.4142135623730951", bucket]
|
|
]}]
|
|
}]}
|
|
}),
|
|
"{phase}: {query}"
|
|
);
|
|
}
|
|
|
|
// These selectors read one unpartitioned histogram table but hold disjoint match groups.
|
|
// The real histogram/histogram pair cannot emit for `*` or `/`, so only a synthetic float
|
|
// can produce a result. Whether that result carries the unsupported-pair Info follows the
|
|
// operation the synthesized sample enters, not whether the pair was synthesized:
|
|
// Prometheus runs the same `doBinOp` on a filled sample, so a pairing it supports
|
|
// (`float * histogram`, `histogram / float`) stays silent, while one it does not
|
|
// (`float / histogram`) is dropped and reported exactly as a matched pair would be.
|
|
let left = "native_comp_h{job=\"api\"}";
|
|
let right = "native_comp_h{job=\"anchor\"}";
|
|
for (operator, expected, dropped_op) in [
|
|
(
|
|
"* on(job) fill(1)",
|
|
vec![("anchor", "1", "2", "1"), ("api", "10", "20", "5")],
|
|
None,
|
|
),
|
|
(
|
|
"* on(job) fill_left(1)",
|
|
vec![("anchor", "1", "2", "1")],
|
|
None,
|
|
),
|
|
(
|
|
"* on(job) fill_right(1)",
|
|
vec![("api", "10", "20", "5")],
|
|
None,
|
|
),
|
|
(
|
|
"/ on(job) fill_right(2)",
|
|
vec![("api", "5", "10", "2.5")],
|
|
None,
|
|
),
|
|
// `fill_left(2)` answers the anchor series with the synthesized float on the left, so
|
|
// the pair is `float / histogram`: division by a histogram has no kernel, and the
|
|
// sample Prometheus would drop here is the one this reports.
|
|
("/ on(job) fill_left(2)", vec![], Some("/")),
|
|
] {
|
|
let query = format!("{left} {operator} {right}");
|
|
let (status, body) = native_histogram_composition_query(
|
|
&client,
|
|
&observer,
|
|
&route,
|
|
&query,
|
|
"query?time=1",
|
|
)
|
|
.await;
|
|
assert_eq!(status, StatusCode::OK, "{phase}: {query}: {body}");
|
|
assert_eq!(body["status"], "success", "{phase}: {query}: {body}");
|
|
assert_eq!(
|
|
body["data"]["resultType"], "vector",
|
|
"{phase}: {query}: {body}"
|
|
);
|
|
match dropped_op {
|
|
Some(op) => assert_eq!(
|
|
body["infos"],
|
|
json!([format!(
|
|
"{op}: dropped native histogram samples because this binary operation is not supported for native histograms"
|
|
)]),
|
|
"{phase}: {query}: {body}"
|
|
),
|
|
None => assert!(
|
|
body.get("infos").is_none(),
|
|
"a supported fill is not a dropped pair: {phase}: {query}: {body}"
|
|
),
|
|
}
|
|
assert!(body.get("warnings").is_none(), "{phase}: {query}: {body}");
|
|
let mut rows = body["data"]["result"].as_array().unwrap().clone();
|
|
rows.sort_by(|a, b| {
|
|
a["metric"]["job"]
|
|
.as_str()
|
|
.cmp(&b["metric"]["job"].as_str())
|
|
});
|
|
let expected_rows: Vec<_> = expected
|
|
.into_iter()
|
|
.map(|(job, count, sum, bucket)| {
|
|
// The anchor fixture's count=1 has bucket counts [0, 1]; the HTTP histogram
|
|
// omits the first, zero-count bucket. The api fixture has two equal buckets.
|
|
let buckets = if job == "anchor" {
|
|
json!([[0, "1", "1.4142135623730951", bucket]])
|
|
} else {
|
|
json!([
|
|
[0, "0.7071067811865476", "1", bucket],
|
|
[0, "1", "1.4142135623730951", bucket]
|
|
])
|
|
};
|
|
json!({
|
|
"metric": {"job": job},
|
|
"histogram": [1.0, {"count": count, "sum": sum, "buckets": buckets}]
|
|
})
|
|
})
|
|
.collect();
|
|
assert_eq!(rows, expected_rows, "{phase}: {query}: {body}");
|
|
}
|
|
// A present, matched histogram/histogram pair still drops and reports its existing Info.
|
|
let query = format!("{left} * on(job) {left}");
|
|
let (status, body) =
|
|
native_histogram_composition_query(&client, &observer, &route, &query, "query?time=1")
|
|
.await;
|
|
assert_eq!(status, StatusCode::OK, "{phase}: {query}: {body}");
|
|
assert_eq!(
|
|
body,
|
|
json!({
|
|
"status": "success",
|
|
"data": {"resultType": "vector", "result": []},
|
|
"infos": ["*: dropped native histogram samples because this binary operation is not supported for native histograms"]
|
|
}),
|
|
"{phase}: {query}"
|
|
);
|
|
|
|
// `clamp` is native-only here, so legacy fallback cannot produce these answers.
|
|
for (query, op) in [
|
|
(
|
|
"clamp(native_comp_f{job=\"api\"}, 1, 2) + on(job) native_comp_h",
|
|
"+",
|
|
),
|
|
(
|
|
"native_comp_h + on(job) clamp(native_comp_f{job=\"api\"}, 1, 2)",
|
|
"+",
|
|
),
|
|
("native_comp_h * on(job) native_comp_h2{job=\"api\"}", "*"),
|
|
] {
|
|
let (status, body) = native_histogram_composition_query(
|
|
&client,
|
|
&observer,
|
|
&route,
|
|
query,
|
|
"query?time=1",
|
|
)
|
|
.await;
|
|
assert_eq!(status, StatusCode::OK, "{phase}: {query}: {body}");
|
|
assert_eq!(
|
|
body,
|
|
json!({
|
|
"status": "success",
|
|
"data": {"resultType": "vector", "result": []},
|
|
"infos": [format!("{op}: dropped native histogram samples because this binary operation is not supported for native histograms")]
|
|
}),
|
|
"{phase}: {query}"
|
|
);
|
|
}
|
|
|
|
// A different match group, or a selector with no samples, must not fabricate a drop Info.
|
|
for query in [
|
|
"clamp(native_comp_f{job=\"api\"}, 1, 2) + on(job) native_comp_h2{job=\"other\"}",
|
|
"native_comp_h * on(job) native_comp_h2{job=\"other\"}",
|
|
"clamp(native_comp_f{job=\"absent\"}, 1, 2) + on(job) native_comp_h",
|
|
"native_comp_h + on(job) clamp(native_comp_f{job=\"absent\"}, 1, 2)",
|
|
] {
|
|
let (status, body) = native_histogram_composition_query(
|
|
&client,
|
|
&observer,
|
|
&route,
|
|
query,
|
|
"query?time=1",
|
|
)
|
|
.await;
|
|
assert_eq!(status, StatusCode::OK, "{phase}: {query}: {body}");
|
|
assert_eq!(
|
|
body,
|
|
json!({"status": "success", "data": {"resultType": "vector", "result": []}}),
|
|
"{phase}: {query}"
|
|
);
|
|
}
|
|
|
|
// The duplicate group is on the right. It is an error both when it matches and when it
|
|
// does not: both sides have samples, so the native matcher's presence gate is open.
|
|
for job in ["duplicate", "api"] {
|
|
let query =
|
|
format!("clamp(native_comp_f{{job=\"{job}\"}}, 1, 2) + on(job) native_comp_dups");
|
|
let (status, body) = native_histogram_composition_query(
|
|
&client,
|
|
&observer,
|
|
&route,
|
|
&query,
|
|
"query?time=1",
|
|
)
|
|
.await;
|
|
assert!(!status.is_success(), "{phase}: {query}: {body}");
|
|
assert_eq!(body["status"], "error", "{phase}: {query}: {body}");
|
|
let error = body["error"].as_str().expect("matching error message");
|
|
assert!(
|
|
error.contains("found duplicate series for the match group {job=\"duplicate\"} on the right hand-side of the operation at 1000 ms; many-to-many matching not allowed"),
|
|
"{phase}: {query}: {body}"
|
|
);
|
|
}
|
|
|
|
// The 121s end minus 1m reads the 61s histogram; @ start() reads the 61s float.
|
|
// Both are 2 after clamp, at both points. A 121s float of 9 exposes a lost @ start().
|
|
// The `anchor` job also exposes a lost @ end() at the first point; both jobs expose a
|
|
// lost offset at the second point. Neither expected series carries its metric name.
|
|
let query = "ceil(clamp(histogram_count(native_comp_h @ end() offset 1m), 1, 2) + on(job) native_comp_f{job=~\"api|anchor\"} @ start())";
|
|
let (status, body) = native_histogram_composition_query(
|
|
&client,
|
|
&observer,
|
|
&route,
|
|
query,
|
|
"query_range?start=61&end=121&step=60s",
|
|
)
|
|
.await;
|
|
assert_eq!(status, StatusCode::OK, "{phase}: {query}: {body}");
|
|
assert_eq!(body["status"], "success", "{phase}: {query}: {body}");
|
|
assert_eq!(
|
|
body["data"]["resultType"], "matrix",
|
|
"{phase}: {query}: {body}"
|
|
);
|
|
let results = body["data"]["result"].as_array().expect("range result");
|
|
assert_eq!(results.len(), 2, "{phase}: {query}: {body}");
|
|
for job in ["api", "anchor"] {
|
|
let expected = json!({
|
|
"metric": {"job": job}, "values": [[61.0, "4.0"], [121.0, "4.0"]]
|
|
});
|
|
assert_eq!(
|
|
results.iter().filter(|result| **result == expected).count(),
|
|
1,
|
|
"{phase}: {query}: {body}"
|
|
);
|
|
}
|
|
assert!(body.get("infos").is_none(), "{phase}: {query}: {body}");
|
|
assert!(body.get("warnings").is_none(), "{phase}: {query}: {body}");
|
|
|
|
// The same two windows asked with the route explicitly off, so the legacy route answers
|
|
// them and the observer proves it did. The engine option is a default here (this instance
|
|
// sets it to `true`), so an unstated request would take the native route; stating `false`
|
|
// is what selects the existing one. This is where the delegated kernel has to agree with
|
|
// the native route: the raw sample's start timestamp is what lets `rate` measure the
|
|
// window, while `* 1` gives that timestamp up, so the derived window has nothing to
|
|
// measure against.
|
|
for (query, expected_count) in [
|
|
("rate(native_comp_start[3s])", Some(8.0 / 3.0)),
|
|
("rate((native_comp_start * 1)[3s:1s])", None),
|
|
] {
|
|
let res = client
|
|
.get(&format!(
|
|
"/v1/prometheus/api/v1/query?query={}&time=3",
|
|
encode(query)
|
|
))
|
|
.header("x-greptime-hints", "query.experimental_native_promql=false")
|
|
.send()
|
|
.await;
|
|
let status = res.status();
|
|
let body: Value = res.json().await;
|
|
assert_eq!(status, StatusCode::OK, "{phase}: {query}: {body}");
|
|
let results = body["data"]["result"]
|
|
.as_array()
|
|
.unwrap_or_else(|| panic!("{phase}: {query}: no result in {body}"));
|
|
match expected_count {
|
|
Some(count) => {
|
|
assert_eq!(results.len(), 1, "{phase}: {query}: {body}");
|
|
let reported: f64 = body["data"]["result"][0]["histogram"][1]["count"]
|
|
.as_str()
|
|
.unwrap_or_else(|| panic!("{phase}: {query}: no count in {body}"))
|
|
.parse()
|
|
.unwrap();
|
|
assert!(
|
|
(reported - count).abs() < 1e-12,
|
|
"{phase}: {query}: expected count {count}, got {body}"
|
|
);
|
|
}
|
|
None => assert!(
|
|
results.is_empty(),
|
|
"{phase}: {query}: `* 1` clears the start timestamp, so the derived window \
|
|
has nothing to measure: {body}"
|
|
),
|
|
}
|
|
assert!(
|
|
body.get("infos").is_none() && body.get("warnings").is_none(),
|
|
"{phase}: {query}: a counter histogram has nothing to report: {body}"
|
|
);
|
|
assert!(
|
|
!executes_fused_series(&route.take_plan()),
|
|
"{phase}: {query} did not run on the legacy route"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A native histogram query that has something to warn about puts it in the response.
|
|
///
|
|
/// `rate` over a gauge histogram is the case: the kernel drops nothing but reports that the input
|
|
/// was the wrong flavor. The warning has to travel from the kernel through the executor and the
|
|
/// query's collector to the HTTP response, which is the whole channel this checks - a collector
|
|
/// the execution never reaches would leave `warnings` absent.
|
|
pub async fn test_remote_write_v2_histogram_native_annotations(store_type: StorageType) {
|
|
let instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"native_histogram_annotations",
|
|
)
|
|
.with_default_store_type(store_type)
|
|
.with_query_options(QueryOptions {
|
|
experimental_native_promql: true,
|
|
..Default::default()
|
|
})
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(instance.fe_instance().clone(), false, None)
|
|
.with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
|
|
// A gauge histogram, which `rate` reports as the wrong flavor.
|
|
let mut request = remote_write_v2::request_with_labels_and_samples(
|
|
vec![
|
|
(
|
|
prom_store::METRIC_NAME_LABEL,
|
|
"native_histogram_annotations",
|
|
),
|
|
("job", "api"),
|
|
],
|
|
vec![],
|
|
);
|
|
let name_ref = request
|
|
.symbols
|
|
.iter()
|
|
.position(|s| s == prom_store::METRIC_NAME_LABEL)
|
|
.unwrap() as u32;
|
|
let metric_ref = request
|
|
.symbols
|
|
.iter()
|
|
.position(|s| s == "native_histogram_annotations")
|
|
.unwrap() as u32;
|
|
let job_ref = request.symbols.iter().position(|s| s == "job").unwrap() as u32;
|
|
let api_ref = request.symbols.iter().position(|s| s == "api").unwrap() as u32;
|
|
request.timeseries.push(RemoteWriteV2TimeSeries {
|
|
labels_refs: vec![name_ref, metric_ref, job_ref, api_ref],
|
|
histograms: vec![
|
|
Histogram {
|
|
count: Some(Count::CountInt(4)),
|
|
sum: 8.0,
|
|
schema: 1,
|
|
positive_spans: vec![BucketSpan {
|
|
offset: 0,
|
|
length: 2,
|
|
}],
|
|
// Increments: the two buckets hold 2 and 2, which sums to the count of 4.
|
|
positive_deltas: vec![2, 0],
|
|
// `CounterResetHint::Gauge`
|
|
reset_hint: 3,
|
|
timestamp: 2000,
|
|
..Default::default()
|
|
},
|
|
Histogram {
|
|
count: Some(Count::CountInt(8)),
|
|
sum: 16.0,
|
|
schema: 1,
|
|
positive_spans: vec![BucketSpan {
|
|
offset: 0,
|
|
length: 2,
|
|
}],
|
|
positive_deltas: vec![4, 0],
|
|
reset_hint: 3,
|
|
timestamp: 3000,
|
|
..Default::default()
|
|
},
|
|
],
|
|
..Default::default()
|
|
});
|
|
let payload = prom_store::snappy_compress(&request.encode_to_vec()).unwrap();
|
|
let response = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.header(
|
|
"Content-Type",
|
|
"application/x-protobuf;proto=io.prometheus.write.v2.Request",
|
|
)
|
|
.body(payload)
|
|
.send()
|
|
.await;
|
|
let status = response.status();
|
|
let body = response.text().await;
|
|
assert_eq!(status, StatusCode::NO_CONTENT, "write rejected: {body}");
|
|
|
|
let response = client
|
|
.get("/v1/prometheus/api/v1/query?query=rate(native_histogram_annotations%5B1m%5D)&time=3")
|
|
.header("x-greptime-hints", "query.experimental_native_promql=true")
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let body: Value = response.json().await;
|
|
let warnings = body["warnings"]
|
|
.as_array()
|
|
.unwrap_or_else(|| panic!("no warnings in {body}"));
|
|
assert!(
|
|
warnings.iter().any(|warning| warning
|
|
.as_str()
|
|
.is_some_and(|text| text.contains("should be a counter histogram"))),
|
|
"the flavor warning did not reach the response: {body}"
|
|
);
|
|
}
|
|
|
|
pub async fn test_remote_write_v2_histogram_prometheus_native_route(store_type: StorageType) {
|
|
let observer = Arc::new(NativeFusedObserver::default());
|
|
let plugins = Plugins::default();
|
|
let wrapper: PhysicalPlanWrapperRef = observer.clone();
|
|
plugins.insert(wrapper);
|
|
let instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"remote_write_v2_histogram_native_route",
|
|
)
|
|
.with_default_store_type(store_type)
|
|
.with_query_options(QueryOptions {
|
|
experimental_native_promql: true,
|
|
..Default::default()
|
|
})
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(instance.fe_instance().clone(), false, None)
|
|
.with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
|
|
let mut request = remote_write_v2::request_with_labels_and_samples(
|
|
vec![
|
|
(prom_store::METRIC_NAME_LABEL, "native_histogram_e2e"),
|
|
("job", "api"),
|
|
],
|
|
vec![],
|
|
);
|
|
let name_ref = request
|
|
.symbols
|
|
.iter()
|
|
.position(|s| s == prom_store::METRIC_NAME_LABEL)
|
|
.unwrap() as u32;
|
|
let metric_ref = request
|
|
.symbols
|
|
.iter()
|
|
.position(|s| s == "native_histogram_e2e")
|
|
.unwrap() as u32;
|
|
let job_ref = request.symbols.iter().position(|s| s == "job").unwrap() as u32;
|
|
let api_ref = request.symbols.iter().position(|s| s == "api").unwrap() as u32;
|
|
request.timeseries.push(RemoteWriteV2TimeSeries {
|
|
labels_refs: vec![name_ref, metric_ref, job_ref, api_ref],
|
|
histograms: vec![Histogram {
|
|
count: Some(Count::CountInt(5)),
|
|
sum: 10.0,
|
|
schema: 1,
|
|
positive_spans: vec![BucketSpan {
|
|
offset: 0,
|
|
length: 2,
|
|
}],
|
|
positive_deltas: vec![2, 1],
|
|
reset_hint: 2,
|
|
timestamp: 3000,
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
});
|
|
let payload = prom_store::snappy_compress(&request.encode_to_vec()).unwrap();
|
|
let written = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.header(
|
|
"Content-Type",
|
|
"application/x-protobuf;proto=io.prometheus.write.v2.Request",
|
|
)
|
|
.body(payload)
|
|
.send()
|
|
.await;
|
|
assert_eq!(written.status(), StatusCode::NO_CONTENT);
|
|
|
|
let response = client
|
|
.get("/v1/prometheus/api/v1/query?query=native_histogram_e2e&time=3")
|
|
.header("x-greptime-hints", "query.experimental_native_promql=true")
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let body: Value = response.json().await;
|
|
assert_eq!(body["data"]["resultType"], "vector");
|
|
assert_eq!(body["data"]["result"][0]["metric"]["job"], "api");
|
|
assert_eq!(body["data"]["result"][0]["histogram"][0], 3.0);
|
|
assert_eq!(
|
|
body["data"]["result"][0]["histogram"][1]["count"], "5",
|
|
"count as {PROMETHEUS_REFERENCE} renders it"
|
|
);
|
|
assert_eq!(
|
|
body["data"]["result"][0]["histogram"][1]["sum"], "10",
|
|
"sum as {PROMETHEUS_REFERENCE} renders it"
|
|
);
|
|
let bucket_counts = body["data"]["result"][0]["histogram"][1]["buckets"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.filter_map(|bucket| bucket[3].as_str())
|
|
.collect::<Vec<_>>();
|
|
assert!(
|
|
bucket_counts.contains(&"2"),
|
|
"bucket counts: {bucket_counts:?}"
|
|
);
|
|
assert!(
|
|
bucket_counts.contains(&"3"),
|
|
"bucket counts: {bucket_counts:?}"
|
|
);
|
|
let fused = observer.take("remote-write native histogram selector");
|
|
assert_eq!(fused.name(), "FusedSeriesExec");
|
|
}
|
|
|
|
/// A statement that reads histograms and answers floats stays on the native route.
|
|
///
|
|
/// The output column follows the kind the tree's root answers, not the kind its scan carries:
|
|
/// `count_over_time`, `histogram_count`, `resets`, `changes` and `timestamp` all reduce a histogram
|
|
/// to a float, and an aggregate above one of them folds floats. Typing the column by the scan
|
|
/// declared a histogram column for a float answer, so every one of these shapes failed at execution
|
|
/// and the aggregate ones failed to plan. Each case below therefore pins three things at once: the
|
|
/// response is the legacy response, the fused node actually ran, and the value is the one the data
|
|
/// holds.
|
|
pub async fn test_remote_write_v2_histogram_float_prefix_native_route(store_type: StorageType) {
|
|
let observer = Arc::new(NativeFusedObserver::default());
|
|
let plugins = Plugins::default();
|
|
let wrapper: PhysicalPlanWrapperRef = observer.clone();
|
|
plugins.insert(wrapper);
|
|
let instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"remote_write_v2_histogram_float_prefix_native_route",
|
|
)
|
|
.with_default_store_type(store_type)
|
|
.with_query_options(QueryOptions {
|
|
experimental_native_promql: true,
|
|
..Default::default()
|
|
})
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(instance.fe_instance().clone(), false, None)
|
|
.with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
|
|
let metric = "native_histogram_float_prefix";
|
|
let mut request = remote_write_v2::request_with_labels_and_samples(
|
|
vec![(prom_store::METRIC_NAME_LABEL, metric), ("job", "api")],
|
|
vec![],
|
|
);
|
|
let name_ref = request
|
|
.symbols
|
|
.iter()
|
|
.position(|s| s == prom_store::METRIC_NAME_LABEL)
|
|
.unwrap() as u32;
|
|
let metric_ref = request.symbols.iter().position(|s| s == metric).unwrap() as u32;
|
|
let job_ref = request.symbols.iter().position(|s| s == "job").unwrap() as u32;
|
|
let api_ref = request.symbols.iter().position(|s| s == "api").unwrap() as u32;
|
|
// Two samples of one run: the second carries a larger count under the same start timestamp, so
|
|
// the pair is a counter that never restarted. `resets` answers 0 rather than borrowing a reset
|
|
// from the payload comparing unequal, which is `changes`.
|
|
// `positive_deltas` are deltas between neighbouring buckets, so the bucket counts are their
|
|
// running sums: `[2, 1]` holds 2 and 3, and the write checks the count against 5. A payload
|
|
// whose buckets disagree with its count is rejected at the write, not at the query.
|
|
let sample = |timestamp: i64, deltas: Vec<i64>, count: u64, sum: f64| Histogram {
|
|
count: Some(Count::CountInt(count)),
|
|
sum,
|
|
schema: 1,
|
|
positive_spans: vec![BucketSpan {
|
|
offset: 0,
|
|
length: deltas.len() as u32,
|
|
}],
|
|
positive_deltas: deltas,
|
|
// `CounterResetHint::NotCounterReset`
|
|
reset_hint: 2,
|
|
start_timestamp: 1500,
|
|
timestamp,
|
|
..Default::default()
|
|
};
|
|
request.timeseries.push(RemoteWriteV2TimeSeries {
|
|
labels_refs: vec![name_ref, metric_ref, job_ref, api_ref],
|
|
histograms: vec![
|
|
sample(2_000, vec![2, 1], 5, 10.0),
|
|
sample(4_000, vec![4, 2], 10, 20.0),
|
|
],
|
|
..Default::default()
|
|
});
|
|
let payload = prom_store::snappy_compress(&request.encode_to_vec()).unwrap();
|
|
let written = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.header(
|
|
"Content-Type",
|
|
"application/x-protobuf;proto=io.prometheus.write.v2.Request",
|
|
)
|
|
.body(payload)
|
|
.send()
|
|
.await;
|
|
let status = written.status();
|
|
let body = written.text().await;
|
|
assert_eq!(status, StatusCode::NO_CONTENT, "write rejected: {body}");
|
|
|
|
// Every query here transforms the value (a range function counts, resets, changes or reads a
|
|
// timestamp; `sum` aggregates), and Prometheus drops the metric name for all of them. The
|
|
// existing route keeps the name of the series it read, so the routes differ in exactly that
|
|
// label and nowhere else.
|
|
for (query, encoded, expected, drops_name) in [
|
|
(
|
|
"histogram_count(h)",
|
|
"histogram_count(native_histogram_float_prefix)",
|
|
"10.0",
|
|
true,
|
|
),
|
|
(
|
|
"count_over_time(h[1m])",
|
|
"count_over_time(native_histogram_float_prefix%5B1m%5D)",
|
|
"2.0",
|
|
true,
|
|
),
|
|
(
|
|
"resets(h[2m])",
|
|
"resets(native_histogram_float_prefix%5B2m%5D)",
|
|
"0.0",
|
|
true,
|
|
),
|
|
(
|
|
"changes(h[2m])",
|
|
"changes(native_histogram_float_prefix%5B2m%5D)",
|
|
"1.0",
|
|
true,
|
|
),
|
|
(
|
|
"timestamp(h)",
|
|
"timestamp(native_histogram_float_prefix)",
|
|
"4.0",
|
|
true,
|
|
),
|
|
(
|
|
"sum by (job)(resets(h[2m]))",
|
|
"sum%20by%20(job)(resets(native_histogram_float_prefix%5B2m%5D))",
|
|
"0.0",
|
|
true,
|
|
),
|
|
] {
|
|
let url = format!("/v1/prometheus/api/v1/query?query={encoded}&time=4");
|
|
observer.clear();
|
|
let native = client
|
|
.get(&url)
|
|
.header("x-greptime-hints", "query.experimental_native_promql=true")
|
|
.send()
|
|
.await;
|
|
assert_eq!(native.status(), StatusCode::OK, "{query} failed natively");
|
|
let native: Value = native.json().await;
|
|
// The fixture turns the native route on by default, so ask for the existing one
|
|
// explicitly: without the hint this request would run the native plan a second time
|
|
// and `take_all` would collect both runs.
|
|
let legacy = client
|
|
.get(&url)
|
|
.header("x-greptime-hints", "query.experimental_native_promql=false")
|
|
.send()
|
|
.await;
|
|
assert_eq!(legacy.status(), StatusCode::OK, "{query} failed legacy");
|
|
let legacy: Value = legacy.json().await;
|
|
if drops_name {
|
|
// The name is gone from the answer Prometheus gives, so the routes differ in exactly
|
|
// that label: the existing route keeps the name of the series it read.
|
|
assert_eq!(
|
|
native["data"]["result"][0]["metric"].get("__name__"),
|
|
None,
|
|
"{query}: a transforming function drops the name: {native}"
|
|
);
|
|
let mut stripped = legacy.clone();
|
|
for series in stripped["data"]["result"].as_array_mut().unwrap() {
|
|
series["metric"].as_object_mut().unwrap().remove("__name__");
|
|
}
|
|
assert_eq!(
|
|
native["data"]["result"], stripped["data"]["result"],
|
|
"{query}: the native answer differs from the legacy one"
|
|
);
|
|
} else {
|
|
assert_eq!(
|
|
native["data"]["result"], legacy["data"]["result"],
|
|
"{query}: the native answer differs from the legacy one"
|
|
);
|
|
}
|
|
assert_eq!(
|
|
native["data"]["result"][0]["value"][1].as_str(),
|
|
Some(expected),
|
|
"{query}: {native}"
|
|
);
|
|
let fused = observer.take_all(query);
|
|
assert!(
|
|
fused.iter().all(|node| node.name() == "FusedSeriesExec"),
|
|
"{query} executed a node that is not the fused producer: {fused:?}"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// `count` and `group` answer over histograms, through the route that folds them.
|
|
///
|
|
/// Prometheus counts every present sample whatever its type is and answers `1` for a non-empty
|
|
/// group, so neither reads a value. The witness writes two histogram samples of one series and
|
|
/// checks both answers against the existing plan's, so a native answer that dropped the histogram
|
|
/// samples - the shape a fold without the presence arm produces - cannot pass.
|
|
pub async fn test_remote_write_v2_histogram_count_and_group_native_route(store_type: StorageType) {
|
|
let observer = Arc::new(NativeFusedObserver::default());
|
|
let plugins = Plugins::default();
|
|
let wrapper: PhysicalPlanWrapperRef = observer.clone();
|
|
plugins.insert(wrapper);
|
|
let instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"remote_write_v2_histogram_count_and_group",
|
|
)
|
|
.with_default_store_type(store_type)
|
|
.with_query_options(QueryOptions {
|
|
experimental_native_promql: true,
|
|
..Default::default()
|
|
})
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(instance.fe_instance().clone(), false, None)
|
|
.with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
|
|
let metric = "native_histogram_counted";
|
|
let mut request = remote_write_v2::request_with_labels_and_samples(
|
|
vec![(prom_store::METRIC_NAME_LABEL, metric), ("job", "api")],
|
|
vec![],
|
|
);
|
|
let name_ref = request
|
|
.symbols
|
|
.iter()
|
|
.position(|s| s == prom_store::METRIC_NAME_LABEL)
|
|
.unwrap() as u32;
|
|
let metric_ref = request.symbols.iter().position(|s| s == metric).unwrap() as u32;
|
|
let job_ref = request.symbols.iter().position(|s| s == "job").unwrap() as u32;
|
|
let api_ref = request.symbols.iter().position(|s| s == "api").unwrap() as u32;
|
|
// One series, two samples: a count over it is one sample per grid point, whether or not the
|
|
// second sample differs from the first.
|
|
let sample = |timestamp: i64, count: u64| Histogram {
|
|
count: Some(Count::CountInt(count)),
|
|
sum: 2.0 * count as f64,
|
|
schema: 1,
|
|
positive_spans: vec![BucketSpan {
|
|
offset: 0,
|
|
length: 1,
|
|
}],
|
|
positive_deltas: vec![count as i64],
|
|
reset_hint: 2,
|
|
start_timestamp: 1_500,
|
|
timestamp,
|
|
..Default::default()
|
|
};
|
|
request.timeseries.push(RemoteWriteV2TimeSeries {
|
|
labels_refs: vec![name_ref, metric_ref, job_ref, api_ref],
|
|
histograms: vec![sample(2_000, 4), sample(4_000, 8)],
|
|
..Default::default()
|
|
});
|
|
let payload = prom_store::snappy_compress(&request.encode_to_vec()).unwrap();
|
|
let written = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.header(
|
|
"Content-Type",
|
|
"application/x-protobuf;proto=io.prometheus.write.v2.Request",
|
|
)
|
|
.body(payload)
|
|
.send()
|
|
.await;
|
|
let status = written.status();
|
|
let body = written.text().await;
|
|
assert_eq!(status, StatusCode::NO_CONTENT, "write rejected: {body}");
|
|
|
|
for (query, encoded, expected) in [
|
|
("count(h)", "count(native_histogram_counted)", "1.0"),
|
|
("group(h)", "group(native_histogram_counted)", "1.0"),
|
|
] {
|
|
let url = format!("/v1/prometheus/api/v1/query?query={encoded}&time=4");
|
|
observer.clear();
|
|
let native = client
|
|
.get(&url)
|
|
.header("x-greptime-hints", "query.experimental_native_promql=true")
|
|
.send()
|
|
.await;
|
|
assert_eq!(native.status(), StatusCode::OK, "{query} failed natively");
|
|
let native: Value = native.json().await;
|
|
// The fixture turns the native route on by default, so ask for the existing one
|
|
// explicitly: without the hint this request would run the native plan a second time
|
|
// and `take_all` would collect both runs.
|
|
let legacy = client
|
|
.get(&url)
|
|
.header("x-greptime-hints", "query.experimental_native_promql=false")
|
|
.send()
|
|
.await;
|
|
assert_eq!(legacy.status(), StatusCode::OK, "{query} failed legacy");
|
|
let legacy: Value = legacy.json().await;
|
|
assert_eq!(
|
|
native["data"]["result"], legacy["data"]["result"],
|
|
"{query}: the native answer differs from the legacy one"
|
|
);
|
|
assert_eq!(
|
|
native["data"]["result"][0]["value"][1].as_str(),
|
|
Some(expected),
|
|
"{query}: {native}"
|
|
);
|
|
let fused = observer.take_all(query);
|
|
assert!(
|
|
fused.iter().all(|node| node.name() == "FusedSeriesExec"),
|
|
"{query} executed a node that is not the fused producer: {fused:?}"
|
|
);
|
|
}
|
|
|
|
// `min` reads a float value, so a group holding only histograms answers nothing - and says why:
|
|
// Prometheus reports the samples it skipped instead of refusing the statement.
|
|
let url = "/v1/prometheus/api/v1/query?query=min(native_histogram_counted)&time=4";
|
|
observer.clear();
|
|
let response = client
|
|
.get(url)
|
|
.header("x-greptime-hints", "query.experimental_native_promql=true")
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let body: Value = response.json().await;
|
|
assert_eq!(
|
|
body["data"]["result"].as_array().map(Vec::len),
|
|
Some(0),
|
|
"a histogram-only group has no float to answer: {body}"
|
|
);
|
|
// The route has to have run before the annotation can exist: check that first, so a missing
|
|
// info is read as an annotation problem and not as a fall back to the existing plan.
|
|
let fused = observer.take_all("min(h)");
|
|
assert_eq!(fused[0].name(), "FusedSeriesExec");
|
|
let infos = body["infos"]
|
|
.as_array()
|
|
.unwrap_or_else(|| panic!("no infos in {body}"));
|
|
assert!(
|
|
infos.iter().any(|info| info
|
|
.as_str()
|
|
.is_some_and(|text| text.contains("ignored native histogram samples"))),
|
|
"the skipped-sample info did not reach the response: {body}"
|
|
);
|
|
}
|
|
|
|
/// `sum` over a native histogram answers every point of a range, not one point per run.
|
|
///
|
|
/// A split aggregate exports one state row per grid point and folds those rows elsewhere, and three
|
|
/// adjacent samples are where a row addressed by its run instead of by its point collapses: every
|
|
/// point of a run is written into the run's first row and the frame drops the rest, so the range
|
|
/// answers one point carrying the run's last count. The counts written here are 1, 2 and 3, so a
|
|
/// range answering 1, 2, 3 has one row per point and a range answering 3 alone has one row per run.
|
|
/// Every current fixture is a single point or an instant query, which is why none of them sees it.
|
|
pub async fn test_remote_write_v2_histogram_sum_over_range(store_type: StorageType) {
|
|
let observer = Arc::new(NativeFusedObserver::default());
|
|
let plugins = Plugins::default();
|
|
let wrapper: PhysicalPlanWrapperRef = observer.clone();
|
|
plugins.insert(wrapper);
|
|
let instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"remote_write_v2_histogram_sum_over_range",
|
|
)
|
|
.with_default_store_type(store_type)
|
|
.with_query_options(QueryOptions {
|
|
experimental_native_promql: true,
|
|
..Default::default()
|
|
})
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(instance.fe_instance().clone(), false, None)
|
|
.with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
// Three adjacent samples of one series: the range below selects one of them per grid point, so
|
|
// the aggregate folds a run of three and the split exports a state row for each.
|
|
let metric = "native_histogram_range_sum";
|
|
for (timestamp, count, sum) in [(1000, 1u64, 10.0), (2000, 2, 20.0), (3000, 3, 30.0)] {
|
|
write_native_histogram(
|
|
&client,
|
|
&[(prom_store::METRIC_NAME_LABEL, metric), ("job", "api")],
|
|
timestamp,
|
|
count,
|
|
sum,
|
|
)
|
|
.await;
|
|
}
|
|
observer.clear();
|
|
let url = format!("/v1/prometheus/api/v1/query_range?query=sum({metric})&start=1&end=3&step=1");
|
|
let response = client
|
|
.get(&url)
|
|
.header("x-greptime-hints", "query.experimental_native_promql=true")
|
|
.send()
|
|
.await;
|
|
assert_eq!(
|
|
response.status(),
|
|
StatusCode::OK,
|
|
"the range failed natively"
|
|
);
|
|
let body: Value = response.json().await;
|
|
assert_eq!(body["data"]["resultType"], "matrix");
|
|
let results = body["data"]["result"]
|
|
.as_array()
|
|
.unwrap_or_else(|| panic!("no result in {body}"));
|
|
assert_eq!(results.len(), 1, "one group: {body}");
|
|
let histograms = results[0]["histograms"]
|
|
.as_array()
|
|
.unwrap_or_else(|| panic!("no histograms in {body}"));
|
|
// One histogram per grid point, in grid order: a run collapsed into its first row answers one.
|
|
assert_eq!(
|
|
histograms.len(),
|
|
3,
|
|
"the range answers every point of the run: {body}"
|
|
);
|
|
for (index, (count, sum)) in [("1", "10"), ("2", "20"), ("3", "30")]
|
|
.into_iter()
|
|
.enumerate()
|
|
{
|
|
assert_eq!(
|
|
histograms[index][0].as_f64(),
|
|
Some(1.0 + index as f64),
|
|
"point {index} is its own timestamp: {body}"
|
|
);
|
|
assert_eq!(
|
|
histograms[index][1]["count"].as_str(),
|
|
Some(count),
|
|
"point {index} answers the count of its own sample: {body}"
|
|
);
|
|
assert_eq!(
|
|
histograms[index][1]["sum"].as_str(),
|
|
Some(sum),
|
|
"point {index} answers the sum of its own sample: {body}"
|
|
);
|
|
}
|
|
// The answer came from the native route, which is the only one that exports this state.
|
|
let fused = observer.take_all("sum over a range");
|
|
assert!(
|
|
fused.iter().all(|node| node.name() == "FusedSeriesExec"),
|
|
"sum over a range executed a node that is not the fused producer: {fused:?}"
|
|
);
|
|
}
|
|
|
|
// Writes one native histogram sample: `count` observations totalling `sum`, in `schema`.
|
|
///
|
|
/// A custom-bucket histogram is one with `schema: CUSTOM_BUCKETS_SCHEMA`; its bucket counts are
|
|
/// spread the same way over the `custom_values` bounds. The deltas are increments, so the decoded
|
|
/// bucket counts are their running sums and the last one is the second half of `count`.
|
|
async fn write_native_histogram_sample(
|
|
client: &TestClient,
|
|
labels: &[(&str, &str)],
|
|
schema: i32,
|
|
custom_values: Vec<f64>,
|
|
timestamp: i64,
|
|
count: u64,
|
|
sum: f64,
|
|
) {
|
|
let mut request = remote_write_v2::request_with_labels_and_samples(labels.to_vec(), vec![]);
|
|
let label_refs = labels
|
|
.iter()
|
|
.flat_map(|(name, value)| {
|
|
let name_ref = request.symbols.iter().position(|s| s == name).unwrap() as u32;
|
|
let value_ref = request.symbols.iter().position(|s| s == value).unwrap() as u32;
|
|
[name_ref, value_ref]
|
|
})
|
|
.collect();
|
|
let first = count / 2;
|
|
let second = count - first;
|
|
request.timeseries.push(RemoteWriteV2TimeSeries {
|
|
labels_refs: label_refs,
|
|
histograms: vec![Histogram {
|
|
count: Some(Count::CountInt(count)),
|
|
sum,
|
|
schema,
|
|
custom_values,
|
|
positive_spans: vec![BucketSpan {
|
|
offset: 0,
|
|
length: 2,
|
|
}],
|
|
positive_deltas: vec![
|
|
i64::try_from(first).expect("count fits a bucket delta"),
|
|
i64::try_from(second - first).expect("count fits a bucket delta"),
|
|
],
|
|
reset_hint: 2,
|
|
timestamp,
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
});
|
|
let payload = prom_store::snappy_compress(&request.encode_to_vec()).unwrap();
|
|
let response = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.header(
|
|
"Content-Type",
|
|
"application/x-protobuf;proto=io.prometheus.write.v2.Request",
|
|
)
|
|
.body(payload)
|
|
.send()
|
|
.await;
|
|
let status = response.status();
|
|
let body = response.text().await;
|
|
assert_eq!(status, StatusCode::NO_CONTENT, "write rejected: {body}");
|
|
}
|
|
|
|
/// The fused node that publishes the statement's rows.
|
|
///
|
|
/// A split aggregate executes twice: the partial producer exports state frames, and the final
|
|
/// consumer above the series router answers the statement. Only the consumer answers the statement's
|
|
/// rows, so its schema is what tells the two apart.
|
|
///
|
|
/// `sum` and `avg` over native histograms answer a histogram (they add the payloads and the
|
|
/// counts), so the answer column is a column whose type is the native histogram value type. Its
|
|
/// name is the expression's now (`sum(some_metric)`), which is why the type and not the name
|
|
/// identifies it.
|
|
///
|
|
/// The two schemas are otherwise unrelated: a state frame's columns are `read_key`, `is_last`,
|
|
/// `time`, `fields`, `name` and `labels`, while the consumer's are the time index, the answer and
|
|
/// the tags. The producer does carry a histogram, but only inside its accumulator column: `fields`
|
|
/// types every state component in one struct, so the live histogram is a field of a list element
|
|
/// under `fields` rather than a column of that schema. This check therefore reads the schema's own
|
|
/// fields and never descends: the answer is a column of the rows the statement publishes, and
|
|
/// looking deeper would be reading the merge state instead of those rows.
|
|
fn answer_consumer(nodes: &[Arc<dyn ExecutionPlan>]) -> &Arc<dyn ExecutionPlan> {
|
|
use datatypes::prelude::DataType;
|
|
|
|
let histogram_type =
|
|
common_query::native_histogram::native_histogram_value_type().as_arrow_type();
|
|
let mut consumers = nodes.iter().filter(|node| {
|
|
node.schema()
|
|
.fields()
|
|
.iter()
|
|
.any(|field| field.data_type() == &histogram_type)
|
|
});
|
|
let consumer = consumers
|
|
.next()
|
|
.unwrap_or_else(|| panic!("no fused node answers a histogram column: {nodes:?}"));
|
|
assert!(
|
|
consumers.next().is_none(),
|
|
"more than one fused node answers the histogram column: {nodes:?}"
|
|
);
|
|
consumer
|
|
}
|
|
|
|
/// One histogram answer's `count` and `sum`, read out of the Prometheus HTTP API's JSON.
|
|
fn histogram_fields(result: &Value) -> (f64, f64) {
|
|
let histogram = &result["histogram"][1];
|
|
let read = |field: &str| {
|
|
let text = histogram[field]
|
|
.as_str()
|
|
.unwrap_or_else(|| panic!("no `{field}` in {result}"));
|
|
text.parse::<f64>()
|
|
.unwrap_or_else(|_| panic!("`{field}` is not a number in {result}"))
|
|
};
|
|
(read("count"), read("sum"))
|
|
}
|
|
|
|
/// Compares two `(count, sum)` pairs with a relative tolerance.
|
|
fn assert_histogram_fields_close(
|
|
actual: (f64, f64),
|
|
expected: (f64, f64),
|
|
tolerance: f64,
|
|
context: &str,
|
|
) {
|
|
for (actual, expected, field) in [
|
|
(actual.0, expected.0, "count"),
|
|
(actual.1, expected.1, "sum"),
|
|
] {
|
|
assert!(
|
|
(actual - expected).abs() <= tolerance * expected.abs().max(1.0),
|
|
"{context}: {field} {actual} differs from {expected}"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// `sum` and `avg` over native histograms run natively, and agree with the existing path on a
|
|
/// fixture whose arithmetic is well conditioned.
|
|
///
|
|
/// Decision 1 of the design lists homogeneous layouts with small integers as a case where the two
|
|
/// paths agree: they part widely only where the compensated sum matters (`1e16, 1, -1e16` and the
|
|
/// like), so `sum` is compared exactly. `avg` is compared with a tolerance, because the two divide
|
|
/// differently: v3.15 divides the sum by the sample count, while the existing path scales by the
|
|
/// reciprocal, which the fixture above makes visible as `12 * (1/3)` - one ulp below the `4` the
|
|
/// compensated divide answers. The native answer itself is still asserted exactly. The three series
|
|
/// deliberately carry two different exponential schemas, because resolution reduction is part of
|
|
/// the merge and must not disturb the scalar fields.
|
|
pub async fn test_remote_write_v2_histogram_sum_and_avg_native_route(store_type: StorageType) {
|
|
let observer = Arc::new(NativeFusedObserver::default());
|
|
let plugins = Plugins::default();
|
|
let wrapper: PhysicalPlanWrapperRef = observer.clone();
|
|
plugins.insert(wrapper);
|
|
let instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"remote_write_v2_histogram_sum_and_avg",
|
|
)
|
|
.with_default_store_type(store_type)
|
|
.with_query_options(QueryOptions {
|
|
experimental_native_promql: true,
|
|
..Default::default()
|
|
})
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(instance.fe_instance().clone(), false, None)
|
|
.with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
|
|
let metric = "native_histogram_summed";
|
|
for (job, schema, count, sum) in [("a", 1, 2, 1.0), ("b", 0, 4, 2.0), ("c", 1, 6, 3.0)] {
|
|
write_native_histogram_sample(
|
|
&client,
|
|
&[(prom_store::METRIC_NAME_LABEL, metric), ("job", job)],
|
|
schema,
|
|
vec![],
|
|
2_000,
|
|
count,
|
|
sum,
|
|
)
|
|
.await;
|
|
}
|
|
|
|
// `count` and `sum` add exactly: 2 + 4 + 6 observations totalling 1 + 2 + 3, and an average of
|
|
// three samples divides both by three.
|
|
for (query, encoded, expected) in [
|
|
("sum(h)", format!("sum({metric})"), (12.0, 6.0)),
|
|
("avg(h)", format!("avg({metric})"), (4.0, 2.0)),
|
|
] {
|
|
let url = format!("/v1/prometheus/api/v1/query?query={encoded}&time=2");
|
|
observer.clear();
|
|
let native = client
|
|
.get(&url)
|
|
.header("x-greptime-hints", "query.experimental_native_promql=true")
|
|
.send()
|
|
.await;
|
|
assert_eq!(native.status(), StatusCode::OK, "{query} failed natively");
|
|
let native: Value = native.json().await;
|
|
let results = native["data"]["result"]
|
|
.as_array()
|
|
.unwrap_or_else(|| panic!("no results in {native}"));
|
|
assert_eq!(results.len(), 1, "{query}: one group: {native}");
|
|
assert_eq!(
|
|
histogram_fields(&results[0]),
|
|
expected,
|
|
"{query} answered the wrong histogram: {native}"
|
|
);
|
|
|
|
// The existing path answers the same histogram. It is compared with a tolerance rather than
|
|
// exactly: it scales by the reciprocal where v3.15 divides, and on this fixture that is the
|
|
// one-ulp difference `12 * (1/3) = 3.9999999999999996` against `12 / 3 = 4`.
|
|
// The fixture turns the native route on by default, so ask for the existing one
|
|
// explicitly: without the hint this request would run the native plan a second time
|
|
// and `take_all` would collect both runs.
|
|
let legacy = client
|
|
.get(&url)
|
|
.header("x-greptime-hints", "query.experimental_native_promql=false")
|
|
.send()
|
|
.await;
|
|
assert_eq!(legacy.status(), StatusCode::OK, "{query} failed legacy");
|
|
let legacy: Value = legacy.json().await;
|
|
assert_histogram_fields_close(
|
|
histogram_fields(&legacy["data"]["result"][0]),
|
|
expected,
|
|
1e-15,
|
|
&format!("{query}: the existing path answered something else: {legacy}"),
|
|
);
|
|
|
|
// Both halves of the split executed: the partial producer folded each partition and the
|
|
// final consumer merged that state into the answer above.
|
|
let fused = observer.take_all(query);
|
|
assert_eq!(
|
|
fused.len(),
|
|
2,
|
|
"{query}: expected the split's two nodes: {fused:?}"
|
|
);
|
|
let consumer = answer_consumer(&fused);
|
|
assert_eq!(consumer.name(), "FusedSeriesExec");
|
|
}
|
|
}
|
|
|
|
/// A histogram average over several scan partitions answers what one partition answers.
|
|
///
|
|
/// The fixture is eight series worth of small integers, one flush each, so the scan hands the
|
|
/// producer several series batches and the query asks for four partitions. Nothing overflows and
|
|
/// every division is exact, so a merge that weighted its partitions differently from the single
|
|
/// execution would still have to reach the same numbers: `count` and `sum` are compared exactly.
|
|
pub async fn test_remote_write_v2_histogram_avg_native_partitions(store_type: StorageType) {
|
|
let observer = Arc::new(NativeFusedObserver::default());
|
|
let plugins = Plugins::default();
|
|
let wrapper: PhysicalPlanWrapperRef = observer.clone();
|
|
plugins.insert(wrapper);
|
|
let instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"remote_write_v2_histogram_avg_partitions",
|
|
)
|
|
.with_default_store_type(store_type)
|
|
.with_query_options(QueryOptions {
|
|
experimental_native_promql: true,
|
|
parallelism: 4,
|
|
..Default::default()
|
|
})
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(instance.fe_instance().clone(), false, None)
|
|
.with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
|
|
let metric = "native_histogram_averaged";
|
|
for index in 0..8u64 {
|
|
let job = format!("job{index}");
|
|
let host = format!("host{index}");
|
|
write_native_histogram_sample(
|
|
&client,
|
|
&[
|
|
(prom_store::METRIC_NAME_LABEL, metric),
|
|
("job", &job),
|
|
("instance", &host),
|
|
],
|
|
1,
|
|
vec![],
|
|
2_000,
|
|
1 + index,
|
|
2.0 * (1 + index) as f64,
|
|
)
|
|
.await;
|
|
tests_integration::test_util::execute_sql(
|
|
instance.fe_instance(),
|
|
&format!("ADMIN FLUSH_TABLE('{metric}')"),
|
|
)
|
|
.await;
|
|
}
|
|
|
|
let url = format!("/v1/prometheus/api/v1/query?query=avg({metric})&time=2");
|
|
observer.clear();
|
|
let partitioned = client
|
|
.get(&url)
|
|
.header(
|
|
"x-greptime-hints",
|
|
"query.experimental_native_promql=true,query_parallelism=4",
|
|
)
|
|
.send()
|
|
.await;
|
|
assert_eq!(partitioned.status(), StatusCode::OK, "avg failed natively");
|
|
let partitioned: Value = partitioned.json().await;
|
|
let results = partitioned["data"]["result"]
|
|
.as_array()
|
|
.unwrap_or_else(|| panic!("no results in {partitioned}"));
|
|
assert_eq!(results.len(), 1, "one group: {partitioned}");
|
|
// Eight samples of 1..=8 observations totalling 2, 4, ... 16: the mean is 4.5 observations
|
|
// summing to 9, and every step of it is exact in binary.
|
|
assert_eq!(
|
|
histogram_fields(&results[0]),
|
|
(4.5, 9.0),
|
|
"the partitioned average is wrong: {partitioned}"
|
|
);
|
|
let fused = observer.take_all("avg over partitions");
|
|
let consumer = answer_consumer(&fused);
|
|
assert_eq!(consumer.name(), "FusedSeriesExec");
|
|
assert_eq!(
|
|
fused.len(),
|
|
2,
|
|
"the split's partial and final nodes both execute: {fused:?}"
|
|
);
|
|
// The producer runs once per partition that read rows, which is what makes this a merge and not
|
|
// a single fold.
|
|
let producer = fused
|
|
.iter()
|
|
.find(|node| !Arc::ptr_eq(node, consumer))
|
|
.expect("the partial producer");
|
|
let metrics = node_metric_set(producer);
|
|
let mut partitions: BTreeMap<usize, usize> = BTreeMap::new();
|
|
for metric in metrics.iter() {
|
|
if metric.value().name() == "native.root.output_values"
|
|
&& let MetricValue::Count { count, .. } = metric.value()
|
|
{
|
|
*partitions
|
|
.entry(metric.partition().expect("partition label"))
|
|
.or_insert(0usize) += count.value();
|
|
}
|
|
}
|
|
assert_eq!(
|
|
partitions.len(),
|
|
4,
|
|
"the statement asks for four partitions and the producer runs one per partition: {partitions:?}"
|
|
);
|
|
// Routing hashes each series' label set, so eight series over four partitions can legitimately
|
|
// leave one of them empty, and on this fixture the flushes land on three of the four. What the
|
|
// topology has to guarantee is that the fold is spread over more than one partition, since one
|
|
// fold is what the merge has to be told apart from; that no series was lost is the answer
|
|
// asserted above, which counts every one of them exactly once.
|
|
let non_empty = partitions.values().filter(|count| **count > 0).count();
|
|
assert!(
|
|
non_empty >= 2,
|
|
"the average folded in one partition, so nothing was merged: {partitions:?}"
|
|
);
|
|
|
|
// The same statement over the same data asking for one partition: the merge has to answer what
|
|
// the single fold answers.
|
|
observer.clear();
|
|
let single = client
|
|
.get(&url)
|
|
.header(
|
|
"x-greptime-hints",
|
|
"query.experimental_native_promql=true,query_parallelism=1",
|
|
)
|
|
.send()
|
|
.await;
|
|
assert_eq!(single.status(), StatusCode::OK, "avg failed natively");
|
|
let single: Value = single.json().await;
|
|
assert_eq!(
|
|
single["data"]["result"], partitioned["data"]["result"],
|
|
"one partition and four answered differently"
|
|
);
|
|
assert_eq!(
|
|
observer.take_all("avg over one partition").len(),
|
|
2,
|
|
"the single-partition query still runs the split"
|
|
);
|
|
}
|
|
|
|
/// A point whose samples disagree on their bucket layout is removed, and the response says why.
|
|
///
|
|
/// One series carries exponential buckets and the other custom ones, which no addition can merge.
|
|
/// Prometheus drops the point rather than the statement, so the group answers nothing and the
|
|
/// warning reaches the caller - from the node that merged the state, which is the only place the
|
|
/// outcome is final.
|
|
pub async fn test_remote_write_v2_histogram_sum_incompatible_layouts_warns(
|
|
store_type: StorageType,
|
|
) {
|
|
let observer = Arc::new(NativeFusedObserver::default());
|
|
let plugins = Plugins::default();
|
|
let wrapper: PhysicalPlanWrapperRef = observer.clone();
|
|
plugins.insert(wrapper);
|
|
let instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"remote_write_v2_histogram_incompatible_layouts",
|
|
)
|
|
.with_default_store_type(store_type)
|
|
.with_query_options(QueryOptions {
|
|
experimental_native_promql: true,
|
|
..Default::default()
|
|
})
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(instance.fe_instance().clone(), false, None)
|
|
.with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
|
|
let metric = "native_histogram_mixed_layouts";
|
|
write_native_histogram_sample(
|
|
&client,
|
|
&[
|
|
(prom_store::METRIC_NAME_LABEL, metric),
|
|
("job", "exponential"),
|
|
],
|
|
1,
|
|
vec![],
|
|
2_000,
|
|
2,
|
|
1.0,
|
|
)
|
|
.await;
|
|
write_native_histogram_sample(
|
|
&client,
|
|
&[(prom_store::METRIC_NAME_LABEL, metric), ("job", "custom")],
|
|
-53,
|
|
vec![1.0, 2.0],
|
|
2_000,
|
|
2,
|
|
2.0,
|
|
)
|
|
.await;
|
|
|
|
observer.clear();
|
|
let response = client
|
|
.get(&format!(
|
|
"/v1/prometheus/api/v1/query?query=sum({metric})&time=2"
|
|
))
|
|
.header("x-greptime-hints", "query.experimental_native_promql=true")
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK, "the statement failed");
|
|
let body: Value = response.json().await;
|
|
assert_eq!(
|
|
body["data"]["result"].as_array().map(Vec::len),
|
|
Some(0),
|
|
"a group whose layouts disagree has no histogram to answer: {body}"
|
|
);
|
|
// The route has to have run before the warning can exist, so it is checked first: a missing
|
|
// warning is then read as an annotation problem and not as a fall back to the existing plan.
|
|
let fused = observer.take_all("sum over mixed layouts");
|
|
assert_eq!(
|
|
fused.len(),
|
|
2,
|
|
"the split's partial and final nodes both execute: {fused:?}"
|
|
);
|
|
answer_consumer(&fused);
|
|
let warnings = body["warnings"]
|
|
.as_array()
|
|
.unwrap_or_else(|| panic!("no warnings in {body}"));
|
|
assert!(
|
|
warnings.iter().any(|warning| warning
|
|
.as_str()
|
|
.is_some_and(|text| text.contains("incompatible native histogram layouts"))),
|
|
"the removed point did not report its layout: {body}"
|
|
);
|
|
}
|
|
|
|
pub async fn test_prometheus_remote_write_v2_native_histogram(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
// The histogram answers at the end of this test are legacy ones, so the route observer is what
|
|
// proves the route they ran on: the engine-wide option stays off and no request carries a hint.
|
|
let route = Arc::new(PromRouteObserver::default());
|
|
let plugins = Plugins::default();
|
|
plugins.insert::<PromQueryInterceptorRef<servers::error::Error>>(route.clone());
|
|
let mut instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"prometheus_remote_write_v2_native_histogram",
|
|
)
|
|
.with_default_store_type(store_type)
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(instance.fe_instance().clone(), false, None)
|
|
.with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
|
|
let mut write_request = remote_write_v2::request_with_labels_and_samples(
|
|
vec![
|
|
(
|
|
prom_store::METRIC_NAME_LABEL,
|
|
"remote_write_v2_sample_seconds",
|
|
),
|
|
("job", "api"),
|
|
("instance", "localhost:9090"),
|
|
],
|
|
vec![RemoteWriteV2Sample {
|
|
value: 5.0,
|
|
timestamp: 2500,
|
|
start_timestamp: 0,
|
|
}],
|
|
);
|
|
let name_ref = write_request
|
|
.symbols
|
|
.iter()
|
|
.position(|symbol| symbol == prom_store::METRIC_NAME_LABEL)
|
|
.unwrap() as u32;
|
|
let job_ref = write_request
|
|
.symbols
|
|
.iter()
|
|
.position(|symbol| symbol == "job")
|
|
.unwrap() as u32;
|
|
let api_ref = write_request
|
|
.symbols
|
|
.iter()
|
|
.position(|symbol| symbol == "api")
|
|
.unwrap() as u32;
|
|
let instance_ref = write_request
|
|
.symbols
|
|
.iter()
|
|
.position(|symbol| symbol == "instance")
|
|
.unwrap() as u32;
|
|
let localhost_ref = write_request
|
|
.symbols
|
|
.iter()
|
|
.position(|symbol| symbol == "localhost:9090")
|
|
.unwrap() as u32;
|
|
let histogram_metric_ref = write_request.symbols.len() as u32;
|
|
write_request
|
|
.symbols
|
|
.push("remote_write_v2_latency_seconds".to_string());
|
|
write_request.timeseries.push(RemoteWriteV2TimeSeries {
|
|
labels_refs: vec![
|
|
name_ref,
|
|
histogram_metric_ref,
|
|
job_ref,
|
|
api_ref,
|
|
instance_ref,
|
|
localhost_ref,
|
|
],
|
|
histograms: vec![
|
|
Histogram {
|
|
count: Some(Count::CountInt(8)),
|
|
sum: 10.0,
|
|
schema: 1,
|
|
zero_threshold: 0.001,
|
|
zero_count: Some(ZeroCount::ZeroCountInt(1)),
|
|
negative_spans: vec![BucketSpan {
|
|
offset: -2,
|
|
length: 1,
|
|
}],
|
|
negative_deltas: vec![1],
|
|
positive_spans: vec![BucketSpan {
|
|
offset: 0,
|
|
length: 3,
|
|
}],
|
|
positive_deltas: vec![1, 2, -1],
|
|
reset_hint: 2,
|
|
timestamp: 3000,
|
|
start_timestamp: 1500,
|
|
..Default::default()
|
|
},
|
|
Histogram {
|
|
count: Some(Count::CountFloat(6.0)),
|
|
sum: 20.0,
|
|
schema: 2,
|
|
zero_threshold: 0.002,
|
|
zero_count: Some(ZeroCount::ZeroCountFloat(0.5)),
|
|
positive_spans: vec![BucketSpan {
|
|
offset: 3,
|
|
length: 2,
|
|
}],
|
|
positive_counts: vec![2.0, 3.5],
|
|
reset_hint: 3,
|
|
timestamp: 4000,
|
|
start_timestamp: 2500,
|
|
..Default::default()
|
|
},
|
|
],
|
|
..Default::default()
|
|
});
|
|
let large = (1u64 << 53) + 1;
|
|
write_request.timeseries[1].histograms.extend([
|
|
Histogram {
|
|
count: Some(Count::CountInt(large)),
|
|
zero_count: Some(ZeroCount::ZeroCountInt(large)),
|
|
timestamp: 5000,
|
|
..Default::default()
|
|
},
|
|
Histogram {
|
|
count: Some(Count::CountInt(0)),
|
|
sum: f64::from_bits(0x7ff0_0000_0000_0002),
|
|
timestamp: 6000,
|
|
..Default::default()
|
|
},
|
|
Histogram {
|
|
count: Some(Count::CountFloat(0.0)),
|
|
sum: f64::from_bits(0x7ff8_0000_0000_0123),
|
|
timestamp: 7000,
|
|
..Default::default()
|
|
},
|
|
]);
|
|
let serialized_request = write_request.encode_to_vec();
|
|
let compressed_request =
|
|
prom_store::snappy_compress(&serialized_request).expect("failed to encode snappy");
|
|
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.header(
|
|
"Content-Type",
|
|
"application/x-protobuf;proto=io.prometheus.write.v2.Request",
|
|
)
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
let headers = res.headers();
|
|
assert_remote_write_v2_written_headers_with_histograms(&headers, "1", "5");
|
|
assert!(res.text().await.is_empty());
|
|
|
|
validate_data(
|
|
"prometheus_remote_write_v2_native_histogram_sample_rows",
|
|
&client,
|
|
"select greptime_timestamp, greptime_value, job, instance from remote_write_v2_sample_seconds order by greptime_timestamp;",
|
|
"[[2500,5.0,\"api\",\"localhost:9090\"]]",
|
|
)
|
|
.await;
|
|
|
|
let histogram_sql = "select greptime_timestamp, greptime_native_histogram, job, instance from remote_write_v2_latency_seconds where greptime_timestamp < 5000::TIMESTAMP order by greptime_timestamp;";
|
|
let histogram_rows = "[[3000,{\"count_f64\":null,\"count_i64\":8,\"custom_values\":[],\"negative_buckets_f64\":[],\"negative_buckets_i64\":[1],\"negative_span_lengths\":[1],\"negative_span_offsets\":[-2],\"positive_buckets_f64\":[],\"positive_buckets_i64\":[1,3,2],\"positive_span_lengths\":[3],\"positive_span_offsets\":[0],\"reset_hint\":2,\"schema\":1,\"start_timestamp\":1500,\"sum\":10.0,\"zero_count_f64\":null,\"zero_count_i64\":1,\"zero_threshold\":0.001},\"api\",\"localhost:9090\"],[4000,{\"count_f64\":6.0,\"count_i64\":null,\"custom_values\":[],\"negative_buckets_f64\":[],\"negative_buckets_i64\":[],\"negative_span_lengths\":[],\"negative_span_offsets\":[],\"positive_buckets_f64\":[2.0,3.5],\"positive_buckets_i64\":[],\"positive_span_lengths\":[2],\"positive_span_offsets\":[3],\"reset_hint\":3,\"schema\":2,\"start_timestamp\":2500,\"sum\":20.0,\"zero_count_f64\":0.5,\"zero_count_i64\":null,\"zero_threshold\":0.002},\"api\",\"localhost:9090\"]]";
|
|
validate_data(
|
|
"prometheus_remote_write_v2_native_histogram_rows",
|
|
&client,
|
|
histogram_sql,
|
|
histogram_rows,
|
|
)
|
|
.await;
|
|
|
|
// The raw sample carries a start timestamp of 1500, strictly inside the window `(0, 3000]` of
|
|
// the requests below, and it is the window's only sample. A legacy `rate`/`increase` can only
|
|
// answer such a window through that start timestamp: the whole sample is what the increase
|
|
// since the synthetic zero measures, and `rate` is that over the window's seconds.
|
|
for (query, count, sum) in [
|
|
(
|
|
"rate(remote_write_v2_latency_seconds[3s])",
|
|
8.0 / 3.0,
|
|
10.0 / 3.0,
|
|
),
|
|
("increase(remote_write_v2_latency_seconds[3s])", 8.0, 10.0),
|
|
] {
|
|
let res = client
|
|
.get(&format!(
|
|
"/v1/prometheus/api/v1/query?query={}&time=3",
|
|
encode(query)
|
|
))
|
|
.send()
|
|
.await;
|
|
let status = res.status();
|
|
let body: Value = res.json().await;
|
|
assert_eq!(status, StatusCode::OK, "{query}: {body}");
|
|
assert_eq!(body["data"]["resultType"], "vector", "{query}: {body}");
|
|
assert_eq!(
|
|
body["data"]["result"].as_array().map(Vec::len),
|
|
Some(1),
|
|
"{query}: {body}"
|
|
);
|
|
let histogram = &body["data"]["result"][0]["histogram"][1];
|
|
let reported = |field: &str| -> f64 {
|
|
histogram[field]
|
|
.as_str()
|
|
.unwrap_or_else(|| panic!("no histogram {field} in {body}"))
|
|
.parse()
|
|
.unwrap()
|
|
};
|
|
assert!(
|
|
(reported("count") - count).abs() < 1e-12 && (reported("sum") - sum).abs() < 1e-12,
|
|
"{query}: expected count {count} and sum {sum}, got {body}"
|
|
);
|
|
assert!(
|
|
body.get("infos").is_none() && body.get("warnings").is_none(),
|
|
"{query}: a counter histogram with a start timestamp has nothing to report: {body}"
|
|
);
|
|
assert!(
|
|
!executes_fused_series(&route.take_plan()),
|
|
"{query} did not run on the legacy route"
|
|
);
|
|
}
|
|
|
|
// A NaN observation: the buckets hold nine of the count's ten observations and the sum is NaN,
|
|
// which is how Prometheus marks the rest. Prometheus's NaN branch walks the buckets forward and
|
|
// leaves the top bucket for the interpolation, so the half quantile reads 2^5 rather than the
|
|
// 0.7711 the buckets' own rank gives - and the response has to say why.
|
|
let nan_request = remote_write_v2::request_with_labels_and_histograms(
|
|
vec![
|
|
(
|
|
prom_store::METRIC_NAME_LABEL,
|
|
"remote_write_v2_nan_quantile",
|
|
),
|
|
("job", "api"),
|
|
],
|
|
vec![Histogram {
|
|
count: Some(Count::CountInt(10)),
|
|
sum: f64::NAN,
|
|
// A bucket's upper bound is 2^(index * 2^-schema), so schema 0 makes the two buckets
|
|
// (0.5, 1] and (1, 2]; the deltas hold 8 and then 1.
|
|
schema: 0,
|
|
positive_spans: vec![BucketSpan {
|
|
offset: 0,
|
|
length: 2,
|
|
}],
|
|
positive_deltas: vec![8, -7],
|
|
reset_hint: 2,
|
|
timestamp: 3_000,
|
|
..Default::default()
|
|
}],
|
|
);
|
|
let payload = prom_store::snappy_compress(&nan_request.encode_to_vec()).unwrap();
|
|
let written = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.header(
|
|
"Content-Type",
|
|
"application/x-protobuf;proto=io.prometheus.write.v2.Request",
|
|
)
|
|
.body(payload)
|
|
.send()
|
|
.await;
|
|
let status = written.status();
|
|
let body = written.text().await;
|
|
assert_eq!(status, StatusCode::NO_CONTENT, "write rejected: {body}");
|
|
|
|
let res = client
|
|
.get(&format!(
|
|
"/v1/prometheus/api/v1/query?query={}&time=3",
|
|
encode("histogram_quantile(0.5, remote_write_v2_nan_quantile)")
|
|
))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body: Value = res.json().await;
|
|
assert_eq!(body["data"]["resultType"], "vector", "{body}");
|
|
assert_eq!(
|
|
body["data"]["result"][0]["value"][1], "32.0",
|
|
"the NaN walk leaves the top bucket: {body}"
|
|
);
|
|
assert_eq!(
|
|
body["infos"],
|
|
json!(["input to histogram_quantile has NaN observations, result is skewed higher"]),
|
|
"{body}"
|
|
);
|
|
assert!(
|
|
!executes_fused_series(&route.take_plan()),
|
|
"`histogram_quantile` did not run on the legacy route"
|
|
);
|
|
|
|
for flushed in [false, true] {
|
|
if flushed {
|
|
flush_table(&client, "remote_write_v2_latency_seconds").await;
|
|
}
|
|
for accepted_response_types in [vec![], vec![0], vec![1, 0]] {
|
|
let query = |name: &str| Query {
|
|
start_timestamp_ms: 0,
|
|
end_timestamp_ms: 10_000,
|
|
matchers: vec![LabelMatcher {
|
|
name: prom_store::METRIC_NAME_LABEL.to_string(),
|
|
value: name.to_string(),
|
|
r#type: 0,
|
|
}],
|
|
..Default::default()
|
|
};
|
|
let request = ReadRequest {
|
|
queries: vec![
|
|
query("remote_write_v2_sample_seconds"),
|
|
query("missing_native_histogram"),
|
|
query("remote_write_v2_latency_seconds"),
|
|
],
|
|
accepted_response_types,
|
|
};
|
|
let response = client
|
|
.post("/v1/prometheus/read")
|
|
.body(prom_store::snappy_compress(&request.encode_to_vec()).unwrap())
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
assert_eq!(response.headers()["content-type"], "application/x-protobuf");
|
|
assert_eq!(response.headers()["content-encoding"], "snappy");
|
|
let body = prom_store::snappy_decompress(&response.bytes().await).unwrap();
|
|
let response = ReadResponse::decode(body.as_slice()).unwrap();
|
|
assert_eq!(response.results.len(), 3);
|
|
assert_eq!(
|
|
response.results[0].timeseries[0].samples,
|
|
vec![Sample {
|
|
value: 5.0,
|
|
timestamp: 2500
|
|
}]
|
|
);
|
|
assert!(response.results[1].timeseries.is_empty());
|
|
let series = &response.results[2].timeseries[0];
|
|
assert!(series.samples.is_empty());
|
|
assert_eq!(
|
|
series
|
|
.histograms
|
|
.iter()
|
|
.map(|h| h.timestamp)
|
|
.collect::<Vec<_>>(),
|
|
vec![3000, 4000, 5000, 6000, 7000]
|
|
);
|
|
assert_eq!(series.histograms[0].positive_deltas, vec![1, 2, -1]);
|
|
assert_eq!(series.histograms[0].negative_deltas, vec![1]);
|
|
assert_eq!(series.histograms[1].positive_counts, vec![2.0, 3.5]);
|
|
assert_eq!(
|
|
series.histograms[2].count,
|
|
Some(api::prom_store::remote::histogram::Count::CountInt(large))
|
|
);
|
|
assert_eq!(
|
|
series.histograms[2].zero_count,
|
|
Some(api::prom_store::remote::histogram::ZeroCount::ZeroCountInt(
|
|
large
|
|
))
|
|
);
|
|
assert_eq!(series.histograms[3].sum.to_bits(), 0x7ff0_0000_0000_0002);
|
|
assert_eq!(series.histograms[4].sum.to_bits(), 0x7ff8_0000_0000_0123);
|
|
}
|
|
}
|
|
|
|
// The native histogram column is a struct of nested lists, so its SST
|
|
// encoding differs from its memtable representation.
|
|
flush_table(&client, "remote_write_v2_latency_seconds").await;
|
|
validate_data(
|
|
"prometheus_remote_write_v2_native_histogram_rows_after_flush",
|
|
&client,
|
|
histogram_sql,
|
|
histogram_rows,
|
|
)
|
|
.await;
|
|
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/metadata?metric=remote_write_v2_latency_seconds")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(
|
|
serde_json::to_string_pretty(&body).unwrap(),
|
|
r#"{
|
|
"status": "success",
|
|
"data": {
|
|
"remote_write_v2_latency_seconds": [
|
|
{
|
|
"type": "histogram",
|
|
"unit": "",
|
|
"help": ""
|
|
}
|
|
]
|
|
}
|
|
}"#
|
|
);
|
|
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/query?query=histogram_count(remote_write_v2_latency_seconds)&time=4")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = res.json::<PrometheusJsonResponse>().await;
|
|
// Finite vector sample values use the HTTP response's ryu contract, so
|
|
// integral f64 values retain their explicit `.0`; timestamps remain JSON numbers.
|
|
assert_eq!(
|
|
serde_json::to_string_pretty(&body).unwrap(),
|
|
r#"{
|
|
"status": "success",
|
|
"data": {
|
|
"resultType": "vector",
|
|
"result": [
|
|
{
|
|
"metric": {
|
|
"__name__": "remote_write_v2_latency_seconds",
|
|
"instance": "localhost:9090",
|
|
"job": "api"
|
|
},
|
|
"value": [
|
|
4.0,
|
|
"6.0"
|
|
]
|
|
}
|
|
]
|
|
}
|
|
}"#
|
|
);
|
|
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/query?query=remote_write_v2_latency_seconds&time=4")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(
|
|
serde_json::to_string_pretty(&body).unwrap(),
|
|
r#"{
|
|
"status": "success",
|
|
"data": {
|
|
"resultType": "vector",
|
|
"result": [
|
|
{
|
|
"metric": {
|
|
"__name__": "remote_write_v2_latency_seconds",
|
|
"instance": "localhost:9090",
|
|
"job": "api"
|
|
},
|
|
"histogram": [
|
|
4.0,
|
|
{
|
|
"count": "6",
|
|
"sum": "20",
|
|
"buckets": [
|
|
[
|
|
3,
|
|
"-0.002",
|
|
"0.002",
|
|
"0.5"
|
|
],
|
|
[
|
|
0,
|
|
"1.4142135623730951",
|
|
"1.681792830507429",
|
|
"2"
|
|
],
|
|
[
|
|
0,
|
|
"1.681792830507429",
|
|
"2",
|
|
"3.5"
|
|
]
|
|
]
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
}"#
|
|
);
|
|
|
|
let res = client
|
|
.get("/v1/prometheus/api/v1/query_range?query=remote_write_v2_latency_seconds&start=3&end=4&step=1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body = res.json::<PrometheusJsonResponse>().await;
|
|
assert_eq!(
|
|
serde_json::to_string_pretty(&body).unwrap(),
|
|
r#"{
|
|
"status": "success",
|
|
"data": {
|
|
"resultType": "matrix",
|
|
"result": [
|
|
{
|
|
"metric": {
|
|
"__name__": "remote_write_v2_latency_seconds",
|
|
"instance": "localhost:9090",
|
|
"job": "api"
|
|
},
|
|
"histograms": [
|
|
[
|
|
3.0,
|
|
{
|
|
"count": "8",
|
|
"sum": "10",
|
|
"buckets": [
|
|
[
|
|
1,
|
|
"-0.5",
|
|
"-0.3535533905932738",
|
|
"1"
|
|
],
|
|
[
|
|
3,
|
|
"-0.001",
|
|
"0.001",
|
|
"1"
|
|
],
|
|
[
|
|
0,
|
|
"0.7071067811865476",
|
|
"1",
|
|
"1"
|
|
],
|
|
[
|
|
0,
|
|
"1",
|
|
"1.4142135623730951",
|
|
"3"
|
|
],
|
|
[
|
|
0,
|
|
"1.4142135623730951",
|
|
"2",
|
|
"2"
|
|
]
|
|
]
|
|
}
|
|
],
|
|
[
|
|
4.0,
|
|
{
|
|
"count": "6",
|
|
"sum": "20",
|
|
"buckets": [
|
|
[
|
|
3,
|
|
"-0.002",
|
|
"0.002",
|
|
"0.5"
|
|
],
|
|
[
|
|
0,
|
|
"1.4142135623730951",
|
|
"1.681792830507429",
|
|
"2"
|
|
],
|
|
[
|
|
0,
|
|
"1.681792830507429",
|
|
"2",
|
|
"3.5"
|
|
]
|
|
]
|
|
}
|
|
]
|
|
]
|
|
}
|
|
]
|
|
}
|
|
}"#
|
|
);
|
|
|
|
validate_data(
|
|
"prometheus_remote_write_v2_native_histogram_table_created",
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name = 'remote_write_v2_latency_seconds';",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
|
|
validate_data(
|
|
"prometheus_remote_write_v2_native_histogram_suffix_table_not_created",
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name = 'remote_write_v2_latency_seconds_native_histogram';",
|
|
"[[0]]",
|
|
)
|
|
.await;
|
|
|
|
instance.guard.remove_all().await;
|
|
}
|
|
|
|
#[apply(both_deployment_cases)]
|
|
async fn test_prometheus_remote_write_batched_skip_wal(distributed: bool) {
|
|
check_prometheus_remote_write_batched_skip_wal(distributed, false).await;
|
|
}
|
|
|
|
#[apply(both_deployment_cases)]
|
|
async fn test_prometheus_remote_write_v2_batched_skip_wal(distributed: bool) {
|
|
check_prometheus_remote_write_batched_skip_wal(distributed, true).await;
|
|
}
|
|
|
|
async fn check_prometheus_remote_write_batched_skip_wal(distributed: bool, v2: bool) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let mut instance =
|
|
MockInstanceImpl::new(&format!("prom_bulk_skip_wal_v2_{v2}"), distributed).await;
|
|
let server = build_test_prom_server(instance.frontend(), true, None).build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
|
|
write_prometheus_skip_wal_sample(&client, v2, 1000, None).await;
|
|
wait_for_data(&client, "SELECT count(*) FROM wal_prom_bulk", "[[1]]").await;
|
|
let mut before = instance.flush_and_snapshot_wal().await;
|
|
// Reuse the same batcher across policy transitions to detect leaked policy.
|
|
for (index, hint) in [None, Some("true"), Some("false"), None]
|
|
.into_iter()
|
|
.enumerate()
|
|
{
|
|
write_prometheus_skip_wal_sample(&client, v2, (index as i64 + 2) * 1000, hint).await;
|
|
wait_for_data(
|
|
&client,
|
|
"SELECT count(*) FROM wal_prom_bulk",
|
|
&format!("[[{}]]", index + 2),
|
|
)
|
|
.await;
|
|
let after = instance.flush_and_snapshot_wal().await;
|
|
assert_wal_delta(&before, &after, hint == Some("true"));
|
|
before = after;
|
|
}
|
|
instance.shutdown().await;
|
|
}
|
|
|
|
async fn write_prometheus_skip_wal_sample(
|
|
client: &TestClient,
|
|
v2: bool,
|
|
timestamp: i64,
|
|
hint: Option<&str>,
|
|
) {
|
|
let payload = if v2 {
|
|
remote_write_v2::request_with_labels_and_samples(
|
|
vec![(prom_store::METRIC_NAME_LABEL, "wal_prom_bulk")],
|
|
vec![RemoteWriteV2Sample {
|
|
value: 1.0,
|
|
timestamp,
|
|
start_timestamp: 0,
|
|
}],
|
|
)
|
|
.encode_to_vec()
|
|
} else {
|
|
WriteRequest {
|
|
timeseries: vec![TimeSeries {
|
|
labels: vec![Label {
|
|
name: prom_store::METRIC_NAME_LABEL.to_string(),
|
|
value: "wal_prom_bulk".to_string(),
|
|
}],
|
|
samples: vec![Sample {
|
|
value: 1.0,
|
|
timestamp,
|
|
}],
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
}
|
|
.encode_to_vec()
|
|
};
|
|
let mut request = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(prom_store::snappy_compress(&payload).unwrap());
|
|
if v2 {
|
|
request = request.header(
|
|
"Content-Type",
|
|
"application/x-protobuf;proto=io.prometheus.write.v2.Request",
|
|
);
|
|
}
|
|
if let Some(hint) = hint {
|
|
request = request.header("x-greptime-insert-skip-wal", hint);
|
|
}
|
|
assert_eq!(request.send().await.status(), StatusCode::NO_CONTENT);
|
|
}
|
|
|
|
/// Batched remote write against a logical table bound to a non-millisecond
|
|
/// physical table: the bulk guard must reject the destination (the bulk
|
|
/// encode only produces millisecond batches) and fall back to the ordinary
|
|
/// insert path, which converts the requests to the table's unit.
|
|
pub async fn test_prometheus_remote_write_batched_mixed_time_index_units(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_prom_app_with_frontend_batched(store_type, "prom_rw_batched_mixed_units").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let res = client
|
|
.get("/v1/sql?db=public&sql=CREATE TABLE phy_us (greptime_timestamp TIMESTAMP(6) NOT NULL, greptime_value DOUBLE NULL, TIME INDEX (greptime_timestamp)) ENGINE = metric WITH ('physical_metric_table' = 'true')")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let write = |metric: &str, value: f64, timestamp: i64| {
|
|
let write_request = WriteRequest {
|
|
timeseries: vec![TimeSeries {
|
|
labels: vec![
|
|
Label {
|
|
name: prom_store::METRIC_NAME_LABEL.to_string(),
|
|
value: metric.to_string(),
|
|
},
|
|
Label {
|
|
name: "job".to_string(),
|
|
value: "demo".to_string(),
|
|
},
|
|
],
|
|
samples: vec![Sample { value, timestamp }],
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
};
|
|
prom_store::snappy_compress(&write_request.encode_to_vec()).unwrap()
|
|
};
|
|
|
|
// Create the logical table on the microsecond physical table. The bulk
|
|
// path handles the microsecond selected physical table: the new table
|
|
// is created on it and the samples are widened to its unit during
|
|
// batch alignment.
|
|
let res = client
|
|
.post("/v1/prometheus/write?physical_table=phy_us")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(write("us_metric", 2.5, 1500))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
// Write the same existing table again while selecting the default
|
|
// (millisecond) physical table: the destination is bound to another
|
|
// physical table, so the bulk eligibility check must reject it and fall
|
|
// back to the ordinary insert path — without the destination binding
|
|
// check the bulk flush would write the rows through the selected
|
|
// physical's regions, silently misplacing them.
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(write("us_metric", 3.5, 2000))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
// Both samples are stored on the microsecond time index.
|
|
validate_data(
|
|
"prom_rw_batched_mixed_units",
|
|
&client,
|
|
"SELECT COUNT(*), MAX(greptime_value) FROM us_metric",
|
|
"[[2,3.5]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
/// A Prometheus write interceptor that redirects every remote write to the
|
|
/// `tenant_redirect` schema, like a per-tenant redirection: the incoming
|
|
/// context targets `public` while the destination tables live elsewhere.
|
|
struct PromSchemaRedirectInterceptor;
|
|
|
|
impl servers::interceptor::PromStoreProtocolInterceptor for PromSchemaRedirectInterceptor {
|
|
type Error = servers::error::Error;
|
|
|
|
fn pre_write(
|
|
&self,
|
|
_write_req: &RowInsertRequests,
|
|
ctx: QueryContextRef,
|
|
) -> servers::error::Result<()> {
|
|
ctx.set_current_schema("tenant_redirect");
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
async fn setup_redirecting_batched_prom_app(
|
|
store_type: StorageType,
|
|
name: &str,
|
|
) -> (TestClient, TestGuard) {
|
|
let plugins = Plugins::default();
|
|
plugins.insert::<servers::interceptor::PromStoreProtocolInterceptorRef<servers::error::Error>>(
|
|
Arc::new(PromSchemaRedirectInterceptor),
|
|
);
|
|
let standalone = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(name)
|
|
.with_default_store_type(store_type)
|
|
.with_plugin(plugins)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(standalone.fe_instance().clone(), true, None)
|
|
.with_greptime_config_options(standalone.opts.datanode_options().to_toml().unwrap())
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
(client, standalone.guard)
|
|
}
|
|
|
|
async fn create_redirect_schema_with_microsecond_table(client: &TestClient, table: &str) {
|
|
for sql in [
|
|
"create database if not exists tenant_redirect",
|
|
"CREATE TABLE tenant_redirect.phy_us \
|
|
(ts timestamp(6) time index, val double, host string primary key) \
|
|
engine=metric with ('physical_metric_table' = 'true')",
|
|
&format!(
|
|
"CREATE TABLE tenant_redirect.{table} \
|
|
(ts timestamp(6) time index, val double, host string primary key) \
|
|
engine=metric with ('on_physical_table' = 'phy_us')"
|
|
),
|
|
] {
|
|
let res = client
|
|
.get(format!("/v1/sql?sql={sql}").as_str())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "setup: {sql}");
|
|
}
|
|
}
|
|
|
|
/// Regression test: with batching enabled and a pre_write interceptor that
|
|
/// redirects the context to a schema holding same-named non-millisecond
|
|
/// tables, the bulk eligibility must be evaluated against the redirected
|
|
/// destinations (after preflight) and fall back to the ordinary insert path
|
|
/// instead of failing in the bulk encode.
|
|
pub async fn test_prometheus_remote_write_batched_interceptor_time_index_units(
|
|
store_type: StorageType,
|
|
) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (client, mut guard) =
|
|
setup_redirecting_batched_prom_app(store_type, "prom_rw_batched_interceptor_units").await;
|
|
create_redirect_schema_with_microsecond_table(&client, "intercept_metric").await;
|
|
|
|
let write_request = WriteRequest {
|
|
timeseries: vec![TimeSeries {
|
|
labels: vec![
|
|
Label {
|
|
name: prom_store::METRIC_NAME_LABEL.to_string(),
|
|
value: "intercept_metric".to_string(),
|
|
},
|
|
Label {
|
|
name: "job".to_string(),
|
|
value: "demo".to_string(),
|
|
},
|
|
],
|
|
samples: vec![Sample {
|
|
value: 1.0,
|
|
timestamp: 1000,
|
|
}],
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
};
|
|
let compressed = prom_store::snappy_compress(&write_request.encode_to_vec()).unwrap();
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(compressed)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
validate_data(
|
|
"prom_rw_batched_interceptor_units",
|
|
&client,
|
|
"SELECT COUNT(*) FROM tenant_redirect.intercept_metric",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
/// The v2 variant of the interceptor regression test: a mixed samples-plus-
|
|
/// histograms request whose existing sample destination lives in the
|
|
/// redirected schema with a microsecond time index must fall back to the
|
|
/// ordinary insert path (both series land), not fail in the bulk encode.
|
|
pub async fn test_prometheus_remote_write_v2_batched_interceptor_time_index_units(
|
|
store_type: StorageType,
|
|
) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (client, mut guard) =
|
|
setup_redirecting_batched_prom_app(store_type, "prom_rw_v2_batched_interceptor_units")
|
|
.await;
|
|
create_redirect_schema_with_microsecond_table(&client, "v2_mixed_sample").await;
|
|
|
|
// One sample series targeting the existing microsecond table, plus one
|
|
// histogram series for a new (millisecond) table.
|
|
let mut symbols = vec![String::new()];
|
|
let mut symbol = |value: &str| {
|
|
symbols.push(value.to_string());
|
|
(symbols.len() - 1) as u32
|
|
};
|
|
let name_ref = symbol("__name__");
|
|
let sample_ref = symbol("v2_mixed_sample");
|
|
let job_ref = symbol("job");
|
|
let demo_ref = symbol("demo");
|
|
let name_ref2 = symbol("__name__");
|
|
let histo_ref = symbol("v2_mixed_histo");
|
|
let request = RemoteWriteV2Request {
|
|
symbols,
|
|
timeseries: vec![
|
|
RemoteWriteV2TimeSeries {
|
|
labels_refs: vec![name_ref, sample_ref, job_ref, demo_ref],
|
|
samples: vec![RemoteWriteV2Sample {
|
|
value: 1.0,
|
|
timestamp: 1000,
|
|
start_timestamp: 0,
|
|
}],
|
|
..Default::default()
|
|
},
|
|
RemoteWriteV2TimeSeries {
|
|
labels_refs: vec![name_ref2, histo_ref, job_ref, demo_ref],
|
|
histograms: vec![Histogram {
|
|
count: Some(Count::CountInt(1)),
|
|
sum: 1.0,
|
|
positive_spans: vec![BucketSpan {
|
|
offset: 0,
|
|
length: 1,
|
|
}],
|
|
positive_deltas: vec![1],
|
|
timestamp: 1000,
|
|
start_timestamp: 1000,
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
},
|
|
],
|
|
};
|
|
let compressed = prom_store::snappy_compress(&request.encode_to_vec()).unwrap();
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header(
|
|
"Content-Type",
|
|
"application/x-protobuf;proto=io.prometheus.write.v2.Request",
|
|
)
|
|
.header("Content-Encoding", "snappy")
|
|
.body(compressed)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
validate_data(
|
|
"prom_rw_v2_batched_interceptor_units",
|
|
&client,
|
|
"SELECT COUNT(*) FROM tenant_redirect.v2_mixed_sample",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"prom_rw_v2_batched_interceptor_units",
|
|
&client,
|
|
"SELECT COUNT(*) FROM tenant_redirect.v2_mixed_histo",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
/// Covers the batched (pending-rows-batcher) Prometheus remote write path, which
|
|
/// bypasses `PromStoreProtocolHandler::write`. Verifies the metric table is created
|
|
/// asynchronously and still carries the Prometheus semantic identity stamped on the
|
|
/// shared request context.
|
|
/// Regression test for <https://github.com/GreptimeTeam/greptimedb/issues/9342>:
|
|
/// prometheus remote write uses the logical batcher against a physical
|
|
/// metric table pre-created with a microsecond time index; the millisecond
|
|
/// samples are widened to the physical table's unit during batch alignment.
|
|
pub async fn test_prometheus_remote_write_batched_microsecond_physical_table(
|
|
store_type: StorageType,
|
|
) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_prom_app_with_frontend_batched(
|
|
store_type,
|
|
"prometheus_remote_write_batched_us_physical",
|
|
)
|
|
.await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// Pre-create the default physical metric table with a microsecond time
|
|
// index before any remote write, so the batched bulk path must handle it.
|
|
let res = client
|
|
.get(
|
|
"/v1/sql?db=public&sql=CREATE TABLE greptime_physical_table \
|
|
(greptime_timestamp TIMESTAMP(6) NOT NULL, greptime_value DOUBLE NULL, \
|
|
TIME INDEX (greptime_timestamp)) \
|
|
ENGINE = metric WITH ('physical_metric_table' = 'true')",
|
|
)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let write_request = WriteRequest {
|
|
timeseries: vec![prom_store::mock_timeseries()[0].clone()],
|
|
..Default::default()
|
|
};
|
|
let serialized_request = write_request.encode_to_vec();
|
|
let compressed_request =
|
|
prom_store::snappy_compress(&serialized_request).expect("failed to encode snappy");
|
|
|
|
// submit_build_and_align counts every batcher submission in both
|
|
// acknowledgement modes, so the stored-value assertions below cannot be
|
|
// satisfied by a silent fallback to ordinary insertion.
|
|
let submissions = servers::metrics::PENDING_ROWS_BATCH_INGEST_STAGE_ELAPSED
|
|
.with_label_values(&["submit_build_and_align"]);
|
|
let before = submissions.get_sample_count();
|
|
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
assert_eq!(
|
|
submissions.get_sample_count() - before,
|
|
1,
|
|
"non-millisecond physical tables must use the logical batcher"
|
|
);
|
|
|
|
// metric1 samples are 1.0@1000ms and 2.0@2000ms; on the microsecond
|
|
// physical table they must be stored as 1_000_000us and 2_000_000us.
|
|
wait_for_data(
|
|
&client,
|
|
"select greptime_timestamp, greptime_value from metric1 order by greptime_timestamp",
|
|
"[[1000000,1.0],[2000000,2.0]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
/// Regression test for the per-series physical-table selection conflict:
|
|
/// one remote-write request containing the same metric under two different
|
|
/// physical-table selections must fall back to the ordinary insert path —
|
|
/// both when the destination table already exists (bound to one of the two
|
|
/// physicals) and when it is missing (conflicting creations). A consistent
|
|
/// selection must still use the batcher.
|
|
pub async fn test_prometheus_remote_write_batched_conflicting_physical_selections(
|
|
store_type: StorageType,
|
|
) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_prom_app_with_frontend_batched(
|
|
store_type,
|
|
"prom_rw_batched_conflicting_physicals",
|
|
)
|
|
.await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let res = client
|
|
.get(
|
|
"/v1/sql?db=public&sql=CREATE TABLE p1 (greptime_timestamp TIMESTAMP(6) NOT NULL, greptime_value DOUBLE NULL, TIME INDEX (greptime_timestamp)) ENGINE = metric WITH ('physical_metric_table' = 'true')",
|
|
)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
// An existing logical table bound to p1.
|
|
let res = client
|
|
.get(
|
|
"/v1/sql?db=public&sql=CREATE TABLE conflict_existing (greptime_timestamp TIMESTAMP(6) NOT NULL, greptime_value DOUBLE NULL, \"job\" STRING NULL, TIME INDEX (greptime_timestamp), PRIMARY KEY (\"job\")) ENGINE = metric WITH ('on_physical_table' = 'p1')",
|
|
)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let write = |metric: &str, job: &str, physical: Option<&str>, value: f64| {
|
|
let mut labels = vec![
|
|
Label {
|
|
name: prom_store::METRIC_NAME_LABEL.to_string(),
|
|
value: metric.to_string(),
|
|
},
|
|
Label {
|
|
name: "job".to_string(),
|
|
value: job.to_string(),
|
|
},
|
|
];
|
|
if let Some(physical) = physical {
|
|
labels.push(Label {
|
|
name: "x_greptime_physical_table".to_string(),
|
|
value: physical.to_string(),
|
|
});
|
|
}
|
|
TimeSeries {
|
|
labels,
|
|
samples: vec![Sample {
|
|
value,
|
|
timestamp: 1000,
|
|
}],
|
|
..Default::default()
|
|
}
|
|
};
|
|
let send = |timeseries: Vec<TimeSeries>| {
|
|
let write_request = WriteRequest {
|
|
timeseries,
|
|
..Default::default()
|
|
};
|
|
prom_store::snappy_compress(&write_request.encode_to_vec()).unwrap()
|
|
};
|
|
|
|
let submissions = servers::metrics::PENDING_ROWS_BATCH_INGEST_STAGE_ELAPSED
|
|
.with_label_values(&["submit_build_and_align"]);
|
|
|
|
// Existing destination under two different selections: the batch must
|
|
// fall back (the batch for the unbound selection would otherwise write
|
|
// through the default physical's regions), and both series land on the
|
|
// bound physical.
|
|
let before = submissions.get_sample_count();
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(send(vec![
|
|
write("conflict_existing", "a", Some("p1"), 1.0),
|
|
write("conflict_existing", "b", None, 2.0),
|
|
]))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
assert_eq!(
|
|
submissions.get_sample_count() - before,
|
|
0,
|
|
"conflicting selections for an existing table must fall back"
|
|
);
|
|
wait_for_data(
|
|
&client,
|
|
"select count(*), sum(greptime_value) from conflict_existing",
|
|
"[[2,3.0]]",
|
|
)
|
|
.await;
|
|
|
|
// Missing destination under two different selections: conflicting
|
|
// creations must also fall back, and both series must be visible
|
|
// through the created table.
|
|
let before = submissions.get_sample_count();
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(send(vec![
|
|
write("conflict_fresh", "a", Some("p1"), 1.0),
|
|
write("conflict_fresh", "b", None, 2.0),
|
|
]))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
assert_eq!(
|
|
submissions.get_sample_count() - before,
|
|
0,
|
|
"conflicting selections for a missing table must fall back"
|
|
);
|
|
wait_for_data(
|
|
&client,
|
|
"select count(*), sum(greptime_value) from conflict_fresh",
|
|
"[[2,3.0]]",
|
|
)
|
|
.await;
|
|
|
|
// A consistent selection still uses the batcher.
|
|
let before = submissions.get_sample_count();
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(send(vec![
|
|
write("consistent_fresh", "a", Some("p1"), 1.0),
|
|
write("consistent_fresh", "b", Some("p1"), 2.0),
|
|
]))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
assert_eq!(
|
|
submissions.get_sample_count() - before,
|
|
1,
|
|
"a consistent selection must use the logical batcher"
|
|
);
|
|
wait_for_data(
|
|
&client,
|
|
"select count(*), sum(greptime_value) from consistent_fresh",
|
|
"[[2,3.0]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_prometheus_remote_write_batched(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_prom_app_with_frontend_batched(store_type, "prometheus_remote_write_batched")
|
|
.await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let write_request = WriteRequest {
|
|
timeseries: prom_store::mock_timeseries(),
|
|
..Default::default()
|
|
};
|
|
let serialized_request = write_request.encode_to_vec();
|
|
let compressed_request =
|
|
prom_store::snappy_compress(&serialized_request).expect("failed to encode snappy");
|
|
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
// The batcher flushes asynchronously, so poll until the table exists and carries
|
|
// the semantic identity (signal_type/source/metadata_quality).
|
|
wait_for_data(
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name = 'metric2' \
|
|
and create_options like '%greptime.semantic.signal_type=metric%' \
|
|
and create_options like '%greptime.semantic.source=prometheus%' \
|
|
and create_options like '%greptime.semantic.metric.metadata_quality=inferred%'",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
|
|
// The batched path bypasses the operator's auto-create: v2 per-series
|
|
// metadata must reach its table options through the batch create too.
|
|
let mut write_request = remote_write_v2::request_with_labels_and_samples(
|
|
vec![
|
|
(
|
|
prom_store::METRIC_NAME_LABEL,
|
|
"remote_write_batched_typed_total",
|
|
),
|
|
("job", "api"),
|
|
],
|
|
vec![RemoteWriteV2Sample {
|
|
value: 1.0,
|
|
timestamp: 1000,
|
|
start_timestamp: 0,
|
|
}],
|
|
);
|
|
let unit_ref = write_request.symbols.len() as u32;
|
|
write_request.symbols.push("bytes".to_string());
|
|
write_request.timeseries[0].metadata = Some(RemoteWriteV2Metadata {
|
|
r#type: RemoteWriteV2MetricType::Gauge as i32,
|
|
help_ref: 0,
|
|
unit_ref,
|
|
});
|
|
let compressed_request = prom_store::snappy_compress(&write_request.encode_to_vec())
|
|
.expect("failed to encode snappy");
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.header(
|
|
"Content-Type",
|
|
"application/x-protobuf;proto=io.prometheus.write.v2.Request",
|
|
)
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
wait_for_data(
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name = 'remote_write_batched_typed_total' \
|
|
and create_options like '%greptime.semantic.metric.type=gauge%' \
|
|
and create_options like '%greptime.semantic.metric.unit=By%' \
|
|
and create_options like '%greptime.semantic.metric.metadata_quality=declared%'",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_prometheus_remote_special_labels(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_prom_app_with_frontend(store_type, "test_prometheus_remote_special_labels")
|
|
.await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// write snappy encoded data
|
|
let write_request = WriteRequest {
|
|
timeseries: prom_store::mock_timeseries_special_labels(),
|
|
..Default::default()
|
|
};
|
|
let serialized_request = write_request.encode_to_vec();
|
|
let compressed_request =
|
|
prom_store::snappy_compress(&serialized_request).expect("failed to encode snappy");
|
|
|
|
// create databases
|
|
let res = client
|
|
.post("/v1/sql?sql=create database idc3")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let res = client
|
|
.post("/v1/sql?sql=create database idc4")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// write data
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
// test idc3
|
|
let expected = "[[\"f1\"],[\"idc3_lo_table\"]]";
|
|
validate_data(
|
|
"test_prometheus_remote_special_labels_idc3",
|
|
&client,
|
|
"show tables from idc3;",
|
|
expected,
|
|
)
|
|
.await;
|
|
let expected = "[[\"idc3_lo_table\",\"CREATE TABLE IF NOT EXISTS \\\"idc3_lo_table\\\" (\\n \\\"greptime_timestamp\\\" TIMESTAMP(3) NOT NULL,\\n \\\"greptime_value\\\" DOUBLE NULL,\\n TIME INDEX (\\\"greptime_timestamp\\\")\\n)\\n\\nENGINE=metric\\nWITH(\\n \'comment\' = 'Created on insertion',\\n 'greptime.semantic.metric.metadata_quality' = 'inferred',\\n 'greptime.semantic.signal_type' = 'metric',\\n 'greptime.semantic.source' = 'prometheus',\\n 'greptime.semantic.source_version' = '1.0',\\n on_physical_table = 'f1'\\n)\"]]";
|
|
validate_data(
|
|
"test_prometheus_remote_special_labels_idc3_show_create_table",
|
|
&client,
|
|
"show create table idc3.idc3_lo_table",
|
|
expected,
|
|
)
|
|
.await;
|
|
let expected = "[[3000,42.0]]";
|
|
validate_data(
|
|
"test_prometheus_remote_special_labels_idc3_select",
|
|
&client,
|
|
"select * from idc3.idc3_lo_table",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// test idc4
|
|
let expected = "[[\"f2\"],[\"idc4_local_table\"]]";
|
|
validate_data(
|
|
"test_prometheus_remote_special_labels_idc4",
|
|
&client,
|
|
"show tables from idc4;",
|
|
expected,
|
|
)
|
|
.await;
|
|
let expected = "[[\"idc4_local_table\",\"CREATE TABLE IF NOT EXISTS \\\"idc4_local_table\\\" (\\n \\\"greptime_timestamp\\\" TIMESTAMP(3) NOT NULL,\\n \\\"greptime_value\\\" DOUBLE NULL,\\n TIME INDEX (\\\"greptime_timestamp\\\")\\n)\\n\\nENGINE=metric\\nWITH(\\n \'comment\' = 'Created on insertion',\\n 'greptime.semantic.metric.metadata_quality' = 'inferred',\\n 'greptime.semantic.signal_type' = 'metric',\\n 'greptime.semantic.source' = 'prometheus',\\n 'greptime.semantic.source_version' = '1.0',\\n on_physical_table = 'f2'\\n)\"]]";
|
|
validate_data(
|
|
"test_prometheus_remote_special_labels_idc4_show_create_table",
|
|
&client,
|
|
"show create table idc4.idc4_local_table",
|
|
expected,
|
|
)
|
|
.await;
|
|
let expected = "[[4000,99.0]]";
|
|
validate_data(
|
|
"test_prometheus_remote_special_labels_idc4_select",
|
|
&client,
|
|
"select * from idc4.idc4_local_table",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_prometheus_remote_write_with_pipeline(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_prom_app_with_frontend(store_type, "prometheus_remote_write_with_pipeline")
|
|
.await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// write snappy encoded data
|
|
let write_request = WriteRequest {
|
|
timeseries: prom_store::mock_timeseries(),
|
|
..Default::default()
|
|
};
|
|
let serialized_request = write_request.encode_to_vec();
|
|
let compressed_request =
|
|
prom_store::snappy_compress(&serialized_request).expect("failed to encode snappy");
|
|
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.header("x-greptime-log-pipeline-name", "greptime_identity")
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
let expected = "[[\"demo\"],[\"demo_metrics\"],[\"demo_metrics_with_nanos\"],[\"greptime_physical_table\"],[\"metric1\"],[\"metric2\"],[\"metric3\"],[\"mito\"],[\"multi_labels\"],[\"numbers\"],[\"phy\"],[\"phy2\"],[\"phy_ns\"]]";
|
|
validate_data(
|
|
"prometheus_remote_write_pipeline",
|
|
&client,
|
|
"show tables;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
let table_val = "[[1000,3.0,\"z001\",\"test_host1\"],[2000,4.0,\"z001\",\"test_host1\"]]";
|
|
validate_data(
|
|
"prometheus_remote_write_pipeline",
|
|
&client,
|
|
"select * from metric2",
|
|
table_val,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
fn schema_series(schema: &str, value: f64) -> TimeSeries {
|
|
TimeSeries {
|
|
labels: vec![
|
|
Label {
|
|
name: "__name__".to_string(),
|
|
value: "schema_metric".to_string(),
|
|
},
|
|
Label {
|
|
name: "__schema__".to_string(),
|
|
value: schema.to_string(),
|
|
},
|
|
Label {
|
|
name: "instance".to_string(),
|
|
value: "host1".to_string(),
|
|
},
|
|
],
|
|
samples: vec![Sample {
|
|
value,
|
|
timestamp: 1000,
|
|
}],
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
fn schema_query(schema: Option<&str>) -> Query {
|
|
let mut matchers = vec![LabelMatcher {
|
|
name: "__name__".to_string(),
|
|
value: "schema_metric".to_string(),
|
|
r#type: MatcherType::Eq as i32,
|
|
}];
|
|
if let Some(schema) = schema {
|
|
matchers.push(LabelMatcher {
|
|
name: "__schema__".to_string(),
|
|
value: schema.to_string(),
|
|
r#type: MatcherType::Eq as i32,
|
|
});
|
|
}
|
|
|
|
Query {
|
|
start_timestamp_ms: 500,
|
|
end_timestamp_ms: 1500,
|
|
matchers,
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
async fn post_remote_read(client: &TestClient, queries: Vec<Query>) -> ReadResponse {
|
|
let read_request = ReadRequest {
|
|
queries,
|
|
..Default::default()
|
|
};
|
|
let compressed_request = prom_store::snappy_compress(&read_request.encode_to_vec())
|
|
.expect("failed to encode snappy");
|
|
|
|
let response = client
|
|
.post("/v1/prometheus/read")
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
let response_body = response.bytes().await;
|
|
let decompressed_response = prom_store::snappy_decompress(&response_body).unwrap();
|
|
ReadResponse::decode(decompressed_response.as_slice()).unwrap()
|
|
}
|
|
|
|
async fn post_remote_read_from_schema_a(client: &TestClient, queries: Vec<Query>) -> ReadResponse {
|
|
let read_request = ReadRequest {
|
|
queries,
|
|
..Default::default()
|
|
};
|
|
let compressed_request = prom_store::snappy_compress(&read_request.encode_to_vec())
|
|
.expect("failed to encode snappy");
|
|
|
|
let response = client
|
|
.post("/v1/prometheus/read")
|
|
.header(GREPTIME_DB_HEADER_NAME.clone(), "schema_a")
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
let response_body = response.bytes().await;
|
|
let decompressed_response = prom_store::snappy_decompress(&response_body).unwrap();
|
|
ReadResponse::decode(decompressed_response.as_slice()).unwrap()
|
|
}
|
|
|
|
fn read_sample_values(read_response: ReadResponse) -> Vec<f64> {
|
|
read_response
|
|
.results
|
|
.into_iter()
|
|
.map(|result| {
|
|
assert_eq!(result.timeseries.len(), 1);
|
|
let timeseries = &result.timeseries[0];
|
|
assert_eq!(timeseries.samples.len(), 1);
|
|
assert_eq!(timeseries.samples[0].timestamp, 1000);
|
|
timeseries.samples[0].value
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
pub async fn test_prometheus_remote_schema_labels(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_prom_app_with_frontend(store_type, "test_prometheus_remote_schema_labels").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
const SCHEMA_A: &str = "schema_a";
|
|
const SCHEMA_B: &str = "schema_b";
|
|
|
|
for schema in [SCHEMA_A, SCHEMA_B] {
|
|
let res = client
|
|
.post(&format!("/v1/sql?sql=create database {schema}"))
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
}
|
|
|
|
// Both series have the same metric, non-schema labels, and timestamp.
|
|
let write_request = WriteRequest {
|
|
timeseries: vec![
|
|
schema_series(SCHEMA_A, 101.0),
|
|
schema_series(SCHEMA_B, 202.0),
|
|
],
|
|
..Default::default()
|
|
};
|
|
let compressed_request = prom_store::snappy_compress(&write_request.encode_to_vec())
|
|
.expect("failed to encode snappy");
|
|
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
// Independent controls establish that each selector resolves its own schema.
|
|
assert_eq!(
|
|
read_sample_values(post_remote_read(&client, vec![schema_query(Some(SCHEMA_A))]).await,),
|
|
vec![101.0]
|
|
);
|
|
assert_eq!(
|
|
read_sample_values(post_remote_read(&client, vec![schema_query(Some(SCHEMA_B))]).await,),
|
|
vec![202.0]
|
|
);
|
|
|
|
// Results must stay aligned with the request query order.
|
|
let forward_values = read_sample_values(
|
|
post_remote_read(
|
|
&client,
|
|
vec![schema_query(Some(SCHEMA_A)), schema_query(Some(SCHEMA_B))],
|
|
)
|
|
.await,
|
|
);
|
|
let reverse_values = read_sample_values(
|
|
post_remote_read(
|
|
&client,
|
|
vec![schema_query(Some(SCHEMA_B)), schema_query(Some(SCHEMA_A))],
|
|
)
|
|
.await,
|
|
);
|
|
assert_eq!(
|
|
(forward_values, reverse_values),
|
|
(vec![101.0, 202.0], vec![202.0, 101.0])
|
|
);
|
|
|
|
// With request database A, unqualified queries must resolve to A independently.
|
|
let explicit_then_default_values = read_sample_values(
|
|
post_remote_read_from_schema_a(
|
|
&client,
|
|
vec![schema_query(Some(SCHEMA_B)), schema_query(None)],
|
|
)
|
|
.await,
|
|
);
|
|
let default_then_explicit_values = read_sample_values(
|
|
post_remote_read_from_schema_a(
|
|
&client,
|
|
vec![schema_query(None), schema_query(Some(SCHEMA_B))],
|
|
)
|
|
.await,
|
|
);
|
|
assert_eq!(
|
|
(explicit_then_default_values, default_then_explicit_values),
|
|
(vec![202.0, 101.0], vec![101.0, 202.0])
|
|
);
|
|
|
|
// write data to unknown schema
|
|
let unknown_schema_series = TimeSeries {
|
|
labels: vec![
|
|
Label {
|
|
name: "__name__".to_string(),
|
|
value: "metric_unknown_schema".to_string(),
|
|
},
|
|
Label {
|
|
name: "__schema__".to_string(),
|
|
value: "unknown_schema".to_string(),
|
|
},
|
|
Label {
|
|
name: "instance".to_string(),
|
|
value: "host2".to_string(),
|
|
},
|
|
],
|
|
samples: vec![Sample {
|
|
value: 200.0,
|
|
timestamp: 2000,
|
|
}],
|
|
..Default::default()
|
|
};
|
|
|
|
let unknown_write_request = WriteRequest {
|
|
timeseries: vec![unknown_schema_series],
|
|
..Default::default()
|
|
};
|
|
let serialized_unknown_request = unknown_write_request.encode_to_vec();
|
|
let compressed_unknown_request =
|
|
prom_store::snappy_compress(&serialized_unknown_request).expect("failed to encode snappy");
|
|
|
|
// Write data to unknown schema
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(compressed_unknown_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
|
|
// Read data from unknown schema
|
|
let unknown_read_request = ReadRequest {
|
|
queries: vec![Query {
|
|
start_timestamp_ms: 1500,
|
|
end_timestamp_ms: 2500,
|
|
matchers: vec![
|
|
LabelMatcher {
|
|
name: "__name__".to_string(),
|
|
value: "metric_unknown_schema".to_string(),
|
|
r#type: MatcherType::Eq as i32,
|
|
},
|
|
LabelMatcher {
|
|
name: "__schema__".to_string(),
|
|
value: "unknown_schema".to_string(),
|
|
r#type: MatcherType::Eq as i32,
|
|
},
|
|
],
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
};
|
|
|
|
let serialized_unknown_read_request = unknown_read_request.encode_to_vec();
|
|
let compressed_unknown_read_request =
|
|
prom_store::snappy_compress(&serialized_unknown_read_request)
|
|
.expect("failed to encode snappy");
|
|
|
|
let unknown_result = client
|
|
.post("/v1/prometheus/read")
|
|
.body(compressed_unknown_read_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(unknown_result.status(), StatusCode::OK);
|
|
let body = prom_store::snappy_decompress(&unknown_result.bytes().await).unwrap();
|
|
let response = ReadResponse::decode(&body[..]).unwrap();
|
|
assert_eq!(response.results.len(), 1);
|
|
assert!(response.results[0].timeseries.is_empty());
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_vm_proto_remote_write(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_prom_app_with_frontend(store_type, "vm_proto_remote_write").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
let res = client
|
|
.post("/v1/prometheus/write?get_vm_proto_version=1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
assert_eq!(res.text().await, "1");
|
|
|
|
// write zstd encoded data
|
|
let write_request = WriteRequest {
|
|
timeseries: prom_store::mock_timeseries(),
|
|
..Default::default()
|
|
};
|
|
let serialized_request = write_request.encode_to_vec();
|
|
let compressed_request =
|
|
zstd::stream::encode_all(&serialized_request[..], 1).expect("Failed to encode zstd");
|
|
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "zstd")
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
// also test fallback logic, vmagent could sent data in wrong content-type
|
|
// we encode it as zstd but send it as snappy
|
|
let compressed_request =
|
|
zstd::stream::encode_all(&serialized_request[..], 1).expect("Failed to encode zstd");
|
|
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "snappy")
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
// reversed
|
|
let compressed_request =
|
|
prom_store::snappy_compress(&serialized_request[..]).expect("Failed to encode snappy");
|
|
|
|
let res = client
|
|
.post("/v1/prometheus/write")
|
|
.header("Content-Encoding", "zstd")
|
|
.body(compressed_request)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::NO_CONTENT);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_pipeline_api(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_http_app_with_frontend(store_type, "test_pipeline_api").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
let pipeline_body = r#"
|
|
processors:
|
|
- date:
|
|
field: time
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
ignore_missing: true
|
|
|
|
transform:
|
|
- fields:
|
|
- id1
|
|
- id2
|
|
type: int32
|
|
index: inverted
|
|
- fields:
|
|
- logger
|
|
type: string
|
|
- field: type
|
|
type: string
|
|
index: skipping
|
|
tag: true
|
|
- field: log
|
|
type: string
|
|
index: fulltext
|
|
tag: true
|
|
- field: time
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/pipelines/greptime_guagua")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline_body)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
assert_eq!(
|
|
res.json::<serde_json::Value>().await["error"]
|
|
.as_str()
|
|
.unwrap(),
|
|
"Invalid request parameter: pipeline_name cannot start with greptime_"
|
|
);
|
|
|
|
let res = client
|
|
.post("/v1/pipelines/test")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline_body)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let content = res.text().await;
|
|
|
|
let content = serde_json::from_str(&content);
|
|
assert!(content.is_ok());
|
|
// {"execution_time_ms":13,"pipelines":[{"name":"test","version":"2024-07-04 08:31:00.987136"}]}
|
|
let content: Value = content.unwrap();
|
|
|
|
let execution_time = content.get("execution_time_ms");
|
|
assert!(execution_time.unwrap().is_number());
|
|
let pipelines = content.get("pipelines");
|
|
let pipelines = pipelines.unwrap().as_array().unwrap();
|
|
assert_eq!(pipelines.len(), 1);
|
|
let pipeline = pipelines.first().unwrap();
|
|
assert_eq!(pipeline.get("name").unwrap(), "test");
|
|
|
|
let version_str = pipeline
|
|
.get("version")
|
|
.unwrap()
|
|
.as_str()
|
|
.unwrap()
|
|
.to_string();
|
|
let encoded_ver_str: String =
|
|
url::form_urlencoded::byte_serialize(version_str.as_bytes()).collect();
|
|
|
|
// get pipeline
|
|
let res = client
|
|
.get(format!("/v1/pipelines/test?version={}", encoded_ver_str).as_str())
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let content = res.text().await;
|
|
let content = serde_json::from_str(&content);
|
|
let content: Value = content.unwrap();
|
|
let pipeline_yaml = content
|
|
.get("pipelines")
|
|
.unwrap()
|
|
.as_array()
|
|
.unwrap()
|
|
.first()
|
|
.unwrap()
|
|
.get("pipeline")
|
|
.unwrap()
|
|
.as_str()
|
|
.unwrap();
|
|
let docs = YamlLoader::load_from_str(pipeline_yaml).unwrap();
|
|
let body_yaml = YamlLoader::load_from_str(pipeline_body).unwrap();
|
|
assert_eq!(docs, body_yaml);
|
|
|
|
// Do not specify version, get the latest version
|
|
let res = client.get("/v1/pipelines/test").send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let content = res.text().await;
|
|
let content = serde_json::from_str(&content);
|
|
let content: Value = content.unwrap();
|
|
let pipeline_yaml = content
|
|
.get("pipelines")
|
|
.unwrap()
|
|
.as_array()
|
|
.unwrap()
|
|
.first()
|
|
.unwrap()
|
|
.get("pipeline")
|
|
.unwrap()
|
|
.as_str()
|
|
.unwrap();
|
|
let docs = YamlLoader::load_from_str(pipeline_yaml).unwrap();
|
|
assert_eq!(docs, body_yaml);
|
|
|
|
// 2. write data
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "I",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster\\n"
|
|
}
|
|
]
|
|
"#;
|
|
let res = client
|
|
.post("/v1/ingest?db=public&table=logs1&pipeline_name=test")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 3. check schema
|
|
let expected_schema = "[[\"logs1\",\"CREATE TABLE IF NOT EXISTS \\\"logs1\\\" (\\n \\\"id1\\\" INT NULL INVERTED INDEX,\\n \\\"id2\\\" INT NULL INVERTED INDEX,\\n \\\"logger\\\" STRING NULL,\\n \\\"type\\\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM'),\\n \\\"log\\\" STRING NULL FULLTEXT INDEX WITH(analyzer = 'English', backend = 'bloom', case_sensitive = 'false', false_positive_rate = '0.01', granularity = '10240'),\\n \\\"time\\\" TIMESTAMP(9) NOT NULL,\\n TIME INDEX (\\\"time\\\"),\\n PRIMARY KEY (\\\"type\\\", \\\"log\\\")\\n)\\n\\nENGINE=mito\\nWITH(\\n \'comment\' = 'Created on insertion',\\n append_mode = 'true'\\n)\"]]";
|
|
validate_data(
|
|
"pipeline_schema",
|
|
&client,
|
|
"show create table logs1",
|
|
expected_schema,
|
|
)
|
|
.await;
|
|
|
|
// 4. cross-ref pipeline
|
|
// create database test_db
|
|
let res = client
|
|
.post("/v1/sql?sql=create database test_db")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// check test_db created
|
|
validate_data(
|
|
"pipeline_db_schema",
|
|
&client,
|
|
"show databases",
|
|
"[[\"greptime_private\"],[\"information_schema\"],[\"public\"],[\"test_db\"]]",
|
|
)
|
|
.await;
|
|
|
|
// cross ref using public's pipeline
|
|
let res = client
|
|
.post("/v1/ingest?db=test_db&table=logs1&pipeline_name=test")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// check write success
|
|
validate_data(
|
|
"pipeline_db_schema",
|
|
&client,
|
|
"select * from test_db.logs1",
|
|
"[[2436,2528,\"INTERACT.MANAGER\",\"I\",\"ClusterAdapter:enter sendTextDataToCluster\\\\n\",1716668197217000000]]",
|
|
)
|
|
.await;
|
|
|
|
// The inverted, skipping and fulltext index files are only written on flush,
|
|
// so the queries below are the first thing that reads them back.
|
|
flush_table(&client, "test_db.logs1").await;
|
|
validate_data(
|
|
"pipeline_db_inverted_index",
|
|
&client,
|
|
"select id2 from test_db.logs1 where id1 = 2436",
|
|
"[[2528]]",
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"pipeline_db_skipping_index",
|
|
&client,
|
|
"select id1 from test_db.logs1 where type = 'I'",
|
|
"[[2436]]",
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"pipeline_db_fulltext_index",
|
|
&client,
|
|
"select id1 from test_db.logs1 where matches(log, 'ClusterAdapter')",
|
|
"[[2436]]",
|
|
)
|
|
.await;
|
|
|
|
// 5. remove pipeline
|
|
let encoded_ver_str: String =
|
|
url::form_urlencoded::byte_serialize(version_str.as_bytes()).collect();
|
|
let res = client
|
|
.delete(format!("/v1/pipelines/test?version={}", encoded_ver_str).as_str())
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// {"pipelines":[{"name":"test","version":"2024-07-04 08:55:29.038347"}],"execution_time_ms":22}
|
|
let content = res.text().await;
|
|
let content: Value = serde_json::from_str(&content).unwrap();
|
|
assert!(content.get("execution_time_ms").unwrap().is_number());
|
|
|
|
assert_eq!(
|
|
content.get("pipelines").unwrap().to_string(),
|
|
format!(r#"[{{"name":"test","version":"{}"}}]"#, version_str).as_str()
|
|
);
|
|
|
|
// 6. write data failed
|
|
let res = client
|
|
.post("/v1/ingest?db=public&table=logs1&pipeline_name=test")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
// todo(shuiyisong): refactor http error handling
|
|
assert_ne!(res.status(), StatusCode::OK);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_pipeline_name_in_header(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_pipeline_name_in_header").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
let pipeline_body = r#"
|
|
processors:
|
|
- date:
|
|
field: time
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
ignore_missing: true
|
|
transform:
|
|
- field: message
|
|
type: string
|
|
- field: time
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
|
|
// create pipeline named `test`
|
|
let res = client
|
|
.post("/v1/events/pipelines/test")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"message": "hello header"
|
|
}
|
|
]
|
|
"#;
|
|
|
|
// 1. pipeline name provided via the `x-greptime-pipeline-name` header,
|
|
// without the `pipeline_name` query parameter.
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=logs1")
|
|
.header("Content-Type", "application/json")
|
|
.header("x-greptime-pipeline-name", "test")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
validate_data(
|
|
"pipeline_name_header",
|
|
&client,
|
|
"select message from logs1",
|
|
"[[\"hello header\"]]",
|
|
)
|
|
.await;
|
|
|
|
// 2. the deprecated `x-greptime-log-pipeline-name` header is also accepted.
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=logs2")
|
|
.header("Content-Type", "application/json")
|
|
.header("x-greptime-log-pipeline-name", "test")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
validate_data(
|
|
"pipeline_name_log_header",
|
|
&client,
|
|
"select message from logs2",
|
|
"[[\"hello header\"]]",
|
|
)
|
|
.await;
|
|
|
|
// 3. the header takes precedence over the query parameter. A bogus
|
|
// `pipeline_name` query param is overridden by the valid header.
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=logs3&pipeline_name=does_not_exist")
|
|
.header("Content-Type", "application/json")
|
|
.header("x-greptime-pipeline-name", "test")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
validate_data(
|
|
"pipeline_name_header_priority",
|
|
&client,
|
|
"select message from logs3",
|
|
"[[\"hello header\"]]",
|
|
)
|
|
.await;
|
|
|
|
// 4. when both headers are present, the non-deprecated
|
|
// `x-greptime-pipeline-name` wins over the deprecated
|
|
// `x-greptime-log-pipeline-name`.
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=logs4")
|
|
.header("Content-Type", "application/json")
|
|
.header("x-greptime-pipeline-name", "test")
|
|
.header("x-greptime-log-pipeline-name", "does_not_exist")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
validate_data(
|
|
"pipeline_name_header_non_deprecated_priority",
|
|
&client,
|
|
"select message from logs4",
|
|
"[[\"hello header\"]]",
|
|
)
|
|
.await;
|
|
|
|
// 5. missing pipeline name in both header and query param is rejected.
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=logs5")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
#[apply(both_deployment_cases)]
|
|
async fn test_json_logs_skip_wal(distributed: bool) {
|
|
let cases = vec![
|
|
HttpWalCase::new(
|
|
"JSON logs",
|
|
"/v1/ingest?table=wal_logs&pipeline_name=greptime_identity",
|
|
"application/json",
|
|
r#"[{"message":"wal test"}]"#,
|
|
),
|
|
HttpWalCase::new(
|
|
"Events logs",
|
|
"/v1/events/logs?table=wal_events&pipeline_name=greptime_identity",
|
|
"application/json",
|
|
r#"[{"message":"wal test"}]"#,
|
|
),
|
|
];
|
|
check_http_skip_wal("json_logs", &cases, distributed).await;
|
|
}
|
|
|
|
pub async fn test_identity_pipeline(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_identity_pipeline").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
let body = r#"{"__time__":1453809242,"__topic__":"","__source__":"10.170.***.***","ip":"10.200.**.***","time":"26/Jan/2016:19:54:02 +0800","url":"POST/PutData?Category=YunOsAccountOpLog&AccessKeyId=<yourAccessKeyId>&Date=Fri%2C%2028%20Jun%202013%2006%3A53%3A30%20GMT&Topic=raw&Signature=<yourSignature>HTTP/1.1","status":"200","user-agent":"aliyun-sdk-java", "json_object": {"a":1,"b":2}, "json_array":[1,2,3]}
|
|
{"__time__":1453809242,"__topic__":"","__source__":"10.170.***.***","ip":"10.200.**.***","time":"26/Jan/2016:19:54:02 +0800","url":"POST/PutData?Category=YunOsAccountOpLog&AccessKeyId=<yourAccessKeyId>&Date=Fri%2C%2028%20Jun%202013%2006%3A53%3A30%20GMT&Topic=raw&Signature=<yourSignature>HTTP/1.1","status":"200","user-agent":"aliyun-sdk-java","hasagei":"hasagei","dongdongdong":"guaguagua", "json_object": {"a":1,"b":2}, "json_array":[1,2,3]}"#;
|
|
let res = client
|
|
.post("/v1/ingest?db=public&table=logs&pipeline_name=greptime_identity")
|
|
.header("Content-Type", "application/json")
|
|
.body(body)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let body: serde_json::Value = res.json().await;
|
|
|
|
assert!(body.get("execution_time_ms").unwrap().is_number());
|
|
assert_eq!(body["output"][0]["affectedrows"], 2);
|
|
|
|
let res = client.get("/v1/sql?sql=select * from logs").send().await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let line1_expected = r#"[null,"10.170.***.***",1453809242,"","10.200.**.***","[1,2,3]",1,2,"200","26/Jan/2016:19:54:02 +0800","POST/PutData?Category=YunOsAccountOpLog&AccessKeyId=<yourAccessKeyId>&Date=Fri%2C%2028%20Jun%202013%2006%3A53%3A30%20GMT&Topic=raw&Signature=<yourSignature>HTTP/1.1","aliyun-sdk-java",null,null]"#;
|
|
let line2_expected = r#"[null,"10.170.***.***",1453809242,"","10.200.**.***","[1,2,3]",1,2,"200","26/Jan/2016:19:54:02 +0800","POST/PutData?Category=YunOsAccountOpLog&AccessKeyId=<yourAccessKeyId>&Date=Fri%2C%2028%20Jun%202013%2006%3A53%3A30%20GMT&Topic=raw&Signature=<yourSignature>HTTP/1.1","aliyun-sdk-java","guaguagua","hasagei"]"#;
|
|
let res = client.get("/v1/sql?sql=select * from logs").send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let resp: serde_json::Value = res.json().await;
|
|
let result = resp["output"][0]["records"]["rows"].as_array().unwrap();
|
|
assert_eq!(result.len(), 2);
|
|
let mut line1 = result[0].as_array().unwrap().clone();
|
|
let mut line2 = result[1].as_array().unwrap().clone();
|
|
assert!(line1.first().unwrap().is_i64());
|
|
assert!(line2.first().unwrap().is_i64());
|
|
// set time index to null for assertion
|
|
*line1.first_mut().unwrap() = serde_json::Value::Null;
|
|
*line2.first_mut().unwrap() = serde_json::Value::Null;
|
|
|
|
assert_eq!(
|
|
line1,
|
|
serde_json::from_str::<Vec<Value>>(line1_expected).unwrap()
|
|
);
|
|
assert_eq!(
|
|
line2,
|
|
serde_json::from_str::<Vec<Value>>(line2_expected).unwrap()
|
|
);
|
|
|
|
let expected = r#"[["greptime_timestamp","TimestampNanosecond","PRI","NO","","TIMESTAMP"],["__source__","String","","YES","","FIELD"],["__time__","Int64","","YES","","FIELD"],["__topic__","String","","YES","","FIELD"],["ip","String","","YES","","FIELD"],["json_array","String","","YES","","FIELD"],["json_object.a","Int64","","YES","","FIELD"],["json_object.b","Int64","","YES","","FIELD"],["status","String","","YES","","FIELD"],["time","String","","YES","","FIELD"],["url","String","","YES","","FIELD"],["user-agent","String","","YES","","FIELD"],["dongdongdong","String","","YES","","FIELD"],["hasagei","String","","YES","","FIELD"]]"#;
|
|
validate_data("identity_schema", &client, "desc logs", expected).await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
/// Test for identity pipeline with null values for new columns.
|
|
/// This test verifies that null values for columns not yet in the schema
|
|
/// are handled correctly - they should NOT push to the row without updating schema.
|
|
/// Regression test for: https://github.com/GreptimeTeam/greptimedb/issues/7654
|
|
///
|
|
/// The bug was that when a null value appeared for a column not in schema:
|
|
/// 1. The schema was not updated (correct)
|
|
/// 2. But row.push() was still called (BUG!)
|
|
///
|
|
/// This caused row.len() > schema.len(), leading to integer underflow in the
|
|
/// padding loop: `let diff = column_count - row.values.len()` where column_count < row.values.len()
|
|
/// causes usize underflow, making the loop try to push billions of elements → OOM.
|
|
///
|
|
/// To trigger this bug:
|
|
/// - Row 1 must have ONLY null values for NEW columns (pushes to row without schema update)
|
|
/// - Row 2 must have FEWER new non-null columns than Row 1's null columns
|
|
///
|
|
/// Example that triggers the bug:
|
|
/// - Row 1: {"a": null, "b": null} → row has [ts, null, null] (3 elements), schema has [ts] (1 col)
|
|
/// - Row 2: {"c": "value"} → row has [ts, "value"] (2 elements), schema has [ts, c] (2 cols)
|
|
/// - Padding: column_count=2, Row1.len()=3 → diff = 2-3 = underflow!
|
|
///
|
|
/// This test also covers resolve_value function branches:
|
|
/// - VrlValue::Null (null values)
|
|
/// - VrlValue::Integer (integer values)
|
|
/// - VrlValue::Float (floating point values)
|
|
/// - VrlValue::Boolean (boolean values)
|
|
/// - VrlValue::Bytes (string values)
|
|
///
|
|
/// Note: VrlValue::Array and VrlValue::Object branches are NOT covered because
|
|
/// the identity pipeline flattens nested objects and stringifies arrays before
|
|
/// resolve_value is called. Testing those branches would require a custom pipeline.
|
|
pub async fn test_identity_pipeline_with_null_column(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_identity_pipeline_with_null_column")
|
|
.await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
// Row 1 has 2 null values for new columns (a, b) - these push to row but don't add to schema
|
|
// Row 2 has 1 non-null value for new column (c) - this adds to schema
|
|
// Result: Row 1 has 3 elements [ts, null, null], schema has 2 columns [ts, c]
|
|
// The padding loop: diff = 2 - 3 = usize underflow → OOM
|
|
let body = r#"{"a":null,"b":null}
|
|
{"c":"value"}"#;
|
|
|
|
let res = client
|
|
.post("/v1/ingest?db=public&table=null_test&pipeline_name=greptime_identity")
|
|
.header("Content-Type", "application/json")
|
|
.body(body)
|
|
.send()
|
|
.await;
|
|
|
|
// The buggy code would OOM or panic here due to the integer underflow causing
|
|
// billions of push operations. If we get here, the bug is fixed.
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let body: serde_json::Value = res.json().await;
|
|
assert_eq!(body["output"][0]["affectedrows"], 2);
|
|
|
|
// Verify the schema only contains columns that had non-null values
|
|
// a and b should NOT be in the schema since they only had null values
|
|
let expected = r#"[["greptime_timestamp","TimestampNanosecond","PRI","NO","","TIMESTAMP"],["c","String","","YES","","FIELD"]]"#;
|
|
validate_data(
|
|
"test_identity_pipeline_null_column_schema",
|
|
&client,
|
|
"desc null_test",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// Verify the data is correct
|
|
let res = client
|
|
.get(
|
|
"/v1/sql?sql=select c from null_test order by case when c is null then 0 else 1 end, c",
|
|
)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let resp: serde_json::Value = res.json().await;
|
|
let rows = resp["output"][0]["records"]["rows"].as_array().unwrap();
|
|
assert_eq!(rows.len(), 2);
|
|
|
|
// Row 1 has null for c (column didn't exist when row 1 was processed)
|
|
assert!(rows[0][0].is_null());
|
|
|
|
// Row 2 has "value" for c
|
|
assert_eq!(rows[1][0], "value");
|
|
|
|
// Test resolve_value function with various data types
|
|
// Covers: VrlValue::Null, Integer, Float, Boolean, Bytes (string)
|
|
// Note: Array and Object are flattened/stringified by identity pipeline before resolve_value
|
|
let body_all_types = r#"{"null_col":null,"int_col":42,"float_col":3.10,"bool_col":true,"str_col":"hello","array_col":[1,2,3],"obj_col":{"nested":"value"}}"#;
|
|
|
|
let res = client
|
|
.post("/v1/ingest?db=public&table=all_types_test&pipeline_name=greptime_identity")
|
|
.header("Content-Type", "application/json")
|
|
.body(body_all_types)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let body: serde_json::Value = res.json().await;
|
|
assert_eq!(body["output"][0]["affectedrows"], 1);
|
|
|
|
// Verify the schema contains all non-null column types
|
|
// null_col should NOT be in schema since it only has null value
|
|
// Note: arrays are converted to strings, nested objects are flattened (obj_col.nested)
|
|
let expected = r#"[["greptime_timestamp","TimestampNanosecond","PRI","NO","","TIMESTAMP"],["array_col","String","","YES","","FIELD"],["bool_col","Boolean","","YES","","FIELD"],["float_col","Float64","","YES","","FIELD"],["int_col","Int64","","YES","","FIELD"],["obj_col.nested","String","","YES","","FIELD"],["str_col","String","","YES","","FIELD"]]"#;
|
|
validate_data(
|
|
"test_identity_pipeline_all_types_schema",
|
|
&client,
|
|
"desc all_types_test",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// Verify the data values are correct
|
|
let res = client
|
|
.get("/v1/sql?sql=select int_col, float_col, bool_col, str_col from all_types_test")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let resp: serde_json::Value = res.json().await;
|
|
let rows = resp["output"][0]["records"]["rows"].as_array().unwrap();
|
|
assert_eq!(rows.len(), 1);
|
|
assert_eq!(rows[0][0], 42); // int_col
|
|
assert_eq!(rows[0][1], 3.10); // float_col
|
|
assert_eq!(rows[0][2], true); // bool_col
|
|
assert_eq!(rows[0][3], "hello"); // str_col
|
|
|
|
// Test with multiple rows containing different combinations of types
|
|
// This ensures the schema evolution works correctly across rows
|
|
let body_multi_rows = r#"{"int_val":100,"str_val":"first"}
|
|
{"int_val":200,"float_val":2.5,"bool_val":false}
|
|
{"str_val":"third","array_val":[4,5,6],"nested_obj":{"key":"val"}}"#;
|
|
|
|
let res = client
|
|
.post("/v1/ingest?db=public&table=multi_types_test&pipeline_name=greptime_identity")
|
|
.header("Content-Type", "application/json")
|
|
.body(body_multi_rows)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let body: serde_json::Value = res.json().await;
|
|
assert_eq!(body["output"][0]["affectedrows"], 3);
|
|
|
|
// Verify all columns from all rows are in the schema
|
|
// Note: arrays are converted to strings, nested objects are flattened (nested_obj.key)
|
|
// Columns appear in order they were first encountered: Row1(int_val,str_val), Row2(float_val,bool_val), Row3(array_val,nested_obj.key)
|
|
let expected = r#"[["greptime_timestamp","TimestampNanosecond","PRI","NO","","TIMESTAMP"],["int_val","Int64","","YES","","FIELD"],["str_val","String","","YES","","FIELD"],["bool_val","Boolean","","YES","","FIELD"],["float_val","Float64","","YES","","FIELD"],["array_val","String","","YES","","FIELD"],["nested_obj.key","String","","YES","","FIELD"]]"#;
|
|
validate_data(
|
|
"test_identity_pipeline_multi_types_schema",
|
|
&client,
|
|
"desc multi_types_test",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// Verify data with nulls for columns not present in each row
|
|
// ORDER BY with explicit NULLS LAST
|
|
let res = client
|
|
.get("/v1/sql?sql=select int_val, str_val, float_val, bool_val from multi_types_test order by case when int_val is null then 1 else 0 end, int_val")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let resp: serde_json::Value = res.json().await;
|
|
let rows = resp["output"][0]["records"]["rows"].as_array().unwrap();
|
|
assert_eq!(rows.len(), 3);
|
|
|
|
// Row 1: int_val=100, str_val="first", float_val=null, bool_val=null
|
|
assert_eq!(rows[0][0], 100);
|
|
assert_eq!(rows[0][1], "first");
|
|
assert!(rows[0][2].is_null());
|
|
assert!(rows[0][3].is_null());
|
|
|
|
// Row 2: int_val=200, str_val=null, float_val=2.5, bool_val=false
|
|
assert_eq!(rows[1][0], 200);
|
|
assert!(rows[1][1].is_null());
|
|
assert_eq!(rows[1][2], 2.5);
|
|
assert_eq!(rows[1][3], false);
|
|
|
|
// Row 3: int_val=null, str_val="third", float_val=null, bool_val=null (nulls sorted last)
|
|
assert!(rows[2][0].is_null());
|
|
assert_eq!(rows[2][1], "third");
|
|
assert!(rows[2][2].is_null());
|
|
assert!(rows[2][3].is_null());
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_pipeline_skip_error(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_pipeline_skip_error").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
let pipeline_body = r#"
|
|
processors:
|
|
- date:
|
|
field: time
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
ignore_missing: true
|
|
transform:
|
|
- field: message
|
|
type: string
|
|
- field: time
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/events/pipelines/test")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
// 2. write data
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"message": "good message"
|
|
},
|
|
{
|
|
"time": "2024-05-25",
|
|
"message": "bad message"
|
|
},
|
|
{
|
|
"message": "another bad message"
|
|
}
|
|
]
|
|
"#;
|
|
|
|
// write data without skip error
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=logs1&pipeline_name=test")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
|
|
// 3. check data
|
|
// without skip error, all data should be dropped
|
|
validate_data(
|
|
"pipeline_skip_error",
|
|
&client,
|
|
"show tables",
|
|
"[[\"demo\"],[\"numbers\"]]",
|
|
)
|
|
.await;
|
|
|
|
// write data with skip error
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=logs1&pipeline_name=test&skip_error=true")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 4. check data
|
|
// with skip error, only the first row should be written
|
|
validate_data(
|
|
"pipeline_skip_error",
|
|
&client,
|
|
"select message, time from logs1",
|
|
"[[\"good message\",1716668197217000000]]",
|
|
)
|
|
.await;
|
|
|
|
// write data with skip error on header
|
|
// header has a higher priority than params
|
|
// write one row
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=logs1&pipeline_name=test&skip_error=false")
|
|
.header("Content-Type", "application/json")
|
|
.header("x-greptime-pipeline-params", "skip_error=true")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// write one row
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=logs1&pipeline_name=test&skip_error=true")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// write zero row
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=logs1&pipeline_name=test&skip_error=true")
|
|
.header("Content-Type", "application/json")
|
|
.header("x-greptime-pipeline-params", "skip_error=false")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
|
|
// 5. check data
|
|
// it will be three rows with the same ts
|
|
validate_data(
|
|
"pipeline_skip_error",
|
|
&client,
|
|
"select message, time from logs1",
|
|
"[[\"good message\",1716668197217000000],[\"good message\",1716668197217000000],[\"good message\",1716668197217000000]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_pipeline_filter(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_pipeline_filter").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
let pipeline_body = r#"
|
|
processors:
|
|
- date:
|
|
field: time
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
- filter:
|
|
field: name
|
|
targets:
|
|
- John
|
|
transform:
|
|
- field: name
|
|
type: string
|
|
- field: time
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/events/pipelines/test")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 2. write data
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"name": "John"
|
|
},
|
|
{
|
|
"time": "2024-05-25 20:16:37.218",
|
|
"name": "JoHN"
|
|
},
|
|
{
|
|
"time": "2024-05-25 20:16:37.328",
|
|
"name": "Jane"
|
|
}
|
|
]
|
|
"#;
|
|
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=logs1&pipeline_name=test")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
validate_data(
|
|
"pipeline_filter",
|
|
&client,
|
|
"select * from logs1",
|
|
"[[\"Jane\",1716668197328000000]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_pipeline_create_table(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_pipeline_create_table").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
let pipeline_body = r#"
|
|
processors:
|
|
- dissect:
|
|
fields:
|
|
- message
|
|
patterns:
|
|
- '%{ip_address} - %{username} [%{timestamp}] "%{http_method} %{request_line} %{protocol}" %{status_code} %{response_size}'
|
|
ignore_missing: true
|
|
- date:
|
|
fields:
|
|
- timestamp
|
|
formats:
|
|
- "%d/%b/%Y:%H:%M:%S %z"
|
|
|
|
transform:
|
|
- fields:
|
|
- timestamp
|
|
type: time
|
|
index: timestamp
|
|
- fields:
|
|
- ip_address
|
|
type: string
|
|
index: skipping
|
|
- fields:
|
|
- username
|
|
type: string
|
|
tag: true
|
|
- fields:
|
|
- http_method
|
|
type: string
|
|
index: inverted
|
|
- fields:
|
|
- request_line
|
|
type: string
|
|
index: fulltext
|
|
- fields:
|
|
- protocol
|
|
type: string
|
|
- fields:
|
|
- status_code
|
|
type: int32
|
|
index: inverted
|
|
tag: true
|
|
- fields:
|
|
- response_size
|
|
type: int64
|
|
on_failure: default
|
|
default: 0
|
|
- fields:
|
|
- message
|
|
type: string
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/events/pipelines/test")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let res = client
|
|
.get("/v1/pipelines/test/ddl?table=logs1")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let resp: serde_json::Value = res.json().await;
|
|
let sql = resp
|
|
.get("sql")
|
|
.unwrap()
|
|
.get("sql")
|
|
.unwrap()
|
|
.as_str()
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
"CREATE TABLE IF NOT EXISTS `logs1` (\n `timestamp` TIMESTAMP(9) NOT NULL,\n `ip_address` STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM'),\n `username` STRING NULL,\n `http_method` STRING NULL INVERTED INDEX,\n `request_line` STRING NULL FULLTEXT INDEX WITH(analyzer = 'English', backend = 'bloom', case_sensitive = 'false', false_positive_rate = '0.01', granularity = '10240'),\n `protocol` STRING NULL,\n `status_code` INT NULL INVERTED INDEX,\n `response_size` BIGINT NULL,\n `message` STRING NULL,\n TIME INDEX (`timestamp`),\n PRIMARY KEY (`username`, `status_code`)\n)\nENGINE=mito\nWITH(\n append_mode = 'true'\n)",
|
|
sql
|
|
);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_pipeline_index_options(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_pipeline_index_options").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
let pipeline_body = r#"
|
|
processors:
|
|
- date:
|
|
field: ts
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
ignore_missing: true
|
|
|
|
transform:
|
|
- field: message
|
|
type: string
|
|
index:
|
|
type: fulltext
|
|
options:
|
|
analyzer: Chinese
|
|
case_sensitive: true
|
|
backend: bloom
|
|
granularity: 2048
|
|
false_positive_rate: 0.02
|
|
- field: trace_id
|
|
type: int64
|
|
index:
|
|
type: skipping
|
|
options:
|
|
granularity: 1024
|
|
false_positive_rate: 0.05
|
|
type: BLOOM
|
|
- field: ts
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
|
|
let res = client
|
|
.post("/v1/pipelines/index_options")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"message": "hello greptime",
|
|
"trace_id": 42,
|
|
"ts": "2024-05-25 20:16:37.217"
|
|
}
|
|
]
|
|
"#;
|
|
let res = client
|
|
.post("/v1/ingest?db=public&table=pipeline_index_options&pipeline_name=index_options")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let expected_schema = "[[\"pipeline_index_options\",\"CREATE TABLE IF NOT EXISTS \\\"pipeline_index_options\\\" (\\n \\\"message\\\" STRING NULL FULLTEXT INDEX WITH(analyzer = 'Chinese', backend = 'bloom', case_sensitive = 'true', false_positive_rate = '0.02', granularity = '2048'),\\n \\\"trace_id\\\" BIGINT NULL SKIPPING INDEX WITH(false_positive_rate = '0.05', granularity = '1024', type = 'BLOOM'),\\n \\\"ts\\\" TIMESTAMP(9) NOT NULL,\\n TIME INDEX (\\\"ts\\\")\\n)\\n\\nENGINE=mito\\nWITH(\\n 'comment' = 'Created on insertion',\\n append_mode = 'true'\\n)\"]]";
|
|
validate_data(
|
|
"pipeline_index_options_schema",
|
|
&client,
|
|
"show create table pipeline_index_options",
|
|
expected_schema,
|
|
)
|
|
.await;
|
|
|
|
// The fulltext and skipping index files are only written on flush, so the
|
|
// queries below are the first thing that reads them back.
|
|
flush_table(&client, "pipeline_index_options").await;
|
|
validate_data(
|
|
"pipeline_index_options_fulltext",
|
|
&client,
|
|
"select trace_id from pipeline_index_options where matches(message, 'greptime')",
|
|
"[[42]]",
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"pipeline_index_options_skipping",
|
|
&client,
|
|
"select message from pipeline_index_options where trace_id = 42",
|
|
r#"[["hello greptime"]]"#,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_pipeline_dispatcher(storage_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(storage_type, "test_pipeline_dispatcher").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
let root_pipeline = r#"
|
|
processors:
|
|
- date:
|
|
field: time
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
ignore_missing: true
|
|
|
|
dispatcher:
|
|
field: type
|
|
rules:
|
|
- value: http
|
|
table_suffix: _http
|
|
pipeline: http
|
|
- value: db
|
|
table_suffix: _db
|
|
- value: not_found
|
|
table_suffix: _not_found
|
|
pipeline: not_found
|
|
|
|
transform:
|
|
- fields:
|
|
- id1, id1_root
|
|
- id2, id2_root
|
|
type: int32
|
|
- fields:
|
|
- type
|
|
- log
|
|
- logger
|
|
type: string
|
|
- field: time
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
|
|
let http_pipeline = r#"
|
|
processors:
|
|
|
|
transform:
|
|
- fields:
|
|
- id1, id1_http
|
|
- id2, id2_http
|
|
type: int32
|
|
- fields:
|
|
- log
|
|
- logger
|
|
type: string
|
|
- field: time
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/events/pipelines/root")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(root_pipeline)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let res = client
|
|
.post("/v1/events/pipelines/http")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(http_pipeline)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 2. write data
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "http",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster\\n"
|
|
}
|
|
]
|
|
"#;
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=logs1&pipeline_name=root")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "db",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster\\n"
|
|
}
|
|
]
|
|
"#;
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=logs1&pipeline_name=root")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "api",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster\\n"
|
|
}
|
|
]
|
|
"#;
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=logs1&pipeline_name=root")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "not_found",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster\\n"
|
|
}
|
|
]
|
|
"#;
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=logs1&pipeline_name=root")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
|
|
// 3. verify data
|
|
let expected = "[[2436]]";
|
|
validate_data(
|
|
"test_dispatcher_pipeline default table",
|
|
&client,
|
|
"select id1_root from logs1",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
let expected = "[[2436]]";
|
|
validate_data(
|
|
"test_dispatcher_pipeline http table",
|
|
&client,
|
|
"select id1_http from logs1_http",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
let expected = "[[\"2436\"]]";
|
|
validate_data(
|
|
"test_dispatcher_pipeline db table",
|
|
&client,
|
|
"select id1 from logs1_db",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_pipeline_suffix_template(storage_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(storage_type, "test_pipeline_suffix_template").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
let root_pipeline = r#"
|
|
processors:
|
|
- date:
|
|
field: time
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
ignore_missing: true
|
|
|
|
transform:
|
|
- fields:
|
|
- id1, id1_root
|
|
- id2, id2_root
|
|
type: int32
|
|
- fields:
|
|
- type
|
|
- log
|
|
- logger
|
|
type: string
|
|
- field: time
|
|
type: time
|
|
index: timestamp
|
|
table_suffix: _${type}
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/events/pipelines/root")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(root_pipeline)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 2. write data
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "http",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster\\n"
|
|
},
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "http",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster\\n"
|
|
},
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "db",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster\\n"
|
|
},
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "http",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster\\n"
|
|
},
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster\\n"
|
|
}
|
|
]
|
|
"#;
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=d_table&pipeline_name=root")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 3. check table list
|
|
validate_data(
|
|
"test_pipeline_suffix_template_table_list",
|
|
&client,
|
|
"show tables",
|
|
"[[\"d_table\"],[\"d_table_db\"],[\"d_table_http\"],[\"demo\"],[\"numbers\"]]",
|
|
)
|
|
.await;
|
|
|
|
// 4. check each table's data
|
|
validate_data(
|
|
"test_pipeline_suffix_template_default",
|
|
&client,
|
|
"select * from d_table",
|
|
"[[2436,2528,null,\"ClusterAdapter:enter sendTextDataToCluster\\\\n\",\"INTERACT.MANAGER\",1716668197217000000]]",
|
|
)
|
|
.await;
|
|
|
|
validate_data(
|
|
"test_pipeline_name_template_db",
|
|
&client,
|
|
"select * from d_table_db",
|
|
"[[2436,2528,\"db\",\"ClusterAdapter:enter sendTextDataToCluster\\\\n\",\"INTERACT.MANAGER\",1716668197217000000]]",
|
|
)
|
|
.await;
|
|
|
|
validate_data(
|
|
"test_pipeline_name_template_http",
|
|
&client,
|
|
"select * from d_table_http",
|
|
"[[2436,2528,\"http\",\"ClusterAdapter:enter sendTextDataToCluster\\\\n\",\"INTERACT.MANAGER\",1716668197217000000],[2436,2528,\"http\",\"ClusterAdapter:enter sendTextDataToCluster\\\\n\",\"INTERACT.MANAGER\",1716668197217000000],[2436,2528,\"http\",\"ClusterAdapter:enter sendTextDataToCluster\\\\n\",\"INTERACT.MANAGER\",1716668197217000000]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_pipeline_context(storage_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(storage_type, "test_pipeline_context").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
let root_pipeline = r#"
|
|
processors:
|
|
- date:
|
|
field: time
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
ignore_missing: true
|
|
|
|
transform:
|
|
- fields:
|
|
- id1, id1_root
|
|
- id2, id2_root
|
|
type: int32
|
|
- fields:
|
|
- type
|
|
- log
|
|
- logger
|
|
type: string
|
|
- field: time
|
|
type: time
|
|
index: timestamp
|
|
table_suffix: _${type}
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/events/pipelines/root")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(root_pipeline)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 2. write data
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "http",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster\\n",
|
|
"greptime_ttl": "1d",
|
|
"greptime_skip_wal": "true"
|
|
},
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "db",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster\\n"
|
|
}
|
|
]
|
|
"#;
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=d_table&pipeline_name=root")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 3. check table list
|
|
validate_data(
|
|
"test_pipeline_context_table_list",
|
|
&client,
|
|
"show tables",
|
|
"[[\"d_table_db\"],[\"d_table_http\"],[\"demo\"],[\"numbers\"]]",
|
|
)
|
|
.await;
|
|
|
|
// 4. check each table's data
|
|
// CREATE TABLE IF NOT EXISTS "d_table_db" (
|
|
// ... ignore
|
|
// )
|
|
// ENGINE=mito
|
|
// WITH(
|
|
// 'comment' = 'Created on insertion',
|
|
// append_mode = 'true'
|
|
// )
|
|
let expected = "[[\"d_table_db\",\"CREATE TABLE IF NOT EXISTS \\\"d_table_db\\\" (\\n \\\"id1_root\\\" INT NULL,\\n \\\"id2_root\\\" INT NULL,\\n \\\"type\\\" STRING NULL,\\n \\\"log\\\" STRING NULL,\\n \\\"logger\\\" STRING NULL,\\n \\\"time\\\" TIMESTAMP(9) NOT NULL,\\n TIME INDEX (\\\"time\\\")\\n)\\n\\nENGINE=mito\\nWITH(\\n 'comment' = 'Created on insertion',\\n append_mode = 'true'\\n)\"]]";
|
|
|
|
validate_data(
|
|
"test_pipeline_context_db",
|
|
&client,
|
|
"show create table d_table_db",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// CREATE TABLE IF NOT EXISTS "d_table_http" (
|
|
// ... ignore
|
|
// )
|
|
// ENGINE=mito
|
|
// WITH(
|
|
// 'comment' = 'Created on insertion',
|
|
// append_mode = 'true',
|
|
// skip_wal = 'true',
|
|
// ttl = '1day'
|
|
// )
|
|
let expected = "[[\"d_table_http\",\"CREATE TABLE IF NOT EXISTS \\\"d_table_http\\\" (\\n \\\"id1_root\\\" INT NULL,\\n \\\"id2_root\\\" INT NULL,\\n \\\"type\\\" STRING NULL,\\n \\\"log\\\" STRING NULL,\\n \\\"logger\\\" STRING NULL,\\n \\\"time\\\" TIMESTAMP(9) NOT NULL,\\n TIME INDEX (\\\"time\\\")\\n)\\n\\nENGINE=mito\\nWITH(\\n \'comment\' = 'Created on insertion',\\n append_mode = 'true',\\n skip_wal = 'true',\\n ttl = '1day'\\n)\"]]";
|
|
validate_data(
|
|
"test_pipeline_context_http",
|
|
&client,
|
|
"show create table d_table_http",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_pipeline_vrl_host_access(storage_type: StorageType) {
|
|
let pipeline = r#"
|
|
processors:
|
|
- vrl:
|
|
source: |
|
|
.value = is_string(get_env_var("GREPTIME_VRL_HOST_ACCESS_TEST") ?? "missing")
|
|
.
|
|
transform:
|
|
- field: value
|
|
type: boolean
|
|
"#;
|
|
|
|
for allow_vrl_host_access in [false, true] {
|
|
let mut instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"test_pipeline_vrl_host_access",
|
|
)
|
|
.with_default_store_type(storage_type)
|
|
.with_pipeline_options(pipeline::PipelineOptions {
|
|
// Force named dry-runs to reload and compile the stored definition.
|
|
cache_ttl: Duration::ZERO,
|
|
allow_vrl_host_access,
|
|
..Default::default()
|
|
})
|
|
.build()
|
|
.await;
|
|
let server = HttpServerBuilder::new(HttpOptions::default())
|
|
.with_log_ingest_handler(instance.fe_instance().clone(), None, None)
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
|
|
let response = client
|
|
.post("/v1/pipelines/_dryrun")
|
|
.header("Content-Type", "application/json")
|
|
.body(json!({"pipeline": pipeline, "data": "[{}]"}).to_string())
|
|
.send()
|
|
.await;
|
|
let expected_status = if allow_vrl_host_access {
|
|
StatusCode::OK
|
|
} else {
|
|
StatusCode::BAD_REQUEST
|
|
};
|
|
assert_eq!(response.status(), expected_status);
|
|
if !allow_vrl_host_access {
|
|
assert!(response.text().await.contains("undefined function"));
|
|
}
|
|
|
|
let response = client
|
|
.post("/v1/pipelines/host_policy")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline)
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), expected_status);
|
|
|
|
if allow_vrl_host_access {
|
|
let response = client
|
|
.post("/v1/pipelines/_dryrun")
|
|
.header("Content-Type", "application/json")
|
|
.body(json!({"pipeline_name": "host_policy", "data": "[{}]"}).to_string())
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
}
|
|
instance.guard.remove_all().await;
|
|
}
|
|
}
|
|
|
|
pub async fn test_pipeline_ignore_errors_config(storage_type: StorageType) {
|
|
let pipeline = "transform:\n - field: value\n type: int32\n";
|
|
let data = "{\"value\":1}\ninvalid JSON\n{\"value\":2}\n";
|
|
|
|
for ignore_errors in [false, true] {
|
|
let mut instance = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"test_pipeline_ignore_errors_config",
|
|
)
|
|
.with_default_store_type(storage_type)
|
|
.with_pipeline_options(pipeline::PipelineOptions {
|
|
ignore_errors,
|
|
..Default::default()
|
|
})
|
|
.build()
|
|
.await;
|
|
let server = HttpServerBuilder::new(HttpOptions::default())
|
|
.with_log_ingest_handler(instance.fe_instance().clone(), None, None)
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
let response = client
|
|
.post("/v1/pipelines/ignore_errors")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline)
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
for request_override in [None, Some(false), Some(true)] {
|
|
let effective = request_override.unwrap_or(ignore_errors);
|
|
let query = request_override
|
|
.map(|value| format!("&ignore_errors={value}"))
|
|
.unwrap_or_default();
|
|
let expected_status = if effective {
|
|
StatusCode::OK
|
|
} else {
|
|
StatusCode::BAD_REQUEST
|
|
};
|
|
|
|
for path in ["/v1/ingest", "/v1/events/logs"] {
|
|
let response = client
|
|
.post(&format!(
|
|
"{path}?table=logs&pipeline_name=ignore_errors{query}"
|
|
))
|
|
.header("Content-Type", "application/x-ndjson")
|
|
.body(data)
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), expected_status);
|
|
if effective {
|
|
let body: Value = response.json().await;
|
|
assert_eq!(body["output"][0]["affectedrows"], 2);
|
|
}
|
|
}
|
|
|
|
for (content_type, body) in [
|
|
("application/x-ndjson", data.to_string()),
|
|
(
|
|
"application/json",
|
|
json!({
|
|
"pipeline_name": "ignore_errors",
|
|
"data_type": "application/x-ndjson",
|
|
"data": data,
|
|
})
|
|
.to_string(),
|
|
),
|
|
(
|
|
"application/json",
|
|
json!({
|
|
"pipeline": pipeline,
|
|
"data_type": "application/x-ndjson",
|
|
"data": data,
|
|
})
|
|
.to_string(),
|
|
),
|
|
] {
|
|
let response = client
|
|
.post(&format!(
|
|
"/v1/pipelines/_dryrun?pipeline_name=ignore_errors{query}"
|
|
))
|
|
.header("Content-Type", content_type)
|
|
.body(body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), expected_status);
|
|
if effective {
|
|
let body: Value = response.json().await;
|
|
assert_eq!(body[0]["rows"].as_array().unwrap().len(), 2);
|
|
}
|
|
}
|
|
|
|
let response = client
|
|
.post(&format!(
|
|
"/v1/ingest?table=timestamps&pipeline_name=greptime_identity\
|
|
&custom_time_index=ts;epoch;s{query}"
|
|
))
|
|
.header("Content-Type", "application/json")
|
|
.body(r#"{"ts":"invalid","value":1}"#)
|
|
.send()
|
|
.await;
|
|
assert_eq!(
|
|
response.status(),
|
|
if effective {
|
|
StatusCode::OK
|
|
} else {
|
|
StatusCode::INTERNAL_SERVER_ERROR
|
|
}
|
|
);
|
|
if effective {
|
|
let body: Value = response.json().await;
|
|
assert_eq!(body["output"][0]["affectedrows"], 1);
|
|
}
|
|
}
|
|
|
|
// Parsing tolerance does not enable skipping pipeline execution errors.
|
|
let response = client
|
|
.post("/v1/ingest?table=logs&pipeline_name=ignore_errors&ignore_errors=true")
|
|
.header("Content-Type", "application/json")
|
|
.body(r#"{"value":"not an integer"}"#)
|
|
.send()
|
|
.await;
|
|
assert!(!response.status().is_success());
|
|
|
|
instance.guard.remove_all().await;
|
|
}
|
|
}
|
|
|
|
pub async fn test_pipeline_with_vrl(storage_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(storage_type, "test_pipeline_with_vrl").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
let pipeline = r#"
|
|
processors:
|
|
- date:
|
|
field: time
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
ignore_missing: true
|
|
- vrl:
|
|
source: |
|
|
.from_source = "channel_2"
|
|
cond, err = .id1 > .id2
|
|
if (cond) {
|
|
.from_source = "channel_1"
|
|
}
|
|
del(.id1)
|
|
del(.id2)
|
|
.
|
|
|
|
transform:
|
|
- fields:
|
|
- from_source
|
|
type: string
|
|
- field: time
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/events/pipelines/root")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 2. write data
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"id1": 2436,
|
|
"id2": 123,
|
|
"time": "2024-05-25 20:16:37.217"
|
|
}
|
|
]
|
|
"#;
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=d_table&pipeline_name=root")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
validate_data(
|
|
"test_pipeline_with_vrl",
|
|
&client,
|
|
"select * from d_table",
|
|
"[[\"channel_1\",1716668197217000000]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_pipeline_with_hint_vrl(storage_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(storage_type, "test_pipeline_with_hint_vrl").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
let pipeline = r#"
|
|
processors:
|
|
- date:
|
|
field: time
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
ignore_missing: true
|
|
- vrl:
|
|
source: |
|
|
.greptime_table_suffix, err = "_" + .id
|
|
.
|
|
|
|
transform:
|
|
- fields:
|
|
- id
|
|
type: int32
|
|
- field: time
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/events/pipelines/root")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 2. write data
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"id": "2436",
|
|
"time": "2024-05-25 20:16:37.217"
|
|
}
|
|
]
|
|
"#;
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=d_table&pipeline_name=root")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
validate_data(
|
|
"test_pipeline_with_hint_vrl",
|
|
&client,
|
|
"show tables",
|
|
"[[\"d_table_2436\"],[\"demo\"],[\"numbers\"]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
#[apply(both_deployment_cases)]
|
|
async fn test_influxdb_skip_wal(distributed: bool) {
|
|
let cases = vec![
|
|
HttpWalCase::new(
|
|
"InfluxDB v1",
|
|
"/v1/influxdb/write?db=public",
|
|
"text/plain",
|
|
"wal_influx,host=a value=1 1700000000000000000",
|
|
),
|
|
HttpWalCase::new(
|
|
"InfluxDB v2",
|
|
"/v1/influxdb/api/v2/write?bucket=public",
|
|
"text/plain",
|
|
"wal_influx_v2,host=a value=1 1700000000000000000",
|
|
),
|
|
];
|
|
check_http_skip_wal("influxdb", &cases, distributed).await;
|
|
}
|
|
|
|
pub async fn test_influxdb_write_with_hints(storage_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(storage_type, "test_influxdb_write_with_hints").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
let result = client
|
|
.post("/v1/influxdb/write?db=public")
|
|
.header("x-greptime-hints", "sst_format=flat,ttl=30d,skip_wal=true")
|
|
.body("sst_fmt_table,host=host1 cpu=1.2 1664370459457010101")
|
|
.send()
|
|
.await;
|
|
assert_eq!(result.status(), 204);
|
|
|
|
let res = client
|
|
.get("/v1/sql?sql=show create table sst_fmt_table")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let resp = res.text().await;
|
|
assert!(
|
|
resp.contains("sst_format = 'flat'"),
|
|
"expected sst_format = 'flat' in SHOW CREATE TABLE output, got: {resp}"
|
|
);
|
|
assert!(
|
|
resp.contains("ttl = '30days'"),
|
|
"expected ttl = '30days' in SHOW CREATE TABLE output, got: {resp}"
|
|
);
|
|
assert!(
|
|
resp.contains("skip_wal = 'true'"),
|
|
"expected skip_wal = 'true' in SHOW CREATE TABLE output, got: {resp}"
|
|
);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_influxdb_write_with_append_mode_hint(storage_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_http_app_with_frontend(
|
|
storage_type,
|
|
"test_influxdb_write_with_append_mode_hint",
|
|
)
|
|
.await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
let result = client
|
|
.post("/v1/influxdb/write?db=public")
|
|
.header("x-greptime-hint-append_mode", "true")
|
|
.body("append_mode_table,host=host1 cpu=1.2 1664370459457010101")
|
|
.send()
|
|
.await;
|
|
assert_eq!(result.status(), 204);
|
|
|
|
let res = client
|
|
.get("/v1/sql?sql=show create table append_mode_table")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let resp = res.text().await;
|
|
assert!(
|
|
resp.contains("append_mode = 'true'"),
|
|
"expected append_mode = 'true' in SHOW CREATE TABLE output, got: {resp}"
|
|
);
|
|
assert!(
|
|
resp.contains("merge_mode = 'last_row'"),
|
|
"expected merge_mode = 'last_row' in SHOW CREATE TABLE output, got: {resp}"
|
|
);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
/// Test one-to-many VRL pipeline expansion.
|
|
/// This test verifies that a VRL processor can return an array, which results in
|
|
/// multiple output rows from a single input row.
|
|
pub async fn test_pipeline_one_to_many_vrl(storage_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(storage_type, "test_pipeline_one_to_many_vrl").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
// Pipeline that expands events array into multiple rows
|
|
let pipeline = r#"
|
|
processors:
|
|
- date:
|
|
field: timestamp
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S"
|
|
ignore_missing: true
|
|
- vrl:
|
|
source: |
|
|
# Extract events array and expand each event into a separate row
|
|
events = del(.events)
|
|
base_host = del(.host)
|
|
base_timestamp = del(.timestamp)
|
|
|
|
# Map each event to a complete row object
|
|
map_values(array!(events)) -> |event| {
|
|
{
|
|
"host": base_host,
|
|
"event_type": event.type,
|
|
"event_value": event.value,
|
|
"timestamp": base_timestamp
|
|
}
|
|
}
|
|
|
|
transform:
|
|
- field: host
|
|
type: string
|
|
- field: event_type
|
|
type: string
|
|
- field: event_value
|
|
type: int32
|
|
- field: timestamp
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/events/pipelines/one_to_many")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 2. write data - single input with multiple events
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"host": "server1",
|
|
"timestamp": "2024-05-25 20:16:37",
|
|
"events": [
|
|
{"type": "cpu", "value": 80},
|
|
{"type": "memory", "value": 60},
|
|
{"type": "disk", "value": 45}
|
|
]
|
|
}
|
|
]
|
|
"#;
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=metrics&pipeline_name=one_to_many")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 3. verify: one input row should produce three output rows
|
|
validate_data(
|
|
"test_pipeline_one_to_many_vrl_count",
|
|
&client,
|
|
"select count(*) from metrics",
|
|
"[[3]]",
|
|
)
|
|
.await;
|
|
|
|
// 4. verify the actual data
|
|
validate_data(
|
|
"test_pipeline_one_to_many_vrl_data",
|
|
&client,
|
|
"select host, event_type, event_value from metrics order by event_type",
|
|
"[[\"server1\",\"cpu\",80],[\"server1\",\"disk\",45],[\"server1\",\"memory\",60]]",
|
|
)
|
|
.await;
|
|
|
|
// 5. Test with multiple input rows, each producing multiple output rows
|
|
let data_body2 = r#"
|
|
[
|
|
{
|
|
"host": "server2",
|
|
"timestamp": "2024-05-25 20:17:00",
|
|
"events": [
|
|
{"type": "cpu", "value": 90},
|
|
{"type": "memory", "value": 70}
|
|
]
|
|
},
|
|
{
|
|
"host": "server3",
|
|
"timestamp": "2024-05-25 20:18:00",
|
|
"events": [
|
|
{"type": "cpu", "value": 50}
|
|
]
|
|
}
|
|
]
|
|
"#;
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=metrics&pipeline_name=one_to_many")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body2)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 6. verify total count: 3 (from first batch) + 2 + 1 = 6 rows
|
|
validate_data(
|
|
"test_pipeline_one_to_many_vrl_total_count",
|
|
&client,
|
|
"select count(*) from metrics",
|
|
"[[6]]",
|
|
)
|
|
.await;
|
|
|
|
// 7. verify rows per host
|
|
validate_data(
|
|
"test_pipeline_one_to_many_vrl_per_host",
|
|
&client,
|
|
"select host, count(*) as cnt from metrics group by host order by host",
|
|
"[[\"server1\",3],[\"server2\",2],[\"server3\",1]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_pipeline_2(storage_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_http_app_with_frontend(storage_type, "test_pipeline_2").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
let pipeline = r#"
|
|
version: 2
|
|
processors:
|
|
- date:
|
|
field: time
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
|
|
transform:
|
|
- field: id1
|
|
type: int32
|
|
index: inverted
|
|
- field: time
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/events/pipelines/root")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 2. write data
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"id1": "123",
|
|
"id2": "2436",
|
|
"time": "2024-05-25 20:16:37.217"
|
|
}
|
|
]
|
|
"#;
|
|
let res = client
|
|
.post("/v1/events/logs?db=public&table=d_table&pipeline_name=root")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// CREATE TABLE IF NOT EXISTS "d_table" (
|
|
// "id1" INT NULL INVERTED INDEX,
|
|
// "time" TIMESTAMP(9) NOT NULL,
|
|
// "id2" STRING NULL,
|
|
// TIME INDEX ("time")
|
|
// )
|
|
// ENGINE=mito
|
|
// WITH(
|
|
// 'comment' = 'Created on insertion',
|
|
// append_mode = 'true'
|
|
// )
|
|
validate_data(
|
|
"test_pipeline_2_schema",
|
|
&client,
|
|
"show create table d_table",
|
|
"[[\"d_table\",\"CREATE TABLE IF NOT EXISTS \\\"d_table\\\" (\\n \\\"id1\\\" INT NULL INVERTED INDEX,\\n \\\"time\\\" TIMESTAMP(9) NOT NULL,\\n \\\"id2\\\" STRING NULL,\\n TIME INDEX (\\\"time\\\")\\n)\\n\\nENGINE=mito\\nWITH(\\n 'comment' = 'Created on insertion',\\n append_mode = 'true'\\n)\"]]",
|
|
)
|
|
.await;
|
|
|
|
validate_data(
|
|
"test_pipeline_2_data",
|
|
&client,
|
|
"select * from d_table",
|
|
"[[123,1716668197217000000,\"2436\"]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_identity_pipeline_with_flatten(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_identity_pipeline_with_flatten").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
let body = r#"{"__time__":1453809242,"__topic__":"","__source__":"10.170.***.***","ip":"10.200.**.***","time":"26/Jan/2016:19:54:02 +0800","url":"POST/PutData?Category=YunOsAccountOpLog&AccessKeyId=<yourAccessKeyId>&Date=Fri%2C%2028%20Jun%202013%2006%3A53%3A30%20GMT&Topic=raw&Signature=<yourSignature>HTTP/1.1","status":"200","user-agent":"aliyun-sdk-java","custom_map":{"value_a":["a","b","c"],"value_b":"b"}}"#;
|
|
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/json"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-params"),
|
|
HeaderValue::from_static("flatten_json_object=true"),
|
|
),
|
|
],
|
|
"/v1/ingest?table=logs&pipeline_name=greptime_identity",
|
|
body.as_bytes().to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let expected = r#"[["greptime_timestamp","TimestampNanosecond","PRI","NO","","TIMESTAMP"],["__source__","String","","YES","","FIELD"],["__time__","Int64","","YES","","FIELD"],["__topic__","String","","YES","","FIELD"],["custom_map.value_a","String","","YES","","FIELD"],["custom_map.value_b","String","","YES","","FIELD"],["ip","String","","YES","","FIELD"],["status","String","","YES","","FIELD"],["time","String","","YES","","FIELD"],["url","String","","YES","","FIELD"],["user-agent","String","","YES","","FIELD"]]"#;
|
|
validate_data(
|
|
"test_identity_pipeline_with_flatten_desc_logs",
|
|
&client,
|
|
"desc logs",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
let expected = "[[\"[\\\"a\\\",\\\"b\\\",\\\"c\\\"]\"]]";
|
|
validate_data(
|
|
"test_identity_pipeline_with_flatten_select_json",
|
|
&client,
|
|
"select `custom_map.value_a` from logs",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_identity_pipeline_with_custom_ts(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_identity_pipeline_with_custom_ts")
|
|
.await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
let body = r#"
|
|
[{"__time__":1453809242,"__source__":"10.170.***.***", "__name__":"hello"},
|
|
{"__time__":1453809252,"__source__":"10.170.***.***"}]
|
|
"#;
|
|
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/json"),
|
|
)],
|
|
"/v1/ingest?table=logs&pipeline_name=greptime_identity&custom_time_index=__time__;epoch;s",
|
|
body.as_bytes().to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let expected = r#"[["__time__","TimestampSecond","PRI","NO","","TIMESTAMP"],["__name__","String","","YES","","FIELD"],["__source__","String","","YES","","FIELD"]]"#;
|
|
validate_data(
|
|
"test_identity_pipeline_with_custom_ts_desc_logs",
|
|
&client,
|
|
"desc logs",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
let expected = r#"[[1453809242,"hello","10.170.***.***"],[1453809252,null,"10.170.***.***"]]"#;
|
|
validate_data(
|
|
"test_identity_pipeline_with_custom_ts_data",
|
|
&client,
|
|
"select * from logs",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// drop table
|
|
let res = client.get("/v1/sql?sql=drop table logs").send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let body = r#"
|
|
[{"__time__":"2019-01-16 02:42:01+08:00","__source__":"10.170.***.***"},
|
|
{"__time__":"2019-01-16 02:42:04+08:00","__source__":"10.170.***.***", "__name__":"hello"}]
|
|
"#;
|
|
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/json"),
|
|
)],
|
|
"/v1/ingest?table=logs&pipeline_name=greptime_identity&custom_time_index=__time__;datestr;%Y-%m-%d %H:%M:%S%z",
|
|
body.as_bytes().to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let expected = r#"[["__time__","TimestampNanosecond","PRI","NO","","TIMESTAMP"],["__source__","String","","YES","","FIELD"],["__name__","String","","YES","","FIELD"]]"#;
|
|
validate_data(
|
|
"test_identity_pipeline_with_custom_ts_desc_logs",
|
|
&client,
|
|
"desc logs",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
let expected = r#"[[1547577721000000000,"10.170.***.***",null],[1547577724000000000,"10.170.***.***","hello"]]"#;
|
|
validate_data(
|
|
"test_identity_pipeline_with_custom_ts_data",
|
|
&client,
|
|
"select * from logs",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_test_pipeline_api(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_http_app_with_frontend(store_type, "test_pipeline_api").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
let pipeline_content = r#"
|
|
processors:
|
|
- date:
|
|
field: time
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
ignore_missing: true
|
|
|
|
transform:
|
|
- fields:
|
|
- id1
|
|
- id2
|
|
type: int32
|
|
- fields:
|
|
- type
|
|
- log
|
|
- logger
|
|
type: string
|
|
- field: time
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/pipelines/test")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline_content)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let content = res.text().await;
|
|
|
|
let content = serde_json::from_str(&content);
|
|
assert!(content.is_ok());
|
|
// {"execution_time_ms":13,"pipelines":[{"name":"test","version":"2024-07-04 08:31:00.987136"}]}
|
|
let content: Value = content.unwrap();
|
|
|
|
let execution_time = content.get("execution_time_ms");
|
|
assert!(execution_time.unwrap().is_number());
|
|
let pipelines = content.get("pipelines");
|
|
let pipelines = pipelines.unwrap().as_array().unwrap();
|
|
assert_eq!(pipelines.len(), 1);
|
|
let pipeline = pipelines.first().unwrap();
|
|
assert_eq!(pipeline.get("name").unwrap(), "test");
|
|
|
|
let dryrun_schema = json!([
|
|
{
|
|
"column_type": "FIELD",
|
|
"data_type": "INT32",
|
|
"fulltext": false,
|
|
"name": "id1"
|
|
},
|
|
{
|
|
"column_type": "FIELD",
|
|
"data_type": "INT32",
|
|
"fulltext": false,
|
|
"name": "id2"
|
|
},
|
|
{
|
|
"column_type": "FIELD",
|
|
"data_type": "STRING",
|
|
"fulltext": false,
|
|
"name": "type"
|
|
},
|
|
{
|
|
"column_type": "FIELD",
|
|
"data_type": "STRING",
|
|
"fulltext": false,
|
|
"name": "log"
|
|
},
|
|
{
|
|
"column_type": "FIELD",
|
|
"data_type": "STRING",
|
|
"fulltext": false,
|
|
"name": "logger"
|
|
},
|
|
{
|
|
"column_type": "TIMESTAMP",
|
|
"data_type": "TIMESTAMP_NANOSECOND",
|
|
"fulltext": false,
|
|
"name": "time"
|
|
}
|
|
]);
|
|
let dryrun_rows = json!([
|
|
[
|
|
{
|
|
"data_type": "INT32",
|
|
"key": "id1",
|
|
"semantic_type": "FIELD",
|
|
"value": 2436
|
|
},
|
|
{
|
|
"data_type": "INT32",
|
|
"key": "id2",
|
|
"semantic_type": "FIELD",
|
|
"value": 2528
|
|
},
|
|
{
|
|
"data_type": "STRING",
|
|
"key": "type",
|
|
"semantic_type": "FIELD",
|
|
"value": "I"
|
|
},
|
|
{
|
|
"data_type": "STRING",
|
|
"key": "log",
|
|
"semantic_type": "FIELD",
|
|
"value": "ClusterAdapter:enter sendTextDataToCluster"
|
|
},
|
|
{
|
|
"data_type": "STRING",
|
|
"key": "logger",
|
|
"semantic_type": "FIELD",
|
|
"value": "INTERACT.MANAGER"
|
|
},
|
|
{
|
|
"data_type": "TIMESTAMP_NANOSECOND",
|
|
"key": "time",
|
|
"semantic_type": "TIMESTAMP",
|
|
"value": 1716668197217000000i64
|
|
}
|
|
],
|
|
[
|
|
{
|
|
"data_type": "INT32",
|
|
"key": "id1",
|
|
"semantic_type": "FIELD",
|
|
"value": 1111
|
|
},
|
|
{
|
|
"data_type": "INT32",
|
|
"key": "id2",
|
|
"semantic_type": "FIELD",
|
|
"value": 2222
|
|
},
|
|
{
|
|
"data_type": "STRING",
|
|
"key": "type",
|
|
"semantic_type": "FIELD",
|
|
"value": "D"
|
|
},
|
|
{
|
|
"data_type": "STRING",
|
|
"key": "log",
|
|
"semantic_type": "FIELD",
|
|
"value": "ClusterAdapter:enter sendTextDataToCluster ggg"
|
|
},
|
|
{
|
|
"data_type": "STRING",
|
|
"key": "logger",
|
|
"semantic_type": "FIELD",
|
|
"value": "INTERACT.MANAGER"
|
|
},
|
|
{
|
|
"data_type": "TIMESTAMP_NANOSECOND",
|
|
"key": "time",
|
|
"semantic_type": "TIMESTAMP",
|
|
"value": 1716668198217000000i64
|
|
}
|
|
]
|
|
]);
|
|
{
|
|
// test original api
|
|
let data_body = r#"
|
|
[
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "I",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster"
|
|
},
|
|
{
|
|
"id1": "1111",
|
|
"id2": "2222",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "D",
|
|
"time": "2024-05-25 20:16:38.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster ggg"
|
|
}
|
|
]
|
|
"#;
|
|
let res = client
|
|
.post("/v1/pipelines/_dryrun?pipeline_name=test")
|
|
.header("Content-Type", "application/json")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body: Value = res.json().await;
|
|
let schema = &body[0]["schema"];
|
|
let rows = &body[0]["rows"];
|
|
assert_eq!(schema, &dryrun_schema);
|
|
assert_eq!(rows, &dryrun_rows);
|
|
}
|
|
{
|
|
// test new api specify pipeline via pipeline_name
|
|
let data = r#"[
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "I",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster"
|
|
},
|
|
{
|
|
"id1": "1111",
|
|
"id2": "2222",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "D",
|
|
"time": "2024-05-25 20:16:38.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster ggg"
|
|
}
|
|
]"#;
|
|
let body = json!({"pipeline_name":"test","data":data});
|
|
let res = client
|
|
.post("/v1/pipelines/_dryrun")
|
|
.header("Content-Type", "application/json")
|
|
.body(body.to_string())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body: Value = res.json().await;
|
|
let schema = &body[0]["schema"];
|
|
let rows = &body[0]["rows"];
|
|
assert_eq!(schema, &dryrun_schema);
|
|
assert_eq!(rows, &dryrun_rows);
|
|
}
|
|
{
|
|
let pipeline_content_for_text = r#"
|
|
processors:
|
|
- dissect:
|
|
fields:
|
|
- message
|
|
patterns:
|
|
- "%{id1} %{id2} %{logger} %{type} \"%{time}\" \"%{log}\""
|
|
- date:
|
|
field: time
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
ignore_missing: true
|
|
|
|
transform:
|
|
- fields:
|
|
- id1
|
|
- id2
|
|
type: int32
|
|
- fields:
|
|
- type
|
|
- log
|
|
- logger
|
|
type: string
|
|
- field: time
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
|
|
// test new api specify pipeline via pipeline raw data
|
|
let data = r#"[
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "I",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster"
|
|
},
|
|
{
|
|
"id1": "1111",
|
|
"id2": "2222",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "D",
|
|
"time": "2024-05-25 20:16:38.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster ggg"
|
|
}
|
|
]"#;
|
|
let mut body = json!({
|
|
"data": data
|
|
});
|
|
body["pipeline"] = json!(pipeline_content);
|
|
let res = client
|
|
.post("/v1/pipelines/_dryrun")
|
|
.header("Content-Type", "application/json")
|
|
.body(body.to_string())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body: Value = res.json().await;
|
|
let schema = &body[0]["schema"];
|
|
let rows = &body[0]["rows"];
|
|
assert_eq!(schema, &dryrun_schema);
|
|
assert_eq!(rows, &dryrun_rows);
|
|
let mut body_for_text = json!({
|
|
"data": r#"2436 2528 INTERACT.MANAGER I "2024-05-25 20:16:37.217" "ClusterAdapter:enter sendTextDataToCluster"
|
|
1111 2222 INTERACT.MANAGER D "2024-05-25 20:16:38.217" "ClusterAdapter:enter sendTextDataToCluster ggg"
|
|
"#,
|
|
});
|
|
body_for_text["pipeline"] = json!(pipeline_content_for_text);
|
|
body_for_text["data_type"] = json!("text/plain");
|
|
let ndjson_content = r#"{"id1":"2436","id2":"2528","logger":"INTERACT.MANAGER","type":"I","time":"2024-05-25 20:16:37.217","log":"ClusterAdapter:enter sendTextDataToCluster"}
|
|
{"id1":"1111","id2":"2222","logger":"INTERACT.MANAGER","type":"D","time":"2024-05-25 20:16:38.217","log":"ClusterAdapter:enter sendTextDataToCluster ggg"}
|
|
"#;
|
|
let body_for_ndjson = json!({
|
|
"pipeline":pipeline_content,
|
|
"data_type": "application/x-ndjson",
|
|
"data": ndjson_content,
|
|
});
|
|
let res = client
|
|
.post("/v1/pipelines/_dryrun")
|
|
.header("Content-Type", "application/json")
|
|
.body(body_for_ndjson.to_string())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body: Value = res.json().await;
|
|
let schema = &body[0]["schema"];
|
|
let rows = &body[0]["rows"];
|
|
assert_eq!(schema, &dryrun_schema);
|
|
assert_eq!(rows, &dryrun_rows);
|
|
|
|
body_for_text["data_type"] = json!("application/yaml");
|
|
let res = client
|
|
.post("/v1/pipelines/_dryrun")
|
|
.header("Content-Type", "application/json")
|
|
.body(body_for_text.to_string())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
let body: Value = res.json().await;
|
|
assert_eq!(
|
|
body["error"],
|
|
json!(
|
|
"Invalid request parameter: invalid content type: application/yaml, expected: one of application/json, application/x-json-stream, application/x-ndjson, text/plain"
|
|
)
|
|
);
|
|
|
|
body_for_text["data_type"] = json!("application/json");
|
|
let res = client
|
|
.post("/v1/pipelines/_dryrun")
|
|
.header("Content-Type", "application/json")
|
|
.body(body_for_text.to_string())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
let body: Value = res.json().await;
|
|
assert_eq!(
|
|
body["error"],
|
|
json!(
|
|
"Invalid request parameter: json format error, please check the date is valid JSON."
|
|
)
|
|
);
|
|
|
|
body_for_text["data_type"] = json!("text/plain");
|
|
let res = client
|
|
.post("/v1/pipelines/_dryrun")
|
|
.header("Content-Type", "application/json")
|
|
.body(body_for_text.to_string())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body: Value = res.json().await;
|
|
|
|
let schema = &body[0]["schema"];
|
|
let rows = &body[0]["rows"];
|
|
assert_eq!(schema, &dryrun_schema);
|
|
assert_eq!(rows, &dryrun_rows);
|
|
}
|
|
{
|
|
// failback to old version api
|
|
// not pipeline and pipeline_name in the body
|
|
let body = json!({
|
|
"data": [
|
|
{
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "I",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster\\n"
|
|
},
|
|
{
|
|
"id1": "1111",
|
|
"id2": "2222",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "D",
|
|
"time": "2024-05-25 20:16:38.217",
|
|
"log": "ClusterAdapter:enter sendTextDataToCluster ggg"
|
|
}
|
|
]
|
|
});
|
|
let res = client
|
|
.post("/v1/pipelines/_dryrun")
|
|
.header("Content-Type", "application/json")
|
|
.body(body.to_string())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
}
|
|
{
|
|
// the dryrun API accepts at most 10 rows
|
|
let rows = (0..11)
|
|
.map(|i| {
|
|
json!({
|
|
"id1": "2436",
|
|
"id2": "2528",
|
|
"logger": "INTERACT.MANAGER",
|
|
"type": "I",
|
|
"time": "2024-05-25 20:16:37.217",
|
|
"log": format!("row {i}")
|
|
})
|
|
})
|
|
.collect::<Vec<_>>();
|
|
let data = Value::Array(rows).to_string();
|
|
let expected =
|
|
json!("Invalid request parameter: dryrun data must not exceed 10 rows, got 11");
|
|
|
|
let body = json!({"pipeline_name": "test", "data": data});
|
|
let res = client
|
|
.post("/v1/pipelines/_dryrun")
|
|
.header("Content-Type", "application/json")
|
|
.body(body.to_string())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
let body: Value = res.json().await;
|
|
assert_eq!(body["error"], expected);
|
|
|
|
let res = client
|
|
.post("/v1/pipelines/_dryrun?pipeline_name=test")
|
|
.header("Content-Type", "application/json")
|
|
.body(data)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
let body: Value = res.json().await;
|
|
assert_eq!(body["error"], expected);
|
|
}
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_plain_text_ingestion(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_http_app_with_frontend(store_type, "test_pipeline_api").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
let body = r#"
|
|
processors:
|
|
- dissect:
|
|
fields:
|
|
- message
|
|
patterns:
|
|
- "%{+ts} %{+ts} %{content}"
|
|
- date:
|
|
fields:
|
|
- ts
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
|
|
transform:
|
|
- fields:
|
|
- content
|
|
type: string
|
|
- field: ts
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/pipelines/test")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(body)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let content = res.text().await;
|
|
|
|
let content = serde_json::from_str(&content);
|
|
assert!(content.is_ok());
|
|
// {"execution_time_ms":13,"pipelines":[{"name":"test","version":"2024-07-04 08:31:00.987136"}]}
|
|
let content: Value = content.unwrap();
|
|
|
|
let version_str = content
|
|
.get("pipelines")
|
|
.unwrap()
|
|
.as_array()
|
|
.unwrap()
|
|
.first()
|
|
.unwrap()
|
|
.get("version")
|
|
.unwrap()
|
|
.as_str()
|
|
.unwrap()
|
|
.to_string();
|
|
assert!(!version_str.is_empty());
|
|
|
|
// 2. write data
|
|
let data_body = r#"
|
|
2024-05-25 20:16:37.217 hello
|
|
2024-05-25 20:16:37.218 hello world
|
|
"#;
|
|
let res = client
|
|
.post("/v1/ingest?db=public&table=logs1&pipeline_name=test")
|
|
.header("Content-Type", "text/plain")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 3. select data
|
|
let res = client.get("/v1/sql?sql=select * from logs1").send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let resp = res.text().await;
|
|
|
|
let v = get_rows_from_output(&resp);
|
|
|
|
assert_eq!(
|
|
v,
|
|
r#"[["hello",1716668197217000000],["hello world",1716668197218000000]]"#,
|
|
);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_json_stream_ingestion(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_json_stream_ingestion").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
let pipeline_body = r#"
|
|
processors:
|
|
- date:
|
|
field: time
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
transform:
|
|
- field: message
|
|
type: string
|
|
- field: time
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/pipelines/test")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 2. write data as concatenated JSON (the `json_stream` format of fluentbit)
|
|
let data_body = r#"{"time": "2024-05-25 20:16:37.217", "message": "hello"}{"time": "2024-05-25 20:16:37.218", "message": "hello world"}"#;
|
|
let res = client
|
|
.post("/v1/ingest?db=public&table=logs1&pipeline_name=test")
|
|
.header("Content-Type", "application/x-json-stream")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 3. check data
|
|
validate_data(
|
|
"json_stream_ingestion",
|
|
&client,
|
|
"select message, time from logs1",
|
|
r#"[["hello",1716668197217000000],["hello world",1716668197218000000]]"#,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_pipeline_auto_transform(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_pipeline_auto_transform").await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
let body = r#"
|
|
processors:
|
|
- dissect:
|
|
fields:
|
|
- message
|
|
patterns:
|
|
- "%{+ts} %{+ts} %{http_status_code} %{content}"
|
|
- date:
|
|
fields:
|
|
- ts
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/pipelines/test")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(body)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 2. write data
|
|
let data_body = r#"
|
|
2024-05-25 20:16:37.217 404 hello
|
|
2024-05-25 20:16:37.218 200 hello world
|
|
"#;
|
|
let res = client
|
|
.post("/v1/ingest?db=public&table=logs1&pipeline_name=test")
|
|
.header("Content-Type", "text/plain")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 3. select data
|
|
let expected = "[[1716668197217000000,\"hello\",\"404\",\"2024-05-25 20:16:37.217 404 hello\"],[1716668197218000000,\"hello world\",\"200\",\"2024-05-25 20:16:37.218 200 hello world\"]]";
|
|
validate_data(
|
|
"test_pipeline_auto_transform",
|
|
&client,
|
|
"select * from logs1",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_pipeline_auto_transform_with_select(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_pipeline_auto_transform_with_select")
|
|
.await;
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
let data_body = r#"
|
|
2024-05-25 20:16:37.217 404 hello
|
|
2024-05-25 20:16:37.218 200 hello world"#;
|
|
|
|
// select include
|
|
{
|
|
let body = r#"
|
|
processors:
|
|
- dissect:
|
|
fields:
|
|
- message
|
|
patterns:
|
|
- "%{+ts} %{+ts} %{http_status_code} %{content}"
|
|
- date:
|
|
fields:
|
|
- ts
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
- select:
|
|
fields:
|
|
- ts
|
|
- http_status_code
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/pipelines/test")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(body)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 2. write data
|
|
let res = client
|
|
.post("/v1/ingest?db=public&table=logs1&pipeline_name=test")
|
|
.header("Content-Type", "text/plain")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 3. select data
|
|
let expected = "[[1716668197217000000,\"404\"],[1716668197218000000,\"200\"]]";
|
|
validate_data(
|
|
"test_pipeline_auto_transform_with_select",
|
|
&client,
|
|
"select * from logs1",
|
|
expected,
|
|
)
|
|
.await;
|
|
}
|
|
|
|
// select include rename
|
|
{
|
|
let body = r#"
|
|
processors:
|
|
- dissect:
|
|
fields:
|
|
- message
|
|
patterns:
|
|
- "%{+ts} %{+ts} %{http_status_code} %{content}"
|
|
- date:
|
|
fields:
|
|
- ts
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
- select:
|
|
fields:
|
|
- ts
|
|
- key: http_status_code
|
|
rename_to: s_code
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/pipelines/test2")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(body)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 2. write data
|
|
let res = client
|
|
.post("/v1/ingest?db=public&table=logs2&pipeline_name=test2")
|
|
.header("Content-Type", "text/plain")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 3. check schema
|
|
let expected = "[[\"ts\",\"TimestampNanosecond\",\"PRI\",\"NO\",\"\",\"TIMESTAMP\"],[\"s_code\",\"String\",\"\",\"YES\",\"\",\"FIELD\"]]";
|
|
validate_data(
|
|
"test_pipeline_auto_transform_with_select_rename",
|
|
&client,
|
|
"desc table logs2",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// 4. check data
|
|
let expected = "[[1716668197217000000,\"404\"],[1716668197218000000,\"200\"]]";
|
|
validate_data(
|
|
"test_pipeline_auto_transform_with_select_rename",
|
|
&client,
|
|
"select * from logs2",
|
|
expected,
|
|
)
|
|
.await;
|
|
}
|
|
|
|
// select exclude
|
|
{
|
|
let body = r#"
|
|
processors:
|
|
- dissect:
|
|
fields:
|
|
- message
|
|
patterns:
|
|
- "%{+ts} %{+ts} %{http_status_code} %{content}"
|
|
- date:
|
|
fields:
|
|
- ts
|
|
formats:
|
|
- "%Y-%m-%d %H:%M:%S%.3f"
|
|
- select:
|
|
type: exclude
|
|
fields:
|
|
- http_status_code
|
|
"#;
|
|
|
|
// 1. create pipeline
|
|
let res = client
|
|
.post("/v1/pipelines/test3")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(body)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 2. write data
|
|
let res = client
|
|
.post("/v1/ingest?db=public&table=logs3&pipeline_name=test3")
|
|
.header("Content-Type", "text/plain")
|
|
.body(data_body)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// 3. check schema
|
|
let expected = "[[\"ts\",\"TimestampNanosecond\",\"PRI\",\"NO\",\"\",\"TIMESTAMP\"],[\"content\",\"String\",\"\",\"YES\",\"\",\"FIELD\"],[\"message\",\"String\",\"\",\"YES\",\"\",\"FIELD\"]]";
|
|
validate_data(
|
|
"test_pipeline_auto_transform_with_select_rename",
|
|
&client,
|
|
"desc table logs3",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// 4. check data
|
|
let expected = "[[1716668197217000000,\"hello\",\"2024-05-25 20:16:37.217 404 hello\"],[1716668197218000000,\"hello world\",\"2024-05-25 20:16:37.218 200 hello world\"]]";
|
|
validate_data(
|
|
"test_pipeline_auto_transform_with_select_rename",
|
|
&client,
|
|
"select * from logs3",
|
|
expected,
|
|
)
|
|
.await;
|
|
}
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
#[apply(both_deployment_cases)]
|
|
async fn test_otlp_metrics_skip_wal(distributed: bool) {
|
|
let mut cases = Vec::new();
|
|
let metrics: ExportMetricsServiceRequest = serde_json::from_value(json!({
|
|
"resourceMetrics":[{"scopeMetrics":[{"metrics":[{"name":"wal_otlp", "gauge":{"dataPoints":[{"timeUnixNano":"1700000000000000000","asDouble":1.0}]}}]}]}]
|
|
})).unwrap();
|
|
cases.push(HttpWalCase::new(
|
|
"OTLP metrics",
|
|
"/v1/otlp/v1/metrics",
|
|
"application/x-protobuf",
|
|
metrics.encode_to_vec(),
|
|
));
|
|
check_http_skip_wal("otlp_metrics", &cases, distributed).await;
|
|
}
|
|
|
|
pub async fn test_otlp_metrics_new(store_type: StorageType) {
|
|
// init
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_otlp_metrics_new").await;
|
|
|
|
let content = r#"
|
|
{"resourceMetrics":[{"resource":{"attributes":[{"key":"host.arch","value":{"stringValue":"arm64"}},{"key":"os.type","value":{"stringValue":"darwin"}},{"key":"os.version","value":{"stringValue":"25.0.0"}},{"key":"service.name","value":{"stringValue":"claude-code"}},{"key":"service.version","value":{"stringValue":"1.0.62"}}],"droppedAttributesCount":0},"scopeMetrics":[{"scope":{"name":"com.anthropic.claude_code","version":"1.0.62","attributes":[],"droppedAttributesCount":0},"metrics":[{"name":"claude_code.cost.usage","description":"Cost of the Claude Code session","unit":"USD","metadata":[],"sum":{"dataPoints":[{"attributes":[{"key":"user.id","value":{"stringValue":"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4"}},{"key":"session.id","value":{"stringValue":"736525A3-F5D4-496B-933E-827AF23A5B97"}},{"key":"terminal.type","value":{"stringValue":"ghostty"}},{"key":"model","value":{"stringValue":"claude-3-5-haiku-20241022"}}],"startTimeUnixNano":"1753780502453000000","timeUnixNano":"1753780559836000000","exemplars":[],"flags":0,"asDouble":0.0052544},{"attributes":[{"key":"user.id","value":{"stringValue":"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4"}},{"key":"session.id","value":{"stringValue":"736525A3-F5D4-496B-933E-827AF23A5B97"}},{"key":"terminal.type","value":{"stringValue":"ghostty"}},{"key":"model","value":{"stringValue":"claude-sonnet-4-20250514"}}],"startTimeUnixNano":"1753780513420000000","timeUnixNano":"1753780559836000000","exemplars":[],"flags":0,"asDouble":2.244618}],"aggregationTemporality":1,"isMonotonic":true}},{"name":"claude_code.token.usage","description":"Number of tokens used","unit":"tokens","metadata":[],"sum":{"dataPoints":[{"attributes":[{"key":"user.id","value":{"stringValue":"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4"}},{"key":"session.id","value":{"stringValue":"736525A3-F5D4-496B-933E-827AF23A5B97"}},{"key":"terminal.type","value":{"stringValue":"ghostty"}},{"key":"type","value":{"stringValue":"input"}},{"key":"model","value":{"stringValue":"claude-3-5-haiku-20241022"}}],"startTimeUnixNano":"1753780502453000000","timeUnixNano":"1753780559836000000","exemplars":[],"flags":0,"asDouble":6208.0},{"attributes":[{"key":"user.id","value":{"stringValue":"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4"}},{"key":"session.id","value":{"stringValue":"736525A3-F5D4-496B-933E-827AF23A5B97"}},{"key":"terminal.type","value":{"stringValue":"ghostty"}},{"key":"type","value":{"stringValue":"output"}},{"key":"model","value":{"stringValue":"claude-3-5-haiku-20241022"}}],"startTimeUnixNano":"1753780502453000000","timeUnixNano":"1753780559836000000","exemplars":[],"flags":0,"asDouble":72.0},{"attributes":[{"key":"user.id","value":{"stringValue":"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4"}},{"key":"session.id","value":{"stringValue":"736525A3-F5D4-496B-933E-827AF23A5B97"}},{"key":"terminal.type","value":{"stringValue":"ghostty"}},{"key":"type","value":{"stringValue":"cacheRead"}},{"key":"model","value":{"stringValue":"claude-3-5-haiku-20241022"}}],"startTimeUnixNano":"1753780502453000000","timeUnixNano":"1753780559836000000","exemplars":[],"flags":0,"asDouble":0.0},{"attributes":[{"key":"user.id","value":{"stringValue":"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4"}},{"key":"session.id","value":{"stringValue":"736525A3-F5D4-496B-933E-827AF23A5B97"}},{"key":"terminal.type","value":{"stringValue":"ghostty"}},{"key":"type","value":{"stringValue":"cacheCreation"}},{"key":"model","value":{"stringValue":"claude-3-5-haiku-20241022"}}],"startTimeUnixNano":"1753780502453000000","timeUnixNano":"1753780559836000000","exemplars":[],"flags":0,"asDouble":0.0},{"attributes":[{"key":"user.id","value":{"stringValue":"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4"}},{"key":"session.id","value":{"stringValue":"736525A3-F5D4-496B-933E-827AF23A5B97"}},{"key":"terminal.type","value":{"stringValue":"ghostty"}},{"key":"type","value":{"stringValue":"input"}},{"key":"model","value":{"stringValue":"claude-sonnet-4-20250514"}}],"startTimeUnixNano":"1753780513420000000","timeUnixNano":"1753780559836000000","exemplars":[],"flags":0,"asDouble":743056.0},{"attributes":[{"key":"user.id","value":{"stringValue":"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4"}},{"key":"session.id","value":{"stringValue":"736525A3-F5D4-496B-933E-827AF23A5B97"}},{"key":"terminal.type","value":{"stringValue":"ghostty"}},{"key":"type","value":{"stringValue":"output"}},{"key":"model","value":{"stringValue":"claude-sonnet-4-20250514"}}],"startTimeUnixNano":"1753780513420000000","timeUnixNano":"1753780559836000000","exemplars":[],"flags":0,"asDouble":1030.0},{"attributes":[{"key":"user.id","value":{"stringValue":"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4"}},{"key":"session.id","value":{"stringValue":"736525A3-F5D4-496B-933E-827AF23A5B97"}},{"key":"terminal.type","value":{"stringValue":"ghostty"}},{"key":"type","value":{"stringValue":"cacheRead"}},{"key":"model","value":{"stringValue":"claude-sonnet-4-20250514"}}],"startTimeUnixNano":"1753780513420000000","timeUnixNano":"1753780559836000000","exemplars":[],"flags":0,"asDouble":0.0},{"attributes":[{"key":"user.id","value":{"stringValue":"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4"}},{"key":"session.id","value":{"stringValue":"736525A3-F5D4-496B-933E-827AF23A5B97"}},{"key":"terminal.type","value":{"stringValue":"ghostty"}},{"key":"type","value":{"stringValue":"cacheCreation"}},{"key":"model","value":{"stringValue":"claude-sonnet-4-20250514"}}],"startTimeUnixNano":"1753780513420000000","timeUnixNano":"1753780559836000000","exemplars":[],"flags":0,"asDouble":0.0}],"aggregationTemporality":1,"isMonotonic":true}}],"schemaUrl":""}],"schemaUrl":""}]}
|
|
"#;
|
|
|
|
let req: ExportMetricsServiceRequest = serde_json::from_str(content).unwrap();
|
|
let body = req.encode_to_vec();
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
// write metrics data
|
|
// with scope attrs and all resource attrs
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-otlp-metric-promote-scope-attrs"),
|
|
HeaderValue::from_static("true"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-otlp-metric-promote-all-resource-attrs"),
|
|
HeaderValue::from_static("true"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-otlp-metric-ignore-resource-attrs"),
|
|
HeaderValue::from_static("os.type"),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/metrics",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let expected = "[[\"claude_code_cost_usage_USD_total\"],[\"claude_code_token_usage_tokens_total\"],[\"demo\"],[\"greptime_otel_resource_info\"],[\"greptime_physical_table\"],[\"numbers\"]]";
|
|
validate_data("otlp_metrics_all_tables", &client, "show tables;", expected).await;
|
|
|
|
// Metric-engine logical table carries the semantic identity. Match substrings
|
|
// because extra_options ordering is not stable.
|
|
validate_data(
|
|
"otlp_metrics_semantic_identity",
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name = 'claude_code_cost_usage_USD_total' \
|
|
and create_options like '%greptime.semantic.signal_type=metric%' \
|
|
and create_options like '%greptime.semantic.source=opentelemetry%';",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
// OTLP metric type is declared, so Phase 2 stamps `metadata_quality=declared`.
|
|
validate_data(
|
|
"otlp_metrics_metadata_quality_declared",
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name = 'claude_code_cost_usage_USD_total' \
|
|
and create_options like '%greptime.semantic.metric.metadata_quality=declared%';",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
|
|
// CREATE TABLE IF NOT EXISTS "claude_code_cost_usage_USD_total" (
|
|
// "greptime_timestamp" TIMESTAMP(3) NOT NULL,
|
|
// "greptime_value" DOUBLE NULL,
|
|
// "host_arch" STRING NULL,
|
|
// "job" STRING NULL,
|
|
// "model" STRING NULL,
|
|
// "os_version" STRING NULL,
|
|
// "otel_scope_name" STRING NULL,
|
|
// "otel_scope_schema_url" STRING NULL,
|
|
// "otel_scope_version" STRING NULL,
|
|
// "otlp_aggregation_temporality" STRING NULL,
|
|
// "service_name" STRING NULL,
|
|
// "service_version" STRING NULL,
|
|
// "session_id" STRING NULL,
|
|
// "terminal_type" STRING NULL,
|
|
// "user_id" STRING NULL,
|
|
// TIME INDEX ("greptime_timestamp"),
|
|
// PRIMARY KEY ("host_arch", "job", "model", "os_version", "otel_scope_name", "otel_scope_schema_url", "otel_scope_version", "otlp_aggregation_temporality", "service_name", "service_version", "session_id", "terminal_type", "user_id")
|
|
// )
|
|
// ENGINE=metric
|
|
// WITH(
|
|
// 'comment' = 'Created on insertion',
|
|
// on_physical_table = 'greptime_physical_table',
|
|
// otlp_metric_compat = 'prom'
|
|
// )
|
|
let expected = "[[\"claude_code_cost_usage_USD_total\",\"CREATE TABLE IF NOT EXISTS \\\"claude_code_cost_usage_USD_total\\\" (\\n \\\"greptime_timestamp\\\" TIMESTAMP(3) NOT NULL,\\n \\\"greptime_value\\\" DOUBLE NULL,\\n \\\"host_arch\\\" STRING NULL,\\n \\\"job\\\" STRING NULL,\\n \\\"model\\\" STRING NULL,\\n \\\"os_version\\\" STRING NULL,\\n \\\"otel_scope_name\\\" STRING NULL,\\n \\\"otel_scope_schema_url\\\" STRING NULL,\\n \\\"otel_scope_version\\\" STRING NULL,\\n \\\"otlp_aggregation_temporality\\\" STRING NULL,\\n \\\"service_name\\\" STRING NULL,\\n \\\"service_version\\\" STRING NULL,\\n \\\"session_id\\\" STRING NULL,\\n \\\"terminal_type\\\" STRING NULL,\\n \\\"user_id\\\" STRING NULL,\\n TIME INDEX (\\\"greptime_timestamp\\\"),\\n PRIMARY KEY (\\\"host_arch\\\", \\\"job\\\", \\\"model\\\", \\\"os_version\\\", \\\"otel_scope_name\\\", \\\"otel_scope_schema_url\\\", \\\"otel_scope_version\\\", \\\"otlp_aggregation_temporality\\\", \\\"service_name\\\", \\\"service_version\\\", \\\"session_id\\\", \\\"terminal_type\\\", \\\"user_id\\\")\\n)\\n\\nENGINE=metric\\nWITH(\\n 'comment' = 'Created on insertion',\\n 'greptime.semantic.metric.metadata_quality' = 'declared',\\n 'greptime.semantic.metric.original_name' = 'claude_code.cost.usage',\\n 'greptime.semantic.metric.temporality' = 'delta',\\n 'greptime.semantic.metric.type' = 'counter',\\n 'greptime.semantic.metric.unit' = 'USD',\\n 'greptime.semantic.signal_type' = 'metric',\\n 'greptime.semantic.source' = 'opentelemetry',\\n on_physical_table = 'greptime_physical_table',\\n otlp_metric_compat = 'prom'\\n)\"]]";
|
|
validate_data(
|
|
"otlp_metrics_all_show_create_table",
|
|
&client,
|
|
"show create table `claude_code_cost_usage_USD_total`;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// select metrics data
|
|
let expected = "[[1753780559836,2.244618,\"arm64\",\"claude-code\",\"claude-sonnet-4-20250514\",\"25.0.0\",\"com.anthropic.claude_code\",\"\",\"1.0.62\",\"delta\",\"claude-code\",\"1.0.62\",\"736525A3-F5D4-496B-933E-827AF23A5B97\",\"ghostty\",\"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4\"],[1753780559836,0.0052544,\"arm64\",\"claude-code\",\"claude-3-5-haiku-20241022\",\"25.0.0\",\"com.anthropic.claude_code\",\"\",\"1.0.62\",\"delta\",\"claude-code\",\"1.0.62\",\"736525A3-F5D4-496B-933E-827AF23A5B97\",\"ghostty\",\"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4\"]]";
|
|
validate_data(
|
|
"otlp_metrics_all_select",
|
|
&client,
|
|
"select * from `claude_code_cost_usage_USD_total` order by model desc;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// A metric-engine logical table stores its rows in the shared physical
|
|
// table, so reading them back from SST files goes through a different
|
|
// column mapping than the memtable read does.
|
|
flush_table(&client, "claude_code_cost_usage_USD_total").await;
|
|
validate_data(
|
|
"otlp_metrics_all_select_after_flush",
|
|
&client,
|
|
"select * from `claude_code_cost_usage_USD_total` order by model desc;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// The synthesized resource descriptor: a plain mito info table keyed by
|
|
// the raw OTel attribute keys (service.name is the only allowlisted
|
|
// resource attribute in this payload), independent of the promote/ignore
|
|
// headers. The timestamp is the newest data-point time.
|
|
validate_data(
|
|
"otlp_metrics_resource_info",
|
|
&client,
|
|
"select job, \"service.name\", greptime_value, greptime_timestamp from greptime_otel_resource_info;",
|
|
"[[\"claude-code\",\"claude-code\",1.0,1753780559836]]",
|
|
)
|
|
.await;
|
|
// All four ownership markers must land on auto-create: the descriptor
|
|
// write path refuses tables that miss any of them, so a missing stamp
|
|
// here would silently stop every follow-up descriptor write.
|
|
validate_data(
|
|
"otlp_metrics_resource_info_options",
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name = 'greptime_otel_resource_info' \
|
|
and engine = 'mito' \
|
|
and create_options like '%greptime.semantic.metric.type=info%' \
|
|
and create_options like '%greptime.semantic.metric.metadata_quality=declared%' \
|
|
and create_options like '%greptime.semantic.signal_type=metric%' \
|
|
and create_options like '%greptime.semantic.source=opentelemetry%';",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
|
|
// drop table
|
|
let res = client
|
|
.get("/v1/sql?sql=drop table `claude_code_cost_usage_USD_total`;")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let res = client
|
|
.get("/v1/sql?sql=drop table claude_code_token_usage_tokens_total;")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let res = client
|
|
.get("/v1/sql?sql=drop table greptime_otel_resource_info;")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// write metrics data
|
|
// with scope attrs
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-otlp-metric-promote-resource-attrs"),
|
|
HeaderValue::from_static("os.type;os.version"),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/metrics",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
// CREATE TABLE IF NOT EXISTS "claude_code_cost_usage_USD_total" (
|
|
// "greptime_timestamp" TIMESTAMP(3) NOT NULL,
|
|
// "greptime_value" DOUBLE NULL,
|
|
// "job" STRING NULL,
|
|
// "model" STRING NULL,
|
|
// "os_type" STRING NULL,
|
|
// "os_version" STRING NULL,
|
|
// "otlp_aggregation_temporality" STRING NULL,
|
|
// "service_name" STRING NULL,
|
|
// "service_version" STRING NULL,
|
|
// "session_id" STRING NULL,
|
|
// "terminal_type" STRING NULL,
|
|
// "user_id" STRING NULL,
|
|
// TIME INDEX ("greptime_timestamp"),
|
|
// PRIMARY KEY ("job", "model", "os_type", "os_version", "otlp_aggregation_temporality", "service_name", "service_version", "session_id", "terminal_type", "user_id")
|
|
// )
|
|
// ENGINE=metric
|
|
// WITH(
|
|
// 'comment' = 'Created on insertion',
|
|
// on_physical_table = 'greptime_physical_table',
|
|
// otlp_metric_compat = 'prom'
|
|
// )
|
|
let expected = "[[\"claude_code_cost_usage_USD_total\",\"CREATE TABLE IF NOT EXISTS \\\"claude_code_cost_usage_USD_total\\\" (\\n \\\"greptime_timestamp\\\" TIMESTAMP(3) NOT NULL,\\n \\\"greptime_value\\\" DOUBLE NULL,\\n \\\"job\\\" STRING NULL,\\n \\\"model\\\" STRING NULL,\\n \\\"os_type\\\" STRING NULL,\\n \\\"os_version\\\" STRING NULL,\\n \\\"otlp_aggregation_temporality\\\" STRING NULL,\\n \\\"service_name\\\" STRING NULL,\\n \\\"service_version\\\" STRING NULL,\\n \\\"session_id\\\" STRING NULL,\\n \\\"terminal_type\\\" STRING NULL,\\n \\\"user_id\\\" STRING NULL,\\n TIME INDEX (\\\"greptime_timestamp\\\"),\\n PRIMARY KEY (\\\"job\\\", \\\"model\\\", \\\"os_type\\\", \\\"os_version\\\", \\\"otlp_aggregation_temporality\\\", \\\"service_name\\\", \\\"service_version\\\", \\\"session_id\\\", \\\"terminal_type\\\", \\\"user_id\\\")\\n)\\n\\nENGINE=metric\\nWITH(\\n 'comment' = 'Created on insertion',\\n 'greptime.semantic.metric.metadata_quality' = 'declared',\\n 'greptime.semantic.metric.original_name' = 'claude_code.cost.usage',\\n 'greptime.semantic.metric.temporality' = 'delta',\\n 'greptime.semantic.metric.type' = 'counter',\\n 'greptime.semantic.metric.unit' = 'USD',\\n 'greptime.semantic.signal_type' = 'metric',\\n 'greptime.semantic.source' = 'opentelemetry',\\n on_physical_table = 'greptime_physical_table',\\n otlp_metric_compat = 'prom'\\n)\"]]";
|
|
validate_data(
|
|
"otlp_metrics_show_create_table",
|
|
&client,
|
|
"show create table `claude_code_cost_usage_USD_total`;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// select metrics data
|
|
let expected = "[[1753780559836,2.244618,\"claude-code\",\"claude-sonnet-4-20250514\",\"darwin\",\"25.0.0\",\"delta\",\"claude-code\",\"1.0.62\",\"736525A3-F5D4-496B-933E-827AF23A5B97\",\"ghostty\",\"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4\"],[1753780559836,0.0052544,\"claude-code\",\"claude-3-5-haiku-20241022\",\"darwin\",\"25.0.0\",\"delta\",\"claude-code\",\"1.0.62\",\"736525A3-F5D4-496B-933E-827AF23A5B97\",\"ghostty\",\"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4\"]]";
|
|
validate_data(
|
|
"otlp_metrics_select",
|
|
&client,
|
|
"select * from `claude_code_cost_usage_USD_total` order by model desc;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// drop table
|
|
let res = client
|
|
.get("/v1/sql?sql=drop table `claude_code_cost_usage_USD_total`;")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let res = client
|
|
.get("/v1/sql?sql=drop table claude_code_token_usage_tokens_total;")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// write metrics data
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
)],
|
|
"/v1/otlp/v1/metrics",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
// CREATE TABLE IF NOT EXISTS "claude_code_cost_usage_USD_total" (
|
|
// "greptime_timestamp" TIMESTAMP(3) NOT NULL,
|
|
// "greptime_value" DOUBLE NULL,
|
|
// "job" STRING NULL,
|
|
// "model" STRING NULL,
|
|
// "otlp_aggregation_temporality" STRING NULL,
|
|
// "service_name" STRING NULL,
|
|
// "service_version" STRING NULL,
|
|
// "session_id" STRING NULL,
|
|
// "terminal_type" STRING NULL,
|
|
// "user_id" STRING NULL,
|
|
// TIME INDEX ("greptime_timestamp"),
|
|
// PRIMARY KEY ("job", "model", "otlp_aggregation_temporality", "service_name", "service_version", "session_id", "terminal_type", "user_id")
|
|
// )
|
|
// ENGINE=metric
|
|
// WITH(
|
|
// 'comment' = 'Created on insertion',
|
|
// on_physical_table = 'greptime_physical_table',
|
|
// otlp_metric_compat = 'prom'
|
|
// )
|
|
let expected = "[[\"claude_code_cost_usage_USD_total\",\"CREATE TABLE IF NOT EXISTS \\\"claude_code_cost_usage_USD_total\\\" (\\n \\\"greptime_timestamp\\\" TIMESTAMP(3) NOT NULL,\\n \\\"greptime_value\\\" DOUBLE NULL,\\n \\\"job\\\" STRING NULL,\\n \\\"model\\\" STRING NULL,\\n \\\"otlp_aggregation_temporality\\\" STRING NULL,\\n \\\"service_name\\\" STRING NULL,\\n \\\"service_version\\\" STRING NULL,\\n \\\"session_id\\\" STRING NULL,\\n \\\"terminal_type\\\" STRING NULL,\\n \\\"user_id\\\" STRING NULL,\\n TIME INDEX (\\\"greptime_timestamp\\\"),\\n PRIMARY KEY (\\\"job\\\", \\\"model\\\", \\\"otlp_aggregation_temporality\\\", \\\"service_name\\\", \\\"service_version\\\", \\\"session_id\\\", \\\"terminal_type\\\", \\\"user_id\\\")\\n)\\n\\nENGINE=metric\\nWITH(\\n 'comment' = 'Created on insertion',\\n 'greptime.semantic.metric.metadata_quality' = 'declared',\\n 'greptime.semantic.metric.original_name' = 'claude_code.cost.usage',\\n 'greptime.semantic.metric.temporality' = 'delta',\\n 'greptime.semantic.metric.type' = 'counter',\\n 'greptime.semantic.metric.unit' = 'USD',\\n 'greptime.semantic.signal_type' = 'metric',\\n 'greptime.semantic.source' = 'opentelemetry',\\n on_physical_table = 'greptime_physical_table',\\n otlp_metric_compat = 'prom'\\n)\"]]";
|
|
validate_data(
|
|
"otlp_metrics_show_create_table_none",
|
|
&client,
|
|
"show create table `claude_code_cost_usage_USD_total`;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// select metrics data
|
|
let expected = "[[1753780559836,2.244618,\"claude-code\",\"claude-sonnet-4-20250514\",\"delta\",\"claude-code\",\"1.0.62\",\"736525A3-F5D4-496B-933E-827AF23A5B97\",\"ghostty\",\"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4\"],[1753780559836,0.0052544,\"claude-code\",\"claude-3-5-haiku-20241022\",\"delta\",\"claude-code\",\"1.0.62\",\"736525A3-F5D4-496B-933E-827AF23A5B97\",\"ghostty\",\"6DA02FD9-B5C5-4E61-9355-9FE8EC9A0CF4\"]]";
|
|
validate_data(
|
|
"otlp_metrics_select_none",
|
|
&client,
|
|
"select * from `claude_code_cost_usage_USD_total` order by model desc;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// the descriptor was auto-created again after the earlier drop
|
|
validate_data(
|
|
"otlp_metrics_resource_info_recreated",
|
|
&client,
|
|
"select count(*) from information_schema.tables \
|
|
where table_name = 'greptime_otel_resource_info' and engine = 'mito';",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
|
|
// drop table
|
|
let res = client
|
|
.get("/v1/sql?sql=drop table `claude_code_cost_usage_USD_total`;")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let res = client
|
|
.get("/v1/sql?sql=drop table claude_code_token_usage_tokens_total;")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let res = client
|
|
.get("/v1/sql?sql=drop table greptime_otel_resource_info;")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// job composition end-to-end: service.namespace folds into "<ns>/<name>"
|
|
// on both the descriptor and the metric tables
|
|
let content = r#"
|
|
{"resourceMetrics":[{"resource":{"attributes":[{"key":"service.name","value":{"stringValue":"api"}},{"key":"service.namespace","value":{"stringValue":"shop"}},{"key":"host.id","value":{"stringValue":"h-1"}}]},"scopeMetrics":[{"scope":{"name":"s"},"metrics":[{"name":"ns_gauge","gauge":{"dataPoints":[{"timeUnixNano":"1753780559836000000","asDouble":1.0}]}}]}]}]}
|
|
"#;
|
|
let req: ExportMetricsServiceRequest = serde_json::from_str(content).unwrap();
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
)],
|
|
"/v1/otlp/v1/metrics",
|
|
req.encode_to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
validate_data(
|
|
"otlp_metrics_namespace_job_descriptor",
|
|
&client,
|
|
"select job, \"service.name\", \"service.namespace\", \"host.id\" from greptime_otel_resource_info;",
|
|
"[[\"shop/api\",\"api\",\"shop\",\"h-1\"]]",
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_metrics_namespace_job_metric_table",
|
|
&client,
|
|
"select job from ns_gauge;",
|
|
"[[\"shop/api\"]]",
|
|
)
|
|
.await;
|
|
|
|
let content = r#"
|
|
{"resourceMetrics":[{"scopeMetrics":[{"metrics":[{"name":"http.stale.sum","sum":{"dataPoints":[{"timeUnixNano":"1753780559836000000","flags":1,"asDouble":99.0}],"aggregationTemporality":1,"isMonotonic":true}},{"name":"http.stale.histogram","histogram":{"dataPoints":[{"timeUnixNano":"1753780559835000000","count":"3","sum":4.0,"bucketCounts":["1","1","1"],"explicitBounds":[1.0,2.0]},{"timeUnixNano":"1753780559836000000","flags":1,"count":"99","sum":99.0,"bucketCounts":["99"],"explicitBounds":[1.0,2.0]}],"aggregationTemporality":1}}]}]}]}
|
|
"#;
|
|
let req: ExportMetricsServiceRequest = serde_json::from_str(content).unwrap();
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
)],
|
|
"/v1/otlp/v1/metrics",
|
|
req.encode_to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let body = ExportMetricsServiceResponse::decode(res.bytes().await).unwrap();
|
|
assert!(body.partial_success.is_none());
|
|
validate_data(
|
|
"otlp_http_stale_sum_identity",
|
|
&client,
|
|
"select otlp_aggregation_temporality, count(*) from http_stale_sum_total \
|
|
group by otlp_aggregation_temporality;",
|
|
"[[\"delta\",1]]",
|
|
)
|
|
.await;
|
|
for (table, expected) in [
|
|
("http_stale_histogram_bucket", "[[\"delta\",6]]"),
|
|
("http_stale_histogram_sum", "[[\"delta\",2]]"),
|
|
("http_stale_histogram_count", "[[\"delta\",2]]"),
|
|
] {
|
|
validate_data(
|
|
"otlp_http_stale_histogram_identity",
|
|
&client,
|
|
&format!(
|
|
"select otlp_aggregation_temporality, count(*) from {table} \
|
|
group by otlp_aggregation_temporality;"
|
|
),
|
|
expected,
|
|
)
|
|
.await;
|
|
}
|
|
|
|
let content = r#"
|
|
{"resourceMetrics":[{"scopeMetrics":[{"metrics":[{"name":"reserved_label_gauge","gauge":{"dataPoints":[{"timeUnixNano":"1753780559836000000","asDouble":1.0,"attributes":[{"key":"otlp_aggregation_temporality","value":{"stringValue":"user"}}]}]}}]}]}]}
|
|
"#;
|
|
let req: ExportMetricsServiceRequest = serde_json::from_str(content).unwrap();
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
)],
|
|
"/v1/otlp/v1/metrics",
|
|
req.encode_to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::BAD_REQUEST, res.status());
|
|
let status = GoogleRpcStatus::decode(res.bytes().await.as_ref()).unwrap();
|
|
assert_eq!(tonic::Code::InvalidArgument as i32, status.code);
|
|
assert!(status.message.contains("reserved label"));
|
|
validate_data(
|
|
"otlp_metrics_reserved_label_stores_nothing",
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name = 'reserved_label_gauge';",
|
|
"[[0]]",
|
|
)
|
|
.await;
|
|
|
|
let content = r#"
|
|
{"resourceMetrics":[{"scopeMetrics":[{"metrics":[{"name":"malformed_delta_histogram","histogram":{"dataPoints":[{"timeUnixNano":"1753780559836000000","count":"1","sum":1.0,"bucketCounts":["1"],"explicitBounds":[1.0,2.0]}],"aggregationTemporality":1}}]}]}]}
|
|
"#;
|
|
let req: ExportMetricsServiceRequest = serde_json::from_str(content).unwrap();
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
)],
|
|
"/v1/otlp/v1/metrics",
|
|
req.encode_to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::BAD_REQUEST, res.status());
|
|
let status = GoogleRpcStatus::decode(res.bytes().await.as_ref()).unwrap();
|
|
assert_eq!(tonic::Code::InvalidArgument as i32, status.code);
|
|
assert!(status.message.contains("bucket_counts length"));
|
|
validate_data(
|
|
"otlp_metrics_malformed_delta_histogram_stores_nothing",
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name in \
|
|
('malformed_delta_histogram_bucket', 'malformed_delta_histogram_sum', \
|
|
'malformed_delta_histogram_count');",
|
|
"[[0]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_otlp_exponential_histogram(store_type: StorageType) {
|
|
use opentelemetry_proto::tonic::metrics::v1::{
|
|
AggregationTemporality, ExponentialHistogram, ExponentialHistogramDataPoint, Metric,
|
|
ResourceMetrics, ScopeMetrics, exponential_histogram_data_point, metric,
|
|
};
|
|
|
|
common_telemetry::init_default_ut_logging();
|
|
let req = ExportMetricsServiceRequest {
|
|
resource_metrics: vec![ResourceMetrics {
|
|
scope_metrics: vec![ScopeMetrics {
|
|
metrics: vec![Metric {
|
|
name: "otlp.exponential.latency".to_string(),
|
|
data: Some(metric::Data::ExponentialHistogram(ExponentialHistogram {
|
|
data_points: vec![
|
|
ExponentialHistogramDataPoint {
|
|
start_time_unix_nano: 1_000_000_000,
|
|
time_unix_nano: 3_000_000_000,
|
|
count: 4,
|
|
sum: Some(8.0),
|
|
scale: 0,
|
|
zero_count: 1,
|
|
positive: Some(exponential_histogram_data_point::Buckets {
|
|
offset: -1,
|
|
bucket_counts: vec![1, 2],
|
|
}),
|
|
..Default::default()
|
|
},
|
|
ExponentialHistogramDataPoint {
|
|
start_time_unix_nano: 1_000_000_000,
|
|
time_unix_nano: 4_000_000_000,
|
|
scale: -5,
|
|
..Default::default()
|
|
},
|
|
],
|
|
aggregation_temporality: AggregationTemporality::Cumulative as i32,
|
|
})),
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
}],
|
|
};
|
|
let body = req.encode_to_vec();
|
|
let headers = || {
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
)]
|
|
};
|
|
|
|
let standalone = tests_integration::standalone::GreptimeDbStandaloneBuilder::new(
|
|
"test_otlp_exponential_histogram",
|
|
)
|
|
.with_default_store_type(store_type)
|
|
.build()
|
|
.await;
|
|
let server = build_test_prom_server(standalone.fe_instance().clone(), false, None)
|
|
.with_otlp_handler(standalone.fe_instance().clone(), true)
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
let mut guard = standalone.guard;
|
|
let res = send_req(&client, headers(), "/v1/otlp/v1/metrics", body, false).await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let response = ExportMetricsServiceResponse::decode(res.bytes().await).unwrap();
|
|
let partial_success = response.partial_success.unwrap();
|
|
assert_eq!(1, partial_success.rejected_data_points);
|
|
assert!(partial_success.error_message.contains("scale -5"));
|
|
|
|
validate_data(
|
|
"otlp_exponential_histogram_row",
|
|
&client,
|
|
"select greptime_timestamp, greptime_native_histogram from otlp_exponential_latency;",
|
|
"[[3000,{\"count_f64\":null,\"count_i64\":4,\"custom_values\":[],\"negative_buckets_f64\":[],\"negative_buckets_i64\":[],\"negative_span_lengths\":[],\"negative_span_offsets\":[],\"positive_buckets_f64\":[],\"positive_buckets_i64\":[1,2],\"positive_span_lengths\":[2],\"positive_span_offsets\":[0],\"reset_hint\":0,\"schema\":0,\"start_timestamp\":1000,\"sum\":8.0,\"zero_count_f64\":null,\"zero_count_i64\":1,\"zero_threshold\":0.0}]]",
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_exponential_histogram_metadata",
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name = 'otlp_exponential_latency' and create_options like '%greptime.semantic.metric.type=histogram%' and create_options like '%greptime.semantic.metric.temporality=cumulative%';",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
|
|
for flushed in [false, true] {
|
|
if flushed {
|
|
flush_table(&client, "otlp_exponential_latency").await;
|
|
}
|
|
let response = post_remote_read(
|
|
&client,
|
|
vec![Query {
|
|
start_timestamp_ms: 0,
|
|
end_timestamp_ms: 4000,
|
|
matchers: vec![LabelMatcher {
|
|
name: "__name__".to_string(),
|
|
value: "otlp_exponential_latency".to_string(),
|
|
r#type: 0,
|
|
}],
|
|
..Default::default()
|
|
}],
|
|
)
|
|
.await;
|
|
let histogram = &response.results[0].timeseries[0].histograms[0];
|
|
assert_eq!(histogram.timestamp, 3000);
|
|
assert_eq!(
|
|
histogram.count,
|
|
Some(api::prom_store::remote::histogram::Count::CountInt(4))
|
|
);
|
|
assert_eq!(
|
|
histogram.zero_count,
|
|
Some(api::prom_store::remote::histogram::ZeroCount::ZeroCountInt(
|
|
1
|
|
))
|
|
);
|
|
assert_eq!(histogram.positive_deltas, vec![1, 1]);
|
|
assert_eq!(histogram.positive_spans[0].offset, 0);
|
|
assert_eq!(histogram.reset_hint, 0);
|
|
assert_eq!(histogram.sum, 8.0);
|
|
}
|
|
|
|
for query in [
|
|
"histogram_count(otlp_exponential_latency)",
|
|
"histogram_sum(otlp_exponential_latency)",
|
|
"histogram_quantile(0.5,otlp_exponential_latency)",
|
|
"histogram_count(rate(otlp_exponential_latency[5s]))",
|
|
] {
|
|
let res = client
|
|
.get(&format!(
|
|
"/v1/prometheus/api/v1/query?query={}&time=3",
|
|
encode(query)
|
|
))
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status(), "query: {query}");
|
|
let body = res.text().await;
|
|
assert!(body.contains("otlp_exponential_latency"), "{query}: {body}");
|
|
}
|
|
|
|
test_otlp_delta_exponential_queries(&client).await;
|
|
test_otlp_invalid_metric_names(&client).await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
async fn test_otlp_invalid_metric_names(client: &TestClient) {
|
|
for (name, strategy) in [
|
|
("!!!", "UnderscoreEscapingWithSuffixes"),
|
|
("bad/name", "NoTranslation"),
|
|
] {
|
|
for reverse in [false, true] {
|
|
let gauge = |name| serde_json::json!({"name":name,"gauge":{"dataPoints":[{"timeUnixNano":"60000000000","asDouble":7.0}]}});
|
|
let mut metrics = vec![gauge(name), gauge("valid_name_sibling")];
|
|
if reverse {
|
|
metrics.reverse();
|
|
}
|
|
let request: ExportMetricsServiceRequest = serde_json::from_value(
|
|
serde_json::json!({"resourceMetrics":[{"scopeMetrics":[{"metrics":metrics}]}]}),
|
|
)
|
|
.unwrap();
|
|
let res = client
|
|
.post("/v1/otlp/v1/metrics")
|
|
.header("content-type", "application/x-protobuf")
|
|
.header("x-greptime-otlp-metric-translation-strategy", strategy)
|
|
.body(request.encode_to_vec())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let response = ExportMetricsServiceResponse::decode(res.bytes().await).unwrap();
|
|
assert_eq!(response.partial_success.unwrap().rejected_data_points, 1);
|
|
}
|
|
}
|
|
validate_data(
|
|
"valid_metric_name_sibling",
|
|
client,
|
|
"SELECT greptime_value FROM valid_name_sibling",
|
|
"[[7.0]]",
|
|
)
|
|
.await;
|
|
}
|
|
|
|
async fn test_otlp_delta_exponential_queries(client: &TestClient) {
|
|
use opentelemetry_proto::tonic::metrics::v1::{
|
|
AggregationTemporality, ExponentialHistogram, ExponentialHistogramDataPoint, Metric,
|
|
ResourceMetrics, ScopeMetrics, exponential_histogram_data_point, metric,
|
|
};
|
|
let request =
|
|
|name: &str, samples: &[(u64, u64, u64)], temporality| ExportMetricsServiceRequest {
|
|
resource_metrics: vec![ResourceMetrics {
|
|
scope_metrics: vec![ScopeMetrics {
|
|
metrics: vec![Metric {
|
|
name: name.into(),
|
|
data: Some(metric::Data::ExponentialHistogram(ExponentialHistogram {
|
|
aggregation_temporality: temporality as i32,
|
|
data_points: samples
|
|
.iter()
|
|
.map(|&(start, end, count)| ExponentialHistogramDataPoint {
|
|
start_time_unix_nano: start * 1_000_000_000,
|
|
time_unix_nano: end * 1_000_000_000,
|
|
count,
|
|
sum: Some(count as f64 * 1.5),
|
|
zero_threshold: 0.001,
|
|
positive: Some(exponential_histogram_data_point::Buckets {
|
|
offset: 0,
|
|
bucket_counts: vec![count],
|
|
}),
|
|
..Default::default()
|
|
})
|
|
.collect(),
|
|
})),
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
}],
|
|
};
|
|
for (name, samples) in [
|
|
(
|
|
"delta_interval",
|
|
vec![(0, 60, 10), (60, 120, 20), (120, 180, 15)],
|
|
),
|
|
("delta_crossing", vec![(90, 150, 12)]),
|
|
("delta_left", vec![(60, 120, 10)]),
|
|
("delta_gap", vec![(120, 180, 10), (240, 300, 20)]),
|
|
] {
|
|
let res = client
|
|
.post("/v1/otlp/v1/metrics")
|
|
.header("content-type", "application/x-protobuf")
|
|
.body(request(name, &samples, AggregationTemporality::Delta).encode_to_vec())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
assert!(
|
|
ExportMetricsServiceResponse::decode(res.bytes().await)
|
|
.unwrap()
|
|
.partial_success
|
|
.is_none()
|
|
);
|
|
}
|
|
for flushed in [false, true] {
|
|
if flushed {
|
|
for name in [
|
|
"delta_interval",
|
|
"delta_crossing",
|
|
"delta_left",
|
|
"delta_gap",
|
|
] {
|
|
flush_table(client, name).await;
|
|
}
|
|
}
|
|
for (expression, time, expected) in [
|
|
(
|
|
"histogram_count(sum_over_time(delta_interval[3m]))",
|
|
180,
|
|
Some(45.0),
|
|
),
|
|
(
|
|
"histogram_sum(sum_over_time(delta_interval[3m]))",
|
|
180,
|
|
Some(67.5),
|
|
),
|
|
(
|
|
"histogram_count(sum_over_time(delta_interval[3m]) / 180)",
|
|
180,
|
|
Some(0.25),
|
|
),
|
|
(
|
|
"histogram_sum(sum_over_time(delta_interval[3m]) / 180)",
|
|
180,
|
|
Some(0.375),
|
|
),
|
|
(
|
|
"histogram_fraction(1, 2, sum_over_time(delta_interval[3m]))",
|
|
180,
|
|
Some(1.0),
|
|
),
|
|
(
|
|
"histogram_count(sum_over_time(delta_crossing[3m]))",
|
|
300,
|
|
Some(12.0),
|
|
),
|
|
(
|
|
"histogram_count(sum_over_time(delta_gap[3m]))",
|
|
300,
|
|
Some(30.0),
|
|
),
|
|
("sum_over_time(delta_left[3m])", 300, None),
|
|
("sum_over_time(delta_interval[3m])", 500, None),
|
|
] {
|
|
let res = client
|
|
.get(&format!(
|
|
"/v1/prometheus/api/v1/query?query={}&time={time}",
|
|
encode(expression)
|
|
))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{expression}");
|
|
let body: serde_json::Value = res.json().await;
|
|
let results = body["data"]["result"].as_array().unwrap();
|
|
if let Some(expected) = expected {
|
|
assert_eq!(results.len(), 1, "{expression}: {body}");
|
|
let value = body["data"]["result"][0]["value"][1]
|
|
.as_str()
|
|
.unwrap()
|
|
.parse::<f64>()
|
|
.unwrap();
|
|
assert!((value - expected).abs() < 1e-12, "{expression}: {body}");
|
|
} else {
|
|
assert!(results.is_empty(), "{expression}: {body}");
|
|
}
|
|
}
|
|
}
|
|
let metadata: serde_json::Value = client
|
|
.get("/v1/prometheus/api/v1/metadata?metric=delta_interval")
|
|
.send()
|
|
.await
|
|
.json()
|
|
.await;
|
|
assert_eq!(
|
|
metadata["data"]["delta_interval"][0]["type"], "unknown",
|
|
"{metadata}"
|
|
);
|
|
// Both creation orders keep cumulative and delta identities distinct.
|
|
let mut mixed = request(
|
|
"mixed_created",
|
|
&[(0, 60, 10)],
|
|
AggregationTemporality::Cumulative,
|
|
);
|
|
mixed.resource_metrics.extend(
|
|
request(
|
|
"mixed_created",
|
|
&[(0, 60, 20)],
|
|
AggregationTemporality::Delta,
|
|
)
|
|
.resource_metrics,
|
|
);
|
|
let response = client
|
|
.post("/v1/otlp/v1/metrics")
|
|
.header("content-type", "application/x-protobuf")
|
|
.body(mixed.encode_to_vec())
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let metadata: serde_json::Value = client
|
|
.get("/v1/prometheus/api/v1/metadata?metric=mixed_created")
|
|
.send()
|
|
.await
|
|
.json()
|
|
.await;
|
|
assert_eq!(
|
|
metadata["data"]["mixed_created"][0]["type"], "unknown",
|
|
"{metadata}"
|
|
);
|
|
for (name, first, second) in [
|
|
(
|
|
"delta_first",
|
|
AggregationTemporality::Delta,
|
|
AggregationTemporality::Cumulative,
|
|
),
|
|
(
|
|
"cumulative_first",
|
|
AggregationTemporality::Cumulative,
|
|
AggregationTemporality::Delta,
|
|
),
|
|
] {
|
|
for temporality in [first, second] {
|
|
let res = client
|
|
.post("/v1/otlp/v1/metrics")
|
|
.header("content-type", "application/x-protobuf")
|
|
.body(request(name, &[(0, 60, 10)], temporality).encode_to_vec())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
}
|
|
for flushed in [false, true] {
|
|
if flushed {
|
|
flush_table(client, name).await;
|
|
}
|
|
validate_data(
|
|
"delta_identity",
|
|
client,
|
|
&format!("SELECT COUNT(*) FROM {name}"),
|
|
"[[2]]",
|
|
)
|
|
.await;
|
|
}
|
|
}
|
|
}
|
|
|
|
pub async fn test_otlp_metric_translation_strategies(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_otlp_metric_translation_strategies")
|
|
.await;
|
|
|
|
let content = r#"
|
|
{"resourceMetrics":[{"resource":{"attributes":[{"key":"service.name","value":{"stringValue":"strategy-service"}},{"key":"resource.attr","value":{"stringValue":"resource-a"}}]},"scopeMetrics":[{"scope":{"name":"test.scope","version":"1.0.0","attributes":[{"key":"scope.attr","value":{"stringValue":"scope-a"}}]},"schemaUrl":"https://example.com/schema","metrics":[{"name":"otel.strategy.duration","description":"strategy test metric","unit":"ms","sum":{"dataPoints":[{"attributes":[{"key":"data.point.label","value":{"stringValue":"duration-a"}}],"startTimeUnixNano":"1000000","timeUnixNano":"2000000","asDouble":42.0}],"aggregationTemporality":2,"isMonotonic":true}}]}]}]}
|
|
"#;
|
|
|
|
let req: ExportMetricsServiceRequest = serde_json::from_str(content).unwrap();
|
|
let body = req.encode_to_vec();
|
|
let client = TestClient::new(app).await;
|
|
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-otlp-metric-translation-strategy"),
|
|
HeaderValue::from_static("NoTranslation"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-otlp-metric-promote-scope-attrs"),
|
|
HeaderValue::from_static("true"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-otlp-metric-promote-all-resource-attrs"),
|
|
HeaderValue::from_static("true"),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/metrics",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let expected = "[[\"otel.strategy.duration\"]]";
|
|
validate_data(
|
|
"otlp_metric_translation_strategy_tables",
|
|
&client,
|
|
"select table_name from information_schema.tables where table_schema = 'public' and table_name = 'otel.strategy.duration';",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
let expected = "[[42.0,\"duration-a\",\"scope-a\",\"resource-a\",\"strategy-service\"]]";
|
|
validate_data(
|
|
"otlp_metric_translation_strategy_ingestion",
|
|
&client,
|
|
"select greptime_value, \"data.point.label\", \"otel_scope_scope.attr\", \"resource.attr\", job from \"otel.strategy.duration\";",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// the descriptor's columns are the fixed raw keys regardless of the
|
|
// translation strategy, and only allowlisted attributes are projected
|
|
validate_data(
|
|
"otlp_metric_translation_strategy_resource_info",
|
|
&client,
|
|
"select job, \"service.name\" from greptime_otel_resource_info;",
|
|
"[[\"strategy-service\",\"strategy-service\"]]",
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_metric_translation_strategy_resource_info_allowlist",
|
|
&client,
|
|
"select count(*) from information_schema.columns \
|
|
where table_name = 'greptime_otel_resource_info' and column_name = 'resource.attr';",
|
|
"[[0]]",
|
|
)
|
|
.await;
|
|
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-otlp-metric-translation-strategy"),
|
|
HeaderValue::from_static("no_translation"),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/metrics",
|
|
body,
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::BAD_REQUEST, res.status());
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_otlp_metrics_resource_info_conflicts(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_otlp_metrics_resource_info_conflicts")
|
|
.await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let content_type = || {
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
)
|
|
};
|
|
|
|
// A metric named like the descriptor wins the table name: it goes through
|
|
// the normal metric-engine path and no descriptor is synthesized.
|
|
let content = r#"
|
|
{"resourceMetrics":[{"resource":{"attributes":[{"key":"service.name","value":{"stringValue":"api"}},{"key":"host.id","value":{"stringValue":"h-1"}}]},"scopeMetrics":[{"scope":{"name":"s"},"metrics":[{"name":"greptime_otel_resource_info","gauge":{"dataPoints":[{"timeUnixNano":"1753780559836000000","asDouble":1.0}]}}]}]}]}
|
|
"#;
|
|
let req: ExportMetricsServiceRequest = serde_json::from_str(content).unwrap();
|
|
let res = send_req(
|
|
&client,
|
|
vec![content_type()],
|
|
"/v1/otlp/v1/metrics",
|
|
req.encode_to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let body = ExportMetricsServiceResponse::decode(res.bytes().await).unwrap();
|
|
assert!(body.partial_success.is_none());
|
|
validate_data(
|
|
"otlp_metrics_name_collision_engine",
|
|
&client,
|
|
"select count(*) from information_schema.tables \
|
|
where table_name = 'greptime_otel_resource_info' and engine = 'metric';",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
let res = execute_sql(&client, "drop table greptime_otel_resource_info;").await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// A pre-existing table with an incompatible schema fails the descriptor
|
|
// write, which degrades to an OTLP partial-success warning while the
|
|
// metric data itself is accepted.
|
|
let res = execute_sql(
|
|
&client,
|
|
"create table greptime_otel_resource_info (ts timestamp time index, job double, greptime_value double);",
|
|
)
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
let content = r#"
|
|
{"resourceMetrics":[{"resource":{"attributes":[{"key":"service.name","value":{"stringValue":"api"}}]},"scopeMetrics":[{"scope":{"name":"s"},"metrics":[{"name":"conflict_gauge","gauge":{"dataPoints":[{"timeUnixNano":"1753780559836000000","asDouble":7.0}]}}]}]}]}
|
|
"#;
|
|
let req: ExportMetricsServiceRequest = serde_json::from_str(content).unwrap();
|
|
let res = send_req(
|
|
&client,
|
|
vec![content_type()],
|
|
"/v1/otlp/v1/metrics",
|
|
req.encode_to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let body = ExportMetricsServiceResponse::decode(res.bytes().await).unwrap();
|
|
let partial_success = body.partial_success.as_ref().unwrap();
|
|
assert_eq!(partial_success.rejected_data_points, 0);
|
|
assert!(
|
|
partial_success
|
|
.error_message
|
|
.contains("greptime_otel_resource_info"),
|
|
"unexpected warning: {}",
|
|
partial_success.error_message
|
|
);
|
|
validate_data(
|
|
"otlp_metrics_conflict_main_data_accepted",
|
|
&client,
|
|
"select greptime_value from conflict_gauge;",
|
|
"[[7.0]]",
|
|
)
|
|
.await;
|
|
|
|
// The descriptor is auto-created again once the conflicting table is
|
|
// gone, and a follow-up write into it does not degrade.
|
|
let res = execute_sql(&client, "drop table greptime_otel_resource_info;").await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
for round in 0..2 {
|
|
let req: ExportMetricsServiceRequest = serde_json::from_str(content).unwrap();
|
|
let res = send_req(
|
|
&client,
|
|
vec![content_type()],
|
|
"/v1/otlp/v1/metrics",
|
|
req.encode_to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let body = ExportMetricsServiceResponse::decode(res.bytes().await).unwrap();
|
|
assert!(
|
|
body.partial_success.is_none(),
|
|
"round {round} unexpectedly degraded: {:?}",
|
|
body.partial_success
|
|
);
|
|
}
|
|
validate_data(
|
|
"otlp_metrics_owned_descriptor_keeps_accepting",
|
|
&client,
|
|
"select count(*) from greptime_otel_resource_info;",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
#[apply(both_deployment_cases)]
|
|
async fn test_otlp_traces_skip_wal(distributed: bool) {
|
|
let mut cases = Vec::new();
|
|
let traces = make_trace_v1_request(
|
|
"wal-test",
|
|
vec![json!({
|
|
"traceId":"0102030405060708090a0b0c0d0e0f10",
|
|
"spanId":"0102030405060708", "name":"wal test",
|
|
"startTimeUnixNano":"1700000000000000000",
|
|
"endTimeUnixNano":"1700000001000000000"
|
|
})],
|
|
);
|
|
let mut traces_case = HttpWalCase::new(
|
|
"OTLP traces",
|
|
"/v1/otlp/v1/traces",
|
|
"application/x-protobuf",
|
|
traces.encode_to_vec(),
|
|
);
|
|
traces_case
|
|
.headers
|
|
.push(("x-greptime-pipeline-name", "greptime_trace_v0"));
|
|
cases.push(traces_case);
|
|
let mut traces_v1 = HttpWalCase::new(
|
|
"OTLP traces v1",
|
|
"/v1/otlp/v1/traces",
|
|
"application/x-protobuf",
|
|
traces.encode_to_vec(),
|
|
);
|
|
traces_v1.headers = vec![
|
|
("x-greptime-pipeline-name", "greptime_trace_v1"),
|
|
("x-greptime-trace-table-name", "wal_traces_v1"),
|
|
];
|
|
cases.push(traces_v1);
|
|
check_http_skip_wal("otlp_traces", &cases, distributed).await;
|
|
}
|
|
|
|
pub async fn test_otlp_traces_v0(store_type: StorageType) {
|
|
// init
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_http_app_with_frontend(store_type, "test_otlp_traces").await;
|
|
|
|
let content = r#"
|
|
{"resourceSpans":[{"resource":{"attributes":[{"key":"service.name","value":{"stringValue":"telemetrygen"}}],"droppedAttributesCount":0},"scopeSpans":[{"scope":{"name":"telemetrygen","version":"","attributes":[],"droppedAttributesCount":0},"spans":[{"traceId":"c05d7a4ec8e1f231f02ed6e8da8655b4","spanId":"9630f2916e2f7909","traceState":"","parentSpanId":"d24f921c75f68e23","flags":256,"name":"okey-dokey-0","kind":2,"startTimeUnixNano":"1736480942444376000","endTimeUnixNano":"1736480942444499000","attributes":[{"key":"net.peer.ip","value":{"stringValue":"1.2.3.4"}},{"key":"peer.service","value":{"stringValue":"telemetrygen-client"}}],"droppedAttributesCount":0,"events":[],"droppedEventsCount":0,"links":[],"droppedLinksCount":0,"status":{"message":"","code":0}},{"traceId":"c05d7a4ec8e1f231f02ed6e8da8655b4","spanId":"d24f921c75f68e23","traceState":"","parentSpanId":"","flags":256,"name":"lets-go","kind":3,"startTimeUnixNano":"1736480942444376000","endTimeUnixNano":"1736480942444499000","attributes":[{"key":"net.peer.ip","value":{"stringValue":"1.2.3.4"}},{"key":"peer.service","value":{"stringValue":"telemetrygen-server"}}],"droppedAttributesCount":0,"events":[],"droppedEventsCount":0,"links":[],"droppedLinksCount":0,"status":{"message":"","code":0}},{"traceId":"cc9e0991a2e63d274984bd44ee669203","spanId":"8f847259b0f6e1ab","traceState":"","parentSpanId":"eba7be77e3558179","flags":256,"name":"okey-dokey-0","kind":2,"startTimeUnixNano":"1736480942444589000","endTimeUnixNano":"1736480942444712000","attributes":[{"key":"net.peer.ip","value":{"stringValue":"1.2.3.4"}},{"key":"peer.service","value":{"stringValue":"telemetrygen-client"}}],"droppedAttributesCount":0,"events":[],"droppedEventsCount":0,"links":[],"droppedLinksCount":0,"status":{"message":"","code":0}},{"traceId":"cc9e0991a2e63d274984bd44ee669203","spanId":"eba7be77e3558179","traceState":"","parentSpanId":"","flags":256,"name":"lets-go","kind":3,"startTimeUnixNano":"1736480942444589000","endTimeUnixNano":"1736480942444712000","attributes":[{"key":"net.peer.ip","value":{"stringValue":"1.2.3.4"}},{"key":"peer.service","value":{"stringValue":"telemetrygen-server"}}],"droppedAttributesCount":0,"events":[],"droppedEventsCount":0,"links":[],"droppedLinksCount":0,"status":{"message":"","code":0}}],"schemaUrl":""}],"schemaUrl":"https://opentelemetry.io/schemas/1.4.0"}]}
|
|
"#;
|
|
|
|
let req: ExportTraceServiceRequest = serde_json::from_str(content).unwrap();
|
|
let body = req.encode_to_vec();
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
// write traces data
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-name"),
|
|
HeaderValue::from_static("greptime_trace_v0"),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/traces",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let expected = r#"[["telemetrygen"]]"#;
|
|
validate_data(
|
|
"otlp_traces",
|
|
&client,
|
|
&format!(
|
|
"select service_name from {};",
|
|
trace_services_table_name(TRACE_TABLE_NAME)
|
|
),
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// Validate operations table
|
|
let expected = r#"[["telemetrygen","SPAN_KIND_CLIENT","lets-go"],["telemetrygen","SPAN_KIND_SERVER","okey-dokey-0"]]"#;
|
|
validate_data(
|
|
"otlp_traces_operations",
|
|
&client,
|
|
&format!(
|
|
"select service_name, span_kind, span_name from {} order by span_kind, span_name;",
|
|
trace_operations_table_name(TRACE_TABLE_NAME)
|
|
),
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// select traces data
|
|
let expected = r#"[[1736480942444376000,1736480942444499000,123000,"c05d7a4ec8e1f231f02ed6e8da8655b4","d24f921c75f68e23",null,"SPAN_KIND_CLIENT","lets-go","STATUS_CODE_UNSET","","","telemetrygen",{"net.peer.ip":"1.2.3.4","peer.service":"telemetrygen-server"},[],[],"telemetrygen","",{},{"service.name":"telemetrygen"}],[1736480942444376000,1736480942444499000,123000,"c05d7a4ec8e1f231f02ed6e8da8655b4","9630f2916e2f7909","d24f921c75f68e23","SPAN_KIND_SERVER","okey-dokey-0","STATUS_CODE_UNSET","","","telemetrygen",{"net.peer.ip":"1.2.3.4","peer.service":"telemetrygen-client"},[],[],"telemetrygen","",{},{"service.name":"telemetrygen"}],[1736480942444589000,1736480942444712000,123000,"cc9e0991a2e63d274984bd44ee669203","eba7be77e3558179",null,"SPAN_KIND_CLIENT","lets-go","STATUS_CODE_UNSET","","","telemetrygen",{"net.peer.ip":"1.2.3.4","peer.service":"telemetrygen-server"},[],[],"telemetrygen","",{},{"service.name":"telemetrygen"}],[1736480942444589000,1736480942444712000,123000,"cc9e0991a2e63d274984bd44ee669203","8f847259b0f6e1ab","eba7be77e3558179","SPAN_KIND_SERVER","okey-dokey-0","STATUS_CODE_UNSET","","","telemetrygen",{"net.peer.ip":"1.2.3.4","peer.service":"telemetrygen-client"},[],[],"telemetrygen","",{},{"service.name":"telemetrygen"}]]"#;
|
|
validate_data(
|
|
"otlp_traces",
|
|
&client,
|
|
"select * from opentelemetry_traces;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// The v0 data model is frozen on the unsigned `duration_nano`: pin the
|
|
// schema so the unsigned→signed flip of the built-in models (which flips
|
|
// only v1) cannot leak into v0 and break writes into pre-existing v0
|
|
// tables.
|
|
validate_data(
|
|
"otlp_traces_v0_schema",
|
|
&client,
|
|
"select column_name, lower(data_type) from information_schema.columns \
|
|
where table_name = 'opentelemetry_traces' and column_name = 'duration_nano';",
|
|
r#"[["duration_nano","bigint unsigned"]]"#,
|
|
)
|
|
.await;
|
|
|
|
// drop table
|
|
let res = client
|
|
.get("/v1/sql?sql=drop table opentelemetry_traces;")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// write traces data with gzip
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-name"),
|
|
HeaderValue::from_static("greptime_trace_v0"),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/traces",
|
|
body.clone(),
|
|
true,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
// write traces data without pipeline
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
)],
|
|
"/v1/otlp/v1/traces",
|
|
body.clone(),
|
|
true,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::BAD_REQUEST, res.status());
|
|
|
|
// select traces data again
|
|
validate_data(
|
|
"otlp_traces_with_gzip",
|
|
&client,
|
|
"select * from opentelemetry_traces;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// The attribute columns are JSON and the event/link columns are lists, whose
|
|
// SST encodings differ from their memtable representations.
|
|
flush_table(&client, "opentelemetry_traces").await;
|
|
validate_data(
|
|
"otlp_traces_after_flush",
|
|
&client,
|
|
"select * from opentelemetry_traces;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
/// Exercises v2 protobuf ingestion and JSON2 paths.
|
|
pub(crate) async fn test_otlp_traces_v2(
|
|
store_type: StorageType,
|
|
) -> Result<(), Box<dyn std::error::Error>> {
|
|
common_telemetry::init_default_ut_logging();
|
|
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_otlp_traces_v2").await;
|
|
let client = TestClient::new(app).await;
|
|
let table_name = "trace_v2_spans";
|
|
|
|
let request: ExportTraceServiceRequest = serde_json::from_value(json!({
|
|
"resourceSpans": [{
|
|
"resource": {
|
|
"attributes": [
|
|
make_string_attr("service.name", "frontend"),
|
|
make_string_attr("deployment.environment", "production")
|
|
]
|
|
},
|
|
"scopeSpans": [{
|
|
"scope": {
|
|
"name": "trace-v2-test",
|
|
"version": "1.0.0",
|
|
"attributes": [make_bool_attr("enabled", true)]
|
|
},
|
|
"spans": [{
|
|
"traceId": "c05d7a4ec8e1f231f02ed6e8da8655b4",
|
|
"spanId": "9630f2916e2f7909",
|
|
"name": "GET /api",
|
|
"kind": 2,
|
|
"startTimeUnixNano": "1736480942444376000",
|
|
"endTimeUnixNano": "1736480942444499000",
|
|
"attributes": [
|
|
make_int_attr("http.status_code", 200),
|
|
make_string_attr("http.route", "/api"),
|
|
{"key": "ratio", "value": {"doubleValue": 1.5}},
|
|
make_string_attr("a\"b", "quoted"),
|
|
make_string_attr("a\\b", "backslash")
|
|
],
|
|
"events": [{
|
|
"timeUnixNano": "1736480942444400000",
|
|
"name": "cache.hit",
|
|
"attributes": [make_int_attr("event.code", 7)]
|
|
}],
|
|
"links": [{
|
|
"traceId": "cc9e0991a2e63d274984bd44ee669203",
|
|
"spanId": "8f847259b0f6e1ab",
|
|
"attributes": [make_string_attr("link.type", "follows_from")]
|
|
}],
|
|
"status": { "message": "", "code": 1 }
|
|
}]
|
|
}],
|
|
"schemaUrl": "https://opentelemetry.io/schemas/1.4.0"
|
|
}]
|
|
}))?;
|
|
|
|
let response = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-name"),
|
|
HeaderValue::from_static(GREPTIME_INTERNAL_TRACE_PIPELINE_V2_NAME),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-trace-table-name"),
|
|
HeaderValue::from_static(table_name),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/traces",
|
|
request.encode_to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, response.status());
|
|
|
|
validate_data(
|
|
"otlp_traces_v2_json2",
|
|
&client,
|
|
"select service_name, span_attributes.\"http.status_code\"::BIGINT, \
|
|
resource_attributes.\"deployment.environment\"::STRING, \
|
|
scope_attributes.enabled::BOOLEAN from trace_v2_spans;",
|
|
r#"[["frontend",200,"production",true]]"#,
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_traces_v2_fixed_schema",
|
|
&client,
|
|
"select count(*) from information_schema.columns where table_name = 'trace_v2_spans' \
|
|
and column_name like 'span_attributes.%';",
|
|
"[[0]]",
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_traces_v2_events_links",
|
|
&client,
|
|
r#"select json_get_string(span_events, '$[0].name'),
|
|
json_get_int(span_events, '$[0].attributes."event.code"'),
|
|
json_get_string(span_links, '$[0].trace_id'),
|
|
json_get_string(span_links, '$[0].attributes."link.type"')
|
|
from trace_v2_spans;"#,
|
|
r#"[["cache.hit",7,"cc9e0991a2e63d274984bd44ee669203","follows_from"]]"#,
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_traces_v2_escaped_keys",
|
|
&client,
|
|
r#"select span_attributes."a""b"::STRING, span_attributes."a\b"::STRING from trace_v2_spans;"#,
|
|
r#"[["quoted","backslash"]]"#,
|
|
).await;
|
|
validate_data(
|
|
"otlp_traces_v2_semantics",
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name = 'trace_v2_spans' \
|
|
and create_options like '%table_data_model=greptime_trace_v2%' \
|
|
and create_options like '%greptime.semantic.pipeline=greptime_trace_v2%';",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
|
|
// JSON2 path extraction has to resolve the same way against SST files as it
|
|
// does against the memtable.
|
|
flush_table(&client, "trace_v2_spans").await;
|
|
validate_data(
|
|
"otlp_traces_v2_json2_after_flush",
|
|
&client,
|
|
"select service_name, span_attributes.\"http.status_code\"::BIGINT, \
|
|
resource_attributes.\"deployment.environment\"::STRING, \
|
|
scope_attributes.enabled::BOOLEAN from trace_v2_spans;",
|
|
r#"[["frontend",200,"production",true]]"#,
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_traces_v2_events_links_after_flush",
|
|
&client,
|
|
r#"select json_get_string(span_events, '$[0].name'),
|
|
json_get_int(span_events, '$[0].attributes."event.code"'),
|
|
json_get_string(span_links, '$[0].trace_id'),
|
|
json_get_string(span_links, '$[0].attributes."link.type"')
|
|
from trace_v2_spans;"#,
|
|
r#"[["cache.hit",7,"cc9e0991a2e63d274984bd44ee669203","follows_from"]]"#,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
Ok(())
|
|
}
|
|
|
|
/// One real OTLP export must come out of `semantic_relationships` as the
|
|
/// zero-configuration chain; resources missing an identity column derive
|
|
/// nothing extra.
|
|
pub async fn test_otlp_traces_v1_entity_graph(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_otlp_traces_entity_graph").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
let span = |trace_id: &str, span_id: &str, parent: &str, kind: u32| {
|
|
json!({
|
|
"traceId": trace_id,
|
|
"spanId": span_id,
|
|
"parentSpanId": parent,
|
|
"name": "op",
|
|
"kind": kind,
|
|
"startTimeUnixNano": "1736480942444376000",
|
|
"endTimeUnixNano": "1736480942444499000",
|
|
"attributes": [],
|
|
"status": { "message": "", "code": 0 }
|
|
})
|
|
};
|
|
let resource_spans = |attributes: Vec<Value>, spans: Vec<Value>| {
|
|
json!({
|
|
"resource": { "attributes": attributes },
|
|
"scopeSpans": [{ "scope": { "name": "graph-tests" }, "spans": spans }],
|
|
"schemaUrl": "https://opentelemetry.io/schemas/1.4.0"
|
|
})
|
|
};
|
|
|
|
// frontend: full identity (instance id + pod uid); cart: bare service —
|
|
// no instance entity; batch: instance id but no pod uid — no runs_on.
|
|
let req: ExportTraceServiceRequest = serde_json::from_value(json!({
|
|
"resourceSpans": [
|
|
resource_spans(
|
|
vec![
|
|
make_string_attr("service.name", "frontend"),
|
|
make_string_attr("service.instance.id", "inst-1"),
|
|
make_string_attr("k8s.pod.uid", "uid-9"),
|
|
make_string_attr("k8s.pod.name", "api-1"),
|
|
make_string_attr("k8s.namespace.name", "default"),
|
|
],
|
|
vec![span("c05d7a4ec8e1f231f02ed6e8da8655b4", "d24f921c75f68e23", "", 3)],
|
|
),
|
|
resource_spans(
|
|
vec![make_string_attr("service.name", "cart")],
|
|
vec![span(
|
|
"c05d7a4ec8e1f231f02ed6e8da8655b4",
|
|
"9630f2916e2f7909",
|
|
"d24f921c75f68e23",
|
|
2,
|
|
)],
|
|
),
|
|
resource_spans(
|
|
vec![
|
|
make_string_attr("service.name", "batch"),
|
|
make_string_attr("service.instance.id", "binst-1"),
|
|
],
|
|
vec![span("cc9e0991a2e63d274984bd44ee669203", "8f847259b0f6e1ab", "", 1)],
|
|
),
|
|
]
|
|
}))
|
|
.unwrap();
|
|
let res = send_trace_v1_req(&client, "graph_traces", req, false).await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let expected = r#"[["service","frontend","service","cart","calls"],["service.instance","batch,binst-1","service","batch","part_of"],["service.instance","frontend,inst-1","service","frontend","part_of"],["service.instance","frontend,inst-1","k8s.pod","uid-9","runs_on"]]"#;
|
|
validate_data(
|
|
"otlp_traces_entity_graph",
|
|
&client,
|
|
"select src_type, src_id, dst_type, dst_id, rel_type \
|
|
from greptime_private.semantic_relationships \
|
|
where observed_at >= '2025-01-01 00:00:00' order by rel_type, src_id;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_otlp_traces_v1(store_type: StorageType) {
|
|
// init
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_http_app_with_frontend(store_type, "test_otlp_traces").await;
|
|
|
|
let content = r#"
|
|
{"resourceSpans":[{"resource":{"attributes":[{"key":"service.name","value":{"stringValue":"telemetrygen"}}],"droppedAttributesCount":0},"scopeSpans":[{"scope":{"name":"telemetrygen","version":"","attributes":[],"droppedAttributesCount":0},"spans":[{"traceId":"c05d7a4ec8e1f231f02ed6e8da8655b4","spanId":"9630f2916e2f7909","traceState":"","parentSpanId":"d24f921c75f68e23","flags":256,"name":"okey-dokey-0","kind":2,"startTimeUnixNano":"1736480942444376000","endTimeUnixNano":"1736480942444499000","attributes":[{"key":"net.peer.ip","value":{"stringValue":"1.2.3.4"}},{"key":"peer.service","value":{"stringValue":"telemetrygen-client"}}],"droppedAttributesCount":0,"events":[],"droppedEventsCount":0,"links":[],"droppedLinksCount":0,"status":{"message":"","code":0}},{"traceId":"c05d7a4ec8e1f231f02ed6e8da8655b4","spanId":"d24f921c75f68e23","traceState":"","parentSpanId":"","flags":256,"name":"lets-go","kind":3,"startTimeUnixNano":"1736480942444376000","endTimeUnixNano":"1736480942444499000","attributes":[{"key":"net.peer.ip","value":{"stringValue":"1.2.3.4"}},{"key":"peer.service","value":{"stringValue":"telemetrygen-server"}}],"droppedAttributesCount":0,"events":[],"droppedEventsCount":0,"links":[],"droppedLinksCount":0,"status":{"message":"","code":0}},{"traceId":"cc9e0991a2e63d274984bd44ee669203","spanId":"8f847259b0f6e1ab","traceState":"","parentSpanId":"eba7be77e3558179","flags":256,"name":"okey-dokey-0","kind":2,"startTimeUnixNano":"1736480942444589000","endTimeUnixNano":"1736480942444712000","attributes":[{"key":"net.peer.ip","value":{"stringValue":"1.2.3.4"}},{"key":"peer.service","value":{"stringValue":"telemetrygen-client"}}],"droppedAttributesCount":0,"events":[],"droppedEventsCount":0,"links":[],"droppedLinksCount":0,"status":{"message":"","code":0}},{"traceId":"cc9e0991a2e63d274984bd44ee669203","spanId":"eba7be77e3558179","traceState":"","parentSpanId":"","flags":256,"name":"lets-go","kind":3,"startTimeUnixNano":"1736480942444589000","endTimeUnixNano":"1736480942444712000","attributes":[{"key":"net.peer.ip","value":{"stringValue":"1.2.3.4"}},{"key":"peer.service","value":{"stringValue":"telemetrygen-server"}}],"droppedAttributesCount":0,"events":[],"droppedEventsCount":0,"links":[],"droppedLinksCount":0,"status":{"message":"","code":0}}],"schemaUrl":""}],"schemaUrl":"https://opentelemetry.io/schemas/1.4.0"}]}
|
|
"#;
|
|
|
|
let trace_table_name = "mytable";
|
|
let req: ExportTraceServiceRequest = serde_json::from_str(content).unwrap();
|
|
let body = req.encode_to_vec();
|
|
|
|
// handshake
|
|
let client = TestClient::new(app).await;
|
|
|
|
// write traces data
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-name"),
|
|
HeaderValue::from_static(GREPTIME_INTERNAL_TRACE_PIPELINE_V1_NAME),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-trace-table-name"),
|
|
HeaderValue::from_static(trace_table_name),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/traces",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let expected = r#"[["telemetrygen"]]"#;
|
|
validate_data(
|
|
"otlp_traces",
|
|
&client,
|
|
&format!(
|
|
"select service_name from {};",
|
|
trace_services_table_name(trace_table_name)
|
|
),
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// Validate operations table
|
|
let expected = r#"[["telemetrygen","SPAN_KIND_CLIENT","lets-go"],["telemetrygen","SPAN_KIND_SERVER","okey-dokey-0"]]"#;
|
|
validate_data(
|
|
"otlp_traces_operations_v1",
|
|
&client,
|
|
&format!(
|
|
"select service_name, span_kind, span_name from {} order by span_kind, span_name;",
|
|
trace_operations_table_name(trace_table_name)
|
|
),
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// select traces data
|
|
let expected = r#"[[1736480942444376000,1736480942444499000,123000,null,"c05d7a4ec8e1f231f02ed6e8da8655b4","d24f921c75f68e23","SPAN_KIND_CLIENT","lets-go","STATUS_CODE_UNSET","","","telemetrygen","","telemetrygen","1.2.3.4","telemetrygen-server",[],[]],[1736480942444376000,1736480942444499000,123000,"d24f921c75f68e23","c05d7a4ec8e1f231f02ed6e8da8655b4","9630f2916e2f7909","SPAN_KIND_SERVER","okey-dokey-0","STATUS_CODE_UNSET","","","telemetrygen","","telemetrygen","1.2.3.4","telemetrygen-client",[],[]],[1736480942444589000,1736480942444712000,123000,null,"cc9e0991a2e63d274984bd44ee669203","eba7be77e3558179","SPAN_KIND_CLIENT","lets-go","STATUS_CODE_UNSET","","","telemetrygen","","telemetrygen","1.2.3.4","telemetrygen-server",[],[]],[1736480942444589000,1736480942444712000,123000,"eba7be77e3558179","cc9e0991a2e63d274984bd44ee669203","8f847259b0f6e1ab","SPAN_KIND_SERVER","okey-dokey-0","STATUS_CODE_UNSET","","","telemetrygen","","telemetrygen","1.2.3.4","telemetrygen-client",[],[]]]"#;
|
|
validate_data("otlp_traces", &client, "select * from mytable;", expected).await;
|
|
|
|
// `span_events` / `span_links` are JSON columns and the table is partitioned
|
|
// on `trace_id`, so the read back has to reassemble SST files across regions.
|
|
flush_table(&client, "mytable").await;
|
|
validate_data(
|
|
"otlp_traces_after_flush",
|
|
&client,
|
|
"select * from mytable;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// The trace v1 main table carries the trace identity (events/links preserved as
|
|
// JSON columns by the v1 model).
|
|
validate_data(
|
|
"otlp_traces_semantic_identity",
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name = 'mytable' \
|
|
and create_options like '%greptime.semantic.signal_type=trace%' \
|
|
and create_options like '%greptime.semantic.source=opentelemetry%' \
|
|
and create_options like '%greptime.semantic.pipeline=greptime_trace_v1%';",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
|
|
let expected_ddl = r#"[["mytable","CREATE TABLE IF NOT EXISTS \"mytable\" (\n \"timestamp\" TIMESTAMP(9) NOT NULL,\n \"timestamp_end\" TIMESTAMP(9) NULL COMPRESSION WITH(encoding = 'delta_binary_packed', type = 'zstd'),\n \"duration_nano\" BIGINT NULL,\n \"parent_span_id\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM') COMPRESSION WITH(encoding = 'plain', page_size = '64KiB', type = 'zstd'),\n \"trace_id\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM'),\n \"span_id\" STRING NULL COMPRESSION WITH(encoding = 'plain', page_size = '64KiB', type = 'zstd'),\n \"span_kind\" STRING NULL,\n \"span_name\" STRING NULL,\n \"span_status_code\" STRING NULL,\n \"span_status_message\" STRING NULL,\n \"trace_state\" STRING NULL,\n \"scope_name\" STRING NULL,\n \"scope_version\" STRING NULL,\n \"service_name\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM'),\n \"span_attributes.net.peer.ip\" STRING NULL,\n \"span_attributes.peer.service\" STRING NULL,\n \"span_events\" JSON NULL,\n \"span_links\" JSON NULL,\n TIME INDEX (\"timestamp\"),\n PRIMARY KEY (\"service_name\")\n)\nPARTITION ON COLUMNS (\"trace_id\") (\n \"trace_id\" < '1',\n \"trace_id\" >= '1' AND \"trace_id\" < '2',\n \"trace_id\" >= '2' AND \"trace_id\" < '3',\n \"trace_id\" >= '3' AND \"trace_id\" < '4',\n \"trace_id\" >= '4' AND \"trace_id\" < '5',\n \"trace_id\" >= '5' AND \"trace_id\" < '6',\n \"trace_id\" >= '6' AND \"trace_id\" < '7',\n \"trace_id\" >= '7' AND \"trace_id\" < '8',\n \"trace_id\" >= '8' AND \"trace_id\" < '9',\n \"trace_id\" >= '9' AND \"trace_id\" < 'a',\n \"trace_id\" >= 'a' AND \"trace_id\" < 'b',\n \"trace_id\" >= 'b' AND \"trace_id\" < 'c',\n \"trace_id\" >= 'c' AND \"trace_id\" < 'd',\n \"trace_id\" >= 'd' AND \"trace_id\" < 'e',\n \"trace_id\" >= 'e' AND \"trace_id\" < 'f',\n \"trace_id\" >= 'f'\n)\nENGINE=mito\nWITH(\n 'comment' = 'Created on insertion',\n append_mode = 'true',\n 'greptime.semantic.entity.service.id' = 'service_name',\n 'greptime.semantic.pipeline' = 'greptime_trace_v1',\n 'greptime.semantic.signal_type' = 'trace',\n 'greptime.semantic.source' = 'opentelemetry',\n 'greptime.semantic.trace.conventions' = 'https://opentelemetry.io/schemas/1.4.0',\n table_data_model = 'greptime_trace_v1'\n)"]]"#;
|
|
validate_data(
|
|
"otlp_traces",
|
|
&client,
|
|
"show create table mytable;",
|
|
expected_ddl,
|
|
)
|
|
.await;
|
|
|
|
let expected_ddl = r#"[["mytable_services","CREATE TABLE IF NOT EXISTS \"mytable_services\" (\n \"timestamp\" TIMESTAMP(9) NOT NULL,\n \"service_name\" STRING NULL,\n TIME INDEX (\"timestamp\"),\n PRIMARY KEY (\"service_name\")\n)\n\nENGINE=mito\nWITH(\n 'comment' = 'Created on insertion',\n append_mode = 'false'\n)"]]"#;
|
|
validate_data(
|
|
"otlp_traces",
|
|
&client,
|
|
&format!(
|
|
"show create table {};",
|
|
trace_services_table_name(trace_table_name)
|
|
),
|
|
expected_ddl,
|
|
)
|
|
.await;
|
|
|
|
// drop table
|
|
let res = client.get("/v1/sql?sql=drop table mytable;").send().await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// test trace table with custom partitions: 1
|
|
let trace_table_part1 = "trace_table_part1";
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-name"),
|
|
HeaderValue::from_static(GREPTIME_INTERNAL_TRACE_PIPELINE_V1_NAME),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-trace-table-name"),
|
|
HeaderValue::from_static(trace_table_part1),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-hints"),
|
|
HeaderValue::from_static("trace_table_partitions=1"),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/traces",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected_ddl = r#"[["trace_table_part1","CREATE TABLE IF NOT EXISTS \"trace_table_part1\" (\n \"timestamp\" TIMESTAMP(9) NOT NULL,\n \"timestamp_end\" TIMESTAMP(9) NULL COMPRESSION WITH(encoding = 'delta_binary_packed', type = 'zstd'),\n \"duration_nano\" BIGINT NULL,\n \"parent_span_id\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM') COMPRESSION WITH(encoding = 'plain', page_size = '64KiB', type = 'zstd'),\n \"trace_id\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM'),\n \"span_id\" STRING NULL COMPRESSION WITH(encoding = 'plain', page_size = '64KiB', type = 'zstd'),\n \"span_kind\" STRING NULL,\n \"span_name\" STRING NULL,\n \"span_status_code\" STRING NULL,\n \"span_status_message\" STRING NULL,\n \"trace_state\" STRING NULL,\n \"scope_name\" STRING NULL,\n \"scope_version\" STRING NULL,\n \"service_name\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM'),\n \"span_attributes.net.peer.ip\" STRING NULL,\n \"span_attributes.peer.service\" STRING NULL,\n \"span_events\" JSON NULL,\n \"span_links\" JSON NULL,\n TIME INDEX (\"timestamp\"),\n PRIMARY KEY (\"service_name\")\n)\n\nENGINE=mito\nWITH(\n 'comment' = 'Created on insertion',\n append_mode = 'true',\n 'greptime.semantic.entity.service.id' = 'service_name',\n 'greptime.semantic.pipeline' = 'greptime_trace_v1',\n 'greptime.semantic.signal_type' = 'trace',\n 'greptime.semantic.source' = 'opentelemetry',\n 'greptime.semantic.trace.conventions' = 'https://opentelemetry.io/schemas/1.4.0',\n table_data_model = 'greptime_trace_v1'\n)"]]"#;
|
|
validate_data(
|
|
"otlp_traces",
|
|
&client,
|
|
"show create table trace_table_part1;",
|
|
expected_ddl,
|
|
)
|
|
.await;
|
|
|
|
// test trace table with custom partitions: 4
|
|
let trace_table_part4 = "trace_table_part4";
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-name"),
|
|
HeaderValue::from_static(GREPTIME_INTERNAL_TRACE_PIPELINE_V1_NAME),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-trace-table-name"),
|
|
HeaderValue::from_static(trace_table_part4),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-hints"),
|
|
HeaderValue::from_static("trace_table_partitions=4"),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/traces",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected_ddl = r#"[["trace_table_part4","CREATE TABLE IF NOT EXISTS \"trace_table_part4\" (\n \"timestamp\" TIMESTAMP(9) NOT NULL,\n \"timestamp_end\" TIMESTAMP(9) NULL COMPRESSION WITH(encoding = 'delta_binary_packed', type = 'zstd'),\n \"duration_nano\" BIGINT NULL,\n \"parent_span_id\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM') COMPRESSION WITH(encoding = 'plain', page_size = '64KiB', type = 'zstd'),\n \"trace_id\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM'),\n \"span_id\" STRING NULL COMPRESSION WITH(encoding = 'plain', page_size = '64KiB', type = 'zstd'),\n \"span_kind\" STRING NULL,\n \"span_name\" STRING NULL,\n \"span_status_code\" STRING NULL,\n \"span_status_message\" STRING NULL,\n \"trace_state\" STRING NULL,\n \"scope_name\" STRING NULL,\n \"scope_version\" STRING NULL,\n \"service_name\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM'),\n \"span_attributes.net.peer.ip\" STRING NULL,\n \"span_attributes.peer.service\" STRING NULL,\n \"span_events\" JSON NULL,\n \"span_links\" JSON NULL,\n TIME INDEX (\"timestamp\"),\n PRIMARY KEY (\"service_name\")\n)\nPARTITION ON COLUMNS (\"trace_id\") (\n \"trace_id\" < '4',\n \"trace_id\" >= '4' AND \"trace_id\" < '8',\n \"trace_id\" >= '8' AND \"trace_id\" < 'c',\n \"trace_id\" >= 'c'\n)\nENGINE=mito\nWITH(\n 'comment' = 'Created on insertion',\n append_mode = 'true',\n 'greptime.semantic.entity.service.id' = 'service_name',\n 'greptime.semantic.pipeline' = 'greptime_trace_v1',\n 'greptime.semantic.signal_type' = 'trace',\n 'greptime.semantic.source' = 'opentelemetry',\n 'greptime.semantic.trace.conventions' = 'https://opentelemetry.io/schemas/1.4.0',\n table_data_model = 'greptime_trace_v1'\n)"]]"#;
|
|
validate_data(
|
|
"otlp_traces",
|
|
&client,
|
|
"show create table trace_table_part4;",
|
|
expected_ddl,
|
|
)
|
|
.await;
|
|
|
|
// test trace table with custom partitions: 32
|
|
let trace_table_part32 = "trace_table_part32";
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-name"),
|
|
HeaderValue::from_static(GREPTIME_INTERNAL_TRACE_PIPELINE_V1_NAME),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-trace-table-name"),
|
|
HeaderValue::from_static(trace_table_part32),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-hints"),
|
|
HeaderValue::from_static("trace_table_partitions=32"),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/traces",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected_ddl = r#"[["trace_table_part32","CREATE TABLE IF NOT EXISTS \"trace_table_part32\" (\n \"timestamp\" TIMESTAMP(9) NOT NULL,\n \"timestamp_end\" TIMESTAMP(9) NULL COMPRESSION WITH(encoding = 'delta_binary_packed', type = 'zstd'),\n \"duration_nano\" BIGINT NULL,\n \"parent_span_id\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM') COMPRESSION WITH(encoding = 'plain', page_size = '64KiB', type = 'zstd'),\n \"trace_id\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM'),\n \"span_id\" STRING NULL COMPRESSION WITH(encoding = 'plain', page_size = '64KiB', type = 'zstd'),\n \"span_kind\" STRING NULL,\n \"span_name\" STRING NULL,\n \"span_status_code\" STRING NULL,\n \"span_status_message\" STRING NULL,\n \"trace_state\" STRING NULL,\n \"scope_name\" STRING NULL,\n \"scope_version\" STRING NULL,\n \"service_name\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM'),\n \"span_attributes.net.peer.ip\" STRING NULL,\n \"span_attributes.peer.service\" STRING NULL,\n \"span_events\" JSON NULL,\n \"span_links\" JSON NULL,\n TIME INDEX (\"timestamp\"),\n PRIMARY KEY (\"service_name\")\n)\nPARTITION ON COLUMNS (\"trace_id\") (\n \"trace_id\" < '08',\n \"trace_id\" >= '08' AND \"trace_id\" < '10',\n \"trace_id\" >= '10' AND \"trace_id\" < '18',\n \"trace_id\" >= '18' AND \"trace_id\" < '20',\n \"trace_id\" >= '20' AND \"trace_id\" < '28',\n \"trace_id\" >= '28' AND \"trace_id\" < '30',\n \"trace_id\" >= '30' AND \"trace_id\" < '38',\n \"trace_id\" >= '38' AND \"trace_id\" < '40',\n \"trace_id\" >= '40' AND \"trace_id\" < '48',\n \"trace_id\" >= '48' AND \"trace_id\" < '50',\n \"trace_id\" >= '50' AND \"trace_id\" < '58',\n \"trace_id\" >= '58' AND \"trace_id\" < '60',\n \"trace_id\" >= '60' AND \"trace_id\" < '68',\n \"trace_id\" >= '68' AND \"trace_id\" < '70',\n \"trace_id\" >= '70' AND \"trace_id\" < '78',\n \"trace_id\" >= '78' AND \"trace_id\" < '80',\n \"trace_id\" >= '80' AND \"trace_id\" < '88',\n \"trace_id\" >= '88' AND \"trace_id\" < '90',\n \"trace_id\" >= '90' AND \"trace_id\" < '98',\n \"trace_id\" >= '98' AND \"trace_id\" < 'a0',\n \"trace_id\" >= 'a0' AND \"trace_id\" < 'a8',\n \"trace_id\" >= 'a8' AND \"trace_id\" < 'b0',\n \"trace_id\" >= 'b0' AND \"trace_id\" < 'b8',\n \"trace_id\" >= 'b8' AND \"trace_id\" < 'c0',\n \"trace_id\" >= 'c0' AND \"trace_id\" < 'c8',\n \"trace_id\" >= 'c8' AND \"trace_id\" < 'd0',\n \"trace_id\" >= 'd0' AND \"trace_id\" < 'd8',\n \"trace_id\" >= 'd8' AND \"trace_id\" < 'e0',\n \"trace_id\" >= 'e0' AND \"trace_id\" < 'e8',\n \"trace_id\" >= 'e8' AND \"trace_id\" < 'f0',\n \"trace_id\" >= 'f0' AND \"trace_id\" < 'f8',\n \"trace_id\" >= 'f8'\n)\nENGINE=mito\nWITH(\n 'comment' = 'Created on insertion',\n append_mode = 'true',\n 'greptime.semantic.entity.service.id' = 'service_name',\n 'greptime.semantic.pipeline' = 'greptime_trace_v1',\n 'greptime.semantic.signal_type' = 'trace',\n 'greptime.semantic.source' = 'opentelemetry',\n 'greptime.semantic.trace.conventions' = 'https://opentelemetry.io/schemas/1.4.0',\n table_data_model = 'greptime_trace_v1'\n)"]]"#;
|
|
validate_data(
|
|
"otlp_traces",
|
|
&client,
|
|
"show create table trace_table_part32;",
|
|
expected_ddl,
|
|
)
|
|
.await;
|
|
|
|
// invalid partition count
|
|
let trace_table_part_abc = "trace_table_part_abc";
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-name"),
|
|
HeaderValue::from_static(GREPTIME_INTERNAL_TRACE_PIPELINE_V1_NAME),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-trace-table-name"),
|
|
HeaderValue::from_static(trace_table_part_abc),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-hints"),
|
|
HeaderValue::from_static("trace_table_partitions=abc"),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/traces",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::BAD_REQUEST, res.status());
|
|
|
|
// write traces data with gzip
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-name"),
|
|
HeaderValue::from_static(GREPTIME_INTERNAL_TRACE_PIPELINE_V1_NAME),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-trace-table-name"),
|
|
HeaderValue::from_static(trace_table_name),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/traces",
|
|
body.clone(),
|
|
true,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
// select traces data again
|
|
validate_data(
|
|
"otlp_traces_with_gzip",
|
|
&client,
|
|
"select * from mytable;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
let coercion_table_name = "trace_type_coercion";
|
|
let coercion_req = make_trace_v1_request(
|
|
"type-coercion",
|
|
vec![make_trace_v1_span(
|
|
"00000000000000000000000000000001",
|
|
"0000000000000001",
|
|
"coercion-seed",
|
|
1_736_480_942_444_376_000,
|
|
1_736_480_942_444_499_000,
|
|
vec![
|
|
make_double_attr("attr_float", 1.5),
|
|
make_int_attr("attr_int", 10),
|
|
make_bool_attr("attr_bool", true),
|
|
],
|
|
)],
|
|
);
|
|
let res = send_trace_v1_req(&client, coercion_table_name, coercion_req, false).await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let coercion_req = make_trace_v1_request(
|
|
"type-coercion",
|
|
vec![make_trace_v1_span(
|
|
"00000000000000000000000000000002",
|
|
"0000000000000002",
|
|
"coercion-apply",
|
|
1_736_480_942_444_589_000,
|
|
1_736_480_942_444_712_000,
|
|
vec![
|
|
make_int_attr("attr_float", 2),
|
|
make_string_attr("attr_int", "20"),
|
|
make_string_attr("attr_bool", "false"),
|
|
],
|
|
)],
|
|
);
|
|
let res = send_trace_v1_req(&client, coercion_table_name, coercion_req, false).await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let string_target_table_name = "trace_type_coercion_to_string";
|
|
let string_target_seed_req = make_trace_v1_request(
|
|
"type-coercion-string",
|
|
vec![make_trace_v1_span(
|
|
"00000000000000000000000000000021",
|
|
"0000000000000021",
|
|
"string-target-seed",
|
|
1_736_480_942_444_720_000,
|
|
1_736_480_942_444_820_000,
|
|
vec![
|
|
make_string_attr("attr_int", "seed"),
|
|
make_string_attr("attr_float", "seed"),
|
|
make_string_attr("attr_bool", "seed"),
|
|
],
|
|
)],
|
|
);
|
|
let res = send_trace_v1_req(
|
|
&client,
|
|
string_target_table_name,
|
|
string_target_seed_req,
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let string_target_req = make_trace_v1_request(
|
|
"type-coercion-string",
|
|
vec![make_trace_v1_span(
|
|
"00000000000000000000000000000022",
|
|
"0000000000000022",
|
|
"string-target-apply",
|
|
1_736_480_942_444_830_000,
|
|
1_736_480_942_444_930_000,
|
|
vec![
|
|
make_int_attr("attr_int", 20),
|
|
make_double_attr("attr_float", 2.5),
|
|
make_bool_attr("attr_bool", false),
|
|
],
|
|
)],
|
|
);
|
|
let res = send_trace_v1_req(&client, string_target_table_name, string_target_req, false).await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
validate_data(
|
|
"otlp_traces_v1_type_coercion_to_string_rows",
|
|
&client,
|
|
&format!(
|
|
"select trace_id, \"span_attributes.attr_bool\", \"span_attributes.attr_float\", \"span_attributes.attr_int\" from {} order by trace_id;",
|
|
string_target_table_name
|
|
),
|
|
r#"[["00000000000000000000000000000021","seed","seed","seed"],["00000000000000000000000000000022","false","2.5","20"]]"#,
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_traces_v1_type_coercion_to_string_schema",
|
|
&client,
|
|
"select column_name, lower(data_type), semantic_type from information_schema.columns where table_name = 'trace_type_coercion_to_string' and column_name in ('span_attributes.attr_bool', 'span_attributes.attr_float', 'span_attributes.attr_int') order by column_name;",
|
|
r#"[["span_attributes.attr_bool","string","FIELD"],["span_attributes.attr_float","string","FIELD"],["span_attributes.attr_int","string","FIELD"]]"#,
|
|
)
|
|
.await;
|
|
|
|
let intra_batch_prefer_non_string_table_name = "trace_type_prefer_non_string";
|
|
let intra_batch_prefer_non_string_req = make_trace_v1_request(
|
|
"type-prefer-non-string",
|
|
vec![
|
|
make_trace_v1_span(
|
|
"00000000000000000000000000000031",
|
|
"0000000000000031",
|
|
"prefer-non-string-seed",
|
|
1_736_480_942_444_940_000,
|
|
1_736_480_942_445_040_000,
|
|
vec![
|
|
make_string_attr("attr_int", "10"),
|
|
make_string_attr("attr_float", "1.5"),
|
|
make_string_attr("attr_bool", "true"),
|
|
],
|
|
),
|
|
make_trace_v1_span(
|
|
"00000000000000000000000000000032",
|
|
"0000000000000032",
|
|
"prefer-non-string-apply",
|
|
1_736_480_942_445_050_000,
|
|
1_736_480_942_445_150_000,
|
|
vec![
|
|
make_int_attr("attr_int", 20),
|
|
make_double_attr("attr_float", 2.5),
|
|
make_bool_attr("attr_bool", false),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
let res = send_trace_v1_req(
|
|
&client,
|
|
intra_batch_prefer_non_string_table_name,
|
|
intra_batch_prefer_non_string_req,
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
validate_data(
|
|
"otlp_traces_v1_prefer_non_string_rows",
|
|
&client,
|
|
&format!(
|
|
"select trace_id, \"span_attributes.attr_bool\", \"span_attributes.attr_float\", \"span_attributes.attr_int\" from {} order by trace_id;",
|
|
intra_batch_prefer_non_string_table_name
|
|
),
|
|
r#"[["00000000000000000000000000000031",true,1.5,10],["00000000000000000000000000000032",false,2.5,20]]"#,
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_traces_v1_prefer_non_string_schema",
|
|
&client,
|
|
"select column_name, lower(data_type), semantic_type from information_schema.columns where table_name = 'trace_type_prefer_non_string' and column_name in ('span_attributes.attr_bool', 'span_attributes.attr_float', 'span_attributes.attr_int') order by column_name;",
|
|
r#"[["span_attributes.attr_bool","boolean","FIELD"],["span_attributes.attr_float","double","FIELD"],["span_attributes.attr_int","bigint","FIELD"]]"#,
|
|
)
|
|
.await;
|
|
|
|
let existing_float_table_name = "trace_type_existing_float_prefers_schema";
|
|
let existing_float_seed_req = make_trace_v1_request(
|
|
"type-existing-float",
|
|
vec![make_trace_v1_span(
|
|
"00000000000000000000000000000041",
|
|
"0000000000000041",
|
|
"existing-float-seed",
|
|
1_736_480_942_445_160_000,
|
|
1_736_480_942_445_260_000,
|
|
vec![make_double_attr("attr_num", 1.25)],
|
|
)],
|
|
);
|
|
let res = send_trace_v1_req(
|
|
&client,
|
|
existing_float_table_name,
|
|
existing_float_seed_req,
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let existing_float_req = make_trace_v1_request(
|
|
"type-existing-float",
|
|
vec![
|
|
make_trace_v1_span(
|
|
"00000000000000000000000000000042",
|
|
"0000000000000042",
|
|
"existing-float-int-first",
|
|
1_736_480_942_445_270_000,
|
|
1_736_480_942_445_370_000,
|
|
vec![make_int_attr("attr_num", 2)],
|
|
),
|
|
make_trace_v1_span(
|
|
"00000000000000000000000000000043",
|
|
"0000000000000043",
|
|
"existing-float-float-later",
|
|
1_736_480_942_445_380_000,
|
|
1_736_480_942_445_480_000,
|
|
vec![make_double_attr("attr_num", 3.5)],
|
|
),
|
|
],
|
|
);
|
|
let res = send_trace_v1_req(
|
|
&client,
|
|
existing_float_table_name,
|
|
existing_float_req,
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
validate_data(
|
|
"otlp_traces_v1_existing_float_prefers_schema_rows",
|
|
&client,
|
|
&format!(
|
|
"select trace_id, \"span_attributes.attr_num\" from {} order by trace_id;",
|
|
existing_float_table_name
|
|
),
|
|
r#"[["00000000000000000000000000000041",1.25],["00000000000000000000000000000042",2.0],["00000000000000000000000000000043",3.5]]"#,
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_traces_v1_existing_float_prefers_schema_type",
|
|
&client,
|
|
"select column_name, lower(data_type), semantic_type from information_schema.columns where table_name = 'trace_type_existing_float_prefers_schema' and column_name = 'span_attributes.attr_num';",
|
|
r#"[["span_attributes.attr_num","double","FIELD"]]"#,
|
|
)
|
|
.await;
|
|
|
|
let existing_int_table_name = "trace_type_existing_int_widens_to_float";
|
|
let existing_int_seed_req = make_trace_v1_request(
|
|
"type-existing-int",
|
|
vec![make_trace_v1_span(
|
|
"00000000000000000000000000000051",
|
|
"0000000000000051",
|
|
"existing-int-seed",
|
|
1_736_480_942_445_490_000,
|
|
1_736_480_942_445_590_000,
|
|
vec![make_int_attr("attr_num", 1)],
|
|
)],
|
|
);
|
|
let res = send_trace_v1_req(
|
|
&client,
|
|
existing_int_table_name,
|
|
existing_int_seed_req,
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let existing_int_req = make_trace_v1_request(
|
|
"type-existing-int",
|
|
vec![
|
|
make_trace_v1_span(
|
|
"00000000000000000000000000000052",
|
|
"0000000000000052",
|
|
"existing-int-upcast-int",
|
|
1_736_480_942_445_600_000,
|
|
1_736_480_942_445_700_000,
|
|
vec![make_int_attr("attr_num", 2)],
|
|
),
|
|
make_trace_v1_span(
|
|
"00000000000000000000000000000053",
|
|
"0000000000000053",
|
|
"existing-int-upcast-float",
|
|
1_736_480_942_445_710_000,
|
|
1_736_480_942_445_810_000,
|
|
vec![make_double_attr("attr_num", 3.5)],
|
|
),
|
|
],
|
|
);
|
|
let res = send_trace_v1_req(&client, existing_int_table_name, existing_int_req, false).await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
// Trace schema reconciliation widens the existing column through a direct
|
|
// alter-table call, so it must identify itself as an OTLP auto alteration.
|
|
wait_for_event_data(
|
|
&client,
|
|
"SELECT DISTINCT json_to_string(event_context) FROM greptime_private.events WHERE type = 'alter_table' AND json_get_string(procedure_trigger, 'type') = 'Submitted' AND json_get_string(event_context, 'protocol') = 'otlp' AND json_get_string(event_context, 'reason') = 'auto_alter'",
|
|
r#"[["{\"protocol\":\"otlp\",\"reason\":\"auto_alter\"}"]]"#,
|
|
)
|
|
.await;
|
|
|
|
validate_data(
|
|
"otlp_traces_v1_existing_int_widens_rows",
|
|
&client,
|
|
&format!(
|
|
"select trace_id, \"span_attributes.attr_num\" from {} order by trace_id;",
|
|
existing_int_table_name
|
|
),
|
|
r#"[["00000000000000000000000000000051",1.0],["00000000000000000000000000000052",2.0],["00000000000000000000000000000053",3.5]]"#,
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_traces_v1_existing_int_widens_type",
|
|
&client,
|
|
"select column_name, lower(data_type), semantic_type from information_schema.columns where table_name = 'trace_type_existing_int_widens_to_float' and column_name = 'span_attributes.attr_num';",
|
|
r#"[["span_attributes.attr_num","double","FIELD"]]"#,
|
|
)
|
|
.await;
|
|
|
|
let existing_int_atomic_table_name = "trace_type_existing_int_widen_atomic";
|
|
let existing_int_atomic_seed_req = make_trace_v1_request(
|
|
"type-existing-int-atomic",
|
|
vec![make_trace_v1_span(
|
|
"00000000000000000000000000000054",
|
|
"0000000000000054",
|
|
"existing-int-atomic-seed",
|
|
1_736_480_942_445_720_000,
|
|
1_736_480_942_445_820_000,
|
|
vec![
|
|
make_int_attr("attr_num", 1),
|
|
make_int_attr("attr_parse", 10),
|
|
],
|
|
)],
|
|
);
|
|
let res = send_trace_v1_req(
|
|
&client,
|
|
existing_int_atomic_table_name,
|
|
existing_int_atomic_seed_req,
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let existing_int_atomic_req = make_trace_v1_request(
|
|
"type-existing-int-atomic",
|
|
vec![make_trace_v1_span(
|
|
"00000000000000000000000000000055",
|
|
"0000000000000055",
|
|
"existing-int-atomic-invalid",
|
|
1_736_480_942_445_830_000,
|
|
1_736_480_942_445_930_000,
|
|
vec![
|
|
make_double_attr("attr_num", 3.5),
|
|
make_string_attr("attr_parse", "not_a_number"),
|
|
],
|
|
)],
|
|
);
|
|
let res = send_trace_v1_req(
|
|
&client,
|
|
existing_int_atomic_table_name,
|
|
existing_int_atomic_req,
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::BAD_REQUEST, res.status());
|
|
let body = res.bytes().await;
|
|
let status = GoogleRpcStatus::decode(body.as_ref()).unwrap();
|
|
assert!(
|
|
status
|
|
.message
|
|
.contains("Accepted 0 spans, rejected 1 spans"),
|
|
"unexpected error body: {status:?}"
|
|
);
|
|
|
|
validate_data(
|
|
"otlp_traces_v1_existing_int_widen_atomic_rows",
|
|
&client,
|
|
&format!(
|
|
"select trace_id, \"span_attributes.attr_num\", \"span_attributes.attr_parse\" from {} order by trace_id;",
|
|
existing_int_atomic_table_name
|
|
),
|
|
r#"[["00000000000000000000000000000054",1,10]]"#,
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_traces_v1_existing_int_widen_atomic_types",
|
|
&client,
|
|
"select column_name, lower(data_type), semantic_type from information_schema.columns where table_name = 'trace_type_existing_int_widen_atomic' and column_name in ('span_attributes.attr_num', 'span_attributes.attr_parse') order by column_name;",
|
|
r#"[["span_attributes.attr_num","bigint","FIELD"],["span_attributes.attr_parse","bigint","FIELD"]]"#,
|
|
)
|
|
.await;
|
|
|
|
let existing_int_float_only_table_name = "trace_type_existing_int_float_only";
|
|
let existing_int_float_only_seed_req = make_trace_v1_request(
|
|
"type-existing-int-float-only",
|
|
vec![make_trace_v1_span(
|
|
"00000000000000000000000000000061",
|
|
"0000000000000061",
|
|
"existing-int-float-only-seed",
|
|
1_736_480_942_445_820_000,
|
|
1_736_480_942_445_920_000,
|
|
vec![make_int_attr("attr_num", 1)],
|
|
)],
|
|
);
|
|
let res = send_trace_v1_req(
|
|
&client,
|
|
existing_int_float_only_table_name,
|
|
existing_int_float_only_seed_req,
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let existing_int_float_only_req = make_trace_v1_request(
|
|
"type-existing-int-float-only",
|
|
vec![make_trace_v1_span(
|
|
"00000000000000000000000000000062",
|
|
"0000000000000062",
|
|
"existing-int-float-only-apply",
|
|
1_736_480_942_445_930_000,
|
|
1_736_480_942_446_030_000,
|
|
vec![make_double_attr("attr_num", 2.5)],
|
|
)],
|
|
);
|
|
let res = send_trace_v1_req(
|
|
&client,
|
|
existing_int_float_only_table_name,
|
|
existing_int_float_only_req,
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
validate_data(
|
|
"otlp_traces_v1_existing_int_float_only_rows",
|
|
&client,
|
|
&format!(
|
|
"select trace_id, \"span_attributes.attr_num\" from {} order by trace_id;",
|
|
existing_int_float_only_table_name
|
|
),
|
|
r#"[["00000000000000000000000000000061",1.0],["00000000000000000000000000000062",2.5]]"#,
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_traces_v1_existing_int_float_only_type",
|
|
&client,
|
|
"select column_name, lower(data_type), semantic_type from information_schema.columns where table_name = 'trace_type_existing_int_float_only' and column_name = 'span_attributes.attr_num';",
|
|
r#"[["span_attributes.attr_num","double","FIELD"]]"#,
|
|
)
|
|
.await;
|
|
|
|
validate_data(
|
|
"otlp_traces_v1_type_coercion_rows",
|
|
&client,
|
|
&format!(
|
|
"select trace_id, \"span_attributes.attr_int\", \"span_attributes.attr_bool\" from {} order by trace_id;",
|
|
coercion_table_name
|
|
),
|
|
r#"[["00000000000000000000000000000001",10,true],["00000000000000000000000000000002",20,false]]"#,
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_traces_v1_type_coercion_float_sum",
|
|
&client,
|
|
&format!(
|
|
"select sum(\"span_attributes.attr_float\") from {};",
|
|
coercion_table_name
|
|
),
|
|
r#"[[3.5]]"#,
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_traces_v1_type_coercion_schema",
|
|
&client,
|
|
"select column_name, lower(data_type), semantic_type from information_schema.columns where table_name = 'trace_type_coercion' and column_name in ('span_attributes.attr_bool', 'span_attributes.attr_float', 'span_attributes.attr_int') order by column_name;",
|
|
r#"[["span_attributes.attr_bool","boolean","FIELD"],["span_attributes.attr_float","double","FIELD"],["span_attributes.attr_int","bigint","FIELD"]]"#,
|
|
)
|
|
.await;
|
|
|
|
let abort_table_name = "trace_type_abort";
|
|
let abort_seed_req = make_trace_v1_request(
|
|
"type-abort",
|
|
vec![make_trace_v1_span(
|
|
"00000000000000000000000000000011",
|
|
"0000000000000011",
|
|
"abort-seed",
|
|
1_736_480_942_444_800_000,
|
|
1_736_480_942_444_900_000,
|
|
vec![make_int_attr("attr_int", 10)],
|
|
)],
|
|
);
|
|
let res = send_trace_v1_req(&client, abort_table_name, abort_seed_req, false).await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let abort_req = make_trace_v1_request(
|
|
"type-abort",
|
|
vec![
|
|
make_trace_v1_span(
|
|
"00000000000000000000000000000012",
|
|
"0000000000000012",
|
|
"abort-parseable",
|
|
1_736_480_942_445_000_000,
|
|
1_736_480_942_445_100_000,
|
|
vec![make_string_attr("attr_int", "20")],
|
|
),
|
|
make_trace_v1_span(
|
|
"00000000000000000000000000000013",
|
|
"0000000000000013",
|
|
"abort-unparsable",
|
|
1_736_480_942_445_200_000,
|
|
1_736_480_942_445_300_000,
|
|
vec![make_string_attr("attr_int", "not_a_number")],
|
|
),
|
|
],
|
|
);
|
|
let res = send_trace_v1_req(&client, abort_table_name, abort_req, false).await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let body = ExportTraceServiceResponse::decode(res.bytes().await).unwrap();
|
|
let partial_success = body.partial_success.as_ref().unwrap();
|
|
assert_eq!(partial_success.rejected_spans, 1);
|
|
assert!(
|
|
partial_success
|
|
.error_message
|
|
.contains("Accepted 1 spans, rejected 1 spans"),
|
|
"unexpected partial success body: {body:?}"
|
|
);
|
|
assert!(
|
|
partial_success.error_message.contains(
|
|
"Rejected span 00000000000000000000000000000013:0000000000000013 (InvalidArguments)"
|
|
),
|
|
"unexpected partial success body: {body:?}"
|
|
);
|
|
// The rejection must name the column, the failing value and the target type,
|
|
// otherwise locating the bad attribute needs a collector-side capture.
|
|
assert!(
|
|
partial_success.error_message.contains(
|
|
"failed to coerce trace column 'span_attributes.attr_int' in table \
|
|
'trace_type_abort' from String(\"not_a_number\") to Int64"
|
|
),
|
|
"unexpected partial success body: {body:?}"
|
|
);
|
|
|
|
validate_data(
|
|
"otlp_traces_v1_type_abort_rows",
|
|
&client,
|
|
&format!(
|
|
"select trace_id, \"span_attributes.attr_int\" from {} order by trace_id",
|
|
abort_table_name
|
|
),
|
|
r#"[["00000000000000000000000000000011",10],["00000000000000000000000000000012",20]]"#,
|
|
)
|
|
.await;
|
|
|
|
let chunk_failure_req = make_trace_v1_request(
|
|
"type-discard",
|
|
vec![
|
|
make_trace_v1_span(
|
|
"00000000000000000000000000000021",
|
|
"0000000000000021",
|
|
"discard-one",
|
|
1_736_480_942_445_400_000,
|
|
1_736_480_942_445_500_000,
|
|
vec![make_string_attr("attr_text", "alpha")],
|
|
),
|
|
make_trace_v1_span(
|
|
"00000000000000000000000000000022",
|
|
"0000000000000022",
|
|
"discard-two",
|
|
1_736_480_942_445_600_000,
|
|
1_736_480_942_445_700_000,
|
|
vec![make_string_attr("attr_text", "beta")],
|
|
),
|
|
],
|
|
);
|
|
let res = send_trace_v1_req_with_db(
|
|
&client,
|
|
"nonexistent",
|
|
"trace_chunk_discard",
|
|
chunk_failure_req,
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::BAD_REQUEST, res.status());
|
|
let body = GoogleRpcStatus::decode(res.bytes().await.as_ref()).unwrap();
|
|
assert!(
|
|
body.message.contains("Accepted 0 spans, rejected 2 spans"),
|
|
"unexpected error body: {body:?}"
|
|
);
|
|
assert!(
|
|
body.message
|
|
.contains("Discarded 2 spans after pre-write request failure"),
|
|
"unexpected error body: {body:?}"
|
|
);
|
|
|
|
// Raw bytes and JSONB values of `x` from different resources share one chunk.
|
|
// That conflict must not keep the unrelated resource `c` out of schema
|
|
// planning: `n` is planned as String, so `c`'s value is not coerced to the
|
|
// Int64 that per-span writes would pick from resource `a` first.
|
|
let mixed_table_name = "trace_type_mixed_binary_json";
|
|
let mixed_resources = [
|
|
(
|
|
"mixed-a",
|
|
vec![
|
|
json!({"key": "x", "value": {"bytesValue": "AQI="}}),
|
|
make_int_attr("n", 1),
|
|
],
|
|
),
|
|
(
|
|
"mixed-b",
|
|
vec![json!({"key": "x", "value": {"arrayValue": {"values": [{"intValue": "1"}]}}})],
|
|
),
|
|
("mixed-c", vec![make_string_attr("n", "abc")]),
|
|
];
|
|
let mut mixed_req = ExportTraceServiceRequest::default();
|
|
for (i, (service_name, attributes)) in mixed_resources.into_iter().enumerate() {
|
|
let id = 0x71 + i;
|
|
mixed_req.resource_spans.extend(
|
|
make_trace_v1_request(
|
|
service_name,
|
|
vec![make_trace_v1_span(
|
|
&format!("{id:032x}"),
|
|
&format!("{id:016x}"),
|
|
service_name,
|
|
1_736_480_942_446_000_000,
|
|
1_736_480_942_446_100_000,
|
|
attributes,
|
|
)],
|
|
)
|
|
.resource_spans,
|
|
);
|
|
}
|
|
let res = send_trace_v1_req(&client, mixed_table_name, mixed_req, false).await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
validate_data(
|
|
"otlp_traces_v1_mixed_binary_json_rows",
|
|
&client,
|
|
&format!(
|
|
"select service_name, \"span_attributes.n\" from {} order by service_name",
|
|
mixed_table_name
|
|
),
|
|
r#"[["mixed-a","1"],["mixed-c","abc"]]"#,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
#[apply(both_deployment_cases)]
|
|
async fn test_otlp_logs_skip_wal(distributed: bool) {
|
|
let mut cases = Vec::new();
|
|
let logs = make_log_request(vec![json!({
|
|
"timeUnixNano": "1700000000000000000",
|
|
"body": {"stringValue": "wal test"}
|
|
})]);
|
|
cases.push(HttpWalCase::new(
|
|
"OTLP logs",
|
|
"/v1/otlp/v1/logs",
|
|
"application/x-protobuf",
|
|
logs.encode_to_vec(),
|
|
));
|
|
check_http_skip_wal("otlp_logs", &cases, distributed).await;
|
|
}
|
|
|
|
pub async fn test_otlp_logs(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_http_app_with_frontend(store_type, "test_otlp_logs").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
let content = r#"
|
|
{"resourceLogs":[{"resource":{"attributes":[],"droppedAttributesCount":0},"scopeLogs":[{"scope":{"name":"","version":"","attributes":[{"key":"instance_num","value":{"stringValue":"10"}}],"droppedAttributesCount":0},"logRecords":[{"timeUnixNano":"1736413568497632000","observedTimeUnixNano":"0","severityNumber":9,"severityText":"Info","body":{"stringValue":"the message line one"},"attributes":[{"key":"app","value":{"stringValue":"server1"}}],"droppedAttributesCount":0,"flags":0,"traceId":"f665100a612542b69cc362fe2ae9d3bf","spanId":"e58f01c4c69f4488"}],"schemaUrl":""}],"schemaUrl":"https://opentelemetry.io/schemas/1.4.0"},{"resource":{"attributes":[],"droppedAttributesCount":0},"scopeLogs":[{"scope":{"name":"","version":"","attributes":[],"droppedAttributesCount":0},"logRecords":[{"timeUnixNano":"1736413568538897000","observedTimeUnixNano":"0","severityNumber":9,"severityText":"Info","body":{"stringValue":"the message line two"},"attributes":[{"key":"app","value":{"stringValue":"server2"}}],"droppedAttributesCount":0,"flags":0,"traceId":"f665100a612542b69cc362fe2ae9d3bf","spanId":"e58f01c4c69f4488"}],"schemaUrl":""}],"schemaUrl":"https://opentelemetry.io/schemas/1.4.0"}]}
|
|
"#;
|
|
|
|
let req: ExportLogsServiceRequest = serde_json::from_str(content).unwrap();
|
|
let body = req.encode_to_vec();
|
|
|
|
{
|
|
// write log data
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
)],
|
|
"/v1/otlp/v1/logs?db=public",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected = "[[1736413568497632000,\"f665100a612542b69cc362fe2ae9d3bf\",\"e58f01c4c69f4488\",\"Info\",9,\"the message line one\",{\"app\":\"server1\"},0,null,\"\",\"\",{\"instance_num\":\"10\"},\"\",{},\"https://opentelemetry.io/schemas/1.4.0\",null,null],[1736413568538897000,\"f665100a612542b69cc362fe2ae9d3bf\",\"e58f01c4c69f4488\",\"Info\",9,\"the message line two\",{\"app\":\"server2\"},0,null,\"\",\"\",{},\"\",{},\"https://opentelemetry.io/schemas/1.4.0\",null,null]]";
|
|
validate_data(
|
|
"otlp_logs",
|
|
&client,
|
|
"select * from opentelemetry_logs;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// The attribute columns are JSON, whose SST encoding differs from their
|
|
// memtable representation.
|
|
flush_table(&client, "opentelemetry_logs").await;
|
|
validate_data(
|
|
"otlp_logs_after_flush",
|
|
&client,
|
|
"select * from opentelemetry_logs;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// The auto-created log table carries the log identity.
|
|
validate_data(
|
|
"otlp_logs_semantic_identity",
|
|
&client,
|
|
"select count(*) from information_schema.tables where table_name = 'opentelemetry_logs' \
|
|
and create_options like '%greptime.semantic.signal_type=log%' \
|
|
and create_options like '%greptime.semantic.source=opentelemetry%' \
|
|
and create_options like '%table_data_model=greptime_log_v1%' \
|
|
and create_options like '%greptime.semantic.pipeline=greptime_log_v1%';",
|
|
"[[1]]",
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_logs_v1_keys_and_indexes",
|
|
&client,
|
|
"select column_name, constraint_name, ordinal_position from information_schema.key_column_usage \
|
|
where table_name = 'opentelemetry_logs' order by constraint_name, ordinal_position, column_name;",
|
|
r#"[["service_name","PRIMARY",1],["scope_name","PRIMARY",2],["timestamp","TIME INDEX",1]]"#,
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_logs_v1_column_indexes",
|
|
&client,
|
|
"select index_name, column_name from information_schema.statistics \
|
|
where table_name = 'opentelemetry_logs' and index_name like '%INDEX_%' order by index_name;",
|
|
r#"[["FULLTEXT_INDEX_body","body"],["SKIPPING_INDEX_trace_id","trace_id"]]"#,
|
|
)
|
|
.await;
|
|
|
|
// Write another batch after the table exists. This verifies existing-schema
|
|
// alignment accepts the built-in JSONB columns in direct OTLP logs.
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
)],
|
|
"/v1/otlp/v1/logs?db=public",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
validate_data(
|
|
"otlp_logs_multiple_batches",
|
|
&client,
|
|
"select count(*) from opentelemetry_logs;",
|
|
"[[4]]",
|
|
)
|
|
.await;
|
|
}
|
|
|
|
{
|
|
// write log data with selector
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-log-table-name"),
|
|
HeaderValue::from_static("cus_logs"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-log-extract-keys"),
|
|
HeaderValue::from_static("resource-attr,instance_num,app,not-exist"),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/logs?db=public",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let expected = "[[1736413568538897000,\"f665100a612542b69cc362fe2ae9d3bf\",\"e58f01c4c69f4488\",\"Info\",9,\"the message line two\",{\"app\":\"server2\"},0,null,\"\",\"\",{},\"\",{},\"https://opentelemetry.io/schemas/1.4.0\",null,null,null,\"server2\"],[1736413568497632000,\"f665100a612542b69cc362fe2ae9d3bf\",\"e58f01c4c69f4488\",\"Info\",9,\"the message line one\",{\"app\":\"server1\"},0,null,\"\",\"\",{\"instance_num\":\"10\"},\"\",{},\"https://opentelemetry.io/schemas/1.4.0\",null,null,\"10\",\"server1\"]]";
|
|
validate_data(
|
|
"otlp_logs_with_selector",
|
|
&client,
|
|
"select * from cus_logs;",
|
|
expected,
|
|
)
|
|
.await;
|
|
}
|
|
|
|
{
|
|
// test same selector with multiple value
|
|
let content = r#"
|
|
{"resourceLogs":[{"resource":{"attributes":[{"key":"fromwhere","value":{"stringValue":"resource"}}],"droppedAttributesCount":0},"scopeLogs":[{"scope":{"name":"","version":"","attributes":[{"key":"fromwhere","value":{"stringValue":"scope"}}],"droppedAttributesCount":0},"logRecords":[{"timeUnixNano":"1736413568497632000","observedTimeUnixNano":"0","severityNumber":9,"severityText":"Info","body":{"stringValue":"the message line one"},"attributes":[{"key":"app","value":{"stringValue":"server"}},{"key":"fromwhere","value":{"stringValue":"log_attr"}}],"droppedAttributesCount":0,"flags":0,"traceId":"f665100a612542b69cc362fe2ae9d3bf","spanId":"e58f01c4c69f4488"}],"schemaUrl":""}],"schemaUrl":"https://opentelemetry.io/schemas/1.4.0"}]}
|
|
"#;
|
|
let req: ExportLogsServiceRequest = serde_json::from_str(content).unwrap();
|
|
let body = req.encode_to_vec();
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-log-table-name"),
|
|
HeaderValue::from_static("logs2"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-log-extract-keys"),
|
|
HeaderValue::from_static("fromwhere"),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/logs?db=public",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let expected = "[[1736413568497632000,\"f665100a612542b69cc362fe2ae9d3bf\",\"e58f01c4c69f4488\",\"Info\",9,\"the message line one\",{\"app\":\"server\",\"fromwhere\":\"log_attr\"},0,null,\"\",\"\",{\"fromwhere\":\"scope\"},\"\",{\"fromwhere\":\"resource\"},\"https://opentelemetry.io/schemas/1.4.0\",null,null,\"log_attr\"]]";
|
|
validate_data(
|
|
"otlp_logs_with_selector_overlapping",
|
|
&client,
|
|
"select * from logs2;",
|
|
expected,
|
|
)
|
|
.await;
|
|
}
|
|
|
|
{
|
|
// The built-in `service_name` comes from the resource `service.name`, and log
|
|
// tables declare no entity.
|
|
let table_name = "otlp_logs_semantic";
|
|
let now_nanos = common_time::util::current_time_millis() * 1_000_000;
|
|
let mut record = make_log_record(
|
|
"00000000000000000000000000000005",
|
|
"0000000000000005",
|
|
now_nanos,
|
|
"semantic layer",
|
|
vec![],
|
|
);
|
|
record["eventName"] = json!("checkout.completed");
|
|
record["observedTimeUnixNano"] = json!((now_nanos + 1).to_string());
|
|
let mut req = make_log_request(vec![record]);
|
|
req.resource_logs[0].resource.as_mut().unwrap().attributes =
|
|
serde_json::from_value(json!([
|
|
{"key": "service.name", "value": {"stringValue": "checkout"}}
|
|
]))
|
|
.unwrap();
|
|
// The first write creates the v1 table; the second reads its marker back.
|
|
for _ in 0..2 {
|
|
let res = send_log_req(&client, table_name, None, req.clone(), false).await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
}
|
|
|
|
validate_data(
|
|
"otlp_logs_semantic_table_semantics",
|
|
&client,
|
|
&format!(
|
|
"select signal_type, source, pipeline, entity_declarations from information_schema.table_semantics where table_name = '{table_name}';"
|
|
),
|
|
r#"[["log","opentelemetry","greptime_log_v1",null]]"#,
|
|
)
|
|
.await;
|
|
let row = format!(r#"["checkout","checkout.completed",{}]"#, now_nanos + 1);
|
|
validate_data(
|
|
"otlp_logs_semantic_v1_columns",
|
|
&client,
|
|
&format!("select service_name, event_name, observed_timestamp from {table_name};"),
|
|
&format!("[{row},{row}]"),
|
|
)
|
|
.await;
|
|
}
|
|
|
|
{
|
|
let existing_table_name = "otlp_logs_existing_schema";
|
|
let res = execute_sql(
|
|
&client,
|
|
&format!(
|
|
"create table {existing_table_name} (\"timestamp\" timestamp(3) time index, trace_id string, host string, body string, primary key(trace_id, host))"
|
|
),
|
|
)
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
|
|
let req = make_log_request(vec![make_log_record(
|
|
"00000000000000000000000000000001",
|
|
"0000000000000001",
|
|
1_736_413_568_497_632_000,
|
|
"existing schema",
|
|
vec![make_int_attr("host", 42)],
|
|
)]);
|
|
let res = send_log_req(&client, existing_table_name, Some("host"), req, false).await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
|
|
validate_data(
|
|
"otlp_logs_existing_schema_rows",
|
|
&client,
|
|
&format!(
|
|
"select trace_id, host, body, \"timestamp\" from {existing_table_name} order by trace_id;"
|
|
),
|
|
r#"[["00000000000000000000000000000001","42","existing schema",1736413568497]]"#,
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_logs_existing_schema_semantic_types",
|
|
&client,
|
|
"select column_name, semantic_type from information_schema.columns where table_name = 'otlp_logs_existing_schema' and column_name in ('timestamp', 'trace_id', 'host', 'scope_name') order by column_name;",
|
|
r#"[["host","TAG"],["scope_name","FIELD"],["timestamp","TIMESTAMP"],["trace_id","TAG"]]"#,
|
|
)
|
|
.await;
|
|
// Tables without the v1 marker keep the legacy built-in columns.
|
|
validate_data(
|
|
"otlp_logs_existing_schema_no_v1_columns",
|
|
&client,
|
|
"select count(*) from information_schema.columns where table_name = 'otlp_logs_existing_schema' and column_name in ('service_name', 'event_name', 'observed_timestamp');",
|
|
"[[0]]",
|
|
)
|
|
.await;
|
|
}
|
|
|
|
{
|
|
let existing_field_table_name = "otlp_logs_existing_string_field";
|
|
let res = execute_sql(
|
|
&client,
|
|
&format!(
|
|
"create table {existing_field_table_name} (\"timestamp\" timestamp(3) time index, host string, body string)"
|
|
),
|
|
)
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
|
|
let req = make_log_request(vec![make_log_record(
|
|
"00000000000000000000000000000004",
|
|
"0000000000000004",
|
|
1_736_413_568_497_700_000,
|
|
"existing string field",
|
|
vec![make_int_attr("host", 42)],
|
|
)]);
|
|
let res = send_log_req(&client, existing_field_table_name, Some("host"), req, false).await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
|
|
validate_data(
|
|
"otlp_logs_existing_string_field_rows",
|
|
&client,
|
|
&format!(
|
|
"select host, body, \"timestamp\" from {existing_field_table_name} order by body;"
|
|
),
|
|
r#"[["42","existing string field",1736413568497]]"#,
|
|
)
|
|
.await;
|
|
validate_data(
|
|
"otlp_logs_existing_string_field_semantic_type",
|
|
&client,
|
|
"select column_name, semantic_type from information_schema.columns where table_name = 'otlp_logs_existing_string_field' and column_name = 'host';",
|
|
r#"[["host","FIELD"]]"#,
|
|
)
|
|
.await;
|
|
}
|
|
|
|
{
|
|
let missing_pk_table_name = "otlp_logs_missing_pk";
|
|
let res = execute_sql(
|
|
&client,
|
|
&format!(
|
|
"create table {missing_pk_table_name} (\"timestamp\" timestamp(9) time index, host string, body string, primary key(host))"
|
|
),
|
|
)
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
|
|
let req = make_log_request(vec![make_log_record(
|
|
"00000000000000000000000000000002",
|
|
"0000000000000002",
|
|
1_736_413_568_498_000_000,
|
|
"missing host",
|
|
vec![make_string_attr("not_host", "node-a")],
|
|
)]);
|
|
let res = send_log_req(&client, missing_pk_table_name, None, req, false).await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
|
|
validate_data(
|
|
"otlp_logs_missing_pk_rows",
|
|
&client,
|
|
&format!("select host, body from {missing_pk_table_name};"),
|
|
r#"[[null,"missing host"]]"#,
|
|
)
|
|
.await;
|
|
}
|
|
|
|
{
|
|
let incompatible_table_name = "otlp_logs_incompatible_schema";
|
|
let res = execute_sql(
|
|
&client,
|
|
&format!(
|
|
"create table {incompatible_table_name} (\"timestamp\" timestamp(9) time index, host bigint, primary key(host))"
|
|
),
|
|
)
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
|
|
let req = make_log_request(vec![make_log_record(
|
|
"00000000000000000000000000000003",
|
|
"0000000000000003",
|
|
1_736_413_568_499_000_000,
|
|
"bad host",
|
|
vec![make_string_attr("host", "node-a")],
|
|
)]);
|
|
let res = send_log_req(&client, incompatible_table_name, Some("host"), req, false).await;
|
|
let status = res.status();
|
|
let body = res.text().await;
|
|
assert_eq!(status, StatusCode::BAD_REQUEST, "{body}");
|
|
assert!(
|
|
body.contains("failed to align log column 'host'"),
|
|
"unexpected error body: {body}"
|
|
);
|
|
validate_data(
|
|
"otlp_logs_incompatible_schema_rows",
|
|
&client,
|
|
&format!("select count(*) from {incompatible_table_name};"),
|
|
"[[0]]",
|
|
)
|
|
.await;
|
|
}
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
#[apply(both_deployment_cases)]
|
|
async fn test_loki_skip_wal(distributed: bool) {
|
|
let mut cases = vec![HttpWalCase::new(
|
|
"Loki JSON",
|
|
"/v1/loki/api/v1/push",
|
|
"application/json",
|
|
r#"{"streams":[{"stream":{"host":"a"},"values":[["1700000000000000000","wal test"]]}]}"#,
|
|
)];
|
|
let loki = loki_proto::logproto::PushRequest {
|
|
streams: vec![loki_proto::logproto::StreamAdapter {
|
|
labels: r#"{host="a"}"#.to_string(),
|
|
entries: vec![loki_proto::logproto::EntryAdapter {
|
|
timestamp: Some(loki_proto::prost_types::Timestamp {
|
|
seconds: 1700000000,
|
|
nanos: 0,
|
|
}),
|
|
line: "wal test".to_string(),
|
|
..Default::default()
|
|
}],
|
|
..Default::default()
|
|
}],
|
|
};
|
|
cases.push(HttpWalCase::new(
|
|
"Loki protobuf",
|
|
"/v1/loki/api/v1/push",
|
|
"application/x-protobuf",
|
|
prom_store::snappy_compress(&loki.encode_to_vec()).unwrap(),
|
|
));
|
|
check_http_skip_wal("loki", &cases, distributed).await;
|
|
}
|
|
|
|
pub async fn test_loki_pb_logs(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_http_app_with_frontend(store_type, "test_loki_pb_logs").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
// init loki request
|
|
let req: PushRequest = PushRequest {
|
|
streams: vec![StreamAdapter {
|
|
labels: r#"{service="test",source="integration",wadaxi="do anything"}"#.to_string(),
|
|
entries: vec![
|
|
EntryAdapter {
|
|
timestamp: Some(Timestamp::from_str("2024-11-07T10:53:50").unwrap()),
|
|
line: "this is a log message".to_string(),
|
|
structured_metadata: vec![
|
|
LabelPairAdapter {
|
|
name: "key1".to_string(),
|
|
value: "value1".to_string(),
|
|
},
|
|
LabelPairAdapter {
|
|
name: "key2".to_string(),
|
|
value: "value2".to_string(),
|
|
},
|
|
],
|
|
parsed: vec![],
|
|
},
|
|
EntryAdapter {
|
|
timestamp: Some(Timestamp::from_str("2024-11-07T10:53:51").unwrap()),
|
|
line: "this is a log message 2".to_string(),
|
|
structured_metadata: vec![LabelPairAdapter {
|
|
name: "key3".to_string(),
|
|
value: "value3".to_string(),
|
|
}],
|
|
parsed: vec![],
|
|
},
|
|
EntryAdapter {
|
|
timestamp: Some(Timestamp::from_str("2024-11-07T10:53:52").unwrap()),
|
|
line: "this is a log message 2".to_string(),
|
|
structured_metadata: vec![],
|
|
parsed: vec![],
|
|
},
|
|
],
|
|
hash: rand::random(),
|
|
}],
|
|
};
|
|
let encode = req.encode_to_vec();
|
|
let body = prom_store::snappy_compress(&encode).unwrap();
|
|
|
|
// write to loki
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("content-encoding"),
|
|
HeaderValue::from_static("snappy"),
|
|
),
|
|
(
|
|
HeaderName::from_static("accept-encoding"),
|
|
HeaderValue::from_static("identity"),
|
|
),
|
|
(
|
|
HeaderName::from_static(GREPTIME_LOG_TABLE_NAME_HEADER_NAME),
|
|
HeaderValue::from_static("loki_table_name"),
|
|
),
|
|
],
|
|
"/v1/loki/api/v1/push",
|
|
body,
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
// test schema
|
|
let expected = "[[\"loki_table_name\",\"CREATE TABLE IF NOT EXISTS \\\"loki_table_name\\\" (\\n \\\"greptime_timestamp\\\" TIMESTAMP(9) NOT NULL,\\n \\\"line\\\" STRING NULL,\\n \\\"structured_metadata\\\" JSON NULL,\\n \\\"service\\\" STRING NULL,\\n \\\"source\\\" STRING NULL,\\n \\\"wadaxi\\\" STRING NULL,\\n TIME INDEX (\\\"greptime_timestamp\\\"),\\n PRIMARY KEY (\\\"service\\\", \\\"source\\\", \\\"wadaxi\\\")\\n)\\n\\nENGINE=mito\\nWITH(\\n 'comment' = 'Created on insertion',\\n append_mode = 'true',\\n 'greptime.semantic.signal_type' = 'log',\\n 'greptime.semantic.source' = 'loki'\\n)\"]]";
|
|
validate_data(
|
|
"loki_pb_schema",
|
|
&client,
|
|
"show create table loki_table_name;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// test content
|
|
let expected = "[[1730976830000000000,\"this is a log message\",{\"key1\":\"value1\",\"key2\":\"value2\"},\"test\",\"integration\",\"do anything\"],[1730976831000000000,\"this is a log message 2\",{\"key3\":\"value3\"},\"test\",\"integration\",\"do anything\"],[1730976832000000000,\"this is a log message 2\",{},\"test\",\"integration\",\"do anything\"]]";
|
|
validate_data(
|
|
"loki_pb_content",
|
|
&client,
|
|
"select * from loki_table_name;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// `structured_metadata` is a JSON column, whose SST encoding differs from its
|
|
// memtable representation.
|
|
flush_table(&client, "loki_table_name").await;
|
|
validate_data(
|
|
"loki_pb_content_after_flush",
|
|
&client,
|
|
"select * from loki_table_name;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_loki_pb_logs_with_pipeline(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_loki_pb_logs_with_pipeline").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
let pipeline = r#"
|
|
processors:
|
|
- epoch:
|
|
field: greptime_timestamp
|
|
resolution: ms
|
|
"#;
|
|
|
|
let res = client
|
|
.post("/v1/pipelines/loki_pipe")
|
|
.header("content-type", "application/x-yaml")
|
|
.body(pipeline)
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
// init loki request
|
|
let req: PushRequest = PushRequest {
|
|
streams: vec![StreamAdapter {
|
|
labels: r#"{service="test",source="integration",wadaxi="do anything"}"#.to_string(),
|
|
entries: vec![
|
|
EntryAdapter {
|
|
timestamp: Some(Timestamp::from_str("2024-11-07T10:53:50").unwrap()),
|
|
line: "this is a log message".to_string(),
|
|
structured_metadata: vec![
|
|
LabelPairAdapter {
|
|
name: "key1".to_string(),
|
|
value: "value1".to_string(),
|
|
},
|
|
LabelPairAdapter {
|
|
name: "key2".to_string(),
|
|
value: "value2".to_string(),
|
|
},
|
|
],
|
|
parsed: vec![],
|
|
},
|
|
EntryAdapter {
|
|
timestamp: Some(Timestamp::from_str("2024-11-07T10:53:51").unwrap()),
|
|
line: "this is a log message 2".to_string(),
|
|
structured_metadata: vec![LabelPairAdapter {
|
|
name: "key3".to_string(),
|
|
value: "value3".to_string(),
|
|
}],
|
|
parsed: vec![],
|
|
},
|
|
EntryAdapter {
|
|
timestamp: Some(Timestamp::from_str("2024-11-07T10:53:52").unwrap()),
|
|
line: "this is a log message 2".to_string(),
|
|
structured_metadata: vec![],
|
|
parsed: vec![],
|
|
},
|
|
],
|
|
hash: rand::random(),
|
|
}],
|
|
};
|
|
let encode = req.encode_to_vec();
|
|
let body = prom_store::snappy_compress(&encode).unwrap();
|
|
|
|
// write to loki
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("content-encoding"),
|
|
HeaderValue::from_static("snappy"),
|
|
),
|
|
(
|
|
HeaderName::from_static("accept-encoding"),
|
|
HeaderValue::from_static("identity"),
|
|
),
|
|
(
|
|
HeaderName::from_static(GREPTIME_LOG_TABLE_NAME_HEADER_NAME),
|
|
HeaderValue::from_static("loki_table_name"),
|
|
),
|
|
(
|
|
HeaderName::from_static(GREPTIME_PIPELINE_NAME_HEADER_NAME),
|
|
HeaderValue::from_static("loki_pipe"),
|
|
),
|
|
],
|
|
"/v1/loki/api/v1/push",
|
|
body,
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
// test schema
|
|
// CREATE TABLE IF NOT EXISTS "loki_table_name" (
|
|
// "greptime_timestamp" TIMESTAMP(3) NOT NULL,
|
|
// "loki_label_service" STRING NULL,
|
|
// "loki_label_source" STRING NULL,
|
|
// "loki_label_wadaxi" STRING NULL,
|
|
// "loki_line" STRING NULL,
|
|
// "loki_metadata_key1" STRING NULL,
|
|
// "loki_metadata_key2" STRING NULL,
|
|
// "loki_metadata_key3" STRING NULL,
|
|
// TIME INDEX ("greptime_timestamp")
|
|
// )
|
|
// ENGINE=mito
|
|
// WITH(
|
|
// 'comment' = 'Created on insertion',
|
|
// append_mode = 'true'
|
|
// )
|
|
let expected = "[[\"loki_table_name\",\"CREATE TABLE IF NOT EXISTS \\\"loki_table_name\\\" (\\n \\\"greptime_timestamp\\\" TIMESTAMP(3) NOT NULL,\\n \\\"loki_label_service\\\" STRING NULL,\\n \\\"loki_label_source\\\" STRING NULL,\\n \\\"loki_label_wadaxi\\\" STRING NULL,\\n \\\"loki_line\\\" STRING NULL,\\n \\\"loki_metadata_key1\\\" STRING NULL,\\n \\\"loki_metadata_key2\\\" STRING NULL,\\n \\\"loki_metadata_key3\\\" STRING NULL,\\n TIME INDEX (\\\"greptime_timestamp\\\")\\n)\\n\\nENGINE=mito\\nWITH(\\n 'comment' = 'Created on insertion',\\n append_mode = 'true',\\n 'greptime.semantic.signal_type' = 'log',\\n 'greptime.semantic.source' = 'loki'\\n)\"]]";
|
|
validate_data(
|
|
"loki_pb_schema",
|
|
&client,
|
|
"show create table loki_table_name;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// test content
|
|
let expected = "[[1730976830000,\"test\",\"integration\",\"do anything\",\"this is a log message\",\"value1\",\"value2\",null],[1730976831000,\"test\",\"integration\",\"do anything\",\"this is a log message 2\",null,null,\"value3\"],[1730976832000,\"test\",\"integration\",\"do anything\",\"this is a log message 2\",null,null,null]]";
|
|
validate_data(
|
|
"loki_pb_content",
|
|
&client,
|
|
"select * from loki_table_name;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_loki_json_logs(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_loki_json_logs").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
let body = r#"
|
|
{
|
|
"streams": [
|
|
{
|
|
"stream": {
|
|
"source": "test",
|
|
"sender": "integration"
|
|
},
|
|
"values": [
|
|
[ "1735901380059465984", "this is line one", {"key1":"value1","key2":"value2"}],
|
|
[ "1735901398478897920", "this is line two", {"key3":"value3"}],
|
|
[ "1735901398478897921", "this is line two updated"]
|
|
]
|
|
}
|
|
]
|
|
}
|
|
"#;
|
|
|
|
let body = body.as_bytes().to_vec();
|
|
|
|
// write plain to loki
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/json"),
|
|
),
|
|
(
|
|
HeaderName::from_static(GREPTIME_LOG_TABLE_NAME_HEADER_NAME),
|
|
HeaderValue::from_static("loki_table_name"),
|
|
),
|
|
],
|
|
"/v1/loki/api/v1/push",
|
|
body,
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
// test schema
|
|
let expected = "[[\"loki_table_name\",\"CREATE TABLE IF NOT EXISTS \\\"loki_table_name\\\" (\\n \\\"greptime_timestamp\\\" TIMESTAMP(9) NOT NULL,\\n \\\"line\\\" STRING NULL,\\n \\\"structured_metadata\\\" JSON NULL,\\n \\\"sender\\\" STRING NULL,\\n \\\"source\\\" STRING NULL,\\n TIME INDEX (\\\"greptime_timestamp\\\"),\\n PRIMARY KEY (\\\"sender\\\", \\\"source\\\")\\n)\\n\\nENGINE=mito\\nWITH(\\n 'comment' = 'Created on insertion',\\n append_mode = 'true',\\n 'greptime.semantic.signal_type' = 'log',\\n 'greptime.semantic.source' = 'loki'\\n)\"]]";
|
|
validate_data(
|
|
"loki_json_schema",
|
|
&client,
|
|
"show create table loki_table_name;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// test content
|
|
let expected = "[[1735901380059465984,\"this is line one\",{\"key1\":\"value1\",\"key2\":\"value2\"},\"integration\",\"test\"],[1735901398478897920,\"this is line two\",{\"key3\":\"value3\"},\"integration\",\"test\"],[1735901398478897921,\"this is line two updated\",{},\"integration\",\"test\"]]";
|
|
validate_data(
|
|
"loki_json_content",
|
|
&client,
|
|
"select * from loki_table_name;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// `structured_metadata` is a JSON column, whose SST encoding differs from its
|
|
// memtable representation.
|
|
flush_table(&client, "loki_table_name").await;
|
|
validate_data(
|
|
"loki_json_content_after_flush",
|
|
&client,
|
|
"select * from loki_table_name;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_loki_json_logs_with_pipeline(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_loki_json_logs_with_pipeline").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
let pipeline = r#"
|
|
processors:
|
|
- epoch:
|
|
field: greptime_timestamp
|
|
resolution: ms
|
|
"#;
|
|
|
|
let res = client
|
|
.post("/v1/pipelines/loki_pipe")
|
|
.header("content-type", "application/x-yaml")
|
|
.body(pipeline)
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let body = r#"
|
|
{
|
|
"streams": [
|
|
{
|
|
"stream": {
|
|
"source": "test",
|
|
"sender": "integration"
|
|
},
|
|
"values": [
|
|
[ "1735901380059465984", "this is line one", {"key1":"value1","key2":"value2"}],
|
|
[ "1735901398478897920", "this is line two", {"key3":"value3"}],
|
|
[ "1735901398478897921", "this is line two updated"]
|
|
]
|
|
}
|
|
]
|
|
}
|
|
"#;
|
|
|
|
let body = body.as_bytes().to_vec();
|
|
|
|
// write plain to loki
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/json"),
|
|
),
|
|
(
|
|
HeaderName::from_static(GREPTIME_LOG_TABLE_NAME_HEADER_NAME),
|
|
HeaderValue::from_static("loki_table_name"),
|
|
),
|
|
(
|
|
HeaderName::from_static(GREPTIME_PIPELINE_NAME_HEADER_NAME),
|
|
HeaderValue::from_static("loki_pipe"),
|
|
),
|
|
],
|
|
"/v1/loki/api/v1/push",
|
|
body,
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
// test schema
|
|
// CREATE TABLE IF NOT EXISTS "loki_table_name" (
|
|
// "greptime_timestamp" TIMESTAMP(3) NOT NULL,
|
|
// "loki_label_sender" STRING NULL,
|
|
// "loki_label_source" STRING NULL,
|
|
// "loki_line" STRING NULL,
|
|
// "loki_metadata_key1" STRING NULL,
|
|
// "loki_metadata_key2" STRING NULL,
|
|
// "loki_metadata_key3" STRING NULL,
|
|
// TIME INDEX ("greptime_timestamp")
|
|
// )
|
|
// ENGINE=mito
|
|
// WITH(
|
|
// 'comment' = 'Created on insertion',
|
|
// append_mode = 'true'
|
|
// )
|
|
let expected = "[[\"loki_table_name\",\"CREATE TABLE IF NOT EXISTS \\\"loki_table_name\\\" (\\n \\\"greptime_timestamp\\\" TIMESTAMP(3) NOT NULL,\\n \\\"loki_label_sender\\\" STRING NULL,\\n \\\"loki_label_source\\\" STRING NULL,\\n \\\"loki_line\\\" STRING NULL,\\n \\\"loki_metadata_key1\\\" STRING NULL,\\n \\\"loki_metadata_key2\\\" STRING NULL,\\n \\\"loki_metadata_key3\\\" STRING NULL,\\n TIME INDEX (\\\"greptime_timestamp\\\")\\n)\\n\\nENGINE=mito\\nWITH(\\n 'comment' = 'Created on insertion',\\n append_mode = 'true',\\n 'greptime.semantic.signal_type' = 'log',\\n 'greptime.semantic.source' = 'loki'\\n)\"]]";
|
|
validate_data(
|
|
"loki_json_schema",
|
|
&client,
|
|
"show create table loki_table_name;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// test content
|
|
let expected = "[[1735901380059,\"integration\",\"test\",\"this is line one\",\"value1\",\"value2\",null],[1735901398478,\"integration\",\"test\",\"this is line two updated\",null,null,null],[1735901398478,\"integration\",\"test\",\"this is line two\",null,null,\"value3\"]]";
|
|
validate_data(
|
|
"loki_json_content",
|
|
&client,
|
|
"select * from loki_table_name;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
#[apply(both_deployment_cases)]
|
|
async fn test_elasticsearch_skip_wal(distributed: bool) {
|
|
let cases = vec![HttpWalCase::new(
|
|
"Elasticsearch",
|
|
"/v1/elasticsearch/_bulk",
|
|
"application/json",
|
|
"{\"create\":{\"_index\":\"wal_elastic\"}}\n{\"message\":\"wal test\"}\n",
|
|
)];
|
|
check_http_skip_wal("elasticsearch", &cases, distributed).await;
|
|
}
|
|
|
|
pub async fn test_elasticsearch_logs(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_elasticsearch_logs").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
let body = r#"
|
|
{"create":{"_index":"test","_id":"1"}}
|
|
{"foo":"foo_value1", "bar":"value1"}
|
|
{"create":{"_index":"test","_id":"2"}}
|
|
{"foo":"foo_value2","bar":"value2"}
|
|
"#;
|
|
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/json"),
|
|
)],
|
|
"/v1/elasticsearch/_bulk",
|
|
body.as_bytes().to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let expected = "[[\"foo_value1\",\"value1\"],[\"foo_value2\",\"value2\"]]";
|
|
|
|
validate_data(
|
|
"test_elasticsearch_logs",
|
|
&client,
|
|
"select foo, bar from test;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_elasticsearch_logs_with_index(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_elasticsearch_logs_with_index").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
// It will write to test_index1 and test_index2(specified in the path).
|
|
let body = r#"
|
|
{"create":{"_index":"test_index1","_id":"1"}}
|
|
{"foo":"foo_value1", "bar":"value1"}
|
|
{"create":{"_id":"2"}}
|
|
{"foo":"foo_value2","bar":"value2"}
|
|
"#;
|
|
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/json"),
|
|
)],
|
|
"/v1/elasticsearch/test_index2/_bulk",
|
|
body.as_bytes().to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
// test content of test_index1
|
|
let expected = "[[\"foo_value1\",\"value1\"]]";
|
|
validate_data(
|
|
"test_elasticsearch_logs_with_index",
|
|
&client,
|
|
"select foo, bar from test_index1;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
// test content of test_index2
|
|
let expected = "[[\"foo_value2\",\"value2\"]]";
|
|
validate_data(
|
|
"test_elasticsearch_logs_with_index_2",
|
|
&client,
|
|
"select foo, bar from test_index2;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_elasticsearch_pipeline_version(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_elasticsearch_pipeline_version").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
// Create version 1 of the pipeline: it renames `name` to `name_v1`.
|
|
let pipeline_v1 = r#"
|
|
processors:
|
|
- date:
|
|
field: ts
|
|
formats:
|
|
- "%Y-%m-%dT%H:%M:%S%.fZ"
|
|
transform:
|
|
- field: name, name_v1
|
|
type: string
|
|
- field: ts
|
|
type: time
|
|
index: timestamp
|
|
"#;
|
|
let res = client
|
|
.post("/v1/pipelines/esv")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline_v1)
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let content: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let version_v1 = content
|
|
.get("pipelines")
|
|
.unwrap()
|
|
.as_array()
|
|
.unwrap()
|
|
.first()
|
|
.unwrap()
|
|
.get("version")
|
|
.unwrap()
|
|
.as_str()
|
|
.unwrap()
|
|
.to_string();
|
|
|
|
// Create version 2: it renames `name` to `name_v2` instead.
|
|
let res = client
|
|
.post("/v1/pipelines/esv")
|
|
.header("Content-Type", "application/x-yaml")
|
|
.body(pipeline_v1.replace("name_v1", "name_v2"))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let content: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let version_v2 = content
|
|
.get("pipelines")
|
|
.unwrap()
|
|
.as_array()
|
|
.unwrap()
|
|
.first()
|
|
.unwrap()
|
|
.get("version")
|
|
.unwrap()
|
|
.as_str()
|
|
.unwrap()
|
|
.to_string();
|
|
|
|
let bulk_body = |index: &str| {
|
|
format!(
|
|
"{{\"create\":{{\"_index\":\"{index}\"}}}}\n{{\"name\":\"alice\",\"ts\":\"2024-01-01T00:00:00.000Z\"}}\n"
|
|
)
|
|
};
|
|
|
|
// 1. Select version 1 with the `x-greptime-pipeline-version` header.
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/json"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-version"),
|
|
HeaderValue::from_str(&version_v1).unwrap(),
|
|
),
|
|
],
|
|
"/v1/elasticsearch/_bulk?pipeline_name=esv",
|
|
bulk_body("es_hdr").into_bytes(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
validate_data(
|
|
"es_pipeline_version_header",
|
|
&client,
|
|
"select name_v1 from es_hdr;",
|
|
"[[\"alice\"]]",
|
|
)
|
|
.await;
|
|
|
|
// 2. Select version 1 with the `version` query parameter.
|
|
let encoded_version: String =
|
|
url::form_urlencoded::byte_serialize(version_v1.as_bytes()).collect();
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/json"),
|
|
)],
|
|
&format!("/v1/elasticsearch/_bulk?pipeline_name=esv&version={encoded_version}"),
|
|
bulk_body("es_url").into_bytes(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
validate_data(
|
|
"es_pipeline_version_url_param",
|
|
&client,
|
|
"select name_v1 from es_url;",
|
|
"[[\"alice\"]]",
|
|
)
|
|
.await;
|
|
|
|
// 3. Without a version, the latest version (2) is used.
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/json"),
|
|
)],
|
|
"/v1/elasticsearch/_bulk?pipeline_name=esv",
|
|
bulk_body("es_latest").into_bytes(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
validate_data(
|
|
"es_pipeline_version_latest",
|
|
&client,
|
|
"select name_v2 from es_latest;",
|
|
"[[\"alice\"]]",
|
|
)
|
|
.await;
|
|
|
|
// 4. An invalid version is rejected with HTTP 400, and the bulk item carries the
|
|
// same HTTP status so Elasticsearch clients do not see an internal status code.
|
|
let res = send_req(
|
|
&client,
|
|
vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/json"),
|
|
)],
|
|
"/v1/elasticsearch/_bulk?pipeline_name=esv&version=not-a-timestamp",
|
|
bulk_body("es_invalid_version").into_bytes(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::BAD_REQUEST, res.status());
|
|
let content: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
assert_eq!(content["errors"], json!(true));
|
|
assert_eq!(content["items"][0]["create"]["status"], json!(400));
|
|
|
|
// 5. The query parameter takes precedence over a conflicting header: the header
|
|
// selects version 1 while the URL selects version 2, so `name_v2` must be written.
|
|
let encoded_version_v2: String =
|
|
url::form_urlencoded::byte_serialize(version_v2.as_bytes()).collect();
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/json"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-version"),
|
|
HeaderValue::from_str(&version_v1).unwrap(),
|
|
),
|
|
],
|
|
&format!("/v1/elasticsearch/_bulk?pipeline_name=esv&version={encoded_version_v2}"),
|
|
bulk_body("es_precedence").into_bytes(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
validate_data(
|
|
"es_pipeline_version_precedence",
|
|
&client,
|
|
"select name_v2 from es_precedence;",
|
|
"[[\"alice\"]]",
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_log_query(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) = setup_test_http_app_with_frontend(store_type, "test_log_query").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
// prepare data with SQL API
|
|
let res = client
|
|
.get("/v1/sql?sql=create table logs (`ts` timestamp time index, `message` string fulltext index with(backend='bloom'));")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
let res = client
|
|
.post(
|
|
"/v1/sql?sql=insert into logs values ('2024-11-06 23:59:59', 'before-date-end'), ('2024-11-07 10:53:50', 'before-explicit-end'), ('2024-11-07 10:53:51', 'at-explicit-end'), ('2024-11-07 10:53:52', 'after-explicit-end');",
|
|
)
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
|
|
// test log query
|
|
let mut log_query = LogQuery {
|
|
table: TableName {
|
|
catalog_name: "greptime".to_string(),
|
|
schema_name: "public".to_string(),
|
|
table_name: "logs".to_string(),
|
|
},
|
|
time_filter: TimeFilter {
|
|
start: Some("2024-11-07".to_string()),
|
|
end: Some("2024-11-07T10:53:51Z".to_string()),
|
|
span: None,
|
|
},
|
|
limit: Limit {
|
|
skip: None,
|
|
fetch: Some(3),
|
|
},
|
|
columns: vec!["ts".to_string(), "message".to_string()],
|
|
filters: Default::default(),
|
|
context: Context::None,
|
|
exprs: vec![],
|
|
};
|
|
let res = client
|
|
.post("/v1/logs")
|
|
.header("Content-Type", "application/json")
|
|
.body(serde_json::to_string(&log_query).unwrap())
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
let resp = res.text().await;
|
|
let v = get_rows_from_output(&resp);
|
|
assert_eq!(v, "[[1730976830000,\"before-explicit-end\"]]");
|
|
|
|
log_query.time_filter.start = Some("2024-11-06".to_string());
|
|
log_query.time_filter.end = Some("2024-11-07".to_string());
|
|
log_query.limit.fetch = Some(4);
|
|
let res = client
|
|
.post("/v1/logs")
|
|
.header("Content-Type", "application/json")
|
|
.body(serde_json::to_string(&log_query).unwrap())
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
let resp = res.text().await;
|
|
let output = serde_json::from_str::<Value>(&resp).unwrap();
|
|
let rows = output["output"][0]["records"]["rows"].as_array().unwrap();
|
|
let mut messages = rows
|
|
.iter()
|
|
.map(|row| row[1].as_str().unwrap())
|
|
.collect::<Vec<_>>();
|
|
messages.sort_unstable();
|
|
assert_eq!(
|
|
messages,
|
|
vec![
|
|
"after-explicit-end",
|
|
"at-explicit-end",
|
|
"before-date-end",
|
|
"before-explicit-end",
|
|
]
|
|
);
|
|
|
|
// The fulltext index file is only written on flush. Replay the log query to
|
|
// cover the SST read path, and add a `matches_term` query, which is the
|
|
// predicate the index applier actually recognises.
|
|
flush_table(&client, "logs").await;
|
|
let res = client
|
|
.post("/v1/logs")
|
|
.header("Content-Type", "application/json")
|
|
.body(serde_json::to_string(&log_query).unwrap())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
let resp = res.text().await;
|
|
let output = serde_json::from_str::<Value>(&resp).unwrap();
|
|
let rows = output["output"][0]["records"]["rows"].as_array().unwrap();
|
|
let mut flushed_messages = rows
|
|
.iter()
|
|
.map(|row| row[1].as_str().unwrap())
|
|
.collect::<Vec<_>>();
|
|
flushed_messages.sort_unstable();
|
|
assert_eq!(flushed_messages, messages);
|
|
|
|
validate_data(
|
|
"log_query_fulltext_index",
|
|
&client,
|
|
"select message from logs where matches_term(message, 'at-explicit-end')",
|
|
r#"[["at-explicit-end"]]"#,
|
|
)
|
|
.await;
|
|
|
|
let res = client
|
|
.get("/v1/sql?sql=create table logs_limit (`ts` timestamp time index, `message` string);")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
let res = client
|
|
.get("/v1/sql?sql=insert into logs_limit select number, 'hello' from numbers limit 1001;")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
|
|
let aggregate_query = LogQuery {
|
|
table: TableName::new("greptime", "public", "logs_limit"),
|
|
time_filter: TimeFilter {
|
|
start: Some("1970-01-01T00:00:00Z".to_string()),
|
|
end: Some("1970-01-01T00:00:02Z".to_string()),
|
|
span: None,
|
|
},
|
|
limit: Limit {
|
|
skip: None,
|
|
fetch: None,
|
|
},
|
|
columns: vec![],
|
|
filters: Default::default(),
|
|
context: Context::None,
|
|
exprs: vec![LogExpr::AggrFunc {
|
|
expr: vec![AggFunc::new(
|
|
"count".to_string(),
|
|
vec![LogExpr::NamedIdent("message".to_string())],
|
|
Some("count_result".to_string()),
|
|
)],
|
|
by: vec![],
|
|
}],
|
|
};
|
|
let res = client
|
|
.post("/v1/logs")
|
|
.header("Content-Type", "application/json")
|
|
.body(serde_json::to_string(&aggregate_query).unwrap())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
assert_eq!(get_rows_from_output(&res.text().await), "[[1001]]");
|
|
|
|
let mut output_query = LogQuery {
|
|
table: TableName::new("greptime", "public", "logs_limit"),
|
|
time_filter: TimeFilter {
|
|
start: Some("1970-01-01T00:00:00Z".to_string()),
|
|
end: Some("1970-01-01T00:00:02Z".to_string()),
|
|
span: None,
|
|
},
|
|
limit: Limit {
|
|
skip: None,
|
|
fetch: None,
|
|
},
|
|
columns: vec!["ts".to_string(), "message".to_string()],
|
|
filters: Default::default(),
|
|
context: Context::None,
|
|
exprs: vec![],
|
|
};
|
|
let res = client
|
|
.post("/v1/logs")
|
|
.header("Content-Type", "application/json")
|
|
.body(serde_json::to_string(&output_query).unwrap())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
assert_eq!(get_output_row_count(&res.text().await), 1000);
|
|
|
|
output_query.limit.fetch = Some(17);
|
|
let res = client
|
|
.post("/v1/logs")
|
|
.header("Content-Type", "application/json")
|
|
.body(serde_json::to_string(&output_query).unwrap())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "{:?}", res.text().await);
|
|
assert_eq!(get_output_row_count(&res.text().await), 17);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_jaeger_v3_query_api(
|
|
store_type: StorageType,
|
|
) -> Result<(), Box<dyn std::error::Error>> {
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_jaeger_v3_query_api").await;
|
|
let client = TestClient::new(app).await;
|
|
for (path, expected_status, expected) in [
|
|
("services", StatusCode::OK, json!({"services":[]})),
|
|
(
|
|
"trace-summaries?query.startTimeMin=2026-01-01T00:00:00Z&query.startTimeMax=2026-01-02T00:00:00Z",
|
|
StatusCode::OK,
|
|
json!({"summaries":[]}),
|
|
),
|
|
(
|
|
"operations?service=checkout",
|
|
StatusCode::OK,
|
|
json!({"operations":[]}),
|
|
),
|
|
(
|
|
"traces/00000000000000000000000000000001",
|
|
StatusCode::NOT_FOUND,
|
|
json!({"error":{"httpCode":404,"message":"No traces found"}}),
|
|
),
|
|
] {
|
|
let response = client
|
|
.get(&format!("/v1/jaeger/api/v3/{path}"))
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), expected_status, "{path}");
|
|
assert_eq!(
|
|
serde_json::from_str::<Value>(&response.text().await)?,
|
|
expected
|
|
);
|
|
}
|
|
for path in [
|
|
"operations",
|
|
"traces",
|
|
"traces/not-hex",
|
|
"traces/0000000000000001?startTime=invalid",
|
|
"traces/0000000000000001?rawTraces=invalid",
|
|
"traces?query.startTimeMin=2026-01-01T00:00:00Z&query.startTimeMax=2026-01-02T00:00:00Z&query.searchDepth=10001",
|
|
"trace-summaries?query.startTimeMin=2026-01-01T00:00:00Z&query.startTimeMax=2026-01-02T00:00:00Z&query.searchDepth=10001",
|
|
] {
|
|
let response = client
|
|
.get(&format!("/v1/jaeger/api/v3/{path}"))
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::BAD_REQUEST, "{path}");
|
|
let body: Value = serde_json::from_str(&response.text().await)?;
|
|
assert_eq!(body["error"]["httpCode"], 400);
|
|
}
|
|
|
|
// The parent starts before the search window. A matching child must return the whole trace.
|
|
let start = chrono::Utc::now()
|
|
.timestamp_nanos_opt()
|
|
.ok_or("Timestamp out of range")? as u64;
|
|
let start = start / 1_000_000_000 * 1_000_000_000 + 123;
|
|
let timestamp = |ns: u64| {
|
|
chrono::DateTime::from_timestamp_nanos(ns as i64)
|
|
.to_rfc3339_opts(chrono::SecondsFormat::Nanos, true)
|
|
};
|
|
let request: ExportTraceServiceRequest = serde_json::from_value(json!({"resourceSpans":[
|
|
{"resource":{"attributes":[make_string_attr("service.name","checkout"), make_string_attr("service.instance.id","one")]},
|
|
"scopeSpans":[{"scope":{"name":"sdk","version":"1.0","attributes":[make_string_attr("scope.key","value"), {"key":"scope.bytes","value":{"bytesValue":"AQID/w=="}}]},"spans":[{
|
|
"traceId":"000000000000000000000000000000ab", "spanId":"0000000000000001", "name":"parent", "kind":2,
|
|
"startTimeUnixNano":start.to_string(), "endTimeUnixNano":(start+2000).to_string()
|
|
}]}]},
|
|
{"resource":{"attributes":[make_string_attr("service.name","checkout"), make_string_attr("service.instance.id","two"), {"key":"resource.bytes","value":{"bytesValue":"AQID/w=="}}]},
|
|
"scopeSpans":[{"scope":{"name":"sdk","version":"1.0","attributes":[make_string_attr("scope.key","value"), {"key":"scope.bytes","value":{"bytesValue":"AQID/w=="}}]},"spans":[{
|
|
"traceId":"000000000000000000000000000000ab", "spanId":"0000000000000002", "parentSpanId":"0000000000000001", "name":"child", "kind":3,
|
|
"traceState":"vendor=value", "startTimeUnixNano":(start+1000).to_string(), "endTimeUnixNano":(start+1999).to_string(),
|
|
"attributes":[make_int_attr("http.status_code",500),make_bool_attr("failed",true),
|
|
{"key":"bytes","value":{"bytesValue":"AQID/w=="}},
|
|
{"key":"integers","value":{"arrayValue":{"values":[{"intValue":"1"},{"intValue":"2"},{"intValue":"3"},{"intValue":"255"}]}}},
|
|
{"key":"nested","value":{"arrayValue":{"values":[{"intValue":"9223372036854775807"},{"kvlistValue":{"values":[make_string_attr("key","value")]}}]}}}],
|
|
"events":[{"name":"exception","timeUnixNano":(start+1011).to_string(),"attributes":[make_string_attr("message","failure")]}],
|
|
"links":[{"traceId":"000000000000000000000000000000cd","spanId":"0000000000000003","traceState":"link=value","attributes":[make_bool_attr("remote",true)]}],
|
|
"status":{"code":2,"message":"failure"}
|
|
}, {
|
|
"traceId":"000000000000000000000000000000cd", "spanId":"0000000000000003", "name":"boundary", "kind":2,
|
|
"startTimeUnixNano":(start+2000).to_string(), "endTimeUnixNano":(start+3000).to_string()
|
|
}]}]}
|
|
]}))?;
|
|
for pipeline in [
|
|
"greptime_trace_v0",
|
|
GREPTIME_INTERNAL_TRACE_PIPELINE_V1_NAME,
|
|
GREPTIME_INTERNAL_TRACE_PIPELINE_V2_NAME,
|
|
] {
|
|
let table = format!("jaeger_api_v3_{pipeline}");
|
|
let response = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-name"),
|
|
HeaderValue::from_str(pipeline)?,
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-trace-table-name"),
|
|
HeaderValue::from_str(&table)?,
|
|
),
|
|
],
|
|
"/v1/otlp/v1/traces",
|
|
request.encode_to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
for (path, expected) in [
|
|
("services", json!({"services":["checkout"]})),
|
|
(
|
|
"operations?service=checkout&span_kind=client",
|
|
json!({"operations":[{"name":"child","spanKind":"client"}]}),
|
|
),
|
|
(
|
|
"operations?service=checkout&spanKind=client",
|
|
json!({"operations":[{"name":"child","spanKind":"client"}]}),
|
|
),
|
|
] {
|
|
let response = client
|
|
.get(&format!("/v1/jaeger/api/v3/{path}"))
|
|
.header("x-greptime-trace-table-name", &table)
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
assert_eq!(
|
|
serde_json::from_str::<Value>(&response.text().await)?,
|
|
expected
|
|
);
|
|
}
|
|
for flushed in [false, true] {
|
|
if flushed {
|
|
let response = client
|
|
.post("/v1/sql")
|
|
.form(&[("sql", format!("ADMIN FLUSH_TABLE('{table}')"))])
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
}
|
|
let response = client.get(&format!(
|
|
"/v1/jaeger/api/v3/trace-summaries?query.serviceName=checkout&query.startTimeMin={}&query.startTimeMax={}",
|
|
timestamp(start+1000), timestamp(start+2000),
|
|
)).header("x-greptime-trace-table-name", &table).send().await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let body: Value = serde_json::from_str(&response.text().await)?;
|
|
assert_eq!(
|
|
body,
|
|
json!({"summaries":[{
|
|
"traceId":"000000000000000000000000000000ab",
|
|
"rootServiceName":"checkout", "rootOperationName":"parent",
|
|
"minStartTimeUnixNano":start.to_string(), "maxEndTimeUnixNano":(start+2000).to_string(),
|
|
"spanCount":2, "errorSpanCount":1, "orphanSpanCount":0,
|
|
"services":[{"name":"checkout", "spanCount":2, "errorSpanCount":1}],
|
|
}]})
|
|
);
|
|
for path in [
|
|
"traces/00000000000000AB".to_string(),
|
|
format!(
|
|
"traces/00000000000000AB?startTime={}",
|
|
timestamp(start + 1000)
|
|
),
|
|
format!("traces/00000000000000AB?endTime={}", timestamp(start + 500)),
|
|
format!(
|
|
"traces/00000000000000AB?start_time={}&end_time={}",
|
|
timestamp(start + 500),
|
|
timestamp(start + 999)
|
|
),
|
|
format!(
|
|
"traces?query.startTimeMin={}&query.startTimeMax={}&query.searchDepth=0",
|
|
timestamp(start + 1000),
|
|
timestamp(start + 2000)
|
|
),
|
|
format!(
|
|
"traces?query.service_name=checkout&query.operation_name=child&query.start_time_min={}&query.start_time_max={}&query.num_traces=1&query.duration_min=999ns&query.duration_max=999ns&query.attributes={}",
|
|
timestamp(start + 1000),
|
|
timestamp(start + 2000),
|
|
encode(r#"{"http.status_code":"500","failed":"true"}"#)
|
|
),
|
|
format!(
|
|
"traces?query.startTimeMin={}&query.startTimeMax={}&query.attributes={}",
|
|
timestamp(start + 1000),
|
|
timestamp(start + 2000),
|
|
encode(r#"{"service.instance.id":"two"}"#)
|
|
),
|
|
] {
|
|
let response = client
|
|
.get(&format!("/v1/jaeger/api/v3/{path}"))
|
|
.header("x-greptime-trace-table-name", &table)
|
|
.send()
|
|
.await;
|
|
let status = response.status();
|
|
let body: Value = serde_json::from_str(&response.text().await)?;
|
|
assert_eq!(status, StatusCode::OK, "{pipeline}: {path}: {body}");
|
|
let resources = body["result"]["resourceSpans"]
|
|
.as_array()
|
|
.ok_or("Missing resource spans")?;
|
|
let spans: Vec<_> = resources
|
|
.iter()
|
|
.flat_map(|r| r["scopeSpans"][0]["spans"].as_array().unwrap())
|
|
.collect();
|
|
assert_eq!(spans.len(), 2, "{body}");
|
|
assert!(
|
|
spans
|
|
.iter()
|
|
.all(|span| span["traceId"] == "000000000000000000000000000000ab")
|
|
);
|
|
let child = spans
|
|
.iter()
|
|
.find(|span| span["name"] == "child")
|
|
.ok_or("Missing child span")?;
|
|
assert_eq!(child["startTimeUnixNano"], (start + 1000).to_string());
|
|
assert_eq!(child["endTimeUnixNano"], (start + 1999).to_string());
|
|
assert_eq!(child["parentSpanId"], "0000000000000001");
|
|
assert_eq!(child["kind"], 3);
|
|
assert_eq!(child["status"]["code"], 2);
|
|
assert_eq!(child["traceState"], "vendor=value");
|
|
assert_eq!(
|
|
child["events"][0]["timeUnixNano"],
|
|
(start + 1011).to_string()
|
|
);
|
|
assert_eq!(child["links"][0]["traceState"], "link=value");
|
|
assert_eq!(
|
|
child["links"][0]["attributes"][0]["value"]["boolValue"],
|
|
true
|
|
);
|
|
let nested = child["attributes"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.find(|attr| attr["key"] == "nested")
|
|
.unwrap();
|
|
assert_eq!(
|
|
nested["value"]["arrayValue"]["values"][0]["intValue"],
|
|
"9223372036854775807"
|
|
);
|
|
let attribute = |values: &Value, key: &str| {
|
|
values
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.find(|attr| attr["key"] == key)
|
|
.unwrap()["value"]
|
|
.clone()
|
|
};
|
|
assert_eq!(
|
|
attribute(&child["attributes"], "integers"),
|
|
json!({"arrayValue":{"values":[{"intValue":"1"},{"intValue":"2"},{"intValue":"3"},{"intValue":"255"}]}})
|
|
);
|
|
if pipeline == GREPTIME_INTERNAL_TRACE_PIPELINE_V1_NAME {
|
|
assert_eq!(
|
|
attribute(&child["attributes"], "bytes"),
|
|
json!({"bytesValue":"AQID/w=="})
|
|
);
|
|
}
|
|
for resource in resources {
|
|
let span = &resource["scopeSpans"][0]["spans"][0];
|
|
let attrs = resource["resource"]["attributes"].as_array().unwrap();
|
|
let instance = attrs
|
|
.iter()
|
|
.find(|attr| attr["key"] == "service.instance.id")
|
|
.unwrap();
|
|
assert_eq!(
|
|
instance["value"]["stringValue"],
|
|
if span["name"] == "parent" {
|
|
"one"
|
|
} else {
|
|
"two"
|
|
}
|
|
);
|
|
let scope_attrs = &resource["scopeSpans"][0]["scope"]["attributes"];
|
|
assert_eq!(
|
|
attribute(scope_attrs, "scope.key"),
|
|
json!({"stringValue":"value"})
|
|
);
|
|
if pipeline == GREPTIME_INTERNAL_TRACE_PIPELINE_V1_NAME {
|
|
assert_eq!(
|
|
attribute(scope_attrs, "scope.bytes"),
|
|
json!({"bytesValue":"AQID/w=="})
|
|
);
|
|
if span["name"] == "child" {
|
|
assert_eq!(
|
|
attribute(&resource["resource"]["attributes"], "resource.bytes"),
|
|
json!({"bytesValue":"AQID/w=="})
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Instance `one` is on the parent, which starts before the search window.
|
|
let response = client
|
|
.get(&format!(
|
|
"/v1/jaeger/api/v3/traces?query.startTimeMin={}&query.startTimeMax={}&query.attributes={}",
|
|
timestamp(start + 1000),
|
|
timestamp(start + 2000),
|
|
encode(r#"{"service.instance.id":"one"}"#)
|
|
))
|
|
.header("x-greptime-trace-table-name", &table)
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::NOT_FOUND, "{pipeline}");
|
|
}
|
|
// Legacy clients continue receiving Jaeger JSON at the old URL.
|
|
let response = client
|
|
.get("/v1/jaeger/api/traces/000000000000000000000000000000ab")
|
|
.header("x-greptime-trace-table-name", &table)
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let body: Value = serde_json::from_str(&response.text().await)?;
|
|
assert_eq!(body["data"][0]["spans"].as_array().unwrap().len(), 2);
|
|
}
|
|
guard.remove_all().await;
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn test_jaeger_query_api(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_jaeger_query_api").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
// Test empty response for `/api/services` API before writing any traces.
|
|
let res = client.get("/v1/jaeger/api/services").send().await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected = r#"
|
|
{
|
|
"data": null,
|
|
"total": 0,
|
|
"limit": 0,
|
|
"offset": 0,
|
|
"errors": []
|
|
}
|
|
"#;
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let expected: Value = serde_json::from_str(expected).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
let content = r#"
|
|
{
|
|
"resourceSpans": [
|
|
{
|
|
"resource": {
|
|
"attributes": [
|
|
{
|
|
"key": "service.name",
|
|
"value": {
|
|
"stringValue": "test-jaeger-query-api"
|
|
}
|
|
}
|
|
]
|
|
},
|
|
"scopeSpans": [
|
|
{
|
|
"scope": {
|
|
"name": "test-jaeger-query-api",
|
|
"version": "1.0.0"
|
|
},
|
|
"spans": [
|
|
{
|
|
"traceId": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spanId": "008421dbbd33a3e9",
|
|
"name": "access-mysql",
|
|
"kind": 2,
|
|
"startTimeUnixNano": "1738726754492421000",
|
|
"endTimeUnixNano": "1738726754592421000",
|
|
"attributes": [
|
|
{
|
|
"key": "operation.type",
|
|
"value": {
|
|
"stringValue": "access-mysql"
|
|
}
|
|
},
|
|
{
|
|
"key": "net.peer.ip",
|
|
"value": {
|
|
"stringValue": "1.2.3.4"
|
|
}
|
|
},
|
|
{
|
|
"key": "peer.service",
|
|
"value": {
|
|
"stringValue": "test-jaeger-query-api"
|
|
}
|
|
}
|
|
],
|
|
"status": {
|
|
"message": "success",
|
|
"code": 0
|
|
}
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"scope": {
|
|
"name": "test-jaeger-query-api",
|
|
"version": "1.0.0"
|
|
},
|
|
"spans": [
|
|
{
|
|
"traceId": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spanId": "ffa03416a7b9ea48",
|
|
"name": "access-redis",
|
|
"kind": 2,
|
|
"startTimeUnixNano": "1738726754492422000",
|
|
"endTimeUnixNano": "1738726754592422000",
|
|
"attributes": [
|
|
{
|
|
"key": "operation.type",
|
|
"value": {
|
|
"stringValue": "access-redis"
|
|
}
|
|
},
|
|
{
|
|
"key": "net.peer.ip",
|
|
"value": {
|
|
"stringValue": "1.2.3.4"
|
|
}
|
|
},
|
|
{
|
|
"key": "peer.service",
|
|
"value": {
|
|
"stringValue": "test-jaeger-query-api"
|
|
}
|
|
}
|
|
],
|
|
"status": {
|
|
"message": "success",
|
|
"code": 0
|
|
}
|
|
}
|
|
]
|
|
}
|
|
],
|
|
"schemaUrl": "https://opentelemetry.io/schemas/1.4.0"
|
|
}
|
|
]
|
|
}
|
|
"#;
|
|
|
|
let req: ExportTraceServiceRequest = serde_json::from_str(content).unwrap();
|
|
let body = req.encode_to_vec();
|
|
|
|
// write traces data.
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-name"),
|
|
HeaderValue::from_static("greptime_trace_v0"),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/traces",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
// Test `/api/services` API.
|
|
let res = client.get("/v1/jaeger/api/services").send().await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected = r#"
|
|
{
|
|
"data": [
|
|
"test-jaeger-query-api"
|
|
],
|
|
"total": 1,
|
|
"limit": 0,
|
|
"offset": 0,
|
|
"errors": []
|
|
}
|
|
"#;
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let expected: Value = serde_json::from_str(expected).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
// Test `/api/operations` API.
|
|
let res = client
|
|
.get("/v1/jaeger/api/operations?service=test-jaeger-query-api&start=1738726754492421&end=1738726754642422")
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected = r#"
|
|
{
|
|
"data": [
|
|
{
|
|
"name": "access-mysql",
|
|
"spanKind": "server"
|
|
},
|
|
{
|
|
"name": "access-redis",
|
|
"spanKind": "server"
|
|
}
|
|
],
|
|
"total": 2,
|
|
"limit": 0,
|
|
"offset": 0,
|
|
"errors": []
|
|
}
|
|
"#;
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let expected: Value = serde_json::from_str(expected).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
// Test `/api/services/{service_name}/operations` API.
|
|
let res = client
|
|
.get("/v1/jaeger/api/services/test-jaeger-query-api/operations?start=1738726754492421&end=1738726754642422")
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected = r#"
|
|
{
|
|
"data": [
|
|
"access-mysql",
|
|
"access-redis"
|
|
],
|
|
"total": 2,
|
|
"limit": 0,
|
|
"offset": 0,
|
|
"errors": []
|
|
}
|
|
"#;
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let expected: Value = serde_json::from_str(expected).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
// Test `/api/traces/{trace_id}` API.
|
|
let res = client
|
|
.get("/v1/jaeger/api/traces/5611dce1bc9ebed65352d99a027b08ea")
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected = r#"
|
|
{
|
|
"data": [
|
|
{
|
|
"traceID": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spans": [
|
|
{
|
|
"traceID": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spanID": "ffa03416a7b9ea48",
|
|
"operationName": "access-redis",
|
|
"references": [],
|
|
"startTime": 1738726754492422,
|
|
"duration": 100000,
|
|
"tags": [
|
|
{
|
|
"key": "net.peer.ip",
|
|
"type": "string",
|
|
"value": "1.2.3.4"
|
|
},
|
|
{
|
|
"key": "operation.type",
|
|
"type": "string",
|
|
"value": "access-redis"
|
|
},
|
|
{
|
|
"key": "otel.scope.name",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "otel.scope.version",
|
|
"type": "string",
|
|
"value": "1.0.0"
|
|
},
|
|
{
|
|
"key": "otel.status_description",
|
|
"type": "string",
|
|
"value": "success"
|
|
},
|
|
{
|
|
"key": "peer.service",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "span.kind",
|
|
"type": "string",
|
|
"value": "server"
|
|
}
|
|
],
|
|
"logs": [],
|
|
"processID": "p1"
|
|
},
|
|
{
|
|
"traceID": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spanID": "008421dbbd33a3e9",
|
|
"operationName": "access-mysql",
|
|
"references": [],
|
|
"startTime": 1738726754492421,
|
|
"duration": 100000,
|
|
"tags": [
|
|
{
|
|
"key": "net.peer.ip",
|
|
"type": "string",
|
|
"value": "1.2.3.4"
|
|
},
|
|
{
|
|
"key": "operation.type",
|
|
"type": "string",
|
|
"value": "access-mysql"
|
|
},
|
|
{
|
|
"key": "otel.scope.name",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "otel.scope.version",
|
|
"type": "string",
|
|
"value": "1.0.0"
|
|
},
|
|
{
|
|
"key": "otel.status_description",
|
|
"type": "string",
|
|
"value": "success"
|
|
},
|
|
{
|
|
"key": "peer.service",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "span.kind",
|
|
"type": "string",
|
|
"value": "server"
|
|
}
|
|
],
|
|
"logs": [],
|
|
"processID": "p1"
|
|
}
|
|
],
|
|
"processes": {
|
|
"p1": {
|
|
"serviceName": "test-jaeger-query-api",
|
|
"tags": []
|
|
}
|
|
}
|
|
}
|
|
],
|
|
"total": 0,
|
|
"limit": 0,
|
|
"offset": 0,
|
|
"errors": []
|
|
}
|
|
"#;
|
|
|
|
let resp_txt = &res.text().await;
|
|
let resp: Value = serde_json::from_str(resp_txt).unwrap();
|
|
let expected: Value = serde_json::from_str(expected).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
// Test `/api/traces` API.
|
|
let res = client
|
|
.get("/v1/jaeger/api/traces?service=test-jaeger-query-api&operation=access-mysql&start=1738726754492421&end=1738726754642422&tags=%7B%22operation.type%22%3A%22access-mysql%22%7D")
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected = r#"
|
|
{
|
|
"data":
|
|
[
|
|
{
|
|
"traceID": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spans":
|
|
[
|
|
{
|
|
"traceID": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spanID": "ffa03416a7b9ea48",
|
|
"operationName": "access-redis",
|
|
"references":
|
|
[],
|
|
"startTime": 1738726754492422,
|
|
"duration": 100000,
|
|
"tags":
|
|
[
|
|
{
|
|
"key": "net.peer.ip",
|
|
"type": "string",
|
|
"value": "1.2.3.4"
|
|
},
|
|
{
|
|
"key": "operation.type",
|
|
"type": "string",
|
|
"value": "access-redis"
|
|
},
|
|
{
|
|
"key": "otel.scope.name",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "otel.scope.version",
|
|
"type": "string",
|
|
"value": "1.0.0"
|
|
},
|
|
{
|
|
"key": "otel.status_description",
|
|
"type": "string",
|
|
"value": "success"
|
|
},
|
|
{
|
|
"key": "peer.service",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "span.kind",
|
|
"type": "string",
|
|
"value": "server"
|
|
}
|
|
],
|
|
"logs":
|
|
[],
|
|
"processID": "p1"
|
|
},
|
|
{
|
|
"traceID": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spanID": "008421dbbd33a3e9",
|
|
"operationName": "access-mysql",
|
|
"references":
|
|
[],
|
|
"startTime": 1738726754492421,
|
|
"duration": 100000,
|
|
"tags":
|
|
[
|
|
{
|
|
"key": "net.peer.ip",
|
|
"type": "string",
|
|
"value": "1.2.3.4"
|
|
},
|
|
{
|
|
"key": "operation.type",
|
|
"type": "string",
|
|
"value": "access-mysql"
|
|
},
|
|
{
|
|
"key": "otel.scope.name",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "otel.scope.version",
|
|
"type": "string",
|
|
"value": "1.0.0"
|
|
},
|
|
{
|
|
"key": "otel.status_description",
|
|
"type": "string",
|
|
"value": "success"
|
|
},
|
|
{
|
|
"key": "peer.service",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "span.kind",
|
|
"type": "string",
|
|
"value": "server"
|
|
}
|
|
],
|
|
"logs":
|
|
[],
|
|
"processID": "p1"
|
|
}
|
|
],
|
|
"processes":
|
|
{
|
|
"p1":
|
|
{
|
|
"serviceName": "test-jaeger-query-api",
|
|
"tags":
|
|
[]
|
|
}
|
|
}
|
|
}
|
|
],
|
|
"total": 0,
|
|
"limit": 0,
|
|
"offset": 0,
|
|
"errors":
|
|
[]
|
|
}
|
|
"#;
|
|
|
|
let resp_txt = &res.text().await;
|
|
let resp: Value = serde_json::from_str(resp_txt).unwrap();
|
|
let expected: Value = serde_json::from_str(expected).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_jaeger_query_api_for_trace_v2(
|
|
store_type: StorageType,
|
|
) -> Result<(), Box<dyn std::error::Error>> {
|
|
common_telemetry::init_default_ut_logging();
|
|
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_jaeger_query_api_v2").await;
|
|
let client = TestClient::new(app).await;
|
|
let request: ExportTraceServiceRequest = serde_json::from_value(json!({
|
|
"resourceSpans": [{
|
|
"resource": {"attributes": [
|
|
make_string_attr("service.name", "jaeger-v2"),
|
|
make_string_attr("region", "west"),
|
|
make_string_attr("shared", "resource"),
|
|
make_string_attr("nullable", "resource"),
|
|
make_int_attr("count", 7),
|
|
make_int_attr("complex", 7)
|
|
]},
|
|
"scopeSpans": [{
|
|
"scope": {"name": "v2-scope", "version": "1.0"},
|
|
"spans": [{
|
|
"traceId": "c05d7a4ec8e1f231f02ed6e8da8655b4",
|
|
"spanId": "9630f2916e2f7909", "parentSpanId": "8f847259b0f6e1ab",
|
|
"name": "op", "kind": 2,
|
|
"startTimeUnixNano": "1736480942444376000",
|
|
"endTimeUnixNano": "1736480942444499000",
|
|
"attributes": [
|
|
make_string_attr("shared", "span"),
|
|
{"key": "nullable"},
|
|
make_string_attr("a\"b\\c.d", "escaped"),
|
|
make_int_attr("http.status_code", 500),
|
|
make_bool_attr("enabled", true),
|
|
{"key": "ratio", "value": {"doubleValue": 1.5}},
|
|
make_string_attr("count", "invalid"),
|
|
{"key": "complex", "value": {"arrayValue": {"values": [{"intValue":"7"}]}}}
|
|
],
|
|
"events": [{"name": "cache.hit", "timeUnixNano": "1736480942444400000",
|
|
"attributes": [make_int_attr("event.code", 7)]}],
|
|
"links": [{"traceId": "cc9e0991a2e63d274984bd44ee669203",
|
|
"spanId": "8f847259b0f6e1ab"}],
|
|
"status": {"code": 2, "message": "failure"}
|
|
}, {
|
|
"traceId": "cc9e0991a2e63d274984bd44ee669203",
|
|
"spanId": "8f847259b0f6e1ab", "name": "op", "kind": 2,
|
|
"startTimeUnixNano": "1736480942444376000",
|
|
"endTimeUnixNano": "1736480942444499000"
|
|
}]
|
|
}]
|
|
}]
|
|
}))?;
|
|
let response = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-name"),
|
|
HeaderValue::from_static(GREPTIME_INTERNAL_TRACE_PIPELINE_V2_NAME),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-trace-table-name"),
|
|
HeaderValue::from_static("jaeger_v2"),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/traces",
|
|
request.encode_to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
for (path, expected) in [
|
|
("/v1/jaeger/api/services", json!(["jaeger-v2"])),
|
|
(
|
|
"/v1/jaeger/api/operations?service=jaeger-v2",
|
|
json!([{"name":"op", "spanKind":"server"}]),
|
|
),
|
|
(
|
|
"/v1/jaeger/api/services/jaeger-v2/operations",
|
|
json!(["op"]),
|
|
),
|
|
] {
|
|
let response = client
|
|
.get(path)
|
|
.header("x-greptime-trace-table-name", "jaeger_v2")
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let body: Value = serde_json::from_str(&response.text().await)?;
|
|
assert_eq!(body["data"], expected, "{path}: {body}");
|
|
}
|
|
|
|
for flushed in [false, true] {
|
|
if flushed {
|
|
let response = client
|
|
.post("/v1/sql")
|
|
.form(&[("sql", "ADMIN FLUSH_TABLE('jaeger_v2')")])
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
}
|
|
for (tags, expected) in [
|
|
(json!({"shared":"span"}), 1),
|
|
(json!({"shared":"resource"}), 1),
|
|
(json!({"region":"west"}), 2),
|
|
(json!({"nullable":"resource"}), 2),
|
|
(json!({"count":7}), 2),
|
|
(json!({"complex":7}), 2),
|
|
(json!({"a\"b\\c.d":"escaped"}), 1),
|
|
(
|
|
json!({"http.status_code":500, "enabled":true, "ratio":1.5}),
|
|
1,
|
|
),
|
|
(json!({"error":true}), 1),
|
|
(json!({"missing":"absent"}), 0),
|
|
(json!({"missing":null}), 2),
|
|
(json!({"count":u64::MAX}), 0),
|
|
] {
|
|
let url = format!(
|
|
"/v1/jaeger/api/traces?service=jaeger-v2&start=1736480942444376&end=1736480942444500&tags={}",
|
|
encode(&tags.to_string())
|
|
);
|
|
let response = client
|
|
.get(&url)
|
|
.header("user-agent", if flushed { "Grafana" } else { "Jaeger" })
|
|
.header("x-greptime-trace-table-name", "jaeger_v2")
|
|
.send()
|
|
.await;
|
|
let status = response.status();
|
|
let body: Value = serde_json::from_str(&response.text().await)?;
|
|
assert_eq!(status, StatusCode::OK, "{tags}: {body}");
|
|
assert_eq!(
|
|
body["data"].as_array().map_or(0, Vec::len),
|
|
expected,
|
|
"{tags}: {body}"
|
|
);
|
|
}
|
|
let response = client
|
|
.get("/v1/jaeger/api/traces/c05d7a4ec8e1f231f02ed6e8da8655b4")
|
|
.header("x-greptime-trace-table-name", "jaeger_v2")
|
|
.send()
|
|
.await;
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let body: Value = serde_json::from_str(&response.text().await)?;
|
|
let process = &body["data"][0]["processes"]["p1"];
|
|
assert_eq!(process["serviceName"], "jaeger-v2");
|
|
assert!(
|
|
process["tags"]
|
|
.as_array()
|
|
.ok_or("Expected process tags")?
|
|
.iter()
|
|
.any(|tag| tag["key"] == "region" && tag["value"] == "west")
|
|
);
|
|
let span = &body["data"][0]["spans"][0];
|
|
assert_eq!(span["startTime"], 1736480942444376u64);
|
|
assert_eq!(span["duration"], 123);
|
|
assert_eq!(
|
|
span["references"],
|
|
json!([
|
|
{"refType":"CHILD_OF", "traceID":"c05d7a4ec8e1f231f02ed6e8da8655b4", "spanID":"8f847259b0f6e1ab"},
|
|
{"refType":"FOLLOWS_FROM", "traceID":"cc9e0991a2e63d274984bd44ee669203", "spanID":"8f847259b0f6e1ab"},
|
|
])
|
|
);
|
|
assert_eq!(span["logs"][0]["timestamp"], 1736480942444400u64);
|
|
assert!(
|
|
span["logs"][0]["fields"]
|
|
.as_array()
|
|
.ok_or("Expected event fields")?
|
|
.iter()
|
|
.any(|field| field["key"] == "event.code" && field["value"] == 7)
|
|
);
|
|
assert!(
|
|
span["tags"]
|
|
.as_array()
|
|
.ok_or("Expected span tags")?
|
|
.iter()
|
|
.any(|tag| tag["key"] == "http.status_code"
|
|
&& tag["type"] == "int64"
|
|
&& tag["value"] == 500)
|
|
);
|
|
}
|
|
guard.remove_all().await;
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn test_jaeger_search_limit_returns_latest_traces(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_jaeger_search_limit").await;
|
|
let client = TestClient::new(app).await;
|
|
|
|
// Twelve single-span traces spread over all trace partitions. The oldest
|
|
// traces get the smallest ids and are written first, so neither id nor
|
|
// write order points at the newest ones.
|
|
let now = Utc::now().timestamp_nanos_opt().unwrap() as u64;
|
|
let traces = (0..12u64)
|
|
.map(|i| {
|
|
let start = now - (60 - i) * 1_000_000_000;
|
|
(format!("{:x}{:031x}", i, i + 1), start)
|
|
})
|
|
.collect::<Vec<_>>();
|
|
let spans = traces
|
|
.iter()
|
|
.map(|(trace_id, start)| {
|
|
json!({
|
|
"traceId": trace_id,
|
|
"spanId": &trace_id[16..],
|
|
"name": "op",
|
|
"kind": 2,
|
|
"startTimeUnixNano": start.to_string(),
|
|
"endTimeUnixNano": (start + 1_000).to_string(),
|
|
})
|
|
})
|
|
.collect::<Vec<_>>();
|
|
let content = json!({
|
|
"resourceSpans": [{
|
|
"resource": {"attributes": [{"key": "service.name", "value": {"stringValue": "svc"}}]},
|
|
"scopeSpans": [{"scope": {"name": "s"}, "spans": spans}],
|
|
}]
|
|
});
|
|
let req: ExportTraceServiceRequest = serde_json::from_value(content).unwrap();
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-name"),
|
|
HeaderValue::from_static(GREPTIME_INTERNAL_TRACE_PIPELINE_V1_NAME),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/traces",
|
|
req.encode_to_vec(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let mut expected = traces[9..]
|
|
.iter()
|
|
.map(|(trace_id, _)| trace_id.clone())
|
|
.collect::<Vec<_>>();
|
|
expected.sort();
|
|
let path = format!(
|
|
"/v1/jaeger/api/traces?service=svc&operation=op&start={}&end={}&limit=3",
|
|
(now - 120_000_000_000) / 1_000,
|
|
now / 1_000
|
|
);
|
|
for _ in 0..5 {
|
|
let res = client.get(&path).send().await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let mut found = resp["data"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|trace| trace["traceID"].as_str().unwrap().to_string())
|
|
.collect::<Vec<_>>();
|
|
found.sort();
|
|
assert_eq!(found, expected);
|
|
}
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
pub async fn test_jaeger_query_api_for_trace_v1(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_jaeger_query_api_v1").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
// Test empty response for `/api/services` API before writing any traces.
|
|
let res = client.get("/v1/jaeger/api/services").send().await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected = r#"
|
|
{
|
|
"data": null,
|
|
"total": 0,
|
|
"limit": 0,
|
|
"offset": 0,
|
|
"errors": []
|
|
}
|
|
"#;
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let expected: Value = serde_json::from_str(expected).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
let content = r#"
|
|
{
|
|
"resourceSpans": [
|
|
{
|
|
"resource": {
|
|
"attributes": [
|
|
{
|
|
"key": "service.name",
|
|
"value": {
|
|
"stringValue": "test-jaeger-query-api"
|
|
}
|
|
}
|
|
]
|
|
},
|
|
"scopeSpans": [
|
|
{
|
|
"scope": {
|
|
"name": "test-jaeger-query-api",
|
|
"version": "1.0.0"
|
|
},
|
|
"spans": [
|
|
{
|
|
"traceId": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spanId": "008421dbbd33a3e9",
|
|
"name": "access-mysql",
|
|
"kind": 2,
|
|
"startTimeUnixNano": "1738726754492421000",
|
|
"endTimeUnixNano": "1738726754592421000",
|
|
"attributes": [
|
|
{
|
|
"key": "operation.type",
|
|
"value": {
|
|
"stringValue": "access-mysql"
|
|
}
|
|
},
|
|
{
|
|
"key": "net.peer.ip",
|
|
"value": {
|
|
"stringValue": "1.2.3.4"
|
|
}
|
|
},
|
|
{
|
|
"key": "peer.service",
|
|
"value": {
|
|
"stringValue": "test-jaeger-query-api"
|
|
}
|
|
}
|
|
],
|
|
"status": {
|
|
"message": "success",
|
|
"code": 0
|
|
}
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"scope": {
|
|
"name": "test-jaeger-query-api",
|
|
"version": "1.0.0"
|
|
},
|
|
"spans": [
|
|
{
|
|
"traceId": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spanId": "ffa03416a7b9ea48",
|
|
"name": "access-redis",
|
|
"kind": 2,
|
|
"startTimeUnixNano": "1738726754492422000",
|
|
"endTimeUnixNano": "1738726754592422000",
|
|
"attributes": [
|
|
{
|
|
"key": "operation.type",
|
|
"value": {
|
|
"stringValue": "access-redis"
|
|
}
|
|
},
|
|
{
|
|
"key": "net.peer.ip",
|
|
"value": {
|
|
"stringValue": "1.2.3.4"
|
|
}
|
|
},
|
|
{
|
|
"key": "peer.service",
|
|
"value": {
|
|
"stringValue": "test-jaeger-query-api"
|
|
}
|
|
}
|
|
],
|
|
"status": {
|
|
"message": "success",
|
|
"code": 0
|
|
}
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"scope": {
|
|
"name": "test-jaeger-get-operations",
|
|
"version": "1.0.0"
|
|
},
|
|
"spans": [
|
|
{
|
|
"traceId": "5611dce1bc9ebed65352d99a027b08ff",
|
|
"spanId": "ffa03416a7b9ea48",
|
|
"name": "access-pg",
|
|
"kind": 2,
|
|
"startTimeUnixNano": "1738726754492422000",
|
|
"endTimeUnixNano": "1738726754592422000",
|
|
"attributes": [
|
|
{
|
|
"key": "operation.type",
|
|
"value": {
|
|
"stringValue": "access-pg"
|
|
}
|
|
}
|
|
],
|
|
"status": {
|
|
"message": "success",
|
|
"code": 0
|
|
}
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"scope": {
|
|
"name": "test-jaeger-find-traces",
|
|
"version": "1.0.0"
|
|
},
|
|
"spans": [
|
|
{
|
|
"traceId": "5611dce1bc9ebed65352d99a027b08fa",
|
|
"spanId": "ffa03416a7b9ea49",
|
|
"name": "access-pg",
|
|
"kind": 2,
|
|
"startTimeUnixNano": "1738726754492423000",
|
|
"endTimeUnixNano": "1738726754592423000",
|
|
"attributes": [
|
|
{
|
|
"key": "operation.type",
|
|
"value": {
|
|
"stringValue": "access-pg"
|
|
}
|
|
}
|
|
],
|
|
"status": {
|
|
"message": "success",
|
|
"code": 0
|
|
}
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"scope": {
|
|
"name": "test-jaeger-grafana-ua",
|
|
"version": "1.0.0"
|
|
},
|
|
"spans": [
|
|
{
|
|
"traceId": "5611dce1bc9ebed65352d99a027b08fb",
|
|
"spanId": "ffa03416a7b9ea50",
|
|
"name": "access-span-1",
|
|
"kind": 2,
|
|
"startTimeUnixNano": "1738726754600000000",
|
|
"endTimeUnixNano": "1738726754700000000",
|
|
"attributes": [
|
|
{
|
|
"key": "operation.type",
|
|
"value": {
|
|
"stringValue": "access-span-1"
|
|
}
|
|
}
|
|
],
|
|
"status": {
|
|
"message": "success",
|
|
"code": 0
|
|
}
|
|
},
|
|
{
|
|
"traceId": "5611dce1bc9ebed65352d99a027b08fb",
|
|
"spanId": "ffa03416a7b9ea51",
|
|
"name": "access-span-2",
|
|
"kind": 2,
|
|
"startTimeUnixNano": "1738726754600001000",
|
|
"endTimeUnixNano": "1738726754700001000",
|
|
"attributes": [
|
|
{
|
|
"key": "operation.type",
|
|
"value": {
|
|
"stringValue": "access-span-2"
|
|
}
|
|
}
|
|
],
|
|
"status": {
|
|
"message": "success",
|
|
"code": 0
|
|
}
|
|
},
|
|
{
|
|
"traceId": "5611dce1bc9ebed65352d99a027b08fb",
|
|
"spanId": "ffa03416a7b9ea52",
|
|
"name": "access-span-3",
|
|
"kind": 2,
|
|
"startTimeUnixNano": "1738726754600002000",
|
|
"endTimeUnixNano": "1738726754700002000",
|
|
"attributes": [
|
|
{
|
|
"key": "operation.type",
|
|
"value": {
|
|
"stringValue": "access-span-3"
|
|
}
|
|
}
|
|
],
|
|
"status": {
|
|
"message": "success",
|
|
"code": 0
|
|
}
|
|
},
|
|
{
|
|
"traceId": "5611dce1bc9ebed65352d99a027b08fb",
|
|
"spanId": "ffa03416a7b9ea53",
|
|
"name": "access-span-4",
|
|
"kind": 2,
|
|
"startTimeUnixNano": "1738726754600003000",
|
|
"endTimeUnixNano": "1738726754700003000",
|
|
"attributes": [
|
|
{
|
|
"key": "operation.type",
|
|
"value": {
|
|
"stringValue": "access-span-4"
|
|
}
|
|
}
|
|
],
|
|
"status": {
|
|
"message": "success",
|
|
"code": 0
|
|
}
|
|
}
|
|
]
|
|
}
|
|
],
|
|
"schemaUrl": "https://opentelemetry.io/schemas/1.4.0"
|
|
}
|
|
]
|
|
}
|
|
"#;
|
|
|
|
let mut req: ExportTraceServiceRequest = serde_json::from_str(content).unwrap();
|
|
// The fixture is written against a fixed base instant. Move it next to now:
|
|
// this table is created with `ttl=7d`, and stale spans would be dropped as
|
|
// expired the moment they are flushed into SST files.
|
|
let now = Utc::now().timestamp_nanos_opt().unwrap() as u64;
|
|
let delta_micros = (now / 1_000) as i64 - 60_000_000 - JAEGER_V1_FIXTURE_BASE_MICROS as i64;
|
|
let delta_nanos = delta_micros * 1_000;
|
|
for span in req.resource_spans.iter_mut() {
|
|
for scope_span in span.scope_spans.iter_mut() {
|
|
for span in scope_span.spans.iter_mut() {
|
|
span.start_time_unix_nano = (span.start_time_unix_nano as i64 + delta_nanos) as u64;
|
|
span.end_time_unix_nano = (span.end_time_unix_nano as i64 + delta_nanos) as u64;
|
|
}
|
|
// `/api/operations` is queried without a time range, so these spans
|
|
// stay pinned to the last few seconds.
|
|
if scope_span.scope.as_ref().unwrap().name == "test-jaeger-get-operations" {
|
|
for span in scope_span.spans.iter_mut() {
|
|
span.start_time_unix_nano = now - 5_000_000_000; // 5 seconds ago
|
|
span.end_time_unix_nano = now;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
let body = req.encode_to_vec();
|
|
|
|
let trace_table_name = "mytable";
|
|
// write traces data.
|
|
let res = send_req(
|
|
&client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-log-pipeline-name"),
|
|
HeaderValue::from_static(GREPTIME_INTERNAL_TRACE_PIPELINE_V1_NAME),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-hints"),
|
|
HeaderValue::from_static("ttl=7d"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-trace-table-name"),
|
|
HeaderValue::from_static(trace_table_name),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/traces",
|
|
body.clone(),
|
|
false,
|
|
)
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let trace_table_sql = "[[\"mytable\",\"CREATE TABLE IF NOT EXISTS \\\"mytable\\\" (\\n \\\"timestamp\\\" TIMESTAMP(9) NOT NULL,\\n \\\"timestamp_end\\\" TIMESTAMP(9) NULL COMPRESSION WITH(encoding = 'delta_binary_packed', type = 'zstd'),\\n \\\"duration_nano\\\" BIGINT NULL,\\n \\\"parent_span_id\\\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM') COMPRESSION WITH(encoding = 'plain', page_size = '64KiB', type = 'zstd'),\\n \\\"trace_id\\\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM'),\\n \\\"span_id\\\" STRING NULL COMPRESSION WITH(encoding = 'plain', page_size = '64KiB', type = 'zstd'),\\n \\\"span_kind\\\" STRING NULL,\\n \\\"span_name\\\" STRING NULL,\\n \\\"span_status_code\\\" STRING NULL,\\n \\\"span_status_message\\\" STRING NULL,\\n \\\"trace_state\\\" STRING NULL,\\n \\\"scope_name\\\" STRING NULL,\\n \\\"scope_version\\\" STRING NULL,\\n \\\"service_name\\\" STRING NULL SKIPPING INDEX WITH(false_positive_rate = '0.01', granularity = '10240', type = 'BLOOM'),\\n \\\"span_attributes.operation.type\\\" STRING NULL,\\n \\\"span_attributes.net.peer.ip\\\" STRING NULL,\\n \\\"span_attributes.peer.service\\\" STRING NULL,\\n \\\"span_events\\\" JSON NULL,\\n \\\"span_links\\\" JSON NULL,\\n TIME INDEX (\\\"timestamp\\\"),\\n PRIMARY KEY (\\\"service_name\\\")\\n)\\nPARTITION ON COLUMNS (\\\"trace_id\\\") (\\n \\\"trace_id\\\" < '1',\\n \\\"trace_id\\\" >= '1' AND \\\"trace_id\\\" < '2',\\n \\\"trace_id\\\" >= '2' AND \\\"trace_id\\\" < '3',\\n \\\"trace_id\\\" >= '3' AND \\\"trace_id\\\" < '4',\\n \\\"trace_id\\\" >= '4' AND \\\"trace_id\\\" < '5',\\n \\\"trace_id\\\" >= '5' AND \\\"trace_id\\\" < '6',\\n \\\"trace_id\\\" >= '6' AND \\\"trace_id\\\" < '7',\\n \\\"trace_id\\\" >= '7' AND \\\"trace_id\\\" < '8',\\n \\\"trace_id\\\" >= '8' AND \\\"trace_id\\\" < '9',\\n \\\"trace_id\\\" >= '9' AND \\\"trace_id\\\" < 'a',\\n \\\"trace_id\\\" >= 'a' AND \\\"trace_id\\\" < 'b',\\n \\\"trace_id\\\" >= 'b' AND \\\"trace_id\\\" < 'c',\\n \\\"trace_id\\\" >= 'c' AND \\\"trace_id\\\" < 'd',\\n \\\"trace_id\\\" >= 'd' AND \\\"trace_id\\\" < 'e',\\n \\\"trace_id\\\" >= 'e' AND \\\"trace_id\\\" < 'f',\\n \\\"trace_id\\\" >= 'f'\\n)\\nENGINE=mito\\nWITH(\\n 'comment' = 'Created on insertion',\\n append_mode = 'true',\\n 'greptime.semantic.entity.service.id' = 'service_name',\\n 'greptime.semantic.pipeline' = 'greptime_trace_v1',\\n 'greptime.semantic.signal_type' = 'trace',\\n 'greptime.semantic.source' = 'opentelemetry',\\n 'greptime.semantic.trace.conventions' = 'https://opentelemetry.io/schemas/1.4.0',\\n table_data_model = 'greptime_trace_v1',\\n ttl = '7days'\\n)\"]]";
|
|
validate_data(
|
|
"trace_v1_create_table",
|
|
&client,
|
|
"show create table mytable;",
|
|
trace_table_sql,
|
|
)
|
|
.await;
|
|
|
|
let trace_meta_table_sql = "[[\"mytable_services\",\"CREATE TABLE IF NOT EXISTS \\\"mytable_services\\\" (\\n \\\"timestamp\\\" TIMESTAMP(9) NOT NULL,\\n \\\"service_name\\\" STRING NULL,\\n TIME INDEX (\\\"timestamp\\\"),\\n PRIMARY KEY (\\\"service_name\\\")\\n)\\n\\nENGINE=mito\\nWITH(\\n 'comment' = 'Created on insertion',\\n append_mode = 'false'\\n)\"]]";
|
|
validate_data(
|
|
"trace_v1_create_meta_table",
|
|
&client,
|
|
"show create table mytable_services;",
|
|
trace_meta_table_sql,
|
|
)
|
|
.await;
|
|
|
|
// Test `/api/services` API.
|
|
let res = client
|
|
.get("/v1/jaeger/api/services")
|
|
.header("x-greptime-trace-table-name", trace_table_name)
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected = r#"
|
|
{
|
|
"data": [
|
|
"test-jaeger-query-api"
|
|
],
|
|
"total": 1,
|
|
"limit": 0,
|
|
"offset": 0,
|
|
"errors": []
|
|
}
|
|
"#;
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let expected: Value =
|
|
serde_json::from_str(&shift_fixture_micros(expected, delta_micros)).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
// Test `/api/operations` API.
|
|
let res = client
|
|
.get("/v1/jaeger/api/operations?service=test-jaeger-query-api")
|
|
.header("x-greptime-trace-table-name", trace_table_name)
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected = r#"
|
|
{
|
|
"data": [
|
|
{
|
|
"name": "access-mysql",
|
|
"spanKind": "server"
|
|
},
|
|
{
|
|
"name": "access-pg",
|
|
"spanKind": "server"
|
|
},
|
|
{
|
|
"name": "access-redis",
|
|
"spanKind": "server"
|
|
},
|
|
{
|
|
"name": "access-span-1",
|
|
"spanKind": "server"
|
|
},
|
|
{
|
|
"name": "access-span-2",
|
|
"spanKind": "server"
|
|
},
|
|
{
|
|
"name": "access-span-3",
|
|
"spanKind": "server"
|
|
},
|
|
{
|
|
"name": "access-span-4",
|
|
"spanKind": "server"
|
|
}
|
|
],
|
|
"total": 7,
|
|
"limit": 0,
|
|
"offset": 0,
|
|
"errors": []
|
|
}
|
|
"#;
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let expected: Value =
|
|
serde_json::from_str(&shift_fixture_micros(expected, delta_micros)).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
// Test `/api/services/{service_name}/operations` API.
|
|
let res = client
|
|
.get(&shift_fixture_micros("/v1/jaeger/api/services/test-jaeger-query-api/operations?start=1738726754492421&end=1738726754642422", delta_micros))
|
|
.header("x-greptime-trace-table-name", trace_table_name)
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected = r#"
|
|
{
|
|
"data": [
|
|
"access-mysql",
|
|
"access-pg",
|
|
"access-redis",
|
|
"access-span-1",
|
|
"access-span-2",
|
|
"access-span-3",
|
|
"access-span-4"
|
|
],
|
|
"total": 7,
|
|
"limit": 0,
|
|
"offset": 0,
|
|
"errors": []
|
|
}
|
|
"#;
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let expected: Value =
|
|
serde_json::from_str(&shift_fixture_micros(expected, delta_micros)).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
// Test `/api/traces/{trace_id}` API without start and end.
|
|
let res = client
|
|
.get("/v1/jaeger/api/traces/5611dce1bc9ebed65352d99a027b08ea")
|
|
.header("x-greptime-trace-table-name", trace_table_name)
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected = r#"{
|
|
"data": [
|
|
{
|
|
"traceID": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spans": [
|
|
{
|
|
"traceID": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spanID": "ffa03416a7b9ea48",
|
|
"operationName": "access-redis",
|
|
"references": [],
|
|
"startTime": 1738726754492422,
|
|
"duration": 100000,
|
|
"tags": [
|
|
{
|
|
"key": "net.peer.ip",
|
|
"type": "string",
|
|
"value": "1.2.3.4"
|
|
},
|
|
{
|
|
"key": "operation.type",
|
|
"type": "string",
|
|
"value": "access-redis"
|
|
},
|
|
{
|
|
"key": "otel.scope.name",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "otel.scope.version",
|
|
"type": "string",
|
|
"value": "1.0.0"
|
|
},
|
|
{
|
|
"key": "otel.status_description",
|
|
"type": "string",
|
|
"value": "success"
|
|
},
|
|
{
|
|
"key": "peer.service",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "span.kind",
|
|
"type": "string",
|
|
"value": "server"
|
|
}
|
|
],
|
|
"logs": [],
|
|
"processID": "p1"
|
|
},
|
|
{
|
|
"traceID": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spanID": "008421dbbd33a3e9",
|
|
"operationName": "access-mysql",
|
|
"references": [],
|
|
"startTime": 1738726754492421,
|
|
"duration": 100000,
|
|
"tags": [
|
|
{
|
|
"key": "net.peer.ip",
|
|
"type": "string",
|
|
"value": "1.2.3.4"
|
|
},
|
|
{
|
|
"key": "operation.type",
|
|
"type": "string",
|
|
"value": "access-mysql"
|
|
},
|
|
{
|
|
"key": "otel.scope.name",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "otel.scope.version",
|
|
"type": "string",
|
|
"value": "1.0.0"
|
|
},
|
|
{
|
|
"key": "otel.status_description",
|
|
"type": "string",
|
|
"value": "success"
|
|
},
|
|
{
|
|
"key": "peer.service",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "span.kind",
|
|
"type": "string",
|
|
"value": "server"
|
|
}
|
|
],
|
|
"logs": [],
|
|
"processID": "p1"
|
|
}
|
|
],
|
|
"processes": {
|
|
"p1": {
|
|
"serviceName": "test-jaeger-query-api",
|
|
"tags": []
|
|
}
|
|
}
|
|
}
|
|
],
|
|
"total": 0,
|
|
"limit": 0,
|
|
"offset": 0,
|
|
"errors": []
|
|
}
|
|
"#;
|
|
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let expected: Value =
|
|
serde_json::from_str(&shift_fixture_micros(expected, delta_micros)).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
// Replay the same lookup against SST files: `trace_id` and `service_name`
|
|
// carry BLOOM skipping indexes whose puffin files only exist after a flush,
|
|
// and `span_events` / `span_links` are JSON columns.
|
|
flush_table(&client, trace_table_name).await;
|
|
let res = client
|
|
.get("/v1/jaeger/api/traces/5611dce1bc9ebed65352d99a027b08ea")
|
|
.header("x-greptime-trace-table-name", trace_table_name)
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
// Test `/api/traces/{trace_id}` API with start and end in microseconds.
|
|
let res = client
|
|
.get(&shift_fixture_micros("/v1/jaeger/api/traces/5611dce1bc9ebed65352d99a027b08ea?start=1738726754492421&end=1738726754642422", delta_micros))
|
|
.header("x-greptime-trace-table-name", trace_table_name)
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected = r#"{
|
|
"data": [
|
|
{
|
|
"traceID": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spans": [
|
|
{
|
|
"traceID": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spanID": "ffa03416a7b9ea48",
|
|
"operationName": "access-redis",
|
|
"references": [],
|
|
"startTime": 1738726754492422,
|
|
"duration": 100000,
|
|
"tags": [
|
|
{
|
|
"key": "net.peer.ip",
|
|
"type": "string",
|
|
"value": "1.2.3.4"
|
|
},
|
|
{
|
|
"key": "operation.type",
|
|
"type": "string",
|
|
"value": "access-redis"
|
|
},
|
|
{
|
|
"key": "otel.scope.name",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "otel.scope.version",
|
|
"type": "string",
|
|
"value": "1.0.0"
|
|
},
|
|
{
|
|
"key": "otel.status_description",
|
|
"type": "string",
|
|
"value": "success"
|
|
},
|
|
{
|
|
"key": "peer.service",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "span.kind",
|
|
"type": "string",
|
|
"value": "server"
|
|
}
|
|
],
|
|
"logs": [],
|
|
"processID": "p1"
|
|
},
|
|
{
|
|
"traceID": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spanID": "008421dbbd33a3e9",
|
|
"operationName": "access-mysql",
|
|
"references": [],
|
|
"startTime": 1738726754492421,
|
|
"duration": 100000,
|
|
"tags": [
|
|
{
|
|
"key": "net.peer.ip",
|
|
"type": "string",
|
|
"value": "1.2.3.4"
|
|
},
|
|
{
|
|
"key": "operation.type",
|
|
"type": "string",
|
|
"value": "access-mysql"
|
|
},
|
|
{
|
|
"key": "otel.scope.name",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "otel.scope.version",
|
|
"type": "string",
|
|
"value": "1.0.0"
|
|
},
|
|
{
|
|
"key": "otel.status_description",
|
|
"type": "string",
|
|
"value": "success"
|
|
},
|
|
{
|
|
"key": "peer.service",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "span.kind",
|
|
"type": "string",
|
|
"value": "server"
|
|
}
|
|
],
|
|
"logs": [],
|
|
"processID": "p1"
|
|
}
|
|
],
|
|
"processes": {
|
|
"p1": {
|
|
"serviceName": "test-jaeger-query-api",
|
|
"tags": []
|
|
}
|
|
}
|
|
}
|
|
],
|
|
"total": 0,
|
|
"limit": 0,
|
|
"offset": 0,
|
|
"errors": []
|
|
}
|
|
"#;
|
|
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let expected: Value =
|
|
serde_json::from_str(&shift_fixture_micros(expected, delta_micros)).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
// Test `/api/traces/{trace_id}` API for non-existent trace.
|
|
let res = client
|
|
.get(&shift_fixture_micros(
|
|
"/v1/jaeger/api/traces/0000000000000000000000000000dead",
|
|
delta_micros,
|
|
))
|
|
.header("x-greptime-trace-table-name", trace_table_name)
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::NOT_FOUND, res.status());
|
|
let expected = r#"{
|
|
"data": null,
|
|
"total": 0,
|
|
"limit": 0,
|
|
"offset": 0,
|
|
"errors": [
|
|
{
|
|
"code": 404,
|
|
"msg": "trace not found"
|
|
}
|
|
]
|
|
}
|
|
"#;
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let expected: Value =
|
|
serde_json::from_str(&shift_fixture_micros(expected, delta_micros)).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
// Test `/api/traces` API.
|
|
let res = client
|
|
.get(&shift_fixture_micros("/v1/jaeger/api/traces?service=test-jaeger-query-api&operation=access-mysql&start=1738726754492421&end=1738726754642422&tags=%7B%22operation.type%22%3A%22access-mysql%22%7D", delta_micros))
|
|
.header("x-greptime-trace-table-name", trace_table_name)
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
let expected = r#"
|
|
{
|
|
"data": [
|
|
{
|
|
"traceID": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spans": [
|
|
{
|
|
"traceID": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spanID": "ffa03416a7b9ea48",
|
|
"operationName": "access-redis",
|
|
"references": [],
|
|
"startTime": 1738726754492422,
|
|
"duration": 100000,
|
|
"tags": [
|
|
{
|
|
"key": "net.peer.ip",
|
|
"type": "string",
|
|
"value": "1.2.3.4"
|
|
},
|
|
{
|
|
"key": "operation.type",
|
|
"type": "string",
|
|
"value": "access-redis"
|
|
},
|
|
{
|
|
"key": "otel.scope.name",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "otel.scope.version",
|
|
"type": "string",
|
|
"value": "1.0.0"
|
|
},
|
|
{
|
|
"key": "otel.status_description",
|
|
"type": "string",
|
|
"value": "success"
|
|
},
|
|
{
|
|
"key": "peer.service",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "span.kind",
|
|
"type": "string",
|
|
"value": "server"
|
|
}
|
|
],
|
|
"logs": [],
|
|
"processID": "p1"
|
|
},
|
|
{
|
|
"traceID": "5611dce1bc9ebed65352d99a027b08ea",
|
|
"spanID": "008421dbbd33a3e9",
|
|
"operationName": "access-mysql",
|
|
"references": [],
|
|
"startTime": 1738726754492421,
|
|
"duration": 100000,
|
|
"tags": [
|
|
{
|
|
"key": "net.peer.ip",
|
|
"type": "string",
|
|
"value": "1.2.3.4"
|
|
},
|
|
{
|
|
"key": "operation.type",
|
|
"type": "string",
|
|
"value": "access-mysql"
|
|
},
|
|
{
|
|
"key": "otel.scope.name",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "otel.scope.version",
|
|
"type": "string",
|
|
"value": "1.0.0"
|
|
},
|
|
{
|
|
"key": "otel.status_description",
|
|
"type": "string",
|
|
"value": "success"
|
|
},
|
|
{
|
|
"key": "peer.service",
|
|
"type": "string",
|
|
"value": "test-jaeger-query-api"
|
|
},
|
|
{
|
|
"key": "span.kind",
|
|
"type": "string",
|
|
"value": "server"
|
|
}
|
|
],
|
|
"logs": [],
|
|
"processID": "p1"
|
|
}
|
|
],
|
|
"processes": {
|
|
"p1": {
|
|
"serviceName": "test-jaeger-query-api",
|
|
"tags": []
|
|
}
|
|
}
|
|
}
|
|
],
|
|
"total": 0,
|
|
"limit": 0,
|
|
"offset": 0,
|
|
"errors": []
|
|
}
|
|
"#;
|
|
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let expected: Value =
|
|
serde_json::from_str(&shift_fixture_micros(expected, delta_micros)).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
// Test `/api/traces` API with tags.
|
|
// 1. first query without tags, get 2 results
|
|
let res = client
|
|
.get(&shift_fixture_micros("/v1/jaeger/api/traces?service=test-jaeger-query-api&start=1738726754492422&end=1738726754592422", delta_micros))
|
|
.header("x-greptime-trace-table-name", trace_table_name)
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let expected = r#"
|
|
{"data":[{"processes":{"p1":{"serviceName":"test-jaeger-query-api","tags":[]}},"spans":[{"duration":100000,"logs":[],"operationName":"access-redis","processID":"p1","references":[],"spanID":"ffa03416a7b9ea48","startTime":1738726754492422,"tags":[{"key":"net.peer.ip","type":"string","value":"1.2.3.4"},{"key":"operation.type","type":"string","value":"access-redis"},{"key":"otel.scope.name","type":"string","value":"test-jaeger-query-api"},{"key":"otel.scope.version","type":"string","value":"1.0.0"},{"key":"otel.status_description","type":"string","value":"success"},{"key":"peer.service","type":"string","value":"test-jaeger-query-api"},{"key":"span.kind","type":"string","value":"server"}],"traceID":"5611dce1bc9ebed65352d99a027b08ea"}],"traceID":"5611dce1bc9ebed65352d99a027b08ea"},{"processes":{"p1":{"serviceName":"test-jaeger-query-api","tags":[]}},"spans":[{"duration":100000,"logs":[],"operationName":"access-pg","processID":"p1","references":[],"spanID":"ffa03416a7b9ea49","startTime":1738726754492423,"tags":[{"key":"operation.type","type":"string","value":"access-pg"},{"key":"otel.scope.name","type":"string","value":"test-jaeger-find-traces"},{"key":"otel.scope.version","type":"string","value":"1.0.0"},{"key":"otel.status_description","type":"string","value":"success"},{"key":"span.kind","type":"string","value":"server"}],"traceID":"5611dce1bc9ebed65352d99a027b08fa"}],"traceID":"5611dce1bc9ebed65352d99a027b08fa"}],"errors":[],"limit":0,"offset":0,"total":0}
|
|
"#;
|
|
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let expected: Value =
|
|
serde_json::from_str(&shift_fixture_micros(expected, delta_micros)).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
// 2. second query with tags, get 1 result
|
|
let res = client
|
|
.get(&shift_fixture_micros("/v1/jaeger/api/traces?service=test-jaeger-query-api&start=1738726754492422&end=1738726754592422&tags=%7B%22operation.type%22%3A%22access-pg%22%7D", delta_micros))
|
|
.header("x-greptime-trace-table-name", trace_table_name)
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let expected = r#"
|
|
{"data":[{"processes":{"p1":{"serviceName":"test-jaeger-query-api","tags":[]}},"spans":[{"duration":100000,"logs":[],"operationName":"access-pg","processID":"p1","references":[],"spanID":"ffa03416a7b9ea49","startTime":1738726754492423,"tags":[{"key":"operation.type","type":"string","value":"access-pg"},{"key":"otel.scope.name","type":"string","value":"test-jaeger-find-traces"},{"key":"otel.scope.version","type":"string","value":"1.0.0"},{"key":"otel.status_description","type":"string","value":"success"},{"key":"span.kind","type":"string","value":"server"}],"traceID":"5611dce1bc9ebed65352d99a027b08fa"}],"traceID":"5611dce1bc9ebed65352d99a027b08fa"}],"errors":[],"limit":0,"offset":0,"total":0}
|
|
"#;
|
|
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let expected: Value =
|
|
serde_json::from_str(&shift_fixture_micros(expected, delta_micros)).unwrap();
|
|
assert_eq!(resp, expected);
|
|
|
|
// Test `/api/traces` API with Grafana User-Agent.
|
|
// When user agent is Grafana, only return at most 3 spans per trace (earliest by timestamp).
|
|
// Trace `5611dce1bc9ebed65352d99a027b08fb` has 4 spans, so only 3 should be returned.
|
|
let res = client
|
|
.get(&shift_fixture_micros("/v1/jaeger/api/traces?service=test-jaeger-query-api&start=1738726754600000&end=1738726754700004", delta_micros))
|
|
.header("x-greptime-trace-table-name", trace_table_name)
|
|
.header("User-Agent", "Grafana/8.0.0")
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
// Verify that the trace has exactly 3 spans (limited by Grafana user agent)
|
|
let data = resp.get("data").unwrap().as_array().unwrap();
|
|
assert_eq!(data.len(), 1, "Expected 1 trace");
|
|
let trace = &data[0];
|
|
assert_eq!(
|
|
trace.get("traceID").unwrap().as_str().unwrap(),
|
|
"5611dce1bc9ebed65352d99a027b08fb"
|
|
);
|
|
let spans = trace.get("spans").unwrap().as_array().unwrap();
|
|
assert_eq!(
|
|
spans.len(),
|
|
3,
|
|
"Expected 3 spans (limited by Grafana user agent), got {}",
|
|
spans.len()
|
|
);
|
|
// Verify that the 3 earliest spans are returned (by timestamp ascending)
|
|
let span_names: Vec<&str> = spans
|
|
.iter()
|
|
.map(|s| s.get("operationName").unwrap().as_str().unwrap())
|
|
.collect();
|
|
assert!(
|
|
span_names.contains(&"access-span-1"),
|
|
"Expected access-span-1 in spans"
|
|
);
|
|
assert!(
|
|
span_names.contains(&"access-span-2"),
|
|
"Expected access-span-2 in spans"
|
|
);
|
|
assert!(
|
|
span_names.contains(&"access-span-3"),
|
|
"Expected access-span-3 in spans"
|
|
);
|
|
assert!(
|
|
!span_names.contains(&"access-span-4"),
|
|
"access-span-4 should NOT be in spans (4th span)"
|
|
);
|
|
|
|
// Test `/api/traces` API without User-Agent (default behavior).
|
|
// All 4 spans should be returned for the trace.
|
|
let res = client
|
|
.get(&shift_fixture_micros("/v1/jaeger/api/traces?service=test-jaeger-query-api&start=1738726754600000&end=1738726754700004", delta_micros))
|
|
.header("x-greptime-trace-table-name", trace_table_name)
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let data = resp.get("data").unwrap().as_array().unwrap();
|
|
assert_eq!(data.len(), 1, "Expected 1 trace");
|
|
let trace = &data[0];
|
|
let spans = trace.get("spans").unwrap().as_array().unwrap();
|
|
assert_eq!(
|
|
spans.len(),
|
|
4,
|
|
"Expected 4 spans (no user agent limit), got {}",
|
|
spans.len()
|
|
);
|
|
|
|
// Test `/api/traces` API with Jaeger User-Agent (should return all spans like default).
|
|
let res = client
|
|
.get(&shift_fixture_micros("/v1/jaeger/api/traces?service=test-jaeger-query-api&start=1738726754600000&end=1738726754700004", delta_micros))
|
|
.header("x-greptime-trace-table-name", trace_table_name)
|
|
.header("User-Agent", "Jaeger-Query/1.0.0")
|
|
.send()
|
|
.await;
|
|
assert_eq!(StatusCode::OK, res.status());
|
|
|
|
let resp: Value = serde_json::from_str(&res.text().await).unwrap();
|
|
let data = resp.get("data").unwrap().as_array().unwrap();
|
|
assert_eq!(data.len(), 1, "Expected 1 trace");
|
|
let trace = &data[0];
|
|
let spans = trace.get("spans").unwrap().as_array().unwrap();
|
|
assert_eq!(
|
|
spans.len(),
|
|
4,
|
|
"Expected 4 spans (Jaeger user agent, no limit), got {}",
|
|
spans.len()
|
|
);
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
#[apply(both_deployment_cases)]
|
|
async fn test_opentsdb_skip_wal(distributed: bool) {
|
|
let cases = vec![HttpWalCase::new(
|
|
"OpenTSDB",
|
|
"/v1/opentsdb/api/put",
|
|
"application/json",
|
|
r#"{"metric":"wal_tsdb","timestamp":1700000000,"value":1,"tags":{"host":"a"}}"#,
|
|
)];
|
|
check_http_skip_wal("opentsdb", &cases, distributed).await;
|
|
}
|
|
|
|
pub async fn test_influxdb_write(store_type: StorageType) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let (app, mut guard) =
|
|
setup_test_http_app_with_frontend(store_type, "test_influxdb_write").await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
// Only write field cpu.
|
|
let result = client
|
|
.post("/v1/influxdb/write?db=public&p=greptime&u=greptime")
|
|
.body("test_alter,host=host1 cpu=1.2 1664370459457010101")
|
|
.send()
|
|
.await;
|
|
assert_eq!(result.status(), 204);
|
|
assert!(result.text().await.is_empty());
|
|
|
|
// Only write field mem.
|
|
let result = client
|
|
.post("/v1/influxdb/write?db=public&p=greptime&u=greptime")
|
|
.body("test_alter,host=host1 mem=10240.0 1664370469457010101")
|
|
.send()
|
|
.await;
|
|
assert_eq!(result.status(), 204);
|
|
assert!(result.text().await.is_empty());
|
|
|
|
// Write field cpu & mem.
|
|
let result = client
|
|
.post("/v1/influxdb/write?db=public&p=greptime&u=greptime")
|
|
.body("test_alter,host=host1 cpu=3.2,mem=20480.0 1664370479457010101")
|
|
.send()
|
|
.await;
|
|
assert_eq!(result.status(), 204);
|
|
assert!(result.text().await.is_empty());
|
|
|
|
let expected = r#"[["host1",1.2,1664370459457010101,null],["host1",null,1664370469457010101,10240.0],["host1",3.2,1664370479457010101,20480.0]]"#;
|
|
validate_data(
|
|
"test_influxdb_write",
|
|
&client,
|
|
"select * from test_alter order by greptime_timestamp;",
|
|
expected,
|
|
)
|
|
.await;
|
|
|
|
guard.remove_all().await;
|
|
}
|
|
|
|
/// Flushes `table` so the rows written so far move from the memtable into SST
|
|
/// files. Replaying a query afterwards covers the encode/decode round trip and,
|
|
/// for indexed columns, the puffin files that only exist after a flush.
|
|
/// Base microsecond instant the `test_jaeger_query_api_for_trace_v1` fixture and
|
|
/// its expected responses are written against.
|
|
const JAEGER_V1_FIXTURE_BASE_MICROS: u64 = 1_738_726_754_492_421;
|
|
|
|
/// Shifts every fixture timestamp in `text` by `delta` microseconds, so expected
|
|
/// responses and query bounds follow the payload after it is moved next to now.
|
|
///
|
|
/// Only integer literals within a day of [`JAEGER_V1_FIXTURE_BASE_MICROS`] are
|
|
/// rewritten; durations, counts and ids are left alone.
|
|
fn shift_fixture_micros(text: &str, delta: i64) -> String {
|
|
let mut out = String::with_capacity(text.len());
|
|
let mut rest = text;
|
|
while let Some(start) = rest.find(|c: char| c.is_ascii_digit()) {
|
|
out.push_str(&rest[..start]);
|
|
let digits_end = rest[start..]
|
|
.find(|c: char| !c.is_ascii_digit())
|
|
.map_or(rest.len(), |offset| start + offset);
|
|
let digits = &rest[start..digits_end];
|
|
match digits.parse::<i64>() {
|
|
Ok(value) if (value - JAEGER_V1_FIXTURE_BASE_MICROS as i64).abs() < 86_400_000_000 => {
|
|
out.push_str(&(value + delta).to_string())
|
|
}
|
|
_ => out.push_str(digits),
|
|
}
|
|
rest = &rest[digits_end..];
|
|
}
|
|
out.push_str(rest);
|
|
out
|
|
}
|
|
|
|
async fn flush_table(client: &TestClient, table: &str) {
|
|
// The argument is parsed as a SQL table name, so an unquoted identifier is
|
|
// lower-cased and will not resolve a mixed-case table.
|
|
let quoted = table
|
|
.split('.')
|
|
.map(|part| format!("\"{part}\""))
|
|
.collect::<Vec<_>>()
|
|
.join(".");
|
|
let res = client
|
|
.post("/v1/sql")
|
|
.form(&[("sql", format!("ADMIN FLUSH_TABLE('{quoted}')"))])
|
|
.send()
|
|
.await;
|
|
let status = res.status();
|
|
let body = res.text().await;
|
|
assert_eq!(status, StatusCode::OK, "flush {table} fail: {body}");
|
|
// A failing statement still comes back as 200 with an `error` envelope.
|
|
assert!(!body.contains("\"error\""), "flush {table} fail: {body}");
|
|
}
|
|
|
|
async fn validate_data(test_name: &str, client: &TestClient, sql: &str, expected: &str) {
|
|
let res = client
|
|
.get(format!("/v1/sql?sql={sql}").as_str())
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "validate {test_name} fail");
|
|
let resp = res.text().await;
|
|
let v = get_rows_from_output(&resp);
|
|
|
|
assert_eq!(
|
|
expected, v,
|
|
"validate {test_name} fail, expected: {expected}, actual: {v}"
|
|
);
|
|
}
|
|
|
|
async fn wait_for_data(client: &TestClient, sql: &str, expected: &str) {
|
|
wait_for_data_with_timeout(client, sql, expected, Duration::from_secs(10)).await;
|
|
}
|
|
|
|
async fn wait_for_event_data(client: &TestClient, sql: &str, expected: &str) {
|
|
wait_for_data_with_timeout(client, sql, expected, Duration::from_secs(30)).await;
|
|
}
|
|
|
|
async fn wait_for_data_with_timeout(
|
|
client: &TestClient,
|
|
sql: &str,
|
|
expected: &str,
|
|
timeout: Duration,
|
|
) {
|
|
tokio::time::timeout(timeout, async {
|
|
let encoded_sql = encode(sql);
|
|
loop {
|
|
let res = client
|
|
.get(format!("/v1/sql?sql={encoded_sql}").as_str())
|
|
.send()
|
|
.await;
|
|
if res.status() != StatusCode::OK {
|
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
|
continue;
|
|
}
|
|
let resp = res.text().await;
|
|
let v = get_rows_from_output(&resp);
|
|
|
|
if expected == v {
|
|
break;
|
|
}
|
|
|
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
|
}
|
|
})
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
async fn execute_sql(client: &TestClient, sql: &str) -> TestResponse {
|
|
let encoded_sql = encode(sql);
|
|
client
|
|
.get(format!("/v1/sql?sql={encoded_sql}").as_str())
|
|
.send()
|
|
.await
|
|
}
|
|
|
|
struct HttpWalCase {
|
|
name: &'static str,
|
|
get: bool,
|
|
create_table: Option<&'static str>,
|
|
expected_written_nodes: Option<usize>,
|
|
path: &'static str,
|
|
content_type: &'static str,
|
|
body: Vec<u8>,
|
|
headers: Vec<(&'static str, &'static str)>,
|
|
}
|
|
|
|
impl HttpWalCase {
|
|
fn new(
|
|
name: &'static str,
|
|
path: &'static str,
|
|
content_type: &'static str,
|
|
body: impl Into<Vec<u8>>,
|
|
) -> Self {
|
|
Self {
|
|
name,
|
|
get: false,
|
|
create_table: None,
|
|
expected_written_nodes: None,
|
|
path,
|
|
content_type,
|
|
body: body.into(),
|
|
headers: Vec::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn check_http_skip_wal(name: &str, cases: &[HttpWalCase], distributed: bool) {
|
|
common_telemetry::init_default_ut_logging();
|
|
let mut instance = MockInstanceImpl::new(&format!("http_skip_wal_{name}"), distributed).await;
|
|
let fe = instance.frontend();
|
|
let server = HttpServerBuilder::new(HttpOptions::default())
|
|
.with_sql_handler(fe.clone())
|
|
.with_influxdb_handler(fe.clone())
|
|
.with_opentsdb_handler(fe.clone())
|
|
.with_log_ingest_handler(fe.clone(), None, None)
|
|
.with_otlp_handler(fe.clone(), true)
|
|
// The pending batcher uses BulkInsert, deliberately outside this PR.
|
|
.with_prom_handler(fe.clone(), Some(fe), true, PromValidationMode::Strict, None)
|
|
.build();
|
|
let client = TestClient::new(server.build(server.make_app()).unwrap()).await;
|
|
for case in cases {
|
|
if let Some(create_table) = case.create_table {
|
|
let response = execute_sql(&client, create_table).await;
|
|
assert!(response.status().is_success(), "{}", response.text().await);
|
|
}
|
|
// Warm up schema-on-write, then change only the request hint. Reusing
|
|
// the same payload also verifies policy is not persisted on the table.
|
|
for (round, hint) in [None, Some("true"), Some("false"), None]
|
|
.into_iter()
|
|
.enumerate()
|
|
{
|
|
common_telemetry::info!("Protocol WAL case: {}, hint: {:?}", case.name, hint);
|
|
let before = instance.flush_and_snapshot_wal().await;
|
|
let mut headers = vec![(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static(case.content_type),
|
|
)];
|
|
headers.extend(case.headers.iter().map(|(key, value)| {
|
|
(
|
|
HeaderName::from_static(key),
|
|
HeaderValue::from_static(value),
|
|
)
|
|
}));
|
|
if let Some(value) = hint {
|
|
headers.push((
|
|
HeaderName::from_static("x-greptime-insert-skip-wal"),
|
|
HeaderValue::from_static(value),
|
|
));
|
|
}
|
|
let response = if case.get {
|
|
let mut request = client.get(case.path).body(case.body.clone());
|
|
for (key, value) in headers {
|
|
request = request.header(key, value);
|
|
}
|
|
request.send().await
|
|
} else {
|
|
send_req(&client, headers, case.path, case.body.clone(), false).await
|
|
};
|
|
let status = response.status();
|
|
let body = response.text().await;
|
|
assert!(status.is_success(), "{}: {status}: {body}", case.name);
|
|
let after = instance.flush_and_snapshot_wal().await;
|
|
if before.keys().eq(after.keys()) {
|
|
assert_wal_delta(&before, &after, hint == Some("true"));
|
|
if let Some(expected_written_nodes) = case.expected_written_nodes {
|
|
let written_nodes = after
|
|
.iter()
|
|
.filter(|(key, (flushed_sequence, _))| *flushed_sequence > before[*key].0)
|
|
.map(|((node_id, _), _)| *node_id)
|
|
.collect::<std::collections::BTreeSet<_>>();
|
|
assert_eq!(
|
|
written_nodes.len(),
|
|
expected_written_nodes,
|
|
"{}: unexpected number of written datanodes",
|
|
case.name
|
|
);
|
|
}
|
|
} else {
|
|
assert!(round == 0, "only warmup may create regions: {}", case.name);
|
|
}
|
|
}
|
|
}
|
|
instance.shutdown().await;
|
|
}
|
|
|
|
async fn send_req(
|
|
client: &TestClient,
|
|
headers: Vec<(HeaderName, HeaderValue)>,
|
|
path: &str,
|
|
body: Vec<u8>,
|
|
with_gzip: bool,
|
|
) -> TestResponse {
|
|
let mut req = client.post(path);
|
|
|
|
for (k, v) in headers {
|
|
req = req.header(k, v);
|
|
}
|
|
|
|
let mut len = body.len();
|
|
|
|
if with_gzip {
|
|
let encoded = compress_vec_with_gzip(body);
|
|
len = encoded.len();
|
|
req = req.header("content-encoding", "gzip").body(encoded);
|
|
} else {
|
|
req = req.body(body);
|
|
}
|
|
|
|
req.header("content-length", len).send().await
|
|
}
|
|
|
|
async fn send_log_req(
|
|
client: &TestClient,
|
|
table_name: &str,
|
|
extract_keys: Option<&str>,
|
|
req: ExportLogsServiceRequest,
|
|
with_gzip: bool,
|
|
) -> TestResponse {
|
|
let mut headers = vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static(GREPTIME_LOG_TABLE_NAME_HEADER_NAME),
|
|
HeaderValue::from_str(table_name).unwrap(),
|
|
),
|
|
];
|
|
if let Some(extract_keys) = extract_keys {
|
|
headers.push((
|
|
HeaderName::from_static(GREPTIME_LOG_EXTRACT_KEYS_HEADER_NAME),
|
|
HeaderValue::from_str(extract_keys).unwrap(),
|
|
));
|
|
}
|
|
|
|
send_req(
|
|
client,
|
|
headers,
|
|
"/v1/otlp/v1/logs?db=public",
|
|
req.encode_to_vec(),
|
|
with_gzip,
|
|
)
|
|
.await
|
|
}
|
|
|
|
async fn send_trace_v1_req(
|
|
client: &TestClient,
|
|
table_name: &str,
|
|
req: ExportTraceServiceRequest,
|
|
with_gzip: bool,
|
|
) -> TestResponse {
|
|
send_trace_v1_req_with_db(client, "public", table_name, req, with_gzip).await
|
|
}
|
|
|
|
async fn send_trace_v1_req_with_db(
|
|
client: &TestClient,
|
|
db_name: &str,
|
|
table_name: &str,
|
|
req: ExportTraceServiceRequest,
|
|
with_gzip: bool,
|
|
) -> TestResponse {
|
|
send_req(
|
|
client,
|
|
vec![
|
|
(
|
|
HeaderName::from_static("content-type"),
|
|
HeaderValue::from_static("application/x-protobuf"),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-pipeline-name"),
|
|
HeaderValue::from_static(GREPTIME_INTERNAL_TRACE_PIPELINE_V1_NAME),
|
|
),
|
|
(
|
|
HeaderName::from_static("x-greptime-trace-table-name"),
|
|
HeaderValue::from_str(table_name).unwrap(),
|
|
),
|
|
(
|
|
GREPTIME_DB_HEADER_NAME.clone(),
|
|
HeaderValue::from_str(db_name).unwrap(),
|
|
),
|
|
],
|
|
"/v1/otlp/v1/traces",
|
|
req.encode_to_vec(),
|
|
with_gzip,
|
|
)
|
|
.await
|
|
}
|
|
|
|
fn make_log_request(records: Vec<Value>) -> ExportLogsServiceRequest {
|
|
serde_json::from_value(json!({
|
|
"resourceLogs": [{
|
|
"resource": {
|
|
"attributes": []
|
|
},
|
|
"scopeLogs": [{
|
|
"scope": {
|
|
"name": "",
|
|
"version": "",
|
|
"attributes": []
|
|
},
|
|
"logRecords": records,
|
|
"schemaUrl": ""
|
|
}],
|
|
"schemaUrl": "https://opentelemetry.io/schemas/1.4.0"
|
|
}]
|
|
}))
|
|
.unwrap()
|
|
}
|
|
|
|
fn make_log_record(
|
|
trace_id: &str,
|
|
span_id: &str,
|
|
time_unix_nano: i64,
|
|
body: &str,
|
|
attributes: Vec<Value>,
|
|
) -> Value {
|
|
json!({
|
|
"timeUnixNano": time_unix_nano.to_string(),
|
|
"observedTimeUnixNano": "0",
|
|
"severityNumber": 9,
|
|
"severityText": "Info",
|
|
"body": {
|
|
"stringValue": body
|
|
},
|
|
"attributes": attributes,
|
|
"droppedAttributesCount": 0,
|
|
"flags": 0,
|
|
"traceId": trace_id,
|
|
"spanId": span_id
|
|
})
|
|
}
|
|
|
|
fn make_trace_v1_request(service_name: &str, spans: Vec<Value>) -> ExportTraceServiceRequest {
|
|
serde_json::from_value(json!({
|
|
"resourceSpans": [{
|
|
"resource": {
|
|
"attributes": [{
|
|
"key": "service.name",
|
|
"value": { "stringValue": service_name }
|
|
}]
|
|
},
|
|
"scopeSpans": [{
|
|
"scope": {
|
|
"name": "trace-v1-type-tests"
|
|
},
|
|
"spans": spans
|
|
}],
|
|
"schemaUrl": "https://opentelemetry.io/schemas/1.4.0"
|
|
}]
|
|
}))
|
|
.unwrap()
|
|
}
|
|
|
|
fn make_trace_v1_span(
|
|
trace_id: &str,
|
|
span_id: &str,
|
|
name: &str,
|
|
start_time_unix_nano: i64,
|
|
end_time_unix_nano: i64,
|
|
attributes: Vec<Value>,
|
|
) -> Value {
|
|
json!({
|
|
"traceId": trace_id,
|
|
"spanId": span_id,
|
|
"name": name,
|
|
"kind": 2,
|
|
"startTimeUnixNano": start_time_unix_nano.to_string(),
|
|
"endTimeUnixNano": end_time_unix_nano.to_string(),
|
|
"attributes": attributes,
|
|
"status": {
|
|
"message": "",
|
|
"code": 0
|
|
}
|
|
})
|
|
}
|
|
|
|
fn make_string_attr(key: &str, value: &str) -> Value {
|
|
json!({
|
|
"key": key,
|
|
"value": {
|
|
"stringValue": value
|
|
}
|
|
})
|
|
}
|
|
|
|
fn make_int_attr(key: &str, value: i64) -> Value {
|
|
json!({
|
|
"key": key,
|
|
"value": {
|
|
"intValue": value.to_string()
|
|
}
|
|
})
|
|
}
|
|
|
|
fn make_double_attr(key: &str, value: f64) -> Value {
|
|
json!({
|
|
"key": key,
|
|
"value": {
|
|
"doubleValue": value
|
|
}
|
|
})
|
|
}
|
|
|
|
fn make_bool_attr(key: &str, value: bool) -> Value {
|
|
json!({
|
|
"key": key,
|
|
"value": {
|
|
"boolValue": value
|
|
}
|
|
})
|
|
}
|
|
|
|
fn get_rows_from_output(output: &str) -> String {
|
|
let resp: Value = serde_json::from_str(output).unwrap();
|
|
resp.get("output")
|
|
.and_then(Value::as_array)
|
|
.and_then(|v| v.first())
|
|
.and_then(|v| v.get("records"))
|
|
.and_then(|v| v.get("rows"))
|
|
.unwrap()
|
|
.to_string()
|
|
}
|
|
|
|
fn get_output_row_count(output: &str) -> usize {
|
|
let resp: Value = serde_json::from_str(output).unwrap();
|
|
resp.get("output")
|
|
.and_then(Value::as_array)
|
|
.and_then(|v| v.first())
|
|
.and_then(|v| v.get("records"))
|
|
.and_then(|v| v.get("rows"))
|
|
.and_then(Value::as_array)
|
|
.unwrap()
|
|
.len()
|
|
}
|
|
|
|
fn compress_vec_with_gzip(data: Vec<u8>) -> Vec<u8> {
|
|
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
|
|
encoder.write_all(&data).unwrap();
|
|
encoder.finish().unwrap()
|
|
}
|
|
|
|
pub async fn test_http_memory_limit(store_type: StorageType) {
|
|
use tests_integration::test_util::setup_test_http_app_with_frontend_and_custom_options;
|
|
|
|
common_telemetry::init_default_ut_logging();
|
|
|
|
let http_opts = servers::http::HttpOptions {
|
|
addr: format!("127.0.0.1:{}", common_test_util::ports::get_port()),
|
|
..Default::default()
|
|
};
|
|
|
|
// Create memory limiter with 2KB limit and fail-fast policy.
|
|
// Note: MemoryManager uses 1KB granularity (PermitGranularity::Kilobyte),
|
|
// so 2KB = 2 permits. Small/medium requests should fit, large should fail.
|
|
let memory_limiter = ServerMemoryLimiter::new(2048, OnExhaustedPolicy::Fail);
|
|
|
|
let (app, mut guard) = setup_test_http_app_with_frontend_and_custom_options(
|
|
store_type,
|
|
"test_http_memory_limit",
|
|
None,
|
|
Some(http_opts),
|
|
Some(memory_limiter),
|
|
)
|
|
.await;
|
|
|
|
let client = TestClient::new(app).await;
|
|
|
|
// Create table first
|
|
let res = client
|
|
.get("/v1/sql?sql=CREATE TABLE test_mem_limit(host STRING, cpu DOUBLE, ts TIMESTAMP TIME INDEX)")
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
// Test 1: Small POST request should succeed (uses actual memory limiting path)
|
|
// Note: GET requests have no body, so they bypass body memory limiting entirely.
|
|
let small_insert = "INSERT INTO test_mem_limit VALUES ('host1', 1.0, 0)";
|
|
let res = client
|
|
.post("/v1/sql")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.body(format!("sql={}", small_insert))
|
|
.send()
|
|
.await;
|
|
assert_eq!(res.status(), StatusCode::OK, "Small POST should succeed");
|
|
|
|
// Test 1B: Medium request in the 500-1024 byte range should also succeed
|
|
// (due to 1KB granularity alignment)
|
|
let medium_values: Vec<String> = (0..8)
|
|
.map(|i| format!("('host{}', {}.5, {})", i, i, i * 1000))
|
|
.collect();
|
|
let medium_insert = format!(
|
|
"INSERT INTO test_mem_limit VALUES {}",
|
|
medium_values.join(", ")
|
|
);
|
|
|
|
let res = client
|
|
.post("/v1/sql")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.body(format!("sql={}", medium_insert))
|
|
.send()
|
|
.await;
|
|
assert_eq!(
|
|
res.status(),
|
|
StatusCode::OK,
|
|
"Medium request (~700 bytes) should succeed within aligned 1KB limit"
|
|
);
|
|
|
|
// Test 2: Large write request should be rejected (exceeds 2KB limit)
|
|
// Generate a large INSERT with many rows and long strings to definitely exceed 2KB
|
|
let large_values: Vec<String> = (0..100)
|
|
.map(|i| {
|
|
format!(
|
|
"('this_is_a_very_long_hostname_string_to_increase_body_size_row_{}', {}.5, {})",
|
|
i,
|
|
i,
|
|
i * 1000
|
|
)
|
|
})
|
|
.collect();
|
|
let large_insert = format!(
|
|
"INSERT INTO test_mem_limit VALUES {}",
|
|
large_values.join(", ")
|
|
);
|
|
|
|
let res = client
|
|
.post("/v1/sql")
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.body(format!("sql={}", large_insert))
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(
|
|
res.status(),
|
|
StatusCode::TOO_MANY_REQUESTS,
|
|
"Large write should be rejected with 429"
|
|
);
|
|
|
|
let error_body = res.text().await;
|
|
assert!(
|
|
error_body.contains("Request body memory limit exceeded"),
|
|
"Error message should be 'Request body memory limit exceeded', got: {}",
|
|
error_body
|
|
);
|
|
|
|
// Test 3A: Small InfluxDB write should succeed
|
|
let small_influx = "test_influx,host=host1 cpu=1.5 1000000000";
|
|
let res = client
|
|
.post("/v1/influxdb/write?db=public")
|
|
.body(small_influx)
|
|
.send()
|
|
.await;
|
|
assert_eq!(
|
|
res.status(),
|
|
StatusCode::NO_CONTENT,
|
|
"Small InfluxDB write should succeed"
|
|
);
|
|
|
|
// Test 3B: Large InfluxDB write should be rejected
|
|
let large_influx_body = (0..100)
|
|
.map(|i| {
|
|
format!(
|
|
"test_influx,host=host{} cpu={}.5 {}",
|
|
i,
|
|
i,
|
|
(i as i64) * 1000000000
|
|
)
|
|
})
|
|
.collect::<Vec<_>>()
|
|
.join("\n");
|
|
|
|
let res = client
|
|
.post("/v1/influxdb/write?db=public")
|
|
.body(large_influx_body)
|
|
.send()
|
|
.await;
|
|
|
|
assert_eq!(
|
|
res.status(),
|
|
StatusCode::TOO_MANY_REQUESTS,
|
|
"Large InfluxDB write should be rejected"
|
|
);
|
|
|
|
guard.remove_all().await;
|
|
}
|