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

[data.search] Add request handler context and asScoped pattern #80775

Merged
merged 19 commits into from
Nov 4, 2020

Conversation

lukasolson
Copy link
Member

Summary

Depends on #80116.
Addresses #78461.

This PR changes the APIs we expose from our server-side data plugin search service. Before this PR, we were exposing a single search function from the start contract:

search(searchRequest: SearchStrategyRequest, options: ISearchOptions, context: RequestHandlerContext): Observable<SearchStrategyResponse>;

Passing context around was problematic because of two reasons:

  1. It provided a poor contract which exposed everything on RequestContext to implementers of search strategies
  2. It made it difficult to use outside of the scope of a route handler (see discussion in [Discuss] Removing RequestHandlerContext from the low-level data.search API. #78461)

This PR attempts to address these by providing two mechanisms of accessing the search method:

  1. Via a newly registered route handler context:
router.post({}, async (context, request, res) => {
   const response = await context.search!.search(searchRequest, options).toPromise();
});
  1. Via a new asScoped method in the data plugin start contract:
async function doSearch(request: KibanaRequest) {
  const response = await data.search.asScoped(request).search(searchRequest, options).toPromise();
}

It also updates the way we register custom search strategies. Instead of accepting a context inside of the search/cancel functions, you now receive SearchStrategyDependencies:

interface SearchStrategyDependencies {
  savedObjectsClient: SavedObjectsClientContract;
  esClient: IScopedClusterClient;
  uiSettingsClient: IUiSettingsClient;
}

In this way, we can control which dependencies are being passed/used in custom search strategies. If we need to add more along the way, we can, instead of relying implicitly on what's inside context.

Checklist

Delete any items that are not applicable to this PR.

For maintainers

@lukasolson lukasolson added Feature:Search Querying infrastructure in Kibana refactoring v8.0.0 Team:AppArch release_note:skip Skip the PR/issue when compiling release notes v7.11.0 labels Oct 15, 2020
@lukasolson lukasolson requested a review from a team as a code owner October 15, 2020 22:12
@lukasolson lukasolson requested a review from a team October 15, 2020 22:12
@lukasolson lukasolson requested review from a team as code owners October 15, 2020 22:12
@lukasolson lukasolson self-assigned this Oct 15, 2020
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-app-arch (Team:AppArch)

@@ -22,17 +22,6 @@ import { IKibanaSearchRequest, IKibanaSearchResponse } from '../types';

export const ES_SEARCH_STRATEGY = 'es';

export interface ISearchOptions {
Copy link
Member Author

@lukasolson lukasolson Oct 15, 2020

Choose a reason for hiding this comment

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

Moved up to common/search/types

@lukasolson lukasolson changed the title [data.search] Add request handler context and asScoped pattern [skip-ci] [data.search] Add request handler context and asScoped pattern Oct 16, 2020
@lukasolson lukasolson changed the title [skip-ci] [data.search] Add request handler context and asScoped pattern [data.search] Add request handler context and asScoped pattern Oct 16, 2020
@lukasolson lukasolson changed the title [data.search] Add request handler context and asScoped pattern [skip-ci] [data.search] Add request handler context and asScoped pattern Oct 16, 2020
@lukasolson
Copy link
Member Author

Skipping CI for now since we know it won't pass until #80116 is merged.

const indexPatternsFetcher = new IndexPatternsFetcher(
elasticsearch.legacy.client.callAsCurrentUser
);
const indexPatternsFetcher = new IndexPatternsFetcher(esClient.asCurrentUser);
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 is the portion that relies on #80116.

Copy link
Member

@lukeelmers lukeelmers left a comment

Choose a reason for hiding this comment

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

Overall approach makes sense to me! In particular I like the addition of SearchStrategyDependencies, this feels like a good way to limit the use of implicit dependencies in strategies to a known quantity.

Still need to do some testing, but in general this LGTM

return {
aggs: this.aggsService.start({ fieldFormats, uiSettings }),
getSearchStrategy: this.getSearchStrategy,
search: this.search.bind(this),
asScoped: getSearchClientAsScoped,
Copy link
Member

Choose a reason for hiding this comment

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

<tangent>

I was thinking about whether this should be async, but I guess it doesn't have to be.

Was trying to remember why I made SearchSource.asScoped async -- turns out it is because index patterns contract is async, because field formats contract is async, because it calls await uiSettings.getAll()

</tangent>

Comment on lines -188 to -197
const fakeRequestHandlerContext = {
core: {
elasticsearch: {
client: esClient,
},
uiSettings: {
client: uiSettingsClient,
},
},
} as RequestHandlerContext;
Copy link
Member

Choose a reason for hiding this comment

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

🙌

@lukasolson lukasolson changed the title [skip-ci] [data.search] Add request handler context and asScoped pattern [data.search] Add request handler context and asScoped pattern Oct 27, 2020
@lukasolson
Copy link
Member Author

@elasticmachine merge master

@lukasolson
Copy link
Member Author

I tested out the EQL search strategy and verified that it's still working as expected 👍 , but I'm going to defer approval for SecuritySolution code to someone from the Threat Hunting team as they wrote the search strategies that are failing on this branch. It appears as though those strategies use of an id conflicts with the async search functionality, causing a verification_exception to be returned from elasticsearch.

Yep, I've fixed this now. @XavierM we should sync up about how the ID parameter is being used in the security solution, as this interferes with the async search mechanism. Right now, the security search strategy is always using the open-source ES search strategy.

@lukasolson
Copy link
Member Author

pinging @elastic/endpoint-app-team and @elastic/kibana-app for codeowner review, please and thank you!

Copy link
Contributor

@lizozom lizozom left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@alexwizp alexwizp left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@stratoula stratoula left a comment

Choose a reason for hiding this comment

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

KibanaApp changes LGTM

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

page load bundle size

id before after diff
data 954.7KB 954.7KB -18.0B
dataEnhanced 27.9KB 28.0KB +92.0B
total +74.0B

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

Copy link
Contributor

@XavierM XavierM left a comment

Choose a reason for hiding this comment

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

Everything is working as expected on the security solutions! Thanks a lot

@lukasolson lukasolson merged commit 44368b0 into elastic:master Nov 4, 2020
lukasolson added a commit to lukasolson/kibana that referenced this pull request Nov 4, 2020
…ic#80775)

* [Search] Add request context and asScoped pattern

* Update docs

* Unify interface for getting search client

* Update examples/search_examples/server/my_strategy.ts

Co-authored-by: Anton Dosov <dosantappdev@gmail.com>

* Review feedback

* Fix checks

* Fix CI

* Fix security search

* Fix test

* Fix test for reals

* Fix types

Co-authored-by: Anton Dosov <dosantappdev@gmail.com>
lukasolson added a commit that referenced this pull request Nov 4, 2020
… (#82648)

* [Search] Add request context and asScoped pattern

* Update docs

* Unify interface for getting search client

* Update examples/search_examples/server/my_strategy.ts

Co-authored-by: Anton Dosov <dosantappdev@gmail.com>

* Review feedback

* Fix checks

* Fix CI

* Fix security search

* Fix test

* Fix test for reals

* Fix types

Co-authored-by: Anton Dosov <dosantappdev@gmail.com>

Co-authored-by: Anton Dosov <dosantappdev@gmail.com>
gmmorris added a commit to gmmorris/kibana that referenced this pull request Nov 5, 2020
* master: (127 commits)
  [ILM] Fix breadcrumbs (elastic#82594)
  [UX]Swap env filter with percentile (elastic#82246)
  Add platform's missing READMEs (elastic#82268)
  [Discover] Adding uiMetric to track Visualize link click (elastic#82344)
  [Search] Add used index pattern name to the search agg error field (elastic#82604)
  improve client-side SO client get pooling (elastic#82603)
  [Security Solution] Unskips Overview tests (elastic#82459)
  Embeddables/migrations (elastic#82296)
  [Enterprise Search] Refactor product server route registrations to their own files/folders (elastic#82663)
  Moving reinstall function outside of promise.all (elastic#82672)
  Load choropleth layer correctly (elastic#82628)
  Master  backport elastic#81233 (elastic#82642)
  [Fleet] Allow snake cased Kibana assets (elastic#77515)
  Reduce saved objects authorization checks (elastic#82204)
  [data.search] Add request handler context and asScoped pattern (elastic#80775)
  [ML] Fixes formatting of fields in index data visualizer (elastic#82593)
  Usage collector readme (elastic#82548)
  [Lens] Visualization validation and better error messages (elastic#81439)
  [ML] Add annotation markers to time series brush area to indicate annotations exist outside of selected range (elastic#81490)
  chore(NA): install microdnf in UBI docker build only (elastic#82611)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:Search Querying infrastructure in Kibana refactoring release_note:skip Skip the PR/issue when compiling release notes v7.11.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.