Skip to content

database: keep the first sample after a collection gap as the baseline - #25235

Draft
stelfrag wants to merge 3 commits into
netdata:masterfrom
stelfrag:fix-gap-reset-baseline
Draft

stelfrag wants to merge 3 commits into
netdata:masterfrom
stelfrag:fix-gap-reset-baseline

Conversation

@stelfrag

@stelfrag stelfrag commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

When a chart is not collected for long enough, rrdset_timed_done() resets it (under dbengine, a gap of more than 60 × update_every, for example a suspended host or a stalled collector). The reset cleared the collection count of the sample that triggered it, so the next sample was treated as another baseline and the first increase after the gap was lost: a counter that returns as 5000, then 5001, stored no event (#25215). The "too old data" reset also cleared the chart clock after the returning sample had set it.

Change

  • The sample that triggers the reset is the first of the new collection: its values become the baselines and store no rate, and the next sample measures its increase from them. A dimension missing from that sample takes its baseline when it next reports.
  • Both reset paths start the chart clock at the returning sample.
  • The reset drops what belongs before the gap: the unstored remainder of an incremental increase and the cached last stored value (/api/v1/allmetrics shows no value for the returning dimension until its next point). ML gets a best-effort missing-point notice, so its prediction window does not span the gap.
  • Over v1 streaming, the child sends that sample as BEGIN "<chart>" 0, so the parent measures the elapsed time with its own clock.
  • percentage-of-incremental-row leaves dimensions that are taking a baseline out of the row total; when every updated dimension has a baseline, the arithmetic is unchanged.

Compatibility

  • Charts store the increase measured after a gap that they used to drop. Nothing is estimated for the gap itself.
  • percentage-of-incremental-row values change only on a collection where a dimension is taking its baseline.
  • The database format and the v2 streaming protocol are unchanged.

Testing

  • -W pluginsd-sample-times adds seven gap cases (both reset paths with and without store_first, a remainder from before the gap, a dimension missing the returning sample, incremental-row percentages). They fail on master and pass with the fix.
  • Full -W unittest passes.

Fixes #25215


Summary by cubic

Fixes the collection-gap reset so the returning sample is kept as the new baseline and the first increase after a gap is no longer lost. Previously the reset cleared the returning sample's collection count, so the next sample was treated as another baseline and the increase was dropped (#25215).

Side effects

  • Both reset paths start the chart clock at the returning sample; a dimension missing from that sample takes its baseline when it next reports.
  • The reset drops the unstored remainder, the cached last stored value, and ML's prediction window across the gap.
  • Over v1 streaming, the child sends BEGIN "<chart>" 0 so the parent measures elapsed time with its own clock; the v2 protocol is unchanged.
  • percentage-of-incremental-row excludes dimensions taking a baseline from the row total.
  • The database format is unchanged; charts now store increases after gaps they previously dropped.
  • The gap tests require one point at every slot after the returning sample, not only the correct total of stored rates.

Written for commit 5bad18a. Summary will update on new commits.

View guided diff

Summary by CodeRabbit

  • Bug Fixes
    • Charts now handle long gaps in incoming data more reliably. The first sample after a gap establishes a fresh baseline, preventing stale rate calculations and unexpected spikes.
    • Percentage-of-increment calculations now exclude initial samples and dimensions restarting after a gap, improving reported values.
    • Streamed chart updates now correctly indicate when no duration should be applied after a reset.

When a chart is reset after a long collection gap, the reset cleared the
collection count of the sample that triggered it, so the next sample was
treated as a baseline too and its increase was lost (netdata#25215).

The reset now keeps the returning sample as the first of the new
collection and drops what belongs before the gap: the unstored
remainder, the last stored value and ML's prediction window. The "too
old data" branch restarts the chart clock at the returning sample, a v1
parent is told to measure the gap itself, and incremental-row
percentages ignore dimensions at their first value.
The v1 BEGIN without a duration is now decided by the reset itself and
passed to the sender, instead of through resync_time_s, which sender and
replication threads also write. The incremental-row denominator keeps
its original arithmetic, over the dimensions past their first value.
The reset's ML notices are one chart statistics update, like a slot.
@coderabbitai

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Collection reset handling now retains the returning sample as a baseline, clears stale rate state, and reports resets through v1 streaming. PCENT_OVER_DIFF_TOTAL now uses increments from dimensions with prior collected values. New sample-time tests cover gap recovery and resulting rates.

Changes: Collection gap recovery

Layer / File(s) Summary
Reset handling, rates, and streaming
src/database/rrdset-collection.c, src/streaming/protocol/command-begin-set-end-v1.c, src/streaming/protocol/commands.h, src/plugins.d/pluginsd_parser.c, src/plugins.d/pluginsd_sample_times-unittest.c
Reset paths retain updated dimensions as baselines and clear stale state. Percentage calculations use increments from dimensions with more than one collected value. The v1 streaming call carries a reset flag and can write a zero duration. Sample-time tests cover long and exact-age gaps, partial dimension updates, store_first, and percentage calculations.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • netdata/netdata#24991: Updates chart clock and incremental-rate handling for samples that are not newer than the chart clock.

Suggested reviewers: vkalintiris, ferroin, ktsaou

Merge Risk: Minimal · up to 5bad1

This change makes charts record the first real counter increase after a long collection gap, without estimating the silent period. Review found no concrete defect in the reset, baseline, or streaming paths, and the new sample-time tests cover the gap cases. Merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the first sample after a collection gap becomes the baseline. The raw summary and objectives confirm this is the central fix. The title is specific and concise. It doe…
Linked Issues check Passed [#25215] requires that the first accepted reading after a long gap set a fresh baseline, and that the next reading record the increase from it, with no estimate for the silent interval. At the reviewe…
Out of Scope Changes check Passed The changes that go beyond the reset function each connect to the gap fix. (1) src/plugins.d/pluginsd_parser.c passes false to stream_send_rrdset_metrics_v1 and src/streaming/protocol/command-begin-se…
Full details: Title check

Explanation

The title names the main change: the first sample after a collection gap becomes the baseline. The raw summary and objectives confirm this is the central fix. The title is specific and concise. It does not cover the v1 streaming flag, ML notice, or percentage-of-incremental-row change, which is acceptable.

Full details: Linked Issues check

Explanation

[#25215] requires that the first accepted reading after a long gap set a fresh baseline, and that the next reading record the increase from it, with no estimate for the silent interval. At the reviewed head, rrdset_collection_reset() in src/database/rrdset-collection.c implements this. It resets st->counter and st->counter_done to 0. For each dimension updated by the returning sample, it sets rd->collector.counter = 1, so that sample is kept as the first of the new collection. The reset also clears rd->collector.last_calculated_value (the unstored remainder) and sets rd->collector.last_stored_value to NAN (the cached value that /api/v1/allmetrics reads). It calls ml_dimension_is_anomalous(rd, now.tv_sec, 0, false) to give ML a missing-point notice, and it flushes storage per tier. The PR description states that the long-gap path and the too-old path both start the chart clock at the returning sample, and that a dimension absent from the returning sample takes its baseline when it next reports. The pluginsd_sample_times gap tests (seven cases, including store_first, a nonzero baseline, a remainder from before the gap, a late dimension, and percentage-of-incremental-row) cover the issue's regression list. I did not run these tests. The diff tool was unavailable, so the reset path was checked from the head file, which was truncated after the reset function. The incremental-calculation hunk was not read directly.

Full details: Out of Scope Changes check

Explanation

The changes that go beyond the reset function each connect to the gap fix. (1) src/plugins.d/pluginsd_parser.c passes false to stream_send_rrdset_metrics_v1 and src/streaming/protocol/command-begin-set-end-v1.c adds a no_duration flag. Together they send BEGIN with duration 0 for the returning sample, so the parent measures elapsed time with its own clock. The v2 protocol is not changed. (2) The percentage-of-incremental-row change (PCENT_OVER_DIFF_TOTAL denominator) excludes dimensions that are taking a baseline. Without it, a baseline dimension would distort the row total on the returning sample. The PR states that results are unchanged when all updated dimensions already have baselines. (3) The ML missing-point notice and the storage flush support the no-span requirement for ML prediction windows. The issue names the partial-update and percentage-related paths as regression coverage. The test file changes add the gap cases. I found no change unrelated to #25215. The percentage-row and v1 streaming hunks were assessed from the PR summary and the head reset function, not from a full diff.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 940add04-2958-4359-9ea8-2dfee8164b65
Commits

Reviewing files that changed from the base of the PR and between fb93940 and 5bad18a.

Files selected for processing: 5

  • src/database/rrdset-collection.c
  • src/plugins.d/pluginsd_parser.c
  • src/plugins.d/pluginsd_sample_times-unittest.c
  • src/streaming/protocol/command-begin-set-end-v1.c
  • src/streaming/protocol/commands.h

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 5 files

Architecture diagram
sequenceDiagram
    participant Collector as Collector (Plugin)
    participant Parser as pluginsd Parser
    participant RRDSet as rrdset_timed_done()
    participant RRDDim as Dimension (rrddim)
    participant ML as ML Engine
    participant Store as Storage Engine (dbengine)
    participant Stream as Streaming Buffer

    Note over Collector,Stream: Collection Gap Reset Flow

    Collector->>Parser: SET dimension values
    Parser->>RRDSet: END chart (with collected values)
    
    RRDSet->>RRDSet: Detect gap timeout or stale clock
    RRDSet->>RRDSet: rrdset_collection_reset(st, now)

    Note over RRDSet,RRDDim: NEW: Returning sample becomes baseline
    RRDSet->>RRDDim: For each updated dimension
    RRDDim->>RRDDim: CHANGED: Set counter=1 (keep as baseline)
    RRDDim->>RRDDim: NEW: Clear last_calculated_value
    RRDDim->>RRDDim: NEW: Set last_stored_value=NAN

    Note over RRDDim,ML: NEW: Missing point notification for ML
    RRDSet->>ML: ml_chart_update_begin(st)
    RRDSet->>RRDDim: For each dimension
    RRDDim->>ML: ml_dimension_is_anomalous(rd, now.tv_sec, 0, false)
    RRDSet->>ML: ml_chart_update_end(st)

    RRDSet->>Store: storage_engine_store_flush() for each tier

    alt Reset via long gap (timeout)
        RRDSet->>RRDSet: Set usec_since_last_update
        RRDSet->>RRDSet: store_this_entry=0, first_entry=1
    else Reset via too-old data
        RRDSet->>RRDSet: CHANGED: Restart clock at returning sample
        RRDSet->>RRDSet: Set usec_since_last_update
    end

    Note over RRDSet,Stream: CHANGED: percentage-of-incremental-row calculation
    RRDSet->>RRDSet: Compute incremental_collected_total
    RRDSet->>RRDSet: Exclude baseline dimensions from totals

    RRDSet->>Stream: stream_send_rrdset_metrics_v1(rsb, st, reset)
    
    alt reset == true AND streaming v1
        Stream->>Stream: NEW: Send BEGIN "<chart>" 0 (no duration)
        Note over Stream: Parent measures elapsed time itself
    else streaming v2
        Stream->>Stream: Send metrics as usual (unchanged)
    end

    Stream-->>Collector: Confirmation (via normal flow)
Loading

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

The gap cases checked only the sum of the rates stored after the gap, so
a missing or misplaced slot could pass. Each case now requires one point
at every slot of the samples after the gap, and the chart's last point
at the last of them.
@sonarqubecloud

sonarqubecloud Bot commented Dec 4, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DBENGINE loses the first incremental counter increase after a long collection gap

1 participant