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

opt: factor out common panic recovery into helper function #91752

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

michae2
Copy link
Collaborator

@michae2 michae2 commented Nov 11, 2022

In the optimizer we have the convention of using panics for error propagation, to avoid adding error handling code everywhere in deeply recursive call stacks. This is safe because the optimizer is single-threaded and not manipulating locks, shared state, etc. But it means that wherever we call into the optimizer we need to add panic recovery.

We were duplicating the same basic block of panic recovery code in all these places. Factor it into a "catch" function, somewhat similar to the vectorized engine's CatchVectorizedRuntimeError.`

Assists: #88246

Release note: None

In the optimizer we use the convention of panic-based error handling for
a performance advantage. This means at the boundaries we must remember
to recover and return errors instead of letting panics escape.

`sql.(*optPlanningCtx).makeQueryIndexRecommendation` calls some
optimizer helper functions in addition to calling
`xform.(*Optimizer).Optimize`. While `Optimize` has the panic-recovery
code, these helper functions do not, so we need to add panic recovery
to `makeQueryIndexRecommendation` as well.

Fixes: cockroachdb#88246

Release note: None
In the optimizer we have the convention of using panics for error
propagation, to avoid adding error handling code everywhere in deeply
recursive call stacks. This is safe because the optimizer is single-
threaded and not manipulating locks, shared state, etc. But it means
that wherever we call into the optimizer we need to add panic recovery.

We were duplicating the same basic block of panic recovery code in all
these places. Factor it into a "catch" function, somewhat similar to the
vectorized engine's `CatchVectorizedRuntimeError`.`

Assists: cockroachdb#88246

Release note: None
@michae2 michae2 requested a review from a team as a code owner November 11, 2022 17:36
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@michae2
Copy link
Collaborator Author

michae2 commented Nov 11, 2022

First commit is #91457 and can be ignored.

Copy link
Collaborator Author

@michae2 michae2 left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @DrewKimball, @RaduBerinde, and @yuzefovich)


pkg/sql/opt/catch.go line 25 at r2 (raw file):

		// string which does not implement error. So in this case we suspect we are
		// not able to recover, and must crash.
		panic(r)

(This comes from #38570 (review) FYI.)

Copy link
Collaborator

@DrewKimball DrewKimball 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 for doing this!

Reviewed 1 of 1 files at r1.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @RaduBerinde and @yuzefovich)

@michae2
Copy link
Collaborator Author

michae2 commented Nov 14, 2022

Unfortunately this approach is broken: https://go.dev/play/p/sXjrTeXt4G9

The call to recover() needs to be in the top-level deferred function call. So either I need to:

  1. defer opt.CatchOptimizerError() instead of defer func() { opt.CatchOptimizerError() }(), or
  2. call the optimizer functions from within CatchOptimizerError the way CatchVectorizedRuntimeError does.

I think I'll probably do (2).

@jordanlewis
Copy link
Member

Drive-by: users and TSE are frequently thrown by seeing "Error" in stack traces that aren't erroring. We may wish to rename those functions to not contain "Error", or instead use your strategy #1.

@michae2
Copy link
Collaborator Author

michae2 commented Nov 14, 2022

Drive-by: users and TSE are frequently thrown by seeing "Error" in stack traces that aren't erroring. We may wish to rename those functions to not contain "Error", or instead use your strategy #1.

Great point, thank you! I'll rename the function so it sounds benign.

@DrewKimball
Copy link
Collaborator

How would people feel about a pattern like this one? Closer to the panic-catching in the vectorized engine.

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.

4 participants