Repository navigation
feat: report data provider queries on the service event bus - #25262
Conversation
|
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 |
|
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? |
|
@claude simplify this according to comments and otherwise also if possible |
|
Claude finished @Artur-'s task in 25m 16s —— View job Simplification — mostly already done by @heruan concurrentlyI 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 (
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
|
|
Thanks @Artur-, fair points. The implementation mimics the existing hooks, but can be simplified here.
Agreed. I copied the 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.
It's how the fetch finds the service:
I put them on |
5c873f8 to
ff62f6c
Compare
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.
ff62f6c to
c844d26
Compare
| if (service == null) { | ||
| return null; | ||
| } | ||
| // A service implementation is not obliged to supply an event bus, and |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
| 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) { |
There was a problem hiding this comment.
Why does this catch RuntimeException specifically?
There was a problem hiding this comment.
I think the only thing thrown is IllegalArgumentException from element.get(StateNode) if no Element is mapped for StateNode
There was a problem hiding this comment.
Redundant catch, lost it.
| activeKeys.add(key); | ||
| }); | ||
| } | ||
| DataFetchObserver.fetch(getUI(), getComponent(), range.getStart(), |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
This and some other tests seem quite pointless. Only retain the really relevant ones
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.
|
All comments addressed. Summary of what changed:
Two things I did not change:
|
|
|
This ticket/PR has been released with Vaadin 25.3.0. |



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 callDataCommunicator.requestFlush(), which registers abeforeClientResponseexecution, andUidlRequestHandlerrunshandleRpc(...)first and only thenwriteUidl->UidlWriter.createUidl->stateTree.runExecutionsBeforeClientResponse(). That later phase is where the count query,fetchFromProviderand the hierarchicalpreloadFlatRange*actually run.So the RPC event for a combo box keystroke covers bookkeeping, not the load, and the time a
Grid,ComboBoxorTreeGridspends 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:DataCountStartedEventDataFetchStartedEventDataCountFailedEvent(getError())DataFetchFailedEvent(getError())DataCountEndedEvent(getCount())DataFetchEndedEvent(getRowsReturned())Both families carry the resolved
Componentand whether the query was filtered; the fetch events also carrygetOffset()andgetLimit(), so a component asking for far more than it renders, or a provider returning short pages, shows up as a gap againstgetRowsReturned().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.
VaadinServiceneeds no changes at all. The bus already carries service-level events.Notes for review
DataCommunicatorpasses theUIandComponentit 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 becauseVaadinService.getCurrent()is unavailable on the thread that matters:enablePushUpdates(Executor)runs fetches on the supplied executor without propagatingCurrentInstance.DataCommunicator.getUI()becomesprotectedand gains a matchinggetComponent(), soHierarchicalDataCommunicatorshares both instead of duplicating them.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.-1. The ended event always fires, so a listener has one place to clean up timing state.HierarchicalDataCommunicatorpassesfetchEnabled = falsetosuper, so the parent flush and thereforeactivate()never run for it, and itsgetDataProviderSize()override never callssuper.Tests
DataQueryEventsTest(flow-server): what the six events expose, including the absent-component case. Dispatch itself is covered byVaadinServiceEventBusTest, 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 thegetCurrent()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
DataFetchListenerSPI 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 at5c50eed(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) and5c873f8(dispatch offVaadinService).