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

fix concurrency on stream execute engine primitives #14586

Merged
merged 6 commits into from Nov 23, 2023

Conversation

harshit-gangal
Copy link
Member

@harshit-gangal harshit-gangal commented Nov 22, 2023

Description

This PR adds the required lock on the engine primitive with Streaming support where a local state is maintained and needs to be synchronized between parallel calls from underlying callbacks.

The primitive were either returning the wrong result or were causing VTGate panics.

Related Issue(s)

Checklist

  • "Backport to:" labels have been added if this change should be back-ported
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on the CI
  • Documentation was added or is not required

… apis

Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Copy link
Contributor

vitess-bot bot commented Nov 22, 2023

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Nov 22, 2023
@harshit-gangal harshit-gangal added Type: Bug Component: Query Serving and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request labels Nov 22, 2023
@github-actions github-actions bot added this to the v19.0.0 milestone Nov 22, 2023
@@ -88,6 +88,7 @@ func (p *Projection) TryStreamExecute(ctx context.Context, vcursor VCursor, bind
env := evalengine.NewExpressionEnv(ctx, bindVars, vcursor)
var once sync.Once
var fields []*querypb.Field
var mu sync.Mutex
Copy link
Contributor

Choose a reason for hiding this comment

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

@harshit-gangal Is this one necessary? There's already locking in the implementation here which should afaik handle any concurrency issues.

@vmg Afaik the evalengine.NewExpressionEnv should be concurrent usage safe, or is it not?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, it definitely isn't, it contains the VM that is used to execute the expressions. You cannot evaluate two expressions at once in the same VM.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah ok, so we gotta lock here then as well for now unless we'd want to move creating the env inside the callback then.

Copy link
Member Author

Choose a reason for hiding this comment

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

The next plan is to create env per shard.

Copy link
Member Author

Choose a reason for hiding this comment

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

That refactor would not be part of the backport and will be made as a separate PR.

@@ -108,6 +110,8 @@ func (l *Limit) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars
return nil
}

mu.Lock()
defer mu.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like need this for the count, but wonder, should we do this with an atomic counter instead? The usage might be complicated though, so the lock might be a lot easier for now and then to optimize later.

go/vt/vtgate/engine/filter.go Show resolved Hide resolved
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
@harshit-gangal harshit-gangal added Backport to: release-16.0 Backport to: release-17.0 Needs to be back ported to release-17.0 Backport to: release-18.0 Needs to be back ported to release-18.0 labels Nov 23, 2023
…rojectio and distinct streaming async test

Signed-off-by: Harshit Gangal <harshit@planetscale.com>
@harshit-gangal harshit-gangal marked this pull request as ready for review November 23, 2023 07:43
return vcursor.StreamExecutePrimitive(ctx, p.Input, bindVars, wantfields, func(qr *sqltypes.Result) error {
var err error
if wantfields {
if wantfields && qr.Fields != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

@harshit-gangal This change breaks a bunch of tests it looks like. Why was this changed?

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this change was made due to the testing in projection_test.go, but I suspect it's a problem with that test? Since it runs concurrent stream results, but even if that happens, it's guaranteed that there would always be a field result first, so I don't think the test models a real scenario then.

Copy link
Contributor

Choose a reason for hiding this comment

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

I also think that now that we lock here to also protect the evalengine expression env, we don't need the more elaborate locking around wantfields anymore.

This ensures that we project fields properly and removes racy behavior
from the limit logic as well by moving up the lock.

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
@dbussink dbussink merged commit b68b1ed into vitessio:main Nov 23, 2023
115 checks passed
@dbussink dbussink deleted the sync-criticalsection-streamexec branch November 23, 2023 11:14
vitess-bot pushed a commit that referenced this pull request Nov 23, 2023
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Co-authored-by: Dirkjan Bussink <d.bussink@gmail.com>
vitess-bot pushed a commit that referenced this pull request Nov 23, 2023
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Co-authored-by: Dirkjan Bussink <d.bussink@gmail.com>
harshit-gangal added a commit that referenced this pull request Nov 23, 2023
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Co-authored-by: Dirkjan Bussink <d.bussink@gmail.com>
harshit-gangal added a commit that referenced this pull request Nov 23, 2023
…14586) (#14591)

Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Co-authored-by: Dirkjan Bussink <d.bussink@gmail.com>
Co-authored-by: Harshit Gangal <harshit@planetscale.com>
harshit-gangal added a commit that referenced this pull request Nov 23, 2023
…14586) (#14592)

Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Co-authored-by: Dirkjan Bussink <d.bussink@gmail.com>
Co-authored-by: Harshit Gangal <harshit@planetscale.com>
dbussink added a commit that referenced this pull request Nov 23, 2023
…14586) (#14590)

Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Co-authored-by: Harshit Gangal <harshit@planetscale.com>
Co-authored-by: Dirkjan Bussink <d.bussink@gmail.com>
ejortegau pushed a commit to slackhq/vitess that referenced this pull request Dec 13, 2023
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
Co-authored-by: Dirkjan Bussink <d.bussink@gmail.com>
@hmaurer hmaurer mentioned this pull request Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backport to: release-17.0 Needs to be back ported to release-17.0 Backport to: release-18.0 Needs to be back ported to release-18.0 Component: Query Serving Type: Bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants