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

[Embeddable] Avoid rerendering loop due to search context reload #203150

Merged
merged 17 commits into from
Dec 11, 2024

Conversation

dej611
Copy link
Contributor

@dej611 dej611 commented Dec 5, 2024

Summary

Fixes #202266

This PR fixes the underline rerendering issue at the useSearchApi hook level, so any embeddable component who uses this hook would benefit from the fix.

Ideally the props passed to the Lens component should be memoized, but this assumption would break many integrations as the previous embeddable component did take care of filtering duplicates.
To test this:

  • Go to Observability > Alerts > Manage rules and Add Rule, pick the Custom threshold option and verify the infinite reload does not happen

Once fixed this, another problem bubbled up with the brushing use case: when brushing a chart the chart was always one time range step behind. The other bug was due to the fetch$(api) function propagating a stale data search context who the Lens embeddable was relying on.
To solve this other problem the following changes have been applied:

  • read the searchSessionId from the api directly (used for autoRefresh)
    • make sure to test the Refresh feature with both relative and absolute time ranges
  • read the search context from the parentApi directly if implements the unifiedSearch API
    • to test this, brush and check that the final time range matches the correct time range

Note: the fundamental issue for the latter is fetch$ not emitting the up-to-date data when the parentApi search context updates. The retrieved data is stale and one step behind, so it is not reliable. cc @elastic/kibana-presentation

As @ThomThomson noticed in his test failure investigation another issue was found in this PR due to the wrong handling of the searchSessionId within the Observability page (for more details see his analysis).
@markov00 and @crespocarlos helped risolve this problem with some additional changes on the Observability side of things: this will lead to some extra searchSessionId to be created, which will be eventually solved by Observability team shortly moving away from the searchSessionId mechanism

Checklist

@dej611 dej611 requested review from a team as code owners December 5, 2024 17:16
@botelastic botelastic bot added the ci:project-deploy-observability Create an Observability project label Dec 5, 2024
Copy link
Contributor

github-actions bot commented Dec 5, 2024

🤖 GitHub comments

Expand to view the GitHub comments

Just comment with:

  • /oblt-deploy : Deploy a Kibana instance using the Observability test environments.
  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)

@dej611 dej611 added release_note:fix Team:Presentation Presentation Team for Dashboard, Input Controls, and Canvas backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) Feature:Embeddables Relating to the Embeddable system and removed ci:project-deploy-observability Create an Observability project labels Dec 5, 2024
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-presentation (Team:Presentation)

@botelastic botelastic bot added the ci:project-deploy-observability Create an Observability project label Dec 5, 2024
Copy link
Contributor

github-actions bot commented Dec 5, 2024

🤖 GitHub comments

Expand to view the GitHub comments

Just comment with:

  • /oblt-deploy : Deploy a Kibana instance using the Observability test environments.
  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)

Copy link
Contributor

@nreese nreese left a comment

Choose a reason for hiding this comment

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

kibana-presentation changes LGTM. useSearchApi is the right places for these fixes

@kibanamachine
Copy link
Contributor

Flaky Test Runner Stats

🟠 Some tests failed. - kibana-flaky-test-suite-runner#7555

[❌] test/functional/apps/discover/group1/config.ts: 0/50 tests passed.
[❌] test/functional/apps/discover/group3/config.ts: 0/50 tests passed.
[❌] x-pack/test/observability_functional/with_rac_write.config.ts: 0/50 tests passed.

see run history

@dej611 dej611 requested a review from a team as a code owner December 5, 2024 20:12
@kibanamachine
Copy link
Contributor

Flaky Test Runner Stats

🟠 Some tests failed. - kibana-flaky-test-suite-runner#7557

[❌] test/functional/apps/discover/group1/config.ts: 0/50 tests passed.
[❌] test/functional/apps/discover/group3/config.ts: 0/50 tests passed.
[❌] x-pack/test/observability_functional/with_rac_write.config.ts: 0/50 tests passed.

see run history

@botelastic botelastic bot added the Team:obs-ux-management Observability Management User Experience Team label Dec 6, 2024
@elasticmachine
Copy link
Contributor

Pinging @elastic/obs-ux-management-team (Team:obs-ux-management)

@kibanamachine
Copy link
Contributor

Flaky Test Runner Stats

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

[✅] test/functional/apps/discover/group1/config.ts: 50/50 tests passed.
[✅] test/functional/apps/discover/group3/config.ts: 50/50 tests passed.
[✅] x-pack/test/observability_functional/with_rac_write.config.ts: 50/50 tests passed.

see run history

@ThomThomson
Copy link
Contributor

The following is a diagnosis of the test failure: FTR Configs #95 / InfraOps App Metrics UI Node Details #Asset type: host with kubernetes section Overview Tab cpuUsage tile should show 50.0%.

Problem

When changing the time picker on the Observability Infrastructure Hosts Overview page the KPI charts don't respond until manual / hard refresh is pressed.

Root of the problem

The root of this problem is a misunderstanding of the searchSessionId prop. In the embeddable architecture.

Search sessions were added in the make it slow project years ago, and are meant to allow a previous search to be restored. They are integrated with the unified search bar, and are set up to change any time any of the search context changes (filters, query, time range etc). They are usually stored in the URL. In the Embeddable infrastructure, we had problems with multiple fetches, or fetches from various sources when search session IDs are provided (e.g. changing the time range would tell the embeddable to load data, then update the search session which also tells the embeddable to load data 🔥).

To fix this, we made the decision that we would not directly query on changes in the time range etc if a search session ID was provided, and instead query only when the search session ID changes. When the search sessions system is set up correctly, this works great. You can see the code for this here

The problem here is that Observability uses a search session ID - but it never changes - in essence this tells the Embeddable framework not to re-fetch data unless the reload button is pressed. In this code you can see that a session is started, but it isn't actually hooked up to anything, so for the whole lifecycle of the page there is just one session.

Recommended solution

It would be possible for Observability to ensure their search session integration works correctly on this page. The above-linked code could be updated to properly regenerate sessions on change. For an example of how this is done, you can see the Dashboard search session integration here

That said, it doesn't seem like this page has an actual usage of search session restoration, or makes use of the search session UI (generally placed to the right of the breadcrumb). Because of this, my recommendation would be to remove all of the search session related code from this area.

To verify that this works, you can update the updateSearchSessionId function in x-pack/plugins/observability_solution/infra/public/hooks/use_search_session.ts to

  const updateSearchSessionId = useCallback(() => {
    const sessionId = search.session.start();
    setSearchSessionId(undefined);
  }, [search.session]);

This isn't a long term solution, but it works because the charts will no longer recieve a static search session ID, which restores proper functionality to the embeddable.

Next steps

Aside from fixing this on the Observability side, we should document this behaviour in jsdoc comments. Additionally, if we can verify that none of the usages of the Lens Embeddable component are making proper use of the search session ID functionality, I can recommend that the @elastic/kibana-visualizations team removes that prop entirely.

@markov00 markov00 requested a review from a team as a code owner December 9, 2024 07:58
@crespocarlos
Copy link
Contributor

Hey @ThomThomson

The problem here is that Observability uses a search session ID - but it never changes - in essence this tells the Embeddable framework not to re-fetch data unless the reload button is pressed. In this code you can see that a session is started, but it isn't actually hooked up to anything, so for the whole lifecycle of the page there is just one session.

In fact, updateSearchSessionId is called when:

1 - The date range changes
2 - Unified search is submitted
3 - The auto-refresh kicks in

Shouldn't the first time a searchSessionId is assigned make the charts to fetch data?

That said, it doesn't seem like this page has an actual usage of search session restoration, or makes use of the search session UI (generally placed to the right of the breadcrumb).

True. The page doesn't need to restore the session.

However, If I'm not mistaken, the searchSessionId was particularly useful in the auto-refresh use case because the data.search.session can tell when all charts have finished loading so that the auto-refresh can trigger the next request batch, without queueing or canceling ongoing requests, making it easy to sync them up. Renewing the searchSessionId automatically triggers a new refresh, as you exaplained above.

If my memory serves me right, we basically tried to replicate what Dashboards UI does on the auto-refresh.

Happy to find alternatives

cc: @markov00

@maryam-saeidi maryam-saeidi self-requested a review December 9, 2024 09:22
@crespocarlos crespocarlos self-requested a review December 9, 2024 09:32
Copy link
Member

@maryam-saeidi maryam-saeidi left a comment

Choose a reason for hiding this comment

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

Worked as expected locally, thanks for the fix ❤️

@@ -138,19 +138,21 @@ export function RuleConditionChart({
const errorDiv = document.querySelector('.lnsEmbeddedError');
if (errorDiv) {
const paragraphElements = errorDiv.querySelectorAll('p');
if (!paragraphElements || paragraphElements.length < 2) return;
if (!paragraphElements) return;
Copy link
Member

Choose a reason for hiding this comment

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

Tested following scenarios, LGTM!

Error in filter query Error in equation
image image

paragraphElements[1].innerText = i18n.translate(
'xpack.observability.ruleCondition.chart.error_equation.description',
{
defaultMessage: 'Check the rule equation.',
Copy link
Member

Choose a reason for hiding this comment

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

Did you find any example for showing this message, or did you just keep the previous implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As for now I've kept the same implementation. I did not have all the legacy knowledge of why the 2 errors check, so I've just fixed what I've seen not working for now.

Copy link
Contributor

@crespocarlos crespocarlos left a comment

Choose a reason for hiding this comment

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

Could focus the changes on removing the searchSessionId only from embeddable usages?
I'll open a ticket on to refactor what is now called use_search_session.ts, but we need the some of the logic that depends on the change of the searchSessionId to control and force new requests

@@ -1,34 +0,0 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

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

Please keep this file and remove the usages of searchSessionId on Lens charts

@@ -37,16 +36,11 @@ export const useLoadingState = () => {
const {
data: { search },
} = services;
const { updateSearchSessionId } = useSearchSessionContext();
Copy link
Contributor

Choose a reason for hiding this comment

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

We'll refactor this context to be timestamp-based instead of relying on searchSessionId, but removing this now will have consequences beyond the embedabbles. This context is useful to control and force requests

@@ -44,22 +29,20 @@ export const ContextProviders = ({
} = props;

return (
<RenderWithOptionalSearchSessionProvider renderMode={renderMode}>
Copy link
Contributor

Choose a reason for hiding this comment

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

Please leave this context here, it is used to control and force new requests. As said above it will be changed to not rely on searchSessionId.

Copy link
Contributor

@crespocarlos crespocarlos 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 the changes and fixing the problem.

@crespocarlos
Copy link
Contributor

I think this might solve the problem with the test: dej611#23

@elasticmachine
Copy link
Contributor

elasticmachine commented Dec 11, 2024

💚 Build Succeeded

  • Buildkite Build
  • Commit: 9e65401
  • Kibana Serverless Image: docker.elastic.co/kibana-ci/kibana-serverless:pr-203150-9e6540198507

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
aiops 615.5KB 615.6KB +169.0B
apm 3.3MB 3.3MB +6.0B
canvas 1.1MB 1.1MB +183.0B
cases 492.7KB 492.7KB +6.0B
controls 494.3KB 494.5KB +188.0B
dataVisualizer 615.9KB 615.9KB +6.0B
discover 785.3KB 785.3KB +12.0B
imageEmbeddable 70.0KB 70.0KB +6.0B
infra 1.8MB 1.8MB +180.0B
inputControlVis 52.5KB 52.5KB -1.0B
lens 1.5MB 1.5MB +163.0B
links 48.6KB 48.6KB +6.0B
ml 4.7MB 4.7MB +12.0B
observability 480.8KB 480.8KB +2.0B
presentationUtil 74.1KB 74.1KB +4.0B
securitySolution 14.7MB 14.7MB +24.0B
slo 856.2KB 856.2KB +6.0B
synthetics 1.1MB 1.1MB +19.0B
total +991.0B

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
dashboard 52.2KB 52.2KB +6.0B
dashboardEnhanced 18.4KB 18.4KB +9.0B
discoverEnhanced 7.6KB 7.1KB -470.0B
embeddable 71.0KB 71.0KB +6.0B
embeddableEnhanced 8.9KB 9.0KB +60.0B
imageEmbeddable 6.0KB 6.0KB +54.0B
infra 57.9KB 57.9KB +6.0B
inputControlVis 8.3KB 8.3KB +7.0B
lens 50.0KB 50.2KB +217.0B
maps 54.9KB 55.1KB +173.0B
presentationPanel 43.7KB 43.8KB +6.0B
presentationUtil 30.9KB 30.9KB +60.0B
reporting 50.8KB 50.8KB +60.0B
synthetics 37.8KB 37.8KB +54.0B
urlDrilldown 17.4KB 17.5KB +60.0B
visualizations 71.4KB 71.4KB +12.0B
total +320.0B

History

@kibanamachine
Copy link
Contributor

Flaky Test Runner Stats

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

[✅] test/functional/apps/discover/group1/config.ts: 50/50 tests passed.
[✅] test/functional/apps/discover/group3/config.ts: 50/50 tests passed.
[✅] x-pack/test/observability_functional/with_rac_write.config.ts: 50/50 tests passed.
[✅] x-pack/test/functional/apps/infra/config.ts: 50/50 tests passed.

see run history

@dej611 dej611 merged commit d4194ba into elastic:main Dec 11, 2024
8 checks passed
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.x

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

kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Dec 11, 2024
…stic#203150)

## Summary

Fixes elastic#202266

This PR fixes the underline rerendering issue at the `useSearchApi` hook
level, so any embeddable component who uses this hook would benefit from
the fix.

Ideally the props passed to the Lens component should be memoized, but
this assumption would break many integrations as the previous embeddable
component did take care of filtering duplicates.
To test this:
* Go to `Observability > Alerts > Manage rules` and `Add Rule`, pick the
`Custom threshold` option and verify the infinite reload does not happen

Once fixed this, another problem bubbled up with the brushing use case:
when brushing a chart the chart was always one time range step behind.
The other bug was due to the `fetch$(api)` function propagating a stale
`data` search context who the Lens embeddable was relying on.
To solve this other problem the following changes have been applied:
* read the `searchSessionId` from the `api` directly (used for
`autoRefresh`)
* make sure to test the `Refresh` feature with both relative and
absolute time ranges
* read the `search context` from the `parentApi` directly if implements
the `unifiedSearch` API
* to test this, brush and check that the final time range matches the
correct time range

**Note**: the fundamental issue for the latter is `fetch$` not emitting
the up-to-date `data` when the parentApi search context updates. The
retrieved `data` is stale and one step behind, so it is not reliable. cc
@elastic/kibana-presentation

As @ThomThomson noticed in his test failure investigation another issue
was found in this PR due to the wrong handling of the searchSessionId
within the Observability page (for more details see [his
analysis](elastic#203150 (comment))).
@markov00 and @crespocarlos helped risolve this problem with some
additional changes on the Observability side of things: this will lead
to some extra searchSessionId to be created, which will be eventually
solved by Observability team [shortly moving away from the
`searchSessionId`
mechanism](elastic#203412)

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: Marco Vettorello <[email protected]>
Co-authored-by: Carlos Crespo <[email protected]>
(cherry picked from commit d4194ba)
@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 Dec 11, 2024
#203150) (#203818)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Embeddable] Avoid rerendering loop due to search context reload
(#203150)](#203150)

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

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

<!--BACKPORT [{"author":{"name":"Marco
Liberati","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-12-11T15:03:36Z","message":"[Embeddable]
Avoid rerendering loop due to search context reload (#203150)\n\n##
Summary\r\n\r\nFixes #202266\r\n\r\nThis PR fixes the underline
rerendering issue at the `useSearchApi` hook\r\nlevel, so any embeddable
component who uses this hook would benefit from\r\nthe
fix.\r\n\r\nIdeally the props passed to the Lens component should be
memoized, but\r\nthis assumption would break many integrations as the
previous embeddable\r\ncomponent did take care of filtering
duplicates.\r\nTo test this:\r\n* Go to `Observability > Alerts > Manage
rules` and `Add Rule`, pick the\r\n`Custom threshold` option and verify
the infinite reload does not happen\r\n\r\nOnce fixed this, another
problem bubbled up with the brushing use case:\r\nwhen brushing a chart
the chart was always one time range step behind.\r\nThe other bug was
due to the `fetch$(api)` function propagating a stale\r\n`data` search
context who the Lens embeddable was relying on.\r\nTo solve this other
problem the following changes have been applied:\r\n* read the
`searchSessionId` from the `api` directly (used
for\r\n`autoRefresh`)\r\n* make sure to test the `Refresh` feature with
both relative and\r\nabsolute time ranges\r\n* read the `search context`
from the `parentApi` directly if implements\r\nthe `unifiedSearch`
API\r\n* to test this, brush and check that the final time range matches
the\r\ncorrect time range\r\n\r\n**Note**: the fundamental issue for the
latter is `fetch# Backport

This will backport the following commits from `main` to `8.x`:
- [[Embeddable] Avoid rerendering loop due to search context reload
(#203150)](#203150)

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

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

<!--BACKPORT not emitting\r\nthe up-to-date `data` when the parentApi
search context updates. The\r\nretrieved `data` is stale and one step
behind, so it is not reliable.
cc\r\n@elastic/kibana-presentation\r\n\r\nAs @ThomThomson noticed in his
test failure investigation another issue\r\nwas found in this PR due to
the wrong handling of the searchSessionId\r\nwithin the Observability
page (for more details see
[his\r\nanalysis](https://github.com/elastic/kibana/pull/203150#issuecomment-2524080129)).\r\n@markov00
and @crespocarlos helped risolve this problem with some\r\nadditional
changes on the Observability side of things: this will lead\r\nto some
extra searchSessionId to be created, which will be eventually\r\nsolved
by Observability team [shortly moving away from
the\r\n`searchSessionId`\r\nmechanism](https://github.com/elastic/kibana/issues/203412)\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Marco Vettorello
<[email protected]>\r\nCo-authored-by: Carlos Crespo
<[email protected]>","sha":"d4194ba5eb5a72960dad00cf956d9ea6e31219e0","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Presentation","v9.0.0","backport:prev-minor","Feature:Embeddables","ci:project-deploy-observability","Team:obs-ux-management"],"title":"[Embeddable]
Avoid rerendering loop due to search context
reload","number":203150,"url":"https://github.com/elastic/kibana/pull/203150","mergeCommit":{"message":"[Embeddable]
Avoid rerendering loop due to search context reload (#203150)\n\n##
Summary\r\n\r\nFixes #202266\r\n\r\nThis PR fixes the underline
rerendering issue at the `useSearchApi` hook\r\nlevel, so any embeddable
component who uses this hook would benefit from\r\nthe
fix.\r\n\r\nIdeally the props passed to the Lens component should be
memoized, but\r\nthis assumption would break many integrations as the
previous embeddable\r\ncomponent did take care of filtering
duplicates.\r\nTo test this:\r\n* Go to `Observability > Alerts > Manage
rules` and `Add Rule`, pick the\r\n`Custom threshold` option and verify
the infinite reload does not happen\r\n\r\nOnce fixed this, another
problem bubbled up with the brushing use case:\r\nwhen brushing a chart
the chart was always one time range step behind.\r\nThe other bug was
due to the `fetch$(api)` function propagating a stale\r\n`data` search
context who the Lens embeddable was relying on.\r\nTo solve this other
problem the following changes have been applied:\r\n* read the
`searchSessionId` from the `api` directly (used
for\r\n`autoRefresh`)\r\n* make sure to test the `Refresh` feature with
both relative and\r\nabsolute time ranges\r\n* read the `search context`
from the `parentApi` directly if implements\r\nthe `unifiedSearch`
API\r\n* to test this, brush and check that the final time range matches
the\r\ncorrect time range\r\n\r\n**Note**: the fundamental issue for the
latter is `fetch# Backport

This will backport the following commits from `main` to `8.x`:
- [[Embeddable] Avoid rerendering loop due to search context reload
(#203150)](#203150)

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

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

<!--BACKPORT not emitting\r\nthe up-to-date `data` when the parentApi
search context updates. The\r\nretrieved `data` is stale and one step
behind, so it is not reliable.
cc\r\n@elastic/kibana-presentation\r\n\r\nAs @ThomThomson noticed in his
test failure investigation another issue\r\nwas found in this PR due to
the wrong handling of the searchSessionId\r\nwithin the Observability
page (for more details see
[his\r\nanalysis](https://github.com/elastic/kibana/pull/203150#issuecomment-2524080129)).\r\n@markov00
and @crespocarlos helped risolve this problem with some\r\nadditional
changes on the Observability side of things: this will lead\r\nto some
extra searchSessionId to be created, which will be eventually\r\nsolved
by Observability team [shortly moving away from
the\r\n`searchSessionId`\r\nmechanism](https://github.com/elastic/kibana/issues/203412)\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Marco Vettorello
<[email protected]>\r\nCo-authored-by: Carlos Crespo
<[email protected]>","sha":"d4194ba5eb5a72960dad00cf956d9ea6e31219e0"}},"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/203150","number":203150,"mergeCommit":{"message":"[Embeddable]
Avoid rerendering loop due to search context reload (#203150)\n\n##
Summary\r\n\r\nFixes #202266\r\n\r\nThis PR fixes the underline
rerendering issue at the `useSearchApi` hook\r\nlevel, so any embeddable
component who uses this hook would benefit from\r\nthe
fix.\r\n\r\nIdeally the props passed to the Lens component should be
memoized, but\r\nthis assumption would break many integrations as the
previous embeddable\r\ncomponent did take care of filtering
duplicates.\r\nTo test this:\r\n* Go to `Observability > Alerts > Manage
rules` and `Add Rule`, pick the\r\n`Custom threshold` option and verify
the infinite reload does not happen\r\n\r\nOnce fixed this, another
problem bubbled up with the brushing use case:\r\nwhen brushing a chart
the chart was always one time range step behind.\r\nThe other bug was
due to the `fetch$(api)` function propagating a stale\r\n`data` search
context who the Lens embeddable was relying on.\r\nTo solve this other
problem the following changes have been applied:\r\n* read the
`searchSessionId` from the `api` directly (used
for\r\n`autoRefresh`)\r\n* make sure to test the `Refresh` feature with
both relative and\r\nabsolute time ranges\r\n* read the `search context`
from the `parentApi` directly if implements\r\nthe `unifiedSearch`
API\r\n* to test this, brush and check that the final time range matches
the\r\ncorrect time range\r\n\r\n**Note**: the fundamental issue for the
latter is `fetch# Backport

This will backport the following commits from `main` to `8.x`:
- [[Embeddable] Avoid rerendering loop due to search context reload
(#203150)](#203150)

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

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

<!--BACKPORT not emitting\r\nthe up-to-date `data` when the parentApi
search context updates. The\r\nretrieved `data` is stale and one step
behind, so it is not reliable.
cc\r\n@elastic/kibana-presentation\r\n\r\nAs @ThomThomson noticed in his
test failure investigation another issue\r\nwas found in this PR due to
the wrong handling of the searchSessionId\r\nwithin the Observability
page (for more details see
[his\r\nanalysis](https://github.com/elastic/kibana/pull/203150#issuecomment-2524080129)).\r\n@markov00
and @crespocarlos helped risolve this problem with some\r\nadditional
changes on the Observability side of things: this will lead\r\nto some
extra searchSessionId to be created, which will be eventually\r\nsolved
by Observability team [shortly moving away from
the\r\n`searchSessionId`\r\nmechanism](https://github.com/elastic/kibana/issues/203412)\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Marco Vettorello
<[email protected]>\r\nCo-authored-by: Carlos Crespo
<[email protected]>","sha":"d4194ba5eb5a72960dad00cf956d9ea6e31219e0"}}]}]
BACKPORT-->

Co-authored-by: Marco Liberati <[email protected]>
CAWilson94 pushed a commit to CAWilson94/kibana that referenced this pull request Dec 12, 2024
…stic#203150)

## Summary

Fixes elastic#202266

This PR fixes the underline rerendering issue at the `useSearchApi` hook
level, so any embeddable component who uses this hook would benefit from
the fix.

Ideally the props passed to the Lens component should be memoized, but
this assumption would break many integrations as the previous embeddable
component did take care of filtering duplicates.
To test this:
* Go to `Observability > Alerts > Manage rules` and `Add Rule`, pick the
`Custom threshold` option and verify the infinite reload does not happen

Once fixed this, another problem bubbled up with the brushing use case:
when brushing a chart the chart was always one time range step behind.
The other bug was due to the `fetch$(api)` function propagating a stale
`data` search context who the Lens embeddable was relying on.
To solve this other problem the following changes have been applied:
* read the `searchSessionId` from the `api` directly (used for
`autoRefresh`)
* make sure to test the `Refresh` feature with both relative and
absolute time ranges
* read the `search context` from the `parentApi` directly if implements
the `unifiedSearch` API
* to test this, brush and check that the final time range matches the
correct time range

**Note**: the fundamental issue for the latter is `fetch$` not emitting
the up-to-date `data` when the parentApi search context updates. The
retrieved `data` is stale and one step behind, so it is not reliable. cc
@elastic/kibana-presentation

As @ThomThomson noticed in his test failure investigation another issue
was found in this PR due to the wrong handling of the searchSessionId
within the Observability page (for more details see [his
analysis](elastic#203150 (comment))).
@markov00 and @crespocarlos helped risolve this problem with some
additional changes on the Observability side of things: this will lead
to some extra searchSessionId to be created, which will be eventually
solved by Observability team [shortly moving away from the
`searchSessionId`
mechanism](elastic#203412)

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: Marco Vettorello <[email protected]>
Co-authored-by: Carlos Crespo <[email protected]>
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) ci:project-deploy-observability Create an Observability project Feature:Embeddables Relating to the Embeddable system release_note:fix Team:obs-ux-management Observability Management User Experience Team Team:Presentation Presentation Team for Dashboard, Input Controls, and Canvas v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Custom threshold] Preview chart goes to infinite loading
9 participants