Skip to content

feat: Report the materialization window in write-time metrics - #388

Merged
Manisha4 merged 3 commits into
masterfrom
feat/materialization-metrics-window
Nov 3, 2026
Merged

Manisha4 merged 3 commits into
masterfrom
feat/materialization-metrics-window

Conversation

@Manisha4

@Manisha4 Manisha4 commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

What

Each feature view's write-time stats (MaterializationMetricsAggregator.to_dict(), read with drain_run_results()) now include two keys:

  • start_date: the start of the window Feast materialized for that feature view run
  • end_date: the end of that window

Both are tz-aware UTC datetimes. They come from the MaterializationTask the provider gives the compute engine. That means they already include the defaults materialize_incremental() applies (the previous end time, now minus ttl, or one year back) and the timezone handling in materialize(). A non-UTC input is converted to UTC, so the instant stays the same.

Why

Right now the materialization job works out the incremental start date by copying Feast's materialize_incremental() rules, and that copy can fall out of sync with Feast. If Feast reports the window it actually used, the job doesn't need to copy those rules.

How

  • The aggregator takes optional start_date / end_date, converts them to UTC and adds them to to_dict(). build_aggregator passes them through.
  • They are set from the task in ComputeEngine.get_execution_context, which covers the local and Spark DAG paths. The Spark engine's offline-store path, which the Expedia provider uses, sets them too.
  • Per-partition stats from Spark executors have no window. merge_stats and merge_from_dict both take the earliest start_date and the latest end_date, so the result doesn't depend on merge order.
  • Driver-side collectors are built with build_aggregator_for_task(task, ...), so a call site can't forget the window. The Spark offline-store path now takes the task.
  • _to_utc uses make_tzaware, and max_event_timestamp is normalized to UTC too, so every timestamp in the stats follows one convention.
  • The /materialize endpoint and feast materialize --disable-event-timestamp end the window at the UTC time instead of the host's local time, matching upstream Feast. This only changes anything on hosts not running in UTC.
  • merge_stats unions fields_written with a set for membership, keeping order. The list scan was quadratic in the number of features, which added up to 0.1–2 s on the driver for wide feature views folded over hundreds of Spark partitions.
  • Nothing changes when metrics are off: no aggregator is created and nothing is recorded.

The new keys are additive. Older eg-feast versions such as 0.9.19 don't emit them, so consumers should treat them as optional.

Testing

  • New unit tests cover:
    • the window defaults, UTC conversion and build_aggregator
    • merges that keep the window
    • FeatureStore.materialize() and materialize_incremental() through the local engine, both the first run (start from ttl) and a later run (start from the previous end)
    • the Spark offline-store path
    • order-independent merges, UTC max_event_timestamp, and the UTC end on the /materialize and CLI paths
    • the metrics-off path
  • pytest tests/unit/test_materialization_metrics.py tests/unit/infra/compute_engines passes in the default, America/Los_Angeles and Asia/Kolkata time zones (Ray tests not run locally).
  • ruff check and ruff format --check are clean. mypy feast reports the same results as master.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment on lines +99 to +105
def _to_utc(ts: Optional[datetime]) -> Optional[datetime]:
"""Return ``ts`` as a tz-aware UTC datetime; naive values are taken as UTC."""
if ts is None:
return None
if ts.tzinfo is None:
return ts.replace(tzinfo=timezone.utc)
return ts.astimezone(timezone.utc)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: _to_utc re-implements feast.utils.make_tzaware (naive → UTC) and adds an .astimezone(). feature_store.materialize already calls make_tzaware on these same values, so we'd now have two helpers encoding the same rule that could drift apart. Suggest:

return None if ts is None else make_tzaware(ts).astimezone(timezone.utc)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good call. _to_utc is now your one-liner, so there's only one naive-is-UTC rule. I kept the .astimezone() because make_tzaware leaves aware non-UTC values as they are, and the UTC-8 tests fail without it.

In 9edeb9c.

"""Return ``ts`` as a tz-aware UTC datetime; naive values are taken as UTC."""
if ts is None:
return None
if ts.tzinfo is None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

_to_utc treats naive datetimes as UTC, which mislabels the window on the /materialize path with disable_event_timestamp=True. feature_server.py:440 builds end_date = datetime.now() (naive local time) and start_date = datetime(1970,1,1). On a UTC-8 host the metrics row reports an end_date 8h earlier than the real run end, labelled as UTC. The start_date of 1970 is also misleading, because that path loads all data regardless of window. Should that path use datetime.now(timezone.utc), or leave the window as None when event timestamps are disabled?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good catch, but the mislabel happens before _to_utc. FeatureStore.materialize already runs make_tzaware on that naive datetime.now(), so the query end and the registry interval were off by the host offset too. I switched this endpoint and the matching CLI path to utils._utc_now(), the same fix upstream Feast already made, and kept the 1970 start because no engine in our fork reads disable_event_timestamp, so [1970, now) really is the window that gets queried.

In 9edeb9c.

Comment on lines +139 to +140
self.start_date = _to_utc(start_date)
self.end_date = _to_utc(end_date)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

start_date/end_date are now tz-aware UTC, but max_event_timestamp is stored raw (naive for e.g. a parquet source with a naive timestamp column). A consumer computing lag as end_date - max_event_timestamp will hit "can't subtract offset-naive and offset-aware datetimes". Suggest normalizing max_event_timestamp with the same _to_utc in observe_event_timestamp so all timestamps in the payload follow one convention.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Agreed. observe_event_timestamp now goes through the same _to_utc, so max_event_timestamp is tz-aware UTC like the window, whether it comes from a batch or from merged partition stats. This was a real case, because Spark hands us naive values. FSM already treated naive as UTC, so the recorded value and lag stay the same.

In 9edeb9c.

Comment on lines +235 to +236
"start_date": self.start_date,
"end_date": self.end_date,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

to_dict() now emits two new keys (start_date, end_date) in every run result. Is the feature-store-materialization consumer / metrics table ready for these? If it builds rows with a fixed schema (Row(**stats), a typed DataFrame, an explicit INSERT), the flush will either fail or silently drop the fields. Since metrics errors are swallowed, the run would look healthy while the rows are lost. The consumer and table schema changes should probably ship alongside this.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Worth checking. FSM never builds a row from this dict: merge_write_stats copies named keys with .get, and #127 reads start_date/end_date into the existing data_interval_start/data_interval_end columns, using the job window when they're missing. Released FSM simply ignores the new keys, so either deploy order is safe and the table needs no change.

an accumulator that merged per-partition :meth:`to_dict` outputs across
executors. The incoming counts are already net (store drops applied
per partition), so they are summed directly rather than replayed through the
``record_*`` methods (which would double-apply the store-drop decrement).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

merge_from_dict never reads start_date/end_date from the incoming dict. If the driver-side aggregator was built without a window (e.g. the executor-side build_aggregator calls in spark/utils.py:104 and :192 don't pass the new kwargs), a window carried by the merged stats is silently dropped and to_dict()['start_date'] stays None. Suggest filling the window when self.start_date is None, as merge_stats does.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fair point. Nothing hits it today, because the executor-side build_aggregator calls are the windowless ones and both driver collectors already carry the task window. merge_from_dict now merges the window with the same min/max rule as merge_stats, so a windowless collector picks it up and the two can't drift.

In 9edeb9c.

Comment on lines +310 to +311
"start_date": _identity("start_date"),
"end_date": _identity("end_date"),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

merge_stats keeps the first non-None start_date/end_date, so the result depends on argument order. That contradicts the docstring's "associative, and commutative" claim. Merging [Jan1, Jan2) with [Jan2, Jan3) gives a different answer depending on order, and neither is the actual covered window [Jan1, Jan3). In the Spark AccumulatorParam use case, the result would vary with partition order. Suggest min(start_date) / max(end_date), mirroring how max_event_timestamp is merged.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Agreed. merge_stats now takes the earliest start_date and the latest end_date, through the same helper as max_event_timestamp, so the result no longer depends on argument order, as the docstring says. Partition order couldn't change the result in practice because executor payloads carry no window, but the code now matches the docstring.

In 9edeb9c.

@@ -231,7 +231,12 @@ def _materialize_from_offline_store(

try:
collector = build_aggregator(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The window has to be threaded by hand into every build_aggregator call site (base.get_execution_context plus this direct offline-store path). Any path that is missed, such as a future engine or Ray's _materialize_from_offline_store if it gains metrics, quietly reports a None window. Consider building the collector from the MaterializationTask (e.g. build_aggregator_for_task(task, ...)) so the window can't be forgotten.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Agreed. I added build_aggregator_for_task(task, ...), and both driver-side sites (get_execution_context, plus this offline-store path, which now takes the task) build their collector through it. The executor-side calls in spark/utils.py stay windowless on purpose, because their partial stats fold into the driver collector, which owns the window.

In 9edeb9c.

…ctors from the task

- _to_utc uses make_tzaware, and max_event_timestamp is normalized to UTC like the window.
- merge_stats and merge_from_dict take the earliest start_date and latest end_date, so the
  result no longer depends on merge order.
- build_aggregator_for_task builds the driver-side collector from the MaterializationTask.
- The /materialize endpoint and `feast materialize --disable-event-timestamp` end the window
  at the UTC time, not the host's local time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Manisha4
Manisha4 merged commit b13f26e into master Nov 3, 2026
@Manisha4
Manisha4 deleted the feat/materialization-metrics-window branch November 3, 2026 03:50
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