-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Change ContextContainer to lazily initialize providers #129896
Change ContextContainer to lazily initialize providers #129896
Conversation
…dler-context' into kbn-84763-route-handler-context
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one tiny thing for OLM otherwise 👌 ✅
import { SecuritySolutionRequestHandlerContext } from '../types'; | ||
import { parseExperimentalConfigValue } from '../../common/experimental_features'; | ||
// A TS error (TS2403) is thrown when attempting to export the mock function below from Cases |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this comment should stay above line 26?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
App Services changes LGTM
|
||
const builtContext = {} as HandlerContextType<RequestHandler>; | ||
(builtContext as unknown as RequestHandlerContext).resolve = async (keys) => { | ||
const resolved = await Promise.all( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to reject everything on the first rejected promise and/or error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, I think we do. Managing partially resolved contexts seems too complex and not really necessary given there's no real risk given the underlying providers. This is why I felt a simple Promise.all
was good enough.
Also, that was the behavior when the context was implicitly awaited before the route handler execution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving for cloud security posture
@@ -6,6 +6,8 @@ | |||
*/ | |||
/* eslint-disable @typescript-eslint/no-explicit-any */ | |||
|
|||
import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this import *
needed if we only use the IndexRequest
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually import *
for the ES client types in the whole codebase, given this has no real impact (especially for test files), but I can use named imports instead if you prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OLM changes LGTM!
Some benchmarking: Kibana instances: GCP Given the testing environment wasn't perfect, the results are to be taken with a grain of salt, but each scenario was ran 3 times with fairly consistent results. Overall summaryAverage response times
Observation: not the best indicator, but the PR's results are plain better. Overall statistics
Overall this looks very positive. Detailed resultsReference - 200 usersPR - 200 usersReference - 300 usersPR - 300 usersRemarks: the failures on the 300 users scenarios are caused by reaching limits on ssh-forwarding and are not real Kibana failures |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
💛 Build succeeded, but was flakyTest Failures
Metrics [docs]Public APIs missing comments
Public APIs missing exports
Unknown metric groupsAPI count
History
To update your PR or re-run it, just comment with: |
* Change ContextContainer to lazily initialize providers * Introduce CustomRequestHandlerContext, start adapting usages * adapt IContextProvider's return type * start fixing violations * fixing violations - 2 * adapt home routes * fix remaining core violation * fix violations on core tests * fixing more violations * fixing more violations * update generated doc... * fix more violations * adapt remaining RequestHandlerContext * fix more violations * fix non-async method * more fixes * fix another await in non async method * add yet another missing async * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' * add yet yet another missing async * update fleet's endpoints * fix telemetry endpoints * fix event_log endpoints * fix some security unit tests * adapt canvas routes * adapt alerting routes * adapt more so_tagging routes * fix data_enhanced routes * fix license_management routes * fix file_upload routes * fix index_management routes * fix lists routes * fix snapshot_restore routes * fix rule_registry routes * fix ingest_pipelines routes * fix remote_clusters routes * fix index_lifecycle_management routes * improve and fix the lazy implementation * fix triggers_actions_ui endpoints * start fixing unit tests * fix cases routes * fix transform routes * fix upgrade_assistant routes * fix uptime route wrapper * fix uptime route wrapper bis * update osquery routes * update cross_cluster_replication routes * fix some ML routes / wrappers * adapt maps routes * adapt rollup routes * fix some canvas unit tests * fix more canvas unit tests * fix observability wrapper * fix (?) infra type hell * start fixing monitoring * fix a few test plugins * woups * fix yet more violations * fixing UA tests * fix logstash handlers * fix fleet unit tests * lint? * one more batch * update security_solution endpoints * start fixing security_solution mocks * start fixing security_solution tests * fix more security_solution tests * fix more security_solution tests * just one more * fix last (?) security_solution tests * fix timelion javascript file * fix more test plugins * fix transforms context type * fix ml context type * fix context tests * fix securitySolution withEndpointAuthz tests * fix features unit tests * fix actions unit tests * fix imports * fix duplicate import * fix some merge problems * fix new usage * fix new test * introduces context.resolve * down the rabbit hole again * start fixing test type failures * more test type failures fixes * move import comment back to correct place * more test type failures fixes, bis * use context.resolve for security solution rules routes * fix new violations due to master merge * remove comment Co-authored-by: kibanamachine <[email protected]>
(obligatory comment: this is not a failed rebase)
Summary
Fix #84763
Enhancement of #78957
For performances purposes, we adapted core's request handler context to lazily instantiate its properties on access (#78957). This PR goes a step further, by ensuring that all registered context providers will only be invoked/instantiated when effectively used by the request handler.
This was done by changing the
ContextContainer
implementation to lazily create the context parts when they are individually accessed instead of eagerly instantiating them all before handler execution.Due to the allowed asynchronous nature of some context providers, this implies that access to context parts within a handler has now to be done asynchronously.
E.g
Now becomes
Or
This PR:
ContextContainer
RequestHandlerContext
accordinglyCustomRequestHandlerContext
type to perform any potential sync context provider -> async context handler type conversionNotes to reviewers
The changes, even if impacting a lot of files, were for the most trivial, and given the overall excellent TS type checks around usages of contexts within request handlers (in addition to our FTR test coverage), the potential risks of having missed any usages is evaluated as fairly low.