Skip to content

feat: report data provider queries on the service event bus - #25262

Merged
Artur- merged 6 commits into
mainfrom
feat/data-fetch-listener-spi
Aug 24, 2026
Merged

Artur- merged 6 commits into
mainfrom
feat/data-fetch-listener-spi

Conversation

@heruan

@heruan heruan commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

Report the count and fetch queries that lazy-loading components make against their data provider, as events on the service event bus added in #25268.

Why

A client-driven load arrives as an RPC invocation: ComboBox.setViewportRange, Grid.setViewportRange, TreeGrid.updateExpandedState. Those are already covered by the RPC events, but they do not query anything. They call DataCommunicator.requestFlush(), which registers a beforeClientResponse execution, and UidlRequestHandler runs handleRpc(...) first and only then writeUidl -> UidlWriter.createUidl -> stateTree.runExecutionsBeforeClientResponse(). That later phase is where the count query, fetchFromProvider and the hierarchical preloadFlatRange* actually run.

So the RPC event for a combo box keystroke covers bookkeeping, not the load, and the time a Grid, ComboBox or TreeGrid spends fetching items is attributable only to the request as a whole, with no component, offset or row count attached to it.

What

Six events in com.vaadin.flow.server.data, one per phase, following the shape of the RPC and session lock events:

service.getEventBus().addListener(DataFetchEndedEvent.class, event -> {
    event.getComponent().ifPresent(component -> registry
            .counter("data.rows", "component", component.getClass().getName())
            .increment(event.getRowsReturned()));
});
Count Fetch
DataCountStartedEvent DataFetchStartedEvent
DataCountFailedEvent (getError()) DataFetchFailedEvent (getError())
DataCountEndedEvent (getCount()) DataFetchEndedEvent (getRowsReturned())

Both families carry the resolved Component and whether the query was filtered; the fetch events also carry getOffset() and getLimit(), so a component asking for far more than it renders, or a provider returning short pages, shows up as a gap against getRowsReturned().

Count and fetch are separate because they have different cost profiles and different causes: a count is one query for a whole level, a fetch loads one page. A hierarchical component issues a child count per expanded parent, so several count events for one component within a single request is the signature of an expensive hierarchy.

VaadinService needs no changes at all. The bus already carries service-level events.

Notes for review

  • The caller supplies the context. DataCommunicator passes the UI and Component it already has, so the reporting helper does no state node walking and only looks up the event bus. The UI is passed rather than looked up because VaadinService.getCurrent() is unavailable on the thread that matters: enablePushUpdates(Executor) runs fetches on the supplied executor without propagating CurrentInstance. DataCommunicator.getUI() becomes protected and gains a matching getComponent(), so HierarchicalDataCommunicator shares both instead of duplicating them.
  • Fetch is measured around consumption, not around the call. A data provider may return a lazily evaluated Stream, so timing the call alone would measure almost nothing. The ended event is fired after the items have been drained, which also makes the row count exact.
  • The ended events are fired in reverse registration order, so listeners nest around the started one, as the session lock release does.
  • A query that threw fires the failed event with the throwable and then reports -1. The ended event always fires, so a listener has one place to clean up timing state.
  • The two abstract bases are public so their accessors are documented on the concrete events, and each one says it cannot be listened for, since the bus dispatches by exact runtime type.
  • The flat and hierarchical paths cannot double-report. HierarchicalDataCommunicator passes fetchEnabled = false to super, so the parent flush and therefore activate() never run for it, and its getDataProviderSize() override never calls super.

Tests

  • DataQueryEventsTest (flow-server): what the six events expose, including the absent-component case. Dispatch itself is covered by VaadinServiceEventBusTest, so it is not retested here.
  • DataFetchEventsTest (flow-data): count and fetch reported with component attribution, started before ended, the filtered flag, a throwing backend and a throwing count each firing failed before ended with the original throwable and -1, listener removal, a throwing listener not breaking loading, the hierarchical root and expanded-child cases, and the asynchronous path where the fetch runs on the executor thread with none of the getCurrent() thread locals set.

263 existing flow-server and flow-data tests still pass, serializability suites included.

Review feedback

Rebased onto #25268 and reworked from a DataFetchListener SPI plus a notifier to bus events, per this comment, and the reporting helper no longer takes a state node but the UI and component the communicator already has, per this one. Earlier iterations, before the rebase, are at 5c50eed (initial SPI), ddbee09 (drop hierarchy depth, which said how deep but never which branch, and was always 0 for flattened hierarchies), e922688 (report every query instead of guarding on listeners) and 5c873f8 (dispatch off VaadinService).

@github-actions

github-actions Bot commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 1 384 files  + 2   1 385 suites  +2   1h 25m 17s ⏱️ - 3m 49s
10 555 tests +13  10 488 ✅ +13  67 💤 ±0  0 ❌ ±0 
10 874 runs  +13  10 806 ✅ +13  68 💤 ±0  0 ❌ ±0 

Results for commit b3617fb. ± Comparison against base commit ee3bd87.

♻️ This comment has been updated with latest results.

@Artur-

Artur- commented Aug 20, 2026

Copy link
Copy Markdown
Member

If the need here is to be able to add a global listener for fetch events, the implementation looks very complex and the architecture looks a bit off. If the listener is on service level then a fetch operation should just be able to fire away a service level event without caring about state nodes or components? Maybe the components/stateNode is the source but why is something else needed? The whole pattern with verifying that there is an observer and then handling null events etc looks very complex compared to the gains of not creating an event object with two integers. Probably it can be simplified to always fire the event - it's not like data is fetched a million times per second by every user.

The methods to fire events should definitely not be in VaadinService - they should be in the fetch event class or a related class. VaadinService should just be the event bus

@mshabarov
mshabarov requested a review from caalador August 20, 2026 07:09
@Artur-

Artur- commented Aug 20, 2026

Copy link
Copy Markdown
Member

Is there a point in gathering the depth fetched for a hierarchical request? Does it help to know if depth is 2, when that provides no reference to which sub tree it is about?

@Artur-

Artur- commented Aug 20, 2026

Copy link
Copy Markdown
Member

@claude simplify this according to comments and otherwise also if possible

@claude

claude Bot commented Aug 20, 2026 •

Copy link
Copy Markdown

Claude finished @Artur-'s task in 25m 16s —— View job


Simplification — mostly already done by @heruan concurrently

I built out the simplification, but while I worked, @heruan pushed two commits that land essentially the same changes. Rather than push a competing commit onto an actively-moving branch, here's where things stand and the one point still open.

Already addressed on the branch (ddbee09, e922688):

  • Depth dropped from DataCountEvent/DataFetchEvent and the getDataProviderChildCount plumbing — matches your comment that a bare depth number identifies "how deep" but never "which branch". (ddbee09)
  • Guard + null-event handling removed. DataFetchObserver.create()→null plus if (event != null) at four call sites is gone; the observer now wraps the query as an IntSupplier so the started/failed/ended sequence lives in one place and every call site is a plain one-liner. hasDataFetchListeners() removed rather than left as dead API. (e922688)
  • Reports every query instead of guarding — your "not fetched a million times per second" point. (e922688)
  • Added the async-executor test and a flow-server-side registration test.

I independently arrived at the same shape (query-wrapping helper + always-fire), so I'm confident this direction is right — no diff from me needed for those.

Still open: "firing methods should not be on VaadinService"

The six fireDataCountStarted/…/fireDataFetchEnded methods still live on VaadinService, and DataFetchObserver calls service.fireData*(…). That's the one part of your feedback not yet reflected — ideally VaadinService only holds/adds/removes and exposes the listeners, with dispatch (iteration + reverse-order ended + exception suppression) living beside the events.

There's a real tension, though: the new DataFetchListenerRegistrationTest drives those fireData* methods directly, and it exists partly to give the new code in-module coverage in flow-server (the SonarQube "coverage on new code" gate). If the dispatch simply moves into flow-data's DataFetchObserver, that coverage — and the ability to unit-test delivery order/suppression in flow-server — moves with it.

A shape that satisfies both: put the dispatch in a small flow-server helper next to the events (e.g. DataFetchListenerNotifier in com.vaadin.flow.server.data) that takes the VaadinService, iterates getDataFetchListeners(), and does the try/catch/log with forward started/failed and reverse ended. Then VaadinService keeps only addDataFetchListener + a package-visible getDataFetchListeners(), DataFetchObserver calls the notifier, and the flow-server test targets the notifier — so it's off VaadinService, in a related class, and coverage stays in-module.

