Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discover] Refactor totalHits$ loading state handling to omit race conditions #196114

Merged
merged 14 commits into from
Oct 22, 2024

Conversation

kertal
Copy link
Member

@kertal kertal commented Oct 14, 2024

Fixes #184600

Summary

A simpler approach than #194904, solving the root cause of the flakiness. We had a race condition based on observable messaging of our data state, that led to the following screenshot:

image

It was happening because the loading state of totalHits$ was set by the histogram in use_discover_histogram.ts.
By setting the loading state in the fetchAll function, which is executed before the hook, the loading state is set on a higher level. This can no longer lead to a state that the overall data fetching is set to loading, while the histogram is still set to complete while already getting new properties like a new data view id, but not having access to a refreshed list of data views. Overall this seems to be the cleaner approach.

[✅] test/functional/apps/discover/group3/config.ts: 25/25 tests passed. kibana-flaky-test-suite-runner#7138

Checklist

@kertal
Copy link
Member Author

kertal commented Oct 14, 2024

/ci

@kibanamachine
Copy link
Contributor

Flaky Test Runner Stats

🎉 All tests passed! - kibana-flaky-test-suite-runner#7137

[✅] test/functional/apps/discover/group3/config.ts: 25/25 tests passed.

see run history

@kertal
Copy link
Member Author

kertal commented Oct 14, 2024

/ci

@kibanamachine
Copy link
Contributor

Flaky Test Runner Stats

🎉 All tests passed! - kibana-flaky-test-suite-runner#7138

[✅] test/functional/apps/discover/group3/config.ts: 25/25 tests passed.

see run history

@kertal
Copy link
Member Author

kertal commented Oct 15, 2024

/ci

@kertal kertal self-assigned this Oct 15, 2024
@kertal kertal added Feature:Discover Discover Application Team:DataDiscovery Discover, search (e.g. data plugin and KQL), data views, saved searches. For ES|QL, use Team:ES|QL. release_note:skip Skip the PR/issue when compiling release notes labels Oct 15, 2024
@kertal kertal marked this pull request as ready for review October 15, 2024 06:42
@kertal kertal requested a review from a team as a code owner October 15, 2024 06:42
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-data-discovery (Team:DataDiscovery)

// dataSubjects.main$.subscribe((msg) => addLog('dataSubjects.main$', msg));
// dataSubjects.documents$.subscribe((msg) => addLog('dataSubjects.documents$', msg));
// dataSubjects.totalHits$.subscribe((msg) => addLog('dataSubjects.totalHits$', msg););
// Add window.ELASTIC_DISCOVER_LOGGER = 'debug' to see messages in console
Copy link
Member Author

Choose a reason for hiding this comment

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

uncommenting the code can be very helpful to unterstand data fetching observables race conditions, I think it's worth keeping it here

Copy link
Member Author

Choose a reason for hiding this comment

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

This could be helpful to debug at least a part of the staff you mention in #165192 (comment) @jughosta

@kertal kertal changed the title Discover change total hits [Discover] Refactor totalHits$ loading state handling to omit race conditions Oct 15, 2024
@@ -98,6 +98,8 @@ export function fetchAll(
sendLoadingMsg(dataSubjects.totalHits$, {
result: dataSubjects.totalHits$.getValue().result,
});
} else {
sendLoadingMsg(dataSubjects.totalHits$);
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should keep the previous result too?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure about that, because I think if I send a loading, the previous result is not longer valid? So if we rely on a previous result somewhere we might change this

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm also not sure it's needed, but we used to do it when setting the loading state in use_discover_histogtam too, so probably best to keep passing result for now. It would get rid of this conditional too (and the comment above doesn't apply anymore anyway).

Copy link
Member Author

Choose a reason for hiding this comment

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

You're both right, let's simplify the code ... done


