chore: backport selected changes for 1.3.0-beta.2 (#9529)

* ci(query-regression): bump RUNNER_IMAGE_EPOCH to 7 (#9395)

ci(query-regression): bump RUNNER_IMAGE_EPOCH to 7 for image m-0xihbfzm5xpbxolybo6i

Signed-off-by: greptimedb-ci <greptimedb-ci@greptime.com>
Co-authored-by: greptimedb-ci <greptimedb-ci@greptime.com>
(cherry picked from commit 886e02f0dc)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(promql): apply offset to subquery evaluation window (#9364)

* fix(promql): apply offset to subquery evaluation window

`prom_subquery_expr_to_plan` destructured `SubqueryExpr` without reading
`offset`, so `<subquery>[range:step] offset <d>` planned exactly the same
window as the un-offset form and silently returned data for the wrong time
range. The plain vector/matrix-selector paths already threaded the offset
through `selector_to_series_normalize_plan` and `RangeManipulate`.

Shift the inner evaluation window back by the offset and pass the offset to
the subquery's `RangeManipulate`, which maps the inner samples forward onto
the evaluation timeline before bucketing them into ranges. This matches
Prometheus, whose `evaluator.subqueryTimeRange` evaluates the inner
expression over `(start - offset - range, end - offset]` and whose
`evalSubquery` then hands the samples to the outer range-vector function as
a `MatrixSelector` that still carries the subquery offset. An offset on the
inner selector composes additively, as `subqueryTimes` documents.

`RangeManipulate`'s protobuf message has no offset field and recovers it on
decode from an immediately underlying `SeriesNormalize`. Since
`RangeManipulate` is commutative in `dist_plan` and can be pushed below a
`MergeScan`, insert that carrier node so the offset survives distributed
planning instead of decoding as zero.

Known divergence, unchanged by this commit: Prometheus anchors subquery step
points on absolute epoch multiples of the step, while GreptimeDB anchors them
on the evaluation start. The two agree whenever the offset is a multiple of
the subquery step; the added sqlness cases stay within that range.

Closes #9330

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Aarav <aaravsjadav@gmail.com>

* docs(promql): correct the subquery-offset rationale and pin the histogram shift

Follow-up to e132c267, which stated two things inaccurately and left one
path untested. No behaviour change beyond a conditional clone.

The `SeriesNormalize` carrier was described as existing "so the offset
survives distributed planning", implying standalone is unaffected. That is
wrong. `local_offset` is read on the substrait decode path
(`range_manipulate.rs`, `instant_manipulate.rs`), and GreptimeDB routes
PromQL plans through `MergeScan`/substrait in standalone too -- the
standalone results `promql/encode_substrait.result` and `precisions.result`
both show `MergeScan [is_placeholder=false, remote_input=[...]]`. Deleting
the carrier therefore empties subquery-with-offset results in standalone as
well, while `count_over_time_subquery_with_offset` keeps passing because it
asserts the pre-serialization plan. Say so at the node, so the next reader
does not remove it believing standalone is safe.

The carrier is also not semantically inert: `SeriesNormalizeStream::normalize`
shifts native histogram `start_timestamp` payloads by `offset`. On the
subquery path that runs on a computed inner result, on top of any offset the
inner selector already applied, and nothing pinned it. It is consistent --
`RangeManipulate` moves the millisecond time index forward by the same
`offset_ms` -- so the distance from a histogram's start timestamp to the
sample carrying it, which reset/rate detection reads, is invariant. Add
`subquery_offset_shifts_histogram_start_and_time_index_together`, which
executes the subquery's node stack over native histogram samples for a
zero and a non-zero inner offset. It cannot be a sqlness case: native
histograms are a struct column with no SQL type or literal
(`sql_data_type_to_concrete_data_type` rejects structs) and the sqlness
runner speaks only MySQL/Postgres, so such rows only arrive over
gRPC/remote-write v2.

The divergence note blamed sub-step offsets. Measured, that condition is too
narrow: an unaligned evaluation timestamp alone diverges, with no offset at
all -- `sum_over_time(fine[20s:10s])` at t=57 samples 47s and 57s here
against Prometheus's 40s and 50s. The real condition is that
`(start - offset)` is not a multiple of the subquery step, and it is
pre-existing: the committed `tql eval (359, 359, '1s')
sum_over_time(metric_total[60s:10s])` case is already an instance of it
(`359 - 60 + 10 = 309`). Reword the comment and the sqlness header
accordingly. A sub-step offset stays accepted: `foo[20s:10s] offset 5s` is
valid PromQL, erroring on it would be a regression and would not close the
gap anyway. This repo has no PromQL-compatibility documentation page, so
there is nowhere else to record it.

Also stop cloning the series key columns when there is no offset, and stop
the `offset 0s` sqlness comment implying its parser error is evidence about
this fix -- the same message comes from a `u32` overflow in
`promql-parser`'s shared duration check (`offset 9999999999d`).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Aarav <aaravsjadav@gmail.com>

* refactor(promql): trim subquery-offset comments and tests per review

Shorten the planner and sqlness comments to the semantic contract, build
`SeriesDivide` with a clone instead of an `Option`, and drop the
normalize.rs histogram test and the `offset 0s` parser case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Bvab9gpXYjKyLN8Mo6JWWL
Signed-off-by: Aarav <aaravsjadav@gmail.com>

* fix(promql): record the subquery offset as the range fold offset

The subquery's `RangeManipulate` now shifts the payload timestamps by the
offset, so `predict_linear` over an offset subquery must recover the
evaluation time with that offset instead of 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Bvab9gpXYjKyLN8Mo6JWWL
Signed-off-by: Aarav <aaravsjadav@gmail.com>

---------

Signed-off-by: Aarav <aaravsjadav@gmail.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
(cherry picked from commit d6474b961c)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: retry failed nightly release on following weekdays (#9394)

Previously the nightly release was scheduled only on Mondays, so a
failed run left users without nightly builds for a whole week.

Now the schedule triggers every weekday at 00:00 UTC, but the release
only proceeds when the latest published nightly release is older than
NIGHTLY_RELEASE_MAX_AGE_DAYS (5) days. The check runs in
allocate-runners before any EC2 runner is allocated, and gates all
schedule-driven jobs (including the Slack notification, so a skipped
nightly is silent). Tag pushes and manual dispatches are unaffected.

Signed-off-by: Ning Sun <sunning@greptime.com>
(cherry picked from commit a7f99b3425)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(client): yield Flight batches and affected rows without waiting for next message (#8918)

* fix(client): avoid Flight metrics lookahead stalls

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* refactor(client): extract trailing Flight metrics task

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(client): synchronize trailing metrics and bound cancellation

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

---------

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
(cherry picked from commit abf1396c28)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(promql): resolve dotted column names as unqualified columns (#9391)

* fix(promql): resolve dotted column names as unqualified columns

col(), From<&str>/From<String> for Column and string join keys go through
Column::from_qualified_name, which splits `service.name` into relation
`service` and column `name` and lowercases unquoted identifiers. Build
PromQL column references with Column::from_name / ident() instead.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(servers): resolve remote read matcher labels as unqualified columns

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(promql): cover same-name columns differing in case and without()

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit 218000e21b)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(promql): align timestamp(), label_join and label_replace with Prometheus semantics (#9385)

* fix(promql): align timestamp() and label_join with Prometheus semantics

- timestamp() over a selector reports the selected sample's timestamp, without adding the offset.
- timestamp() over any other expression reports the evaluation time instead of the input value.
- label_join that overwrites an existing label rejects duplicate label sets at runtime.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): align label_replace and label_join edge cases with Prometheus

- label_replace leaves non-matching series unchanged instead of copying the source value into the destination label.
- label_replace may overwrite an existing label; duplicate label sets are rejected at runtime like label_join.
- label_join with no source labels removes the destination label, and an empty source label name is rejected.
- Empty label values produced by these functions are NULL, the same as an absent label.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): join absent labels as empty strings in label_join

concat_ws skips NULL arguments together with their separator, so a label that is absent (NULL) dropped its separator, e.g. label_join(label_join(vector(1), "a", "", "missing"), "b", "-", "a", "a") lost b="-". Read every source label as coalesce(label, '') like label_replace does.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): skip rows without a sample in timestamp()

timestamp() replaced the input fields with the timestamp before the empty-value filter ran, so a row whose fields were all NULL got a timestamp, e.g. timestamp(-m) for a NULL sample. Filter such rows before the projection, for both the selector and the evaluation-time paths.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit 78a7b93292)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(mito2): truncate parquet column index min/max for SST writes (#9420)

* fix(mito2): truncate parquet column index min/max for SST writes

The SST writer and the bulk part encoder disabled column index truncation
together with statistics truncation in #6977. Mito never reads the column
index, but every page of a large string or binary column still stored its
full min and max values, uncompressed. Truncate the column index to the
parquet default of 64 bytes and keep chunk statistics untruncated, since
primary key pruning decodes them.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* docs(mito2): note that the column index must not be used to decode primary keys

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit a314ac4281)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: upgrade EC2 runner action to restore runner registration (#9430)

ci: update EC2 runner action for minimum runner version enforcement

Signed-off-by: evenyag <realevenyag@gmail.com>
(cherry picked from commit 0d502e1fc1)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(catalog): skip backend batch_get on full cache hits (#9431)

fix(catalog): return cached batch values without an empty backend call

Count backend batch_get invocations in regression tests and cover empty input.

Signed-off-by: WenyXu <wenymedia@gmail.com>
(cherry picked from commit acebd6b35b)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: make query regression non-blocking for scheduled nightly releases (#9434)

* ci: make query regression non-blocking for scheduled nightly releases

Scheduled nightly releases no longer gate publishing on the query
regression release test. The test still runs (validation policy stays
'all' for automatic releases) and uploads its report, but any result -
failure, timeout, cancellation, or skip - no longer blocks image
publishing or the GitHub release for schedule-triggered runs.

Since reusable-workflow caller jobs cannot use continue-on-error, the
gate is relaxed at the consumers instead: the if conditions of
release-images-to-dockerhub and publish-github-release now also pass
when github.event_name == 'schedule' regardless of the query-regression
result. All other gates (runner allocation, artifact builds,
prepare-release-validation) are unchanged.

Tag-push and manual-dispatch releases remain fully blocking unless a
skip policy is explicitly chosen via the release_validation input.

Signed-off-by: Ning Sun <sunning@greptime.com>

* ci: preserve downstream nightly release jobs after regression failures

Signed-off-by: evenyag <realevenyag@gmail.com>

---------

Signed-off-by: Ning Sun <sunning@greptime.com>
Signed-off-by: evenyag <realevenyag@gmail.com>
Co-authored-by: evenyag <realevenyag@gmail.com>
(cherry picked from commit a9ce6e2d01)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix: coalesce equal histogram quantile bucket boundaries (#9451)

* fix: coalesce equal histogram quantile bucket boundaries

Merge numerically equivalent bucket boundaries before monotonicity repair, preserving the allocation-free path for unique boundaries. Share coalescing with histogram_fraction and cover duplicate boundaries with unit and SQL regressions.

Fixes #9443

Signed-off-by: evenyag <realevenyag@gmail.com>

* fix: keep duplicate infinity buckets in one histogram group

Confirm classic histogram completion at a series/timestamp boundary or EOF instead of the first positive infinity. Retain lookahead across batches and reject incomplete optimistic groups so equivalent infinity labels reach numeric coalescing together.

Add stream and SQL regressions for duplicate infinity labels, including established bucket counts and EOF. Correct older fixtures to use distinct keys for distinct histogram groups.

Signed-off-by: evenyag <realevenyag@gmail.com>

* fix: avoid buffering histogram groups with invalid trailing bounds

Discover candidate bucket counts at the first group boundary and let optimistic validation fall back to safe mode for malformed bounds. Document discovery and cover trailing invalid and null labels with a focused streaming regression.

Signed-off-by: evenyag <realevenyag@gmail.com>

---------

Signed-off-by: evenyag <realevenyag@gmail.com>
(cherry picked from commit 97913b4b3a)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* test(sqlness): wait for mirrored inserts before flushing streaming flows (#9424)

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit b78dc4b33b)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix: accept a bool value for the CLI --auto-create-schema flag (#9450)

* fix: accept a value for the CLI --auto-create-schema flag

clap derives ArgAction::SetTrue for bool fields, so --auto-create-schema
took no value and could only set true, which is already the default.
Users could not turn automatic schema creation off from the CLI, unlike
metasrv's auto_create_schema = false.

Set action = clap::ArgAction::Set so the flag accepts an explicit
true/false value while keeping true as the default.

Signed-off-by: fengyue-xve <1759519137@qq.com>

* chore: enhance CLI argument for auto_create_schema option

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

---------

Signed-off-by: fengyue-xve <1759519137@qq.com>
Co-authored-by: dennis zhuang <killme2008@gmail.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
(cherry picked from commit 329edc88ec)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(otlp): coerce empty and "-" trace attribute strings to null (#9461)

* fix(otlp): coerce empty and "-" trace attribute strings to null

Instrumentations fill numeric attributes from absent headers with an empty
string (PSR-7 getHeaderLine returns ""). Coercing that into an existing
numeric column failed and rejected the whole span.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test: trim redundant row from empty-string rewrite test

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit d640ab8bc5)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(meta-client): bound heartbeat handshake timeout (#9438)

* fix(meta-client): bound heartbeat handshake timeout

Signed-off-by: Aetherance <inkToAether@gmail.com>

* chore(meta-client): fix test license header

Signed-off-by: Aetherance <inkToAether@gmail.com>

* test(meta-client): prefix heartbeat tests with test

Signed-off-by: Aetherance <inkToAether@gmail.com>

---------

Signed-off-by: Aetherance <inkToAether@gmail.com>
(cherry picked from commit 9c69e381ae)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* docs(config): align example configs and config.md with actual defaults (#9460)

* docs(config): align example configs and config.md with actual defaults

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(cmd): compare example defaults inside optional sections

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit e2fd8c5695)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(partition): preserve region mapping during repartition (#9458)

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit 90f117b957)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(promql): restore exact filtering for nullable label matchers (#9418)

* fix(promql): restore exact filtering for nullable label matchers

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test: verify compound filter semantics and last-row selection

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test: consolidate label filter regression coverage

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* refactor: separate filter evaluation and timestamp adaptation

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* refactor: inline predicate and timestamp tests

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* refactor: keep direct IN predicates outside simple filters

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix: coerce partition bounds before simple filtering

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test: cover partition bound overflow after timestamp widening

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit 526dd48bda)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* chore: update rust toolchain to 1.98.1 (#9429)

(cherry picked from commit 7f0f592b6b)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: update dev-builder image tag (#9469)

Signed-off-by: greptimedb-ci <greptimedb-ci@greptime.com>
Co-authored-by: greptimedb-ci <greptimedb-ci@greptime.com>
(cherry picked from commit 5affb50bb5)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(metasrv): retry failed heartbeat address updates (#9437)

* fix(metasrv): retry failed heartbeat address updates

Signed-off-by: WenyXu <wenymedia@gmail.com>

* refactor(metasrv): move shared heartbeat helpers to utils

Signed-off-by: WenyXu <wenymedia@gmail.com>

---------

Signed-off-by: WenyXu <wenymedia@gmail.com>
(cherry picked from commit 65b9a35692)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(operator): bound FlowMirrorTask pending rows to prevent frontend OOM (#9421)

* fix(operator): bound FlowMirrorTask pending rows to prevent frontend OOM

When flownode is slow or unreachable, FlowMirrorTask::detach spawns
unbounded tasks that each pin a full insert batch for ~10s (gRPC
timeout). With continuous writes, in-flight memory grows unboundedly
(throughput x 10s x peers), causing frontend-write OOMKill and a
million-per-hour ERROR storm.

Add a MAX_MIRROR_PENDING_ROWS cap (1M rows) on Inserter: detach
fetch_adds the batch's row count before spawning; if the cap is
exceeded the reservation is rolled back, the batch is dropped, a
DIST_MIRROR_DROPPED_ROW_COUNT metric is bumped, and a warn is logged
instead of spawning. On normal completion the reservation is released
in both success and error paths.

This keeps core writes alive while degrading flow mirror writes to
best-effort, which is the intended trade-off: protect the primary
insert path at the cost of possible flow aggregation gaps.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* fix: address review - balanced reservation, gauge race, log rate limit

P1: Compute pending_rows() from actual per-peer payloads in
self.requests instead of the pre-fanout num_rows field. This fixes
under-reservation for same-table multi-region batches and over-release
in multi-flownode fanout. Each peer task now releases exactly its own
share so reservations and releases balance.

P2: Replace load+set gauge publication with add/sub symmetry.
reserve_mirror_pending_rows does fetch_add + gauge.add, release does
fetch_update + gauge.sub, eliminating the stale-value race.

P3: Rate-limit the dropped-batch warning to at most once per 10s via
MirrorDropLog (atomic compare_exchange), accumulating batch/row counts
between reports.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test: fix race in dual-peer test - wait for gauge release before unlocking

The dual-peer test previously only waited for the pending budget to
return to its start value before releasing MIRROR_METRIC_TEST_LOCK.
But release_mirror_pending_rows moves the budget before the gauge, so
a worker's gauge.sub could still be in flight when the next test
acquires the lock and records its baseline, polluting the result.

Now waits for both pending == start AND gauge == gauge_before.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* fix(operator): preserve instant writes and limit mirror failure logs

Reject saturated requests containing instant-TTL rows with the existing retryable write-admission error before mirror or datanode dispatch. Keep normal source-table inserts independent of best-effort mirror drops.

Reuse a smaller process-wide event limiter for both dropped batches and failed peer tasks, reporting at most once per 10 seconds with failure count, peer context, and error chain. Cover actual mirror construction, mixed and instant admission, fast/slow peer shares, failed-task release, and log concurrency.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* fix(operator): reject oversized instant mirror batches permanently

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

---------

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
(cherry picked from commit 198d92407c)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(promql): align subqueries to the absolute evaluation grid (#9400)

* fix(promql): align subqueries to the absolute evaluation grid

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* docs(promql): streamline subquery comments

Remove redundant arithmetic narration and restated test comments. Keep grid, fallback, validation and overflow contracts, and preserve all regression tests and assertions. Regenerate SQLness output only for comment echoes.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

---------

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
(cherry picked from commit 0d2cb1aa7e)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* build(deps): update opensrv-mysql to fix MariaDB 12.3 USE information_schema hang (#9357)

Update opensrv-mysql from f3135e5 to 1f3d88c, which replies a proper EOF packet to COM_FIELD_LIST instead of a malformed OK packet that MariaDB 12.3 misparses and hangs on USE information_schema.

Fixes #9158

Signed-off-by: tian1220A <1573612521@qq.com>
(cherry picked from commit 21536a092b)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(promql): keep vector labels when a computed scalar is on the right (#9478)

* fix(promql): keep vector labels when a computed scalar is on the right

`metric + time()` or `metric * scalar(other)` planned the join with the
scalar side's context, which has no tags, so every series collapsed into
one unlabeled series. Use the vector side's context when the right operand
is scalar-typed.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(promql): update results for labels kept with a computed scalar on the right

v * pi() and v + time() now keep the vector's labels and time index name,
as in Prometheus.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): qualify columns of an unnamed operand in vector joins

A comparison filter's projection has no table name, so after the join its
time index resolved unqualified and was ambiguous when the other operand had
the same column. Also keep the empty-metric branch unchanged for scalar
left operands.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(promql): cover comparison filters joined with a scalar

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit 6da5352c53)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(otlp): convert boolean metric attributes into label values (#9468)

Signed-off-by: fengyue-xve <1759519137@qq.com>
(cherry picked from commit 2ebb03fbf6)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* perf(catalog): optimize single-table lookups in information_schema.partitions (#9473)

* perf(catalog): optimize single-table lookups in information_schema.partitions

Signed-off-by: 风月 <1759519137@qq.com>

* style: apply rustfmt formatting to partitions tests

Signed-off-by: 风月 <1759519137@qq.com>

---------

Signed-off-by: 风月 <1759519137@qq.com>
(cherry picked from commit 4ade13d9b6)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* test(promql): cover label_replace normalization with distinct series (#9485)

The duplicate-label-set rejection must not fire when a label rewrite keeps
every series distinct through another label. Normalizing mixed histogram
boundary representations (`10.0` -> `10`) while `instance` still differs is
the false positive reported in #9444. The check already accounts for it
since it became a per-timestamp row count instead of a planning-time
rejection, but no fixture covered the positive case. Add one, together
with the aggregation variant that produces a genuine duplicate and must
still fail.

Signed-off-by: fengyue-xve <1759519137@qq.com>
(cherry picked from commit 764ab8bc1a)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(promql): return no point for zero-interval irate/idelta (#9455)

fix(promql): irate/idelta return no point for a zero-ms sample gap

Signed-off-by: Niladri Adhikary <niladrix719@gmail.com>
(cherry picked from commit 0dde3a7b70)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(metasrv): stop parking the heartbeat response on the region supervisor queue (#9474)

* fix(meta-srv): stop parking the heartbeat response on the region supervisor queue

Signed-off-by: lczllx <2181719471@qq.com>

* test(meta-srv): cover the heartbeat response path at the handler level

Signed-off-by: lczllx <2181719471@qq.com>

---------

Signed-off-by: lczllx <2181719471@qq.com>
(cherry picked from commit d1151b0ed6)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(promql): keep metric name for comparison operators (#9332)

* fix(promql): keep metric name for comparison operators

`collect_metric_names` dropped the response `__name__` for every binary
operator except `and` / `or` / `unless`, so comparisons such as
`node_load1 > 5` came back without the metric name. Grafana rendered them
as `{instance="..."}` and alert templates reading `$labels.__name__` got
nothing.

Follow Prometheus' `shouldDropMetricName` / `resultMetric`: arithmetic
operators drop the name, comparisons keep the left-hand name, except when
the comparison returns a bool or is a one-to-one `on(...)` match. A
`group_left` / `group_right` modifier does not reduce the labels, so the
name is kept there.

Both the HTTP PromQL API and the gRPC gateway share
`retrieve_metric_name_and_result_type`, so one fix covers both.

Signed-off-by: Aumnertic <hznuzc@gmail.com>

* docs: document binary_keeps_metric_name

Signed-off-by: Aumnertic <hznuzc@gmail.com>

* fix(promql): handle on(__name__), scalar lhs and group_right

Signed-off-by: Aumnertic <hznuzc@gmail.com>

* chore(promql): correct result-side docs and drop unreachable String arm

Signed-off-by: Aumnertic <hznuzc@gmail.com>

* fix(promql): drop metric name when explicitly ignored

Handle ignoring(__name__) for one-to-one comparisons and cover
comparison, group_left, group_right and unless behavior.

Signed-off-by: Aumnertic <hznuzc@gmail.com>

---------

Signed-off-by: Aumnertic <hznuzc@gmail.com>
(cherry picked from commit c33a6cfa56)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* perf(promql): reduce timestamp payload allocations and use narrow cursor scans (#9417)

* perf(promql): zero-copy timestamp payload for millisecond zero-offset ranges

RangeManipulate rebuilt the timestamp range payload per sample through
wide-precision i128 conversion even for millisecond input with zero
offset, where the input is already millisecond and nothing changes.
That turned a previously zero-copy path into a per-sample loop plus a
Vec allocation, regressing range-function queries.

Reuse the input timestamp array directly when the time index is already
millisecond and the offset is zero. Other units and non-zero offsets
keep the wide-precision conversion (required so ns/us data near the
native type limits stays correct under large offsets).

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
Signed-off-by: discord9 <discord9@163.com>

* perf(promql): vectorize timestamp payload with plain i64

RangeManipulate rebuilt the timestamp range payload per sample through
wide-precision i128 conversion, turning a hot path into a scalar loop
with a Vec allocation. Realistic timestamps and offsets stay far inside
i64, so the wide path is unnecessary.

- millisecond + zero offset reuses the input array with zero copy;
- other units / offsets shift in one vectorized i64 pass (no i128, no
  per-sample Option, no overflow branch).

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
Signed-off-by: discord9 <discord9@163.com>

* perf(promql): check timestamp payload overflow via arrow try_unary

Replace the silent wrapping shift with try_unary + checked_mul/checked_add.
checked arithmetic lowers to a per-lane overflow flag so the single pass
still vectorizes over the dense i64 buffer; any overflow short-circuits to
an error instead of silently wrapping to a wrong timestamp. No i128, no
per-sample Option.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
Signed-off-by: discord9 <discord9@163.com>

* perf(promql): vectorize cursor scans with i64 fast path

RangeManipulate::calculate_range and InstantManipulate::manipulate
compared every sample through i128 wide-precision arithmetic. For
realistic timestamps and offsets the shifted values stay far inside
i64, so the wide path was pure overhead.

Split both scans into an i64 fast path (checked arithmetic precomputed
once per batch) and an i128 exact fallback. Any input that would
overflow i64 falls back to the i128 scan, keeping window membership
semantics identical.

Verified: 47/47 range_manipulate + instant_manipulate tests, 275/275
promql crate tests, 1612 randomized cases byte-identical to i128 scan.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
Signed-off-by: discord9 <discord9@163.com>

* refactor(promql): generalize cursor scans and share endpoint precheck

Address review comments on PR #9417:
- Generalize scan_ranges/scan_instants over Nanoseconds (i64/i128),
  eliminating the duplicated scan bodies.
- Extract shared narrow_scan precheck used by both RangeManipulate and
  InstantManipulate, with identical checked endpoint logic.
- Trim doc comments to one line; fix factual errors about fallback
  trigger (2262 for ms/s, not native type limits).

Semantics unchanged: same cursor algorithm, same overflow fallback,
same i64-vs-i128 selection, same stale/null/tie handling.

Verified: 47/47 range_manipulate + instant_manipulate tests, clippy clean.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
Signed-off-by: discord9 <discord9@163.com>

* perf(promql): vectorize payload with try_unary and add scan carrier bench

Replace the per-sample i128 payload loop with a single arrow try_unary
pass: i128 intermediate holds any native timestamp shift, only the final
millisecond result must fit i64. This is semantically equivalent to the
original i128 implementation (no silent wrap, no per-sample branching).

Add cursor_scan_carriers A/B benchmark: i64 fast path vs i128 exact
fallback on four workloads. Add five deterministic differential tests
pinning scan_ranges::<i64> and scan_ranges::<i128> to be pointwise
equivalent on boundary shapes (sub-ms samples, fallback triggers,
nanosecond extremes, seeded precision matrix).

Bench (N=4096, 15s cadence):
- payload wall_time 20x15s: 74.94us -> 48.98us (-35%)
- payload wall_time 240x15s: 102.44us
- scan i64 fast path: 10.97us vs i128 19.84us (1.81x)

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* perf(promql): restore zero-copy payload for ms+offset=0 and fix comment

Restore the zero-copy fast path for millisecond timestamps with zero
offset, guarded by the input column's exact data type (Timestamp
(Millisecond, None)) rather than self.time_unit. The old time_unit guard
would accept a tz-carrying millisecond column and produce a dictionary
value type disagreeing with the output schema; the datatype guard is the
correct check.

Also fix the comment: arrow's try_unary is a fallible kernel that LLVM
cannot auto-vectorize; the win over the hand-written loop comes from
eliminating the per-sample Option/Vec and branches, not SIMD.

Add a regression test with a timezone-carrying millisecond index to
prove it falls back to the try_unary path.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* perf(promql): replace try_unary with unary + Cell overflow flag

Arrow's try_unary is a fallible kernel that LLVM cannot auto-vectorize.
Replace it with the infallible unary kernel: the closure is a plain
i64 -> i64 mapping that sets a Cell<bool> on overflow instead of
returning Err per sample. The flag is checked once after the loop.

This removes the per-lane Result construction and lets LLVM vectorize
the shift+truncate sequence. Bench (N=4096, 15s cadence):
- window4x15s: 42.12us -> 28.77us (-32%)
- window20x15s: 48.98us -> 27.68us (-43%)
- window240x15s: 102.44us -> 27.61us (-73%)
- regular_sparse: 90.04us -> 29.20us (-68%)

All cases now run at ~27-29us regardless of window size; the zero-copy
path dominates for ms+offset=0.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* perf(promql): dispatch payload conversion on null_count; drop wall-time bench

Null-free batches (dominant PromQL case) take arrow's vectorizable unary
kernel with a Cell<bool> overflow flag; nullable batches take try_unary so
a NULL slot's arbitrary underlying value cannot raise a false batch-wide
overflow error. Add a regression test exercising both paths.

Remove bench_range_manipulate_wall_time (all-ms zero-offset cases only
exercised the zero-copy path, measuring nothing about the conversion) and
its orphaned helpers; restore the cursor-scan bench helpers it shared.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* fix(promql): keep nullable timestamp cursor scans on i128

NULL slots may contain unordered arbitrary values, so the endpoint-only
narrow-scan proof cannot bound their interior timestamp arithmetic. Both
range and instant scans now attempt i64 only for null-free timestamp arrays
and otherwise retain the existing exact i128 path.

Add focused interior-NULL regressions through both active entrypoints,
checking concrete selections and output against the existing wide scan.
The null-free fast path and timestamp payload conversion are unchanged.

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>
(cherry picked from commit b3c756a7dd)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(metasrv): stop holding the pusher registry lock across mailbox sends (#9445)

fix(meta-srv): stop holding the pusher registry lock across mailbox sends

Signed-off-by: lczllx <2181719471@qq.com>
(cherry picked from commit 7605f27608)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(mito): flush memtables containing skip-WAL writes on region close (#9440)

* fix(mito): flush unlogged writes when closing regions

Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(mito): align skip-WAL naming and metric recovery tests

Signed-off-by: WenyXu <wenymedia@gmail.com>

---------

Signed-off-by: WenyXu <wenymedia@gmail.com>
(cherry picked from commit a026b3b280)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: enhance benchmark workflows with version comparisons and diagnostics (#9383)

* ci: update Agent and Traces benchmark runtime

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: use unrestricted benchmark runtimes by default

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: retry benchmark image pulls before generating datasets

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: increase tracesbench defaults to 8 CPU and 32 GiB

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: configure tracesbench query timeout and update runtime

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: update tracesbench runtime and configure import scan workers

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: use shared parallel tracesbench import preparation

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: update tracesbench runtime to preserve Tempo trace requests

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: summarize tracesbench query results and lifecycle

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: show benchmark resource usage and update parallel loader runtime

Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(ci): resolve shared summary module in existing test harness

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: select tracesbench engines and GreptimeDB image version

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: update benchmark runtime with parallel Agent preparation

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: select benchmark runtime registry by cloud provider

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: retry long-range benchmark image pulls

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: compare baseline and candidate GreptimeDB benchmarks

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: pin published dual-version benchmark runtime

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: prepare agent benchmark indexes with updated runtime

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: validate agent admission and render traces version comparison

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: use runtime with traces index preparation

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: use consolidated runtime for benchmark version reports

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test: add license header to benchmark comparison tests

Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(ci): read legacy Tempo ingestion metrics in traces summaries

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: require tracesbench query plan artifacts

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: use optimized tracesbench report runtime

Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: update tracesbench runtime to merged report validation

Signed-off-by: WenyXu <wenymedia@gmail.com>

---------

Signed-off-by: WenyXu <wenymedia@gmail.com>
(cherry picked from commit 6e1a137411)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(promql): accept evaluation times before the Unix epoch (#9480)

* fix(promql): accept evaluation times before the Unix epoch

Prometheus accepts negative timestamps in query parameters; GreptimeDB
rejected them while parsing and would panic converting them in the planner.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test: update flow_tql error for a negative TQL end time

A negative end now parses and fails the end >= start check instead of
the timestamp conversion.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): reject evaluation times outside the millisecond range

The signed millisecond conversion cast u128 to i64 unchecked, so a far-past
time wrapped into the future. Reuse the checked conversion from
label_values and return an error instead.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* chore: drop an unused import

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): round query times to milliseconds like Prometheus

Numeric times round the fraction of a second to milliseconds, and
pre-epoch times floor sub-millisecond parts, matching Prometheus'
parseTime and timestamp.FromTime.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): round the float fraction before converting to nanoseconds

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test: skip the overflow case where SystemTime cannot hold it

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit a8e293fd79)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix: unify unknown flow option rejection across OSS and enterprise builds (#9319)

* fix(operator): unify unknown flow option rejection across OSS and enterprise builds

Remove the enterprise-gated passthrough that let unknown flow options
through SQL-layer validation. Both OSS and enterprise binaries now reject
unknown flow options with a single 1004(InvalidArguments) error at the
SQL layer, producing identical sqlness output.

The metasrv-side validate_flow_options remains as a defensive check.

Signed-off-by: discord9 <discord9@163.com>

* fix(flow): use DISTINCT output columns as sink key for streaming flows

When a streaming flow's source table has no primary key, sink key
inference produced an empty key. For SELECT DISTINCT v, the DISTINCT
establishes v as the output key, but the inference only looked at
source primary keys. Combined with the zero-valued placeholder
timestamp, all output rows shared the same storage identity and were
deduplicated into a single row.

Use all non-time-index output columns as the sink key when the plan
is a DISTINCT and no source primary key was inherited.

Signed-off-by: discord9 <discord9@163.com>

* fix(flow): preserve extension options and complete DISTINCT sink identity

Keep enterprise CREATE FLOW options available to the existing handler and report unknown ordinary options as InvalidArguments with the OSS SQL error text. Infer DISTINCT sink keys from every selected non-time-index output column, including when source key columns survive. Wait for asynchronous mirrors in show_create_flow and exercise 1000 distinct sink rows in the SQL regression.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(flow): align meta unknown option status assertions

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(flow): use valid aliases in DISTINCT sink regression SQL

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(flow): record complete DISTINCT sink SQL results

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

---------

Signed-off-by: discord9 <discord9@163.com>
Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
(cherry picked from commit 178a0021b8)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(promql): return range-vector results for instant queries (#9477)

* fix(promql): return range-vector results for instant queries

An instant query of a range selector or subquery (e.g. `up[5m]`) returned
the internal range arrays, which panicked when the frontend decoded them as
dictionaries. Plan such queries as the selected samples with their own
timestamps, and reject range-vector expressions in range queries like
Prometheus does.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(promql): keep encode_substrait on a valid range query

A range query of a range-vector expression now fails like in Prometheus.
Wrap the subquery in max_over_time so the case still encodes both
RangeManipulate nodes through substrait.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): plan wrapped range-vector roots and reject a zero subquery step

TQL EXPLAIN, ANALYZE and AS alias wrap the root in an extension, which the
range-vector path did not unwrap. A zero subquery step divided by zero.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(promql): cover wrapped range-vector roots and a zero subquery step

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): default subquery step and reject range vectors in range queries

A root subquery without a step uses the one-minute default instead of
the query step. The HTTP range query endpoint rejects range-vector
expressions even when start equals end.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test: cover root subquery default step

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* refactor(promql): align root subqueries with subquery_child_window

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): return millisecond timestamps for range-vector results

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* refactor(promql): keep the time index timezone and name its table in errors

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit b913be2af7)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(query): stop reporting region scans as hash partitioned (#9491)

* fix(query): stop reporting region scans as hash partitioned

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(query): use current DataFusion plan downcasts

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(query): match null rendering in join regression

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(query): refresh union plan for unknown scan partitioning

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(query): refresh shared plans after distribution rule removal

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(query): cover semi joins with remote dynamic filtering

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(query): strengthen partitioned merge scan regressions

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(query): remove unused merge scan test helpers

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(perf): measure merge scan hash repartition cost

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* fix(query): adapt merge scan deserialization to two-arg constructor

Adapt the production and test serializer callers added by #9206 and remove the obsolete partition_cols assertion.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(query): adapt nested merge scan integration constructor

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

---------

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
(cherry picked from commit 32e8467cda)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(table): require complete primary keys for hash partitioning (#9507)

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
(cherry picked from commit 28ec3ff5aa)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(deps): bump OpenDAL to 0.59.4 for OSS RRSA credentials (#9520)

Signed-off-by: WenyXu <wenymedia@gmail.com>
(cherry picked from commit e6f3ca10a5)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(promql): default a stepless subquery to a one-minute step (#9481)

* fix(promql): default a stepless subquery to a one-minute step

`x[5m:]` evaluated the inner expression at the query step, which is 1s
for instant queries. Prometheus uses the evaluation interval, one minute
by default.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(promql): cover the default subquery step

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(promql): cover the default subquery step in a range query

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit 845fc0af9c)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(promql): follow IEEE 754 float semantics in comparisons and aggregations (#9479)

* fix(promql): follow IEEE 754 float semantics in operators and aggregations

- Comparisons: Arrow orders floats totally (NaN == NaN, NaN > +Inf); PromQL
  comparisons with NaN are false except !=.
- min/max: skip NaN unless the whole group is NaN. DataFusion's grouped
  min/max let NaN replace and be replaced by neighbours, so the result
  depended on input order.
- topk/bottomk rank NaN last.
- round() rounds ties up and returns NaN for a zero step, like Prometheus.
- sqrt() of a negative number is NaN instead of an error.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): round() without a step rounds to integers

The planner passed a zero step for single-argument round(), which now
yields NaN like Prometheus; pass 1 instead. Found by the compatibility harness.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): apply the sqrt guard to builtin functions and register min/max for plan decoding

sqrt resolves through the session's builtin functions, not the UDF branch.
Distributed plans are decoded with the serializer's session, which did not
know prom_min/prom_max.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(promql): cover NaN and rounding semantics

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): keep the nested count rewrite for PromQL min/max

count(min(x) by (l)) was rewritten by count_nest_aggr only for the builtin
min/max names. prom_min/prom_max return NULL for the same groups, so the
rewrite stays valid. Update plan snapshots for the IEEE comparison, sqrt,
topk and min/max changes.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): register prom_min/prom_max in the function registry

Distributed plans split an aggregation into state and merge steps only for
functions whose state function exists in FUNCTION_REGISTRY; prom_min and
prom_max lived in the promql crate, so min/max stopped being pushed down to
datanodes. Move them to common-function and register them before the state
functions are derived.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test: update sqlness results for PromQL float semantics

Plans and column names now show prom_min/prom_max, the NaN guards of
comparisons and sqrt, and the NaN sort key of topk. round() results follow
Prometheus' formula. Distributed min/max keep the state/merge split.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): keep column names and NULLs in IEEE comparisons and sqrt

Rewriting comparisons as `a op b AND NOT (isnan(a) OR isnan(b))` changed
TQL output column names and turned NULL operands into true for `!=` and
false for the bool forms. Implement comparisons and sqrt as PromQL UDFs
that propagate NULL and report the column name of the expression they
replace; prom_min/prom_max report min/max column names the same way.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test: restore column names in sqlness results for PromQL float semantics

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(promql): share the prom_min/prom_max groups accumulator with tests

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(promql): keep distinct names for prom_min/prom_max

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit f3a0841f4f)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(query): keep filter fetch when rewriting matches term (#9521)

* test: cover matches term limit dropped by constant rewrite

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* fix(query): keep filter fetch when rewriting matches term

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

---------

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
(cherry picked from commit 483d1e49a3)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix: adapt release OptionMap iteration to Rust 1.98

Signed-off-by: WenyXu <wenymedia@gmail.com>

* build: reduce checkout-specific inputs for cache reuse (#9198)

* test(mito2): cover regex inverted index pruning

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* build: keep build inputs checkout-relative for cross-worktree cache reuse

Two small changes so compilations are byte-identical across worktrees
of the same commit and can share a build cache (mbx, sccache):

- src/common/version/build.rs: derive the workspace root relative to
  CARGO_MANIFEST_DIR instead of the CARGO_WORKSPACE_DIR injected by
  .cargo/config.toml (an absolute path). The env var remains as an
  explicit override for the submodule case; the [env] injection in
  .cargo/config.toml is removed so the relative derivation is used by
  default.
- Replace three env!("CARGO_MANIFEST_DIR") uses in test/bench code
  (servers prom_decode bench + decode test, common-meta test_util)
  with a runtime std::env::var lookup, so the compiled artifact no
  longer embeds the checkout path.

No behavior change: same files are read, same version info emitted.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* build: address cross-worktree cache review feedback

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

---------

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
(cherry picked from commit f50f42140a)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(mito): preserve version ordering in bulk merges and flushes (#9517)

* fix(mito): preserve delete barriers in flat last-non-null dedup

Signed-off-by: evenyag <realevenyag@gmail.com>

* fix(mito): respect sequence ranges in bulk merges and flushes

Signed-off-by: evenyag <realevenyag@gmail.com>

* perf(mito): avoid merge selection on the bulk write path

Signed-off-by: evenyag <realevenyag@gmail.com>

---------

Signed-off-by: evenyag <realevenyag@gmail.com>
(cherry picked from commit 674ba72669)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* ci: benchmark the object store WAL against S3 from an EC2 runner (#9536)

* ci: benchmark the object store WAL against S3 from an EC2 runner

Add a workflow_dispatch workflow that loads the o11ybench agent observability
dataset into a fresh GreptimeDB container per cell and compares three WAL
configurations that differ only in [wal]: Raft Engine with sync_write on the
instance store, and the object store WAL in durable and enqueued mode, with
the data files on S3 in the runner's region for all three. Rounds alternate
the configuration order. A summary reports rows per second, D/R and E/R as
the geometric mean over rounds, append acknowledgement p50 and p99, and the
object store WAL counters. A smoke mode runs the same chain on ubuntu-latest
against the MinIO fixture.

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* ci: scrape WAL metrics on failed loads and keep logic out of the benchmark YAML

Scrape the object store WAL metrics before stopping a cell's server whether
the load succeeded or failed, so failed cells keep their stall, poison and
create failure counters. Build the summary from the plan so that cells which
never ran are listed and leave their ratios undefined. Move budget, region,
runtime image, manifest and credential scan logic from the workflow into the
helper scripts, and drive the cell lifecycle in the tests through fake docker
and curl binaries.

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* ci: drop redundant required flags from benchmark inputs

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* ci: fail a benchmark cell whose metrics scrape fails and zero untouched WAL counters

A load that succeeds without its metrics scrape now fails the cell with exit
code 4 and records metrics_scraped in cell.json. The summary reports an
event counter that never registered as zero when the cell is an object store
WAL cell whose scrape succeeded and created objects, and as unknown otherwise.
Planned cells that never ran are read through the same path as recorded ones.

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* ci: register the S3 benchmark workflow for dispatch from its branch

A workflow_dispatch workflow can only be dispatched once GitHub has
registered it, which happens on the default branch or when one of its
triggers fires. A push trigger limited to this branch registers it; validate
runs only for workflow_dispatch, so a push run skips every job.

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* ci: delete the benchmark S3 prefix one object at a time

boto3 now sends a CRC checksum instead of Content-MD5 with DeleteObjects,
which the MinIO fixture of the smoke run rejects. DeleteObject needs neither
and the prefix of one run holds at most a few thousand objects.

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* ci: default the S3 benchmark to c6id.2xlarge

c7id is not offered in the runner region ap-southeast-1, where DescribeInstanceTypes
rejects it. c6id is the compute optimized x86 family with a local NVMe instance
store that the region offers, with the same 8 vCPU and 16 GiB at 2xlarge.

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* ci: drop the registration trigger of the S3 benchmark workflow

The workflow is registered now, so dispatches no longer need a push trigger
on its branch.

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* ci: fail an S3 benchmark run on EC2 without a local instance store

Without an instance store the Raft Engine WAL of the R cells silently stayed
on the root EBS volume, so the run measured a different baseline. Only a smoke
run may keep it on the root volume now.

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* ci: let the instance store check fail its workflow step

The step piped wal-disk into tee without pipefail, so a rejected host still
passed. wal-disk now writes its record itself and the step runs it alone.

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

---------

Signed-off-by: jeremyhi <fengjiachun@gmail.com>
(cherry picked from commit c1a0ca9595)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(mito2): ignore missing inverted indexes in old SSTs (#9545)

Signed-off-by: WenyXu <wenymedia@gmail.com>
(cherry picked from commit 287ced2de2)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix: prevent stale leader metadata after concurrent writes (#9549)

fix: fence leader cache fills around store mutations

Signed-off-by: jeremyhi <fengjiachun@gmail.com>
(cherry picked from commit 394562aa9e)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(promql): stop fabricating histogram fold hash partitioning (#9509)

* fix(promql): stop fabricating histogram fold hash partitioning

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(promql): cover histogram fraction binary join missing rows

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(promql): normalize dictionary labels in histogram regression

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test: cover histogram joins on logical metric tables

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test: specify insert columns for logical metric fixture

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test: use supported histogram child replacement API

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(promql): keep histogram fold fixtures truthful

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

---------

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
(cherry picked from commit 2039f508fc)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* feat(client): support region request reuse and route refresh hints (#9540)

* feat(client): support shared region requests without deep cloning

Signed-off-by: WenyXu <wenymedia@gmail.com>

* feat: propagate route refresh retry hints

Signed-off-by: WenyXu <wenymedia@gmail.com>

* style: preserve local error variant imports in retry hints

Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(mito2): distinguish staging rejection from write backpressure

Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(meta): preserve legacy retry hints in instruction replies

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test(client): inline retry hint compatibility cases

Signed-off-by: WenyXu <wenymedia@gmail.com>

* test(datanode): align batch-open retry hint assertion

Signed-off-by: WenyXu <wenymedia@gmail.com>

* style: use imported types in region request tests

Signed-off-by: WenyXu <wenymedia@gmail.com>

---------

Signed-off-by: WenyXu <wenymedia@gmail.com>
(cherry picked from commit 684c8ff291)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* refactor(operator): centralize write partitioning and dispatch (#9541)

* refactor(operator): centralize write partitioning and dispatch

Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(operator): preserve write dispatch behavior during refactor

Signed-off-by: WenyXu <wenymedia@gmail.com>

* style(operator): import write dispatch error paths

Signed-off-by: WenyXu <wenymedia@gmail.com>

* style(operator): align dispatch imports and test utilities

Signed-off-by: WenyXu <wenymedia@gmail.com>

* style(operator): use imported types in partition tests

Signed-off-by: WenyXu <wenymedia@gmail.com>

---------

Signed-off-by: WenyXu <wenymedia@gmail.com>
(cherry picked from commit a2d28e6060)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* feat(operator): retry failed writes with refreshed partitions (#9542)

* feat(operator): retry failed writes with refreshed partitions

Signed-off-by: WenyXu <wenymedia@gmail.com>

* style(operator): simplify retry test type references

Signed-off-by: WenyXu <wenymedia@gmail.com>

* refactor(operator): clarify write request rebuilding names

Signed-off-by: WenyXu <wenymedia@gmail.com>

* refactor(operator): encapsulate write retry attempts

Signed-off-by: WenyXu <wenymedia@gmail.com>

* style(operator): group inserter configuration methods

Signed-off-by: WenyXu <wenymedia@gmail.com>

* refactor(operator): share routed peers and separate retry decisions

Signed-off-by: WenyXu <wenymedia@gmail.com>

* refactor(operator): avoid temporary write routing allocations

Signed-off-by: WenyXu <wenymedia@gmail.com>

---------

Signed-off-by: WenyXu <wenymedia@gmail.com>
(cherry picked from commit d86741d0ad)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* feat(frontend): retry transient distributed write failures (#9543)

* refactor(operator): route shared requests through NodeManager

Remove the concrete NodeClients requester API and use the existing node manager for owned and shared requests. Preserve the original write hint while the legacy error adapter remains in place.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(meta): preserve retry policy through error hints

Propagate client errors through External and let DDL read retry hints while preserving cleanup and terminal-child policies. Region transport failures retain the legacy Retryable classification, which now also participates in bounded write retries.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(operator): restore sparse partition tags when rebuilding writes

Decode retained primary keys when refreshed partition expressions need different tags. Keep the partitioner and multi-region sparse write regression tests with the fix.

Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(operator): stop detached write retries when callers cancel

Signed-off-by: WenyXu <wenymedia@gmail.com>

* feat(frontend): configure retries across distributed write paths

Wire the shared dispatcher through frontend and logical batching, preserving partition columns and request headers. Keep standalone retries disabled and include configuration, batching, and storage regression coverage.

Signed-off-by: WenyXu <wenymedia@gmail.com>

---------

Signed-off-by: WenyXu <wenymedia@gmail.com>
(cherry picked from commit 863eebdecb)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(table): stop claiming tag order for logical metric scans (#9522)

* test: cover metric logical scan tag ordering

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* fix(table): only claim projected primary key prefix ordering

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* fix(table): keep physical key ordering for metric projections

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(table): compare unknown partition counts explicitly

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(table): model scoped internal TSID scan properties

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

---------

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
(cherry picked from commit 75bc4ce46d)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(promql): stop claiming scalar calculate preserves order (#9523)

* test: cover scalar calculate order contract

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* fix(promql): stop claiming scalar calculate preserves order

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* docs(test): describe scalar ordering regression as pre-fix behavior

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

---------

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
(cherry picked from commit 0e40281262)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix: align fulltext bloom tokens with matches_term and use them for LIKE (#9470)

* fix: align fulltext bloom tokens with matches_term and use them for LIKE

The bloom fulltext index stored tokens that did not follow
matches_term's word boundaries (`_`, ASCII next to Han, Han substrings),
so pruning dropped rows matches_term accepts.

Tokens and query probes now share the character classes of
matches_term, Han runs are indexed as bigrams, and LIKE patterns use the
same probes. The bloom blob is written under a new key; blobs from older
versions are no longer probed. Tantivy no longer serves matches_term.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* chore: drop unused lazy_static from index and avoid typo-flagged test tokens

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* docs: link like_probes to tokenize instead of the downstream ScriptTokenizer

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* refactor: name probe extraction with verbs and document CharClass

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* feat: rebuild legacy fulltext bloom indexes and probe lower(col)

The manifest records the fulltext bloom version per column, so ADMIN
BUILD_INDEX now rebuilds SSTs whose bloom index predates the current
tokens, and ssts_index_meta shows the version.

Case-insensitive bloom indexes also store the tokens of the whole
lowercased text for rows where it can differ (U+0130, U+212A, sigma),
so matches_term(lower(col), ..) and lower(col) LIKE .. use the index.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test: cover legacy bloom index rewrite by compaction in the compat case

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* chore: log outdated fulltext bloom indexes in the consistency check

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* docs: explain the unrecorded fulltext bloom version

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit 5c4d9eb4f8)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(query): guard deep expressions during query planning (#9564)

* fix(query): avoid speculative clones in constant normalization

Signed-off-by: evenyag <realevenyag@gmail.com>

* fix(query): reject deeply nested expressions before optimization

Signed-off-by: evenyag <realevenyag@gmail.com>

---------

Signed-off-by: evenyag <realevenyag@gmail.com>
(cherry picked from commit 00aa65de7a)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(operator): reuse catalog table resolution for write retries (#9574)

* fix(operator): inherit logical table partitions when retrying writes

Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(operator): resolve retried writes through catalog manager

Signed-off-by: WenyXu <wenymedia@gmail.com>

---------

Signed-off-by: WenyXu <wenymedia@gmail.com>
(cherry picked from commit 36423a95c6)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(query): accept narrowed nullability in MergeScan remote schema check (#9592)

On append-mode tables a region reports an exact row count. When it is
zero, DataFusion's AggregateStatistics replaces the region-side
__min_state/__max_state aggregate with a non-null struct literal, so the
remote stream advertises the state column as non-nullable. The MergeScan
schema check required equal nullability and failed the query.

Accept a remote field that is non-nullable where the expected field is
nullable, matching DataFusion's own physical-plan schema invariant.
Batches are already rebuilt with the expected schema. Widening is still
rejected, and nested field nullability is still compared exactly as part
of the data type.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit ef1298ce02)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(table): stop claiming exact null and distinct counts for append scans (#9593)

RegionScanExec reported null_count = 0 and distinct_count = total_rows for
every column of an append-only table. DataFusion answers count(col) and
count(DISTINCT col) from these statistics, so both returned the row count.
Keep the exact row count and an exact zero null count for non-nullable
columns, so count(*) is still answered from statistics.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit 5cfdc30a69)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix: avoid pinning memtables during series-index reconciliation (#9588)

Signed-off-by: evenyag <realevenyag@gmail.com>
(cherry picked from commit ce66d8d4ac)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* perf(query): infer safe aggregate input bounds from HAVING (#9582)

* perf(query): infer safe aggregate input bounds from HAVING

Signed-off-by: Lei, HUANG <ratuthomm@gmail.com>

* test(query): refresh explain snapshots for aggregate bound inference

Signed-off-by: Lei, HUANG <ratuthomm@gmail.com>

* fix(query): avoid reinferring bounds across MergeScan

Signed-off-by: Lei, HUANG <ratuthomm@gmail.com>

* fix(query): validate aggregate bound column provenance

Signed-off-by: Lei, HUANG <ratuthomm@gmail.com>

* test(query): retain zero-match groups for nonpositive HAVING thresholds

Signed-off-by: Lei, HUANG <ratuthomm@gmail.com>

* test(query): assert inferred bounds in remote scan plans

Signed-off-by: Lei, HUANG <ratuthomm@gmail.com>

---------

Signed-off-by: Lei, HUANG <ratuthomm@gmail.com>
(cherry picked from commit af396e60a3)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* fix(query): keep state column names unique when splitting aggregates (#9594)

* fix(query): keep state column names unique when splitting aggregates

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(query): probe allocated names when renaming colliding state columns

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
(cherry picked from commit f7a3a7033c)
Signed-off-by: WenyXu <wenymedia@gmail.com>

* chore: bump version to 1.3.0-beta.2

Signed-off-by: WenyXu <wenymedia@gmail.com>

---------

Signed-off-by: greptimedb-ci <greptimedb-ci@greptime.com>
Signed-off-by: WenyXu <wenymedia@gmail.com>
Signed-off-by: Aarav <aaravsjadav@gmail.com>
Signed-off-by: Ning Sun <sunning@greptime.com>
Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
Signed-off-by: evenyag <realevenyag@gmail.com>
Signed-off-by: fengyue-xve <1759519137@qq.com>
Signed-off-by: Aetherance <inkToAether@gmail.com>
Signed-off-by: tian1220A <1573612521@qq.com>
Signed-off-by: 风月 <1759519137@qq.com>
Signed-off-by: Niladri Adhikary <niladrix719@gmail.com>
Signed-off-by: lczllx <2181719471@qq.com>
Signed-off-by: Aumnertic <hznuzc@gmail.com>
Signed-off-by: discord9 <discord9@163.com>
Signed-off-by: jeremyhi <fengjiachun@gmail.com>
Signed-off-by: Lei, HUANG <ratuthomm@gmail.com>
Co-authored-by: LFC <990479+MichaelScofield@users.noreply.github.com>
Co-authored-by: greptimedb-ci <greptimedb-ci@greptime.com>
Co-authored-by: DeviousCardi <115358213+DeviousCardi@users.noreply.github.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Ning Sun <sunng@protonmail.com>
Co-authored-by: discord9 <discord9@163.com>
Co-authored-by: dennis zhuang <killme2008@gmail.com>
Co-authored-by: Yingwen <realevenyag@gmail.com>
Co-authored-by: fengyue-xve <1759519137@qq.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Aether <98370028+Aetherance@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: zhaiAohan <1573612521@qq.com>
Co-authored-by: Niladri Adhikary <91966855+niladrix719@users.noreply.github.com>
Co-authored-by: Dragon Roar <2181719471@qq.com>
Co-authored-by: Aum <hznuzc@gmail.com>
Co-authored-by: jeremyhi <jiachun_feng@proton.me>
Co-authored-by: Lei, HUANG <6406592+v0y4g3r@users.noreply.github.com>
This commit is contained in:
Weny Xu
2026-10-15 20:34:19 +08:00
committed by GitHub
co-authored by LFC greptimedb-ci DeviousCardi Claude Opus 5 Ning Sun discord9 dennis zhuang Yingwen fengyue-xve Copilot Autofix powered by AI Aether github-actions[bot] zhaiAohan Niladri Adhikary Dragon Roar Aum jeremyhi Lei, HUANG
parent 865986d306
commit a788cd977a
350 changed files with 30890 additions and 6504 deletions
+26 -2
View File
@@ -192,6 +192,10 @@ impl KvBackend for CachedKvBackend {
}
}
if miss_keys.is_empty() {
return Ok(BatchGetResponse { kvs });
}
let batch_get_req = BatchGetRequest::new().with_keys(miss_keys.clone());
let pre_version = self.version();
@@ -503,6 +507,7 @@ mod tests {
pub struct SimpleKvBackend {
inner_map: DashMap<Vec<u8>, Vec<u8>>,
get_execute_times: Arc<AtomicU32>,
batch_get_execute_times: Arc<AtomicU32>,
}
impl TxnService for SimpleKvBackend {
@@ -520,6 +525,7 @@ mod tests {
}
async fn batch_get(&self, req: BatchGetRequest) -> Result<BatchGetResponse, Self::Error> {
self.batch_get_execute_times.fetch_add(1, Ordering::SeqCst);
let mut kvs = Vec::with_capacity(req.keys.len());
for key in req.keys.iter() {
if let Some(kv) = self.get(key).await? {
@@ -571,6 +577,7 @@ mod tests {
async fn test_cached_kv_backend() {
let simple_kv = Arc::new(SimpleKvBackend::default());
let get_execute_times = simple_kv.get_execute_times.clone();
let batch_get_execute_times = simple_kv.batch_get_execute_times.clone();
let cached_kv = CachedKvBackend::wrap(simple_kv);
add_some_vals(&cached_kv).await;
@@ -582,9 +589,11 @@ mod tests {
assert_eq!(get_execute_times.load(Ordering::SeqCst), 0);
for _ in 0..10 {
let _batch_get_resp = cached_kv.batch_get(batch_get_req.clone()).await.unwrap();
let batch_get_resp = cached_kv.batch_get(batch_get_req.clone()).await.unwrap();
assert_eq!(batch_get_resp.kvs.len(), 2);
assert_eq!(get_execute_times.load(Ordering::SeqCst), 2);
assert_eq!(batch_get_execute_times.load(Ordering::SeqCst), 1);
}
let batch_get_req = BatchGetRequest {
@@ -594,14 +603,29 @@ mod tests {
let _batch_get_resp = cached_kv.batch_get(batch_get_req.clone()).await.unwrap();
assert_eq!(get_execute_times.load(Ordering::SeqCst), 3);
assert_eq!(batch_get_execute_times.load(Ordering::SeqCst), 2);
for _ in 0..10 {
let _batch_get_resp = cached_kv.batch_get(batch_get_req.clone()).await.unwrap();
let batch_get_resp = cached_kv.batch_get(batch_get_req.clone()).await.unwrap();
assert_eq!(batch_get_resp.kvs.len(), 3);
assert_eq!(get_execute_times.load(Ordering::SeqCst), 3);
assert_eq!(batch_get_execute_times.load(Ordering::SeqCst), 2);
}
}
#[tokio::test]
async fn test_cached_kv_backend_empty_batch_get() {
let simple_kv = Arc::new(SimpleKvBackend::default());
let cached_kv = CachedKvBackend::wrap(simple_kv.clone());
let response = cached_kv.batch_get(BatchGetRequest::new()).await.unwrap();
assert!(response.kvs.is_empty());
assert_eq!(simple_kv.batch_get_execute_times.load(Ordering::SeqCst), 0);
assert_eq!(simple_kv.get_execute_times.load(Ordering::SeqCst), 0);
}
#[tokio::test]
async fn test_cached_kv_backend_rejects_writes_with_read_only_inner() {
let inner = Arc::new(MemoryKvBackend::<common_meta::error::Error>::new());
@@ -263,6 +263,51 @@ impl InformationSchemaPartitionsBuilder {
let predicates = Predicates::from_scan_request(&request);
// Fast path: when the predicates pin both the schema and the table name
// with equality, resolve the table directly instead of enumerating all
// tables in all schemas and fetching partition metadata for each of them.
if let (Some(schema_name), Some(table_name)) = (
predicates
.find_eq_value(TABLE_SCHEMA)
.and_then(Value::as_string),
predicates
.find_eq_value(TABLE_NAME)
.and_then(Value::as_string),
) {
if let Some(table) = catalog_manager
.table(&catalog_name, &schema_name, &table_name, None)
.await?
{
let table_info = table.table_info();
// The enumeration path below skips temporary tables, keep the
// behavior identical.
if table_info.table_type != TableType::Temporary {
let mut table_partitions = partition_manager
.batch_find_table_partitions(&[table_info.table_id()])
.await
.context(FindPartitionsSnafu)?;
let partitions = table_partitions
.remove(&table_info.table_id())
.unwrap_or_default();
self.add_partitions(
&predicates,
&table_info,
&catalog_name,
&schema_name,
&table_info.name,
&partitions,
);
}
}
// The pinned table fully determines the result: the table above was
// found (or not) unambiguously, so no other table can contribute rows.
return self.finish();
}
for schema_name in catalog_manager.schema_names(&catalog_name, None).await? {
let table_info_stream = catalog_manager
.tables(&catalog_name, &schema_name, None)
@@ -430,3 +475,253 @@ impl DfPartitionStream for InformationSchemaPartitions {
))
}
}
#[cfg(test)]
mod tests {
use api::v1::meta::Peer;
use cache::{build_fundamental_cache_registry, with_default_composite_cache_registry};
use common_catalog::consts::{DEFAULT_CATALOG_NAME, INFORMATION_SCHEMA_NAME};
use common_meta::cache::{CacheRegistryBuilder, LayeredCacheRegistryBuilder};
use common_meta::key::schema_name::SchemaNameKey;
use common_meta::key::table_route::TableRouteValue;
use common_meta::kv_backend::memory::MemoryKvBackend;
use common_meta::rpc::router::{Region, RegionRoute};
use common_meta::wal_provider::RegionWalOptions;
use datafusion::logical_expr::{BinaryExpr, Expr, Operator, col, lit};
use futures::TryStreamExt;
use store_api::storage::RegionId;
use super::*;
use crate::information_schema::NoopInformationExtension;
use crate::kvbackend::KvBackendCatalogManagerBuilder;
const SCHEMA_1: &str = "partitions_schema_1";
const SCHEMA_2: &str = "partitions_schema_2";
const PARTITIONED_TABLE: &str = "partitioned_table";
const MISSING_TABLE: &str = "missing_table";
const TABLE_ID_1: TableId = 1;
const TABLE_ID_2: TableId = 2;
const REGION_NUMBER_1: u32 = 1;
const REGION_NUMBER_2: u32 = 2;
const REGION_NUMBER_7: u32 = 7;
fn kv_catalog_manager() -> Arc<KvBackendCatalogManager> {
let backend = Arc::new(MemoryKvBackend::default());
let layered_cache_builder = LayeredCacheRegistryBuilder::default()
.add_cache_registry(CacheRegistryBuilder::default().build());
let fundamental_cache_registry = build_fundamental_cache_registry(backend.clone());
let layered_cache_registry = Arc::new(
with_default_composite_cache_registry(
layered_cache_builder.add_cache_registry(fundamental_cache_registry),
)
.unwrap()
.build(),
);
KvBackendCatalogManagerBuilder::new(
Arc::new(NoopInformationExtension),
backend,
layered_cache_registry,
)
.build()
}
fn region_route(table_id: TableId, region_number: u32) -> RegionRoute {
RegionRoute {
region: Region {
id: RegionId::new(table_id, region_number),
..Default::default()
},
leader_peer: Some(Peer {
id: 1,
addr: "127.0.0.1:3001".to_string(),
}),
..Default::default()
}
}
fn table_info(schema: &str, table: &str, table_id: TableId) -> TableInfo {
// The catalog defaults to the default catalog name.
let mut table_info =
common_meta::key::test_utils::new_test_table_info_with_name(table_id, table);
table_info.schema_name = schema.to_string();
table_info
}
/// Creates a catalog with two partitioned tables named `PARTITIONED_TABLE`
/// in two different schemas:
///
/// - `SCHEMA_1.PARTITIONED_TABLE` has two regions.
/// - `SCHEMA_2.PARTITIONED_TABLE` has one region.
async fn setup_catalog() -> Arc<KvBackendCatalogManager> {
let manager = kv_catalog_manager();
let metadata = manager.table_metadata_manager_ref().clone();
for schema in [SCHEMA_1, SCHEMA_2] {
metadata
.schema_manager()
.create(SchemaNameKey::new(DEFAULT_CATALOG_NAME, schema), None, true)
.await
.unwrap();
}
for (table_id, schema, region_numbers) in [
(TABLE_ID_1, SCHEMA_1, vec![REGION_NUMBER_1, REGION_NUMBER_2]),
(TABLE_ID_2, SCHEMA_2, vec![REGION_NUMBER_7]),
] {
let routes = region_numbers
.into_iter()
.map(|region_number| region_route(table_id, region_number))
.collect();
metadata
.create_table_metadata(
table_info(schema, PARTITIONED_TABLE, table_id),
TableRouteValue::physical(routes),
RegionWalOptions::default(),
)
.await
.unwrap();
}
manager
}
/// Scans `information_schema.partitions` and returns the total row count
/// and the pretty printed batches.
async fn scan_partitions(
manager: &Arc<dyn CatalogManager>,
filters: Vec<Expr>,
) -> (usize, String) {
let table = manager
.table(
DEFAULT_CATALOG_NAME,
INFORMATION_SCHEMA_NAME,
PARTITIONS,
None,
)
.await
.unwrap()
.unwrap();
let stream = table
.scan_to_stream(ScanRequest {
filters,
..Default::default()
})
.await
.unwrap();
let batches = stream.try_collect::<Vec<_>>().await.unwrap();
let rows = batches.iter().map(|batch| batch.num_rows()).sum();
let output = batches
.iter()
.map(|batch| batch.pretty_print())
.collect::<Vec<_>>()
.join("\n");
(rows, output)
}
fn eq_filter(column: &str, value: &str) -> Expr {
Expr::BinaryExpr(BinaryExpr::new(
Box::new(col(column)),
Operator::Eq,
Box::new(lit(value)),
))
}
fn not_eq_filter(column: &str, value: &str) -> Expr {
Expr::BinaryExpr(BinaryExpr::new(
Box::new(col(column)),
Operator::NotEq,
Box::new(lit(value)),
))
}
#[tokio::test]
async fn test_scan_pinned_table_skips_catalog_enumeration() {
let manager: Arc<dyn CatalogManager> = setup_catalog().await;
// No predicates: every partitioned table is listed.
let (rows, output) = scan_partitions(&manager, vec![]).await;
assert_eq!(3, rows, "{output}");
// Pins both the schema and the table name, so the scan resolves the
// table directly instead of enumerating all catalog entries.
let (rows, output) = scan_partitions(
&manager,
vec![
eq_filter(TABLE_SCHEMA, SCHEMA_1),
eq_filter(TABLE_NAME, PARTITIONED_TABLE),
],
)
.await;
assert_eq!(2, rows, "{output}");
assert!(output.contains(SCHEMA_1), "{output}");
assert!(!output.contains(SCHEMA_2), "{output}");
assert!(
output.contains(
&RegionId::new(TABLE_ID_1, REGION_NUMBER_1)
.as_u64()
.to_string()
),
"{output}"
);
assert!(
output.contains(
&RegionId::new(TABLE_ID_1, REGION_NUMBER_2)
.as_u64()
.to_string()
),
"{output}"
);
// The fallback enumeration path must return the identical result. The
// inequality on `table_name` doesn't pin a single name, so no direct
// lookup happens here.
let (fallback_rows, fallback_output) = scan_partitions(
&manager,
vec![
eq_filter(TABLE_SCHEMA, SCHEMA_1),
not_eq_filter(TABLE_NAME, MISSING_TABLE),
],
)
.await;
assert_eq!(rows, fallback_rows, "{fallback_output}");
assert_eq!(output, fallback_output);
// Only the table name is pinned: the schema can't be resolved and both
// tables of that name are kept.
let (rows, output) =
scan_partitions(&manager, vec![eq_filter(TABLE_NAME, PARTITIONED_TABLE)]).await;
assert_eq!(3, rows, "{output}");
assert!(output.contains(SCHEMA_1), "{output}");
assert!(output.contains(SCHEMA_2), "{output}");
// An unknown table in a pinned schema yields no rows.
let (rows, output) = scan_partitions(
&manager,
vec![
eq_filter(TABLE_SCHEMA, SCHEMA_1),
eq_filter(TABLE_NAME, MISSING_TABLE),
],
)
.await;
assert_eq!(0, rows, "{output}");
}
#[tokio::test]
async fn test_scan_equality_under_disjunction_falls_back_to_enumeration() {
let manager: Arc<dyn CatalogManager> = setup_catalog().await;
// `table_schema = SCHEMA_1 OR table_name = PARTITIONED_TABLE` matches
// the tables in both schemas: equality nested under a disjunction must
// not pin a single table.
let filter = Expr::BinaryExpr(BinaryExpr::new(
Box::new(eq_filter(TABLE_SCHEMA, SCHEMA_1)),
Operator::Or,
Box::new(eq_filter(TABLE_NAME, PARTITIONED_TABLE)),
));
let (rows, output) = scan_partitions(&manager, vec![filter]).await;
assert_eq!(3, rows, "{output}");
assert!(output.contains(SCHEMA_1), "{output}");
assert!(output.contains(SCHEMA_2), "{output}");
}
}
+100
View File
@@ -214,6 +214,22 @@ impl Predicate {
_ => None,
}
}
/// Collects the values that constrain `column` to be equal to them at this
/// predicate's top-level conjunctions.
///
/// Constraints under `Or`/`Not` are ignored because they don't guarantee
/// that `column` equals a single value.
fn collect_eq_values<'a>(&'a self, column: &str, values: &mut Vec<&'a Value>) {
match self {
Predicate::Eq(c, v) if c == column => values.push(v),
Predicate::And(left, right) => {
left.collect_eq_values(column, values);
right.collect_eq_values(column, values);
}
_ => {}
}
}
}
/// Perform SQL left LIKE right, return `None` if fail to evaluate.
@@ -269,6 +285,25 @@ impl Predicates {
}
}
/// Returns the single equality value that constrains `column` in the
/// top-level conjunctions of the predicates.
///
/// Returns `None` if there is no equality constraint on `column`, or if
/// the predicates constrain it with multiple distinct values.
pub fn find_eq_value(&self, column: &str) -> Option<&Value> {
let mut values = Vec::new();
for predicate in &self.predicates {
predicate.collect_eq_values(column, &mut values);
}
let (first, rest) = values.split_first()?;
if rest.iter().all(|v| v == first) {
Some(*first)
} else {
None
}
}
/// Evaluate the predicates with the row.
/// returns true when all the predicates are satisfied or can't be evaluated.
pub fn eval(&self, row: &[(&str, &Value)]) -> bool {
@@ -556,6 +591,71 @@ mod tests {
);
}
#[test]
fn test_find_eq_value() {
fn eq(name: &str, value: &str) -> Predicate {
Predicate::Eq(name.to_string(), Value::from(value))
}
fn and(left: Predicate, right: Predicate) -> Predicate {
Predicate::And(Box::new(left), Box::new(right))
}
fn or(left: Predicate, right: Predicate) -> Predicate {
Predicate::Or(Box::new(left), Box::new(right))
}
fn predicates(predicates: Vec<Predicate>) -> Predicates {
Predicates { predicates }
}
// No predicates, no equality value.
assert!(predicates(vec![]).find_eq_value("a").is_none());
// No equality on the column.
let p = predicates(vec![Predicate::NotEq("a".to_string(), Value::from("x"))]);
assert!(p.find_eq_value("a").is_none());
assert!(p.find_eq_value("b").is_none());
// Simple equality.
let p = predicates(vec![eq("a", "x")]);
let v = p.find_eq_value("a").unwrap();
assert!(match_string_value(v, "x"));
assert!(p.find_eq_value("b").is_none());
// Duplicated equality on the same value.
let p = predicates(vec![and(eq("a", "x"), eq("a", "x"))]);
let v = p.find_eq_value("a").unwrap();
assert!(match_string_value(v, "x"));
// Conflicting equalities reject the lookup.
let p = predicates(vec![and(eq("a", "x"), eq("a", "y"))]);
assert!(p.find_eq_value("a").is_none());
// Conflicting equalities in separate predicates reject the lookup too.
let p = predicates(vec![eq("a", "x"), eq("a", "y")]);
assert!(p.find_eq_value("a").is_none());
// Equality nested in deeper conjunctions.
let p = predicates(vec![and(and(eq("b", "y"), eq("a", "x")), eq("a", "x"))]);
let v = p.find_eq_value("a").unwrap();
assert!(match_string_value(v, "x"));
// Equality under a disjunction or a negation doesn't constrain the column.
let p = predicates(vec![or(eq("a", "x"), eq("a", "x"))]);
assert!(p.find_eq_value("a").is_none());
let p = predicates(vec![Predicate::Not(Box::new(eq("a", "x")))]);
assert!(p.find_eq_value("a").is_none());
// Equality value in a top-level conjunction composed of separate predicates.
let p = predicates(vec![
eq("a", "x"),
Predicate::Like("b".to_string(), "%y".to_string(), false),
]);
let v = p.find_eq_value("a").unwrap();
assert!(match_string_value(v, "x"));
}
#[test]
fn test_predicates_eval_row() {
let wrong_row = [
+31 -1
View File
@@ -86,7 +86,13 @@ pub struct StoreConfig {
/// Automatically create PostgreSQL schema if it doesn't exist (default: true).
#[cfg(feature = "pg_kvbackend")]
#[clap(long, default_value_t = true)]
#[clap(
long,
default_value_t = true,
default_missing_value = "true",
num_args = 0..=1,
action = clap::ArgAction::Set
)]
pub auto_create_schema: bool,
/// TLS mode for backend store connections (etcd, PostgreSQL, MySQL)
@@ -223,3 +229,27 @@ impl StoreConfig {
}
}
}
#[cfg(all(test, feature = "pg_kvbackend"))]
mod tests {
use clap::Parser;
use super::StoreConfig;
#[test]
fn test_auto_create_schema_accepts_explicit_value() {
let config =
StoreConfig::try_parse_from(["store", "--auto-create-schema", "false"]).unwrap();
assert!(!config.auto_create_schema);
let config =
StoreConfig::try_parse_from(["store", "--auto-create-schema", "true"]).unwrap();
assert!(config.auto_create_schema);
}
#[test]
fn test_auto_create_schema_defaults_to_true() {
let config = StoreConfig::try_parse_from(["store"]).unwrap();
assert!(config.auto_create_schema);
}
}
+1
View File
@@ -45,3 +45,4 @@ substrait.workspace = true
tokio.workspace = true
tokio-stream = { workspace = true, features = ["net"] }
tonic.workspace = true
tonic-prost.workspace = true
+5 -17
View File
@@ -20,7 +20,6 @@ use api::v1::HealthCheckRequest;
use api::v1::flow::flow_client::FlowClient as PbFlowClient;
use api::v1::health_check_client::HealthCheckClient;
use api::v1::prometheus_gateway_client::PrometheusGatewayClient;
use api::v1::region::region_client::RegionClient as PbRegionClient;
use arrow_flight::flight_service_client::FlightServiceClient;
use common_grpc::channel_manager::{
ChannelConfig, ChannelManager, ClientTlsOption, load_client_tls_config,
@@ -526,23 +525,12 @@ impl Client {
Ok(FlightClient { addr, client })
}
pub(crate) fn raw_region_client(&self) -> Result<(String, PbRegionClient<Channel>)> {
/// Creates the region gRPC transport with the configured message limits.
pub(crate) fn raw_region_client(&self) -> Result<(String, tonic::client::Grpc<Channel>)> {
let (addr, channel) = self.find_channel()?;
let client = PbRegionClient::new(channel)
.max_decoding_message_size(
self.inner
.control_channel_manager
.config()
.max_recv_message_size
.as_bytes() as usize,
)
.max_encoding_message_size(
self.inner
.control_channel_manager
.config()
.max_send_message_size
.as_bytes() as usize,
);
let client = tonic::client::Grpc::new(channel)
.max_decoding_message_size(self.max_grpc_recv_message_size())
.max_encoding_message_size(self.max_grpc_send_message_size());
Ok((addr, client))
}
+303 -45
View File
@@ -16,7 +16,7 @@ use std::collections::HashMap;
use std::pin::Pin;
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, RwLock};
use std::sync::{Arc, Mutex, RwLock};
use std::task::{Context, Poll};
use std::time::Duration;
@@ -55,6 +55,7 @@ use futures::future;
use futures_util::{Stream, StreamExt, TryStreamExt};
use prost::Message;
use snafu::{IntoError, ResultExt};
use tokio::sync::Notify;
use tonic::metadata::{AsciiMetadataKey, AsciiMetadataValue, MetadataMap, MetadataValue};
use tonic::transport::Channel;
@@ -70,20 +71,47 @@ type FlightDataStream = Pin<Box<dyn Stream<Item = FlightData> + Send>>;
type DoPutResponseStream = Pin<Box<dyn Stream<Item = Result<DoPutResponse>>>>;
const HINTS_METADATA_KEY: &str = "x-greptime-hints";
/// Maximum time to wait for the optional trailing metrics message after
/// affected rows have already been delivered.
const FLIGHT_TRAILING_METRICS_TIMEOUT: Duration = Duration::from_secs(5);
/// Terminal metrics associated with a query output.
///
/// For streaming outputs, metrics are only final after the stream is fully
/// drained and [`Self::is_ready`] returns `true`.
/// drained and [`Self::is_ready`] returns `true`. Affected-row outputs may
/// briefly await a compatibility trailing metrics message.
#[derive(Debug, Clone, Default)]
pub struct OutputMetrics {
inner: Arc<OutputMetricsInner>,
}
#[derive(Debug, Default)]
#[derive(Debug)]
struct OutputMetricsInner {
metrics: RwLock<Option<RecordBatchMetrics>>,
completion_error: RwLock<Option<String>>,
ready: AtomicBool,
ready_notify: Notify,
compatibility_task: Mutex<Option<tokio::task::AbortHandle>>,
}
impl Default for OutputMetricsInner {
fn default() -> Self {
Self {
metrics: RwLock::new(None),
completion_error: RwLock::new(None),
ready: AtomicBool::new(false),
ready_notify: Notify::new(),
compatibility_task: Mutex::new(None),
}
}
}
impl Drop for OutputMetricsInner {
fn drop(&mut self) {
if let Some(handle) = self.compatibility_task.get_mut().unwrap().take() {
handle.abort();
}
}
}
impl OutputMetrics {
@@ -98,10 +126,45 @@ impl OutputMetrics {
/// Marks the terminal metrics as final for this output.
pub fn mark_ready(&self) {
let _ = self
if self
.inner
.ready
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire);
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
.is_ok()
{
self.inner.ready_notify.notify_waiters();
}
}
/// Waits until terminal metrics are final.
pub async fn wait_ready(&self) {
loop {
let notified = self.inner.ready_notify.notified();
if self.is_ready() {
return;
}
notified.await;
}
}
/// Returns an error encountered while completing the output, if any.
pub fn completion_error(&self) -> Option<String> {
self.inner.completion_error.read().unwrap().clone()
}
fn set_completion_error(&self, error: impl Into<String>) {
*self.inner.completion_error.write().unwrap() = Some(error.into());
}
fn set_compatibility_task(&self, handle: tokio::task::AbortHandle) {
let mut task = self.inner.compatibility_task.lock().unwrap();
if !self.is_ready() {
*task = Some(handle);
}
}
fn take_compatibility_task(&self) -> Option<tokio::task::AbortHandle> {
self.inner.compatibility_task.lock().unwrap().take()
}
/// Returns whether terminal metrics are final.
@@ -148,7 +211,8 @@ impl OutputMetrics {
///
/// The contained [`OutputMetrics`] lets callers read stream terminal metrics
/// after consuming `output`. For non-stream outputs, metrics are ready
/// immediately.
/// immediately. Flight affected-row outputs without inline metrics require
/// [`OutputMetrics::wait_ready`] before compatibility trailing metrics can be read.
#[derive(Debug)]
pub struct OutputWithMetrics {
pub output: Output,
@@ -194,6 +258,55 @@ fn parse_terminal_metrics(metrics_json: &str) -> Result<RecordBatchMetrics> {
})
}
fn spawn_affected_rows_trailing_metrics_task<S>(
terminal_metrics: &OutputMetrics,
mut reader: FlightMessageReader<S>,
) where
S: Stream<Item = Result<FlightMessage>> + Send + Unpin + 'static,
{
let metrics_ref = Arc::downgrade(&terminal_metrics.inner);
let remote_addr = reader.remote_addr().to_string();
let task = common_runtime::spawn_global(async move {
let result =
tokio::time::timeout(FLIGHT_TRAILING_METRICS_TIMEOUT, reader.read_next()).await;
let Some(inner) = metrics_ref.upgrade() else {
return;
};
let metrics = OutputMetrics { inner };
match result {
Ok(Ok(Some(FlightMessage::Metrics(s)))) => match parse_terminal_metrics(&s) {
Ok(metrics_json) => metrics.update(Some(metrics_json)),
Err(error) => {
metrics.set_completion_error(error.to_string());
warn!(
"Failed to decode trailing Flight metrics from {}: {}",
remote_addr, error
);
}
},
Ok(Ok(None)) => {}
Ok(Ok(Some(other))) => {
let error = format!("Unexpected trailing Flight message: {other:?}");
metrics.set_completion_error(error.clone());
warn!("{} from {}", error, remote_addr);
}
Ok(Err(error)) => {
let error = flight_stream_error(&remote_addr, error);
metrics.set_completion_error(error.to_string());
warn!("{}", error);
}
Err(_) => {
let error = "Timed out waiting for trailing Flight metrics";
metrics.set_completion_error(error);
warn!("{} from {}", error, remote_addr);
}
}
metrics.mark_ready();
metrics.take_compatibility_task();
});
terminal_metrics.set_compatibility_task(task.abort_handle());
}
struct StreamWithMetrics {
stream: common_recordbatch::SendableRecordBatchStream,
metrics: OutputMetrics,
@@ -233,6 +346,9 @@ impl Stream for StreamWithMetrics {
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let polled = Pin::new(&mut self.stream).poll_next(cx);
if let Poll::Ready(Some(Err(error))) = &polled {
self.metrics.set_completion_error(error.to_string());
}
if let Poll::Ready(None) = &polled {
self.sync_terminal_metrics();
self.metrics.mark_ready();
@@ -280,32 +396,12 @@ where
FlightMessage::AffectedRows { rows, metrics } => {
let terminal_metrics = OutputMetrics::new();
if let Some(metrics) = metrics {
// Inline metrics are authoritative. They complete the output
// without touching the rest of the Flight stream.
terminal_metrics.update(Some(parse_terminal_metrics(&metrics)?));
}
let next_message = reader
.read_next()
.await
.map_err(|error| flight_stream_error(reader.remote_addr(), error))?;
match next_message {
None => terminal_metrics.mark_ready(),
Some(FlightMessage::Metrics(s)) if terminal_metrics.get().is_none() => {
terminal_metrics.update(Some(parse_terminal_metrics(&s)?));
terminal_metrics.mark_ready();
}
Some(FlightMessage::Metrics(_)) => {
return IllegalFlightMessagesSnafu {
reason: "'AffectedRows' Flight metadata already carries Metrics and cannot be followed by another Metrics message",
}
.fail();
}
Some(other) => {
return IllegalFlightMessagesSnafu {
reason: format!(
"'AffectedRows' Flight message can only be followed by a Metrics message, got {other:?}"
),
}
.fail();
}
terminal_metrics.mark_ready();
} else {
spawn_affected_rows_trailing_metrics_task(&terminal_metrics, reader);
}
Ok(OutputWithMetrics {
output: Output::new_with_affected_rows(rows),
@@ -749,7 +845,9 @@ impl Database {
/// Executes a SQL query and returns the output with terminal metrics.
///
/// For stream outputs, callers must consume the stream before reading final
/// terminal metrics from [`OutputWithMetrics::metrics`].
/// terminal metrics from [`OutputWithMetrics::metrics`]. For affected-row
/// outputs without inline metrics, call [`OutputMetrics::wait_ready`] when
/// compatibility trailing metrics are required.
pub async fn sql_with_terminal_metrics<S>(
&self,
sql: S,
@@ -986,6 +1084,7 @@ mod tests {
use datatypes::schema::{ColumnSchema, Schema};
use datatypes::vectors::Int32Vector;
use futures_util::StreamExt;
use tokio::sync::oneshot;
use tonic::codegen::http::{HeaderMap, HeaderValue};
use tonic::metadata::MetadataMap;
use tonic::{Code, Status};
@@ -1296,11 +1395,6 @@ mod tests {
);
}
#[test]
fn test_parse_terminal_metrics_rejects_invalid_json() {
assert!(parse_terminal_metrics("{not-json}").is_err());
}
#[tokio::test]
async fn test_affected_rows_inline_metrics_are_parsed() {
let output = output_from_flight_message_stream(
@@ -1322,27 +1416,191 @@ mod tests {
}
#[tokio::test]
async fn test_affected_rows_inline_metrics_rejects_trailing_metrics() {
async fn test_affected_rows_inline_metrics_do_not_poll_trailer() {
let metrics_json = terminal_metrics_json();
let err = output_from_flight_message_stream(
let output = output_from_flight_message_stream(
"test-peer".to_string(),
futures_util::stream::iter(vec![
Ok(FlightMessage::AffectedRows {
rows: 3,
metrics: Some(metrics_json.clone()),
metrics: Some(metrics_json),
}),
Ok(FlightMessage::Metrics(metrics_json)),
Ok(FlightMessage::Metrics(terminal_metrics_json_with_seq(99))),
] as Vec<Result<FlightMessage>>),
)
.await
.unwrap_err();
.unwrap();
assert!(
err.to_string().contains("already carries Metrics"),
"unexpected error: {err:?}"
assert!(output.metrics.is_ready());
assert_eq!(
output.metrics.region_watermark_map(),
Some(std::collections::HashMap::from([(7, 42)]))
);
}
#[tokio::test]
async fn test_affected_rows_without_inline_metrics_becomes_ready_after_trailer() {
// Hold the trailer until the not-ready state has been observed.
let (trailer_tx, trailer_rx) = oneshot::channel();
let trailer = futures_util::stream::once(trailer_rx).map(|ready| {
ready.unwrap();
Ok(FlightMessage::Metrics(terminal_metrics_json()))
});
let output = output_from_flight_message_stream(
"test-peer".to_string(),
futures_util::stream::iter(vec![Ok(FlightMessage::AffectedRows {
rows: 3,
metrics: None,
})])
.chain(trailer),
)
.await
.unwrap();
assert!(!output.metrics.is_ready());
trailer_tx.send(()).unwrap();
tokio::time::timeout(Duration::from_secs(1), output.metrics.wait_ready())
.await
.expect("terminal metrics must become ready once the trailer arrives");
assert!(output.metrics.completion_error().is_none());
assert_eq!(
output.metrics.region_watermark_map(),
Some(std::collections::HashMap::from([(7, 42)]))
);
}
#[tokio::test]
async fn test_affected_rows_malformed_trailer_sets_completion_error_and_ready() {
let output = output_from_flight_message_stream(
"test-peer".to_string(),
futures_util::stream::iter(vec![
Ok(FlightMessage::AffectedRows {
rows: 3,
metrics: None,
}),
Ok(FlightMessage::Metrics("{not-json}".to_string())),
] as Vec<Result<FlightMessage>>),
)
.await
.unwrap();
output.metrics.wait_ready().await;
let error = output.metrics.completion_error().unwrap();
assert!(error.contains("Invalid terminal metrics message"));
assert!(output.metrics.is_ready());
}
#[tokio::test]
async fn test_affected_rows_transport_trailer_error_sets_completion_error_and_ready() {
let output = output_from_flight_message_stream(
"test-peer".to_string(),
futures_util::stream::iter(vec![
Ok(FlightMessage::AffectedRows {
rows: 3,
metrics: None,
}),
Err(Status::unavailable("trailer read failed").into()),
] as Vec<Result<FlightMessage>>),
)
.await
.unwrap();
output.metrics.wait_ready().await;
let error = output.metrics.completion_error().unwrap();
assert!(error.contains("trailer read failed"));
assert!(output.metrics.is_ready());
}
#[tokio::test]
async fn test_affected_rows_compatibility_reader_is_cancelled_after_second_poll_begins() {
struct DropProbe {
first: Option<Result<FlightMessage>>,
polled: Option<oneshot::Sender<()>>,
dropped: Option<oneshot::Sender<()>>,
}
impl Stream for DropProbe {
type Item = Result<FlightMessage>;
fn poll_next(
mut self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
if let Some(message) = self.first.take() {
return Poll::Ready(Some(message));
}
if let Some(polled) = self.polled.take() {
let _ = polled.send(());
}
Poll::Pending
}
}
impl Drop for DropProbe {
fn drop(&mut self) {
if let Some(dropped) = self.dropped.take() {
let _ = dropped.send(());
}
}
}
let (polled_tx, polled_rx) = oneshot::channel();
let (dropped_tx, dropped_rx) = oneshot::channel();
let output = output_from_flight_message_stream(
"test-peer".to_string(),
DropProbe {
first: Some(Ok(FlightMessage::AffectedRows {
rows: 3,
metrics: None,
})),
polled: Some(polled_tx),
dropped: Some(dropped_tx),
},
)
.await
.unwrap();
polled_rx.await.unwrap();
drop(output);
// Require cancellation before the five-second compatibility timeout.
tokio::time::timeout(Duration::from_secs(1), dropped_rx)
.await
.expect("the compatibility reader must be dropped once the output is dropped")
.unwrap();
}
#[tokio::test]
async fn test_schema_record_batch_yields_before_pending_message_stream() {
let schema = Arc::new(Schema::new(vec![ColumnSchema::new(
"v",
ConcreteDataType::int32_datatype(),
false,
)]));
let batch = RecordBatch::new(
schema.clone(),
vec![Arc::new(Int32Vector::from_slice([1])) as VectorRef],
)
.unwrap();
let messages = futures_util::stream::iter(vec![
Ok(FlightMessage::Schema(schema.arrow_schema().clone())),
Ok(FlightMessage::RecordBatch(batch.into_df_record_batch())),
] as Vec<Result<FlightMessage>>)
.chain(futures_util::stream::pending());
let output = output_from_flight_message_stream("test-peer".to_string(), messages)
.await
.unwrap();
let OutputData::Stream(mut stream) = output.output.data else {
panic!("expected stream output");
};
let batch = tokio::time::timeout(Duration::from_secs(1), stream.next())
.await
.unwrap()
.unwrap()
.unwrap();
assert_eq!(batch.num_rows(), 1);
assert!(!output.metrics.is_ready());
}
#[tokio::test]
async fn test_invalid_terminal_metrics_after_record_batch_yields_batch_then_error() {
let schema = Arc::new(Schema::new(vec![ColumnSchema::new(
+24 -2
View File
@@ -18,7 +18,7 @@ use common_error::define_from_tonic_status;
use common_error::ext::{BoxedError, ErrorExt, RetryHint};
use common_error::status_code::StatusCode;
use common_macro::stack_trace_debug;
use snafu::{Location, Snafu};
use snafu::{Location, Snafu, location};
use tonic::Code;
use tonic::metadata::errors::InvalidMetadataValue;
@@ -191,9 +191,22 @@ impl ErrorExt for Error {
Error::Tonic { retry_hint, .. } => *retry_hint,
Error::FlightGet { source, .. }
| Error::FlightStream { source, .. }
| Error::RegionServer { source, .. }
| Error::FlowServer { source, .. }
| Error::External { source, .. } => source.retry_hint(),
Error::RegionServer { code, source, .. } => {
let hint = source.retry_hint();
// Preserve the transport retry policy previously applied by should_retry().
if !hint.is_retryable()
&& matches!(
code,
Code::Unavailable | Code::Cancelled | Code::DeadlineExceeded
)
{
RetryHint::Retryable
} else {
hint
}
}
Error::ConvertFlightData { source, .. }
| Error::CreateChannel { source, .. }
| Error::CreateTlsChannel { source, .. } => source.retry_hint(),
@@ -205,6 +218,15 @@ impl ErrorExt for Error {
define_from_tonic_status!(Error, Tonic);
impl From<Error> for common_meta::error::Error {
fn from(error: Error) -> Self {
Self::External {
source: BoxedError::new(error),
location: location!(),
}
}
}
impl Error {
/// Returns the gRPC status code if this error is caused by a gRPC request failure.
pub fn tonic_code(&self) -> Option<Code> {
+2 -39
View File
@@ -12,40 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::pin::Pin;
use arrow_flight::FlightData;
use common_grpc::flight::{FlightDecoder, FlightMessage};
use futures_util::stream::Peekable;
use futures_util::{Stream, StreamExt};
use snafu::{OptionExt, ResultExt};
use crate::Result;
use crate::error::{ConvertFlightDataSnafu, Error, IllegalFlightMessagesSnafu};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FlightMessageKind {
Schema,
RecordBatch,
AffectedRows,
Metrics,
}
impl From<&FlightMessage> for FlightMessageKind {
fn from(message: &FlightMessage) -> Self {
match message {
FlightMessage::Schema(_) => Self::Schema,
FlightMessage::RecordBatch(_) => Self::RecordBatch,
FlightMessage::AffectedRows { .. } => Self::AffectedRows,
FlightMessage::Metrics(_) => Self::Metrics,
}
}
}
pub(crate) struct FlightMessageReader<S: Stream + Unpin> {
/// Remote Flight peer associated with this response stream.
remote_addr: String,
messages: Peekable<S>,
messages: S,
}
impl<S> FlightMessageReader<S>
@@ -55,7 +33,7 @@ where
pub(crate) fn new(remote_addr: impl Into<String>, messages: S) -> Self {
Self {
remote_addr: remote_addr.into(),
messages: messages.peekable(),
messages,
}
}
@@ -72,21 +50,6 @@ where
pub(crate) async fn read_next(&mut self) -> Result<Option<FlightMessage>> {
self.messages.next().await.transpose()
}
pub(crate) async fn peek_next_message_kind(&mut self) -> Result<Option<FlightMessageKind>> {
match Pin::new(&mut self.messages).peek().await {
Some(Ok(message)) => Ok(Some(message.into())),
None => Ok(None),
Some(Err(_)) => match self.read_next().await {
// `peek` only borrows the error; consume it to preserve the source error.
Err(error) => Err(error),
Ok(_) => IllegalFlightMessagesSnafu {
reason: "Flight stream changed after peek".to_string(),
}
.fail(),
},
}
}
}
pub(crate) fn decode_flight_data(
+646 -165
View File
@@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
mod codec;
use std::borrow::Borrow;
use std::sync::Arc;
use std::time::Duration;
@@ -48,11 +51,13 @@ use crate::error::{
self, FlightGetSnafu, IllegalDatabaseResponseSnafu, IllegalFlightMessagesSnafu,
MissingFieldSnafu, Result, ServerSnafu,
};
use crate::flight::{FlightMessageKind, FlightMessageReader, decode_flight_data};
use crate::flight::{FlightMessageReader, decode_flight_data};
use crate::region::codec::RegionCodec;
use crate::{Client, metrics};
const FLIGHT_DO_GET_TIMEOUT: Duration = Duration::from_secs(10);
/// Sends region operations through a configured gRPC client.
#[derive(Debug)]
pub struct RegionRequester {
client: Client,
@@ -62,20 +67,8 @@ pub struct RegionRequester {
#[async_trait]
impl Datanode for RegionRequester {
async fn handle(&self, request: RegionRequest) -> MetaResult<RegionResponse> {
self.handle_inner(request).await.map_err(|err| {
if err.should_retry() {
meta_error::Error::RetryLater {
source: BoxedError::new(err),
clean_poisons: false,
}
} else {
meta_error::Error::External {
source: BoxedError::new(err),
location: location!(),
}
}
})
async fn handle_request(&self, request: Arc<RegionRequest>) -> MetaResult<RegionResponse> {
self.handle_inner(request).await.map_err(Into::into)
}
async fn handle_query(&self, request: QueryRequest) -> MetaResult<SendableRecordBatchStream> {
@@ -144,11 +137,49 @@ impl RegionRequester {
recordbatches_from_flight_message_stream(addr, flight_message_stream).await
}
async fn handle_inner(&self, request: RegionRequest) -> Result<RegionResponse> {
/// Sends an owned or shared request without deep-cloning its protobuf payload.
///
/// Accepts both [`RegionRequest`] and [`Arc<RegionRequest>`]. Callers can retain a
/// shared request for another attempt; this method does not retry or determine
/// whether replaying the request is safe.
pub async fn handle<R>(&self, request: R) -> Result<RegionResponse>
where
R: Borrow<RegionRequest> + Send + Sync + 'static,
{
self.handle_inner(request).await
}
/// Sends a remote dynamic filter update to the region service.
pub async fn handle_remote_dyn_filter_update(
&self,
query_id: impl Into<String>,
update: RemoteDynFilterUpdate,
) -> Result<RegionResponse> {
self.handle_inner(build_remote_dyn_filter_update_request(query_id, update))
.await
}
/// Unregisters a remote dynamic filter in the region service.
pub async fn handle_remote_dyn_filter_unregister(
&self,
query_id: impl Into<String>,
unregister: RemoteDynFilterUnregister,
) -> Result<RegionResponse> {
self.handle_inner(build_remote_dyn_filter_unregister_request(
query_id, unregister,
))
.await
}
async fn handle_inner<R>(&self, request: R) -> Result<RegionResponse>
where
R: Borrow<RegionRequest> + Send + Sync + 'static,
{
let request_body = request
.borrow()
.body
.as_ref()
.with_context(|| MissingFieldSnafu { field: "body" })?;
.context(MissingFieldSnafu { field: "body" })?;
let is_insert = matches!(
request_body,
region_request::Body::Inserts(_) | region_request::Body::BulkInsert(_)
@@ -168,48 +199,42 @@ impl RegionRequester {
}
}
let response = client
.handle(request)
.await
.map_err(|e| {
let code = e.code();
// Uses `Error::RegionServer` instead of `Error::Server`
error::Error::RegionServer {
addr,
code,
source: BoxedError::new(error::Error::from(e)),
location: location!(),
}
})?
.into_inner();
let response = async {
client
.ready()
.await
.map_err(|e| tonic::Status::unknown(format!("Service was not ready: {e}")))?;
let mut request = tonic::Request::new(request);
request.extensions_mut().insert(tonic::GrpcMethod::new(
"greptime.v1.region.Region",
"Handle",
));
client
.unary(
request,
tonic::codegen::http::uri::PathAndQuery::from_static(
"/greptime.v1.region.Region/Handle",
),
RegionCodec::default(),
)
.await
}
.await
.map_err(|status| map_region_error(status, addr))?
.into_inner();
check_response_header(&response.header)?;
Ok(RegionResponse::from_region_response(response))
}
}
pub async fn handle(&self, request: RegionRequest) -> Result<RegionResponse> {
self.handle_inner(request).await
}
pub async fn handle_remote_dyn_filter_update(
&self,
query_id: impl Into<String>,
update: RemoteDynFilterUpdate,
) -> Result<RegionResponse> {
self.handle_inner(build_remote_dyn_filter_update_request(query_id, update))
.await
}
pub async fn handle_remote_dyn_filter_unregister(
&self,
query_id: impl Into<String>,
unregister: RemoteDynFilterUnregister,
) -> Result<RegionResponse> {
self.handle_inner(build_remote_dyn_filter_unregister_request(
query_id, unregister,
))
.await
fn map_region_error(status: tonic::Status, addr: String) -> error::Error {
error::Error::RegionServer {
addr,
code: status.code(),
source: BoxedError::new(error::Error::from(status)),
location: location!(),
}
}
@@ -247,9 +272,7 @@ where
"poll_flight_data_stream"
));
let mut stream_ended = false;
while !stream_ended {
loop {
let flight_message = match reader.read_next().await {
Ok(Some(message)) => message,
Ok(None) => break,
@@ -262,59 +285,27 @@ where
match flight_message {
FlightMessage::RecordBatch(record_batch) => {
let result_to_yield =
RecordBatch::from_df_record_batch(schema_cloned.clone(), record_batch);
// Metrics follow a batch so MergeScan can observe them before yielding it.
match reader.peek_next_message_kind().await {
Ok(Some(FlightMessageKind::Metrics)) => {
let metrics_message = match reader.read_next().await {
Ok(Some(FlightMessage::Metrics(metrics))) => metrics,
Ok(Some(_) | None) => {
yield IllegalFlightMessagesSnafu {
reason: "Flight stream changed after peek",
}
.fail()
.map_err(BoxedError::new)
.context(ExternalSnafu);
break;
}
Err(error) => {
yield Err(BoxedError::new(flight_stream_error(
&stream_addr,
error,
)))
.context(ExternalSnafu);
break;
}
};
let metrics = serde_json::from_str(&metrics_message).ok().map(Arc::new);
metrics_ref.swap(metrics);
}
Ok(Some(FlightMessageKind::RecordBatch)) => {}
Ok(Some(FlightMessageKind::Schema | FlightMessageKind::AffectedRows)) => {
yield IllegalFlightMessagesSnafu {
reason: "A RecordBatch message can only be succeeded by a Metrics message or another RecordBatch message"
}
.fail()
.map_err(BoxedError::new)
.context(ExternalSnafu);
break;
}
Ok(None) => stream_ended = true,
Err(error) => {
yield Err(BoxedError::new(flight_stream_error(&stream_addr, error)))
.context(ExternalSnafu);
break;
}
}
yield Ok(result_to_yield);
// Deliver each batch immediately. In particular, do not
// wait for a possible following Metrics message; it is
// consumed on the next poll of this stream.
yield Ok(RecordBatch::from_df_record_batch(
schema_cloned.clone(),
record_batch,
));
}
FlightMessage::Metrics(s) => {
// Metrics may arrive before the next RecordBatch.
let m = serde_json::from_str(&s).ok().map(Arc::new);
metrics_ref.swap(m);
match serde_json::from_str(&s) {
Ok(metrics) => {
metrics_ref.swap(Some(Arc::new(metrics)));
}
Err(error) => {
common_telemetry::warn!(
"Failed to decode region Flight metrics: {}",
error
);
}
}
continue;
}
_ => {
@@ -419,13 +410,22 @@ pub fn check_response_header(header: &Option<ResponseHeader>) -> Result<()> {
#[cfg(test)]
#[allow(deprecated)]
mod test {
use api::v1::Status as PbStatus;
mod tests {
use std::collections::VecDeque;
use std::sync::Mutex;
use api::v1::region::region_server::{Region, RegionServer};
use api::v1::region::{
BulkInsertRequest, RegionResponse as PbRegionResponse, RemoteDynFilterUnregister,
RemoteDynFilterUpdate, region_request, remote_dyn_filter_request,
BulkInsertRequest, InsertRequest, InsertRequests, RegionResponse as PbRegionResponse,
RemoteDynFilterUnregister, RemoteDynFilterUpdate, region_request,
remote_dyn_filter_request,
};
use api::v1::value::ValueData;
use api::v1::{
ColumnDataType, ColumnSchema as PbColumnSchema, Row, Rows, Status as PbStatus, Value,
};
use common_error::ext::RetryHint;
use common_grpc::channel_manager::{ChannelConfig, ChannelManager};
use common_recordbatch::adapter::RecordBatchMetrics;
use datatypes::arrow::array::Int32Array;
use datatypes::prelude::{ConcreteDataType, VectorRef};
@@ -433,74 +433,526 @@ mod test {
use datatypes::vectors::Int32Vector;
use futures_util::stream;
use tokio::net::TcpListener;
use tokio::task::JoinHandle;
use tokio_stream::wrappers::TcpListenerStream;
use tonic::codec::CompressionEncoding;
use tonic::{Request, Response, Status};
use super::*;
use crate::Error::{self, IllegalDatabaseResponse, Server};
use crate::Error;
#[derive(Clone)]
struct CompressionRecordingRegionService {
zstd_headers: Arc<std::sync::Mutex<Vec<(bool, bool)>>>,
#[derive(Clone, Default)]
struct RecordingRegionService {
requests: Arc<Mutex<Vec<(RegionRequest, bool, bool)>>>,
responses: Arc<Mutex<VecDeque<std::result::Result<PbRegionResponse, Status>>>>,
}
#[tonic::async_trait]
impl Region for CompressionRecordingRegionService {
impl Region for RecordingRegionService {
async fn handle(
&self,
request: Request<RegionRequest>,
) -> std::result::Result<Response<PbRegionResponse>, Status> {
let metadata = request.metadata();
let sends_zstd = metadata
.get("grpc-encoding")
.and_then(|v| v.to_str().ok())
.is_some_and(|value| value == "zstd");
let sends_zstd = metadata.get("grpc-encoding").is_some_and(|v| v == "zstd");
let accepts_zstd = metadata
.get("grpc-accept-encoding")
.and_then(|value| value.to_str().ok())
.is_some_and(|value| value.split(',').any(|encoding| encoding == "zstd"));
self.zstd_headers
.and_then(|v| v.to_str().ok())
.is_some_and(|v| v.split(',').any(|encoding| encoding == "zstd"));
self.requests
.lock()
.unwrap()
.push((sends_zstd, accepts_zstd));
.push((request.into_inner(), sends_zstd, accepts_zstd));
self.responses
.lock()
.unwrap()
.pop_front()
.unwrap_or_else(|| Ok(build_response(StatusCode::Success)))
.map(Response::new)
}
}
Ok(Response::new(PbRegionResponse {
header: Some(ResponseHeader {
status: Some(PbStatus {
status_code: StatusCode::Success as u32,
..Default::default()
}),
struct RegionTestServer {
addr: String,
service: RecordingRegionService,
task: JoinHandle<()>,
}
impl RegionTestServer {
async fn start() -> Self {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap().to_string();
let service = RecordingRegionService::default();
let server_service = service.clone();
let task = tokio::spawn(async move {
tonic::transport::Server::builder()
.add_service(
RegionServer::new(server_service)
.accept_compressed(CompressionEncoding::Zstd)
.send_compressed(CompressionEncoding::Zstd),
)
.serve_with_incoming(TcpListenerStream::new(listener))
.await
.unwrap();
});
Self {
addr,
service,
task,
}
}
fn create_requester(
&self,
config: ChannelConfig,
send: bool,
accept: bool,
) -> RegionRequester {
RegionRequester::new(
Client::with_manager_and_urls(
ChannelManager::with_config(config, None),
[self.addr.clone()],
),
send,
accept,
)
}
}
impl Drop for RegionTestServer {
fn drop(&mut self) {
self.task.abort();
}
}
fn build_response(code: StatusCode) -> PbRegionResponse {
PbRegionResponse {
header: Some(ResponseHeader {
status: Some(PbStatus {
status_code: code as u32,
..Default::default()
}),
}),
affected_rows: 1,
..Default::default()
}
}
fn build_insert_request() -> RegionRequest {
RegionRequest {
header: Some(RegionRequestHeader {
dbname: "shared-request-test".to_string(),
..Default::default()
}))
}),
body: Some(region_request::Body::Inserts(InsertRequests {
requests: vec![InsertRequest {
region_id: 42,
rows: Some(Rows {
schema: vec![
PbColumnSchema {
column_name: "message".to_string(),
datatype: ColumnDataType::String as i32,
..Default::default()
},
PbColumnSchema {
column_name: "payload".to_string(),
datatype: ColumnDataType::Binary as i32,
..Default::default()
},
],
rows: vec![Row {
values: vec![
Value {
value_data: Some(ValueData::StringValue(
"hello 世界".to_string(),
)),
},
Value {
value_data: Some(ValueData::BinaryValue(vec![0, 1, 127, 255])),
},
],
}],
}),
..Default::default()
}],
})),
}
}
#[tokio::test]
async fn test_inserts_and_bulk_insert_use_transport_compression() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let zstd_headers = Arc::new(std::sync::Mutex::new(Vec::new()));
let service = CompressionRecordingRegionService {
zstd_headers: zstd_headers.clone(),
};
let server = tokio::spawn(async move {
tonic::transport::Server::builder()
.add_service(
RegionServer::new(service)
.accept_compressed(CompressionEncoding::Zstd)
.send_compressed(CompressionEncoding::Zstd),
)
.serve_with_incoming(TcpListenerStream::new(listener))
async fn test_owned_and_shared_requests_preserve_payload_and_compression() {
let server = RegionTestServer::start().await;
let shared = Arc::new(build_insert_request());
for (send, accept) in [(false, false), (true, false), (false, true), (true, true)] {
let requester = server.create_requester(ChannelConfig::default(), send, accept);
let channel =
tonic::transport::Endpoint::from_shared(format!("http://{}", server.addr))
.unwrap()
.connect()
.await
.unwrap();
let mut baseline = api::v1::region::region_client::RegionClient::new(channel);
if send {
baseline = baseline.send_compressed(CompressionEncoding::Zstd);
}
if accept {
baseline = baseline.accept_compressed(CompressionEncoding::Zstd);
}
assert_eq!(
baseline
.handle(build_insert_request())
.await
.unwrap()
.into_inner()
.affected_rows,
1
);
assert_eq!(
requester
.handle(build_insert_request())
.await
.unwrap()
.affected_rows,
1
);
assert_eq!(
requester
.handle(Arc::clone(&shared))
.await
.unwrap()
.affected_rows,
1
);
}
let requests = server.service.requests.lock().unwrap();
assert_eq!(requests.len(), 12);
for ((request, sends, accepts), (send, accept)) in requests.iter().zip(
[(false, false), (true, false), (false, true), (true, true)]
.into_iter()
.flat_map(|settings| [settings, settings, settings]),
) {
assert_eq!(request, shared.as_ref());
assert_eq!((*sends, *accepts), (send, accept));
}
}
#[tokio::test]
async fn test_shared_request_can_be_resent_after_transport_error() {
let server = RegionTestServer::start().await;
let mut status = Status::unavailable("migrating");
status.metadata_mut().insert(
common_error::GREPTIME_DB_HEADER_ERROR_RETRY_HINT,
RetryHint::Retryable.as_str().parse().unwrap(),
);
server
.service
.responses
.lock()
.unwrap()
.push_back(Err(status));
let requester = server.create_requester(ChannelConfig::default(), false, false);
let shared = Arc::new(build_insert_request());
let error = requester.handle(Arc::clone(&shared)).await.unwrap_err();
assert_eq!(error.retry_hint(), RetryHint::Retryable);
assert!(
matches!(error, Error::RegionServer { addr, code: tonic::Code::Unavailable, .. } if addr == server.addr)
);
requester.handle(Arc::clone(&shared)).await.unwrap();
let requests = server.service.requests.lock().unwrap();
assert_eq!(requests.len(), 2);
assert_eq!(&requests[0].0, shared.as_ref());
assert_eq!(&requests[1].0, shared.as_ref());
}
#[test]
fn test_retry_hint_wire_compatibility() {
for (name, wire_hint, expected_hint) in [
("legacy_missing", None, RetryHint::NonRetryable),
("legacy_retryable", Some("retryable"), RetryHint::Retryable),
(
"legacy_non_retryable",
Some("non_retryable"),
RetryHint::NonRetryable,
),
(
"route_refresh",
Some("retry_after_route_refresh"),
RetryHint::RetryAfterRouteRefresh,
),
(
"unknown_future_hint",
Some("future_retry_hint"),
RetryHint::NonRetryable,
),
] {
let mut status = Status::failed_precondition("region unavailable");
status.metadata_mut().insert(
common_error::GREPTIME_DB_HEADER_ERROR_CODE,
(StatusCode::RegionNotReady as u32)
.to_string()
.parse()
.unwrap(),
);
if let Some(hint) = wire_hint {
status.metadata_mut().insert(
common_error::GREPTIME_DB_HEADER_ERROR_RETRY_HINT,
hint.parse().unwrap(),
);
}
let error: Error = status.into();
assert_eq!(error.retry_hint(), expected_hint, "{name}");
assert_eq!(error.status_code(), StatusCode::RegionNotReady, "{name}");
assert_eq!(
error.tonic_code(),
Some(tonic::Code::FailedPrecondition),
"{name}"
);
}
}
#[tokio::test]
async fn test_transport_error_parity_and_datanode_mapping() {
let server = RegionTestServer::start().await;
let requester = server.create_requester(ChannelConfig::default(), false, false);
for hint in [
RetryHint::Retryable,
RetryHint::RetryAfterRouteRefresh,
RetryHint::NonRetryable,
] {
let mut status = Status::failed_precondition("region unavailable");
status.metadata_mut().insert(
common_error::GREPTIME_DB_HEADER_ERROR_RETRY_HINT,
hint.as_str().parse().unwrap(),
);
status.metadata_mut().insert(
common_error::GREPTIME_DB_HEADER_ERROR_CODE,
(StatusCode::RegionNotFound as u32)
.to_string()
.parse()
.unwrap(),
);
server
.service
.responses
.lock()
.unwrap()
.extend((0..4).map(|_| Err(status.clone())));
let mut baseline = api::v1::region::region_client::RegionClient::connect(format!(
"http://{}",
server.addr
))
.await
.unwrap();
let baseline: Error = baseline
.handle(build_insert_request())
.await
.unwrap();
.unwrap_err()
.into();
assert_eq!(baseline.status_code(), StatusCode::RegionNotFound);
assert_eq!(baseline.retry_hint(), hint);
assert_eq!(baseline.tonic_code(), Some(tonic::Code::FailedPrecondition));
let owned = requester.handle(build_insert_request()).await.unwrap_err();
let shared = requester
.handle(Arc::new(build_insert_request()))
.await
.unwrap_err();
for error in [owned, shared] {
assert_eq!(error.status_code(), baseline.status_code());
assert_eq!(error.retry_hint(), baseline.retry_hint());
assert_eq!(error.tonic_code(), baseline.tonic_code());
}
let datanode: &dyn Datanode = &requester;
let error = datanode.handle(build_insert_request()).await.unwrap_err();
assert_eq!(error.status_code(), StatusCode::RegionNotFound);
assert_eq!(error.retry_hint(), hint);
assert!(matches!(error, meta_error::Error::External { .. }));
}
}
#[tokio::test]
async fn test_datanode_transport_retry_hints() {
let server = RegionTestServer::start().await;
let requester = server.create_requester(ChannelConfig::default(), false, false);
let datanode: &dyn Datanode = &requester;
let statuses = [
tonic::Code::Unavailable,
tonic::Code::Cancelled,
tonic::Code::DeadlineExceeded,
tonic::Code::FailedPrecondition,
]
.into_iter()
.flat_map(|code| {
[
None,
Some(RetryHint::NonRetryable),
Some(RetryHint::Retryable),
Some(RetryHint::RetryAfterRouteRefresh),
]
.into_iter()
.map(move |hint| (code, hint))
});
let client = Client::with_urls([addr.to_string()]);
let requester = RegionRequester::new(client.clone(), true, true);
let send_only_requester = RegionRequester::new(client.clone(), true, false);
let accept_only_requester = RegionRequester::new(client.clone(), false, true);
let disabled_requester = RegionRequester::new(client, false, false);
for (code, hint) in statuses {
for body in [
region_request::Body::Inserts(Default::default()),
region_request::Body::Deletes(Default::default()),
region_request::Body::BulkInsert(Default::default()),
region_request::Body::Create(Default::default()),
region_request::Body::RemoteDynFilter(Default::default()),
] {
let request_type = body.as_ref().to_string();
let mut status = Status::new(code, "request failed");
// Metadata status is deliberately different from the transport status.
status.metadata_mut().insert(
common_error::GREPTIME_DB_HEADER_ERROR_CODE,
(StatusCode::RegionNotReady as u32)
.to_string()
.parse()
.unwrap(),
);
if let Some(hint) = hint {
status.metadata_mut().insert(
common_error::GREPTIME_DB_HEADER_ERROR_RETRY_HINT,
hint.as_str().parse().unwrap(),
);
}
let original = Error::from(status.clone());
let expected = if original.should_retry() && !original.retry_hint().is_retryable() {
RetryHint::Retryable
} else {
original.retry_hint()
};
server
.service
.responses
.lock()
.unwrap()
.extend([Err(status.clone()), Err(status)]);
let request = RegionRequest {
header: None,
body: Some(body),
};
let shared = Arc::new(request);
let shared_error = datanode.handle(Arc::clone(&shared)).await.unwrap_err();
assert_eq!(Arc::strong_count(&shared), 1);
let request = Arc::try_unwrap(shared).unwrap();
let owned_error = datanode.handle(request).await.unwrap_err();
for error in [owned_error, shared_error] {
assert_eq!(
error.retry_hint(),
expected,
"{code:?}, {hint:?}, {request_type}"
);
assert_eq!(error.retry_hint().is_retryable(), original.should_retry());
assert_eq!(error.status_code(), StatusCode::RegionNotReady);
let meta_error::Error::External { source, .. } = error else {
panic!("expected an external error");
};
assert_eq!(source.retry_hint(), expected);
let Error::RegionServer {
source,
code: actual_code,
..
} = source.as_any().downcast_ref::<Error>().unwrap()
else {
panic!("expected a region server error");
};
assert_eq!(*actual_code, code);
assert_eq!(source.retry_hint(), original.retry_hint());
assert_eq!(source.to_string(), original.to_string());
}
}
}
}
#[tokio::test]
async fn test_shared_request_preserves_response_header_errors() {
let server = RegionTestServer::start().await;
server.service.responses.lock().unwrap().extend([
Ok(PbRegionResponse::default()),
Ok(build_response(StatusCode::RegionNotFound)),
]);
let requester = server.create_requester(ChannelConfig::default(), false, false);
let shared = Arc::new(build_insert_request());
assert!(matches!(
requester.handle(Arc::clone(&shared)).await.unwrap_err(),
Error::IllegalDatabaseResponse { .. }
));
assert!(matches!(
requester.handle(shared).await.unwrap_err(),
Error::Server {
code: StatusCode::RegionNotFound,
..
}
));
}
#[tokio::test]
async fn test_shared_request_respects_configured_message_limits() {
let server = RegionTestServer::start().await;
let shared = Arc::new(build_insert_request());
for send_limit in [true, false] {
let mut config = ChannelConfig::default();
if send_limit {
config.max_send_message_size = "1B".parse().unwrap();
} else {
config.max_recv_message_size = "1B".parse().unwrap();
}
let mut baseline = api::v1::region::region_client::RegionClient::connect(format!(
"http://{}",
server.addr
))
.await
.unwrap()
.max_encoding_message_size(config.max_send_message_size.as_bytes() as usize)
.max_decoding_message_size(config.max_recv_message_size.as_bytes() as usize);
let baseline: Error = baseline
.handle(build_insert_request())
.await
.unwrap_err()
.into();
if !send_limit {
assert_eq!(
baseline.tonic_code(),
Some(tonic::Code::OutOfRange),
"{baseline:?}"
);
}
let requester = server.create_requester(config, false, false);
let error = requester.handle(Arc::clone(&shared)).await.unwrap_err();
assert_eq!(
error.tonic_code(),
baseline.tonic_code(),
"shared: {error:?}; generated: {baseline:?}"
);
assert_eq!(
error.status_code(),
baseline.status_code(),
"shared: {error:?}; generated: {baseline:?}"
);
let Error::RegionServer { source, .. } = &error else {
panic!("expected a region server error: {error:?}");
};
assert_eq!(
source.retry_hint(),
baseline.retry_hint(),
"shared: {error:?}; generated: {baseline:?}"
);
assert_eq!(
server.service.requests.lock().unwrap().len(),
if send_limit { 0 } else { 2 }
);
}
let requester = server.create_requester(ChannelConfig::default(), false, false);
requester.handle(shared).await.unwrap();
}
#[tokio::test]
async fn test_inserts_and_bulk_insert_use_transport_compression() {
let server = RegionTestServer::start().await;
let requester = server.create_requester(ChannelConfig::default(), true, true);
let send_only_requester = server.create_requester(ChannelConfig::default(), true, false);
let accept_only_requester = server.create_requester(ChannelConfig::default(), false, true);
let disabled_requester = server.create_requester(ChannelConfig::default(), false, false);
let inserts = || RegionRequest {
body: Some(region_request::Body::Inserts(Default::default())),
..Default::default()
@@ -536,9 +988,15 @@ mod test {
(false, false),
(false, false)
],
*zstd_headers.lock().unwrap()
server
.service
.requests
.lock()
.unwrap()
.iter()
.map(|(_, send, accept)| (*send, *accept))
.collect::<Vec<_>>()
);
server.abort();
}
#[test]
@@ -600,13 +1058,13 @@ mod test {
let result = check_response_header(&None);
assert!(matches!(
result.unwrap_err(),
IllegalDatabaseResponse { .. }
Error::IllegalDatabaseResponse { .. }
));
let result = check_response_header(&Some(ResponseHeader { status: None }));
assert!(matches!(
result.unwrap_err(),
IllegalDatabaseResponse { .. }
Error::IllegalDatabaseResponse { .. }
));
let result = check_response_header(&Some(ResponseHeader {
@@ -625,7 +1083,7 @@ mod test {
}));
assert!(matches!(
result.unwrap_err(),
IllegalDatabaseResponse { .. }
Error::IllegalDatabaseResponse { .. }
));
let result = check_response_header(&Some(ResponseHeader {
@@ -634,7 +1092,7 @@ mod test {
err_msg: "blabla".to_string(),
}),
}));
let Server { code, msg, .. } = result.unwrap_err() else {
let Error::Server { code, msg, .. } = result.unwrap_err() else {
unreachable!()
};
assert_eq!(code, StatusCode::Internal);
@@ -719,7 +1177,34 @@ mod test {
}
#[tokio::test]
async fn test_record_batch_stream_updates_following_metrics_before_yielding_batch() {
async fn test_record_batch_is_yielded_without_waiting_for_next_message() {
let schema = test_schema();
let batch = RecordBatch::new(
schema.clone(),
vec![Arc::new(Int32Vector::from_slice([1])) as VectorRef],
)
.unwrap();
let messages = stream::iter(vec![
Ok(FlightMessage::Schema(schema.arrow_schema().clone())),
Ok(FlightMessage::RecordBatch(batch.into_df_record_batch())),
])
.chain(stream::pending::<Result<FlightMessage>>());
let mut recordbatches =
recordbatches_from_flight_message_stream("test-peer".to_string(), messages)
.await
.unwrap();
let batch = tokio::time::timeout(Duration::from_secs(1), recordbatches.next())
.await
.expect("the first batch must not wait for lookahead")
.unwrap()
.unwrap();
assert_eq!(batch.num_rows(), 1);
}
#[tokio::test]
async fn test_malformed_region_metrics_are_non_fatal() {
let schema = test_schema();
let batch = RecordBatch::new(
schema.clone(),
@@ -730,23 +1215,19 @@ mod test {
"test-peer".to_string(),
stream::iter(vec![
Ok(FlightMessage::Schema(schema.arrow_schema().clone())),
Ok(FlightMessage::Metrics("{not-json}".to_string())),
Ok(FlightMessage::RecordBatch(batch.into_df_record_batch())),
Ok(FlightMessage::Metrics(test_metrics_json())),
]),
)
.await
.unwrap();
let batch = recordbatches.next().await.unwrap().unwrap();
assert_eq!(batch.num_rows(), 1);
let metrics = recordbatches.metrics().unwrap();
assert_eq!(metrics.elapsed_compute, 7);
assert_eq!(recordbatches.next().await.unwrap().unwrap().num_rows(), 1);
assert!(recordbatches.next().await.is_none());
}
#[tokio::test]
async fn test_record_batch_stream_preserves_peeked_record_batch() {
async fn test_record_batch_stream_preserves_following_record_batch() {
let schema = test_schema();
let first_batch = RecordBatch::new(
schema.clone(),
+63
View File
@@ -0,0 +1,63 @@
// 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::borrow::Borrow;
use std::marker::PhantomData;
use api::v1::region::{RegionRequest, RegionResponse};
use prost::Message;
use tonic::Status;
use tonic::codec::{Codec, EncodeBuf, Encoder};
use tonic_prost::ProstDecoder;
/// Encodes owned or shared region requests using the existing protobuf wire format.
pub(crate) struct RegionCodec<R>(PhantomData<R>);
impl<R> Default for RegionCodec<R> {
fn default() -> Self {
Self(PhantomData)
}
}
impl<R> Codec for RegionCodec<R>
where
R: Borrow<RegionRequest> + Send + 'static,
{
type Encode = R;
type Decode = RegionResponse;
type Encoder = RegionEncoder<R>;
type Decoder = ProstDecoder<RegionResponse>;
fn encoder(&mut self) -> Self::Encoder {
RegionEncoder(PhantomData)
}
fn decoder(&mut self) -> Self::Decoder {
ProstDecoder::default()
}
}
/// Borrows the protobuf payload while consuming its owned or shared container.
pub(crate) struct RegionEncoder<R>(PhantomData<R>);
impl<R: Borrow<RegionRequest>> Encoder for RegionEncoder<R> {
type Item = R;
type Error = Status;
fn encode(&mut self, item: R, buf: &mut EncodeBuf<'_>) -> Result<(), Status> {
item.borrow()
.encode(buf)
.map_err(|error| Status::internal(error.to_string()))
}
}
+255
View File
@@ -475,6 +475,261 @@ fn test_load_standalone_example_config() {
similar_asserts::assert_eq!(options, expected);
}
/// Renders an example config with its `#+` options enabled, as `config.md`
/// lists them. With `drop_none_default`, options annotated with
/// `@toml2docs:none-default` are removed, because `config.md` does not present
/// their example value as the default.
fn render_example(example: &str, drop_none_default: bool) -> String {
let mut rendered = String::new();
let mut none_default = false;
for line in example.lines() {
let line = line.trim_start();
if line.starts_with("##") {
none_default |= line.contains("@toml2docs:none-default");
continue;
}
let line = line.strip_prefix("#+").map_or(line, str::trim_start);
let is_option = !line.starts_with('[') && line.contains('=');
if !(is_option && drop_none_default && none_default) {
rendered.push_str(line);
rendered.push('\n');
}
none_default = false;
}
rendered
}
/// Flattens leaf values into dotted paths. Arrays of tables such as
/// `region_engine` are merged, since each element is keyed by its engine name.
fn leaf_values(value: &toml::Value, prefix: &str, leaves: &mut Vec<(String, toml::Value)>) {
match value {
toml::Value::Table(table) => {
for (key, value) in table {
let path = if prefix.is_empty() {
key.clone()
} else {
format!("{prefix}.{key}")
};
leaf_values(value, &path, leaves);
}
}
toml::Value::Array(array)
if array.iter().all(toml::Value::is_table) && !array.is_empty() =>
{
for element in array {
leaf_values(element, prefix, leaves);
}
}
_ => leaves.push((prefix.to_string(), value.clone())),
}
}
/// Removes the option at a dotted path produced by [`leaf_values`].
fn remove_leaf(value: &mut toml::Value, path: &str) -> bool {
match value {
toml::Value::Array(array) => array.iter_mut().any(|element| remove_leaf(element, path)),
toml::Value::Table(table) => match path.split_once('.') {
None => table.remove(path).is_some(),
Some((head, rest)) => table
.get_mut(head)
.is_some_and(|value| remove_leaf(value, rest)),
},
_ => false,
}
}
fn load_as_leaves<T>(
config: Option<&str>,
) -> common_config::error::Result<std::collections::HashMap<String, toml::Value>>
where
T: Configurable + serde::Serialize,
{
let file = config.map(|config| {
let mut file = create_named_temp_file();
file.write_all(config.as_bytes()).unwrap();
file
});
let options = GreptimeOptions::<T>::load_layered_options(
file.as_ref().map(|file| file.path().to_str().unwrap()),
"",
)?;
let mut leaves = Vec::new();
leaf_values(&toml::Value::try_from(&options).unwrap(), "", &mut leaves);
Ok(leaves.into_iter().collect())
}
/// Returns the default of an option inside the example's other settings, so
/// that options in sections absent by default still have one to compare with.
/// Removes the closest enclosing table that still loads without the option.
fn default_within_example<T>(full: &toml::Value, path: &str) -> Option<toml::Value>
where
T: Configurable + serde::Serialize,
{
let mut removed = path;
loop {
let mut without = full.clone();
assert!(remove_leaf(&mut without, removed));
if let Ok(defaults) = load_as_leaves::<T>(Some(&toml::to_string(&without).unwrap())) {
return defaults.get(path).cloned();
}
removed = removed
.rsplit_once('.')
.unwrap_or_else(|| panic!("the example does not load without `{path}`"))
.0;
}
}
/// Checks that every option in an example config exists, and that every value
/// `config.md` presents as a default is the actual default.
///
/// `variants` lists tagged sections, e.g. `("wal", "provider", ["kafka"])`,
/// whose options only deserialize under a specific tag value.
fn check_example_defaults<T>(name: &str, variants: &[(&str, &str, &[&str])]) -> Vec<String>
where
T: Configurable + serde::Serialize,
{
let example = std::fs::read_to_string(common_test_util::find_workspace_path(&format!(
"config/{name}"
)))
.unwrap();
let full: toml::Value = toml::from_str(&render_example(&example, false)).unwrap();
let documented: toml::Value = toml::from_str(&render_example(&example, true)).unwrap();
// (options loaded from the example, options loaded without config) per variant
let mut loaded = vec![(
load_as_leaves::<T>(Some(&toml::to_string(&full).unwrap())).unwrap(),
load_as_leaves::<T>(None).unwrap(),
)];
for (section, tag, values) in variants {
for value in *values {
let mut variant = full.clone();
variant[section][tag] = toml::Value::String(value.to_string());
loaded.push((
load_as_leaves::<T>(Some(&toml::to_string(&variant).unwrap())).unwrap(),
load_as_leaves::<T>(Some(&format!("[{section}]\n{tag} = \"{value}\"\n"))).unwrap(),
));
}
}
// Deserialize-only fields, invisible in the serialized options.
const SKIP_SERIALIZING: &[&str] = &[
"storage.access_key_id",
"storage.secret_access_key",
"storage.access_key_secret",
"storage.account_name",
"storage.account_key",
"storage.credential_path",
"storage.credential",
"region_engine.mito.inverted_index.intermediate_path",
];
let mut errors = Vec::new();
let mut full_leaves = Vec::new();
leaf_values(&full, "", &mut full_leaves);
for (path, _) in full_leaves {
if SKIP_SERIALIZING.contains(&path.as_str())
|| (path == "tracing.tokio_console_addr" && !cfg!(feature = "tokio-console"))
{
continue;
}
if !loaded
.iter()
.any(|(example, _)| example.contains_key(&path))
{
errors.push(format!("{name}: `{path}` is not a known option"));
}
}
// Options left unset by default and resolved where they are used.
let resolved_at_runtime: [(&str, toml::Value); 7] = [
(
"logging.dir",
format!("{DEFAULT_DATA_HOME}/{DEFAULT_LOGGING_DIR}").into(),
),
("logging.otlp_endpoint", DEFAULT_OTLP_HTTP_ENDPOINT.into()),
("logging.otlp_export_protocol", "http".into()),
("logging.tracing_sample_ratio.default_ratio", 1.0.into()),
("wal.sync_period", "5s".into()),
(
"storage.copy_root",
format!("{DEFAULT_DATA_HOME}/copy").into(),
),
("flow.batching_mode.frontend_tls.enabled", false.into()),
];
let mut documented_leaves = Vec::new();
leaf_values(&documented, "", &mut documented_leaves);
for (path, documented_value) in documented_leaves {
if SKIP_SERIALIZING.contains(&path.as_str()) {
continue;
}
if let Some((_, resolved)) = resolved_at_runtime.iter().find(|(p, _)| *p == path) {
if &documented_value != resolved {
errors.push(format!(
"{name}: `{path}` is documented as default `{documented_value}`, but resolves to `{resolved}`"
));
}
continue;
}
let mut pairs = loaded
.iter()
.filter_map(|(example, default)| Some((example.get(&path)?, default.get(&path)?)))
.map(|(example, default)| (example.clone(), default.clone()))
.collect::<Vec<_>>();
if pairs.is_empty() {
// The option sits in a section that is absent without config, or
// is itself optional.
match default_within_example::<T>(&full, &path) {
Some(default) => pairs.push((loaded[0].0[&path].clone(), default)),
None => {
errors.push(format!(
"{name}: `{path}` has no default to compare with; annotate it with \
`@toml2docs:none-default` or list its runtime default in this test"
));
continue;
}
}
}
if pairs.iter().all(|(example, default)| example != default) {
let (example, default) = &pairs[0];
errors.push(format!(
"{name}: `{path}` is documented as default `{example}`, but the default is `{default}`; \
fix the value or annotate it with `@toml2docs:none-default`"
));
}
}
errors
}
// The examples document HDFS storage options.
#[cfg(feature = "hdfs-object-store")]
#[test]
fn test_example_configs_document_actual_defaults() {
const WAL: (&str, &str, &[&str]) = ("wal", "provider", &["kafka", "experimental_object_store"]);
const STORAGE: (&str, &str, &[&str]) =
("storage", "type", &["S3", "Oss", "Azblob", "Gcs", "Hdfs"]);
let mut errors =
check_example_defaults::<StandaloneOptions>("standalone.example.toml", &[WAL, STORAGE]);
errors.extend(check_example_defaults::<FrontendOptions>(
"frontend.example.toml",
&[],
));
errors.extend(check_example_defaults::<DatanodeOptions>(
"datanode.example.toml",
&[WAL, STORAGE],
));
errors.extend(check_example_defaults::<MetasrvOptions>(
"metasrv.example.toml",
&[("wal", "provider", &["kafka"])],
));
errors.extend(check_example_defaults::<FlownodeOptions>(
"flownode.example.toml",
&[],
));
assert!(errors.is_empty(), "{}", errors.join("\n"));
}
#[test]
fn test_load_removed_histogram_options() {
for enabled in [false, true] {
+1
View File
@@ -14,6 +14,7 @@ async-trait.workspace = true
bitvec = "1.0"
bytes.workspace = true
futures.workspace = true
icu_properties.workspace = true
lazy_static.workspace = true
pin-project.workspace = true
regex.workspace = true
+1
View File
@@ -25,6 +25,7 @@ pub mod readable_size;
pub mod regex_pattern;
pub mod secrets;
pub mod serde;
pub mod term_token;
pub type AffectedRows = usize;
+280
View File
@@ -0,0 +1,280 @@
// 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.
//! Word boundaries shared by `matches_term` and the fulltext bloom index.
//!
//! Bloom pruning is only sound if the index splits text exactly where
//! `matches_term` sees word boundaries, so both use [`classify_char`], and the
//! query side derives its probes with [`extract_term_probes`] and [`extract_like_probes`].
use std::ops::Range;
use icu_properties::props::Script;
use icu_properties::{CodePointMapData, CodePointMapDataBorrowed};
/// Classes of characters that decide where `matches_term` sees a word boundary.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CharClass {
/// `[0-9A-Za-z]`.
AsciiWord,
/// Characters of the Han script, alphanumeric or not.
Han,
/// Non-ASCII alphanumerics outside the Han script, such as `é` or `д`.
UnicodeWord,
/// Everything else, including `_`.
Other,
}
/// Returns the [`CharClass`] of `c`. [`tokenize`] splits text wherever the class
/// changes and at every [`CharClass::Other`] character.
pub fn classify_char(c: char) -> CharClass {
if c.is_ascii_alphanumeric() {
CharClass::AsciiWord
} else if is_han(c) {
CharClass::Han
} else if c.is_alphanumeric() {
CharClass::UnicodeWord
} else {
CharClass::Other
}
}
static HAN_SCRIPT_DATA: CodePointMapDataBorrowed<'static, Script> =
CodePointMapData::<Script>::new();
fn is_han(c: char) -> bool {
HAN_SCRIPT_DATA.get(c) == Script::Han
}
/// Returns whether lowercasing `text` as a whole can produce tokens that lowercasing
/// each token of `text` doesn't, which `matches_term(lower(col), ..)` and
/// `lower(col) LIKE ..` rely on. Every other character lowercases to one character
/// of the same class, but U+0130 and U+212A change length or class, and `Σ`
/// lowercases depending on the characters after it.
pub fn has_irregular_lowercase(text: &str) -> bool {
!text.is_ascii() && text.contains(['\u{130}', '\u{212A}', 'Σ'])
}
/// Returns the tokens the fulltext bloom index stores for `text`.
///
/// A token is a maximal run of ASCII alphanumerics or of non-Han alphanumerics;
/// `_` and other symbols only separate tokens. A run of Han characters is
/// emitted as its bigrams, or as itself when it has a single character, since
/// `matches_term` matches Han terms as plain substrings.
pub fn tokenize(text: &str) -> Vec<&str> {
let mut tokens = Vec::new();
for_each_word_run(text, |class, range| {
let run = &text[range];
if class == CharClass::Han {
push_han_tokens(run, &mut tokens);
} else {
tokens.push(run);
}
});
tokens
}
/// Calls `f` on every maximal run of word characters of the same class.
fn for_each_word_run(text: &str, mut f: impl FnMut(CharClass, Range<usize>)) {
if text.is_ascii() {
// ASCII text only has `AsciiWord` and `Other` characters, so scan bytes
// instead of decoding chars. Most log lines take this path.
let mut start = None;
for (i, b) in text.bytes().enumerate() {
match (b.is_ascii_alphanumeric(), start) {
(true, None) => start = Some(i),
(false, Some(s)) => {
f(CharClass::AsciiWord, s..i);
start = None;
}
_ => {}
}
}
if let Some(s) = start {
f(CharClass::AsciiWord, s..text.len());
}
return;
}
let mut run: Option<(CharClass, usize)> = None;
for (i, c) in text.char_indices() {
let class = classify_char(c);
if let Some((run_class, start)) = run {
if run_class == class {
continue;
}
f(run_class, start..i);
run = None;
}
if class != CharClass::Other {
run = Some((class, i));
}
}
if let Some((run_class, start)) = run {
f(run_class, start..text.len());
}
}
fn push_han_tokens<'a>(run: &'a str, tokens: &mut Vec<&'a str>) {
let mut starts = run.char_indices().map(|(i, _)| i).skip(1);
let Some(mut second) = starts.next() else {
tokens.push(run);
return;
};
let mut first = 0;
for next in starts.chain(std::iter::once(run.len())) {
tokens.push(&run[first..next]);
first = second;
second = next;
}
}
/// Returns tokens that every text satisfying `matches_term(text, term)` contains,
/// as produced by [`tokenize`].
pub fn extract_term_probes(term: &str) -> Vec<String> {
// Non-Han terms only match between word boundaries that `tokenize` also
// splits at, so their edge runs are whole tokens. Han-containing terms match
// as plain substrings and give no such guarantee at their edges.
let bounded = !term.chars().any(|c| classify_char(c) == CharClass::Han);
let mut probes = Vec::new();
push_literal_probes(term, bounded, bounded, &mut probes);
probes
}
/// Returns tokens that every text satisfying `text LIKE pattern` contains, as
/// produced by [`tokenize`]. `\` escapes the next character, following
/// arrow's `like` kernel; a trailing `\` is a literal backslash.
pub fn extract_like_probes(pattern: &str) -> Vec<String> {
let mut probes = Vec::new();
let mut literal = String::new();
// The pattern start and end are anchors; a wildcard can stand for word
// characters, so a run touching it may be part of a longer token.
let mut anchored_left = true;
let mut chars = pattern.chars();
while let Some(c) = chars.next() {
match c {
'%' | '_' => {
push_literal_probes(&literal, anchored_left, false, &mut probes);
literal.clear();
anchored_left = false;
}
'\\' => literal.push(chars.next().unwrap_or('\\')),
c => literal.push(c),
}
}
push_literal_probes(&literal, anchored_left, true, &mut probes);
probes
}
/// Pushes the tokens of `literal` that stay whole tokens wherever `literal`
/// occurs. A side is bounded when the character beyond it can't extend the
/// adjacent run.
fn push_literal_probes(
literal: &str,
left_bounded: bool,
right_bounded: bool,
probes: &mut Vec<String>,
) {
for_each_word_run(literal, |class, range| {
let whole =
(range.start > 0 || left_bounded) && (range.end < literal.len() || right_bounded);
let run = &literal[range];
if class == CharClass::Han && run.chars().nth(1).is_some() {
// Bigrams inside the run occur in every text containing it.
let mut tokens = Vec::new();
push_han_tokens(run, &mut tokens);
probes.extend(tokens.into_iter().map(str::to_string));
} else if whole {
probes.push(run.to_string());
}
});
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn han_detection_uses_script_not_all_cjk() {
assert!(is_han('汉'));
assert!(is_han('\u{30000}'));
assert!(!is_han('あ'));
assert!(!is_han('한'));
}
#[test]
fn test_script_tokenizer() {
let cases: [(&str, &[&str]); 7] = [
("hello_world __x__", &["hello", "world", "x"]),
("trace_id=abc", &["trace", "id", "abc"]),
("错误error日志", &["错误", "error", "日志"]),
("连接timeout.5次", &["连接", "timeout", "5", "次"]),
("用户user-123登录", &["用户", "user", "123", "登录"]),
("中国农业银行", &["中国", "国农", "农业", "业银", "银行"]),
("Größe日x", &["Gr", "öß", "e", "日", "x"]),
];
for (text, expected) in cases {
assert_eq!(tokenize(text), expected, "text: {text}");
}
}
#[test]
fn test_irregular_lowercase_chars() {
for c in (0..=0x10FFFF).filter_map(char::from_u32) {
let lower = c.to_lowercase().collect::<Vec<_>>();
let regular = lower.len() == 1 && classify_char(lower[0]) == classify_char(c);
let expect_regular = !matches!(c, '\u{130}' | '\u{212A}');
assert_eq!(regular, expect_regular, "U+{:04X}", c as u32);
}
}
#[test]
fn test_term_probes() {
let cases: [(&str, &[&str]); 8] = [
("world", &["world"]),
("hello world", &["hello", "world"]),
("/start", &["start"]),
("naïve", &["na", "ï", "ve"]),
// Han terms match as substrings: only bigrams and runs enclosed by
// other characters of the term are safe.
("农业", &["农业"]),
("农", &[]),
("机号1888", &["机号"]),
("x错误y日z", &["错误", "y", "日"]),
];
for (term, expected) in cases {
assert_eq!(extract_term_probes(term), expected, "term: {term}");
}
}
#[test]
fn test_like_probes() {
let cases: [(&str, &[&str]); 11] = [
("%timeout%", &[]),
("% timeout %", &["timeout"]),
("timeout%", &[]),
("timeout: %", &["timeout"]),
("%: timeout", &["timeout"]),
("a_b c", &["c"]),
(r"trace\_id=%", &["trace", "id"]),
(r"100\% done", &["100", "done"]),
(r"x\\y", &["x", "y"]),
("%错误error日%", &["错误", "error"]),
("%错误 x%", &["错误"]),
];
for (pattern, expected) in cases {
assert_eq!(extract_like_probes(pattern), expected, "pattern: {pattern}");
}
}
}
+16 -2
View File
@@ -46,6 +46,10 @@ use crate::status_code::StatusCode;
pub enum RetryHint {
/// The operation may succeed if retried later.
Retryable,
/// The operation may succeed after refreshing routing and partition information.
/// The request may need rebuilding when its partition assignment changes.
/// This hint does not imply that a failed operation had no side effects.
RetryAfterRouteRefresh,
/// Retrying the same operation is not expected to help.
///
/// This is the default for errors that do not explicitly opt in to retry.
@@ -53,16 +57,23 @@ pub enum RetryHint {
}
const RETRY_HINT_RETRYABLE: &str = "retryable";
const RETRY_HINT_AFTER_ROUTE_REFRESH: &str = "retry_after_route_refresh";
const RETRY_HINT_NON_RETRYABLE: &str = "non_retryable";
impl RetryHint {
/// Returns whether another attempt may succeed, possibly after refreshing routes.
pub fn is_retryable(self) -> bool {
matches!(self, RetryHint::Retryable)
matches!(
self,
RetryHint::Retryable | RetryHint::RetryAfterRouteRefresh
)
}
/// Returns the retry hint encoded in gRPC error metadata.
pub fn as_str(self) -> &'static str {
match self {
RetryHint::Retryable => RETRY_HINT_RETRYABLE,
RetryHint::RetryAfterRouteRefresh => RETRY_HINT_AFTER_ROUTE_REFRESH,
RetryHint::NonRetryable => RETRY_HINT_NON_RETRYABLE,
}
}
@@ -90,6 +101,7 @@ impl FromStr for RetryHint {
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
RETRY_HINT_RETRYABLE => Ok(RetryHint::Retryable),
RETRY_HINT_AFTER_ROUTE_REFRESH => Ok(RetryHint::RetryAfterRouteRefresh),
RETRY_HINT_NON_RETRYABLE => Ok(RetryHint::NonRetryable),
_ => Err(()),
}
@@ -133,7 +145,9 @@ pub trait ErrorExt: StackError {
///
/// Implementations should return [`RetryHint::Retryable`] only when retrying the
/// same operation may succeed without changing the request. The default is
/// [`RetryHint::NonRetryable`] to avoid accidental retry loops.
/// [`RetryHint::NonRetryable`] to avoid accidental retry loops. Use
/// [`RetryHint::RetryAfterRouteRefresh`] when routing or partition information
/// must be refreshed before another attempt.
fn retry_hint(&self) -> RetryHint {
RetryHint::NonRetryable
}
+9
View File
@@ -164,6 +164,15 @@ fn test_transparent_flag() {
#[test]
fn test_retry_hint_helpers() {
assert!(RetryHint::Retryable.is_retryable());
assert!(RetryHint::RetryAfterRouteRefresh.is_retryable());
assert_eq!(
RetryHint::RetryAfterRouteRefresh.as_str(),
"retry_after_route_refresh"
);
assert_eq!(
RetryHint::from_str("retry_after_route_refresh").unwrap(),
RetryHint::RetryAfterRouteRefresh
);
assert!(!RetryHint::NonRetryable.is_retryable());
assert_eq!(RetryHint::Retryable.as_str(), "retryable");
-1
View File
@@ -46,7 +46,6 @@ geo-types = { version = "0.7", optional = true }
geohash = { version = "0.13", optional = true }
h3o = { version = "0.6", optional = true }
hyperloglogplus = "0.4"
icu_properties.workspace = true
jsonb.workspace = true
memchr = "2.7"
mito-codec.workspace = true
+1
View File
@@ -14,6 +14,7 @@
pub mod approximate;
pub mod count_hash;
pub mod extremum;
#[cfg(feature = "geo")]
pub mod geo;
pub mod vector;
+39 -4
View File
@@ -22,6 +22,7 @@
//! `foo_merge`'s input arg is the same as `foo_state`'s output, and its output is the same as `foo`'s input.
//!
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
@@ -183,7 +184,13 @@ impl StateMergeHelper {
)));
}
};
let mut lower_aggr_exprs = vec![];
// (unaliased state expression, output column name)
let mut lower_states: Vec<(Expr, String)> = vec![];
let mut lower_output_names: HashSet<String> = aggr
.group_expr
.iter()
.map(|e| e.qualified_name().1)
.collect();
let mut upper_aggr_exprs = vec![];
// group exprs for upper plan should refer to the output group expr as column from lower plan
@@ -218,9 +225,28 @@ impl StateMergeHelper {
params: aggr_func.params.clone(),
};
let expr = Expr::AggregateFunction(expr);
let lower_state_output_col_name = expr.schema_name().to_string();
lower_aggr_exprs.push(expr);
// The state column is named after the unaliased state expression, which can
// repeat even though the original output names are unique: a name-preserving
// rewrite such as `count(*)` -> `count(ts) AS count(*)` can make two aggregates
// share one state expression, and schema names hide casts, so
// `count(CAST(ts AS Int64))` is named `count(t.ts)` too. Identical states are
// computed once; a different state with a taken name gets a numeric suffix.
let lower_state_output_col_name =
if let Some((_, name)) = lower_states.iter().find(|(e, _)| *e == expr) {
name.clone()
} else {
let base = expr.schema_name().to_string();
let mut name = base.clone();
let mut suffix = 0;
while lower_output_names.contains(&name) {
suffix += 1;
name = format!("{base}_{suffix}");
}
lower_output_names.insert(name.clone());
lower_states.push((expr, name.clone()));
name
};
// then create the merge function using the physical expression of the original aggregate function
let (name, human_display) = match aggr_expr {
@@ -270,7 +296,16 @@ impl StateMergeHelper {
}
let mut lower = aggr.clone();
lower.aggr_expr = lower_aggr_exprs;
lower.aggr_expr = lower_states
.into_iter()
.map(|(expr, name)| {
if expr.schema_name().to_string() == name {
expr
} else {
expr.alias(name)
}
})
.collect();
let lower_plan = LogicalPlan::Aggregate(lower);
// update aggregate's output schema
+259
View File
@@ -0,0 +1,259 @@
// 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.
//! PromQL `min` and `max` aggregation operators.
//!
//! PromQL ignores NaN samples unless every sample of a group is NaN. DataFusion's
//! `min`/`max` order NaN differently depending on the code path (its grouped
//! accumulator lets NaN replace the current value and be replaced by the next one),
//! so the result depends on the order of the input.
use std::sync::Arc;
use datafusion::arrow::array::{ArrayRef, AsArray};
use datafusion::arrow::datatypes::{DataType, Field, FieldRef, Float64Type};
use datafusion::error::Result as DfResult;
use datafusion::logical_expr::function::{AccumulatorArgs, StateFieldsArgs};
use datafusion::logical_expr::{
Accumulator, AggregateUDF, AggregateUDFImpl, GroupsAccumulator, Signature, Volatility,
};
use datafusion_common::ScalarValue;
use datafusion_functions_aggregate_common::aggregate::groups_accumulator::prim_op::PrimitiveGroupsAccumulator;
use crate::function_registry::FunctionRegistry;
/// `max` or `min` with PromQL NaN semantics.
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct Extremum {
name: &'static str,
is_max: bool,
signature: Signature,
}
impl Extremum {
pub const MAX_NAME: &'static str = "prom_max";
pub const MIN_NAME: &'static str = "prom_min";
/// Registers both functions; the registry also derives their state functions, which
/// distributed plans need to split the aggregation between datanodes and the frontend.
pub fn register(registry: &FunctionRegistry) {
registry.register_aggr(Self::max_udaf());
registry.register_aggr(Self::min_udaf());
}
pub fn max_udaf() -> AggregateUDF {
AggregateUDF::from(Self::new(Self::MAX_NAME, true))
}
pub fn min_udaf() -> AggregateUDF {
AggregateUDF::from(Self::new(Self::MIN_NAME, false))
}
fn new(name: &'static str, is_max: bool) -> Self {
Self {
name,
is_max,
signature: Signature::exact(vec![DataType::Float64], Volatility::Immutable),
}
}
}
/// Folds `value` into `current` like Prometheus' aggregation: a NaN is replaced by any
/// sample, and a NaN sample never replaces a number.
fn fold(is_max: bool, current: &mut f64, value: f64) {
let better = if is_max {
value > *current
} else {
value < *current
};
if current.is_nan() || better {
*current = value;
}
}
impl AggregateUDFImpl for Extremum {
fn name(&self) -> &str {
self.name
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, _arg_types: &[DataType]) -> DfResult<DataType> {
Ok(DataType::Float64)
}
fn accumulator(&self, _acc_args: AccumulatorArgs) -> DfResult<Box<dyn Accumulator>> {
Ok(Box::new(ExtremumAccumulator {
is_max: self.is_max,
value: None,
}))
}
fn state_fields(&self, args: StateFieldsArgs) -> DfResult<Vec<FieldRef>> {
Ok(vec![Arc::new(Field::new(
format!("{}[{}]", args.name, self.name),
DataType::Float64,
true,
))])
}
fn groups_accumulator_supported(&self, _args: AccumulatorArgs) -> bool {
true
}
fn create_groups_accumulator(
&self,
_args: AccumulatorArgs,
) -> DfResult<Box<dyn GroupsAccumulator>> {
Ok(groups_accumulator(self.is_max))
}
}
fn groups_accumulator(is_max: bool) -> Box<dyn GroupsAccumulator> {
Box::new(
PrimitiveGroupsAccumulator::<Float64Type, _>::new(&DataType::Float64, move |cur, v| {
fold(is_max, cur, v)
})
.with_starting_value(f64::NAN),
)
}
#[derive(Debug)]
struct ExtremumAccumulator {
is_max: bool,
value: Option<f64>,
}
impl ExtremumAccumulator {
fn update(&mut self, values: &ArrayRef) {
for v in values.as_primitive::<Float64Type>().iter().flatten() {
match &mut self.value {
Some(current) => fold(self.is_max, current, v),
None => self.value = Some(v),
}
}
}
}
impl Accumulator for ExtremumAccumulator {
fn update_batch(&mut self, values: &[ArrayRef]) -> DfResult<()> {
self.update(&values[0]);
Ok(())
}
fn merge_batch(&mut self, states: &[ArrayRef]) -> DfResult<()> {
self.update(&states[0]);
Ok(())
}
fn state(&mut self) -> DfResult<Vec<ScalarValue>> {
Ok(vec![ScalarValue::Float64(self.value)])
}
fn evaluate(&mut self) -> DfResult<ScalarValue> {
Ok(ScalarValue::Float64(self.value))
}
fn size(&self) -> usize {
size_of_val(self)
}
}
#[cfg(test)]
mod tests {
use datafusion::arrow::array::Float64Array;
use datafusion::logical_expr::EmitTo;
use super::*;
fn groups_result(
is_max: bool,
values: Vec<Option<f64>>,
groups: Vec<usize>,
) -> Vec<Option<f64>> {
let num_groups = groups.iter().max().map_or(0, |g| g + 1);
let mut acc = groups_accumulator(is_max);
let values: ArrayRef = Arc::new(Float64Array::from(values));
acc.update_batch(&[values], &groups, None, num_groups)
.unwrap();
let out = acc.evaluate(EmitTo::All).unwrap();
out.as_primitive::<Float64Type>().iter().collect()
}
fn is_nan(v: Option<f64>) -> bool {
v.is_some_and(f64::is_nan)
}
#[test]
fn nan_is_ignored_unless_the_group_is_all_nan() {
// Group 0 has NaN on both sides of a number; group 1 is all NaN; group 2 has only NULL.
let values = vec![
Some(f64::NAN),
Some(3.0),
Some(f64::NAN),
Some(f64::NAN),
None,
];
let groups = vec![0, 0, 0, 1, 2];
for is_max in [true, false] {
let out = groups_result(is_max, values.clone(), groups.clone());
assert_eq!(out[0], Some(3.0));
assert!(is_nan(out[1]));
assert_eq!(out[2], None);
}
}
#[test]
fn result_does_not_depend_on_input_order() {
let forward = vec![Some(1.0), Some(f64::NAN), Some(4.0), Some(2.0)];
let mut backward = forward.clone();
backward.reverse();
assert_eq!(
groups_result(true, forward.clone(), vec![0; 4]),
vec![Some(4.0)]
);
assert_eq!(
groups_result(true, backward.clone(), vec![0; 4]),
vec![Some(4.0)]
);
assert_eq!(groups_result(false, forward, vec![0; 4]), vec![Some(1.0)]);
assert_eq!(groups_result(false, backward, vec![0; 4]), vec![Some(1.0)]);
}
#[test]
fn merged_partial_states_keep_nan_semantics() {
let mut partial_nan = ExtremumAccumulator {
is_max: true,
value: None,
};
partial_nan.update(&(Arc::new(Float64Array::from(vec![f64::NAN])) as ArrayRef));
let mut partial_num = ExtremumAccumulator {
is_max: true,
value: None,
};
partial_num.update(&(Arc::new(Float64Array::from(vec![2.0, 5.0])) as ArrayRef));
let mut merged = ExtremumAccumulator {
is_max: true,
value: None,
};
for partial in [&mut partial_nan, &mut partial_num] {
let state = partial.state().unwrap()[0].to_array().unwrap();
merged.merge_batch(&[state]).unwrap();
}
assert_eq!(merged.evaluate().unwrap(), ScalarValue::Float64(Some(5.0)));
}
}
@@ -25,6 +25,7 @@ use crate::admin::AdminFunction;
use crate::aggrs::aggr_wrapper::StateMergeHelper;
use crate::aggrs::approximate::ApproximateFunction;
use crate::aggrs::count_hash::CountHash;
use crate::aggrs::extremum::Extremum;
use crate::aggrs::vector::VectorFunction as VectorAggrFunction;
use crate::function::{Function, FunctionRef};
use crate::function_factory::ScalarFunctionFactory;
@@ -260,6 +261,9 @@ pub static FUNCTION_REGISTRY: LazyLock<Arc<FunctionRegistry>> = LazyLock::new(||
// CountHash function
CountHash::register(&function_registry);
// PromQL min/max; registered before the state functions are derived.
Extremum::register(&function_registry);
// state function of supported aggregate functions
StateMergeHelper::register(&function_registry);
@@ -15,13 +15,12 @@
use std::fmt;
use std::sync::Arc;
use common_base::term_token::{CharClass, classify_char};
use datafusion_common::arrow::array::{Array, AsArray, BooleanArray, BooleanBuilder};
use datafusion_common::arrow::compute;
use datafusion_common::arrow::datatypes::DataType;
use datafusion_common::{DataFusionError, ScalarValue};
use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, Signature, Volatility};
use icu_properties::props::Script;
use icu_properties::{CodePointMapData, CodePointMapDataBorrowed};
use memchr::memmem;
use crate::function::Function;
@@ -231,14 +230,6 @@ pub struct MatchesTermFinder {
ends_with_other: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CharClass {
AsciiWord,
Han,
UnicodeWord,
Other,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum TermKind {
AsciiLike,
@@ -246,25 +237,6 @@ enum TermKind {
HanContaining,
}
fn classify_char(c: char) -> CharClass {
if c.is_ascii_alphanumeric() {
CharClass::AsciiWord
} else if is_han(c) {
CharClass::Han
} else if c.is_alphanumeric() {
CharClass::UnicodeWord
} else {
CharClass::Other
}
}
static HAN_SCRIPT_DATA: CodePointMapDataBorrowed<'static, Script> =
CodePointMapData::<Script>::new();
fn is_han(c: char) -> bool {
HAN_SCRIPT_DATA.get(c) == Script::Han
}
fn classify_term(term: &str) -> TermKind {
let mut has_han = false;
let mut has_unicode_word = false;
@@ -508,12 +480,4 @@ mod tests {
assert!(MatchesTermFinder::new("行账号").find("中国农业银行账号"));
assert!(MatchesTermFinder::new("登录").find("登录手机号18888888888的动态key"));
}
#[test]
fn han_detection_uses_script_not_all_cjk() {
assert!(is_han('汉'));
assert!(is_han('\u{30000}'));
assert!(!is_han('あ'));
assert!(!is_han('한'));
}
}
@@ -53,8 +53,10 @@ pub fn binlit_as_veclit(bytes: &[u8]) -> Result<Cow<'_, [f32]>> {
})
} else {
let v = bytes
.chunks_exact(std::mem::size_of::<f32>())
.map(|chunk| f32::from_le_bytes(chunk.try_into().unwrap()))
.as_chunks::<{ std::mem::size_of::<f32>() }>()
.0
.iter()
.map(|chunk| f32::from_le_bytes(*chunk))
.collect::<Vec<f32>>();
Ok(Cow::Owned(v))
}
+9
View File
@@ -41,3 +41,12 @@ cargo nextest run -p common-meta
```
Backend-specific coverage may also need `pg_kvbackend` or `mysql_kvbackend`.
`test_util::etcd_certs_dir` and `test_util::test_certs_dir` resolve fixture
certificates using `CARGO_MANIFEST_DIR` at **run time** (`std::env::var`, not `env!`).
`cargo nextest run` sets it; direct test-binary execution requires setting the
variable to the crate directory. Run this example from the repository root:
```bash
CARGO_MANIFEST_DIR="$PWD/src/common/meta" ./target/debug/deps/<test_binary>
```
+3 -3
View File
@@ -42,7 +42,7 @@ use crate::cache_invalidator::Context;
use crate::ddl::DdlContext;
use crate::ddl::event::flow::{CREATE_FLOW_EVENT_TYPE, CreateFlowEventIntent, FlowDdlEvent};
use crate::ddl::utils::{add_peer_context_if_needed, map_to_procedure_error};
use crate::error::{self, Result, UnexpectedSnafu};
use crate::error::{self, InvalidFlowOptionSnafu, Result, UnexpectedSnafu};
use crate::instruction::{CacheIdent, CreateFlow, DropFlow};
use crate::key::flow::flow_info::{FlowInfoValue, FlowScheduleConfig, FlowStatus};
use crate::key::flow::flow_route::FlowRouteValue;
@@ -558,9 +558,9 @@ pub fn validate_flow_options(flow_task: &CreateFlowTask) -> Result<()> {
| FLOW_EXPERIMENTAL_ENABLE_INCREMENTAL_READ_KEY
| FlowType::FLOW_TYPE_KEY => {}
unknown => {
return UnexpectedSnafu {
return InvalidFlowOptionSnafu {
err_msg: format!(
"Unknown flow option '{unknown}', supported user options: {DEFER_ON_MISSING_SOURCE_KEY}, {FLOW_EXPERIMENTAL_ENABLE_INCREMENTAL_READ_KEY}"
"unknown flow option '{unknown}', supported options: {DEFER_ON_MISSING_SOURCE_KEY}, {FLOW_EXPERIMENTAL_ENABLE_INCREMENTAL_READ_KEY}"
),
}
.fail();
+3 -1
View File
@@ -19,6 +19,8 @@ use std::collections::HashMap;
use async_trait::async_trait;
use common_error::ext::BoxedError;
#[cfg(feature = "enterprise")]
use common_error::ext::ErrorExt;
use common_event_recorder::Event;
use common_procedure::error::{ExternalSnafu, FromJsonSnafu, ToJsonSnafu};
use common_procedure::{
@@ -53,7 +55,7 @@ use crate::rpc::router::{RegionRoute, operating_leader_region_roles};
#[cfg(feature = "enterprise")]
fn ensure_retry_later(err: error::Error) -> error::Error {
if err.is_retry_later() {
if err.retry_hint().is_retryable() {
err
} else {
error::Error::retry_later(err)
+26 -3
View File
@@ -18,6 +18,8 @@ use std::sync::Arc;
use api::v1::flow::CreateRequest;
use common_catalog::consts::{DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME};
use common_error::ext::ErrorExt;
use common_error::status_code::StatusCode;
use common_procedure::{Procedure, Status};
use common_procedure_test::execute_procedure_until_done;
use table::table_name::TableName;
@@ -326,6 +328,26 @@ fn test_validate_flow_options_allows_incremental_read_option() {
validate_flow_options(&task).unwrap();
}
#[test]
fn test_validate_flow_options_unknown_option_matches_invalid_sql_error() {
let mut task = test_create_flow_task(
"my_flow",
vec![],
TableName::new(DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME, "my_sink_table"),
false,
);
task.flow_options
.insert("foo".to_string(), "sensitive-value".to_string());
let err = validate_flow_options(&task).unwrap_err();
assert_eq!(StatusCode::InvalidArguments, err.status_code());
assert_eq!(
"Invalid SQL, error: unknown flow option 'foo', supported options: defer_on_missing_source, experimental_enable_incremental_read",
err.output_msg()
);
assert!(!err.output_msg().contains("sensitive-value"));
}
#[test]
fn test_validate_flow_options_rejects_schedule_and_internal_keys_as_unknown() {
for key in [
@@ -346,9 +368,10 @@ fn test_validate_flow_options_rejects_schedule_and_internal_keys_as_unknown() {
.insert(key.to_string(), "value".to_string());
let err = validate_flow_options(&task).unwrap_err();
assert_eq!(StatusCode::InvalidArguments, err.status_code());
assert!(
err.to_string()
.contains(&format!("Unknown flow option '{key}'")),
.contains(&format!("unknown flow option '{key}'")),
"unexpected error for {key}: {err}"
);
}
@@ -884,10 +907,10 @@ async fn test_create_flow_rejects_unknown_option_in_meta_task() {
let mut procedure = CreateFlowProcedure::new(task, query_ctx, ddl_context);
let err = procedure.on_prepare().await.unwrap_err();
assert_matches!(err, error::Error::Unexpected { .. });
assert_eq!(StatusCode::InvalidArguments, err.status_code());
assert!(
err.to_string()
.contains("Unknown flow option 'unknown_option'")
.contains("unknown flow option 'unknown_option'")
);
}
+56 -4
View File
@@ -17,8 +17,8 @@ use api::v1::region::{
use async_trait::async_trait;
use common_procedure::error::{FromJsonSnafu, ToJsonSnafu};
use common_procedure::{
Context as ProcedureContext, EventContext, EventTrigger, LockKey, Procedure,
Result as ProcedureResult, Status,
Context as ProcedureContext, Error as ProcedureError, EventContext, EventTrigger, LockKey,
Procedure, Result as ProcedureResult, Status,
};
use common_telemetry::tracing_context::TracingContext;
use common_telemetry::warn;
@@ -156,6 +156,7 @@ impl UndropTableProcedure {
}
.await;
if let Err(err) = restore_result {
// Keep this classification aligned with map_restore_error().
let should_cleanup_opened_regions = !err.is_retry_later();
let err = self.map_restore_metadata_error(err);
if should_cleanup_opened_regions
@@ -274,6 +275,19 @@ impl UndropTableProcedure {
Ok(())
}
fn map_restore_error(err: error::Error) -> ProcedureError {
// Restore retries keep opened regions, while other failures close them. Preserve
// this policy even when the underlying metadata error carries a retry hint:
// retrying after cleanup would restore metadata without reopening the regions.
if err.is_retry_later() {
map_to_procedure_error(err)
} else if err.need_clean_poisons() {
ProcedureError::external_and_clean_poisons(err)
} else {
ProcedureError::external(err)
}
}
fn map_restore_metadata_error(&self, err: error::Error) -> error::Error {
match err {
error::Error::TombstoneTargetAlreadyExists { .. } => {
@@ -413,7 +427,12 @@ impl Procedure for UndropTableProcedure {
async fn execute(&mut self, _: &ProcedureContext) -> ProcedureResult<Status> {
match self.data.state {
UndropTableState::Prepare => self.on_prepare().await,
UndropTableState::RestoreMetadata => self.on_restore_metadata().await,
UndropTableState::RestoreMetadata => {
return self
.on_restore_metadata()
.await
.map_err(Self::map_restore_error);
}
UndropTableState::OpenRegions => self.on_open_regions().await,
UndropTableState::InvalidateTableCache => self.on_broadcast().await,
}
@@ -606,7 +625,8 @@ mod tests {
use api::v1::region::region_request;
use async_trait::async_trait;
use common_catalog::consts::{DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME};
use common_error::ext::ErrorExt;
use common_error::ext::{BoxedError, ErrorExt};
use common_error::mock::MockError;
use common_error::status_code::StatusCode;
use store_api::storage::RegionId;
use table::table_name::TableName;
@@ -635,6 +655,38 @@ mod tests {
}
}
#[test]
fn test_restore_metadata_retry_policy() {
let source = error::Error::retry_later(MockError::new(StatusCode::Internal));
let err = Err::<(), _>(BoxedError::new(source))
.context(error::ExternalSnafu)
.unwrap_err();
assert!(err.retry_hint().is_retryable());
assert!(!err.is_retry_later());
let err = UndropTableProcedure::map_restore_error(err);
assert!(!err.is_retry_later());
assert!(!err.need_clean_poisons());
for retryable in [false, true] {
for clean_poisons in [false, true] {
let source = BoxedError::new(MockError::new(StatusCode::Internal));
let err = if retryable {
error::Error::RetryLater {
source,
clean_poisons,
}
} else {
Err::<(), _>(source)
.context(error::AbortProcedureSnafu { clean_poisons })
.unwrap_err()
};
let err = UndropTableProcedure::map_restore_error(err);
assert_eq!(err.is_retry_later(), retryable);
assert_eq!(err.need_clean_poisons(), clean_poisons);
}
}
}
#[test]
fn test_map_restore_metadata_error_without_table_name() {
let context = new_ddl_context(Arc::new(MockDatanodeManager::new(())));
+70 -4
View File
@@ -28,7 +28,7 @@ use api::v1::region::{
region_request,
};
use common_catalog::consts::{METRIC_ENGINE, MITO_ENGINE};
use common_error::ext::BoxedError;
use common_error::ext::{BoxedError, ErrorExt};
use common_procedure::error::Error as ProcedureError;
use common_telemetry::tracing_context::TracingContext;
use common_telemetry::{error, info, warn};
@@ -61,7 +61,7 @@ use crate::wal_provider::RegionWalOptions;
pub fn add_peer_context_if_needed(datanode: Peer) -> impl FnOnce(Error) -> Error {
move |err| {
error!(err; "Failed to operate datanode, peer: {}", datanode);
if !err.is_retry_later() {
if !err.retry_hint().is_retryable() {
return Err::<(), BoxedError>(BoxedError::new(err))
.context(OperateDatanodeSnafu { peer: datanode })
.unwrap_err();
@@ -84,7 +84,7 @@ pub(crate) fn is_metric_engine_logical_table(
/// This function determines whether the error should be retried and if poison cleanup is needed,
/// then maps it to the appropriate procedure error variant.
pub fn map_to_procedure_error(e: Error) -> ProcedureError {
match (e.is_retry_later(), e.need_clean_poisons()) {
match (e.retry_hint().is_retryable(), e.need_clean_poisons()) {
(true, true) => ProcedureError::retry_later_and_clean_poisons(e),
(true, false) => ProcedureError::retry_later(e),
(false, true) => ProcedureError::external_and_clean_poisons(e),
@@ -256,7 +256,7 @@ pub fn handle_multiple_results<T: Debug>(results: Vec<Result<T>>) -> MultipleRes
match result {
Ok(value) => ok_results.push(value),
Err(err) => {
if err.is_retry_later() {
if err.retry_hint().is_retryable() {
retryable_results.push(err);
} else {
non_retryable_results.push(err);
@@ -475,8 +475,74 @@ pub fn extract_column_metadatas(
#[cfg(test)]
mod tests {
use common_error::mock::MockError;
use common_error::status_code::StatusCode;
use super::*;
fn external_error(retryable: bool) -> Error {
let source = MockError::new(StatusCode::Internal);
let source = if retryable {
BoxedError::new(Error::retry_later(source))
} else {
BoxedError::new(source)
};
Err::<(), _>(source)
.context(error::ExternalSnafu)
.unwrap_err()
}
#[test]
fn test_map_to_procedure_error() {
for retryable in [false, true] {
for clean_poisons in [false, true] {
let err = if retryable {
Error::RetryLater {
source: BoxedError::new(external_error(false)),
clean_poisons,
}
} else {
Err::<(), _>(BoxedError::new(external_error(true)))
.context(error::AbortProcedureSnafu { clean_poisons })
.unwrap_err()
};
let err = map_to_procedure_error(err);
assert_eq!(err.is_retry_later(), retryable);
assert_eq!(err.need_clean_poisons(), clean_poisons);
}
let err = map_to_procedure_error(external_error(retryable));
assert_eq!(err.is_retry_later(), retryable);
assert!(!err.need_clean_poisons());
}
}
#[test]
fn test_handle_multiple_results_retry_hint() {
assert!(matches!(
handle_multiple_results(vec![Ok(()), Err(external_error(true))]),
MultipleResults::PartialRetryable(_)
));
assert!(matches!(
handle_multiple_results::<()>(vec![
Err(external_error(true)),
Err(external_error(true))
]),
MultipleResults::AllRetryable(_)
));
assert!(matches!(
handle_multiple_results(vec![
Ok(()),
Err(external_error(true)),
Err(external_error(false))
]),
MultipleResults::PartialNonRetryable(_)
));
assert!(matches!(
handle_multiple_results::<()>(vec![Err(external_error(false))]),
MultipleResults::AllNonRetryable(_)
));
}
#[test]
fn test_get_catalog_and_schema() {
let test_catalog = "my_catalog";
+53 -5
View File
@@ -515,6 +515,13 @@ pub enum Error {
location: Location,
},
#[snafu(display("Invalid SQL, error: {err_msg}"))]
InvalidFlowOption {
err_msg: String,
#[snafu(implicit)]
location: Location,
},
#[snafu(display("Invalid flow request body: {:?}", body))]
InvalidFlowRequestBody {
body: Box<Option<api::v1::flow::flow_request::Body>>,
@@ -1247,6 +1254,7 @@ impl ErrorExt for Error {
ProcedureNotFound { .. }
| InvalidViewInfo { .. }
| InvalidFlowOption { .. }
| PrimaryKeyNotFound { .. }
| EmptyKey { .. }
| AlterLogicalTablesInvalidArguments { .. }
@@ -1338,8 +1346,11 @@ impl ErrorExt for Error {
use Error::*;
match self {
RetryLater { .. }
| GetLatestCacheRetryExceeded { .. }
RetryLater { source, .. } => match source.retry_hint() {
RetryHint::RetryAfterRouteRefresh => RetryHint::RetryAfterRouteRefresh,
RetryHint::Retryable | RetryHint::NonRetryable => RetryHint::Retryable,
},
GetLatestCacheRetryExceeded { .. }
| NoLeader { .. }
| ElectionNoLeader { .. }
| ElectionLeaderLeaseExpired { .. }
@@ -1362,15 +1373,15 @@ impl ErrorExt for Error {
| StartProcedureManager { source, .. }
| StopProcedureManager { source, .. }
| RegisterProcedureLoader { source, .. }
| PutPoison { source, .. }
| ProcedureStateReceiver { source, .. } => source.retry_hint(),
| PutPoison { source, .. } => source.retry_hint(),
External { source, .. }
| ResponseExceededSizeLimit { source, .. }
| OperateDatanode { source, .. }
| AbortProcedure { source, .. }
| RegisterRepartitionProcedureLoader { source, .. }
| CreateRepartitionProcedure { source, .. }
| PersistRepartitionGcRequirement { source, .. } => source.retry_hint(),
// These errors end an operation; their sources cannot make it retryable.
AbortProcedure { .. } | ProcedureStateReceiver { .. } => RetryHint::NonRetryable,
Table { source, .. } => source.retry_hint(),
ConvertAlterTableRequest { source, .. } => source.retry_hint(),
ConvertColumnDef { source, .. } => source.retry_hint(),
@@ -1455,6 +1466,7 @@ mod retry_hint_tests {
use std::sync::Arc;
use common_error::mock::MockError;
use snafu::ResultExt;
use super::*;
@@ -1472,6 +1484,42 @@ mod retry_hint_tests {
assert_eq!(err.retry_hint(), RetryHint::Retryable);
}
#[test]
fn test_leader_retry_hints() {
for (error, expected) in [
(
NoLeaderSnafu { table_id: 1_u32 }.build(),
RetryHint::Retryable,
),
(ElectionNoLeaderSnafu.build(), RetryHint::Retryable),
(
ElectionLeaderLeaseExpiredSnafu.build(),
RetryHint::Retryable,
),
(
ElectionLeaderLeaseChangedSnafu.build(),
RetryHint::Retryable,
),
] {
assert_eq!(error.retry_hint(), expected, "{error:?}");
}
}
#[test]
fn test_procedure_error_retry_hints() {
let source = common_procedure::Error::retry_later(MockError::new(StatusCode::Internal));
let err = Err::<(), _>(source).context(PutPoisonSnafu).unwrap_err();
assert_eq!(err.retry_hint(), RetryHint::Retryable);
let source = common_procedure::Error::retry_later(MockError::new(StatusCode::Internal));
let err = Err::<(), _>(source)
.context(ProcedureStateReceiverSnafu {
procedure_id: ProcedureId::random(),
})
.unwrap_err();
assert_eq!(err.retry_hint(), RetryHint::NonRetryable);
}
#[test]
fn test_get_cache_forwards_retry_hint() {
let source = Arc::new(Error::retry_later(MockError::new(StatusCode::Internal)));
+35 -3
View File
@@ -49,7 +49,7 @@ pub struct InstructionError {
pub message: String,
/// Retry hint serialized as a string.
#[serde(
serialize_with = "RetryHint::serialize_as_str",
serialize_with = "InstructionError::serialize_retry_hint",
deserialize_with = "RetryHint::deserialize_from_str"
)]
pub retry_hint: RetryHint,
@@ -108,6 +108,18 @@ impl InstructionError {
retry_hint: error.retry_hint(),
}
}
/// Keeps instruction replies readable by peers that only know the legacy hints.
fn serialize_retry_hint<S>(hint: &RetryHint, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let hint = match hint {
RetryHint::RetryAfterRouteRefresh => RetryHint::Retryable,
hint => *hint,
};
RetryHint::serialize_as_str(&hint, serializer)
}
}
impl Display for InstructionError {
@@ -732,12 +744,12 @@ impl PackedRegionFileRefs {
}
let mut refs = HashSet::new();
for bytes in files.chunks_exact(16) {
for bytes in files.as_chunks::<16>().0 {
let mut id = [0; 16];
id.copy_from_slice(bytes);
refs.insert(FileRef::new(region, FileId::from_bytes(id), None));
}
for bytes in indexed.chunks_exact(24) {
for bytes in indexed.as_chunks::<24>().0 {
let mut id = [0; 16];
id.copy_from_slice(&bytes[..16]);
let mut version = [0; 8];
@@ -1450,6 +1462,26 @@ mod tests {
);
}
#[test]
fn test_instruction_error_route_refresh_compatibility() {
let error = InstructionError::new(
StatusCode::RegionNotFound,
"region not found",
RetryHint::RetryAfterRouteRefresh,
);
let serialized = serde_json::to_string(&error).unwrap();
assert_eq!(
r#"{"code":4005,"message":"region not found","retry_hint":"retryable"}"#,
serialized
);
let decoded: InstructionError = serde_json::from_str(&serialized).unwrap();
assert_eq!(decoded.code, error.code);
assert_eq!(decoded.message, error.message);
assert_eq!(decoded.retry_hint, RetryHint::Retryable);
assert_eq!(error.retry_hint, RetryHint::RetryAfterRouteRefresh);
assert_eq!(error.retry_hint.as_str(), "retry_after_route_refresh");
}
#[test]
fn test_instruction_error_from_error() {
let error = MockError::new(StatusCode::RegionNotFound);
+1 -1
View File
@@ -82,7 +82,7 @@ impl KvBackend for ChrootKvBackend {
async fn batch_put(&self, mut req: BatchPutRequest) -> Result<BatchPutResponse, Self::Error> {
for kv in req.kvs.iter_mut() {
kv.key = self.key_prepend_root(kv.key.drain(..).collect());
kv.key = self.key_prepend_root(std::mem::take(&mut kv.key));
}
let mut res = self.inner.batch_put(req).await?;
res.prev_kvs = res
+11 -1
View File
@@ -28,12 +28,22 @@ use crate::peer::Peer;
#[async_trait::async_trait]
pub trait Datanode: Send + Sync {
/// Handles DML, and DDL requests.
async fn handle(&self, request: RegionRequest) -> Result<RegionResponse>;
async fn handle_request(&self, request: Arc<RegionRequest>) -> Result<RegionResponse>;
/// Handles query requests
async fn handle_query(&self, request: QueryRequest) -> Result<SendableRecordBatchStream>;
}
impl dyn Datanode + '_ {
/// Handles an owned or shared request without cloning its payload.
pub async fn handle<R>(&self, request: R) -> Result<RegionResponse>
where
R: Into<Arc<RegionRequest>> + Send,
{
self.handle_request(request.into()).await
}
}
pub type DatanodeRef = Arc<dyn Datanode>;
/// The trait for handling requests to flownode
+3
View File
@@ -22,6 +22,9 @@ use crate::cluster::NodeInfo;
use crate::error::Error;
use crate::{DatanodeId, FlownodeId};
/// Shared reference to a [`Peer`].
pub type PeerRef = Arc<Peer>;
/// [`PeerResolver`] provides methods to look up peer information by node ID.
///
/// This trait allows resolving both datanode and flownode peers, regardless of their current activity status.
@@ -16,6 +16,7 @@ use std::any::Any;
use std::fmt::Debug;
use std::time::Instant;
use common_error::ext::ErrorExt;
use common_procedure::error::FromJsonSnafu;
use common_procedure::{
Context as ProcedureContext, Error as ProcedureError, EventContext, EventTrigger, LockKey,
@@ -191,7 +192,7 @@ impl Procedure for ReconcileCatalogProcedure {
Ok(status)
}
Err(e) => {
if e.is_retry_later() {
if e.retry_hint().is_retryable() {
metrics::METRIC_META_RECONCILIATION_PROCEDURE_ERROR
.with_label_values(&[procedure_name, step, metrics::ERROR_TYPE_RETRYABLE])
.inc();
@@ -23,6 +23,7 @@ use std::fmt::Debug;
use std::time::Instant;
use async_trait::async_trait;
use common_error::ext::ErrorExt;
use common_procedure::error::{FromJsonSnafu, ToJsonSnafu};
use common_procedure::{
Context as ProcedureContext, Error as ProcedureError, EventContext, EventTrigger, LockKey,
@@ -231,7 +232,7 @@ impl Procedure for ReconcileDatabaseProcedure {
Ok(status)
}
Err(e) => {
if e.is_retry_later() {
if e.retry_hint().is_retryable() {
metrics::METRIC_META_RECONCILIATION_PROCEDURE_ERROR
.with_label_values(&[procedure_name, step, metrics::ERROR_TYPE_RETRYABLE])
.inc();
@@ -22,6 +22,7 @@ use std::any::Any;
use std::fmt::Debug;
use async_trait::async_trait;
use common_error::ext::ErrorExt;
use common_procedure::error::{FromJsonSnafu, ToJsonSnafu};
use common_procedure::{
Context as ProcedureContext, Error as ProcedureError, EventContext, EventTrigger, LockKey,
@@ -207,7 +208,7 @@ impl Procedure for ReconcileLogicalTablesProcedure {
Ok(status)
}
Err(e) => {
if e.is_retry_later() {
if e.retry_hint().is_retryable() {
metrics::METRIC_META_RECONCILIATION_PROCEDURE_ERROR
.with_label_values(&[procedure_name, step, metrics::ERROR_TYPE_RETRYABLE])
.inc();
@@ -20,6 +20,7 @@ pub(crate) mod update_table_info;
use std::fmt::Debug;
use common_error::ext::ErrorExt;
use common_procedure::error::{FromJsonSnafu, ToJsonSnafu};
use common_procedure::{
Context as ProcedureContext, Error as ProcedureError, EventContext, EventTrigger, LockKey,
@@ -318,7 +319,7 @@ impl Procedure for ReconcileTableProcedure {
Ok(status)
}
Err(e) => {
if e.is_retry_later() {
if e.retry_hint().is_retryable() {
metrics::METRIC_META_RECONCILIATION_PROCEDURE_ERROR
.with_label_values(&[procedure_name, step, metrics::ERROR_TYPE_RETRYABLE])
.inc();
+33 -2
View File
@@ -48,7 +48,7 @@ pub fn region_distribution(region_routes: &[RegionRoute]) -> RegionDistribution
.add_follower_region(region_number);
}
}
for (_, region_role_set) in regions_id_map.iter_mut() {
for region_role_set in regions_id_map.values_mut() {
// Sort the regions in ascending order.
region_role_set.sort()
}
@@ -114,15 +114,25 @@ pub fn convert_to_region_leader_map(region_routes: &[RegionRoute]) -> HashMap<Re
.collect::<HashMap<_, _>>()
}
/// Returns the leader of the first route matching the region number.
pub fn find_region_leader(
region_routes: &[RegionRoute],
region_number: RegionNumber,
) -> Option<Peer> {
find_region_leader_ref(region_routes, region_number).cloned()
}
/// Borrows the leader of the first route matching the region number.
///
/// Returns `None` if no route matches or the first matching route has no leader.
pub fn find_region_leader_ref(
region_routes: &[RegionRoute],
region_number: RegionNumber,
) -> Option<&Peer> {
region_routes
.iter()
.find(|x| x.region.id.region_number() == region_number)
.and_then(|r| r.leader_peer.as_ref())
.cloned()
}
/// Returns the region numbers of the leader regions on the target datanode.
@@ -442,6 +452,27 @@ mod tests {
}
}
#[test]
fn test_find_region_leader() {
let mut routes = vec![
new_test_region_route(RegionId::new(1, 1)),
new_test_region_route(RegionId::new(1, 1)),
];
routes[1].leader_peer = Some(Peer::new(2, "a2"));
assert_eq!(
find_region_leader_ref(&routes, 1),
routes[0].leader_peer.as_ref()
);
assert_eq!(find_region_leader(&routes, 1), routes[0].leader_peer);
assert_eq!(find_region_leader_ref(&routes, 2), None);
assert_eq!(find_region_leader(&routes, 2), None);
routes[0].leader_peer = None;
assert_eq!(find_region_leader_ref(&routes, 1), None);
assert_eq!(find_region_leader(&routes, 1), None);
}
#[test]
fn test_leader_is_downgraded() {
let mut region_route = RegionRoute {
+3 -3
View File
@@ -199,7 +199,7 @@ impl RangeResponse {
}
pub fn take_kvs(&mut self) -> Vec<KeyValue> {
self.kvs.drain(..).collect()
std::mem::take(&mut self.kvs)
}
}
@@ -451,7 +451,7 @@ impl BatchPutResponse {
}
pub fn take_prev_kvs(&mut self) -> Vec<KeyValue> {
self.prev_kvs.drain(..).collect()
std::mem::take(&mut self.prev_kvs)
}
}
@@ -780,7 +780,7 @@ impl DeleteRangeResponse {
}
pub fn take_prev_kvs(&mut self) -> Vec<KeyValue> {
self.prev_kvs.drain(..).collect()
std::mem::take(&mut self.prev_kvs)
}
}
+2 -2
View File
@@ -188,7 +188,7 @@ impl Inner {
}
error::NextSequenceSnafu {
err_msg: format!("{}.next()", &self.name),
err_msg: format!("{}.next()", self.name),
}
.fail()
}
@@ -264,7 +264,7 @@ impl Inner {
}
error::NextSequenceSnafu {
err_msg: format!("{}.next_range()", &self.name),
err_msg: format!("{}.next_range()", self.name),
}
.fail()
}
+6 -5
View File
@@ -111,7 +111,8 @@ struct MockNode<T> {
#[async_trait::async_trait]
impl<T: MockDatanodeHandler> Datanode for MockNode<T> {
async fn handle(&self, request: RegionRequest) -> Result<RegionResponse> {
async fn handle_request(&self, request: Arc<RegionRequest>) -> Result<RegionResponse> {
let request = Arc::try_unwrap(request).expect("mock request must be exclusively owned");
self.handler.handle(&self.peer, request).await
}
@@ -322,8 +323,8 @@ macro_rules! maybe_skip_etcd_tls_integration_test {
/// Returns the directory of the etcd TLS certs.
pub fn etcd_certs_dir() -> PathBuf {
let project_path = env!("CARGO_MANIFEST_DIR");
let project_path = PathBuf::from(project_path);
let project_path =
PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"));
let base = project_path.ancestors().nth(3).unwrap();
base.join("tests-integration")
.join("fixtures")
@@ -332,8 +333,8 @@ pub fn etcd_certs_dir() -> PathBuf {
/// Returns the directory of the test certs.
pub fn test_certs_dir() -> PathBuf {
let project_path = env!("CARGO_MANIFEST_DIR");
let project_path = PathBuf::from(project_path);
let project_path =
PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"));
let base = project_path.ancestors().nth(3).unwrap();
base.join("tests-integration")
.join("fixtures")
+20 -1
View File
@@ -306,7 +306,8 @@ impl ErrorExt for Error {
| Error::DeletePoison { source, .. }
| Error::GetPoison { source, .. }
| Error::CheckStatus { source, .. } => source.retry_hint(),
Error::ProcedureExec { source, .. } => source.retry_hint(),
// The watcher reports this only after the child has failed or been poisoned.
Error::ProcedureExec { .. } => RetryHint::NonRetryable,
Error::StartRemoveOutdatedMetaTask { source, .. }
| Error::StopRemoveOutdatedMetaTask { source, .. } => source.retry_hint(),
Error::DeleteState { error, .. } => retry_hint_from_opendal_error(error),
@@ -379,8 +380,11 @@ mod tests {
use std::sync::Arc;
use common_error::mock::MockError;
use tokio::sync::watch;
use super::*;
use crate::procedure::ProcedureState;
use crate::watcher;
#[test]
fn test_retry_later_hint_is_retryable() {
@@ -397,6 +401,21 @@ mod tests {
assert_eq!(err.retry_hint(), RetryHint::Retryable);
}
#[tokio::test]
async fn test_finished_procedure_hint_is_non_retryable() {
let source = Arc::new(Error::retry_later(MockError::new(StatusCode::Internal)));
for state in [
ProcedureState::failed(source.clone()),
ProcedureState::poisoned(Default::default(), source),
] {
let (sender, mut receiver) = watch::channel(ProcedureState::Running);
sender.send(state).unwrap();
let err = watcher::wait(&mut receiver).await.unwrap_err();
assert_eq!(err.retry_hint(), RetryHint::NonRetryable);
}
}
#[test]
fn test_retry_exceeded_hint_is_non_retryable() {
let source = Arc::new(Error::retry_later(MockError::new(StatusCode::Internal)));
+1 -1
View File
@@ -95,7 +95,7 @@ impl DisplayAs for StreamScanAdapter {
f,
"StreamScanAdapter: [<SendableRecordBatchStream>], schema: ["
)?;
write!(f, "{:?}", &self.arrow_schema)?;
write!(f, "{:?}", self.arrow_schema)?;
write!(f, "]")
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,944 @@
// 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.
//! Predicate recognition, execution, and precompiled regular expressions.
use datafusion::logical_expr::{Expr, Operator};
use datafusion_common::ScalarValue;
use datafusion_common::arrow::array::{ArrayRef, Datum, Scalar};
use datafusion_common::arrow::buffer::BooleanBuffer;
use datafusion_common::arrow::compute::kernels::cmp;
use datafusion_common::cast::as_string_array;
use datatypes::arrow::array::{
Array, ArrayAccessor, ArrayData, BooleanArray, BooleanBufferBuilder, DictionaryArray,
StringArrayType,
};
use datatypes::arrow::datatypes::{DataType, UInt32Type};
use datatypes::arrow::error::ArrowError;
use datatypes::compute::or_kleene;
use regex::Regex;
use snafu::ResultExt;
use crate::error::{ArrowComputeSnafu, Result, UnsupportedOperationSnafu};
#[derive(Debug, Clone)]
pub(crate) enum SimplePredicate {
Comparison {
literal: Scalar<ArrayRef>,
op: Operator,
regex: Option<Regex>,
regex_negative: bool,
},
OrEqChain {
literals: Vec<Scalar<ArrayRef>>,
},
IsNull {
negated: bool,
},
Constant(Option<bool>),
And(Box<Self>, Box<Self>),
Or(Box<Self>, Box<Self>),
}
impl SimplePredicate {
pub(crate) fn try_new(expr: &Expr, column_type: Option<&DataType>) -> Option<Self> {
match expr {
Expr::BinaryExpr(binary) if matches!(binary.op, Operator::And | Operator::Or) => {
let left = Self::try_new(&binary.left, column_type)?;
let right = Self::try_new(&binary.right, column_type)?;
if binary.op == Operator::And {
return Some(Self::And(Box::new(left), Box::new(right)));
}
// Preserve the encoded-key IN-list fast path for pure equality disjunctions.
if let (Some(mut lhs), Some(rhs)) =
(left.equality_literals(), right.equality_literals())
{
lhs.extend(rhs);
return Some(Self::OrEqChain { literals: lhs });
}
Some(Self::Or(Box::new(left), Box::new(right)))
}
Expr::BinaryExpr(binary) => {
let mut op = binary.op;
if !matches!(
op,
Operator::Eq
| Operator::NotEq
| Operator::Lt
| Operator::LtEq
| Operator::Gt
| Operator::GtEq
| Operator::RegexMatch
| Operator::RegexIMatch
| Operator::RegexNotMatch
| Operator::RegexNotIMatch
) {
return None;
}
let literal = if let Expr::Literal(literal, _) = &*binary.right {
Self::check_column(&binary.left, column_type)?;
literal
} else if let Expr::Literal(literal, _) = &*binary.left {
Self::check_column(&binary.right, column_type)?;
op = op.swap()?;
literal
} else {
return None;
};
let (regex, regex_negative) = Self::maybe_build_regex(op, literal).ok()?;
Some(Self::Comparison {
literal: literal.to_scalar().ok()?,
op,
regex,
regex_negative,
})
}
Expr::IsNull(column) | Expr::IsNotNull(column) => {
Self::check_column(column, column_type)?;
Some(Self::IsNull {
negated: matches!(expr, Expr::IsNotNull(_)),
})
}
Expr::Literal(ScalarValue::Boolean(value), _) => Some(Self::Constant(*value)),
_ => None,
}
}
fn check_column(expr: &Expr, column_type: Option<&DataType>) -> Option<()> {
match expr {
Expr::Column(_) => Some(()),
Expr::Cast(cast) if cast.field.data_type() == &DataType::Utf8 => {
let Expr::Column(_) = &*cast.expr else {
return None;
};
match column_type? {
DataType::Utf8 => Some(()),
DataType::Dictionary(key, value)
if **key == DataType::UInt32 && **value == DataType::Utf8 =>
{
Some(())
}
_ => None,
}
}
_ => None,
}
}
fn equality_literals(&self) -> Option<Vec<Scalar<ArrayRef>>> {
match self {
Self::Comparison {
literal,
op: Operator::Eq,
..
} => Some(vec![literal.clone()]),
Self::OrEqChain { literals } => Some(literals.clone()),
_ => None,
}
}
pub(crate) fn evaluate(&self, input: &impl Datum, len: usize) -> Result<BooleanArray> {
match self {
Self::Comparison {
literal,
op,
regex,
regex_negative,
} => match op {
Operator::Eq => cmp::eq(input, literal),
Operator::NotEq => cmp::neq(input, literal),
Operator::Lt => cmp::lt(input, literal),
Operator::LtEq => cmp::lt_eq(input, literal),
Operator::Gt => cmp::gt(input, literal),
Operator::GtEq => cmp::gt_eq(input, literal),
Operator::RegexMatch
| Operator::RegexIMatch
| Operator::RegexNotMatch
| Operator::RegexNotIMatch => {
Self::regex_match(input, regex.as_ref(), *regex_negative)
}
_ => {
return UnsupportedOperationSnafu {
reason: format!("{op:?}"),
}
.fail();
}
}
.context(ArrowComputeSnafu),
Self::OrEqChain { literals } => {
let mut result = BooleanArray::from(vec![false; len]);
for literal in literals {
let rhs = cmp::eq(input, literal).context(ArrowComputeSnafu)?;
result = or_kleene(&result, &rhs).context(ArrowComputeSnafu)?;
}
Ok(result)
}
Self::IsNull { negated } => {
let values = match input.get().0.logical_nulls() {
Some(nulls) if *negated => nulls.inner().clone(),
Some(nulls) => !nulls.inner(),
None if *negated => BooleanBuffer::new_set(len),
None => BooleanBuffer::new_unset(len),
};
Ok(BooleanArray::new(values, None))
}
Self::Constant(value) => Ok(BooleanArray::from(vec![*value; len])),
Self::And(left, right) => datatypes::compute::and_kleene(
&left.evaluate(input, len)?,
&right.evaluate(input, len)?,
)
.context(ArrowComputeSnafu),
Self::Or(left, right) => {
or_kleene(&left.evaluate(input, len)?, &right.evaluate(input, len)?)
.context(ArrowComputeSnafu)
}
}
}
/// Builds a regex pattern from a scalar value and operator.
/// Returns the `(regex, negative)` and if successful.
///
/// Returns `Err` if
/// - the value is not a string
/// - the regex pattern is invalid
///
/// The regex is `None` if
/// - the operator is not a regex operator
/// - the pattern is empty
fn maybe_build_regex(
operator: Operator,
value: &ScalarValue,
) -> Result<(Option<Regex>, bool), ArrowError> {
let (ignore_case, negative) = match operator {
Operator::RegexMatch => (false, false),
Operator::RegexIMatch => (true, false),
Operator::RegexNotMatch => (false, true),
Operator::RegexNotIMatch => (true, true),
_ => return Ok((None, false)),
};
let flag = if ignore_case { Some("i") } else { None };
let regex = value
.try_as_str()
.ok_or_else(|| ArrowError::CastError(format!("Cannot cast {:?} to str", value)))?
.ok_or_else(|| ArrowError::CastError("Regex should not be null".to_string()))?;
let pattern = match flag {
Some(flag) => format!("(?{flag}){regex}"),
None => regex.to_string(),
};
if pattern.is_empty() {
Ok((None, negative))
} else {
Regex::new(pattern.as_str())
.map_err(|e| {
ArrowError::ComputeError(format!("Regular expression did not compile: {e:?}"))
})
.map(|regex| (Some(regex), negative))
}
}
fn regex_match(
input: &impl Datum,
regex: Option<&Regex>,
negative: bool,
) -> std::result::Result<BooleanArray, ArrowError> {
let array = input.get().0;
// Try to cast to StringArray first
if let Ok(string_array) = as_string_array(array) {
let mut result = regexp_is_match_scalar(string_array, regex)?;
if negative {
result = datatypes::compute::not(&result)?;
}
return Ok(result);
}
// Try to cast to StringDictionaryArray
if let Some(dict_array) = array.as_any().downcast_ref::<DictionaryArray<UInt32Type>>() {
let mut result = regexp_is_match_dictionary(dict_array, regex)?;
if negative {
result = datatypes::compute::not(&result)?;
}
return Ok(result);
}
Err(ArrowError::CastError(format!(
"Cannot cast {:?} to StringArray or StringDictionaryArray",
array.data_type()
)))
}
}
/// The same as arrow [regexp_is_match_scalar()](datatypes::compute::kernels::regexp::regexp_is_match_scalar())
/// with pre-compiled regex.
/// See <https://github.com/apache/arrow-rs/blob/54.2.0/arrow-string/src/regexp.rs#L204-L246> for the implementation details.
pub fn regexp_is_match_scalar<'a, S>(
array: &'a S,
regex: Option<&Regex>,
) -> Result<BooleanArray, ArrowError>
where
&'a S: StringArrayType<'a>,
{
let null_bit_buffer = array.nulls().map(|x| x.inner().sliced());
let mut result = BooleanBufferBuilder::new(array.len());
if let Some(re) = regex {
for i in 0..array.len() {
let value = array.value(i);
result.append(re.is_match(value));
}
} else {
result.append_n(array.len(), true);
}
let buffer = result.into();
let data = unsafe {
ArrayData::new_unchecked(
DataType::Boolean,
array.len(),
None,
null_bit_buffer,
0,
vec![buffer],
vec![],
)
};
Ok(BooleanArray::from(data))
}
/// Similar to [regexp_is_match_scalar] but for StringDictionaryArray.
/// Iterates through dictionary keys to get string values and applies regex matching.
pub fn regexp_is_match_dictionary(
dict_array: &DictionaryArray<UInt32Type>,
regex: Option<&Regex>,
) -> Result<BooleanArray, ArrowError> {
// Get the string values from the dictionary
let string_values = dict_array
.values()
.as_any()
.downcast_ref::<datatypes::arrow::array::StringArray>()
.ok_or_else(|| {
ArrowError::CastError("Dictionary values must be StringArray".to_string())
})?;
// Dictionary logical nulls include both null keys and keys whose dictionary value is null.
let logical_nulls = dict_array.logical_nulls();
let null_bit_buffer = logical_nulls.as_ref().map(|x| x.inner().sliced());
let mut result = BooleanBufferBuilder::new(dict_array.len());
if let Some(re) = regex {
let keys = dict_array.keys().values();
for i in 0..dict_array.len() {
if logical_nulls.as_ref().is_some_and(|nulls| nulls.is_null(i)) {
result.append(false);
} else {
let key = keys[i] as usize;
let string_value = string_values.value(key);
result.append(re.is_match(string_value));
}
}
} else {
result.append_n(dict_array.len(), true);
}
let buffer = result.into();
let data = unsafe {
ArrayData::new_unchecked(
DataType::Boolean,
dict_array.len(),
None,
null_bit_buffer,
0,
vec![buffer],
vec![],
)
};
Ok(BooleanArray::from(data))
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use datafusion::execution::context::ExecutionProps;
use datafusion::logical_expr::physical_planning_context::PhysicalPlanningContext;
use datafusion::logical_expr::{BinaryExpr, Expr, Literal, Operator, col, lit};
use datafusion::physical_expr::create_physical_expr;
use datafusion_common::arrow::array::ArrayRef;
use datafusion_common::arrow::buffer::BooleanBuffer;
use datafusion_common::{Column, DFSchema, ScalarValue};
use datatypes::arrow::array::{Array, DictionaryArray, RecordBatch};
use datatypes::arrow::datatypes::{DataType, Field, Schema, TimeUnit, UInt32Type};
use datatypes::value::Value;
use regex::Regex;
use crate::filter::predicate::{SimplePredicate, regexp_is_match_dictionary};
use crate::filter::{SimpleFilterEvaluator, batch_filter};
#[test]
fn unsupported_filter_op() {
// `+` is not supported
let expr = Expr::BinaryExpr(BinaryExpr {
left: Box::new(Expr::Column(Column::from_name("foo"))),
op: Operator::Plus,
right: Box::new(1.lit()),
});
assert!(SimpleFilterEvaluator::try_new(&expr).is_none());
// two literal is not supported
let expr = Expr::BinaryExpr(BinaryExpr {
left: Box::new(1.lit()),
op: Operator::Eq,
right: Box::new(1.lit()),
});
assert!(SimpleFilterEvaluator::try_new(&expr).is_none());
// two column is not supported
let expr = Expr::BinaryExpr(BinaryExpr {
left: Box::new(Expr::Column(Column::from_name("foo"))),
op: Operator::Eq,
right: Box::new(Expr::Column(Column::from_name("bar"))),
});
assert!(SimpleFilterEvaluator::try_new(&expr).is_none());
// compound expr is not supported
let expr = Expr::BinaryExpr(BinaryExpr {
left: Box::new(Expr::BinaryExpr(BinaryExpr {
left: Box::new(Expr::Column(Column::from_name("foo"))),
op: Operator::Eq,
right: Box::new(1.lit()),
})),
op: Operator::Eq,
right: Box::new(1.lit()),
});
assert!(SimpleFilterEvaluator::try_new(&expr).is_none());
for negated in [false, true] {
let membership = col("label").in_list(vec![lit(""), lit("lo")], negated);
assert!(SimpleFilterEvaluator::try_new(&membership).is_none());
assert!(
SimpleFilterEvaluator::try_new(&col("label").is_null().or(membership)).is_none()
);
}
for expr in [
col("other").is_null().or(col("label").not_eq(lit("lo"))),
col("a.label")
.is_null()
.or(col("b.label").not_eq(lit("lo"))),
col("label").is_null().or(col("label").eq(col("other"))),
col("label")
.is_null()
.or(Expr::Cast(datafusion::logical_expr::Cast::new(
Box::new(col("label")),
DataType::Int64,
))
.eq(lit("lo"))),
] {
assert!(SimpleFilterEvaluator::try_new(&expr).is_none(), "{expr}");
}
}
#[test]
fn string_cast_requires_matching_column_type() {
let cast = Expr::Cast(datafusion::logical_expr::Cast::new(
Box::new(col("label")),
DataType::Utf8,
));
let expr = col("label").is_null().or(cast.not_eq(lit("lo")));
for data_type in [
DataType::Int64,
DataType::Timestamp(TimeUnit::Millisecond, None),
DataType::Binary,
] {
assert!(
SimpleFilterEvaluator::try_new_with_column_type(&expr, &|_| Some(
data_type.clone()
))
.is_none()
);
}
assert!(SimpleFilterEvaluator::try_new(&expr).is_none());
}
#[tokio::test]
async fn single_column_predicates_match_datafusion() {
use datafusion::prelude::SessionContext;
use datafusion_common::tree_node::{TreeNode, TreeNodeRecursion};
use datatypes::arrow::array::{Int32Array, StringArray, UInt32Array};
let values = vec![
None,
Some(""),
Some("lo"),
Some("eth0"),
Some("a\nb"),
Some("服务"),
None,
];
let strings: ArrayRef = Arc::new(StringArray::from(values));
let dictionary: ArrayRef = Arc::new(DictionaryArray::<UInt32Type>::new(
UInt32Array::from(vec![
None,
Some(0),
Some(1),
Some(2),
Some(3),
Some(4),
Some(5),
]),
Arc::new(StringArray::from(vec![
Some(""),
Some("lo"),
Some("eth0"),
Some("a\nb"),
Some("服务"),
None,
])),
));
let null = || lit(ScalarValue::Utf8(None));
let mut predicates = vec![
col("label").is_null(),
col("label").is_not_null(),
col("label")
.is_null()
.or(col("label").eq(lit("")))
.and(col("label").is_not_null()),
col("label")
.not_eq(lit("lo"))
.and(col("label").not_eq(lit("eth0"))),
col("label")
.eq(lit("lo"))
.or(col("label").eq(lit("eth0")))
.or(col("label").eq(lit(""))),
col("label").eq(null()).or(col("label").eq(lit("eth0"))),
col("label").gt(lit("a")).and(col("label").lt(lit("z"))),
lit("lo").not_eq(col("label")),
];
for (op, literal) in [
(Operator::Eq, ""),
(Operator::NotEq, "lo"),
(Operator::RegexNotMatch, "^(?:lo|vfat)$"),
(Operator::RegexMatch, "^(?:|eth0)$"),
] {
let comparison = Expr::BinaryExpr(BinaryExpr::new(
Box::new(col("label")),
op,
Box::new(lit(literal)),
));
predicates.extend([
col("label").is_null().or(comparison.clone()),
comparison.or(col("label").is_null()),
]);
}
for input in [strings, dictionary] {
let ctx = SessionContext::new();
let schema = Arc::new(Schema::new(vec![
Field::new("label", input.data_type().clone(), true),
Field::new("id", DataType::Int32, false),
]));
ctx.register_batch(
"labels",
RecordBatch::try_new(
schema,
vec![
input.clone(),
Arc::new(Int32Array::from_iter_values(0..input.len() as i32)),
],
)
.unwrap(),
)
.unwrap();
for expr in &predicates {
let df = ctx
.table("labels")
.await
.unwrap()
.filter(expr.clone())
.unwrap();
let plan = df.clone().into_optimized_plan().unwrap();
let expected = df
.collect()
.await
.unwrap()
.iter()
.flat_map(|batch| {
batch
.column_by_name("id")
.unwrap()
.as_any()
.downcast_ref::<Int32Array>()
.unwrap()
.values()
.to_vec()
})
.collect::<Vec<_>>();
let evaluate = |expr: &Expr| {
let evaluator =
SimpleFilterEvaluator::try_new_with_column_type(expr, &|name| {
(name == "label").then(|| input.data_type().clone())
})
.unwrap_or_else(|| panic!("unsupported: {expr}"));
let mask = evaluator.evaluate_array(&input).unwrap();
for (i, matched) in mask.iter().enumerate() {
let scalar = ScalarValue::try_from_array(&input, i).unwrap();
assert_eq!(
evaluator.evaluate_scalar(&scalar).unwrap(),
matched,
"{expr}, row={i}"
);
}
mask
};
let selected = |mask: BooleanBuffer| {
mask.iter()
.enumerate()
.filter_map(|(i, matched)| matched.then_some(i as i32))
.collect::<Vec<_>>()
};
assert_eq!(selected(evaluate(expr)), expected, "{expr}");
let mut filters = Vec::new();
plan.apply(|plan| {
if let datafusion::logical_expr::LogicalPlan::Filter(filter) = plan {
filters.push(filter.predicate.clone());
}
Ok(TreeNodeRecursion::Continue)
})
.unwrap();
if !filters.is_empty() {
let mut mask = BooleanBuffer::new_set(input.len());
for filter in filters {
mask = &mask & &evaluate(&filter);
}
assert_eq!(selected(mask), expected, "optimized: {plan}");
}
}
}
}
#[test]
fn supported_filter_op() {
// equal
let expr = Expr::BinaryExpr(BinaryExpr {
left: Box::new(Expr::Column(Column::from_name("foo"))),
op: Operator::Eq,
right: Box::new(1.lit()),
});
let _ = SimpleFilterEvaluator::try_new(&expr).unwrap();
// swap operands
let expr = Expr::BinaryExpr(BinaryExpr {
left: Box::new(1.lit()),
op: Operator::Lt,
right: Box::new(Expr::Column(Column::from_name("foo"))),
});
let evaluator = SimpleFilterEvaluator::try_new(&expr).unwrap();
assert!(evaluator.is_gt());
assert_eq!(evaluator.column_name, "foo".to_string());
}
#[test]
fn run_on_array() {
let expr = Expr::BinaryExpr(BinaryExpr {
left: Box::new(Expr::Column(Column::from_name("foo"))),
op: Operator::Eq,
right: Box::new(1i64.lit()),
});
let evaluator = SimpleFilterEvaluator::try_new(&expr).unwrap();
let input_1 = Arc::new(datatypes::arrow::array::Int64Array::from(vec![1, 2, 3])) as _;
let result = evaluator.evaluate_array(&input_1).unwrap();
assert_eq!(result, BooleanBuffer::from(vec![true, false, false]));
let input_2 = Arc::new(datatypes::arrow::array::Int64Array::from(vec![1, 1, 1])) as _;
let result = evaluator.evaluate_array(&input_2).unwrap();
assert_eq!(result, BooleanBuffer::from(vec![true, true, true]));
let input_3 = Arc::new(datatypes::arrow::array::Int64Array::new_null(0)) as _;
let result = evaluator.evaluate_array(&input_3).unwrap();
assert_eq!(result, BooleanBuffer::from(vec![]));
}
#[test]
fn run_on_scalar() {
let expr = Expr::BinaryExpr(BinaryExpr {
left: Box::new(Expr::Column(Column::from_name("foo"))),
op: Operator::Lt,
right: Box::new(1i64.lit()),
});
let evaluator = SimpleFilterEvaluator::try_new(&expr).unwrap();
let input_1 = ScalarValue::Int64(Some(1));
let result = evaluator.evaluate_scalar(&input_1).unwrap();
assert!(!result);
let input_2 = ScalarValue::Int64(Some(0));
let result = evaluator.evaluate_scalar(&input_2).unwrap();
assert!(result);
let input_3 = ScalarValue::Int64(None);
let result = evaluator.evaluate_scalar(&input_3).unwrap();
assert!(!result);
}
#[test]
fn test_complex_filter_expression() {
// Create an expression tree for: col = 'B' OR col = 'C' OR col = 'D'
let col_eq_b = col("col").eq(lit("B"));
let col_eq_c = col("col").eq(lit("C"));
let col_eq_d = col("col").eq(lit("D"));
// Build the OR chain
let col_or_expr = col_eq_b.or(col_eq_c).or(col_eq_d);
// Check that SimpleFilterEvaluator can handle OR chain
let or_evaluator = SimpleFilterEvaluator::try_new(&col_or_expr).unwrap();
assert_eq!(or_evaluator.column_name, "col");
assert!(or_evaluator.is_or_eq_chain());
assert_eq!(
or_evaluator.literal_list_values().unwrap(),
vec![Value::from("B"), Value::from("C"), Value::from("D")]
);
// Create a schema and batch for testing
let schema = Schema::new(vec![Field::new("col", DataType::Utf8, false)]);
let df_schema = DFSchema::try_from(schema.clone()).unwrap();
let props = ExecutionProps::new();
let physical_expr = create_physical_expr(
&col_or_expr,
&df_schema,
&props,
&PhysicalPlanningContext::default(),
)
.unwrap();
// Create test data
let col_data = Arc::new(datatypes::arrow::array::StringArray::from(vec![
"B", "C", "E", "B", "C", "D", "F",
]));
let batch = RecordBatch::try_new(Arc::new(schema), vec![col_data]).unwrap();
let expected = datatypes::arrow::array::StringArray::from(vec!["B", "C", "B", "C", "D"]);
// Filter the batch
let filtered_batch = batch_filter(&batch, &physical_expr).unwrap();
// Expected: rows with col in ("B", "C", "D")
// That would be rows 0, 1, 3, 4, 5
assert_eq!(filtered_batch.num_rows(), 5);
let col_filtered = filtered_batch
.column(0)
.as_any()
.downcast_ref::<datatypes::arrow::array::StringArray>()
.unwrap();
assert_eq!(col_filtered, &expected);
}
#[test]
fn test_maybe_build_regex() {
// Test case for RegexMatch (case sensitive, non-negative)
let (regex, negative) = SimplePredicate::maybe_build_regex(
Operator::RegexMatch,
&ScalarValue::Utf8(Some("a.*b".to_string())),
)
.unwrap();
assert!(regex.is_some());
assert!(!negative);
assert!(regex.unwrap().is_match("axxb"));
// Test case for RegexIMatch (case insensitive, non-negative)
let (regex, negative) = SimplePredicate::maybe_build_regex(
Operator::RegexIMatch,
&ScalarValue::Utf8(Some("a.*b".to_string())),
)
.unwrap();
assert!(regex.is_some());
assert!(!negative);
assert!(regex.unwrap().is_match("AxxB"));
// Test case for RegexNotMatch (case sensitive, negative)
let (regex, negative) = SimplePredicate::maybe_build_regex(
Operator::RegexNotMatch,
&ScalarValue::Utf8(Some("a.*b".to_string())),
)
.unwrap();
assert!(regex.is_some());
assert!(negative);
// Test case for RegexNotIMatch (case insensitive, negative)
let (regex, negative) = SimplePredicate::maybe_build_regex(
Operator::RegexNotIMatch,
&ScalarValue::Utf8(Some("a.*b".to_string())),
)
.unwrap();
assert!(regex.is_some());
assert!(negative);
// Test with empty regex pattern
let (regex, negative) = SimplePredicate::maybe_build_regex(
Operator::RegexMatch,
&ScalarValue::Utf8(Some("".to_string())),
)
.unwrap();
assert!(regex.is_none());
assert!(!negative);
// Test with non-regex operator
let (regex, negative) = SimplePredicate::maybe_build_regex(
Operator::Eq,
&ScalarValue::Utf8(Some("a.*b".to_string())),
)
.unwrap();
assert!(regex.is_none());
assert!(!negative);
// Test with invalid regex pattern
let result = SimplePredicate::maybe_build_regex(
Operator::RegexMatch,
&ScalarValue::Utf8(Some("a(b".to_string())),
);
assert!(result.is_err());
// Test with non-string value
let result = SimplePredicate::maybe_build_regex(
Operator::RegexMatch,
&ScalarValue::Int64(Some(123)),
);
assert!(result.is_err());
// Test with null value
let result =
SimplePredicate::maybe_build_regex(Operator::RegexMatch, &ScalarValue::Utf8(None));
assert!(result.is_err());
}
#[test]
fn test_regex_match_dictionary_array() {
use datatypes::arrow::array::StringDictionaryBuilder;
// Create a StringDictionaryArray
let mut builder = StringDictionaryBuilder::<UInt32Type>::new();
builder.append("apple").unwrap();
builder.append("banana").unwrap();
builder.append("apple").unwrap();
builder.append("cherry").unwrap();
let dict_array = builder.finish();
// Test regex that matches "apple"
let regex = regex::Regex::new(r"app.*").unwrap();
let result = regexp_is_match_dictionary(&dict_array, Some(&regex)).unwrap();
// Should match indices 0 and 2 (both "apple")
assert_eq!(result.len(), 4);
assert!(result.value(0)); // "apple"
assert!(!result.value(1)); // "banana"
assert!(result.value(2)); // "apple"
assert!(!result.value(3)); // "cherry"
// Test regex that matches "banana"
let regex2 = regex::Regex::new(r"ban.*").unwrap();
let result2 = regexp_is_match_dictionary(&dict_array, Some(&regex2)).unwrap();
assert!(!result2.value(0)); // "apple"
assert!(result2.value(1)); // "banana"
assert!(!result2.value(2)); // "apple"
assert!(!result2.value(3)); // "cherry"
// Test with no regex (should match all)
let result3 = regexp_is_match_dictionary(&dict_array, None).unwrap();
assert!(result3.value(0));
assert!(result3.value(1));
assert!(result3.value(2));
assert!(result3.value(3));
}
#[test]
fn test_regex_scan_masks_preserve_sql_null_semantics() {
let plain = Arc::new(datatypes::arrow::array::StringArray::from(vec![
Some("api"),
Some("API"),
Some("db"),
None,
])) as ArrayRef;
assert_regex_scan_masks(
&plain,
[
vec![true, false, false, false],
vec![true, true, false, false],
vec![false, true, true, false],
vec![false, false, true, false],
],
);
let dictionary = DictionaryArray::new(
datatypes::arrow::array::UInt32Array::from(vec![
Some(0),
Some(1),
Some(2),
None,
Some(3),
]),
Arc::new(datatypes::arrow::array::StringArray::from(vec![
Some("api"),
Some("API"),
Some("db"),
None,
])),
);
let raw =
regexp_is_match_dictionary(&dictionary, Some(&Regex::new("^api$").unwrap())).unwrap();
assert!(raw.is_null(3)); // null dictionary key
assert!(raw.is_null(4)); // non-null key referencing a null dictionary value
let dictionary = Arc::new(dictionary) as ArrayRef;
assert_regex_scan_masks(
&dictionary,
[
vec![true, false, false, false, false],
vec![true, true, false, false, false],
vec![false, true, true, false, false],
vec![false, false, true, false, false],
],
);
let negative = regex_evaluator(Operator::RegexNotMatch);
assert!(!negative.evaluate_scalar(&ScalarValue::Utf8(None)).unwrap());
}
fn assert_regex_scan_masks(input: &ArrayRef, expected: [Vec<bool>; 4]) {
for (op, expected) in [
Operator::RegexMatch,
Operator::RegexIMatch,
Operator::RegexNotMatch,
Operator::RegexNotIMatch,
]
.into_iter()
.zip(expected)
{
assert_eq!(
BooleanBuffer::from(expected),
regex_evaluator(op).evaluate_array(input).unwrap(),
"{op:?}"
);
}
}
fn regex_evaluator(op: Operator) -> SimpleFilterEvaluator {
let expr = Expr::BinaryExpr(BinaryExpr {
left: Box::new(Expr::Column(Column::from_name("host"))),
op,
right: Box::new("^api$".lit()),
});
SimpleFilterEvaluator::try_new(&expr).unwrap()
}
}
@@ -0,0 +1,72 @@
// 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::sync::Arc;
use datafusion::execution::context::ExecutionProps;
use datafusion::logical_expr::physical_planning_context::PhysicalPlanningContext;
use datafusion::logical_expr::{col, lit};
use datafusion::physical_expr::create_physical_expr;
use datafusion_common::DFSchema;
use datatypes::arrow::datatypes::{DataType, Field, Schema};
use datatypes::arrow::array::{BooleanArray, RecordBatch};
use crate::filter::{batch_filter, boolean_array_to_scan_mask};
#[test]
fn batch_filter_test() {
let expr = col("ts").gt(lit(123456u64));
let schema = Schema::new(vec![
Field::new("a", DataType::Int32, true),
Field::new("ts", DataType::UInt64, false),
]);
let df_schema = DFSchema::try_from(schema.clone()).unwrap();
let props = ExecutionProps::new();
let physical_expr = create_physical_expr(
&expr,
&df_schema,
&props,
&PhysicalPlanningContext::default(),
)
.unwrap();
let batch = RecordBatch::try_new(
Arc::new(schema),
vec![
Arc::new(datatypes::arrow::array::Int32Array::from(vec![4, 5, 6])),
Arc::new(datatypes::arrow::array::UInt64Array::from(vec![
123456, 123457, 123458,
])),
],
)
.unwrap();
let new_batch = batch_filter(&batch, &physical_expr).unwrap();
assert_eq!(new_batch.num_rows(), 2);
let first_column_values = new_batch
.column(0)
.as_any()
.downcast_ref::<datatypes::arrow::array::Int32Array>()
.unwrap();
let expected = datatypes::arrow::array::Int32Array::from(vec![5, 6]);
assert_eq!(first_column_values, &expected);
}
#[test]
fn test_nullable_boolean_predicate_becomes_scan_mask() {
let predicate = BooleanArray::from(vec![Some(true), None, Some(false)]);
assert_eq!(
BooleanArray::from(vec![true, false, false]),
boolean_array_to_scan_mask(&predicate)
);
}
@@ -0,0 +1,439 @@
// 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.
//! Timestamp-unit adaptation for predicates evaluated against historical SSTs.
use common_time::timestamp::div_mod_units;
use datafusion::logical_expr::Operator;
use datafusion_common::ScalarValue;
use datafusion_common::arrow::array::{ArrayRef, Datum, Scalar};
use datatypes::arrow::datatypes::TimeUnit;
use crate::filter::predicate::SimplePredicate;
/// Builds a tz-naive timestamp literal for `value` in `unit`.
pub fn timestamp_scalar_value(value: i64, unit: TimeUnit) -> ScalarValue {
match unit {
TimeUnit::Second => ScalarValue::TimestampSecond(Some(value), None),
TimeUnit::Millisecond => ScalarValue::TimestampMillisecond(Some(value), None),
TimeUnit::Microsecond => ScalarValue::TimestampMicrosecond(Some(value), None),
TimeUnit::Nanosecond => ScalarValue::TimestampNanosecond(Some(value), None),
}
}
impl SimplePredicate {
pub(crate) fn cast_timestamp_unit(&self, target_unit: TimeUnit) -> Option<Self> {
// Constants represent scan outcomes here. As in the public Matched/Pruned
// result, UNKNOWN can be collapsed to false under these AND/OR-only trees.
match self {
Self::And(left, right) | Self::Or(left, right) => {
let left = left.cast_timestamp_unit(target_unit)?;
let right = right.cast_timestamp_unit(target_unit)?;
let and = matches!(self, Self::And(..));
Some(match (left, right) {
(Self::Constant(Some(false)), _) | (_, Self::Constant(Some(false))) if and => {
Self::Constant(Some(false))
}
(Self::Constant(Some(true)), _) | (_, Self::Constant(Some(true))) if !and => {
Self::Constant(Some(true))
}
(Self::Constant(Some(true)), other) | (other, Self::Constant(Some(true)))
if and =>
{
other
}
(Self::Constant(Some(false)), other) | (other, Self::Constant(Some(false)))
if !and =>
{
other
}
(left, right) => {
if and {
Self::And(Box::new(left), Box::new(right))
} else {
Self::Or(Box::new(left), Box::new(right))
}
}
})
}
Self::IsNull { .. } => Some(self.clone()),
Self::Constant(value) => Some(Self::Constant(Some(value.unwrap_or(false)))),
Self::OrEqChain { literals } => {
let mut converted = Vec::with_capacity(literals.len());
for literal in literals {
let scalar = ScalarValue::try_from_array(literal.get().0, 0).ok()?;
let (value, unit) = timestamp_scalar_parts(&scalar)?;
let Some(value) = value else {
continue;
};
let cast = div_mod_units(value, unit.into(), target_unit.into())?;
if cast.remainder == 0 {
converted.push(timestamp_scalar(cast.quotient, target_unit)?);
}
}
if converted.is_empty() {
return Some(Self::Constant(Some(false)));
}
Some(Self::OrEqChain {
literals: converted,
})
}
Self::Comparison { literal, op, .. } => {
let scalar = ScalarValue::try_from_array(literal.get().0, 0).ok()?;
let (value, unit) = timestamp_scalar_parts(&scalar)?;
let Some(value) = value else {
return Some(Self::Constant(Some(false)));
};
if unit == target_unit {
return Some(self.clone());
}
let cast = div_mod_units(value, unit.into(), target_unit.into())?;
let divisible = cast.remainder == 0;
let literal = timestamp_scalar(cast.quotient, target_unit)?;
let filter = |op| Self::Comparison {
literal: literal.clone(),
op,
regex: None,
regex_negative: false,
};
Some(match op {
Operator::Eq if divisible => filter(Operator::Eq),
Operator::Eq => Self::Constant(Some(false)),
Operator::NotEq if divisible => filter(Operator::NotEq),
Operator::NotEq => Self::Constant(Some(true)),
// The quotient is floor(L), including negative timestamps.
// v > L is v > quotient whether or not L is representable.
Operator::Gt => filter(Operator::Gt),
Operator::GtEq if divisible => filter(Operator::GtEq),
// Between target values, v >= L is v > quotient.
Operator::GtEq => filter(Operator::Gt),
Operator::Lt if divisible => filter(Operator::Lt),
// Between target values, v < L is v <= quotient.
Operator::Lt => filter(Operator::LtEq),
Operator::LtEq => filter(Operator::LtEq),
_ => return None,
})
}
}
}
}
/// Extracts the value and unit from a tz-naive timestamp scalar.
fn timestamp_scalar_parts(scalar: &ScalarValue) -> Option<(Option<i64>, TimeUnit)> {
let (value, unit, timezone) = match scalar {
ScalarValue::TimestampSecond(v, tz) => (*v, TimeUnit::Second, tz),
ScalarValue::TimestampMillisecond(v, tz) => (*v, TimeUnit::Millisecond, tz),
ScalarValue::TimestampMicrosecond(v, tz) => (*v, TimeUnit::Microsecond, tz),
ScalarValue::TimestampNanosecond(v, tz) => (*v, TimeUnit::Nanosecond, tz),
_ => return None,
};
// A timezone-aware literal doesn't compare against a tz-naive column;
// leave it to the caller instead of guessing the intended semantics.
(timezone.is_none()).then_some((value, unit))
}
/// Builds a one-element scalar array holding `value` in `unit`.
fn timestamp_scalar(value: i64, unit: TimeUnit) -> Option<Scalar<ArrayRef>> {
timestamp_scalar_value(value, unit).to_scalar().ok()
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use datafusion::execution::context::ExecutionProps;
use datafusion::logical_expr::physical_planning_context::PhysicalPlanningContext;
use datafusion::logical_expr::{Expr, col, lit};
use datafusion::physical_expr::create_physical_expr;
use datafusion_common::arrow::array::ArrayRef;
use datafusion_common::arrow::buffer::BooleanBuffer;
use datafusion_common::cast::as_boolean_array;
use datafusion_common::{DFSchema, ScalarValue};
use datatypes::arrow::array::{
RecordBatch, TimestampMillisecondArray, TimestampNanosecondArray,
};
use datatypes::arrow::datatypes::{Field, Schema};
use datatypes::data_type::ConcreteDataType;
use crate::filter::{SimpleFilterEvaluator, TimestampUnitCast, boolean_array_to_scan_mask};
#[test]
fn compound_timestamp_filters_preserve_unit_conversion() {
use datatypes::arrow::array::TimestampMicrosecondArray;
let null = || lit(ScalarValue::TimestampMicrosecond(None, None));
let predicates = [
col("ts")
.gt(lit(ts_us(-1500)))
.and(col("ts").lt(lit(ts_us(500)))),
col("ts")
.eq(lit(ts_us(500)))
.or(col("ts").gt_eq(lit(ts_us(1500)))),
col("ts")
.is_not_null()
.and(col("ts").not_eq(lit(ts_us(500)))),
col("ts").eq(null()).or(col("ts").eq(lit(ts_us(-1000)))),
col("ts")
.eq(lit(ts_us(500)))
.and(col("ts").gt(lit(ts_us(0)))),
col("ts")
.not_eq(lit(ts_us(500)))
.or(col("ts").lt(lit(ts_us(0)))),
];
let native: ArrayRef = Arc::new(TimestampMicrosecondArray::from(vec![
-2000, -1000, 0, 1000, 2000,
]));
let old: ArrayRef = Arc::new(TimestampMillisecondArray::from(vec![-2, -1, 0, 1, 2]));
let schema = Arc::new(Schema::new(vec![Field::new(
"ts",
native.data_type().clone(),
false,
)]));
let batch = RecordBatch::try_new(schema.clone(), vec![native]).unwrap();
for expr in predicates {
let physical = create_physical_expr(
&expr,
&DFSchema::try_from(schema.as_ref().clone()).unwrap(),
&ExecutionProps::new(),
&PhysicalPlanningContext::default(),
)
.unwrap();
let result = physical
.evaluate(&batch)
.unwrap()
.into_array(batch.num_rows())
.unwrap();
let expected = boolean_array_to_scan_mask(as_boolean_array(&result).unwrap())
.values()
.clone();
let actual = match cast_to_ms(&expr).unwrap() {
TimestampUnitCast::Matched => BooleanBuffer::new_set(old.len()),
TimestampUnitCast::Pruned => BooleanBuffer::new_unset(old.len()),
TimestampUnitCast::Filter(filter) => filter.evaluate_array(&old).unwrap(),
};
assert_eq!(actual, expected, "{expr}");
}
}
fn ts_us(v: i64) -> ScalarValue {
ScalarValue::TimestampMicrosecond(Some(v), None)
}
fn cast_to_ms(expr: &Expr) -> Option<TimestampUnitCast> {
SimpleFilterEvaluator::try_new(expr)
.unwrap()
.cast_timestamp_unit(&ConcreteDataType::timestamp_millisecond_datatype())
}
/// Evaluates a cast `Filter` outcome against millisecond values and
/// returns the mask.
fn eval_ms_mask(cast: Option<TimestampUnitCast>, values: &[i64]) -> Vec<bool> {
let filter = match cast.expect("cast must apply") {
TimestampUnitCast::Filter(filter) => filter,
other => panic!("expected Filter outcome, got {other:?}"),
};
let array = Arc::new(TimestampMillisecondArray::from(values.to_vec())) as ArrayRef;
filter.evaluate_array(&array).unwrap().iter().collect()
}
#[test]
fn cast_timestamp_unit_converts_representable_literal() {
// ts = 7_000_000us evaluated against a ms column becomes ts = 7000ms.
assert_eq!(
vec![false, true, false],
eval_ms_mask(
cast_to_ms(&col("ts").eq(lit(ts_us(7_000_000)))),
&[6_999, 7_000, 7_001]
)
);
// != complements =.
assert_eq!(
vec![true, false, true],
eval_ms_mask(
cast_to_ms(&col("ts").not_eq(lit(ts_us(7_000_000)))),
&[6_999, 7_000, 7_001]
)
);
}
#[test]
fn cast_timestamp_unit_prunes_non_representable_equality() {
// 7_000_500us is not a whole millisecond: no ms row can equal it,
// and every ms row satisfies `!=`.
assert!(matches!(
cast_to_ms(&col("ts").eq(lit(ts_us(7_000_500)))),
Some(TimestampUnitCast::Pruned)
));
assert!(matches!(
cast_to_ms(&col("ts").not_eq(lit(ts_us(7_000_500)))),
Some(TimestampUnitCast::Matched)
));
}
#[test]
fn cast_timestamp_unit_strengthens_inequalities() {
// 2_500_500us is strictly between 2500ms and 2501ms: the cast must
// not round the literal to a boundary the original predicate
// excludes (>= 2500ms would wrongly match 2500ms).
let values = vec![2_499, 2_500, 2_501];
assert_eq!(
vec![false, false, true],
eval_ms_mask(cast_to_ms(&col("ts").gt(lit(ts_us(2_500_500)))), &values)
);
assert_eq!(
vec![false, false, true],
eval_ms_mask(cast_to_ms(&col("ts").gt_eq(lit(ts_us(2_500_500)))), &values)
);
assert_eq!(
vec![true, true, false],
eval_ms_mask(cast_to_ms(&col("ts").lt(lit(ts_us(2_500_500)))), &values)
);
assert_eq!(
vec![true, true, false],
eval_ms_mask(cast_to_ms(&col("ts").lt_eq(lit(ts_us(2_500_500)))), &values)
);
// Representable boundary keeps the original operator.
let values = vec![2_499, 2_500, 2_501];
assert_eq!(
vec![false, false, true],
eval_ms_mask(cast_to_ms(&col("ts").gt(lit(ts_us(2_500_000)))), &values)
);
assert_eq!(
vec![false, true, true],
eval_ms_mask(cast_to_ms(&col("ts").gt_eq(lit(ts_us(2_500_000)))), &values)
);
assert_eq!(
vec![true, false, false],
eval_ms_mask(cast_to_ms(&col("ts").lt(lit(ts_us(2_500_000)))), &values)
);
assert_eq!(
vec![true, true, false],
eval_ms_mask(cast_to_ms(&col("ts").lt_eq(lit(ts_us(2_500_000)))), &values)
);
}
#[test]
fn cast_timestamp_unit_handles_negative_instants() {
// -2_500_500us floors to -2501ms with remainder 500us: only rows
// with instant >= -2.5005ms may match >= / >.
let values = vec![-2_502, -2_501, -2_500];
assert_eq!(
vec![false, false, true],
eval_ms_mask(
cast_to_ms(&col("ts").gt_eq(lit(ts_us(-2_500_500)))),
&values
)
);
assert_eq!(
vec![false, false, true],
eval_ms_mask(cast_to_ms(&col("ts").gt(lit(ts_us(-2_500_500)))), &values)
);
assert_eq!(
vec![true, true, false],
eval_ms_mask(
cast_to_ms(&col("ts").lt_eq(lit(ts_us(-2_500_500)))),
&values
)
);
// Exactly representable negative literal: -2_500_000us == -2500ms.
assert_eq!(
vec![false, false, true],
eval_ms_mask(cast_to_ms(&col("ts").eq(lit(ts_us(-2_500_000)))), &values)
);
}
#[test]
fn cast_timestamp_unit_or_chain_drops_unrepresentable_literals() {
// Only 7_000_000us is a whole millisecond; 7_000_500us cannot match.
let expr = col("ts")
.eq(lit(ts_us(7_000_500)))
.or(col("ts").eq(lit(ts_us(7_000_000))));
assert_eq!(
vec![false, true],
eval_ms_mask(cast_to_ms(&expr), &[6_999, 7_000])
);
// A chain where no literal is representable matches nothing.
let expr = col("ts")
.eq(lit(ts_us(7_000_500)))
.or(col("ts").eq(lit(ts_us(6_000_500))));
assert!(matches!(cast_to_ms(&expr), Some(TimestampUnitCast::Pruned)));
}
#[test]
fn cast_timestamp_unit_null_literal_prunes() {
// A NULL literal never compares true.
assert!(matches!(
cast_to_ms(&col("ts").eq(lit(ScalarValue::TimestampMicrosecond(None, None)))),
Some(TimestampUnitCast::Pruned)
));
}
#[test]
fn cast_timestamp_unit_same_unit_returns_filter_directly() {
// A literal already in the target unit is returned unchanged.
let expr = col("ts").eq(lit(ts_us(7_000_000)));
let filter = SimpleFilterEvaluator::try_new(&expr).unwrap();
let cast = filter
.cast_timestamp_unit(&ConcreteDataType::timestamp_microsecond_datatype())
.unwrap();
let TimestampUnitCast::Filter(f) = cast else {
panic!("expected Filter, got {cast:?}")
};
assert!(filter.is_eq() && f.is_eq());
assert_eq!(filter.column_name(), f.column_name());
assert_eq!(filter.literal_value(), f.literal_value());
}
#[test]
fn cast_timestamp_unit_rejects_non_timestamps() {
// Non-timestamp target type.
assert!(
SimpleFilterEvaluator::try_new(&col("ts").gt(lit(ts_us(1))))
.unwrap()
.cast_timestamp_unit(&ConcreteDataType::int64_datatype())
.is_none()
);
// Non-timestamp literal against a timestamp target.
assert!(cast_to_ms(&col("ts").gt(lit(42_i64))).is_none());
}
#[test]
fn cast_timestamp_unit_to_finer_unit() {
// 5 seconds against a nanosecond column becomes 5_000_000_000ns.
let filter = SimpleFilterEvaluator::try_new(
&col("ts").eq(lit(ScalarValue::TimestampSecond(Some(5), None))),
)
.unwrap()
.cast_timestamp_unit(&ConcreteDataType::timestamp_nanosecond_datatype())
.and_then(|cast| match cast {
TimestampUnitCast::Filter(filter) => Some(filter),
_ => None,
})
.unwrap();
let array = Arc::new(TimestampNanosecondArray::from(vec![
4_999_999_999,
5_000_000_000,
])) as ArrayRef;
assert_eq!(
vec![false, true],
filter
.evaluate_array(&array)
.unwrap()
.iter()
.collect::<Vec<_>>()
);
}
}
+14 -5
View File
@@ -30,12 +30,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("cargo:rerun-if-env-changed=RUSTC");
println!("cargo:rerun-if-env-changed=GREPTIME_PRODUCT_NAME");
println!("cargo:rerun-if-env-changed=CARGO_WORKSPACE_DIR");
// The "CARGO_WORKSPACE_DIR" is set manually (not by Rust itself) in Cargo config file, to
// solve the problem where the "CARGO_MANIFEST_DIR" is not what we want when this repo is
// made as a submodule in another repo.
let workspace_dir =
env::var("CARGO_WORKSPACE_DIR").or_else(|_| env::var("CARGO_MANIFEST_DIR"))?;
// Resolve the workspace root from CARGO_MANIFEST_DIR (this crate is
// src/common/version, three levels below the root). The path remains absolute
// and is used at build time. CARGO_WORKSPACE_DIR remains an explicit override
// for submodule builds; see https://github.com/rust-lang/cargo/issues/3946.
let workspace_dir = match env::var("CARGO_WORKSPACE_DIR") {
Ok(dir) if !dir.is_empty() => dir,
_ => env::var("CARGO_MANIFEST_DIR").map(|dir| {
PathBuf::from(dir)
.join("../../..")
.to_string_lossy()
.into_owned()
})?,
};
let workspace_root = PathBuf::from(&workspace_dir);
println!(
"cargo:rerun-if-changed={}",
+22 -8
View File
@@ -554,8 +554,8 @@ impl ErrorExt for Error {
use Error::*;
match self {
RegionNotReady { .. } | RegionNotFound { .. } => RetryHint::RetryAfterRouteRefresh,
RegionBusy { .. }
| RegionNotReady { .. }
| ConcurrentQueryLimiterClosed { .. }
| ConcurrentQueryLimiterTimeout { .. } => RetryHint::Retryable,
NewPlanDecoder { source, .. } | ExecuteLogicalPlan { source, .. } => {
@@ -595,14 +595,28 @@ mod tests {
use super::*;
#[test]
fn test_region_state_hints_are_retryable() {
fn test_region_state_retry_hints() {
let region_id = RegionId::new(1024, 1);
let err = RegionBusySnafu { region_id }.build();
assert_eq!(err.retry_hint(), RetryHint::Retryable);
let err = RegionNotReadySnafu { region_id }.build();
assert_eq!(err.retry_hint(), RetryHint::Retryable);
for (error, status, hint) in [
(
RegionBusySnafu { region_id }.build(),
StatusCode::RegionBusy,
RetryHint::Retryable,
),
(
RegionNotReadySnafu { region_id }.build(),
StatusCode::RegionNotReady,
RetryHint::RetryAfterRouteRefresh,
),
(
RegionNotFoundSnafu { region_id }.build(),
StatusCode::RegionNotFound,
RetryHint::RetryAfterRouteRefresh,
),
] {
assert_eq!(error.status_code(), status);
assert_eq!(error.retry_hint(), hint);
}
}
#[test]
+1 -1
View File
@@ -2566,7 +2566,7 @@ mod tests {
.unwrap_err();
assert_matches!(&err, error::Error::HandleBatchOpenRequest { .. });
assert_eq!(err.status_code(), StatusCode::RegionNotFound);
assert_eq!(err.retry_hint(), RetryHint::NonRetryable);
assert_eq!(err.retry_hint(), RetryHint::RetryAfterRouteRefresh);
}
struct CurrentEngineTest {
+4 -2
View File
@@ -86,8 +86,10 @@ pub fn vector_type_value_to_string(val: &[u8], dim: u32) -> Result<String> {
}
let elements = val
.chunks_exact(std::mem::size_of::<f32>())
.map(|e| f32::from_le_bytes(e.try_into().unwrap()));
.as_chunks::<{ std::mem::size_of::<f32>() }>()
.0
.iter()
.map(|e| f32::from_le_bytes(*e));
let mut s = String::from("[");
for (i, e) in elements.enumerate() {
+16 -3
View File
@@ -141,8 +141,9 @@ fn relation_desc_from_output(
columns: &[ColumnSchema],
lineage: &[Option<usize>],
source_primary_key_indices: &[usize],
plan: &LogicalPlan,
) -> RelationDesc {
let keys = source_primary_key_indices
let mut keys = source_primary_key_indices
.iter()
.filter_map(|source_index| {
lineage
@@ -150,6 +151,14 @@ fn relation_desc_from_output(
.position(|index| index == &Some(*source_index))
})
.collect_vec();
// DISTINCT defines identity from every output tuple; the source timestamp participates
// separately as the relation time index, so it is excluded from the tag key.
if matches!(plan, LogicalPlan::Distinct(Distinct::All(_))) {
keys = columns
.iter()
.positions(|column| !column.is_time_index())
.collect();
}
let time_index = columns.iter().position(ColumnSchema::is_time_index);
RelationDesc {
typ: crate::repr::RelationType {
@@ -849,8 +858,12 @@ impl StreamingEngine {
let source_primary_key_indices = source_meta.primary_key_indices;
stateless::validate_source_scan(&flow_plan, source_table_id, &source_schema)?;
let (inferred_schema, lineage) = output_column_schemas(&flow_plan, &source_schema)?;
let inferred_relation =
relation_desc_from_output(&inferred_schema, &lineage, &source_primary_key_indices);
let inferred_relation = relation_desc_from_output(
&inferred_schema,
&lineage,
&source_primary_key_indices,
&flow_plan,
);
let sink_exists = self.fetch_table_pk_schema(sink_table_name).await?.is_some();
if !sink_exists {
validate_auto_column_names(&inferred_schema)?;
+126 -3
View File
@@ -20,7 +20,7 @@ use datafusion::arrow::datatypes::{DataType as ArrowDataType, Field, TimeUnit as
use datafusion::catalog::MemTable;
use datafusion::datasource::provider_as_source;
use datafusion_common::TableReference;
use datafusion_expr::LogicalPlanBuilder;
use datafusion_expr::{LogicalPlan, LogicalPlanBuilder};
use datatypes::schema::{ColumnDefaultConstraint, ColumnSchema, Schema, SchemaBuilder};
use store_api::storage::{ConcreteDataType, TableId};
use table::metadata::{TableInfo, TableInfoBuilder, TableMetaBuilder};
@@ -194,10 +194,133 @@ fn stateless_distinct_preserves_direct_column_lineage() {
let (output, lineage) = super::output_column_schemas(&plan, &source).unwrap();
assert_eq!(output[0].name, "dis");
assert_eq!(lineage, vec![Some(0)]);
let relation = super::relation_desc_from_output(&output, &lineage, &[0]);
let relation = super::relation_desc_from_output(&output, &lineage, &[0], &plan);
assert_eq!(relation.typ.keys[0].column_indices, vec![0]);
}
#[test]
fn stateless_distinct_output_is_always_the_sink_key() {
let source = Arc::new(Schema::new(vec![
ColumnSchema::new("v", ConcreteDataType::int32_datatype(), true),
ColumnSchema::new("k", ConcreteDataType::int32_datatype(), true),
ColumnSchema::new("other", ConcreteDataType::int32_datatype(), true),
ColumnSchema::new(
"ts",
ConcreteDataType::timestamp_millisecond_datatype(),
false,
)
.with_time_index(true),
]));
// All cases share one provider so the key assertions exercise only plan/output metadata.
let provider = Arc::new(
MemTable::try_new(
Arc::new(datafusion::arrow::datatypes::Schema::new(vec![
Field::new("v", ArrowDataType::Int32, true),
Field::new("k", ArrowDataType::Int32, true),
Field::new("other", ArrowDataType::Int32, true),
Field::new(
"ts",
ArrowDataType::Timestamp(ArrowTimeUnit::Millisecond, None),
false,
),
])),
vec![vec![]],
)
.unwrap(),
);
let make_plan = |expressions: Vec<datafusion_expr::Expr>, distinct: bool| {
let builder = LogicalPlanBuilder::scan(
TableReference::bare("source"),
provider_as_source(provider.clone()),
None,
)
.unwrap()
.project(expressions)
.unwrap();
if distinct {
builder.distinct().unwrap().build().unwrap()
} else {
builder.build().unwrap()
}
};
let assert_key = |plan: &LogicalPlan, source_keys: &[usize], expected: &[usize]| {
let (output, lineage) = super::output_column_schemas(plan, &source).unwrap();
let relation = super::relation_desc_from_output(&output, &lineage, source_keys, plan);
assert_eq!(
relation
.typ
.keys
.iter()
.map(|key| key.column_indices.clone())
.collect::<Vec<_>>(),
if expected.is_empty() {
vec![]
} else {
vec![expected.to_vec()]
}
);
relation
};
// No source PK: all distinct output fields form the key.
assert_key(&make_plan(vec![datafusion_expr::col("v")], true), &[], &[0]);
// A surviving subset of a composite source PK cannot identify projected DISTINCT tuples.
assert_key(
&make_plan(
vec![datafusion_expr::col("v"), datafusion_expr::col("other")],
true,
),
&[0, 1],
&[0, 1],
);
// Even a retained source PK needs the additional selected fields in the DISTINCT key.
assert_key(
&make_plan(
vec![datafusion_expr::col("v"), datafusion_expr::col("other")],
true,
),
&[0],
&[0, 1],
);
// Aliases and computed output expressions are included by output position.
let alias_plan = make_plan(
vec![
datafusion_expr::col("v").alias("renamed"),
(datafusion_expr::col("v") + datafusion_expr::col("other")).alias("computed"),
],
true,
);
let (output, lineage) = super::output_column_schemas(&alias_plan, &source).unwrap();
let relation = super::relation_desc_from_output(&output, &lineage, &[0], &alias_plan);
assert_eq!(
output
.iter()
.map(|column| column.name.as_str())
.collect_vec(),
["renamed", "computed"]
);
assert_eq!(relation.typ.keys[0].column_indices, vec![0, 1]);
// A selected time index remains the relation time index, not part of the tag key.
let relation = assert_key(
&make_plan(
vec![datafusion_expr::col("v"), datafusion_expr::col("ts")],
true,
),
&[0],
&[0],
);
assert_eq!(relation.typ.time_index, Some(1));
// Non-DISTINCT plans retain only inherited source-key lineage (or no key if none survives).
let ordinary = make_plan(
vec![datafusion_expr::col("v"), datafusion_expr::col("other")],
false,
);
assert_key(&ordinary, &[0, 1], &[0]);
assert_key(&ordinary, &[], &[]);
}
#[test]
fn stateless_normalizes_dictionary_output_type() {
let field = Field::new_dictionary("host", ArrowDataType::UInt32, ArrowDataType::Utf8, true);
@@ -220,7 +343,7 @@ fn stateless_normalizes_dictionary_output_type() {
assert_eq!(output[0].data_type, ConcreteDataType::string_datatype());
assert_eq!(lineage, vec![Some(0)]);
let relation = super::relation_desc_from_output(&output, &lineage, &[0]);
let relation = super::relation_desc_from_output(&output, &lineage, &[0], &plan);
assert_eq!(relation.typ.keys[0].column_indices, vec![0]);
}
+6
View File
@@ -1400,6 +1400,12 @@ impl BatchingTask {
match res {
Ok(res) => {
let (affected_rows, _) = res.output.extract_rows_and_cost();
if matches!(&res.output.data, common_query::OutputData::AffectedRows(_)) {
res.metrics.wait_ready().await;
}
if let Some(error) = res.metrics.completion_error() {
warn!("Flow {flow_id} completed with terminal metrics error: {error}");
}
debug!(
"Flow {flow_id} executed, affected_rows: {affected_rows:?}, elapsed: {:?}, watermark: {:?}",
elapsed,
-1
View File
@@ -22,7 +22,6 @@ use common_recordbatch::recordbatch::merge_record_batches;
use common_recordbatch::{RecordBatch, util};
use common_time::Timestamp;
use datafusion_common::ScalarValue;
use datafusion_common::tree_node::TreeNode as _;
use datafusion_expr::{GroupingSet, TableScanBuilder};
use datatypes::arrow::array::{Array, AsArray};
use datatypes::arrow::datatypes::{Float64Type, Int64Type, UInt64Type};
+5 -2
View File
@@ -42,7 +42,9 @@ remote datanodes via `operator`/`client`.
- **Insert** (`instance/grpc.rs`): `handle_inserts` / `handle_row_inserts` →
`check_permission` → `operator`'s `Inserter` (schema validation, optional
auto-create, partition routing, meter admission) → local `RegionServer`
(standalone) or RPC to datanodes (distributed). Arrow bulk inserts pass the
(standalone) or RPC to datanodes (distributed). `FrontendBuilder` configures
the operator write dispatcher with frontend retry options for distributed
inserts, deletes, and bulk writes. Arrow bulk inserts pass the
request channel to `Inserter` and check meter admission for each nonempty batch.
- Finite ingestion requests split internally admit their total rows per database
before dispatch (`operator::insert::admit_write` / `admit_row_insert_batches`).
@@ -96,7 +98,8 @@ cargo nextest run -p frontend
`servers`; permissions, planning, and routing live here.
- Standalone vs distributed diverge in datanode access (the `standalone` crate's
local `RegionServer` adapter vs `NodeClients` RPC), MetaClient usage, and
whether heartbeat matters. In standalone, the cache invalidator is a no-op.
whether heartbeat matters. The builder defaults local invalidation to its
layered cache registry; callers may supply a custom invalidator.
## Maintenance contract
+9 -1
View File
@@ -323,6 +323,13 @@ pub enum Error {
name: String,
},
#[snafu(display("Invalid write retry config: {}", reason))]
InvalidWriteRetryConfig {
reason: &'static str,
#[snafu(implicit)]
location: Location,
},
#[snafu(display("Invalid tls config"))]
InvalidTlsConfig {
#[snafu(source)]
@@ -391,7 +398,8 @@ impl ErrorExt for Error {
| Error::IllegalAuthConfig { .. }
| Error::ColumnNoneDefaultValue { .. }
| Error::IncompleteGrpcRequest { .. }
| Error::InvalidTlsConfig { .. } => StatusCode::InvalidArguments,
| Error::InvalidTlsConfig { .. }
| Error::InvalidWriteRetryConfig { .. } => StatusCode::InvalidArguments,
Error::NotSupported { .. } => StatusCode::Unsupported,
+71 -1
View File
@@ -36,9 +36,10 @@ use crate::heartbeat::HeartbeatTask;
use crate::instance::Instance;
use crate::service_config::{
InfluxdbOptions, JaegerOptions, MysqlOptions, OpentsdbOptions, OtlpOptions,
PendingRowsBatcherOptions, PostgresOptions, PromStoreOptions,
PendingRowsBatcherOptions, PostgresOptions, PromStoreOptions, WriteRetryOptions,
};
/// Configures frontend protocols, query execution, and distributed write retries.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub struct FrontendOptions {
@@ -75,6 +76,8 @@ pub struct FrontendOptions {
pub meta_client: Option<MetaClientOptions>,
pub logging: LoggingOptions,
pub datanode: DatanodeClientOptions,
/// Retry policy for retryable distributed write failures.
pub write_retry: WriteRetryOptions,
pub user_provider: Option<String>,
pub tracing: TracingOptions,
pub query: QueryOptions,
@@ -112,6 +115,7 @@ impl Default for FrontendOptions {
meta_client: None,
logging: LoggingOptions::default(),
datanode: DatanodeClientOptions::default(),
write_retry: WriteRetryOptions::default(),
user_provider: None,
tracing: TracingOptions::default(),
query: QueryOptions::default(),
@@ -227,6 +231,72 @@ mod tests {
type GrpcStream<T> =
Pin<Box<dyn Stream<Item = std::result::Result<T, Status>> + Send + Sync + 'static>>;
#[test]
fn test_write_retry_config() {
let defaults: FrontendOptions = toml::from_str("").unwrap();
assert_eq!(defaults.write_retry, WriteRetryOptions::default());
let options: FrontendOptions = toml::from_str(
r#"
[write_retry]
max_retries = 5
initial_backoff = "250ms"
max_backoff = "5s"
backoff_multiplier = 3.0
"#,
)
.unwrap();
assert_eq!(
options.write_retry,
WriteRetryOptions {
max_retries: 5,
initial_backoff: Duration::from_millis(250),
max_backoff: Duration::from_secs(5),
backoff_multiplier: 3.0,
}
);
options.write_retry.validate().unwrap();
let serialized = toml::to_string(&options).unwrap();
let parsed: FrontendOptions = toml::from_str(&serialized).unwrap();
assert_eq!(options.write_retry, parsed.write_retry);
let disabled: FrontendOptions = toml::from_str("[write_retry]\nmax_retries = 0").unwrap();
assert_eq!(disabled.write_retry.max_retries, 0);
assert_eq!(
disabled.write_retry.initial_backoff,
defaults.write_retry.initial_backoff
);
}
#[test]
fn test_write_retry_config_from_env() {
temp_env::with_vars(
[
("FRONTEND_RETRY_TEST__WRITE_RETRY__MAX_RETRIES", Some("4")),
(
"FRONTEND_RETRY_TEST__WRITE_RETRY__INITIAL_BACKOFF",
Some("500ms"),
),
("FRONTEND_RETRY_TEST__WRITE_RETRY__MAX_BACKOFF", Some("8s")),
(
"FRONTEND_RETRY_TEST__WRITE_RETRY__BACKOFF_MULTIPLIER",
Some("1.5"),
),
],
|| {
let options =
FrontendOptions::load_layered_options(None, "FRONTEND_RETRY_TEST").unwrap();
assert_eq!(
options.write_retry,
WriteRetryOptions {
max_retries: 4,
initial_backoff: Duration::from_millis(500),
max_backoff: Duration::from_secs(8),
backoff_multiplier: 1.5,
}
);
},
);
}
#[test]
fn test_logical_batcher_protocols_from_env() {
temp_env::with_vars(
+107 -12
View File
@@ -25,7 +25,7 @@ use common_base::Plugins;
use common_datasource::object_store::LocalFileAccess;
use common_event_recorder::{EventRecorderImpl, EventRecorderRef};
use common_meta::cache::{LayeredCacheRegistryRef, TableFlownodeSetCacheRef, TableRouteCacheRef};
use common_meta::cache_invalidator::{CacheInvalidatorRef, DummyCacheInvalidator};
use common_meta::cache_invalidator::CacheInvalidatorRef;
use common_meta::key::TableMetadataManager;
use common_meta::key::flow::FlowMetadataManager;
use common_meta::kv_backend::KvBackendRef;
@@ -45,6 +45,7 @@ use operator::statement::{
StatementExecutor, StatementExecutorConfiguratorRef, StatementExecutorRef,
};
use operator::table::TableMutationOperator;
use operator::write_dispatcher::WriteDispatcher;
use partition::cache::PartitionInfoCacheRef;
use partition::manager::PartitionRuleManager;
use pipeline::pipeline_operator::PipelineOperator;
@@ -68,7 +69,7 @@ pub struct FrontendBuilder {
options: FrontendOptions,
kv_backend: KvBackendRef,
layered_cache_registry: LayeredCacheRegistryRef,
local_cache_invalidator: Option<CacheInvalidatorRef>,
local_cache_invalidator: CacheInvalidatorRef,
catalog_manager: CatalogManagerRef,
node_manager: NodeManagerRef,
plugins: Option<Plugins>,
@@ -78,6 +79,9 @@ pub struct FrontendBuilder {
}
impl FrontendBuilder {
/// Creates a frontend builder from its configuration and shared services.
///
/// Uses the layered cache registry for local invalidation unless overridden.
#[allow(clippy::too_many_arguments)]
pub fn new(
options: FrontendOptions,
@@ -91,8 +95,8 @@ impl FrontendBuilder {
Self {
options,
kv_backend,
local_cache_invalidator: layered_cache_registry.clone(),
layered_cache_registry,
local_cache_invalidator: None,
catalog_manager,
node_manager,
plugins: None,
@@ -138,9 +142,10 @@ impl FrontendBuilder {
)
}
/// Overrides the default local cache invalidator.
pub fn with_local_cache_invalidator(self, cache_invalidator: CacheInvalidatorRef) -> Self {
Self {
local_cache_invalidator: Some(cache_invalidator),
local_cache_invalidator: cache_invalidator,
..self
}
}
@@ -185,7 +190,11 @@ impl FrontendBuilder {
}
}
/// Validates configuration and assembles the frontend [`Instance`].
pub async fn try_build(self) -> Result<Instance> {
if let Err(reason) = self.options.write_retry.validate() {
return error::InvalidWriteRetryConfigSnafu { reason }.fail();
}
let kv_backend = self.kv_backend;
let node_manager = self.node_manager;
let plugins = self.plugins.unwrap_or_default();
@@ -208,9 +217,18 @@ impl FrontendBuilder {
partition_info_cache.clone(),
));
let local_cache_invalidator = self
.local_cache_invalidator
.unwrap_or_else(|| Arc::new(DummyCacheInvalidator));
let local_cache_invalidator = self.local_cache_invalidator;
let mut write_dispatcher =
WriteDispatcher::new(node_manager.clone(), partition_manager.clone());
if self.options.write_retry.max_retries > 0 {
write_dispatcher = write_dispatcher.with_retry(
self.options.write_retry.clone(),
self.catalog_manager.clone(),
local_cache_invalidator.clone(),
);
}
let write_dispatcher = Arc::new(write_dispatcher);
let region_query_handler =
if let Some(factory) = plugins.get::<RegionQueryHandlerFactoryRef>() {
@@ -234,6 +252,7 @@ impl FrontendBuilder {
table_flownode_cache.clone(),
self.options.auto_create_table,
)
.with_write_dispatcher(write_dispatcher.clone())
};
// The execution-only inserter owns no batchers, avoiding an Arc cycle.
let bulk_inserter = Arc::new(create_inserter());
@@ -262,11 +281,14 @@ impl FrontendBuilder {
create_inserter()
.with_pending_rows_batcher(build_batcher(self.options.table_batcher_options())),
);
let deleter = Arc::new(Deleter::new(
self.catalog_manager.clone(),
partition_manager.clone(),
node_manager.clone(),
));
let deleter = Arc::new(
Deleter::new(
self.catalog_manager.clone(),
partition_manager.clone(),
node_manager.clone(),
)
.with_write_dispatcher(write_dispatcher),
);
let requester = Arc::new(Requester::new(
self.catalog_manager.clone(),
partition_manager.clone(),
@@ -409,3 +431,76 @@ impl FrontendBuilder {
})
}
}
#[cfg(test)]
mod tests {
use api::v1::meta::Role;
use common_meta::cache_invalidator::{Context, DummyCacheInvalidator};
use common_meta::instruction::CacheIdent;
use common_meta::key::table_route::{TableRouteKey, TableRouteValue};
use common_meta::key::{MetadataKey, MetadataValue};
use common_meta::rpc::router::{Region, RegionRoute};
use common_meta::rpc::store::PutRequest;
use meta_client::client::MetaClientBuilder;
use store_api::storage::RegionId;
use super::*;
#[tokio::test]
async fn test_local_cache_invalidator() {
for custom_invalidator in [false, true] {
let mut builder = FrontendBuilder::new_test(
&FrontendOptions::default(),
Arc::new(MetaClientBuilder::new(0, Role::Frontend).build()),
);
if custom_invalidator {
builder = builder.with_local_cache_invalidator(Arc::new(DummyCacheInvalidator));
}
let backend = builder.kv_backend.clone();
let routes: TableRouteCacheRef = builder.layered_cache_registry.get().unwrap();
let partitions: PartitionInfoCacheRef = builder.layered_cache_registry.get().unwrap();
let instance = builder.try_build().await.unwrap();
let table_id = 1024;
for region_number in [1, 2] {
let route = TableRouteValue::physical(vec![RegionRoute {
region: Region {
id: RegionId::new(table_id, region_number),
..Default::default()
},
..Default::default()
}]);
backend
.put(
PutRequest::new()
.with_key(TableRouteKey::new(table_id).to_bytes())
.with_value(route.try_as_raw_value().unwrap()),
)
.await
.unwrap();
if region_number == 1 {
routes.get(table_id).await.unwrap().unwrap();
partitions.get(table_id).await.unwrap().unwrap();
}
}
assert!(routes.contains_key(&table_id));
assert!(partitions.contains_key(&table_id));
instance
.cache_invalidator()
.invalidate(&Context::default(), &[CacheIdent::TableId(table_id)])
.await
.unwrap();
assert_eq!(custom_invalidator, routes.contains_key(&table_id));
assert_eq!(custom_invalidator, partitions.contains_key(&table_id));
let route = routes.get(table_id).await.unwrap().unwrap();
let region = route.as_physical_table_route_ref().unwrap().region_routes[0]
.region
.id;
assert_eq!(
if custom_invalidator { 1 } else { 2 },
region.region_number()
);
}
}
}
@@ -48,6 +48,7 @@ impl Instance {
LogicalTablePendingRowsBatcher::try_new(
self.partition_manager().clone(),
self.node_manager().clone(),
self.inserter.write_dispatcher().clone(),
self.catalog_manager().clone(),
self.table_flownode_set_cache().clone(),
true,
+20 -17
View File
@@ -519,31 +519,34 @@ mod tests {
);
}
/// The PHP instrumentation case: an empty string must be distinguishable
/// from any other unparsable value in the reported diagnostics.
/// The PHP instrumentation case: `getHeaderLine('Content-Length')` sends ""
/// when the header is absent. The span must be kept with a null attribute.
#[test]
fn test_prepare_trace_column_rewrites_reports_empty_string_value() {
let rows = vec![Row {
values: vec![Value {
value_data: Some(ValueData::StringValue(String::new())),
fn test_prepare_trace_column_rewrites_nulls_empty_string_value() {
let mut rows = Rows {
schema: vec![ColumnSchema {
column_name: "span_attributes.http.response.body.size".to_string(),
datatype: ColumnDataType::String as i32,
..Default::default()
}],
}];
rows: vec![Row {
values: vec![Value {
value_data: Some(ValueData::StringValue(String::new())),
}],
}],
};
let pending_rewrites = vec![PendingTraceColumnRewrite {
col_idx: 0,
target_type: ColumnDataType::Int64,
column_name: "span_attributes.http.response.body.size".to_string(),
}];
let err = prepare_trace_column_rewrites(&rows, pending_rewrites, "opentelemetry_traces")
.unwrap_err();
assert!(
err.error.to_string().contains(
"'span_attributes.http.response.body.size' in table 'opentelemetry_traces' \
from String(\"\") to Int64"
),
"unexpected error message: {}",
err.error
);
prepare_trace_column_rewrites(&rows.rows, pending_rewrites, "opentelemetry_traces")
.unwrap()
.apply(&mut rows);
assert_eq!(rows.schema[0].datatype, ColumnDataType::Int64 as i32);
assert_eq!(rows.rows[0].values[0].value_data, None);
}
#[test]
+1
View File
@@ -25,6 +25,7 @@ pub use influxdb::{InfluxdbMergeMode, InfluxdbOptions};
pub use jaeger::JaegerOptions;
pub use mysql::MysqlOptions;
pub use opentsdb::OpentsdbOptions;
pub use operator::write_dispatcher::WriteRetryOptions;
pub use otlp::OtlpOptions;
pub use pending_rows_batcher::{BatcherOptions, PendingRowsBatcherOptions};
pub use postgres::PostgresOptions;
-2
View File
@@ -23,8 +23,6 @@ fst.workspace = true
futures.workspace = true
greptime-proto.workspace = true
itertools.workspace = true
jieba-rs = "0.10"
lazy_static.workspace = true
mockall.workspace = true
pin-project.workspace = true
prost.workspace = true
+2 -2
View File
@@ -31,7 +31,7 @@ use index::bloom_filter::reader::BloomFilterReaderImpl;
use index::external_provider::{ExternalTempFileProvider, Reader, Writer};
use index::fulltext_index::Config;
use index::fulltext_index::create::{BloomFilterFulltextIndexCreator, FulltextIndexCreator};
use index::fulltext_index::tokenizer::{Analyzer, EnglishTokenizer};
use index::fulltext_index::tokenizer::{Analyzer, ScriptTokenizer};
use index::inverted_index::create::InvertedIndexCreator;
use index::inverted_index::create::sort::external_sort::ExternalSorter;
use index::inverted_index::create::sort_create::SortIndexCreator;
@@ -365,7 +365,7 @@ fn bench_bloom_search(c: &mut Criterion) {
let mut group = c.benchmark_group("bloom_search");
let lines = service_logs(ROWS * 10);
let analyzer = Analyzer::new(Box::new(EnglishTokenizer), false);
let analyzer = Analyzer::new(Box::new(ScriptTokenizer), false);
let blob = rt.block_on(async {
let mut creator = bloom_creator();
for line in &lines {
+11 -11
View File
@@ -21,7 +21,7 @@ use async_trait::async_trait;
use criterion::{BatchSize, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use futures::AsyncRead;
use index::fulltext_index::create::{FulltextIndexCreator, TantivyFulltextIndexCreator};
use index::fulltext_index::tokenizer::{ChineseTokenizer, EnglishTokenizer, Tokenizer};
use index::fulltext_index::tokenizer::{ScriptTokenizer, Tokenizer};
use index::fulltext_index::{Analyzer, Config};
use puffin::puffin_manager::{PuffinWriter, PutOptions};
@@ -86,8 +86,8 @@ impl PuffinWriter for NoopPuffinWriter {
}
}
fn bench_english_tokenizer(c: &mut Criterion) {
let tokenizer = EnglishTokenizer;
fn bench_tokenizer_english_text(c: &mut Criterion) {
let tokenizer = ScriptTokenizer;
let texts = vec![
("short", "Hello, world! This is a test."),
@@ -125,7 +125,7 @@ fn bench_english_tokenizer(c: &mut Criterion) {
),
];
let mut group = c.benchmark_group("english_tokenizer");
let mut group = c.benchmark_group("tokenizer_english_text");
for (size, text) in texts {
group.bench_with_input(BenchmarkId::new("tokenize", size), &text, |b, text| {
@@ -136,7 +136,7 @@ fn bench_english_tokenizer(c: &mut Criterion) {
group.finish();
// Benchmark with repeated tokenization to simulate real-world usage
let mut repeat_group = c.benchmark_group("english_tokenizer_repeated");
let mut repeat_group = c.benchmark_group("tokenizer_english_text_repeated");
let sample_text = "The quick brown fox jumps over the lazy dog. This sentence contains most letters of the alphabet.";
@@ -157,9 +157,9 @@ fn bench_english_tokenizer(c: &mut Criterion) {
repeat_group.finish();
}
fn bench_chinese_tokenizer(c: &mut Criterion) {
let tokenizer = ChineseTokenizer;
let mut group = c.benchmark_group("chinese_tokenizer");
fn bench_tokenizer_chinese_text(c: &mut Criterion) {
let tokenizer = ScriptTokenizer;
let mut group = c.benchmark_group("tokenizer_chinese_text");
for (name, text) in CHINESE_TOKENIZER_TEXTS {
group.throughput(Throughput::Bytes(text.len() as u64));
@@ -170,7 +170,7 @@ fn bench_chinese_tokenizer(c: &mut Criterion) {
group.finish();
let mut repeat_group = c.benchmark_group("chinese_tokenizer_repeated");
let mut repeat_group = c.benchmark_group("tokenizer_chinese_text_repeated");
let sample_text = CHINESE_TOKENIZER_TEXTS
.iter()
.find(|(name, _)| *name == "mixed_log")
@@ -253,8 +253,8 @@ fn bench_tantivy_chinese_fulltext_index(c: &mut Criterion) {
criterion_group!(
benches,
bench_english_tokenizer,
bench_chinese_tokenizer,
bench_tokenizer_english_text,
bench_tokenizer_chinese_text,
bench_tantivy_chinese_fulltext_index
);
criterion_main!(benches);
+4 -2
View File
@@ -270,8 +270,10 @@ mod tests {
/// Converts a slice of bytes to a vector of `u64`.
pub fn u64_vec_from_bytes(bytes: &[u8]) -> Vec<u64> {
bytes
.chunks_exact(std::mem::size_of::<u64>())
.map(|chunk| u64::from_le_bytes(chunk.try_into().unwrap()))
.as_chunks::<{ std::mem::size_of::<u64>() }>()
.0
.iter()
.map(|chunk| u64::from_le_bytes(*chunk))
.collect()
}
@@ -29,7 +29,7 @@ use crate::fulltext_index::error::{
AbortedSnafu, BiErrorsSnafu, BloomFilterFinishSnafu, ExternalSnafu, PuffinAddBlobSnafu, Result,
SerializeToJsonSnafu,
};
use crate::fulltext_index::tokenizer::{Analyzer, ChineseTokenizer, EnglishTokenizer};
use crate::fulltext_index::tokenizer::{Analyzer, ScriptTokenizer};
use crate::fulltext_index::{Config, KEY_FULLTEXT_CONFIG};
const PIPE_BUFFER_SIZE_FOR_SENDING_BLOB: usize = 8192;
@@ -50,11 +50,9 @@ impl BloomFilterFulltextIndexCreator {
global_memory_usage: Arc<AtomicUsize>,
global_memory_usage_threshold: Option<usize>,
) -> Self {
let tokenizer = match config.analyzer {
crate::fulltext_index::Analyzer::English => Box::new(EnglishTokenizer) as _,
crate::fulltext_index::Analyzer::Chinese => Box::new(ChineseTokenizer) as _,
};
let analyzer = Analyzer::new(tokenizer, config.case_sensitive);
// Both analyzers tokenize the same way: query probes must be derivable from the
// term alone, which a context-dependent segmenter like jieba can't provide.
let analyzer = Analyzer::new(Box::new(ScriptTokenizer), config.case_sensitive);
let inner = BloomFilterCreator::new(
rows_per_segment,
+53 -539
View File
@@ -12,121 +12,32 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use common_base::term_token::{self, has_irregular_lowercase};
use crate::Bytes;
use crate::bloom_filter::element_hash;
use crate::fulltext_index::error::Result;
lazy_static::lazy_static! {
static ref JIEBA: jieba_rs::Jieba = jieba_rs::Jieba::new();
}
/// A-Z, a-z, 0-9, and '_' are true
const VALID_ASCII_TOKEN: [bool; 256] = [
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, true, true, true, true, true,
true, true, true, true, true, false, false, false, false, false, false, false, true, true,
true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
true, true, true, true, true, true, true, true, false, false, false, false, true, false, true,
true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
true, true, true, true, true, true, true, true, true, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
];
/// `Tokenizer` tokenizes a text into a list of tokens.
pub trait Tokenizer: Send {
fn tokenize<'a>(&self, text: &'a str) -> Vec<&'a str>;
}
/// `EnglishTokenizer` tokenizes an English text.
///
/// It splits the text by non-alphabetic characters.
/// `ScriptTokenizer` produces the tokens described in [`term_token::tokenize`].
#[derive(Debug, Default)]
pub struct EnglishTokenizer;
pub struct ScriptTokenizer;
impl Tokenizer for EnglishTokenizer {
impl Tokenizer for ScriptTokenizer {
fn tokenize<'a>(&self, text: &'a str) -> Vec<&'a str> {
if text.is_ascii() {
let mut tokens = Vec::new();
let mut start = 0;
for (i, &byte) in text.as_bytes().iter().enumerate() {
if !VALID_ASCII_TOKEN[byte as usize] {
if start < i {
tokens.push(&text[start..i]);
}
start = i + 1;
}
}
if start < text.len() {
tokens.push(&text[start..]);
}
tokens
} else {
text.split(|c: char| !c.is_alphanumeric() && c != '_')
.filter(|s| !s.is_empty())
.collect()
}
term_token::tokenize(text)
}
}
/// `ChineseTokenizer` tokenizes a Chinese text.
///
/// It uses Jieba search-mode tokenization to improve recall for Chinese fulltext search.
/// Enabling HMM also helps merge some unknown fragments into larger tokens, which can reduce
/// token cardinality versus a fully fragmented output.
#[derive(Debug, Default)]
pub struct ChineseTokenizer;
impl Tokenizer for ChineseTokenizer {
fn tokenize<'a>(&self, text: &'a str) -> Vec<&'a str> {
if text.is_ascii() {
EnglishTokenizer {}.tokenize(text)
} else {
// Search-mode tokenization emits finer-grained searchable terms, while HMM helps
// merge some unknown fragments and avoid excessive token fragmentation.
let mut tokens = JIEBA
.cut_for_search(text, true)
.into_iter()
.map(|token| token.word)
.filter(|token| is_indexable_token(token))
.collect::<Vec<_>>();
let english = EnglishTokenizer {};
tokens.extend(
english
.tokenize(text)
.into_iter()
.filter(|token| is_ascii_underscore_token(token)),
);
tokens
}
}
}
fn is_indexable_token(token: &str) -> bool {
token.chars().any(|c| c.is_alphanumeric() || c == '_')
}
fn is_ascii_underscore_token(token: &str) -> bool {
token.is_ascii() && token.chars().any(|c| c == '_')
}
/// `Analyzer` analyzes a text into a list of tokens.
///
/// It uses a `Tokenizer` to tokenize the text and optionally lowercases the tokens.
/// When case-insensitive, it also emits the tokens of the whole lowercased text if
/// they can differ, so that predicates on `lower(col)` can probe the index.
pub struct Analyzer {
tokenizer: Box<dyn Tokenizer>,
case_sensitive: bool,
@@ -152,22 +63,36 @@ impl Analyzer {
buf: &'a mut Vec<u8>,
) -> impl Iterator<Item = u64> + use<'a> {
let case_sensitive = self.case_sensitive;
self.tokenizer.tokenize(text).into_iter().map(move |token| {
if case_sensitive {
element_hash(token.as_bytes())
} else if token.is_ascii() {
buf.clear();
buf.extend(token.bytes().map(|b| b.to_ascii_lowercase()));
element_hash(buf)
} else {
element_hash(token.to_lowercase().as_bytes())
}
})
let lowercased_text_hashes = self
.irregular_lowercased_text(text)
.map(|lowered| {
self.tokenizer
.tokenize(&lowered)
.into_iter()
.map(|token| element_hash(token.as_bytes()))
.collect::<Vec<_>>()
})
.unwrap_or_default();
self.tokenizer
.tokenize(text)
.into_iter()
.map(move |token| {
if case_sensitive {
element_hash(token.as_bytes())
} else if token.is_ascii() {
buf.clear();
buf.extend(token.bytes().map(|b| b.to_ascii_lowercase()));
element_hash(buf)
} else {
element_hash(token.to_lowercase().as_bytes())
}
})
.chain(lowercased_text_hashes)
}
/// Analyzes the given text into a list of tokens.
pub fn analyze_text(&self, text: &str) -> Result<Vec<Bytes>> {
let res = self
let mut res = self
.tokenizer
.tokenize(text)
.iter()
@@ -178,9 +103,22 @@ impl Analyzer {
s.to_lowercase().as_bytes().to_vec()
}
})
.collect();
.collect::<Vec<_>>();
if let Some(lowered) = self.irregular_lowercased_text(text) {
res.extend(
self.tokenizer
.tokenize(&lowered)
.into_iter()
.map(|token| token.as_bytes().to_vec()),
);
}
Ok(res)
}
/// Returns the lowercased text whose tokens must be indexed too, if any.
fn irregular_lowercased_text(&self, text: &str) -> Option<String> {
(!self.case_sensitive && has_irregular_lowercase(text)).then(|| text.to_lowercase())
}
}
#[cfg(test)]
@@ -190,12 +128,8 @@ mod tests {
#[test]
fn test_analyze_text_hashes_matches_analyze_text() {
let text = "Hello, WORLD ship_Ship 清洁表面 ÄÖÜ straße İstanbul x";
for (tokenizer, case_sensitive) in [
(Box::new(EnglishTokenizer) as Box<dyn Tokenizer>, false),
(Box::new(EnglishTokenizer), true),
(Box::new(ChineseTokenizer), false),
] {
let analyzer = Analyzer::new(tokenizer, case_sensitive);
for case_sensitive in [false, true] {
let analyzer = Analyzer::new(Box::new(ScriptTokenizer), case_sensitive);
let expected = analyzer
.analyze_text(text)
.unwrap()
@@ -209,433 +143,13 @@ mod tests {
}
}
#[test]
fn test_english_tokenizer() {
let tokenizer = EnglishTokenizer;
let text = "Hello, world!!! This is a----++ test012_345+67890 ship_ship ship__ship _ __ __IDENTIFIER__ _ship ship_";
let tokens = tokenizer.tokenize(text);
assert_eq!(
tokens,
vec![
"Hello",
"world",
"This",
"is",
"a",
"test012_345",
"67890",
"ship_ship",
"ship__ship",
"_",
"__",
"__IDENTIFIER__",
"_ship",
"ship_"
]
);
}
#[test]
fn test_english_tokenizer_with_utf8() {
let tokenizer = EnglishTokenizer;
let text = "💸unfold the 纸巾😣and gently 清洁表😭面";
let tokens = tokenizer.tokenize(text);
assert_eq!(
tokens,
// Don't care what happens to non-ASCII characters.
// It's kind of a misconfiguration to use EnglishTokenizer on non-ASCII text.
vec!["unfold", "the", "纸巾", "and", "gently", "清洁表", "面"]
);
}
#[test]
fn test_chinese_tokenizer() {
let tokenizer = ChineseTokenizer;
let text = "我喜欢苹果";
let tokens = tokenizer.tokenize(text);
assert_eq!(tokens, vec!["我", "喜欢", "苹果"]);
}
#[test]
fn test_chinese_tokenizer_issue_7943_sample() {
let tokenizer = ChineseTokenizer;
let text = "[2026/04/09/ 13:56:11.031]2026-04-09 13:56:11.031 - [ trace_id=340a6a44b0bd8e37bb7697ss7da61ff0 span_id=085ff5ttf1e0a23b trace_flags=01] - [http-nio-8081-exec-16] INFO c.h.p.xx.web.service.impl.CCCXForwardKKKServiceImpl.pushout(188) - 登录手机号18888888888的动态key:829889AC8 ship_ship ship__ship _ __ __IDENTIFIER__ _ship ship_ EOF";
let tokens = tokenizer.tokenize(text);
assert_eq!(
tokens,
vec![
"2026",
"04",
"09",
"13",
"56",
"11.031",
"2026-04",
"09",
"13",
"56",
"11.031",
"trace",
"_",
"id",
"340a6a44b0bd8e37bb7697ss7da61ff0",
"span",
"_",
"id",
"085ff5ttf1e0a23b",
"trace",
"_",
"flags",
"01",
"http",
"nio-8081",
"exec-16",
"INFO",
"c",
"h",
"p",
"xx",
"web",
"service",
"impl",
"CCCXForwardKKKServiceImpl",
"pushout",
"188",
"登录",
"手机",
"手机号",
"18888888888",
"的",
"动态",
"key",
"829889AC8",
"ship",
"_",
"ship",
"ship",
"__",
"ship",
"_",
"__",
"__",
"IDENTIFIER",
"__",
"_",
"ship",
"ship",
"_",
"EOF",
"trace_id",
"span_id",
"trace_flags",
"ship_ship",
"ship__ship",
"_",
"__",
"__IDENTIFIER__",
"_ship",
"ship_"
]
);
}
#[test]
fn test_chinese_tokenizer_keeps_ascii_underscore_compounds() {
let tokenizer = ChineseTokenizer;
let text = "trace_id=abc 登录手机号 dynamic_key=xyz";
let tokens = tokenizer.tokenize(text);
assert!(tokens.contains(&"trace_id"));
assert!(tokens.contains(&"dynamic_key"));
assert!(tokens.contains(&"登录"));
assert!(tokens.contains(&"手机号"));
}
#[test]
fn test_chinese_tokenizer_skips_non_ascii_underscore_tokens() {
let tokenizer = ChineseTokenizer;
let text = "登录_id trace_id 手机号_trace";
let tokens = tokenizer.tokenize(text);
assert_eq!(
tokens,
[
"登录",
"_",
"id",
"trace",
"_",
"id",
"手机",
"手机号",
"_",
"trace",
"trace_id"
]
);
}
#[test]
fn test_chinese_tokenizer_aggressive_tokenization_probe() {
let tokenizer = ChineseTokenizer;
let text = "哈基米哦南北绿豆,噢马自立曼波。登录手机号。中国农业银行。装电视台,中国中央广播电视台。压不缩,笑不活。";
let default_tokens = tokenizer.tokenize(text);
let cut_hmm_false = JIEBA
.cut(text, false)
.into_iter()
.map(|token| token.word)
.collect::<Vec<_>>();
let cut_hmm_true = JIEBA
.cut(text, true)
.into_iter()
.map(|token| token.word)
.collect::<Vec<_>>();
let cut_for_search_hmm_false = JIEBA
.cut_for_search(text, false)
.into_iter()
.map(|token| token.word)
.collect::<Vec<_>>();
let cut_for_search_hmm_true = JIEBA
.cut_for_search(text, true)
.into_iter()
.map(|token| token.word)
.collect::<Vec<_>>();
assert_eq!(
default_tokens,
[
"哈基米",
"哦",
"南北",
"绿豆",
"噢",
"马",
"自立",
"曼波",
"登录",
"手机",
"手机号",
"中国",
"农业",
"银行",
"中国农业银行",
"装",
"电视",
"电视台",
"中国",
"中央",
"广播",
"电视",
"电视台",
"不缩",
"压不缩",
"笑",
"不活",
]
);
assert_eq!(
cut_hmm_false,
[
"哈",
"基",
"米",
"哦",
"南北",
"绿豆",
",",
"噢",
"马",
"自立",
"曼",
"波",
"。",
"登录",
"手机号",
"。",
"中国农业银行",
"。",
"装",
"电视台",
",",
"中国",
"中央",
"广播",
"电视台",
"。",
"压",
"不",
"缩",
",",
"笑",
"不",
"活",
"。"
]
);
assert_eq!(
cut_hmm_true,
[
"哈基米",
"哦",
"南北",
"绿豆",
",",
"噢",
"马",
"自立",
"曼波",
"。",
"登录",
"手机号",
"。",
"中国农业银行",
"。",
"装",
"电视台",
",",
"中国",
"中央",
"广播",
"电视台",
"。",
"压不缩",
",",
"笑",
"不活",
"。"
]
);
assert_eq!(
cut_for_search_hmm_false,
[
"哈",
"基",
"米",
"哦",
"南北",
"绿豆",
",",
"噢",
"马",
"自立",
"曼",
"波",
"。",
"登录",
"手机",
"手机号",
"。",
"中国",
"农业",
"银行",
"中国农业银行",
"。",
"装",
"电视",
"电视台",
",",
"中国",
"中央",
"广播",
"电视",
"电视台",
"。",
"压",
"不",
"缩",
",",
"笑",
"不",
"活",
"。"
]
);
assert_eq!(
cut_for_search_hmm_true,
[
"哈基米",
"哦",
"南北",
"绿豆",
",",
"噢",
"马",
"自立",
"曼波",
"。",
"登录",
"手机",
"手机号",
"。",
"中国",
"农业",
"银行",
"中国农业银行",
"。",
"装",
"电视",
"电视台",
",",
"中国",
"中央",
"广播",
"电视",
"电视台",
"。",
"不缩",
"压不缩",
",",
"笑",
"不活",
"。"
]
);
}
#[test]
fn test_valid_ascii_token_lookup_table() {
// Test all ASCII values in a single loop
for c in 0u8..=255u8 {
let is_valid = VALID_ASCII_TOKEN[c as usize];
let should_be_valid = (c as char).is_ascii_alphanumeric() || c == b'_';
assert_eq!(
is_valid,
should_be_valid,
"Character '{}' (byte {}) validity mismatch: expected {}, got {}",
if c.is_ascii() && !c.is_ascii_control() {
c as char
} else {
'?'
},
c,
should_be_valid,
is_valid
);
}
}
#[test]
fn test_analyzer() {
let tokenizer = EnglishTokenizer;
let analyzer = Analyzer::new(Box::new(tokenizer), false);
let text = "Hello, world! This is a test.";
let tokens = analyzer.analyze_text(text).unwrap();
let analyzer = Analyzer::new(Box::new(ScriptTokenizer), false);
let tokens = analyzer.analyze_text("Hello, World_Two").unwrap();
assert_eq!(
tokens,
vec![
b"hello".to_vec(),
b"world".to_vec(),
b"this".to_vec(),
b"is".to_vec(),
b"a".to_vec(),
b"test".to_vec()
]
vec![b"hello".to_vec(), b"world".to_vec(), b"two".to_vec()]
);
}
}
+4 -7
View File
@@ -158,13 +158,10 @@ impl RecordsBuffer {
impl RecordsBuffer {
fn pop_front(&mut self) -> Option<RecordAndOffset> {
while let Some(index) = self.index.peek() {
if let Some(record_and_offset) = self.buffer.pop_front() {
if index == record_and_offset.offset as u64 {
self.index.next();
return Some(record_and_offset);
}
} else {
return None;
let record_and_offset = self.buffer.pop_front()?;
if index == record_and_offset.offset as u64 {
self.index.next();
return Some(record_and_offset);
}
}
+19 -10
View File
@@ -19,7 +19,7 @@ use std::time::Duration;
use api::v1::meta::heartbeat_client::HeartbeatClient;
use api::v1::meta::{HeartbeatRequest, HeartbeatResponse, RequestHeader, Role};
use common_grpc::channel_manager::ChannelManager;
use common_meta::distributed_time_constants::BASE_HEARTBEAT_INTERVAL;
use common_meta::distributed_time_constants::{BASE_HEARTBEAT_INTERVAL, HEARTBEAT_TIMEOUT};
use common_meta::util;
use common_telemetry::tracing_context::TracingContext;
use common_telemetry::{info, warn};
@@ -249,17 +249,26 @@ impl Inner {
})?;
let receiver = ReceiverStream::new(receiver);
let mut stream = leader
.heartbeat(receiver)
.await
.map_err(error::Error::from)?
.into_inner();
let handshake = async {
let mut stream = leader
.heartbeat(receiver)
.await
.map_err(error::Error::from)?
.into_inner();
let res = stream
.message()
let res = stream
.message()
.await
.map_err(error::Error::from)?
.context(error::CreateHeartbeatStreamSnafu)?;
Ok::<_, error::Error>((stream, res))
};
let (stream, res) = tokio::time::timeout(HEARTBEAT_TIMEOUT, handshake)
.await
.map_err(error::Error::from)?
.context(error::CreateHeartbeatStreamSnafu)?;
.with_context(|_| error::HeartbeatHandshakeTimeoutSnafu {
timeout: HEARTBEAT_TIMEOUT,
})??;
let config = HeartbeatConfig::from_response(&res);
+11
View File
@@ -86,6 +86,15 @@ pub enum Error {
location: Location,
},
#[snafu(display("Heartbeat handshake timeout after {timeout:?}"))]
HeartbeatHandshakeTimeout {
timeout: std::time::Duration,
#[snafu(implicit)]
location: Location,
#[snafu(source)]
error: tokio::time::error::Elapsed,
},
#[snafu(display("Invalid response header"))]
InvalidResponseHeader {
#[snafu(implicit)]
@@ -150,6 +159,7 @@ impl ErrorExt for Error {
| Error::MissingQueryContext { .. }
| Error::SendHeartbeat { .. }
| Error::CreateHeartbeatStream { .. }
| Error::HeartbeatHandshakeTimeout { .. }
| Error::CreateChannel { .. }
| Error::RetryTimesExceeded { .. }
| Error::ConvertMetaConfig { .. } => StatusCode::Internal,
@@ -168,6 +178,7 @@ impl ErrorExt for Error {
fn retry_hint(&self) -> RetryHint {
match self {
Error::MetaServer { retry_hint, .. } => *retry_hint,
Error::HeartbeatHandshakeTimeout { .. } => RetryHint::Retryable,
Error::InvalidResponseHeader { source, .. }
| Error::ConvertMetaRequest { source, .. }
| Error::ConvertMetaResponse { source, .. }
@@ -0,0 +1,140 @@
// 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::pin::Pin;
use std::time::Duration;
use api::v1::meta::heartbeat_server::{Heartbeat, HeartbeatServer};
use api::v1::meta::{
AskLeaderRequest, AskLeaderResponse, HeartbeatRequest, HeartbeatResponse, Peer, ResponseHeader,
};
use common_error::ext::{ErrorExt, RetryHint};
use common_meta::distributed_time_constants::HEARTBEAT_TIMEOUT;
use futures::Stream;
use meta_client::error::Error;
use meta_client::{MetaClientOptions, MetaClientRef, MetaClientType, create_meta_client};
use tokio_stream::wrappers::TcpListenerStream;
use tonic::codec::CompressionEncoding;
use tonic::{Request, Response, Status, Streaming};
type HeartbeatStream =
Pin<Box<dyn Stream<Item = Result<HeartbeatResponse, Status>> + Send + 'static>>;
struct MockMetasrv {
addr: String,
send_first_heartbeat_response: bool,
}
#[async_trait::async_trait]
impl Heartbeat for MockMetasrv {
type HeartbeatStream = HeartbeatStream;
async fn heartbeat(
&self,
request: Request<Streaming<HeartbeatRequest>>,
) -> Result<Response<Self::HeartbeatStream>, Status> {
let send_first_heartbeat_response = self.send_first_heartbeat_response;
let stream = futures::stream::once(async move {
let mut request = request.into_inner();
request
.message()
.await?
.ok_or_else(|| Status::invalid_argument("missing heartbeat request"))?;
if send_first_heartbeat_response {
Ok(HeartbeatResponse::default())
} else {
futures::future::pending::<Result<HeartbeatResponse, Status>>().await
}
});
Ok(Response::new(Box::pin(stream)))
}
async fn ask_leader(
&self,
_request: Request<AskLeaderRequest>,
) -> Result<Response<AskLeaderResponse>, Status> {
Ok(Response::new(AskLeaderResponse {
header: Some(ResponseHeader::default()),
leader: Some(Peer {
id: 1,
addr: self.addr.clone(),
}),
}))
}
}
async fn create_client(send_first_heartbeat_response: bool) -> MetaClientRef {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let mock = MockMetasrv {
addr: addr.to_string(),
send_first_heartbeat_response,
};
tokio::spawn(async move {
let service = HeartbeatServer::new(mock)
.accept_compressed(CompressionEncoding::Gzip)
.accept_compressed(CompressionEncoding::Zstd)
.send_compressed(CompressionEncoding::Gzip)
.send_compressed(CompressionEncoding::Zstd);
tonic::transport::Server::builder()
.add_service(service)
.serve_with_incoming(TcpListenerStream::new(listener))
.await
.unwrap();
});
let options = MetaClientOptions {
metasrv_addrs: vec![addr.to_string()],
connect_timeout: Duration::from_millis(500),
..Default::default()
};
create_meta_client(
MetaClientType::Datanode { member_id: 1 },
&options,
None,
None,
)
.await
.expect("the mock answers leader discovery")
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_heartbeat_handshake_succeeds_when_the_server_responds() {
let client = create_client(true).await;
tokio::time::timeout(HEARTBEAT_TIMEOUT, client.heartbeat())
.await
.expect("heartbeat handshake must complete before its deadline")
.expect("heartbeat handshake must succeed");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_heartbeat_handshake_times_out_when_the_stream_stays_silent() {
let client = create_client(false).await;
let result = tokio::time::timeout(2 * HEARTBEAT_TIMEOUT, client.heartbeat())
.await
.expect("heartbeat handshake must be bounded");
let error = match result {
Ok(_) => panic!("a silent heartbeat stream must fail the handshake"),
Err(error) => error,
};
assert!(matches!(
&error,
Error::HeartbeatHandshakeTimeout { timeout, .. } if *timeout == HEARTBEAT_TIMEOUT
));
assert_eq!(error.retry_hint(), RetryHint::Retryable);
}
+5 -1
View File
@@ -38,7 +38,11 @@ abstraction, election, key encoding, and the DDL manager live in `common-meta`;
- **Heartbeat**: `service/heartbeat.rs` receives datanode/frontend heartbeats and
runs the handler chain in `handler/`; responses carry mailbox messages (DDL
results, cache invalidation).
results, cache invalidation). Node information is refreshed in memory on each
heartbeat. Datanode statistics collection and flownode remapping share
`handler/utils.rs` for per-node address persistence. Datanode address
updates retain the statistics handler's per-node ordering. Address epochs are
confirmed only after a successful save; cache invalidation remains best effort.
- **DDL**: `service/procedure.rs` (`ddl`) is leader-only and hands off to
`common-meta`'s `DdlManager`, executed via `common-procedure`.
- **Region migration**: `procedure/region_migration/manager.rs` plus per-step
+157 -37
View File
@@ -81,6 +81,7 @@ pub mod publish_heartbeat_handler;
pub mod region_lease_handler;
pub mod remap_flow_peer_handler;
pub mod response_header_handler;
mod utils;
#[cfg(test)]
pub mod test_utils;
@@ -223,18 +224,50 @@ impl Pusher {
}
#[inline]
pub async fn push(&self, res: HeartbeatResponse) -> Result<()> {
self.sender.send(Ok(res)).await.map_err(|e| {
error::PushMessageSnafu {
err_msg: e.to_string(),
}
.build()
})
pub fn header(&self) -> ResponseHeader {
self.res_header.clone()
}
}
/// An owned snapshot of the [`Pusher`] fields the push paths need, so a caller can drop
/// the [`Pushers`] read guard before awaiting the send. It must not hold a [`Pusher`]:
/// dropping one fires the deregister signal, which reads as a closed channel.
struct PusherHandle {
sender: Sender<std::result::Result<HeartbeatResponse, tonic::Status>>,
res_header: ResponseHeader,
deregister_signal_receiver: DeregisterSignalReceiver,
}
impl PusherHandle {
/// Snapshots `pusher`, which callers read under the [`Pushers`] guard.
fn from_pusher(pusher: &Pusher) -> Self {
Self {
sender: pusher.sender.clone(),
res_header: pusher.header(),
deregister_signal_receiver: pusher.deregister_signal_receiver.clone(),
}
}
/// Sends one mailbox message, consuming the handle.
async fn push(self, mailbox_message: MailboxMessage) -> Result<()> {
self.sender
.send(Ok(HeartbeatResponse {
header: Some(self.res_header),
mailbox_message: Some(mailbox_message),
..Default::default()
}))
.await
.map_err(|e| {
error::PushMessageSnafu {
err_msg: e.to_string(),
}
.build()
})
}
#[inline]
pub fn header(&self) -> ResponseHeader {
self.res_header.clone()
fn deregister_signal_receiver(&self) -> DeregisterSignalReceiver {
self.deregister_signal_receiver.clone()
}
}
@@ -248,41 +281,41 @@ impl Pushers {
pusher_id: PusherId,
mailbox_message: MailboxMessage,
) -> Result<DeregisterSignalReceiver> {
let pushers = self.0.read().await;
let pusher = pushers
.get(&pusher_id)
.with_context(|| error::PusherNotFoundSnafu {
pusher_id: pusher_id.to_string(),
})?;
// Releases the guard before the send below, which parks while the target's
// channel is full. `PusherNotFound` is still reported from inside the guard.
let handle = {
let pushers = self.0.read().await;
let pusher = pushers
.get(&pusher_id)
.with_context(|| error::PusherNotFoundSnafu {
pusher_id: pusher_id.to_string(),
})?;
PusherHandle::from_pusher(pusher)
};
pusher
.push(HeartbeatResponse {
header: Some(pusher.header()),
mailbox_message: Some(mailbox_message),
..Default::default()
})
.await?;
let deregister_signal_receiver = handle.deregister_signal_receiver();
handle.push(mailbox_message).await?;
Ok(pusher.deregister_signal_receiver.clone())
Ok(deregister_signal_receiver)
}
async fn broadcast(&self, role: Role, mailbox_message: &MailboxMessage) -> Result<()> {
let pushers = self.0.read().await;
let pushers = pushers
.range(PusherId::role_range(role))
.map(|(_, value)| value)
.collect::<Vec<_>>();
let mut results = Vec::with_capacity(pushers.len());
// Collects owned handles, releasing the guard before the sends below. Collecting
// `&Pusher` would hold it for the whole `join_all`.
let targets = {
let pushers = self.0.read().await;
pushers
.range(PusherId::role_range(role))
.map(|(_, pusher)| PusherHandle::from_pusher(pusher))
.collect::<Vec<_>>()
};
for pusher in pushers {
let mut results = Vec::with_capacity(targets.len());
for handle in targets {
let mut mailbox_message = mailbox_message.clone();
mailbox_message.id = 0; // one-way message
results.push(pusher.push(HeartbeatResponse {
header: Some(pusher.header()),
mailbox_message: Some(mailbox_message),
..Default::default()
}))
results.push(handle.push(mailbox_message));
}
// Checks the error out of the loop.
@@ -923,14 +956,15 @@ mod tests {
use std::sync::Arc;
use std::time::Duration;
use api::v1::meta::{HeartbeatRequest, MailboxMessage, RequestHeader, Role};
use api::v1::meta::{HeartbeatRequest, HeartbeatResponse, MailboxMessage, RequestHeader, Role};
use common_meta::kv_backend::memory::MemoryKvBackend;
use common_meta::sequence::SequenceBuilder;
use futures::future::poll_immediate;
use tokio::sync::mpsc;
use super::{
HandleControl, HeartbeatAccumulator, HeartbeatHandler, HeartbeatHandlerGroupBuilder,
PusherId, Pushers,
PusherHandle, PusherId, Pushers,
};
use crate::error;
use crate::handler::collect_stats_handler::CollectStatsHandler;
@@ -1396,4 +1430,90 @@ mod tests {
let extensions = res.extensions;
assert_eq!(extensions.get("custom.key").unwrap(), b"custom-value");
}
async fn full_pusher(
pushers: &Pushers,
id: PusherId,
) -> mpsc::Receiver<std::result::Result<HeartbeatResponse, tonic::Status>> {
let (tx, rx) = mpsc::channel(1);
tx.try_send(Ok(HeartbeatResponse::default())).unwrap();
pushers.insert(id, Pusher::new(tx)).await;
// Callers keep `rx` alive: dropping it would make the send fail instead of park.
rx
}
fn test_mailbox_message(subject: &str) -> MailboxMessage {
MailboxMessage {
id: 0,
subject: subject.to_string(),
timestamp_millis: 123,
..Default::default()
}
}
#[tokio::test]
async fn test_pushers_push_does_not_fence_the_registry() {
let pushers = Pushers::default();
let full_id = PusherId::new(Role::Datanode, 1);
let _full_rx = full_pusher(&pushers, full_id).await;
let (healthy_tx, mut healthy_rx) = mpsc::channel(1);
let healthy_id = PusherId::new(Role::Datanode, 2);
pushers.insert(healthy_id, Pusher::new(healthy_tx)).await;
let msg = test_mailbox_message("fence-test");
let mut parked = Box::pin(pushers.push(full_id, msg.clone()));
assert!(poll_immediate(parked.as_mut()).await.is_none());
let (writer_tx, _writer_rx) = mpsc::channel(1);
let mut writer =
Box::pin(pushers.insert(PusherId::new(Role::Datanode, 3), Pusher::new(writer_tx)));
assert!(poll_immediate(writer.as_mut()).await.is_some());
let mut healthy = Box::pin(pushers.push(healthy_id, msg));
assert!(poll_immediate(healthy.as_mut()).await.is_some());
assert!(healthy_rx.try_recv().is_ok());
}
#[tokio::test]
async fn test_pushers_broadcast_does_not_fence_the_registry() {
let pushers = Pushers::default();
let _full_rx = full_pusher(&pushers, PusherId::new(Role::Datanode, 1)).await;
let (healthy_tx, _healthy_rx) = mpsc::channel(1);
pushers
.insert(PusherId::new(Role::Datanode, 2), Pusher::new(healthy_tx))
.await;
let msg = test_mailbox_message("broadcast-fence-test");
let mut parked = Box::pin(pushers.broadcast(Role::Datanode, &msg));
assert!(poll_immediate(parked.as_mut()).await.is_none());
let (writer_tx, _writer_rx) = mpsc::channel(1);
let mut writer =
Box::pin(pushers.insert(PusherId::new(Role::Flownode, 1), Pusher::new(writer_tx)));
assert!(poll_immediate(writer.as_mut()).await.is_some());
}
#[tokio::test]
async fn test_pusher_handle_does_not_fire_the_deregister_signal() {
let (tx, _rx) = mpsc::channel(1);
let pusher = Pusher::new(tx);
let mut signal = pusher.deregister_signal_receiver.clone();
let handle = PusherHandle::from_pusher(&pusher);
drop(handle);
assert!(
tokio::time::timeout(Duration::from_millis(100), signal.changed())
.await
.is_err()
);
// The real `Pusher` still owns the signal.
drop(pusher);
assert!(signal.changed().await.is_ok());
}
}
@@ -46,30 +46,21 @@ impl HeartbeatHandler for CollectFrontendClusterInfoHandler {
ctx: &mut Context,
_acc: &mut HeartbeatAccumulator,
) -> Result<HandleControl> {
let Some((key, peer, info, env_vars)) = extract_base_info(req) else {
let Some(update) = NodeInfoUpdate::from_request(req) else {
return Ok(HandleControl::Continue);
};
let frontend_workloads = get_frontend_workloads(req.node_workloads.as_ref());
let value = NodeInfo {
peer,
last_activity_ts: common_time::util::current_time_millis(),
status: NodeStatus::Frontend(FrontendStatus {
workloads: frontend_workloads,
}),
version: info.version,
git_commit: info.git_commit,
start_time_ms: info.start_time_ms,
total_cpu_millicores: info.total_cpu_millicores,
total_memory_bytes: info.total_memory_bytes,
cpu_usage_millicores: info.cpu_usage_millicores,
memory_usage_bytes: info.memory_usage_bytes,
hostname: info.hostname,
env_vars,
};
put_into_memory_store(ctx, key, value).await?;
update
.save_to_memory(
ctx,
common_time::util::current_time_millis(),
NodeStatus::Frontend(FrontendStatus {
workloads: frontend_workloads,
}),
)
.await?;
Ok(HandleControl::Continue)
}
@@ -89,29 +80,20 @@ impl HeartbeatHandler for CollectFlownodeClusterInfoHandler {
ctx: &mut Context,
_acc: &mut HeartbeatAccumulator,
) -> Result<HandleControl> {
let Some((key, peer, info, env_vars)) = extract_base_info(req) else {
let Some(update) = NodeInfoUpdate::from_request(req) else {
return Ok(HandleControl::Continue);
};
let flownode_workloads = get_flownode_workloads(req.node_workloads.as_ref());
let value = NodeInfo {
peer,
last_activity_ts: common_time::util::current_time_millis(),
status: NodeStatus::Flownode(FlownodeStatus {
workloads: flownode_workloads,
}),
version: info.version,
git_commit: info.git_commit,
start_time_ms: info.start_time_ms,
total_cpu_millicores: info.total_cpu_millicores,
total_memory_bytes: info.total_memory_bytes,
cpu_usage_millicores: info.cpu_usage_millicores,
memory_usage_bytes: info.memory_usage_bytes,
hostname: info.hostname,
env_vars,
};
put_into_memory_store(ctx, key, value).await?;
update
.save_to_memory(
ctx,
common_time::util::current_time_millis(),
NodeStatus::Flownode(FlownodeStatus {
workloads: flownode_workloads,
}),
)
.await?;
Ok(HandleControl::Continue)
}
@@ -132,7 +114,7 @@ impl HeartbeatHandler for CollectDatanodeClusterInfoHandler {
ctx: &mut Context,
acc: &mut HeartbeatAccumulator,
) -> Result<HandleControl> {
let Some((key, peer, info, env_vars)) = extract_base_info(req) else {
let Some(update) = NodeInfoUpdate::from_request(req) else {
return Ok(HandleControl::Continue);
};
@@ -147,16 +129,64 @@ impl HeartbeatHandler for CollectDatanodeClusterInfoHandler {
.count();
let follower_regions = stat.region_stats.len() - leader_regions;
let value = NodeInfo {
update
.save_to_memory(
ctx,
stat.timestamp_millis,
NodeStatus::Datanode(DatanodeStatus {
rcus: stat.rcus,
wcus: stat.wcus,
leader_regions,
follower_regions,
workloads: stat.datanode_workloads.clone(),
}),
)
.await?;
Ok(HandleControl::Continue)
}
}
/// Common fields collected for one heartbeat's in-memory node information
/// update. Persistent routing addresses have a separate lifecycle.
struct NodeInfoUpdate {
key: NodeInfoKey,
peer: Peer,
info: PbNodeInfo,
env_vars: HashMap<String, String>,
}
impl NodeInfoUpdate {
fn from_request(request: &HeartbeatRequest) -> Option<Self> {
let key = NodeInfoKey::new(request)?;
let peer = request.peer.clone()?;
let info = request.info.clone()?;
let env_vars = EnvVars::from_extensions(&request.extensions)
.inspect_err(|e| {
warn!(e; "Failed to deserialize __env_vars from heartbeat extensions, peer: {}", peer);
})
.unwrap_or_default()
.map(|e| e.vars)
.unwrap_or_default();
Some(Self {
key,
peer,
last_activity_ts: stat.timestamp_millis,
status: NodeStatus::Datanode(DatanodeStatus {
rcus: stat.rcus,
wcus: stat.wcus,
leader_regions,
follower_regions,
workloads: stat.datanode_workloads.clone(),
}),
info,
env_vars,
})
}
async fn save_to_memory(
self,
ctx: &Context,
last_activity_ts: i64,
status: NodeStatus,
) -> Result<()> {
let info = self.info;
let value = NodeInfo {
peer: self.peer,
last_activity_ts,
status,
version: info.version,
git_commit: info.git_commit,
start_time_ms: info.start_time_ms,
@@ -165,53 +195,88 @@ impl HeartbeatHandler for CollectDatanodeClusterInfoHandler {
cpu_usage_millicores: info.cpu_usage_millicores,
memory_usage_bytes: info.memory_usage_bytes,
hostname: info.hostname,
env_vars,
env_vars: self.env_vars,
};
put_into_memory_store(ctx, key, value).await?;
Ok(HandleControl::Continue)
let value = value.try_into().context(InvalidClusterInfoFormatSnafu)?;
ctx.in_memory
.put(PutRequest {
key: (&self.key).into(),
value,
..Default::default()
})
.await
.context(SaveClusterInfoSnafu)?;
Ok(())
}
}
fn extract_base_info(
request: &HeartbeatRequest,
) -> Option<(NodeInfoKey, Peer, PbNodeInfo, HashMap<String, String>)> {
let HeartbeatRequest { peer, info, .. } = request;
let key = NodeInfoKey::new(request)?;
let Some(peer) = &peer else {
return None;
};
let Some(info) = &info else {
return None;
};
#[cfg(test)]
mod tests {
use api::v1::meta::RequestHeader;
use common_meta::datanode::Stat;
let env_vars = EnvVars::from_extensions(&request.extensions)
.inspect_err(|e| {
warn!(e;
"Failed to deserialize __env_vars from heartbeat extensions, peer: {}", peer
use super::*;
use crate::handler::test_utils::TestEnv;
#[tokio::test]
async fn test_node_info_refreshes_without_epoch_change() {
let mut ctx = TestEnv::new().ctx();
let handlers: Vec<(Role, Box<dyn HeartbeatHandler>)> = vec![
(Role::Frontend, Box::new(CollectFrontendClusterInfoHandler)),
(Role::Datanode, Box::new(CollectDatanodeClusterInfoHandler)),
(Role::Flownode, Box::new(CollectFlownodeClusterInfoHandler)),
];
for (role, handler) in handlers {
let mut req = HeartbeatRequest {
header: Some(RequestHeader {
role: role as i32,
..Default::default()
}),
peer: Some(Peer {
id: 7,
addr: "node:3001".into(),
}),
info: Some(PbNodeInfo {
version: "v1".into(),
hostname: "host".into(),
..Default::default()
}),
node_epoch: 10,
..Default::default()
};
let mut acc = HeartbeatAccumulator {
stat: Some(Stat {
timestamp_millis: 123,
rcus: 5,
wcus: 6,
..Default::default()
}),
..Default::default()
};
handler.handle(&req, &mut ctx, &mut acc).await.unwrap();
let key: Vec<u8> = (&NodeInfoKey::new(&req).unwrap()).into();
let first =
NodeInfo::try_from(ctx.in_memory.get(&key).await.unwrap().unwrap().value).unwrap();
assert_eq!(first.peer, req.peer.clone().unwrap());
assert_eq!(first.version, "v1");
assert_eq!(first.hostname, "host");
req.info.as_mut().unwrap().version = "v2".into();
req.info.as_mut().unwrap().memory_usage_bytes = 42;
acc.stat.as_mut().unwrap().timestamp_millis = 456;
handler.handle(&req, &mut ctx, &mut acc).await.unwrap();
let second =
NodeInfo::try_from(ctx.in_memory.get(&key).await.unwrap().unwrap().value).unwrap();
assert_eq!(second.version, "v2");
assert_eq!(second.memory_usage_bytes, 42);
assert_eq!(
serde_json::to_value(&second.status).unwrap(),
serde_json::to_value(&first.status).unwrap()
);
})
.unwrap_or_default()
.map(|e| e.vars)
.unwrap_or_default();
Some((key, peer.clone(), info.clone(), env_vars))
}
async fn put_into_memory_store(ctx: &mut Context, key: NodeInfoKey, value: NodeInfo) -> Result<()> {
let key = (&key).into();
let value = value.try_into().context(InvalidClusterInfoFormatSnafu)?;
let put_req = PutRequest {
key,
value,
..Default::default()
};
ctx.in_memory
.put(put_req)
.await
.context(SaveClusterInfoSnafu)?;
Ok(())
if role == Role::Datanode {
assert_eq!(second.last_activity_ts, 456);
}
// Node information must not be persisted to the configured backend.
assert!(ctx.kv_backend.get(&key).await.unwrap().is_none());
}
}
}
+158 -53
View File
@@ -15,19 +15,19 @@
use std::cmp::Ordering;
use std::sync::Arc;
use api::v1::meta::{HeartbeatRequest, Role};
use api::v1::meta::{HeartbeatRequest, Peer, Role};
use common_meta::datanode::{DatanodeStatKey, DatanodeStatValue, Stat};
use common_meta::instruction::CacheIdent;
use common_meta::key::node_address::{NodeAddressKey, NodeAddressValue};
use common_meta::key::{MetadataKey, MetadataValue};
use common_meta::peer::Peer;
use common_meta::key::MetadataKey;
use common_meta::key::node_address::NodeAddressKey;
use common_meta::rpc::store::PutRequest;
use common_telemetry::{error, info, warn};
use common_telemetry::{error, warn};
use dashmap::DashMap;
use snafu::ResultExt;
use tokio::sync::Mutex;
use crate::error::{self, Result};
use crate::handler::utils::{self, NodeAddressUpdater};
use crate::handler::{HandleControl, HeartbeatAccumulator, HeartbeatHandler};
use crate::metasrv::Context;
@@ -40,7 +40,7 @@ struct EpochStats {
impl EpochStats {
#[inline]
fn drain_all(&mut self) -> Vec<Stat> {
self.stats.drain(..).collect()
std::mem::take(&mut self.stats)
}
#[inline]
@@ -74,6 +74,7 @@ const DEFAULT_FLUSH_STATS_FACTOR: usize = 3;
pub struct CollectStatsHandler {
stats_cache: DashMap<DatanodeStatKey, Arc<Mutex<EpochStats>>>,
flush_stats_factor: usize,
address_updater: NodeAddressUpdater,
}
impl Default for CollectStatsHandler {
@@ -87,6 +88,7 @@ impl CollectStatsHandler {
Self {
flush_stats_factor: flush_stats_factor.unwrap_or(DEFAULT_FLUSH_STATS_FACTOR),
stats_cache: DashMap::default(),
address_updater: NodeAddressUpdater::default(),
}
}
}
@@ -119,6 +121,7 @@ impl HeartbeatHandler for CollectStatsHandler {
let key: Vec<u8> = key.into();
let current_epoch = current_stat.node_epoch;
let refresh = if let Some(epoch) = epoch_stats.epoch() {
match current_stat.node_epoch.cmp(&epoch) {
Ordering::Greater => {
@@ -146,11 +149,13 @@ impl HeartbeatHandler for CollectStatsHandler {
true
};
// Need to refresh the [datanode -> address] mapping
if refresh {
// Safety: `epoch_stats.stats` is not empty
let last = epoch_stats.stats.last().unwrap();
rewrite_node_address(ctx, last).await;
// Keep address updates and statistics in the original per-node order.
// Address acknowledgement is independent of the statistics epoch so a
// failed save can be retried by the next heartbeat of the same epoch.
if epoch_stats.epoch() == Some(current_epoch)
&& let Some(stat) = epoch_stats.stats.last()
{
update_datanode_address(&self.address_updater, ctx, stat).await;
}
if !refresh && epoch_stats.len() < self.flush_stats_factor {
@@ -178,48 +183,26 @@ impl HeartbeatHandler for CollectStatsHandler {
}
}
async fn rewrite_node_address(ctx: &Context, stat: &Stat) {
let peer = Peer {
id: stat.id,
addr: stat.addr.clone(),
};
let key = NodeAddressKey::with_datanode(peer.id).to_bytes();
if let Ok(value) = NodeAddressValue::new(peer.clone()).try_as_raw_value() {
let put = PutRequest {
key,
value,
prev_kv: false,
};
match ctx.leader_cached_kv_backend.put(put).await {
Ok(_) => {
info!(
"Successfully updated datanode `NodeAddressValue`: {:?}",
peer
);
// broadcast invalidating cache
let cache_idents = stat
.table_ids()
.into_iter()
.map(CacheIdent::TableId)
.collect::<Vec<_>>();
if let Err(e) = ctx
.cache_invalidator
.invalidate(&Default::default(), &cache_idents)
.await
{
error!(e; "Failed to invalidate {} `NodeAddressKey` cache, peer: {:?}", cache_idents.len(), peer);
}
}
Err(e) => {
error!(e; "Failed to update datanode `NodeAddressValue`: {:?}", peer);
}
}
} else {
warn!(
"Failed to serialize datanode `NodeAddressValue`: {:?}",
peer
);
/// Applies datanode-specific address and table-cache updates. The caller keeps
/// its statistics lock so notifications and statistics retain their ordering.
async fn update_datanode_address(updater: &NodeAddressUpdater, ctx: &Context, stat: &Stat) {
let updated = updater
.update_if_needed((Role::Datanode, stat.id), stat.node_epoch, || async {
let peer = Peer { id: stat.id, addr: stat.addr.clone() };
let key = NodeAddressKey::with_datanode(stat.id).to_bytes();
utils::save_node_address(ctx, key, peer).await
})
.await
.inspect_err(|e| {
error!(e; "Failed to update datanode address, node: {}, address: {}", stat.id, stat.addr);
});
if let Ok(true) = updated {
let cache_idents = stat
.table_ids()
.into_iter()
.map(CacheIdent::TableId)
.collect::<Vec<_>>();
utils::invalidate_address_caches(ctx, stat.id, &cache_idents).await;
}
}
@@ -553,4 +536,126 @@ mod tests {
handler.handle(&req, &mut ctx, &mut acc).await.unwrap();
}
}
#[tokio::test]
async fn test_persistence_failure_retries_but_notification_failure_does_not() {
use crate::handler::collect_stats_handler::CollectStatsHandler;
use crate::handler::test_utils::FailingCacheInvalidator;
use crate::handler::{HandleControl, HeartbeatAccumulator, HeartbeatHandler};
use crate::service::store::cached_kv::LeaderCachedKvBackend;
use api::v1::meta::HeartbeatRequest;
use common_meta::key::MetadataValue;
use common_meta::key::node_address::NodeAddressValue;
use common_meta::kv_backend::test_util::MockKvBackendBuilder;
use common_meta::rpc::store::PutResponse;
let mut ctx = TestEnv::new().ctx();
let puts = Arc::new(std::sync::Mutex::new(Vec::<PutRequest>::new()));
let backend = MockKvBackendBuilder::default()
.put_fn(Arc::new({
let puts = puts.clone();
move |req: PutRequest| {
let mut puts = puts.lock().unwrap();
puts.push(req);
if puts.len() == 1 {
return common_meta::error::UnexpectedSnafu {
err_msg: "injected persistence failure",
}
.fail();
}
Ok(PutResponse::default())
}
}) as _)
.build()
.unwrap();
ctx.leader_cached_kv_backend =
Arc::new(LeaderCachedKvBackend::with_always_leader(Arc::new(backend)));
let invalidator = Arc::new(FailingCacheInvalidator::default());
ctx.cache_invalidator = invalidator.clone();
let handler = CollectStatsHandler::default();
let mut stat = Stat {
id: 101,
addr: "dn-original".into(),
node_epoch: 7,
..Default::default()
};
let req = HeartbeatRequest::default();
// A backend error must not interrupt subsequent heartbeat handlers.
assert!(matches!(
handler
.handle(
&req,
&mut ctx,
&mut HeartbeatAccumulator {
stat: Some(stat.clone()),
..Default::default()
}
)
.await
.unwrap(),
HandleControl::Continue
));
assert_eq!(puts.lock().unwrap().len(), 1);
assert_eq!(invalidator.attempts(), 0);
let stats_key: Vec<u8> = common_meta::datanode::DatanodeStatKey { node_id: 101 }.into();
assert!(ctx.in_memory.get(&stats_key).await.unwrap().is_some());
// Same epoch retries persistence using the current heartbeat's address.
stat.addr = "dn-retry".into();
handler
.handle(
&req,
&mut ctx,
&mut HeartbeatAccumulator {
stat: Some(stat.clone()),
..Default::default()
},
)
.await
.unwrap();
{
let puts = puts.lock().unwrap();
assert_eq!(puts.len(), 2);
assert_eq!(puts[1].key, NodeAddressKey::with_datanode(101).to_bytes());
assert_eq!(
NodeAddressValue::try_from_raw_value(&puts[1].value)
.unwrap()
.peer
.addr,
"dn-retry"
);
}
assert_eq!(invalidator.attempts(), 1);
// Notification remains best-effort: its failure neither rewrites nor retries.
handler
.handle(
&req,
&mut ctx,
&mut HeartbeatAccumulator {
stat: Some(stat.clone()),
..Default::default()
},
)
.await
.unwrap();
assert_eq!(puts.lock().unwrap().len(), 2);
assert_eq!(invalidator.attempts(), 1);
stat.node_epoch = 8;
handler
.handle(
&req,
&mut ctx,
&mut HeartbeatAccumulator {
stat: Some(stat.clone()),
..Default::default()
},
)
.await
.unwrap();
assert_eq!(puts.lock().unwrap().len(), 3);
assert_eq!(invalidator.attempts(), 2);
}
}
+80 -37
View File
@@ -52,9 +52,10 @@ impl HeartbeatHandler for RegionFailureHandler {
return Ok(HandleControl::Continue);
};
// Never blocks: the region lease has already been decided earlier in the
// chain, so a busy supervisor must not withhold the heartbeat response.
self.heartbeat_acceptor
.accept(DatanodeHeartbeat::from(stat))
.await;
.accept(DatanodeHeartbeat::from(stat));
Ok(HandleControl::Continue)
}
@@ -62,6 +63,8 @@ impl HeartbeatHandler for RegionFailureHandler {
#[cfg(test)]
mod tests {
use std::time::Duration;
use api::v1::meta::HeartbeatRequest;
use common_catalog::consts::default_engine;
use common_meta::datanode::{RegionManifestInfo, RegionStat, Stat};
@@ -73,7 +76,45 @@ mod tests {
use crate::handler::{HeartbeatAccumulator, HeartbeatHandler};
use crate::metasrv::builder::MetasrvBuilder;
use crate::region::supervisor::tests::new_test_supervisor;
use crate::region::supervisor::{Event, HeartbeatAcceptor};
use crate::region::supervisor::{DatanodeHeartbeat, Event, HeartbeatAcceptor};
fn new_region_stat(region_id: u64) -> RegionStat {
RegionStat {
id: RegionId::from_u64(region_id),
rcus: 0,
wcus: 0,
approximate_bytes: 0,
engine: default_engine().to_string(),
role: RegionRole::Follower,
num_rows: 0,
memtable_size: 0,
manifest_size: 0,
sst_size: 0,
sst_num: 0,
index_size: 0,
region_manifest: RegionManifestInfo::Mito {
manifest_version: 0,
flushed_entry_id: 0,
file_removed_cnt: 0,
},
data_topic_latest_entry_id: 0,
metadata_topic_latest_entry_id: 0,
written_bytes: 0,
query_cpu_time: 0,
query_scanned_bytes: 0,
min_timestamp: None,
max_timestamp: None,
}
}
fn new_stat() -> Stat {
Stat {
id: 42,
region_stats: vec![new_region_stat(1), new_region_stat(2), new_region_stat(3)],
timestamp_millis: 1000,
..Default::default()
}
}
#[tokio::test]
async fn test_handle_heartbeat() {
@@ -85,40 +126,7 @@ mod tests {
let metasrv = builder.build().await.unwrap();
let mut ctx = metasrv.new_ctx();
let acc = &mut HeartbeatAccumulator::default();
fn new_region_stat(region_id: u64) -> RegionStat {
RegionStat {
id: RegionId::from_u64(region_id),
rcus: 0,
wcus: 0,
approximate_bytes: 0,
engine: default_engine().to_string(),
role: RegionRole::Follower,
num_rows: 0,
memtable_size: 0,
manifest_size: 0,
sst_size: 0,
sst_num: 0,
index_size: 0,
region_manifest: RegionManifestInfo::Mito {
manifest_version: 0,
flushed_entry_id: 0,
file_removed_cnt: 0,
},
data_topic_latest_entry_id: 0,
metadata_topic_latest_entry_id: 0,
written_bytes: 0,
query_cpu_time: 0,
query_scanned_bytes: 0,
min_timestamp: None,
max_timestamp: None,
}
}
acc.stat = Some(Stat {
id: 42,
region_stats: vec![new_region_stat(1), new_region_stat(2), new_region_stat(3)],
timestamp_millis: 1000,
..Default::default()
});
acc.stat = Some(new_stat());
handler.handle(req, &mut ctx, acc).await.unwrap();
let (tx, rx) = oneshot::channel();
@@ -126,4 +134,39 @@ mod tests {
let detector = rx.await.unwrap();
assert_eq!(detector.iter().collect::<Vec<_>>().len(), 3);
}
#[tokio::test]
async fn test_handle_heartbeat_does_not_block_on_full_supervisor_queue() {
// Regression test for #9419: a full queue used to park the handler.
// Keep the supervisor alive, or the queue reports Closed.
let (_supervisor, sender) = new_test_supervisor();
// Built directly, so the supervisor stays undrained.
let handler = RegionFailureHandler {
heartbeat_acceptor: HeartbeatAcceptor::new(sender.clone()),
};
let stat = new_stat();
let mut queued = 0;
while sender
.try_send(Event::HeartbeatArrived(DatanodeHeartbeat::from(&stat)))
.is_ok()
{
queued += 1;
}
assert_eq!(queued, sender.max_capacity());
let builder = MetasrvBuilder::new();
let metasrv = builder.build().await.unwrap();
let mut ctx = metasrv.new_ctx();
let acc = &mut HeartbeatAccumulator::default();
// Without a stat the handler returns before the acceptor.
acc.stat = Some(stat);
let result = tokio::time::timeout(
Duration::from_secs(5),
handler.handle(&HeartbeatRequest::default(), &mut ctx, acc),
)
.await;
assert!(result.is_ok());
}
}
@@ -12,22 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use api::v1::meta::{HeartbeatRequest, Peer, Role};
use api::v1::meta::{HeartbeatRequest, Role};
use common_meta::instruction::CacheIdent;
use common_meta::key::node_address::{NodeAddressKey, NodeAddressValue};
use common_meta::key::{MetadataKey, MetadataValue};
use common_meta::rpc::store::PutRequest;
use common_telemetry::{error, info, warn};
use dashmap::DashMap;
use common_meta::key::MetadataKey;
use common_meta::key::node_address::NodeAddressKey;
use common_telemetry::error;
use crate::Result;
use crate::handler::utils::{self, NodeAddressUpdater};
use crate::handler::{HandleControl, HeartbeatAccumulator, HeartbeatHandler};
use crate::metasrv::Context;
#[derive(Debug, Default)]
pub struct RemapFlowPeerHandler {
/// flow_node_id -> epoch
epoch_cache: DashMap<u64, u64>,
address_updater: NodeAddressUpdater,
}
#[async_trait::async_trait]
@@ -45,61 +43,83 @@ impl HeartbeatHandler for RemapFlowPeerHandler {
let Some(peer) = req.peer.as_ref() else {
return Ok(HandleControl::Continue);
};
let current_epoch = req.node_epoch;
let flow_node_id = peer.id;
let refresh = if let Some(mut epoch) = self.epoch_cache.get_mut(&flow_node_id) {
if current_epoch > *epoch.value() {
*epoch.value_mut() = current_epoch;
true
} else {
false
}
} else {
self.epoch_cache.insert(flow_node_id, current_epoch);
true
};
if refresh {
rewrite_node_address(ctx, peer).await;
let updated = self
.address_updater
.update_if_needed((Role::Flownode, peer.id), req.node_epoch, || {
let key = NodeAddressKey::with_flownode(peer.id).to_bytes();
utils::save_node_address(ctx, key, peer.clone())
})
.await
.inspect_err(|e| {
// Address errors must not prevent later heartbeat handlers from running.
error!(e; "Failed to update flownode address, peer: {:?}", peer);
});
if let Ok(true) = updated {
let cache_idents = [CacheIdent::FlowNodeAddressChange(peer.id)];
utils::invalidate_address_caches(ctx, peer.id, &cache_idents).await;
}
Ok(HandleControl::Continue)
}
}
async fn rewrite_node_address(ctx: &mut Context, peer: &Peer) {
let key = NodeAddressKey::with_flownode(peer.id).to_bytes();
if let Ok(value) = NodeAddressValue::new(peer.clone()).try_as_raw_value() {
let put = PutRequest {
key,
value,
prev_kv: false,
};
#[cfg(test)]
mod tests {
use super::*;
use crate::handler::test_utils::TestEnv;
use api::v1::meta::Peer;
use std::sync::Arc;
#[tokio::test]
async fn test_notification_failure_keeps_persisted_epoch() {
use crate::handler::{HeartbeatAccumulator, HeartbeatHandler};
use api::v1::meta::HeartbeatRequest;
match ctx.leader_cached_kv_backend.put(put).await {
Ok(_) => {
info!("Successfully updated flow `NodeAddressValue`: {:?}", peer);
// broadcast invalidating cache to all frontends
let cache_idents = vec![CacheIdent::FlowNodeAddressChange(peer.id)];
info!(
"Invalidate flow node cache for new address with cache idents: {:?}",
cache_idents
);
if let Err(e) = ctx
.cache_invalidator
.invalidate(&Default::default(), &cache_idents)
.await
{
error!(e; "Failed to invalidate {} `NodeAddressKey` cache, peer: {:?}", cache_idents.len(), peer);
}
}
Err(e) => {
error!(e; "Failed to update flow `NodeAddressValue`: {:?}", peer);
}
}
} else {
warn!("Failed to serialize flow `NodeAddressValue`: {:?}", peer);
use crate::handler::test_utils::FailingCacheInvalidator;
use common_meta::key::MetadataValue;
use common_meta::key::node_address::NodeAddressValue;
let mut ctx = TestEnv::new().ctx();
let invalidator = Arc::new(FailingCacheInvalidator::default());
ctx.cache_invalidator = invalidator.clone();
let handler = RemapFlowPeerHandler::default();
let mut req = HeartbeatRequest {
peer: Some(Peer {
id: 1,
addr: "first".into(),
}),
node_epoch: 1,
..Default::default()
};
let key = NodeAddressKey::with_flownode(1).to_bytes();
handler
.handle(&req, &mut ctx, &mut HeartbeatAccumulator::default())
.await
.unwrap();
req.peer.as_mut().unwrap().addr = "same-epoch".into();
handler
.handle(&req, &mut ctx, &mut HeartbeatAccumulator::default())
.await
.unwrap();
assert_eq!(invalidator.attempts(), 1);
let kv = ctx.kv_backend.get(&key).await.unwrap().unwrap();
assert_eq!(
NodeAddressValue::try_from_raw_value(&kv.value)
.unwrap()
.peer
.addr,
"first"
);
req.node_epoch = 2;
handler
.handle(&req, &mut ctx, &mut HeartbeatAccumulator::default())
.await
.unwrap();
assert_eq!(invalidator.attempts(), 2);
let kv = ctx.kv_backend.get(&key).await.unwrap().unwrap();
assert_eq!(
NodeAddressValue::try_from_raw_value(&kv.value)
.unwrap()
.peer
.addr,
"same-epoch"
);
}
}
+36 -1
View File
@@ -13,9 +13,13 @@
// limitations under the License.
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use common_meta::cache_invalidator::{CacheInvalidatorRef, DummyCacheInvalidator};
use common_meta::cache_invalidator::{
CacheInvalidator, CacheInvalidatorRef, Context as InvalidationContext, DummyCacheInvalidator,
};
use common_meta::distributed_time_constants::BASE_HEARTBEAT_INTERVAL;
use common_meta::instruction::CacheIdent;
use common_meta::key::{TableMetadataManager, TableMetadataManagerRef};
use common_meta::kv_backend::memory::MemoryKvBackend;
use common_meta::kv_backend::{KvBackendRef, ResettableKvBackendRef};
@@ -97,3 +101,34 @@ impl TestEnv {
}
}
}
/// Counts failed notification attempts without requiring heartbeat connections.
#[derive(Default)]
pub(crate) struct FailingCacheInvalidator {
attempts: AtomicUsize,
}
impl FailingCacheInvalidator {
pub(crate) fn attempts(&self) -> usize {
self.attempts.load(Ordering::Relaxed)
}
}
#[async_trait::async_trait]
impl CacheInvalidator for FailingCacheInvalidator {
async fn invalidate(
&self,
_: &InvalidationContext,
_: &[CacheIdent],
) -> common_meta::error::Result<()> {
self.attempts.fetch_add(1, Ordering::Relaxed);
common_meta::error::UnexpectedSnafu {
err_msg: "injected notification failure",
}
.fail()
}
fn invalidate_all(&self) -> common_meta::error::Result<()> {
Ok(())
}
}
+281
View File
@@ -0,0 +1,281 @@
// 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::future::Future;
use std::sync::Arc;
use api::v1::meta::{Peer, Role};
use common_meta::instruction::CacheIdent;
use common_meta::key::MetadataValue;
use common_meta::key::node_address::NodeAddressValue;
use common_meta::rpc::store::PutRequest;
use common_telemetry::{error, info};
use dashmap::DashMap;
use snafu::ResultExt;
use tokio::sync::Mutex;
use crate::Result;
use crate::error::{InvalidNodeInfoFormatSnafu, KvBackendSnafu};
use crate::metasrv::Context;
/// Serializes address changes per node and acknowledges only successful updates.
/// Node information refreshed on every heartbeat has a separate lifecycle.
#[derive(Debug, Default)]
pub(crate) struct NodeAddressUpdater {
states: DashMap<(Role, u64), Arc<Mutex<NodeAddressEpochs>>>,
}
#[derive(Debug, Default)]
struct NodeAddressEpochs {
/// Rejects stale heartbeats even after a newer epoch failed to save.
seen_epoch: Option<u64>,
/// Suppresses retries only after persistence succeeds.
saved_epoch: Option<u64>,
}
impl NodeAddressUpdater {
/// Saves a new or previously failed epoch. Returns true only when this call
/// saved the address, or false when the heartbeat is stale or already saved.
/// Cache invalidation is separate and does not affect acknowledgement.
pub(crate) async fn update_if_needed<F, Fut>(
&self,
node: (Role, u64),
epoch: u64,
save: F,
) -> Result<bool>
where
F: FnOnce() -> Fut,
Fut: Future<Output = Result<()>>,
{
// Never retain a DashMap guard across await, even while waiting for a
// different node: its entry may live in the same shard.
let state = {
let entry = self.states.entry(node).or_default();
Arc::clone(entry.value())
};
let mut state = state.lock().await;
if state.seen_epoch.is_some_and(|seen| epoch < seen) || state.saved_epoch == Some(epoch) {
return Ok(false);
}
// Seeing an epoch rejects stale heartbeats, but only a successful save
// suppresses retries of that same epoch. Cancellation also leaves it retryable.
state.seen_epoch = Some(epoch);
save().await?;
state.saved_epoch = Some(epoch);
Ok(true)
}
}
/// Persists an address through the leader cache.
pub(crate) async fn save_node_address(ctx: &Context, key: Vec<u8>, peer: Peer) -> Result<()> {
let address = NodeAddressValue::new(peer);
let value = address
.try_as_raw_value()
.context(InvalidNodeInfoFormatSnafu)?;
ctx.leader_cached_kv_backend
.put(PutRequest {
key,
value,
prev_kv: false,
})
.await
.context(KvBackendSnafu)?;
info!("Successfully updated node address: {:?}", address.peer);
Ok(())
}
/// Preserves the existing best-effort notification behavior. Notification
/// failures do not undo a persisted address or cause same-epoch rewrites.
pub(crate) async fn invalidate_address_caches(
ctx: &Context,
node_id: u64,
cache_idents: &[CacheIdent],
) {
if let Err(e) = ctx
.cache_invalidator
.invalidate(&Default::default(), cache_idents)
.await
{
error!(e; "Failed to invalidate address caches for node {}: {:?}", node_id, cache_idents);
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use tokio::sync::Notify;
use tokio::time::timeout;
use super::*;
#[tokio::test]
async fn test_failed_save_retries_same_epoch_and_rejects_stale_heartbeats() {
let updater = NodeAddressUpdater::default();
let key = (Role::Datanode, 1);
assert!(
updater
.update_if_needed(key, 2, || async {
crate::error::UnexpectedSnafu {
violated: "injected save failure",
}
.fail()
})
.await
.is_err()
);
assert!(
!updater
.update_if_needed(key, 1, || async {
panic!("stale heartbeat must not save");
})
.await
.unwrap()
);
assert!(
updater
.update_if_needed(key, 2, || async { Ok(()) })
.await
.unwrap()
);
assert!(
!updater
.update_if_needed(key, 2, || async {
panic!("successful epoch must not save again");
})
.await
.unwrap()
);
assert!(
updater
.update_if_needed(key, 3, || async { Ok(()) })
.await
.unwrap()
);
}
#[tokio::test]
async fn test_cancelled_save_can_be_retried() {
let updater = NodeAddressUpdater::default();
let key = (Role::Datanode, 1);
let entered = Notify::new();
{
let first = updater.update_if_needed(key, 0, || async {
entered.notify_one();
std::future::pending::<Result<()>>().await
});
tokio::pin!(first);
tokio::select! {
_ = entered.notified() => {},
_ = &mut first => panic!("save must remain pending"),
}
}
assert!(
timeout(
Duration::from_secs(2),
updater.update_if_needed(key, 0, || async { Ok(()) })
)
.await
.unwrap()
.unwrap()
);
}
#[test]
fn test_same_node_serialized_other_nodes_progress() {
// A watchdog outside the runtime detects synchronous DashMap deadlocks;
// a Tokio timeout alone cannot fire if the only worker is blocked.
let (progress_tx, progress_rx) = std::sync::mpsc::sync_channel(1);
let release = Arc::new(Notify::new());
let finish = Arc::new(Notify::new());
let worker = std::thread::spawn({
let release = release.clone();
let finish = finish.clone();
move || {
tokio::runtime::Builder::new_current_thread()
.enable_time()
.build()
.unwrap()
.block_on(async move {
let updater = Arc::new(NodeAddressUpdater::default());
let entered = Arc::new(Notify::new());
let first = tokio::spawn({
let updater = updater.clone();
let entered = entered.clone();
let release = release.clone();
async move {
updater
.update_if_needed((Role::Datanode, 1), 1, || async {
entered.notify_one();
release.notified().await;
Ok(())
})
.await
}
});
entered.notified().await;
let second = tokio::spawn({
let updater = updater.clone();
async move {
updater
.update_if_needed((Role::Datanode, 1), 2, || async { Ok(()) })
.await
}
});
tokio::task::yield_now().await;
assert!(!second.is_finished());
// Exercise shared DashMap shards on a single-thread runtime while the
// first node is suspended in external I/O.
timeout(Duration::from_secs(2), async {
for id in 0_u64..128 {
assert!(
updater
.update_if_needed((Role::Datanode, id + 2), 1, || async {
Ok(())
})
.await
.unwrap()
);
}
})
.await
.unwrap();
progress_tx.send(()).unwrap();
finish.notified().await;
timeout(Duration::from_secs(2), async {
assert!(first.await.unwrap().unwrap());
assert!(second.await.unwrap().unwrap());
})
.await
.unwrap();
assert!(
!updater
.update_if_needed((Role::Datanode, 1), 1, || async {
panic!("old epoch must not overwrite a newer one");
})
.await
.unwrap()
);
});
}
});
let progress = progress_rx.recv_timeout(Duration::from_secs(5));
release.notify_one();
finish.notify_one();
progress.expect("other node updates must progress while one node save is blocked");
worker.join().unwrap();
}
}
+3
View File
@@ -48,6 +48,9 @@ lazy_static! {
/// Heartbeat received by metasrv.
pub static ref METRIC_META_HEARTBEAT_RECV: IntCounterVec =
register_int_counter_vec!("greptime_meta_heartbeat_recv", "heartbeats received by metasrv", &["pusher_key"]).unwrap();
/// Heartbeats dropped by metasrv.
pub static ref METRIC_META_HEARTBEAT_DROPPED: IntCounterVec =
register_int_counter_vec!("greptime_meta_heartbeat_dropped", "heartbeats dropped by metasrv", &["reason"]).unwrap();
/// The migration execute histogram.
pub static ref METRIC_META_REGION_MIGRATION_EXECUTE: HistogramVec =
register_histogram_vec!("greptime_meta_region_migration_execute", "meta region migration execute", &["state"]).unwrap();
@@ -82,7 +82,7 @@ pub fn merge_and_validate_region_wal_options(
table_id: TableId,
) -> Result<RegionWalOptions> {
// Doesn't allow overwriting existing WAL options.
for (region_number, _) in new_region_wal_options.iter() {
for region_number in new_region_wal_options.keys() {
if region_wal_options.contains_key(region_number) {
return error::UnexpectedSnafu {
violated: format!(
+71 -6
View File
@@ -35,6 +35,7 @@ use common_time::util::current_time_millis;
use futures::{StreamExt, TryStreamExt};
use snafu::{ResultExt, ensure};
use store_api::storage::RegionId;
use tokio::sync::mpsc::error::TrySendError;
use tokio::sync::mpsc::{Receiver, Sender};
use tokio::sync::oneshot;
use tokio::time::{MissedTickBehavior, interval, interval_at};
@@ -43,6 +44,7 @@ use crate::discovery::utils::accept_ingest_workload;
use crate::error::{self, Result};
use crate::failure_detector::PhiAccrualFailureDetectorOptions;
use crate::metasrv::{RegionStatAwareSelectorRef, SelectTarget, SelectorContext, SelectorRef};
use crate::metrics::METRIC_META_HEARTBEAT_DROPPED;
use crate::procedure::region_migration::manager::{
RegionMigrationManagerRef, RegionMigrationTriggerReason, SubmitRegionMigrationTaskResult,
};
@@ -356,10 +358,29 @@ impl HeartbeatAcceptor {
Self { sender }
}
/// Accepts heartbeats from datanodes.
pub(crate) async fn accept(&self, heartbeat: DatanodeHeartbeat) {
if let Err(err) = self.sender.send(Event::HeartbeatArrived(heartbeat)).await {
error!(err; "RegionSupervisor has stop receiving heartbeat.");
/// Accepts heartbeats from datanodes without waiting for queue capacity: a full queue
/// drops the heartbeat, counted in [`METRIC_META_HEARTBEAT_DROPPED`]. A dropped
/// heartbeat costs the failure detector one sample.
pub(crate) fn accept(&self, heartbeat: DatanodeHeartbeat) {
let datanode_id = heartbeat.datanode_id;
match self.sender.try_send(Event::HeartbeatArrived(heartbeat)) {
Ok(()) => {}
Err(TrySendError::Full(_)) => {
METRIC_META_HEARTBEAT_DROPPED
.with_label_values(&["full"])
.inc();
warn!(
"Dropping heartbeat because the region supervisor event queue is full, datanode_id: {}, queue_capacity: {}",
datanode_id,
self.sender.max_capacity()
);
}
Err(TrySendError::Closed(err)) => {
METRIC_META_HEARTBEAT_DROPPED
.with_label_values(&["closed"])
.inc();
error!(err; "RegionSupervisor has stop receiving heartbeat.");
}
}
}
}
@@ -899,14 +920,15 @@ pub(crate) mod tests {
use tokio::time::sleep;
use super::RegionSupervisorSelector;
use crate::metrics::METRIC_META_HEARTBEAT_DROPPED;
use crate::procedure::region_migration::RegionMigrationTriggerReason;
use crate::procedure::region_migration::manager::{
RegionMigrationManager, SubmitRegionMigrationTaskResult,
};
use crate::procedure::region_migration::test_util::TestingEnv;
use crate::region::supervisor::{
DatanodeHeartbeat, Event, RegionFailureDetectorControl, RegionSupervisor,
RegionSupervisorTicker,
DatanodeHeartbeat, Event, HeartbeatAcceptor, RegionFailureDetectorControl,
RegionSupervisor, RegionSupervisorTicker,
};
use crate::selector::test_utils::{RandomNodeSelector, new_test_selector_context};
@@ -940,6 +962,14 @@ pub(crate) mod tests {
)
}
fn filler_heartbeat() -> DatanodeHeartbeat {
DatanodeHeartbeat {
datanode_id: 1,
regions: vec![],
timestamp: 0,
}
}
#[tokio::test]
async fn test_heartbeat() {
let (mut supervisor, sender) = new_test_supervisor();
@@ -1372,4 +1402,39 @@ pub(crate) mod tests {
assert!(supervisor.failure_detector.contains(&detecting_region));
assert!(supervisor.failover_counts.contains_key(&detecting_region));
}
#[tokio::test]
async fn test_heartbeat_accept_does_not_block_on_full_queue() {
// Regression test for #9419: a full queue used to park the caller.
// The supervisor is never run, so the queue stays full.
let (mut supervisor, sender) = new_test_supervisor();
let mut queued = 0;
while sender
.try_send(Event::HeartbeatArrived(filler_heartbeat()))
.is_ok()
{
queued += 1;
}
assert_eq!(queued, sender.max_capacity());
assert_eq!(sender.capacity(), 0);
let acceptor = HeartbeatAcceptor::new(sender.clone());
let dropped = || {
METRIC_META_HEARTBEAT_DROPPED
.with_label_values(&["full"])
.get()
};
let dropped_before = dropped();
// The full queue drops the heartbeat and counts it.
acceptor.accept(filler_heartbeat());
assert_eq!(dropped(), dropped_before + 1);
assert_eq!(sender.capacity(), 0);
// Free one slot: the next heartbeat is accepted.
supervisor.receiver.try_recv().unwrap();
acceptor.accept(filler_heartbeat());
assert_eq!(dropped(), dropped_before + 1);
assert_eq!(sender.capacity(), 0);
}
}
-1
View File
@@ -419,7 +419,6 @@ mod tests {
use std::sync::Arc;
use api::v1::meta::heartbeat_server::Heartbeat;
use api::v1::meta::*;
use common_meta::kv_backend::memory::MemoryKvBackend;
use common_telemetry::tracing_context::W3cTrace;
use servers::grpc::GrpcOptions;
+359 -2
View File
@@ -69,6 +69,9 @@ pub struct LeaderCachedKvBackend {
check_leader: CheckLeaderRef,
store: KvBackendRef,
cache: ResettableKvBackendRef,
/// Incremented before and after each store mutation. The first increment
/// detects overlapping writers; the second invalidates cache fills that
/// read the old value while the store mutation was in flight.
version: AtomicUsize,
name: String,
}
@@ -214,9 +217,11 @@ impl KvBackend for LeaderCachedKvBackend {
let ver = self.create_new_version();
let res = self.store.put(req.clone()).await?;
self.create_new_version();
let _ = self.cache.put(req.clone()).await?;
if !self.validate_version(ver) {
// Only our own completion increment may follow the starting version.
if !self.validate_version(ver + 1) {
self.invalid_key(req.key).await?;
}
@@ -231,9 +236,11 @@ impl KvBackend for LeaderCachedKvBackend {
let ver = self.create_new_version();
let res = self.store.batch_put(req.clone()).await?;
self.create_new_version();
let _ = self.cache.batch_put(req.clone()).await?;
if !self.validate_version(ver) {
// Only our own completion increment may follow the starting version.
if !self.validate_version(ver + 1) {
let keys = req.kvs.into_iter().map(|kv| kv.key).collect::<Vec<_>>();
self.invalid_keys(keys).await?;
}
@@ -306,6 +313,7 @@ impl KvBackend for LeaderCachedKvBackend {
let key = req.key.clone();
let res = self.store.compare_and_put(req).await?;
self.create_new_version();
// Delete key in the cache.
//
// Cache can not deal with the CAS operation, because it does
@@ -322,6 +330,7 @@ impl KvBackend for LeaderCachedKvBackend {
let _ = self.create_new_version();
let res = self.store.delete_range(req.clone()).await?;
self.create_new_version();
let _ = self.cache.delete_range(req).await?;
Ok(res)
}
@@ -334,6 +343,7 @@ impl KvBackend for LeaderCachedKvBackend {
let _ = self.create_new_version();
let res = self.store.batch_delete(req.clone()).await?;
self.create_new_version();
let _ = self.cache.batch_delete(req).await?;
Ok(res)
}
@@ -351,6 +361,7 @@ impl TxnService for LeaderCachedKvBackend {
let _ = self.create_new_version();
let res = self.store.txn(txn.clone()).await?;
self.create_new_version();
let TxnRequest {
success, failure, ..
} = txn.into();
@@ -384,6 +395,7 @@ impl TxnService for LeaderCachedKvBackend {
impl ResettableKvBackend for LeaderCachedKvBackend {
fn reset(&self) {
self.create_new_version();
self.cache.reset()
}
@@ -394,10 +406,355 @@ impl ResettableKvBackend for LeaderCachedKvBackend {
#[cfg(test)]
mod tests {
use std::sync::Mutex;
use common_meta::kv_backend::txn::{Compare, CompareOp};
use common_meta::rpc::KeyValue;
use futures::poll;
use tokio::sync::oneshot;
use super::*;
/// Pauses one backend call at a known point, without relying on task scheduling.
#[derive(Default)]
struct Pause {
receiver: Mutex<Option<oneshot::Receiver<()>>>,
}
impl Pause {
fn arm(&self) -> oneshot::Sender<()> {
let (sender, receiver) = oneshot::channel();
assert!(self.receiver.lock().unwrap().replace(receiver).is_none());
sender
}
async fn wait(&self) {
let receiver = self.receiver.lock().unwrap().take();
if let Some(receiver) = receiver {
receiver.await.unwrap();
}
}
}
#[derive(Default)]
struct ControlledBackend {
inner: MemoryKvBackend<Error>,
before_write: Pause,
after_write: Pause,
after_read: Pause,
}
#[async_trait::async_trait]
impl TxnService for ControlledBackend {
type Error = Error;
async fn txn(&self, txn: Txn) -> Result<TxnResponse> {
self.before_write.wait().await;
let res = self.inner.txn(txn).await;
self.after_write.wait().await;
res
}
fn max_txn_ops(&self) -> usize {
self.inner.max_txn_ops()
}
}
#[async_trait::async_trait]
impl KvBackend for ControlledBackend {
fn name(&self) -> &str {
"ControlledBackend"
}
fn as_any(&self) -> &dyn Any {
self
}
async fn range(&self, req: RangeRequest) -> Result<RangeResponse> {
let res = self.inner.range(req).await;
self.after_read.wait().await;
res
}
async fn batch_get(&self, req: BatchGetRequest) -> Result<BatchGetResponse> {
let res = self.inner.batch_get(req).await;
self.after_read.wait().await;
res
}
async fn put(&self, req: PutRequest) -> Result<PutResponse> {
self.before_write.wait().await;
let res = self.inner.put(req).await;
self.after_write.wait().await;
res
}
async fn batch_put(&self, req: BatchPutRequest) -> Result<BatchPutResponse> {
self.before_write.wait().await;
let res = self.inner.batch_put(req).await;
self.after_write.wait().await;
res
}
async fn delete_range(&self, req: DeleteRangeRequest) -> Result<DeleteRangeResponse> {
self.before_write.wait().await;
let res = self.inner.delete_range(req).await;
self.after_write.wait().await;
res
}
async fn batch_delete(&self, req: BatchDeleteRequest) -> Result<BatchDeleteResponse> {
self.before_write.wait().await;
let res = self.inner.batch_delete(req).await;
self.after_write.wait().await;
res
}
}
#[derive(Clone, Copy, Debug)]
enum Write {
Txn,
TxnFailure,
Put,
BatchPut,
CompareAndPut,
DeleteRange,
BatchDelete,
}
const WRITES: [Write; 7] = [
Write::Txn,
Write::TxnFailure,
Write::Put,
Write::BatchPut,
Write::CompareAndPut,
Write::DeleteRange,
Write::BatchDelete,
];
const KEY: &[u8] = b"route";
impl Write {
async fn execute(self, store: &LeaderCachedKvBackend, value: &[u8]) {
match self {
Self::Txn => {
store
.txn(Txn::new().and_then(vec![TxnOp::Put(KEY.to_vec(), value.to_vec())]))
.await
.unwrap();
}
Self::TxnFailure => {
let res = store
.txn(
Txn::new()
.when(vec![Compare::with_value_not_exists(
KEY.to_vec(),
CompareOp::Equal,
)])
.or_else(vec![TxnOp::Put(KEY.to_vec(), value.to_vec())]),
)
.await
.unwrap();
assert!(!res.succeeded);
}
Self::Put => {
store
.put(PutRequest::new().with_key(KEY).with_value(value))
.await
.unwrap();
}
Self::BatchPut => {
store
.batch_put(BatchPutRequest {
kvs: vec![KeyValue {
key: KEY.to_vec(),
value: value.to_vec(),
}],
..Default::default()
})
.await
.unwrap();
}
Self::CompareAndPut => {
let res = store
.compare_and_put(
CompareAndPutRequest::new()
.with_key(KEY)
.with_expect(b"old")
.with_value(value),
)
.await
.unwrap();
assert!(res.success);
}
Self::DeleteRange => {
store
.delete_range(DeleteRangeRequest::new().with_key(KEY))
.await
.unwrap();
}
Self::BatchDelete => {
store
.batch_delete(BatchDeleteRequest {
keys: vec![KEY.to_vec()],
prev_kv: false,
})
.await
.unwrap();
}
}
}
}
async fn read(store: &LeaderCachedKvBackend, batch: bool) -> Option<KeyValue> {
if batch {
store
.batch_get(BatchGetRequest {
keys: vec![KEY.to_vec()],
})
.await
.unwrap()
.kvs
.pop()
} else {
store.get(KEY).await.unwrap()
}
}
/// A cache miss reads the old value after a writer starts, then returns after
/// the writer has committed and updated/invalidated the cache (#9548).
#[tokio::test]
async fn test_cache_miss_overlaps_write() {
for write in WRITES {
for batch in [true, false] {
let backing = Arc::new(ControlledBackend::default());
backing
.inner
.put(PutRequest::new().with_key(KEY).with_value(b"old"))
.await
.unwrap();
let store = LeaderCachedKvBackend::with_always_leader(backing.clone());
let commit = backing.before_write.arm();
let writer = write.execute(&store, b"new");
tokio::pin!(writer);
assert!(poll!(writer.as_mut()).is_pending());
let return_read = backing.after_read.arm();
let reader = read(&store, batch);
tokio::pin!(reader);
assert!(poll!(reader.as_mut()).is_pending());
commit.send(()).unwrap();
writer.await;
return_read.send(()).unwrap();
assert_eq!(reader.await.unwrap().value(), b"old");
let durable = backing.inner.get(KEY).await.unwrap();
assert_eq!(
read(&store, batch).await,
durable,
"{write:?}, batch={batch}"
);
}
}
}
/// Start order, commit order and response order can all differ. An older
/// put response must not overwrite the cache after a newer mutation.
#[tokio::test]
async fn test_overlapping_writes() {
for first in [Write::Put, Write::BatchPut] {
for second in WRITES {
for reverse_commit in [false, true] {
let backing = Arc::new(ControlledBackend::default());
backing
.inner
.put(PutRequest::new().with_key(KEY).with_value(b"old"))
.await
.unwrap();
let store = LeaderCachedKvBackend::with_always_leader(backing.clone());
let resume = if reverse_commit {
backing.before_write.arm()
} else {
backing.after_write.arm()
};
// Keep the initial value so the second CAS succeeds.
let writer = first.execute(&store, b"old");
tokio::pin!(writer);
assert!(poll!(writer.as_mut()).is_pending());
second.execute(&store, b"new").await;
resume.send(()).unwrap();
writer.await;
let durable = backing.inner.get(KEY).await.unwrap();
assert_eq!(
read(&store, true).await,
durable,
"{first:?}, {second:?}, reverse_commit={reverse_commit}"
);
}
}
}
}
/// The later-started put commits first but returns last, after its stored
/// value has already been replaced by the first writer.
#[tokio::test]
async fn test_write_response_reordering() {
for first in WRITES {
for second in [Write::Put, Write::BatchPut] {
let backing = Arc::new(ControlledBackend::default());
backing
.inner
.put(PutRequest::new().with_key(KEY).with_value(b"old"))
.await
.unwrap();
let store = LeaderCachedKvBackend::with_always_leader(backing.clone());
let commit_first = backing.before_write.arm();
let first_writer = first.execute(&store, b"new");
tokio::pin!(first_writer);
assert!(poll!(first_writer.as_mut()).is_pending());
let return_second = backing.after_write.arm();
let second_writer = second.execute(&store, b"old");
tokio::pin!(second_writer);
assert!(poll!(second_writer.as_mut()).is_pending());
commit_first.send(()).unwrap();
first_writer.await;
return_second.send(()).unwrap();
second_writer.await;
let durable = backing.inner.get(KEY).await.unwrap();
assert_eq!(read(&store, true).await, durable, "{first:?}, {second:?}");
}
}
}
#[tokio::test]
async fn test_cache_miss_overlaps_reset() {
for batch in [true, false] {
let backing = Arc::new(ControlledBackend::default());
backing
.inner
.put(PutRequest::new().with_key(KEY).with_value(b"old"))
.await
.unwrap();
let store = LeaderCachedKvBackend::with_always_leader(backing.clone());
let return_read = backing.after_read.arm();
let reader = read(&store, batch);
tokio::pin!(reader);
assert!(poll!(reader.as_mut()).is_pending());
store.reset();
return_read.send(()).unwrap();
reader.await;
assert!(store.cache.get(KEY).await.unwrap().is_none());
}
}
fn create_leader_cached_kv_backend() -> LeaderCachedKvBackend {
let store = Arc::new(MemoryKvBackend::new());
LeaderCachedKvBackend::with_always_leader(store)
+56 -18
View File
@@ -354,17 +354,38 @@ mod tests {
#[tokio::test]
async fn test_bulk_insert_physical_region_passthrough() {
check_bulk_insert_physical_region_passthrough(false).await;
check_bulk_insert_physical_region_passthrough(true).await;
check_bulk_insert_physical_region_passthrough(false, false, false).await;
check_bulk_insert_physical_region_passthrough(true, false, false).await;
}
async fn check_bulk_insert_physical_region_passthrough(skip_wal: bool) {
#[tokio::test]
async fn test_bulk_insert_physical_region_close_recovery() {
check_bulk_insert_physical_region_passthrough(false, true, false).await;
check_bulk_insert_physical_region_passthrough(true, true, false).await;
}
#[tokio::test]
async fn test_physical_bulk_partition_columns_preserve_stored_rows() {
for skip_wal in [false, true] {
let stripped =
check_bulk_insert_physical_region_passthrough(skip_wal, true, false).await;
let retained =
check_bulk_insert_physical_region_passthrough(skip_wal, true, true).await;
assert_eq!(retained, stripped, "skip_wal={skip_wal}");
}
}
async fn check_bulk_insert_physical_region_passthrough(
skip_wal: bool,
close: bool,
retain_partition_columns: bool,
) -> String {
// Use flat format so that BulkMemtable is used (supports write_bulk).
let mito_config = MitoConfig {
default_flat_format: true,
..Default::default()
};
let env = TestEnv::with_mito_config("", mito_config, Default::default()).await;
let mut env = TestEnv::with_mito_config("", mito_config, Default::default()).await;
env.init_metric_region().await;
env.metric().inner.flush_task.stop().await.unwrap();
let physical_region_id = env.default_physical_region_id();
@@ -388,7 +409,11 @@ mod tests {
index: 2,
column_id: 2, // column_id for "job" in the physical table
}];
let non_tag_indices = vec![0, 1]; // timestamp, value
let non_tag_indices = if retain_partition_columns {
vec![0, 1, 2] // timestamp, value, and the job partition column
} else {
vec![0, 1] // timestamp, value
};
let second_batch = build_logical_batch(3, 3);
let physical_batch = modify_batch_sparse(
second_batch,
@@ -397,6 +422,10 @@ mod tests {
&non_tag_indices,
)
.unwrap();
assert_eq!(
physical_batch.column_by_name("job").is_some(),
retain_partition_columns
);
let request = build_bulk_request(physical_region_id, physical_batch, skip_wal);
let response = env
.metric()
@@ -414,20 +443,23 @@ mod tests {
let batches = RecordBatches::try_collect(stream).await.unwrap();
assert_eq!(batches.iter().map(|b| b.num_rows()).sum::<usize>(), 6);
// Closing without a flush must recover only WAL-backed data. Recreate
// the wrapper too, so its metadata cache cannot hide missing metadata.
let stat = env.mito().region_statistic(physical_region_id).unwrap();
assert_eq!(stat.sst_num, 0);
env.metric()
.handle_request(
physical_region_id,
RegionRequest::Close(RegionCloseRequest {
flush_on_close: false,
}),
)
.await
.unwrap();
let reopened = MetricEngine::try_new(env.mito(), Default::default()).unwrap();
let reopened = if close {
env.metric()
.handle_request(
physical_region_id,
RegionRequest::Close(RegionCloseRequest::default()),
)
.await
.unwrap();
// Recreating the wrapper prevents its metadata cache from hiding data loss.
MetricEngine::try_new(env.mito(), Default::default()).unwrap()
} else {
// Stopping workers without Close exercises recovery from WAL alone.
env.reopen_engine(Default::default()).await;
env.metric()
};
reopened.inner.flush_task.stop().await.unwrap();
reopened
.handle_request(
@@ -446,6 +478,11 @@ mod tests {
)
.await
.unwrap();
let stat = env
.mito()
.region_statistic(crate::utils::to_data_region_id(physical_region_id))
.unwrap();
assert_eq!(stat.sst_num > 0, skip_wal && close);
let stream = reopened
.scan_to_stream(logical_region_id, ScanRequest::default())
.await
@@ -453,8 +490,9 @@ mod tests {
let batches = RecordBatches::try_collect(stream).await.unwrap();
assert_eq!(
batches.iter().map(|b| b.num_rows()).sum::<usize>(),
if skip_wal { 0 } else { 6 },
if skip_wal && !close { 0 } else { 6 },
);
batches.pretty_print().unwrap()
}
#[tokio::test]
+36 -20
View File
@@ -934,14 +934,22 @@ mod tests {
#[tokio::test]
async fn test_put_skip_wal_batch_recovery() {
check_put_skip_wal_batch_recovery("sparse", false).await;
check_put_skip_wal_batch_recovery("sparse", true).await;
check_put_skip_wal_batch_recovery("dense", false).await;
check_put_skip_wal_batch_recovery("dense", true).await;
check_put_skip_wal_batch_recovery("sparse", false, false).await;
check_put_skip_wal_batch_recovery("sparse", true, false).await;
check_put_skip_wal_batch_recovery("dense", false, false).await;
check_put_skip_wal_batch_recovery("dense", true, false).await;
}
async fn check_put_skip_wal_batch_recovery(encoding: &str, skip_wal: bool) {
let env = TestEnv::new().await;
#[tokio::test]
async fn test_put_skip_wal_batch_close_recovery() {
check_put_skip_wal_batch_recovery("sparse", false, true).await;
check_put_skip_wal_batch_recovery("sparse", true, true).await;
check_put_skip_wal_batch_recovery("dense", false, true).await;
check_put_skip_wal_batch_recovery("dense", true, true).await;
}
async fn check_put_skip_wal_batch_recovery(encoding: &str, skip_wal: bool, close: bool) {
let mut env = TestEnv::new().await;
let engine = env.metric();
engine.inner.flush_task.stop().await.unwrap();
let physical_region_id = env.default_physical_region_id();
@@ -1009,18 +1017,21 @@ mod tests {
assert!(stat.memtable_size > 0);
assert_eq!(stat.sst_num, 0);
}
engine
.handle_request(
physical_region_id,
RegionRequest::Close(RegionCloseRequest {
flush_on_close: false,
}),
)
.await
.unwrap();
// Recreate the wrapper as well, discarding its metadata cache.
let reopened = MetricEngine::try_new(env.mito(), Default::default()).unwrap();
let reopened = if close {
engine
.handle_request(
physical_region_id,
RegionRequest::Close(RegionCloseRequest::default()),
)
.await
.unwrap();
// Recreate the wrapper as well, discarding its metadata cache.
MetricEngine::try_new(env.mito(), Default::default()).unwrap()
} else {
// Bypass Close to retain coverage of rows lost without WAL protection.
env.reopen_engine(Default::default()).await;
env.metric()
};
reopened.inner.flush_task.stop().await.unwrap();
reopened
.handle_request(
@@ -1042,12 +1053,17 @@ mod tests {
)
.await
.unwrap();
let stat = env
.mito()
.region_statistic(crate::utils::to_data_region_id(physical_region_id))
.unwrap();
assert_eq!(stat.sst_num > 0, skip_wal && close);
let recovered_metadata = reopened.get_metadata(logical_region_id).await.unwrap();
assert_eq!(
metadata_before.column_metadatas,
recovered_metadata.column_metadatas
);
let expected = if skip_wal {
let expected = if skip_wal && !close {
vec![]
} else {
vec![(0, 30.0), (1, 10.0), (2, 20.0), (3, 30.0)]
@@ -1055,7 +1071,7 @@ mod tests {
assert_eq!(
scan_timestamp_values(&reopened, logical_region_id).await,
expected,
"encoding={encoding}, skip_wal={skip_wal}"
"encoding={encoding}, skip_wal={skip_wal}, close={close}"
);
}
+20
View File
@@ -486,6 +486,9 @@ impl ErrorExt for Error {
use Error::*;
match self {
PhysicalRegionNotFound { .. } | LogicalRegionNotFound { .. } => {
RetryHint::RetryAfterRouteRefresh
}
CreateMitoRegion { source, .. }
| OpenMitoRegion { source, .. }
| CleanUpMitoRegion { source, .. }
@@ -510,3 +513,20 @@ impl ErrorExt for Error {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_missing_region_retry_hints() {
let region_id = RegionId::new(1024, 1);
for error in [
PhysicalRegionNotFoundSnafu { region_id }.build(),
LogicalRegionNotFoundSnafu { region_id }.build(),
] {
assert_eq!(error.status_code(), StatusCode::RegionNotFound);
assert_eq!(error.retry_hint(), RetryHint::RetryAfterRouteRefresh);
}
}
}
+11
View File
@@ -121,6 +121,17 @@ impl TestEnv {
self.metric.clone()
}
/// Restarts Mito without closing regions and recreates the metric metadata cache.
/// Callers stop the old metric engine's metadata flush task before restarting.
pub async fn reopen_engine(&mut self, config: EngineConfig) {
let mito_config = self.mito.mito_config().clone();
self.mito = self
.mito_env
.reopen_engine(self.mito.clone(), mito_config)
.await;
self.metric = MetricEngine::try_new(self.mito.clone(), config).unwrap();
}
/// Creates a new follower engine with the same config as the leader engine.
pub async fn create_follower_engine(&mut self) -> (MitoEngine, MetricEngine) {
let mito = self
@@ -116,7 +116,7 @@ impl<'a, 'b> SparsePrimaryKeyView<'a, 'b> {
// encoded_value checked the markers and every chunk length. Reserve once
// before unchunking so the buffer does not grow for each 8-byte chunk.
buffer.reserve(encoded.len() - 2);
for chunk in encoded[2..].chunks_exact(9) {
for chunk in encoded[2..].as_chunks::<9>().0 {
let len = usize::from(chunk[8]).min(8);
buffer.extend_from_slice(&chunk[..len]);
}
+1 -1
View File
@@ -268,7 +268,7 @@ fn filter_1_host(c: &mut Criterion) {
)
.unwrap();
for (_range_id, range) in ranges.ranges.iter() {
for range in ranges.ranges.values() {
let iter = range.build_record_batch_iter(None, None).unwrap();
for batch in iter {
let _batch = batch.unwrap();
+1 -1
View File
@@ -579,7 +579,7 @@ pub struct SstWriteRequest {
pub source: FlatSource,
pub cache_manager: CacheManagerRef,
/// Optional uniform row sequence for writes that do not preserve sequences.
/// Compaction passes `None` to retain the reader's effective input sequences.
/// Compaction and LastNonNull flushes pass `None` to retain input sequences.
pub max_sequence: Option<SequenceNumber>,
pub sst_write_format: FormatType,
+10 -2
View File
@@ -23,7 +23,7 @@ use store_api::storage::{ColumnId, FileId};
use crate::metrics::{CACHE_BYTES, CACHE_EVICTION, CACHE_HIT, CACHE_MISS};
use crate::sst::index::fulltext_index::applier::builder::{
FulltextQuery, FulltextRequest, FulltextTerm,
FulltextLike, FulltextQuery, FulltextRequest, FulltextTerm,
};
use crate::sst::parquet::row_selection::RowGroupSelection;
@@ -176,7 +176,12 @@ impl FulltextIndexKey {
.iter()
.map(|term| term.term.len() + size_of::<FulltextTerm>())
.sum::<usize>();
query_size + term_size
let like_size = request
.likes
.iter()
.map(|like| like.pattern.len() + size_of::<FulltextLike>())
.sum::<usize>();
query_size + term_size + like_size
})
.sum();
Self {
@@ -343,6 +348,7 @@ mod tests {
FulltextQuery("another long query string".to_string()),
],
terms: vec![],
likes: vec![],
};
predicates1.insert(1, request1);
let key1 = PredicateKey::new_fulltext(Arc::new(predicates1));
@@ -357,6 +363,7 @@ mod tests {
FulltextQuery("another long query string".to_string()),
],
terms: vec![],
likes: vec![],
};
predicates2.insert(1, request2);
let key2 = PredicateKey::new_fulltext(Arc::new(predicates2));
@@ -427,6 +434,7 @@ mod tests {
col_lowered: false,
term: "test term".to_string(),
}],
likes: vec![],
};
predicates1.insert(1, request1);
let key1 = PredicateKey::new_fulltext(Arc::new(predicates1));
+1 -1
View File
@@ -546,7 +546,7 @@ async fn test_planning_panic_notifies_and_clears_status() {
let finished = recv_compaction_pick_finished(&mut rx).await;
let CompactionPlanningResult::Error(err) = &finished.result else {
panic!("expected planning error, got {:?}", &finished.result);
panic!("expected planning error, got {:?}", finished.result);
};
assert!(err.to_string().contains("planning boom"));
let pending_ddls = scheduler
+51 -9
View File
@@ -1262,13 +1262,13 @@ async fn test_all_region_infos() {
async fn test_all_index_metas_list_all_types() {
test_all_index_metas_list_all_types_with_format(false, r#"
PuffinIndexMetaEntry { table_dir: "test/", index_file_path: "test/11_0000000001/index/<file_id>.puffin", region_id: 47244640257(11, 1), table_id: 11, region_number: 1, region_group: 0, region_sequence: 1, file_id: "<file_id>", index_file_size: Some(6000), index_type: "bloom_filter", target_type: "column", target_key: "1", target_json: "{\"column\":1}", blob_size: 751, meta_json: Some("{\"bloom\":{\"bloom_filter_size\":640,\"row_count\":20,\"rows_per_segment\":2,\"segment_count\":10}}"), node_id: None }
PuffinIndexMetaEntry { table_dir: "test/", index_file_path: "test/11_0000000001/index/<file_id>.puffin", region_id: 47244640257(11, 1), table_id: 11, region_number: 1, region_group: 0, region_sequence: 1, file_id: "<file_id>", index_file_size: Some(6000), index_type: "fulltext_bloom", target_type: "column", target_key: "4", target_json: "{\"column\":4}", blob_size: 89, meta_json: Some("{\"bloom\":{\"bloom_filter_size\":64,\"row_count\":20,\"rows_per_segment\":4,\"segment_count\":5},\"fulltext\":{\"analyzer\":\"English\",\"case_sensitive\":false}}"), node_id: None }
PuffinIndexMetaEntry { table_dir: "test/", index_file_path: "test/11_0000000001/index/<file_id>.puffin", region_id: 47244640257(11, 1), table_id: 11, region_number: 1, region_group: 0, region_sequence: 1, file_id: "<file_id>", index_file_size: Some(6000), index_type: "fulltext_bloom", target_type: "column", target_key: "4", target_json: "{\"column\":4}", blob_size: 89, meta_json: Some("{\"bloom\":{\"bloom_filter_size\":64,\"row_count\":20,\"rows_per_segment\":4,\"segment_count\":5},\"fulltext\":{\"analyzer\":\"English\",\"bloom_version\":2,\"case_sensitive\":false}}"), node_id: None }
PuffinIndexMetaEntry { table_dir: "test/", index_file_path: "test/11_0000000001/index/<file_id>.puffin", region_id: 47244640257(11, 1), table_id: 11, region_number: 1, region_group: 0, region_sequence: 1, file_id: "<file_id>", index_file_size: Some(6000), index_type: "fulltext_tantivy", target_type: "column", target_key: "5", target_json: "{\"column\":5}", blob_size: 1000, meta_json: Some("{\"fulltext\":{\"analyzer\":\"Chinese\",\"case_sensitive\":true}}"), node_id: None }
PuffinIndexMetaEntry { table_dir: "test/", index_file_path: "test/11_0000000001/index/<file_id>.puffin", region_id: 47244640257(11, 1), table_id: 11, region_number: 1, region_group: 0, region_sequence: 1, file_id: "<file_id>", index_file_size: Some(6000), index_type: "inverted", target_type: "column", target_key: "1", target_json: "{\"column\":1}", blob_size: 518, meta_json: Some("{\"inverted\":{\"base_offset\":0,\"bitmap_type\":\"Roaring\",\"fst_size\":150,\"inverted_index_size\":518,\"null_bitmap_size\":8,\"relative_fst_offset\":368,\"relative_null_bitmap_offset\":0,\"segment_row_count\":1024,\"total_row_count\":20}}"), node_id: None }
PuffinIndexMetaEntry { table_dir: "test/", index_file_path: "test/11_0000000001/index/<file_id>.puffin", region_id: 47244640257(11, 1), table_id: 11, region_number: 1, region_group: 0, region_sequence: 1, file_id: "<file_id>", index_file_size: Some(6000), index_type: "inverted", target_type: "column", target_key: "2", target_json: "{\"column\":2}", blob_size: 515, meta_json: Some("{\"inverted\":{\"base_offset\":0,\"bitmap_type\":\"Roaring\",\"fst_size\":147,\"inverted_index_size\":515,\"null_bitmap_size\":8,\"relative_fst_offset\":368,\"relative_null_bitmap_offset\":0,\"segment_row_count\":1024,\"total_row_count\":20}}"), node_id: None }"#).await;
test_all_index_metas_list_all_types_with_format(true, r#"
PuffinIndexMetaEntry { table_dir: "test/", index_file_path: "test/11_0000000001/index/<file_id>.puffin", region_id: 47244640257(11, 1), table_id: 11, region_number: 1, region_group: 0, region_sequence: 1, file_id: "<file_id>", index_file_size: Some(6000), index_type: "bloom_filter", target_type: "column", target_key: "1", target_json: "{\"column\":1}", blob_size: 751, meta_json: Some("{\"bloom\":{\"bloom_filter_size\":640,\"row_count\":20,\"rows_per_segment\":2,\"segment_count\":10}}"), node_id: None }
PuffinIndexMetaEntry { table_dir: "test/", index_file_path: "test/11_0000000001/index/<file_id>.puffin", region_id: 47244640257(11, 1), table_id: 11, region_number: 1, region_group: 0, region_sequence: 1, file_id: "<file_id>", index_file_size: Some(6000), index_type: "fulltext_bloom", target_type: "column", target_key: "4", target_json: "{\"column\":4}", blob_size: 89, meta_json: Some("{\"bloom\":{\"bloom_filter_size\":64,\"row_count\":20,\"rows_per_segment\":4,\"segment_count\":5},\"fulltext\":{\"analyzer\":\"English\",\"case_sensitive\":false}}"), node_id: None }
PuffinIndexMetaEntry { table_dir: "test/", index_file_path: "test/11_0000000001/index/<file_id>.puffin", region_id: 47244640257(11, 1), table_id: 11, region_number: 1, region_group: 0, region_sequence: 1, file_id: "<file_id>", index_file_size: Some(6000), index_type: "fulltext_bloom", target_type: "column", target_key: "4", target_json: "{\"column\":4}", blob_size: 89, meta_json: Some("{\"bloom\":{\"bloom_filter_size\":64,\"row_count\":20,\"rows_per_segment\":4,\"segment_count\":5},\"fulltext\":{\"analyzer\":\"English\",\"bloom_version\":2,\"case_sensitive\":false}}"), node_id: None }
PuffinIndexMetaEntry { table_dir: "test/", index_file_path: "test/11_0000000001/index/<file_id>.puffin", region_id: 47244640257(11, 1), table_id: 11, region_number: 1, region_group: 0, region_sequence: 1, file_id: "<file_id>", index_file_size: Some(6000), index_type: "fulltext_tantivy", target_type: "column", target_key: "5", target_json: "{\"column\":5}", blob_size: 1000, meta_json: Some("{\"fulltext\":{\"analyzer\":\"Chinese\",\"case_sensitive\":true}}"), node_id: None }
PuffinIndexMetaEntry { table_dir: "test/", index_file_path: "test/11_0000000001/index/<file_id>.puffin", region_id: 47244640257(11, 1), table_id: 11, region_number: 1, region_group: 0, region_sequence: 1, file_id: "<file_id>", index_file_size: Some(6000), index_type: "inverted", target_type: "column", target_key: "1", target_json: "{\"column\":1}", blob_size: 518, meta_json: Some("{\"inverted\":{\"base_offset\":0,\"bitmap_type\":\"Roaring\",\"fst_size\":150,\"inverted_index_size\":518,\"null_bitmap_size\":8,\"relative_fst_offset\":368,\"relative_null_bitmap_offset\":0,\"segment_row_count\":1024,\"total_row_count\":20}}"), node_id: None }
PuffinIndexMetaEntry { table_dir: "test/", index_file_path: "test/11_0000000001/index/<file_id>.puffin", region_id: 47244640257(11, 1), table_id: 11, region_number: 1, region_group: 0, region_sequence: 1, file_id: "<file_id>", index_file_size: Some(6000), index_type: "inverted", target_type: "column", target_key: "2", target_json: "{\"column\":2}", blob_size: 515, meta_json: Some("{\"inverted\":{\"base_offset\":0,\"bitmap_type\":\"Roaring\",\"fst_size\":147,\"inverted_index_size\":515,\"null_bitmap_size\":8,\"relative_fst_offset\":368,\"relative_null_bitmap_offset\":0,\"segment_row_count\":1024,\"total_row_count\":20}}"), node_id: None }"#).await;
@@ -1500,7 +1500,10 @@ async fn check_request_skip_wal_recovery(flat_format: bool, skip_wal: bool) {
let before = RecordBatches::try_collect(stream).await.unwrap();
assert_eq!(before.iter().map(|b| b.num_rows()).sum::<usize>(), 4);
reopen_region(&engine, region_id, table_dir, false, HashMap::new()).await;
// Stop the engine directly, without region close, to exercise WAL-only recovery.
let engine =
restart_without_region_close(&mut env, engine, flat_format, region_id, table_dir).await;
let stream = engine
.scan_to_stream(region_id, ScanRequest::default())
.await
@@ -1625,7 +1628,7 @@ async fn check_request_skip_wal_mixed_batch_recovery(
skip_wal: bool,
flush_before_reopen: bool,
) {
use crate::region_write_ctx::RegionWriteCtx;
use crate::region_write_ctx::{RegionWriteCtx, WriteSource};
use crate::request::OptionOutputTx;
use crate::test_util::LogStoreImpl;
use crate::wal::Wal;
@@ -1657,6 +1660,7 @@ async fn check_request_skip_wal_mixed_batch_recovery(
&region.version_control,
region.provider.clone(),
None,
WriteSource::Request,
);
let mut receivers = Vec::with_capacity(4);
for (index, (skip, timestamp)) in [(false, 0), (skip_wal, 0), (false, 0), (skip_wal, 1)]
@@ -1692,8 +1696,7 @@ async fn check_request_skip_wal_mixed_batch_recovery(
ctx.add_wal_entry(&mut writer).unwrap();
let response = writer.write_to_wal().await.unwrap();
assert_eq!(response.last_entry_ids.get(&region_id), Some(&1));
ctx.write_memtable().await;
ctx.publish_sequence_and_entry_id();
ctx.write_memtables().await;
drop(ctx);
for rx in receivers {
assert_eq!(rx.await.unwrap().unwrap(), 2);
@@ -1751,9 +1754,13 @@ async fn check_request_skip_wal_mixed_batch_recovery(
);
}
// A no-flush close discards memtables. Only WAL-backed rows recover
// unless an explicit flush has already persisted all requests.
reopen_region(&engine, region_id, table_dir, true, HashMap::new()).await;
// Bypass region close so only WAL-backed rows recover unless the explicit
// flush above persisted all requests.
let engine =
restart_without_region_close(&mut env, engine, flat_format, region_id, table_dir).await;
engine
.set_region_role(region_id, RegionRole::Leader)
.unwrap();
let loses_skipped_rows = skip_wal && !flush_before_reopen;
let mut expected_values = if loses_skipped_rows {
vec![(0, 20.0), (1000, 1.0), (3000, 21.0)]
@@ -1830,3 +1837,38 @@ async fn request_skip_wal_values(engine: &MitoEngine, region_id: RegionId) -> Ve
values.sort_unstable_by_key(|(timestamp, _)| *timestamp);
values
}
/// Restarts workers without closing regions, preserving WAL-only recovery coverage.
async fn restart_without_region_close(
env: &mut TestEnv,
engine: MitoEngine,
flat_format: bool,
region_id: RegionId,
table_dir: String,
) -> MitoEngine {
let engine = env
.reopen_engine(
engine,
MitoConfig {
default_flat_format: flat_format,
..Default::default()
},
)
.await;
engine
.handle_request(
region_id,
RegionRequest::Open(RegionOpenRequest {
engine: String::new(),
table_dir,
options: HashMap::new(),
skip_wal_replay: false,
path_type: PathType::Bare,
checkpoint: None,
requirements: Default::default(),
}),
)
.await
.unwrap();
engine
}
+310 -5
View File
@@ -24,9 +24,11 @@ use api::v1::Rows;
use async_trait::async_trait;
use common_base::Plugins;
use common_base::readable_size::ReadableSize;
use common_error::ext::{ErrorExt, RetryHint};
use common_recordbatch::RecordBatches;
use common_time::util::current_time_millis;
use common_wal::options::{KafkaWalOptions, WAL_OPTIONS_KEY, WalOptions};
use datafusion_expr::{col, lit};
use parquet::basic::{Encoding, Type as PhysicalType};
use rstest::rstest;
use rstest_reuse::{self, apply};
@@ -35,9 +37,9 @@ use store_api::metadata::RegionMetadataRef;
use store_api::mito_engine_options::WRITE_BUFFER_SIZE_KEY;
use store_api::region_engine::{RegionEngine, RegionRole};
use store_api::region_request::{
AlterKind, PathType, RegionAlterRequest, RegionCloseRequest, RegionFlushRequest,
RegionOpenRequest, RegionPutRequest, RegionRequest, ReplayCheckpoint, SetRegionOption,
UnsetRegionOption,
AlterKind, PathType, RegionAlterRequest, RegionCloseRequest, RegionCompactRequest,
RegionFlushRequest, RegionOpenRequest, RegionPutRequest, RegionRequest, ReplayCheckpoint,
SetRegionOption, UnsetRegionOption,
};
use store_api::storage::{RegionId, ScanRequest};
use tokio::sync::Notify;
@@ -49,9 +51,11 @@ use crate::engine::listener::{EventListener, FlushListener, StallListener};
use crate::engine::region_hook::{RegionGcInfo, RegionHook, RegionHookRef, SstFileInfo};
use crate::error::Error;
use crate::manifest::action::RegionMetaActionList;
use crate::memtable::RangesOptions;
use crate::test_util::{
CreateRequestBuilder, LogStoreFactory, MockWriteBufferManager, TestEnv, build_rows,
build_rows_for_key, flush_region, kafka_log_store_factory, multiple_log_store_factories,
CreateRequestBuilder, LogStoreFactory, MockWriteBufferManager, TestEnv,
build_delete_rows_for_key, build_rows, build_rows_for_key, build_rows_with_fields, delete_rows,
delete_rows_schema, flush_region, kafka_log_store_factory, multiple_log_store_factories,
prepare_test_for_kafka_log_store, put_rows, raft_engine_log_store_factory, reopen_region,
rows_schema, single_kafka_log_store_factory,
};
@@ -1003,6 +1007,7 @@ async fn test_region_write_buffer_rejects_only_full_region_queue() {
.expect("the stalled queue should be rejected at the region hard limit")
.unwrap()
.unwrap_err();
assert_eq!(err.retry_hint(), RetryHint::Retryable);
assert_matches!(
err.into_inner().as_any().downcast_ref::<Error>().unwrap(),
Error::RejectWrite { .. }
@@ -1818,3 +1823,303 @@ async fn test_region_hook() {
manifest_count,
);
}
/// Explicitly finish a merge instead of sleeping for the background dispatcher.
fn compact_bulk_memtables(engine: &MitoEngine, region_id: RegionId) {
let version = engine.get_region(region_id).unwrap().version();
for memtable in version.memtables.list_memtables() {
memtable.compact(false).unwrap();
}
}
async fn scan_bulk_test_keys(engine: &MitoEngine, region_id: RegionId) -> String {
let mut schema = None;
let mut batches = Vec::new();
for key in ["deleted", "k"] {
let request = ScanRequest {
filters: vec![col("tag_0").eq(lit(key))],
..Default::default()
};
let stream = engine.scan_to_stream(region_id, request).await.unwrap();
schema = Some(stream.schema());
batches.extend(RecordBatches::try_collect(stream).await.unwrap().take());
}
RecordBatches::try_new(schema.unwrap(), batches)
.unwrap()
.pretty_print()
.unwrap()
}
#[rstest]
#[tokio::test]
async fn test_bulk_flush_keeps_newer_version(#[values(false, true)] delete_newer: bool) {
let mut env = TestEnv::new().await;
let config = MitoConfig {
default_flat_format: true,
min_compaction_interval: Duration::from_secs(3600),
..Default::default()
};
let engine = env.create_engine(config.clone()).await;
let region_id = RegionId::new(1, 1);
let request = CreateRequestBuilder::new()
.insert_option("memtable.type", "bulk")
.insert_option("memtable.bulk.merge_threshold", "2")
.insert_option("memtable.bulk.encode_row_threshold", "1")
.build();
let schema = rows_schema(&request);
let delete_schema = delete_rows_schema(&request);
engine
.handle_request(region_id, RegionRequest::Create(request.clone()))
.await
.unwrap();
put_rows(
&engine,
region_id,
Rows {
schema: schema.clone(),
rows: build_rows_for_key("k", 1, 2, 1),
},
)
.await;
put_rows(
&engine,
region_id,
Rows {
schema: schema.clone(),
rows: build_rows_for_key("a", 0, 1024, 0),
},
)
.await;
if delete_newer {
let mut rows = build_delete_rows_for_key("b", 0, 1023);
rows.extend(build_delete_rows_for_key("k", 1, 2));
delete_rows(
&engine,
region_id,
Rows {
schema: delete_schema,
rows,
},
)
.await;
} else {
let mut rows = build_rows_for_key("b", 0, 1023, 0);
rows.extend(build_rows_for_key("k", 1, 2, 2));
put_rows(
&engine,
region_id,
Rows {
schema: schema.clone(),
rows,
},
)
.await;
}
compact_bulk_memtables(&engine, region_id);
// Previously this advanced the old unordered row above the newer encoded row.
put_rows(
&engine,
region_id,
Rows {
schema,
rows: build_rows_for_key("c", 0, 1, 0),
},
)
.await;
let expected = if delete_newer {
"\
+-------+---------+----+
| tag_0 | field_0 | ts |
+-------+---------+----+
+-------+---------+----+"
} else {
"\
+-------+---------+---------------------+
| tag_0 | field_0 | ts |
+-------+---------+---------------------+
| k | 2.0 | 1970-01-01T00:00:01 |
+-------+---------+---------------------+"
};
assert_eq!(expected, scan_bulk_test_keys(&engine, region_id).await);
flush_region(&engine, region_id, None).await;
assert_eq!(expected, scan_bulk_test_keys(&engine, region_id).await);
let engine = env.reopen_engine(engine, config).await;
reopen_region(
&engine,
region_id,
request.table_dir,
false,
request.options,
)
.await;
assert_eq!(expected, scan_bulk_test_keys(&engine, region_id).await);
}
#[rstest]
#[tokio::test]
async fn test_bulk_last_non_null_merge_and_flush(#[values(false, true)] encoded: bool) {
let mut env = TestEnv::new().await;
let config = MitoConfig {
default_flat_format: true,
min_compaction_interval: Duration::from_secs(3600),
..Default::default()
};
let engine = env.create_engine(config.clone()).await;
let region_id = RegionId::new(1, 1);
env.get_schema_metadata_manager()
.register_region_table_info(
region_id.table_id(),
"test_table",
"test_catalog",
"test_schema",
None,
env.get_kv_backend(),
)
.await;
let request = CreateRequestBuilder::new()
.field_num(2)
.insert_option("memtable.type", "bulk")
.insert_option("merge_mode", "last_non_null")
.insert_option("memtable.bulk.merge_threshold", "4")
.insert_option(
"memtable.bulk.encode_row_threshold",
if encoded { "1" } else { "1000000" },
)
.insert_option("compaction.type", "twcs")
.insert_option("compaction.twcs.trigger_file_num", "2")
.build();
let schema = rows_schema(&request);
engine
.handle_request(region_id, RegionRequest::Create(request.clone()))
.await
.unwrap();
// An older SST must remain hidden behind a delete retained in the memtable.
put_rows(
&engine,
region_id,
Rows {
schema: schema.clone(),
rows: build_rows_with_fields("deleted", &[1], &[(Some(1), Some(1))]),
},
)
.await;
flush_region(&engine, region_id, None).await;
let mut a = build_rows_with_fields(
"a",
&(0..1023).collect::<Vec<_>>(),
&[(Some(0), None); 1023],
);
a.extend(build_rows_with_fields("k", &[1], &[(Some(1), None)]));
put_rows(
&engine,
region_id,
Rows {
schema: schema.clone(),
rows: a,
},
)
.await;
// Intermediate writes must be bulk parts, not an unordered merge barrier.
let mut b = build_rows_with_fields(
"b",
&(0..1023).collect::<Vec<_>>(),
&[(Some(0), None); 1023],
);
b.extend(build_rows_with_fields("k", &[1], &[(Some(2), None)]));
put_rows(
&engine,
region_id,
Rows {
schema: schema.clone(),
rows: b,
},
)
.await;
let mut deletes = build_delete_rows_for_key("d", 0, 1023);
deletes.extend(build_delete_rows_for_key("deleted", 1, 2));
delete_rows(
&engine,
region_id,
Rows {
schema: delete_rows_schema(&request),
rows: deletes,
},
)
.await;
let before = scan_bulk_test_keys(&engine, region_id).await;
assert!(before.contains("| k | 2.0"));
assert!(!before.contains("| deleted"));
let mut c = build_rows_with_fields(
"c",
&(0..1022).collect::<Vec<_>>(),
&[(Some(0), None); 1022],
);
c.extend(build_rows_with_fields("k", &[1], &[(None, Some(3))]));
c.extend(build_rows_with_fields("deleted", &[1], &[(None, Some(3))]));
put_rows(
&engine,
region_id,
Rows {
schema: schema.clone(),
rows: c,
},
)
.await;
compact_bulk_memtables(&engine, region_id);
// All four sources must have merged, with the requested output format.
let version = engine.get_region(region_id).unwrap().version();
let mut ranges = Vec::new();
for memtable in version.memtables.list_memtables() {
ranges.extend(
memtable
.ranges(None, RangesOptions::default())
.unwrap()
.ranges
.into_values(),
);
}
assert_eq!(1, ranges.len());
assert_eq!(encoded, ranges[0].encoded().is_some());
drop(ranges);
drop(version);
let expected = "\
+---------+---------+---------+---------------------+
| tag_0 | field_0 | field_1 | ts |
+---------+---------+---------+---------------------+
| deleted | | 3.0 | 1970-01-01T00:00:01 |
| k | 2.0 | 3.0 | 1970-01-01T00:00:01 |
+---------+---------+---------+---------------------+";
assert_eq!(expected, scan_bulk_test_keys(&engine, region_id).await);
// Leave a raw source beside merged parts so flush also exercises grouping.
put_rows(
&engine,
region_id,
Rows {
schema,
rows: build_rows_with_fields("z", &[0], &[(Some(0), None)]),
},
)
.await;
flush_region(&engine, region_id, None).await;
assert_eq!(expected, scan_bulk_test_keys(&engine, region_id).await);
engine
.handle_request(
region_id,
RegionRequest::Compact(RegionCompactRequest::default()),
)
.await
.unwrap();
assert_eq!(expected, scan_bulk_test_keys(&engine, region_id).await);
let engine = env.reopen_engine(engine, config).await;
reopen_region(
&engine,
region_id,
request.table_dir,
false,
request.options,
)
.await;
assert_eq!(expected, scan_bulk_test_keys(&engine, region_id).await);
}
+1 -5
View File
@@ -288,7 +288,7 @@ async fn test_prune_memtable_complex_expr_with_format(flat_format: bool) {
)
.await;
// ts filter will be ignored when pruning time series in memtable.
// Both the compound time range and the tag predicate are evaluated during the scan.
let filters = vec![time_range_expr(4, 7), col("tag_0").lt(lit("6"))];
let stream = engine
@@ -306,10 +306,6 @@ async fn test_prune_memtable_complex_expr_with_format(flat_format: bool) {
+-------+---------+---------------------+
| tag_0 | field_0 | ts |
+-------+---------+---------------------+
| 0 | 0.0 | 1970-01-01T00:00:00 |
| 1 | 1.0 | 1970-01-01T00:00:01 |
| 2 | 2.0 | 1970-01-01T00:00:02 |
| 3 | 3.0 | 1970-01-01T00:00:03 |
| 4 | 4.0 | 1970-01-01T00:00:04 |
| 5 | 5.0 | 1970-01-01T00:00:05 |
+-------+---------+---------------------+";
+23 -9
View File
@@ -39,7 +39,8 @@ use crate::cache::index::inverted_index::{CachedInvertedIndexBlobReader, Inverte
use crate::sst::file::RegionIndexId;
use crate::sst::index::bloom_filter::INDEX_BLOB_TYPE as BLOOM_BLOB_TYPE;
use crate::sst::index::fulltext_index::{
INDEX_BLOB_TYPE_BLOOM as FULLTEXT_BLOOM_BLOB_TYPE,
FULLTEXT_BLOOM_VERSION, INDEX_BLOB_TYPE_BLOOM as FULLTEXT_BLOOM_BLOB_TYPE,
INDEX_BLOB_TYPE_BLOOM_V1 as FULLTEXT_BLOOM_V1_BLOB_TYPE,
INDEX_BLOB_TYPE_TANTIVY as FULLTEXT_TANTIVY_BLOB_TYPE,
};
use crate::sst::index::inverted_index::INDEX_BLOB_TYPE as INVERTED_BLOB_TYPE;
@@ -126,7 +127,7 @@ pub(crate) async fn collect_index_entries_from_puffin(
);
entries.push(entry);
}
Some(BlobIndexTypeTargetKey::FulltextBloom(target_key)) => {
Some(BlobIndexTypeTargetKey::FulltextBloom(target_key, bloom_version)) => {
let bloom_meta = try_read_bloom_meta(
&reader,
region_index_id,
@@ -139,7 +140,7 @@ pub(crate) async fn collect_index_entries_from_puffin(
.await;
let bloom_value = bloom_meta.as_deref().map(bloom_meta_value);
let fulltext_value = Some(fulltext_meta_value(blob));
let fulltext_value = Some(fulltext_meta_value(blob, Some(bloom_version)));
let (target_type, target_json) = decode_target_info(target_key);
let meta_json = build_meta_json(bloom_value, fulltext_value, None);
let entry = build_index_entry(
@@ -154,7 +155,7 @@ pub(crate) async fn collect_index_entries_from_puffin(
entries.push(entry);
}
Some(BlobIndexTypeTargetKey::FulltextTantivy(target_key)) => {
let fulltext_value = Some(fulltext_meta_value(blob));
let fulltext_value = Some(fulltext_meta_value(blob, None));
let (target_type, target_json) = decode_target_info(target_key);
let meta_json = build_meta_json(None, fulltext_value, None);
let entry = build_index_entry(
@@ -392,12 +393,16 @@ fn bloom_meta_value(meta: &BloomFilterMeta) -> Value {
})
}
fn fulltext_meta_value(blob: &BlobMetadata) -> Value {
fn fulltext_meta_value(blob: &BlobMetadata, bloom_version: Option<u8>) -> Value {
let config = FulltextConfig::from_blob_metadata(blob).unwrap_or_default();
json!({
let mut value = json!({
"analyzer": config.analyzer.to_str(),
"case_sensitive": config.case_sensitive,
})
});
if let Some(version) = bloom_version {
value["bloom_version"] = json!(version);
}
value
}
fn inverted_meta_value(meta: &InvertedIndexMeta, metas: &InvertedIndexMetas) -> Value {
@@ -441,7 +446,8 @@ fn build_meta_json(
enum BlobIndexTypeTargetKey<'a> {
BloomFilter(&'a str),
FulltextBloom(&'a str),
/// Target key and version of a fulltext bloom blob.
FulltextBloom(&'a str, u8),
FulltextTantivy(&'a str),
Inverted,
}
@@ -451,9 +457,17 @@ impl<'a> BlobIndexTypeTargetKey<'a> {
if let Some(target_key) = Self::target_key_from_blob(blob_type, BLOOM_BLOB_TYPE) {
Some(BlobIndexTypeTargetKey::BloomFilter(target_key))
} else if let Some(target_key) =
// v2 goes first: the v1 type is a prefix of it.
Self::target_key_from_blob(blob_type, FULLTEXT_BLOOM_BLOB_TYPE)
{
Some(BlobIndexTypeTargetKey::FulltextBloom(target_key))
Some(BlobIndexTypeTargetKey::FulltextBloom(
target_key,
FULLTEXT_BLOOM_VERSION,
))
} else if let Some(target_key) =
Self::target_key_from_blob(blob_type, FULLTEXT_BLOOM_V1_BLOB_TYPE)
{
Some(BlobIndexTypeTargetKey::FulltextBloom(target_key, 1))
} else if let Some(target_key) =
Self::target_key_from_blob(blob_type, FULLTEXT_TANTIVY_BLOB_TYPE)
{

Some files were not shown because too many files have changed in this diff Show More