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

pass elasticsearch response to browser as a stream #193060

Merged
merged 58 commits into from
Nov 7, 2024

Conversation

ppisljar
Copy link
Member

@ppisljar ppisljar commented Sep 16, 2024

Remove decoding and re-encoding of the ES-response from Kibana-server. Resolves #189640

  • Only works with bfetch disabled (bfetch:disable advanced setting set to true, which is the default)
  • only works with async esql and async dsl search strategies
  • In above cases it does not parse ES response server-side, but forwards the Readable stream from the ES JS client to the HTTP response
  • Reads ID (x-elasticsearch-async-id) and completion status (x-elasticsearch-async-is-running) from headers so that parsing the body can happen on the client rather than both on the client & server

How to test:
when using ese or async esql search strategies (discover, lens, maps, ....) you should see in the network tab of developer tools that the responses to your requests come directly from es and they should be gzipped
Screenshot 2024-10-07 at 11 41 11

  • functionality wise nothing should change

@ppisljar ppisljar added release_note:skip Skip the PR/issue when compiling release notes backport:skip This commit does not require backporting labels Sep 17, 2024
@ppisljar
Copy link
Member Author

/ci

@ppisljar
Copy link
Member Author

/ci

@ppisljar
Copy link
Member Author

/ci

@ppisljar
Copy link
Member Author

/ci

Copy link
Contributor

@yngrdyn yngrdyn left a comment

Choose a reason for hiding this comment

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

log_entry_search test LGTM


/**
* Get the `total`/`loaded` for this response (see `IKibanaSearchResponse`). Note that `skipped` is
* not included as it is already included in `successful`.
* @internal
*/
export function getTotalLoaded(response: estypes.SearchResponse<unknown>) {
if (!response._shards) return {};
Copy link
Member

Choose a reason for hiding this comment

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

Looking at the estypes.SearchResponse type this should never happen. Have you encountered a case where _shards property doesn't exist in response?

src/plugins/data/common/search/utils.ts Outdated Show resolved Hide resolved
}
)
.then((rawResponse) => {
const response = rawResponse.body!;
Copy link
Member

Choose a reason for hiding this comment

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

too much trust here, probably better to avoid non-null assertion since everything else here seems soo fragile

Comment on lines 480 to 481
const error = (response as unknown as Record<string, unknown>).error;
if (error) {
Copy link
Member

Choose a reason for hiding this comment

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

Does this error surfaced only with the stream configuration?
Because if yes we shouldn't solve it here, but probably in a more deeper substrate. Feels wrong to handle an error from something that should be a valid response

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, this only comes up with streams

Comment on lines 85 to 86
'kbn-is-restored': response.isRestored ? '?1' : '?0',
'kbn-search-request-params': JSON.stringify(response.requestParams),
Copy link
Member

Choose a reason for hiding this comment

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

Can we improve the naming here, in particular, the kbn-is-restored doesn't describe much of what is restored.
Would be great if we could move these headers into some reused const.

...request,
...searchOptions,
}),
.post<IKibanaSearchResponse>(
Copy link
Member

Choose a reason for hiding this comment

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

You can probably change this type to IKibanaSearchResponse | ErrorResponseBase ErrorResponseBase comes from elasticsearch client and seems to the the type of response that is returned by ES when you have an error. To test it you can created a custom DSL filter with a wrong syntax like

{
  "match_ph": {
    "bytes": 21212
  }
}

Copy link
Member

Choose a reason for hiding this comment

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

it is very strange that we never considered using this type in our code. ES always return 200 even if there is an error in the query, it look like it just throw other status code if the call itself is wrong, not if the response is wrong.

Copy link
Member Author

Choose a reason for hiding this comment

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

no idea, but my goal was to keep the scope of this pr as limited as possible.

@markov00 markov00 self-requested a review November 4, 2024 10:14
Copy link
Member

@markov00 markov00 left a comment

Choose a reason for hiding this comment

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

Changes looks good to me

@elasticmachine
Copy link
Contributor

elasticmachine commented Nov 7, 2024

💚 Build Succeeded

  • Buildkite Build
  • Commit: a556138
  • Kibana Serverless Image: docker.elastic.co/kibana-ci/kibana-serverless:pr-193060-a556138439d8

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
data 515 516 +1

Page load bundle

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

id before after diff
data 419.9KB 421.5KB +1.6KB
Unknown metric groups

API count

id before after diff
@kbn/search-types 50 51 +1

ESLint disabled line counts

id before after diff
data 56 57 +1

Total ESLint disabled count

id before after diff
data 58 59 +1

History

cc @ppisljar

@ppisljar ppisljar merged commit 7b81bcf into elastic:main Nov 7, 2024
29 checks passed
mbondyra pushed a commit to mbondyra/kibana that referenced this pull request Nov 8, 2024
Remove decoding and re-encoding of the ES-response from Kibana-server.
Resolves elastic#189640
ppisljar added a commit that referenced this pull request Nov 19, 2024
@lukasolson
Copy link
Member

💚 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

lukasolson pushed a commit to lukasolson/kibana that referenced this pull request Jan 7, 2025
Remove decoding and re-encoding of the ES-response from Kibana-server.
Resolves elastic#189640

(cherry picked from commit 7b81bcf)
lukasolson added a commit that referenced this pull request Jan 8, 2025
…205811)

# Backport

This will backport the following commits from `main` to `8.x`:
- [pass elasticsearch response to browser as a stream
(#193060)](#193060)

<!--- Backport version: 8.9.8 -->

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

<!--BACKPORT [{"author":{"name":"Peter
Pisljar","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-07T11:19:13Z","message":"pass
elasticsearch response to browser as a stream (#193060)\n\nRemove
decoding and re-encoding of the ES-response from
Kibana-server.\r\nResolves
https://github.com/elastic/kibana/issues/189640","sha":"7b81bcf60a7fbc287fdfe2aa6b44009b04024229","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Search","backport","release_note:skip","backport:skip","v9.0.0","ci:project-deploy-observability","v8.18.0"],"number":193060,"url":"https://github.com/elastic/kibana/pull/193060","mergeCommit":{"message":"pass
elasticsearch response to browser as a stream (#193060)\n\nRemove
decoding and re-encoding of the ES-response from
Kibana-server.\r\nResolves
https://github.com/elastic/kibana/issues/189640","sha":"7b81bcf60a7fbc287fdfe2aa6b44009b04024229"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","labelRegex":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/193060","number":193060,"mergeCommit":{"message":"pass
elasticsearch response to browser as a stream (#193060)\n\nRemove
decoding and re-encoding of the ES-response from
Kibana-server.\r\nResolves
https://github.com/elastic/kibana/issues/189640","sha":"7b81bcf60a7fbc287fdfe2aa6b44009b04024229"}},{"branch":"8.18","label":"v8.18.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Peter Pisljar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting backport ci:project-deploy-observability Create an Observability project Feature:Search Querying infrastructure in Kibana release_note:skip Skip the PR/issue when compiling release notes v8.18.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Search] Remove unnecessary encoding/decoding of the ES-response in Kibana server
7 participants