Happy to implement that on top of e922688 if you'd like — just say the word and I'll push it (or leave it to @heruan since he's iterating on this live).
• branch feat/data-fetch-listener-spi

@heruan

heruan commented Aug 20, 2026

Copy link
Copy Markdown
Member Author

Thanks @Artur-, fair points. The implementation mimics the existing hooks, but can be simplified here.

If the listener is on service level then a fetch operation should just be able to fire away a service level event without caring about state nodes or components

Agreed. I copied the hasRpcInvocationListeners() pattern from ServerRpcHandler, but there it skips parsing JSON many times per request. Here it only skips one small allocation, once or twice per request. Not worth it.

I'd also like to remove the four repeated try/catch/finally blocks, which is most of the noise. If the observer wraps the query, each call site becomes one statement:

public int getDataProviderSize() {
    return DataFetchObserver.count(stateNode, getFilter() != null,
            DataCountEvent.NO_DEPTH,
            () -> countCallback != null
                    ? countCallback.count(new Query(getFilter()))
                    : getDataProvider().size(new Query(getFilter())));

No nulls at the call sites, and the started/failed/ended order lives in one place. hasDataFetchListeners() then gets deleted instead of staying as unused public API.

Maybe the components/stateNode is the source but why is something else needed?

It's how the fetch finds the service: DataCommunicator has no service reference, and VaadinService.getCurrent() doesn't work on the async path. With enablePushUpdates(Executor) the fetch runs on the executor thread, and nothing propagates CurrentInstance there. The event still found its UI and component from the node. Using getCurrent() would silently drop every async fetch. I'll turn that probe into a real test, since the async path has no test today, which is my miss.

The methods to fire events should definitely not be in VaadinService - they should be in the fetch event class or a related class

I put them on VaadinService because that's where all the existing ones are: fireRpcInvocationStarted/Failed/Ended, fireSessionLock*, fireSessionDestroy, fireUIInitListeners (see #24499). No event class in Flow has a fire method today: moving them also means exposing the listener list or a dispatcher, since the caller here is in another module.

Lazy-loading components make count and fetch queries against their data
provider while the response is being built, in
StateTree#runExecutionsBeforeClientResponse, after RPC handling. A
client-driven load arrives as an RPC invocation such as setViewportRange
or updateExpandedState, but that invocation only registers a flush, so the
RPC events never cover the queries themselves and the time a Grid,
ComboBox or TreeGrid spends loading items is attributable only to the
request as a whole.

Six events on the service event bus cover them, one per phase, following
the shape introduced in #25268: DataCountStartedEvent,
DataCountFailedEvent, DataCountEndedEvent and their fetch counterparts.
Count and fetch are separate because they have different cost profiles
and different causes: a count is one query for a whole level, a fetch
loads one page. A hierarchical component issues a child count per
expanded parent, so several count events for one component in a single
request is the signature of an expensive hierarchy.

VaadinService needs no changes at all, since the bus already carries
service-level events.

The data communicator passes the UI and the component it already has
access to, so the reporting helper does no state node walking and only
looks up the event bus. The UI is passed rather than looked up because
VaadinService#getCurrent is unavailable on the thread that matters:
enablePushUpdates runs fetches on the supplied executor without
propagating CurrentInstance.

Fetch queries are reported around consumption of the returned items, so
the measured duration covers the backend round-trip even for a data
provider returning a lazily evaluated stream, and the reported row count
is exact. The ended events are fired in reverse registration order so
listeners nest around the started one.

The two abstract event bases are public so their accessors document
themselves on the concrete events, and each says it cannot be listened
for, since the bus dispatches by exact runtime type.
@heruan
heruan force-pushed the feat/data-fetch-listener-spi branch from ff62f6c to c844d26 Compare August 21, 2026 21:43
@heruan heruan changed the title feat: data fetch listener support feat: report data provider queries on the service event bus Aug 21, 2026
@heruan heruan self-assigned this Aug 21, 2026
@heruan
heruan requested a review from Artur- August 21, 2026 21:46
if (service == null) {
return null;
}
// A service implementation is not obliged to supply an event bus, and

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.

This does not sound correct, a service must always have an event bus even though some tests might cheat

* Resolves the event bus to report on, or {@code null} when there is
* nothing to report to.
*/
private static VaadinServiceEventBus eventBus(UI ui) {

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.

Suggested change
private static VaadinServiceEventBus eventBus(UI ui) {
private static VaadinServiceEventBus getEventBus(UI ui) {

protected Component getComponent() {
try {
return Element.get(stateNode).getComponent().orElse(null);
} catch (RuntimeException e) {

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.

Why does this catch RuntimeException specifically?

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.

I think the only thing thrown is IllegalArgumentException from element.get(StateNode) if no Element is mapped for StateNode

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.

Redundant catch, lost it.

activeKeys.add(key);
});
}
DataFetchObserver.fetch(getUI(), getComponent(), range.getStart(),

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.

Somehow the architecture feels "off" here when you need to pass everything through the observer but maybe the code would be way more complex if there was just an event injected here and there. Maybe it would be easier to read if the actual fetch lambda was declared first and then just passed to DataFetchObserver

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.

Also just giving the full Range object instead of parting here start and length would make it seem simpler.

A real VaadinService always has an event bus, since it is a final field
initialised inline, so production code should not guard against a null
one. The guard existed only because DataCommunicatorTest's mocked service
returns null from every accessor; the mock now supplies an event bus
instead.

Renames the private lookup to getEventBus, drops the try/catch around
component resolution in favour of ElementUtil#from, which returns an
Optional rather than throwing, and passes the requested Range through to
the observer instead of splitting it into an offset and a limit at each
call site. The events keep exposing the offset and limit as ints, since
Range is internal API and the events are not.

Hoists the fetch lambdas into named locals so the observed query reads
before the call that observes it.
return getDataProvider().size(new Query(getFilter()));
}
return DataFetchObserver.count(getUI(), getComponent(),
getFilter() != null,

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.

The same applies for count queries - easier to read if the lambda is outside the call

The fetch lambdas were moved into named locals in the previous commit but
the count ones were left inline, which is the inconsistency the review
pointed out. The hierarchical child count is the one that benefits most,
since it nests a validating block with a throw inside the call.
}

@Test
void countStarted_exposesUiComponentAndFilter() {

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.

This and some other tests seem quite pointless. Only retain the really relevant ones

heruan added 2 commits August 24, 2026 10:24
Most of DataQueryEventsTest was constructor and getter assertions that
could only fail on a typo. What remains are the three things the events
actually promise: that -1 means the query threw rather than a count of
zero or an empty page, that the requested limit and the returned row
count stay separate so a short page is visible, and that the component is
optional because a data communicator can be driven by a bare element.

A failed count now asserts that it carries its throwable, which a failed
fetch already did.

Coverage of the package stays at 95% of lines, so the reduction does not
reopen the quality gate; the two uncovered lines are UI accessors on the
fetch event base.
Moves the private constructor above the static methods, explains why the
ArrayUpdater no-ops in the test are empty, and uses static imports for
the Mockito calls in that test.

Keeps catching Throwable around the query, now with the reasoning in a
comment. An Error is the most interesting failure a data query can have,
a fetch large enough to exhaust the heap, and narrowing the catch would
not even avoid notifying listeners on that path since the ended event
fires from the finally block either way. ServerRpcHandler catches
Throwable around the RPC phase events for the same reason.

The one remaining static import finding is left alone: DataCommunicatorTest
qualifies its three other Mockito calls, and the repository prefers the
qualified form by 118 files to 25.
@heruan

heruan commented Aug 24, 2026

Copy link
Copy Markdown
Member Author

All comments addressed. Summary of what changed:

  • Rebased on feat: give VaadinService an event bus and route its listeners through it #25268. The listener interface and the notifier are gone, replaced by six events on the service event bus, and VaadinService needs no changes at all now.
  • The observer takes the UI and the Component the communicator already has, so it no longer walks the state node.
  • The count and fetch lambdas are declared before the call that observes them.
  • Component resolution uses ElementUtil.from, which returns an Optional, so there is no try/catch left.
  • Trimmed the event tests down to the three that assert something the events actually promise.

Two things I did not change:

  • The catch around the query is still Throwable, with the reason in a comment. An Error is the most interesting failure a data query can have, and narrowing it would not avoid notifying listeners on that path anyway, since the ended event fires from the finally either way. ServerRpcHandler catches Throwable around the RPC phase events for the same reason.
  • The observer takes a Range, but the events keep getOffset() and getLimit() as ints. Range is internal API and the events are not.

@Artur-
Artur- enabled auto-merge August 24, 2026 10:01
@sonarqubecloud

Copy link
Copy Markdown

@Artur-
Artur- added this pull request to the merge queue Aug 24, 2026
Merged via the queue into main with commit 65c7ff3 Aug 24, 2026
34 checks passed
@Artur-
Artur- deleted the feat/data-fetch-listener-spi branch August 24, 2026 10:53
@github-project-automation github-project-automation Bot moved this from ⚒️ In progress to Done in Vaadin Flow | Hilla | Kits ongoing work Aug 24, 2026
@vaadin-bot

Copy link
Copy Markdown
Collaborator

This ticket/PR has been released with Vaadin 25.3.0.

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.

4 participants