// Do not sync the loading state since it's already handled by the saved search
if (fetchStatus !== FetchStatus.LOADING) {
savedSearchHits$.next({
Copy link
Contributor

@jughosta jughosta Oct 15, 2024

Choose a reason for hiding this comment

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

I think there was a good reason why the histogram was sending the loading status all this time. But I don't remember all the cases about that. Do you mind if we wait for @davismcphee review?

Copy link
Member Author

@kertal kertal Oct 15, 2024

Choose a reason for hiding this comment

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

yes, let's wait, from what I've seen, it doesn't hurt keeping it, but on the other hand, I thought it's redundant

Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I can tell, as long as we're setting the loading state every time infetchAll, I think this should be ok.

…/use_discover_histogram.ts

Co-authored-by: Julia Rechkunova <[email protected]>
@kertal kertal added the backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) label Oct 15, 2024
@kibanamachine
Copy link
Contributor

Flaky Test Runner Stats

🎉 All tests passed! - kibana-flaky-test-suite-runner#7143

[✅] test/functional/apps/discover/group3/config.ts: 50/50 tests passed.

see run history

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
discover 824.9KB 824.9KB +2.0B

History

cc @kertal

Copy link
Contributor

@davismcphee davismcphee left a comment

Choose a reason for hiding this comment

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

Code changes look good, and it worked well when testing in both data view and ES|QL mode! Just one thing around passing result I think we should update before merging.

@@ -98,6 +98,8 @@ export function fetchAll(
sendLoadingMsg(dataSubjects.totalHits$, {
result: dataSubjects.totalHits$.getValue().result,
});
} else {
sendLoadingMsg(dataSubjects.totalHits$);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm also not sure it's needed, but we used to do it when setting the loading state in use_discover_histogtam too, so probably best to keep passing result for now. It would get rid of this conditional too (and the comment above doesn't apply anymore anyway).


// Do not sync the loading state since it's already handled by the saved search
if (fetchStatus !== FetchStatus.LOADING) {
savedSearchHits$.next({
Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I can tell, as long as we're setting the loading state every time infetchAll, I think this should be ok.

@kertal
Copy link
Member Author

kertal commented Oct 21, 2024

@elasticmachine merge upstream

Copy link
Contributor

@davismcphee davismcphee left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for fixing this!

@kertal kertal enabled auto-merge (squash) October 22, 2024 04:26
@kertal kertal merged commit 722a913 into elastic:main Oct 22, 2024
24 checks passed
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.x

https://github.com/elastic/kibana/actions/runs/11454245367

kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Oct 22, 2024
…nditions (elastic#196114)

Fix loading state management in `use_discover_histogram.ts`

Moving the loading state for `totalHits$` to the `fetchAll` function, which is executed before the hook. This ensures that the loading state is set at a higher level, preventing a situation where the overall data fetching is in a `loading` state, but the histogram is marked as `complete` while receiving new properties (like a new data view ID) without access to refreshed data views.

(cherry picked from commit 722a913)
@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.x

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Oct 22, 2024
…ace conditions (#196114) (#197166)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Discover] Refactor totalHits$ loading state handling to omit race
conditions (#196114)](#196114)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Matthias
Wilhelm","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-22T06:06:38Z","message":"[Discover]
Refactor totalHits$ loading state handling to omit race conditions
(#196114)\n\nFix loading state management in
`use_discover_histogram.ts`\r\n\r\nMoving the loading state for
`totalHits# Backport

This will backport the following commits from `main` to `8.x`:
- [[Discover] Refactor totalHits$ loading state handling to omit race
conditions (#196114)](#196114)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT to the `fetchAll` function, which is executed before the
hook. This ensures that the loading state is set at a higher level,
preventing a situation where the overall data fetching is in a `loading`
state, but the histogram is marked as `complete` while receiving new
properties (like a new data view ID) without access to refreshed data
views.","sha":"722a913c54cd1521d615f9b093d6dd495c65fde9","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Discover","release_note:skip","v9.0.0","Team:DataDiscovery","backport:prev-minor"],"title":"[Discover]
Refactor totalHits$ loading state handling to omit race
conditions","number":196114,"url":"https://github.com/elastic/kibana/pull/196114","mergeCommit":{"message":"[Discover]
Refactor totalHits$ loading state handling to omit race conditions
(#196114)\n\nFix loading state management in
`use_discover_histogram.ts`\r\n\r\nMoving the loading state for
`totalHits# Backport

This will backport the following commits from `main` to `8.x`:
- [[Discover] Refactor totalHits$ loading state handling to omit race
conditions (#196114)](#196114)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT to the `fetchAll` function, which is executed before the
hook. This ensures that the loading state is set at a higher level,
preventing a situation where the overall data fetching is in a `loading`
state, but the histogram is marked as `complete` while receiving new
properties (like a new data view ID) without access to refreshed data
views.","sha":"722a913c54cd1521d615f9b093d6dd495c65fde9"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/196114","number":196114,"mergeCommit":{"message":"[Discover]
Refactor totalHits$ loading state handling to omit race conditions
(#196114)\n\nFix loading state management in
`use_discover_histogram.ts`\r\n\r\nMoving the loading state for
`totalHits# Backport

This will backport the following commits from `main` to `8.x`:
- [[Discover] Refactor totalHits$ loading state handling to omit race
conditions (#196114)](#196114)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT to the `fetchAll` function, which is executed before the
hook. This ensures that the loading state is set at a higher level,
preventing a situation where the overall data fetching is in a `loading`
state, but the histogram is marked as `complete` while receiving new
properties (like a new data view ID) without access to refreshed data
views.","sha":"722a913c54cd1521d615f9b093d6dd495c65fde9"}}]}]
BACKPORT-->

Co-authored-by: Matthias Wilhelm <[email protected]>
@kertal kertal deleted the discover-change-total-hits branch October 28, 2024 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) Feature:Discover Discover Application release_note:skip Skip the PR/issue when compiling release notes Team:DataDiscovery Discover, search (e.g. data plugin and KQL), data views, saved searches. For ES|QL, use Team:ES|QL. v9.0.0
Projects
None yet
5 participants