Skip to content

storage/remote: migrate Remote Write receivers to AppenderV2 - #19457

Closed
krajorama wants to merge 5 commits into
krajo/appenderv2-mandatory-exemplarsfrom
krajo/rw1-receiver-appenderv2-v2
Closed

krajorama wants to merge 5 commits into
krajo/appenderv2-mandatory-exemplarsfrom
krajo/rw1-receiver-appenderv2-v2

Conversation

@krajorama

@krajorama krajorama commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

Part of #17632. Stacked on top of #19455 (mandatory, batched AppendExemplars) — base branch is krajo/appenderv2-mandatory-exemplars.

What

Both Remote Write receiver paths are ported from V1 storage.Appender to storage.AppenderV2:

  • writeHandler.write (RW 1.0)
  • writeHandler.writeV2 / appendV2 (RW 2.x)

This was originally split into two stacked PRs (#19457 for RW 1.0, #19459 for RW 2.x) because RW 2.x's exemplars didn't have an obvious home under the old, optional ExemplarAppenderV2 capability design. Since #19455 made AppendExemplars mandatory and batched, that reason to split no longer applies, so both migrations are combined here. #19459 is superseded by this PR.

RW 1.0 (write)

Since AppendExemplars is mandatory on AppenderV2, the receiver just calls app.AppendExemplars(ref, ls, exemplars) directly — no capability-bundle interface, no type assertion, no "does this appender support exemplars" error path. Metadata is untouched: RW 1.0's per-series path never called UpdateMetadata even before this change (confirmed by inspection), so there's nothing to wire up.

RW 2.x (writeV2 / appendV2)

  • Start timestamps are now passed directly as the AppenderV2.Append st parameter; the appender owns ST zero-sample injection, so the explicit AppendSTZeroSample / AppendHistogramSTZeroSample calls (and the handleHistogramZeroSample helper) are removed. The receiver still gates on ingestSTZeroSample, preserving prior behaviour.
  • Metadata is attached via AppendV2Options.Metadata on every sample/histogram append of a series (not just the first) — it's best-effort and Append no longer reports metadata errors, so samplesAppendedWithoutMetadata is no longer incremented on this path (the metric stays registered and is still used by the RW 1.0 path). AppenderV2 implementations (e.g. TSDB head) only persist a metadata WAL record when it actually changed, so attaching it on every append instead of just the first is a cheap no-op in the common case, and closes a gap where a rejected first sample would otherwise silently drop metadata for the whole series in that request.
  • Exemplars are collected per series, sorted by timestamp (required by AppendExemplars's contract), and appended in one batched AppendExemplars call instead of a per-exemplar loop. Out-of-order exemplars still map to a 400; other exemplar errors remain a no-op — preserving prior behaviour.

Both paths

With both RW 1.0 and 2.x on AppenderV2, the V1 appendable is no longer used by the receiver at all: NewWriteHandler drops its storage.Appendable parameter entirely, and the now-dead remoteWriteAppender wrapper is removed.

Also includes a small errors.As → errors.AsType modernization to match the Go 1.26 baseline main picked up during rebase.

Tests

  • mockAppenderV2.Append mirrors what a real AppenderV2 implementation does internally: it injects the ST zero-sample (via the existing V1 mock methods) when st != 0, and records metadata via opts.Metadata when present — both best-effort, matching production semantics.
  • Added a case exercising metadata stored via AppendV2Options (appendMetadata: true), which wasn't previously covered — the existing table only tested the disabled case.
  • mockAppendable gained an AppenderV2 method reusing the existing V1 recording/error-injection methods (AppendHistogram, AppendExemplar, Commit, Rollback) since their signatures are compatible, so existing assertions on the mock's recorded slices hold unchanged.
NONE

@krajorama
krajorama force-pushed the krajo/appenderv2-mandatory-exemplars branch 2 times, most recently from c8fbef3 to 89ef560 Compare August 26, 2026 13:18
@krajorama
krajorama force-pushed the krajo/rw1-receiver-appenderv2-v2 branch from 7b881d7 to 3dc06b2 Compare August 26, 2026 13:24
@krajorama krajorama changed the title storage/remote: migrate Remote Write 1.0 receiver to AppenderV2 storage/remote: migrate Remote Write receivers to AppenderV2 Aug 26, 2026
Comment thread storage/remote/write_handler.go Outdated

allSamplesSoFar := rs.AllSamples()
var ref storage.SeriesRef
// Attach metadata to the first sample/histogram append of the series only; if that

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we attach to all which is likely fine - (cause we typically have a single sample) - and if we update for multiple it's deduped. Comment is lying ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a line later to clear the whole options struct, so the comment is correct. On the other hand it makes little sense to actually do this now without exemplars. Removing the reset.

Port the RW 1.0 receiver path (writeHandler.write) from V1
storage.Appender to storage.AppenderV2. Since AppenderV2 makes
AppendExemplars mandatory, the receiver can call it directly (batched,
one call per series) without any capability-detection dance. Metadata
is unused by RW 1.0's per-series path, so no metadata plumbing is added.

NewWriteHandler now also takes an appendableV2; the Remote Write 2.x
path (writeV2) still uses the V1 appendable until it is migrated
separately.

Part of #17632.

Signed-off-by: György Krajcsovits <[email protected]>
Port the Remote Write 2.x receiver path (writeHandler.writeV2 / appendV2)
from V1 storage.Appender to storage.AppenderV2, matching the RW 1.0 path.

- Start timestamps are now passed directly as the AppenderV2.Append st
  parameter; the appender owns ST zero-sample injection, so the explicit
  AppendSTZeroSample / AppendHistogramSTZeroSample calls (and the
  handleHistogramZeroSample helper) are removed. The receiver still gates
  on ingestSTZeroSample, preserving prior behaviour.
- Metadata is attached via AppendV2Options.Metadata on the first
  sample/histogram append of each series; if that append is rejected,
  metadata is dropped with it. It is best-effort: Append no longer
  reports metadata errors, so samplesAppendedWithoutMetadata is no
  longer incremented on this path (the metric stays registered and is
  still used by the RW 1.0 path).
- Exemplars are collected per series, sorted by timestamp, and appended
  in one batched AppendExemplars call instead of a per-exemplar loop.
  Out-of-order exemplars still map to a 400; other exemplar errors
  remain a no-op, preserving prior behaviour.

With both RW 1.0 and 2.x on AppenderV2, the V1 appendable is no longer
used by the receiver: NewWriteHandler drops its storage.Appendable
parameter and the now-dead remoteWriteAppender wrapper is removed.

Part of #17632.

Signed-off-by: György Krajcsovits <[email protected]>
Match the errors.AsType convention adopted for AppendPartialError
detection elsewhere in the codebase now that go.mod requires Go 1.26.

Signed-off-by: György Krajcsovits <[email protected]>
Previously metadata was only attached to the first sample/histogram
Append of a series, cleared to empty options right after (success or
failure). If that first append was rejected (e.g. out-of-order), the
whole request silently dropped metadata for that series, even if a
later sample/histogram in the same request succeeded.

Attach it on every append instead. AppenderV2 implementations already
persist a metadata WAL record only when it actually changed (see
head_append_v2.go's metaChanged check), so this costs a cheap
comparison per extra sample and closes the drop-on-first-reject gap
for free.

Signed-off-by: György Krajcsovits <[email protected]>
Signed-off-by: György Krajcsovits <[email protected]>
}
if !opts.Metadata.IsEmpty() {
// Metadata is best-effort within Append; errors are not surfaced to the caller.
_, _ = a.asV1().UpdateMetadata(ref, l, opts.Metadata)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if best-effort 🤔

}
// AppendExemplars requires exemplars sorted by timestamp.
slices.SortFunc(exemplars, exemplar.Compare)
if _, err := app.AppendExemplars(ref, ls, exemplars); err != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In PRW 1.0, if a series only has native histograms and exemplars without float samples, ref is 0 and the series does not exist yet in TSDB Head, causing AppendExemplars to fail and silently drop them. Should we run appendV1Histograms before AppendExemplars like appendV2 does?

}

func (h *writeHandler) appendV2(app storage.Appender, req *writev2.Request, rs *remoteapi.WriteResponseStats) (samplesWithoutMetadata, errHTTPCode int, err error) {
func (h *writeHandler) appendV2(app *remoteWriteAppenderV2, req *writev2.Request, rs *remoteapi.WriteResponseStats) (samplesWithoutMetadata, errHTTPCode int, err error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this accept storage.AppenderV2 instead of *remoteWriteAppenderV2 to match appendV1Samples and appendV1Histograms?

Suggested change
func (h *writeHandler) appendV2(app *remoteWriteAppenderV2, req *writev2.Request, rs *remoteapi.WriteResponseStats) (samplesWithoutMetadata, errHTTPCode int, err error) {
func (h *writeHandler) appendV2(app storage.AppenderV2, req *writev2.Request, rs *remoteapi.WriteResponseStats) (samplesWithoutMetadata, errHTTPCode int, err error) {

// of the series; implementations only persist a metadata WAL record when it changed.
opts := storage.AOptions{}
if h.appendMetadata {
opts.Metadata = m

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Head (headAppenderV2.Append), metaChanged checks s.meta, which is only updated on commit. For any new series or metadata change with multiple samples in a request, every sample evaluates metaChanged == true, acquiring the lock and writing duplicate metadata WAL records. Maybe clear opts.Metadata once an append succeeds so subsequent samples in the series skip it?

}

ref, err = app.Append(ref, ls, s.GetTimestamp(), s.GetValue())
ref, err = app.Append(ref, ls, st, s.GetTimestamp(), s.GetValue(), nil, nil, opts)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an append fails with an out-of-order or duplicate error, app.Append returns (0, err), resetting ref to 0 for subsequent samples and exemplars in the series. Maybe only update ref on success?

if pErr, ok := errors.AsType[*storage.AppendPartialError](err); ok {
appended -= len(pErr.ExemplarErrors)
for _, eErr := range pErr.ExemplarErrors {
if errors.Is(eErr, storage.ErrOutOfOrderExemplar) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ErrOutOfBounds from AppendExemplars falls through to the error log and is dropped without failing the request. Should we also check errors.Is(eErr, storage.ErrOutOfBounds) here so out-of-bounds exemplars return 400 bad request like samples and histograms do?

if err := fh.ReduceResolution(histogram.ExponentialSchemaMax); err != nil {
return 0, err
}
ref, err := app.AppenderV2.AppendExemplars(ref, ls, valid)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If all exemplars were in the future (len(valid) == 0), delegating to AppendExemplars causes Head to return (0, nil) which clobbers ref to 0. Maybe return early if len(valid) == 0?

expectedCode: http.StatusNoContent,
enableTypeAndUnitLabels: false,
appendMetadata: true,
expectedLabels: labels.FromStrings("__name__", "test_metric_wal", "instance", "localhost"),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because expectedLabels is set, this test case hits the early return at line 805 and skips the appendable.metadata assertions at lines 862-867. We should assert appendable.metadata here so metadata storage via opts.Metadata is actually verified.

@krajorama

Copy link
Copy Markdown
Member Author

Closed in favor of #19812

@krajorama krajorama closed this Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants