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

Adds Compile-Time Thread.interrupted() checks #398

Merged
merged 2 commits into from
May 5, 2021

Conversation

dlurton
Copy link
Member

@dlurton dlurton commented Apr 30, 2021

A few times over the past few years we've received reports from a PartiQL service where with a customer sending a very large query (in terms of size of the SQL), where PartiQL would take a long time (>30s) to parse, rewrite, validate, and compile.

Although we have optimized every case where this has happened, this PR applies a mitigation measure which can be used in case it happens again. I have identified several "hot" loops where the state of Thread.interrupted() can be polled and if set an InterruptedException is thrown. This is the hope that if such a query is encountered again, it can be aborted by the calling code rather than risk causing service outages. This approach still might miss loops that we did not anticipate would become hot in the case of unexpected user input, but at least this should allow most such scenarios to be aborted. If it later discovered that we missed such a hot loop in the future, it is a simple matter to add a call to checkThreadInterrupted().

What this PR does not do is add an evaluation-time poll for Thread.interrupted(). That will come in another PR if it is later determined to be needed.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@dlurton dlurton requested review from alancai98, almann and abhikuhikar and removed request for alancai98 and almann April 30, 2021 23:07
@dlurton dlurton self-assigned this Apr 30, 2021
almann
almann previously approved these changes May 4, 2021
Copy link
Contributor

@almann almann left a comment

Choose a reason for hiding this comment

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

:shipit: LGTM, some minor questions below.

lang/src/org/partiql/lang/ast/AstDeserialization.kt Outdated Show resolved Hide resolved
lang/src/org/partiql/lang/ast/passes/AstRewriterBase.kt Outdated Show resolved Hide resolved
lang/src/org/partiql/lang/eval/EvaluatingCompiler.kt Outdated Show resolved Hide resolved
lang/src/org/partiql/lang/eval/EvaluatingCompiler.kt Outdated Show resolved Hide resolved
alancai98
alancai98 previously approved these changes May 4, 2021
Copy link
Member

@alancai98 alancai98 left a comment

Choose a reason for hiding this comment

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

LGTM overall. Only have a couple nits/suggestions. Also, should these checks also be added to the v0.1.5 tag @dlurton ?

lang/src/org/partiql/lang/CompilerPipeline.kt Outdated Show resolved Hide resolved
lang/src/org/partiql/lang/ast/passes/AstRewriterBase.kt Outdated Show resolved Hide resolved
Copy link
Contributor

@abhikuhikar abhikuhikar left a comment

Choose a reason for hiding this comment

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

Some minor nits.
Can you also add an interrupt check in the function validateTopLevelNodes in SqlParser?
Since it recurses over the entire tree, I remember it had very "high flames" in the CPU profiler.

lang/src/org/partiql/lang/CompilerPipeline.kt Outdated Show resolved Hide resolved
lang/src/org/partiql/lang/ast/AstDeserialization.kt Outdated Show resolved Hide resolved
lang/src/org/partiql/lang/ast/ast.kt Outdated Show resolved Hide resolved
lang/src/org/partiql/lang/ast/ast.kt Outdated Show resolved Hide resolved
@dlurton
Copy link
Member Author

dlurton commented May 4, 2021

Can you also add an interrupt check in the function validateTopLevelNodes in SqlParser?

-- Added a check to validateTopLevelNodes

@dlurton
Copy link
Member Author

dlurton commented May 5, 2021

LGTM overall. Only have a couple nits/suggestions. Also, should these checks also be added to the v0.1.5 tag @dlurton ?

That's next after this has been merged to master.

@dlurton dlurton force-pushed the compile-time-interruption branch from 41d2e15 to 3bc9116 Compare May 5, 2021 00:08
@codecov-commenter
Copy link

codecov-commenter commented May 5, 2021

Codecov Report

Merging #398 (3bc9116) into master (eee99ae) will increase coverage by 0.02%.
The diff coverage is 94.11%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #398      +/-   ##
============================================
+ Coverage     81.88%   81.91%   +0.02%     
- Complexity     1350     1351       +1     
============================================
  Files           168      169       +1     
  Lines         10491    10508      +17     
  Branches       1737     1738       +1     
============================================
+ Hits           8591     8608      +17     
  Misses         1360     1360              
  Partials        540      540              
Flag Coverage Δ Complexity Δ
CLI 18.18% <ø> (ø) 21.00 <ø> (ø)
EXAMPLES 74.85% <ø> (ø) 29.00 <ø> (ø)
LANG 84.41% <94.11%> (+0.02%) 1144.00 <70.00> (+1.00)
PTS ∅ <ø> (∅) 0.00 <ø> (ø)
TEST_SCRIPT 79.68% <ø> (ø) 157.00 <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ Complexity Δ
lang/src/org/partiql/lang/ast/passes/AstVisitor.kt 0.00% <ø> (ø) 0.00 <0.00> (ø)
.../src/org/partiql/lang/util/ThreadInterruptUtils.kt 50.00% <50.00%> (ø) 0.00 <0.00> (?)
lang/src/org/partiql/lang/CompilerPipeline.kt 88.37% <100.00%> (+5.03%) 0.00 <0.00> (ø)
...ang/src/org/partiql/lang/ast/AstDeserialization.kt 86.09% <100.00%> (+0.04%) 0.00 <0.00> (ø)
lang/src/org/partiql/lang/ast/AstSerialization.kt 85.98% <100.00%> (+0.03%) 0.00 <0.00> (ø)
...ng/src/org/partiql/lang/ast/ExprNodeToStatement.kt 93.06% <100.00%> (+0.02%) 0.00 <0.00> (ø)
...ng/src/org/partiql/lang/ast/StatementToExprNode.kt 96.67% <100.00%> (+0.01%) 0.00 <0.00> (ø)
lang/src/org/partiql/lang/ast/ast.kt 89.08% <100.00%> (ø) 0.00 <0.00> (ø)
...src/org/partiql/lang/ast/passes/AstRewriterBase.kt 100.00% <100.00%> (ø) 124.00 <21.00> (ø)
lang/src/org/partiql/lang/ast/passes/AstWalker.kt 71.14% <100.00%> (+0.19%) 29.00 <0.00> (ø)
... and 13 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update eee99ae...3bc9116. Read the comment docs.

abhikuhikar
abhikuhikar previously approved these changes May 5, 2021
Copy link
Contributor

@abhikuhikar abhikuhikar 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.

almann
almann previously approved these changes May 5, 2021
Copy link
Contributor

@almann almann left a comment

Choose a reason for hiding this comment

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

:shipit: For the future, I recommend that you try to avoid rebasing/squashing commits after you get feedback as it makes it impossible for the reviewer to do inter-diffs or only look at what you changed.

alancai98
alancai98 previously approved these changes May 5, 2021
Copy link
Member

@alancai98 alancai98 left a comment

Choose a reason for hiding this comment

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

Noticed one small typo. Rest of changes LGTM

* to [transformExpr].
*
* All transforms should derive from this class instead of [PartiqlAst.VisitorTransform] so that they can
* be interrupted of they take a long time to process large ASTs.
Copy link
Member

Choose a reason for hiding this comment

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

typo: "be interrupted if..."

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

@dlurton dlurton dismissed stale reviews from alancai98, almann, and abhikuhikar via 8c8b5e5 May 5, 2021 15:51
@dlurton dlurton requested a review from alancai98 May 5, 2021 15:51
@dlurton
Copy link
Member Author

dlurton commented May 5, 2021

:shipit: For the future, I recommend that you try to avoid rebasing/squashing commits after you get feedback as it makes it impossible for the reviewer to do inter-diffs or only look at what you changed.

Yes, sorry, I realized that too late.

@dlurton dlurton merged commit 7926b29 into master May 5, 2021
@dlurton dlurton deleted the compile-time-interruption branch May 6, 2021 21:11
dlurton added a commit that referenced this pull request May 7, 2021
* Add Thread.interrupted() checks
dlurton added a commit that referenced this pull request May 7, 2021
…403)

* Adds Compile-Time `Thread.interrupted()` checks (#398)

* Add Thread.interrupted() checks

* Reduce unit test memory usage

* Remove `@Deprecated`
@dlurton dlurton added this to the v0.3.0 milestone Jun 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants