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

sql: remove redundant session iteration #95745

Merged
merged 1 commit into from
Jan 27, 2023
Merged

sql: remove redundant session iteration #95745

merged 1 commit into from
Jan 27, 2023

Conversation

ecwall
Copy link
Contributor

@ecwall ecwall commented Jan 24, 2023

Fixes #95743

Improves session/query cancelation with the following

  1. Replaces session scanning by session ID with map lookup.
  2. Replaces active query scanning by query ID with map lookup
    (session containing query to cancel is still scanned for).
  3. Does not serialize entire session to get session username or id.

Informs #77676

77676 was closed but some test cases incorrectly mentioned that addressing
77676 fixed them. This PR correctly fixes these test cases.

Release note: None

@blathers-crl
Copy link

blathers-crl bot commented Jan 24, 2023

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@ecwall ecwall marked this pull request as ready for review January 25, 2023 15:57
@ecwall ecwall requested a review from a team January 25, 2023 15:57
@ecwall ecwall requested a review from a team as a code owner January 25, 2023 15:57
@ecwall ecwall requested a review from a team January 25, 2023 15:57
@ecwall ecwall requested review from a team as code owners January 25, 2023 15:57
@ecwall ecwall requested review from rytaft and yuzefovich and removed request for rytaft January 25, 2023 15:57
Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

Nice! I have some comments and questions. It'd probably be beneficial for someone from @cockroachdb/sql-observability to take a look too.

Reviewed 5 of 5 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ecwall)


pkg/server/status.go line 293 at r1 (raw file):

	}

	hasAdmin, err := b.privilegeChecker.hasAdminRole(ctx, reqUsername)

nit: I think we can avoid this hasAdminRole call (which issues an internal SQL query under the hood) in cases when we just overwrote reqUsername to ctxUsername above. Not sure how frequent and important this is though.


pkg/server/status.go line 323 at r1 (raw file):

			return serverError(ctx, err)
		}
		if userName.Undefined() || userName == sessionUser {

Hm, this doesn't seem like an equivalent change.

Previously the logic was:

  • if reqUsername is defined
  • and reqUsername is different from sessionUsername
  • and ctx user is not an admin,
  • then we return errRequiresAdmin.

Now the logic is:

  • if reqUsername is defined
  • and ctx user is not an admin,
  • then we return errRequiresAdmin.

Am I missing something? Should the req user be always able to see and to cancel their own query (i.e. reqUsername == sessionUsername), regardless of the privilege?


pkg/sql/conn_executor.go line 3150 at r1 (raw file):

// CancelQuery is part of the RegistrySession interface.
func (ex *connExecutor) CancelQuery(queryID clusterunique.ID) bool {
	ex.mu.Lock()

nit: since in CancelQuery and CancelActiveQueries we don't delete the queries from the map and we don't modify queryMeta object, I think we can use read locks. This is orthogonal to your change, so maybe just leave a TODO to use read locks as much as possible.

Fixes #95743

Improves session/query cancelation with the following
1) Replaces session scanning by session ID with map lookup.
2) Replaces active query scanning by query ID with map lookup
   (session containing query to cancel is still scanned for).
3) Does not serialize entire session to get session username or id.

Informs #77676

77676 was closed but some test cases incorrectly mentioned that addressing
77676 fixed them. This PR correctly fixes these test cases.

Release note: None
Copy link
Contributor Author

@ecwall ecwall 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 @yuzefovich)


pkg/server/status.go line 293 at r1 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: I think we can avoid this hasAdminRole call (which issues an internal SQL query under the hood) in cases when we just overwrote reqUsername to ctxUsername above. Not sure how frequent and important this is though.

I'll look at cleaning this up further in another PR. I think it can also be skipped
if sessionUsername == reqUsername because the cancel query privileges are only checked if !hasAdmin && sessionUsername != reqUsername


pkg/server/status.go line 323 at r1 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

Hm, this doesn't seem like an equivalent change.

Previously the logic was:

  • if reqUsername is defined
  • and reqUsername is different from sessionUsername
  • and ctx user is not an admin,
  • then we return errRequiresAdmin.

Now the logic is:

  • if reqUsername is defined
  • and ctx user is not an admin,
  • then we return errRequiresAdmin.

Am I missing something? Should the req user be always able to see and to cancel their own query (i.e. reqUsername == sessionUsername), regardless of the privilege?

Yeah you are right, I changed this to reqUsername != ctxUsername && !isAdmin.


pkg/sql/conn_executor.go line 3150 at r1 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

nit: since in CancelQuery and CancelActiveQueries we don't delete the queries from the map and we don't modify queryMeta object, I think we can use read locks. This is orthogonal to your change, so maybe just leave a TODO to use read locks as much as possible.

I'll check this separately also.

@ecwall ecwall requested a review from yuzefovich January 26, 2023 14:16
Copy link
Contributor Author

@ecwall ecwall 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 @yuzefovich)


pkg/server/status.go line 293 at r1 (raw file):

Previously, ecwall (Evan Wall) wrote…

I'll look at cleaning this up further in another PR. I think it can also be skipped
if sessionUsername == reqUsername because the cancel query privileges are only checked if !hasAdmin && sessionUsername != reqUsername

#95993


pkg/sql/conn_executor.go line 3150 at r1 (raw file):

Previously, ecwall (Evan Wall) wrote…

I'll check this separately also.

#95994

Copy link
Member

@xinhaoz xinhaoz left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 2 of 5 files at r1, 1 of 2 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @yuzefovich)

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 2 of 2 files at r2, all commit messages.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @ecwall)

@ecwall
Copy link
Contributor Author

ecwall commented Jan 27, 2023

bors r=xinhaoz,yuzefovich

@craig
Copy link
Contributor

craig bot commented Jan 27, 2023

Build succeeded:

@craig craig bot merged commit 5f0c56e into cockroachdb:master Jan 27, 2023
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.

sql: remove redundant session iteration
4 participants