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

[WIP] Expressions caching #51160

Closed
wants to merge 1 commit into from
Closed

Conversation

ppisljar
Copy link
Member

@ppisljar ppisljar commented Nov 20, 2019

Summary

Adds caching for interpreter:

  • caching can be disabled for every call at interpretAst
  • at the time function should be executed we calculate a hash of: input, args, executionContext and fnDef and check the cache for existing result. If we find one we can skip executing this function.
  • function can have disableCache flag set to true, which will prevent caching of that function

arguments are resolved before this, so using subexpressions, default values etc should work as expected (if subexpression evaluates to the same result it did last time executing the current function can be skipped)

todo:
as timerange which we use in esaggs is in estime format now-90d caching kicks in when we rerun same expression at later time and we don't get new result. For now esaggs function has the disableCache flag set to true. When we refactore esaggs in the future this flag should move to the function that will be generating actual timerange from the estime format

open questions:

  • how to handle cache expiration and limits

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

For maintainers

@elasticmachine
Copy link
Contributor

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

@elasticmachine
Copy link
Contributor

💔 Build Failed

@ppisljar ppisljar force-pushed the expr/caching branch 3 times, most recently from aea6428 to f240fd0 Compare November 26, 2019 15:38
@ppisljar ppisljar marked this pull request as ready for review November 26, 2019 15:38
@ppisljar ppisljar requested a review from a team as a code owner November 26, 2019 15:38
@ppisljar ppisljar added Feature:ExpressionLanguage Interpreter expression language (aka canvas pipeline) review v7.7.0 v8.0.0 labels Nov 26, 2019
@ppisljar ppisljar requested a review from Dosant November 26, 2019 15:39
@elasticmachine
Copy link
Contributor

💔 Build Failed

@ppisljar ppisljar added the release_note:skip Skip the PR/issue when compiling release notes label Nov 26, 2019
@elasticmachine
Copy link
Contributor

💔 Build Failed

@@ -37,12 +38,13 @@ export interface InterpreterConfig {
functions: FunctionsRegistry;
types: any;
handlers: any;
functionCache: Map<any, any>;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
functionCache: Map<any, any>;
functionCache: Map<string, any>;

Copy link
Contributor

@streamich streamich Feb 24, 2020

Choose a reason for hiding this comment

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

functionCache: Map<string, unknown>;

I don't know if this will work out, but if it does, it is more restrictive.

Copy link
Contributor

@streamich streamich left a comment

Choose a reason for hiding this comment

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

Would be nice to have unit tests for this feature.

Copy link
Member

@lukasolson lukasolson left a comment

Choose a reason for hiding this comment

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

Mostly a nit: Can we standardize on a single variable name like disableCache or enableCache and use it as the name for both the variable in the function definition and when invoking the interpreter?

Copy link
Contributor

@streamich streamich left a comment

Choose a reason for hiding this comment

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

Also, maybe there is a way to write this in a more modular way and put the caching logic in some separate place. Otherwise, this interpreterProvider function is already too long.

handlers.getInitialContext(),
]);
fnOutput = functionCache.get(hash);
if (!fnOutput) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Possible 0, "", false edge case?
I'd use functionCache.has(hash) to check if there is a cache hit

@ppisljar ppisljar changed the title Expr/caching Expressions caching Jan 14, 2020
@ppisljar
Copy link
Member Author

Also, maybe there is a way to write this in a more modular way and put the caching logic in some separate place. Otherwise, this interpreterProvider function is already too long.

are you fine for leaving it as is for now, you plan to refactor interpreterProvider anyway ?

@streamich
Copy link
Contributor

... you plan to refactor interpreterProvider anyway ?

@ppisljar yes in my branch this interpreterProvider function is all refactored and does not exist anymore, which also means we have huge merge conflicts. I can either add caching in my branch based on the example you have here, or we can merge refactoring first.

@ppisljar
Copy link
Member Author

ppisljar commented Jan 14, 2020

lets merge refactoring first, and lets add caching in separate PR. There is not much of code here so it should not be hard to reimplement in the refactored version.

@ppisljar
Copy link
Member Author

updated this on top of refactoring.

@ppisljar
Copy link
Member Author

retest

Copy link
Contributor

@streamich streamich left a comment

Choose a reason for hiding this comment

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

You have disabled caching from some of the functions maintained in expressions plugin and esaggs, and tsvb. But opting in by default all functions into caching behaviour seems to be a breaking change, as I understand it, or no? For example, shall we also opt-out of caching some of Lens and Canvas functions?

src/plugins/expressions/common/execution/types.ts Outdated Show resolved Hide resolved
/**
* prevents caching in the current execution.
*/
disableCache?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this something we should expose through execution context to all functions? How would functions use this?

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 should already be provided to all functions on the executionContext.

if some function would to do some internal caching (esaggs with make it slow) this would indicate that it should do it.

Comment on lines 230 to 283
const hash = calculateObjectHash([fn, normalizedInput, args, this.context.search]);
if (!this.context.disableCache && !fn.disableCache && this.functionCache.has(hash)) {
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 can first check if we have to cache and only then hash.

const doCaching = !this.context.disableCache && !fn.disableCache;
if (doCaching) {
  const hash = calculateObjectHash([fn, normalizedInput, args, this.context.search]);
}

src/plugins/expressions/common/execution/execution.ts Outdated Show resolved Hide resolved
src/plugins/expressions/common/execution/execution.ts Outdated Show resolved Hide resolved
@spalger spalger added v7.7.1 and removed v7.7.0 labels May 14, 2020
@lukeelmers lukeelmers added the WIP Work in progress label Jul 7, 2020
@ppisljar ppisljar added v7.11.0 and removed v7.7.1 labels Jul 21, 2020
@ppisljar ppisljar changed the title Expressions caching [WIP] Expressions caching Jul 30, 2020
@LeeDr
Copy link

LeeDr commented Jan 7, 2021

Since we're past 7.11.0 FF, and this isn't a bug fix, should probably bump to 7.12.0.

@ppisljar ppisljar requested a review from a team April 1, 2021 01:25
@flash1293
Copy link
Contributor

@ppisljar This PR is super outdated, did you mean to move it out of draft status or did you want to close it?

@kibanamachine
Copy link
Contributor

kibanamachine commented Apr 1, 2021

💔 Build Failed

Failed CI Steps

Metrics [docs]

‼️ ERROR: no builds found for mergeBase sha [b125472]

History

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

@ppisljar
Copy link
Member Author

we implemented request caching on search service which should resolved main needs for which we were thinking about expression caching

@ppisljar ppisljar closed this Apr 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:ExpressionLanguage Interpreter expression language (aka canvas pipeline) release_note:skip Skip the PR/issue when compiling release notes v8.0.0 WIP Work in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants