Skip to content

fix(stats): prevent aggregation/reindex loop from legacy stats migration - #665

Draft
zzacharo wants to merge 2 commits into
CERNDocumentServer:masterfrom
zzacharo:fix/stats-aggregation-loop
Draft

zzacharo wants to merge 2 commits into
CERNDocumentServer:masterfrom
zzacharo:fix/stats-aggregation-loop

Conversation

@zzacharo

@zzacharo zzacharo commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes the code path behind #664 (stats aggregation/reindex loop after legacy-stats migration).

Problem

The post-migration stats aggregate command reset every aggregation bookmark to the year 2000 and then aggregated the whole history in a single synchronous pass. That pass never completes, so set_bookmark (the last line of invenio_stats StatAggregator.run()) is never reached — the bookmark stays at 2000 and the hourly aggregation re-processes ~26 years of events forever, re-stamping historical stats docs and triggering a full-corpus reindex (reindex_stats) every cycle.

Changes

  • cds_migrator_kit/rdm/stats/event_generator.py (and the videos equivalent): set updated_timestamp to the ingestion time on migrated events, mirroring invenio-stats' live EventsIndexer (processors.py). Previously absent for the RDM path — which disables the aggregator's skip guard for migrated buckets. Ingestion time (not the historical event time) is required so a live backfill still treats the buckets as pending.
  • cds_migrator_kit/rdm/cli.py — rewrite stats aggregate:
    • remove the BOOKMARK_START_DATE = 2000-... constant and the delete-all-bookmarks + reset logic;
    • require --start-date, process [start, end] month-by-month (_month_chunks);
    • call aggregate_events(..., update_bookmark=False) so the live bookmarks are never reset or moved — safe to run on a live system.

Notes for reviewers / operators

closes #664

🤖 Generated with Claude Code


Update after review (supersedes the points below where they differ)

The goal is to migrate the stats events and aggregate them without changing any bookmark. The first version of this PR did not fully reach that, so the following was changed and justified:

1. Migrated events carry no updated_timestamp (reverted from the first version)

  • StatAggregator.agg_iter skips a bucket when max(updated_timestamp) < live bookmark. The live bookmark moves forward every hour with the beat.
  • With an ingestion-time updated_timestamp on the events, a backfill started more than ~1h after ingestion (or after the next beat) would find every migrated bucket "already aggregated" and silently aggregate nothing. It would only work with the stats beat paused.
  • Without the field, last_update is null and the guard never fires: every bucket of the requested range is aggregated, whatever the bookmark is.
  • The aggregator selects events by their event timestamp, not by updated_timestamp, so the --start-date/--end-date range is unaffected.
  • The original cause of the loop was the reset of the bookmarks to 2000 plus the single never-finishing pass, not the missing field.
  • The CDS-Videos generator already set updated_timestamp before this PR and is left as it was (the same timing caveat applies there).

2. stats aggregate changes no bookmark, not even stats_reindex

  • It only calls aggregate_events(..., update_bookmark=False), month by month.
  • The reindex_stats(...) call at the end was removed: it sets the stats_reindex bookmark to now.
  • The records are still reindexed: every aggregate document is written with a fresh updated_timestamp (agg_iter, even for a bucket of 2017), and the regular reindex_stats task selects the aggregate documents with updated_timestamp >= its bookmark, collects their parent_recid and reindexes those records. A backfill is therefore picked up by its next run (also if the task was paused: its bookmark stays old).
  • Re-running is safe: the aggregate _id is fixed (<key>-<day>) and the count is recomputed from the raw events, so nothing is counted twice.

3. Operator notes

  • The command is invenio migration stats aggregate --start-date <first day> --end-date <last day before the first live event> (the first description wrote invenio cds-migrator-kit rdm stats aggregate, which is not the command).
  • Set --end-date to the day before the first live event: a day that already holds live events newer than the live bookmark is skipped by the aggregator, so its migrated events would be missed.
  • The month-by-month split only bounds the size of each call; the aggregation index names follow the index_interval of the aggregation configuration, not the chunks.
  • The recipe (migration_cli/openshift_migration-recipe.md), step_by_step_documentation.md and the videos README no longer tell to delete or reset the stats-bookmarks index.

4. Tests

tests/cds-rdm/test_stats_aggregate.py: month chunks (start mid-month, single day, year rollover and leap year), update_bookmark=False on every call, no reindex_stats and no set_bookmark, --start-date required and the range validated.

Still to verify on sandbox

Ingest events of one collection, run aggregate for a narrow range, and check that: the bookmark documents are unchanged, the aggregate indices grow, and the migrated records show the stats after the next reindex_stats run.

zzacharo and others added 2 commits November 3, 2026 11:17
The post-migration `stats aggregate` command reset every aggregation
bookmark to the year 2000 and then aggregated the whole history in a
single synchronous pass. That pass never completes, so the bookmark is
never advanced; the hourly aggregation then re-processes ~26 years of
events forever, re-stamping historical stats docs and triggering a
full-corpus reindex every cycle.

- stats/event_generator.py (rdm + videos): set `updated_timestamp` to the
  ingestion time on migrated events, mirroring invenio-stats' live
  EventsIndexer (processors.py). Without it the aggregator's skip guard
  can never treat migrated buckets as already-processed.
- rdm/cli.py: rewrite `stats aggregate` to backfill a bounded date range
  month-by-month with `update_bookmark=False`, so it never resets or moves
  the live aggregation bookmarks (safe to run on a live system).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Migrated events no longer get an updated_timestamp: with it, the aggregator
skips every migrated bucket once the live bookmark moves past the ingestion
time, so the backfill depended on pausing the stats beat. Without it the
skip guard never fires and the requested date range is always aggregated.

stats aggregate no longer calls reindex_stats (which moves the stats_reindex
bookmark): the regular task reindexes the records, as every aggregate is
written with a fresh updated_timestamp.

Docs no longer tell to delete or reset the stats-bookmarks index, and the
tests cover the month chunks and that no bookmark is written.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Dec 4, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • 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

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.

Stats aggregation/reindex loop after legacy-stats migration (bookmark reset to year 2000)

2 participants