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: use RLock in connExecutor.CancelQuery and connExecutor.CancelActiveQueries #96241

Merged
merged 1 commit into from
Feb 2, 2023
Merged

sql: use RLock in connExecutor.CancelQuery and connExecutor.CancelActiveQueries #96241

merged 1 commit into from
Feb 2, 2023

Conversation

ecwall
Copy link
Contributor

@ecwall ecwall commented Jan 30, 2023

Fixes #95994

connExecutor.CancelQuery and connExecutor.CancelActiveQueries do not modify mu.ActiveQueries or the *queryMetas inside so they can safely use RLock instead of Lock.

Release note: None

@ecwall ecwall requested a review from yuzefovich January 30, 2023 22:01
@ecwall ecwall requested a review from a team as a code owner January 30, 2023 22:01
@blathers-crl
Copy link

blathers-crl bot commented Jan 30, 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.

@ecwall ecwall requested a review from rafiss January 30, 2023 22:01
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@@ -2034,12 +2034,6 @@ type queryMeta struct {
database string
}

// cancel cancels the query associated with this queryMeta, by closing the
// associated stmt context.
func (q *queryMeta) cancel() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method just calls a similarly named function field and seems like an unnecessary layer.

Copy link
Collaborator

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


-- commits line 7 at r1:
just curious: then what is responsible for removing the query from the ActiveQueries list?


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

// BaseSessionUser is part of the RegistrySession interface.
func (ex *connExecutor) BaseSessionUser() username.SQLUsername {

nit: the word Base is redundant in the function name. the session user is immutable, and always is the user who originally logged in. (you could also replace ex.sessionDataStack.Base().SessionUser() below with ex.sessionDataStack.SessionUser())

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 @rafiss and @yuzefovich)


-- commits line 7 at r1:

Previously, rafiss (Rafi Shamim) wrote…

just curious: then what is responsible for removing the query from the ActiveQueries list?

It happens here in a defer after it is added to ActiveQueries:

ex.removeActiveQuery(queryID, ast)


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

Previously, rafiss (Rafi Shamim) wrote…

nit: the word Base is redundant in the function name. the session user is immutable, and always is the user who originally logged in. (you could also replace ex.sessionDataStack.Base().SessionUser() below with ex.sessionDataStack.SessionUser())

I renamed BaseSessionUser() to SessionUser(), but Stack does not have a SessionUser() method.

Are you saying that

func (s *SessionData) SessionUser() username.SQLUsername {
	if s.SessionUserProto == "" {
		return s.User()
	}
	return s.SessionUserProto.Decode()
}

should actually be a method on Stack?

func (s *Stack) SessionUser() username.SQLUsername {
	session := s.base
	if session.SessionUserProto == "" {
		return session.User()
	}
	return session.SessionUserProto.Decode()
}

Copy link
Collaborator

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


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

Previously, ecwall (Evan Wall) wrote…

I renamed BaseSessionUser() to SessionUser(), but Stack does not have a SessionUser() method.

Are you saying that

func (s *SessionData) SessionUser() username.SQLUsername {
	if s.SessionUserProto == "" {
		return s.User()
	}
	return s.SessionUserProto.Decode()
}

should actually be a method on Stack?

func (s *Stack) SessionUser() username.SQLUsername {
	session := s.base
	if session.SessionUserProto == "" {
		return session.User()
	}
	return session.SessionUserProto.Decode()
}

no, i mistyped. i meant to say that you can use ex.sessionData().SessionUser(). analogous to the user() function you deleted above.

@ecwall ecwall requested a review from a team February 1, 2023 18:48
@ecwall ecwall requested a review from a team as a code owner February 1, 2023 18:48
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, 1 of 1 files at r3, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @ecwall and @rafiss)


-- commits line 7 at r1:

Previously, ecwall (Evan Wall) wrote…

It happens here in a defer after it is added to ActiveQueries:

ex.removeActiveQuery(queryID, ast)

When I was reviewing #95745, I was also confused by this. Consider adding a quick comment to CancelQuery and CancelActiveQueries that we can use read-lock there because the actual removal of the query meta from the map occurs in removeActiveQuery.

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

lgtm with one nit, and i also agree with yahor's sugegstion to comment this

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @ecwall)


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

Previously, rafiss (Rafi Shamim) wrote…

no, i mistyped. i meant to say that you can use ex.sessionData().SessionUser(). analogous to the user() function you deleted above.

nit: i think it would be more consistent with other usages of it were changed

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! 1 of 0 LGTMs obtained (waiting on @rafiss)


-- commits line 7 at r1:

Previously, yuzefovich (Yahor Yuzefovich) wrote…

When I was reviewing #95745, I was also confused by this. Consider adding a quick comment to CancelQuery and CancelActiveQueries that we can use read-lock there because the actual removal of the query meta from the map occurs in removeActiveQuery.

Added


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

Previously, rafiss (Rafi Shamim) wrote…

nit: i think it would be more consistent with other usages of it were changed

That is using the SessionData from the top of the stack instead of the bottom like before.

Copy link
Collaborator

@rafiss rafiss 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! 1 of 0 LGTMs obtained (waiting on @ecwall)


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

Previously, ecwall (Evan Wall) wrote…

That is using the SessionData from the top of the stack instead of the bottom like before.

as i mentioned above, SessionUser is immutable. see

// SessionUser retrieves the session_user.
// The SessionUser is the username that originally logged into the session.
// If a user applies SET ROLE, the SessionUser remains the same whilst the
// User() changes.

Copy link
Collaborator

@rafiss rafiss 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! 1 of 0 LGTMs obtained (waiting on @ecwall)


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

Previously, rafiss (Rafi Shamim) wrote…

as i mentioned above, SessionUser is immutable. see

// SessionUser retrieves the session_user.
// The SessionUser is the username that originally logged into the session.
// If a user applies SET ROLE, the SessionUser remains the same whilst the
// User() changes.

using Base does work, but it's just confusing since it's inconsistent with all the other usages of SessionUser

…iveQueries

Fixes #95994

`connExecutor.CancelQuery` and `connExecutor.CancelActiveQueries` do not
modify `mu.ActiveQueries` or the `*queryMetas` inside so they can safely
use `RLock` instead of `Lock`.

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 (and 1 stale) (waiting on @rafiss and @yuzefovich)


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

Previously, rafiss (Rafi Shamim) wrote…

using Base does work, but it's just confusing since it's inconsistent with all the other usages of SessionUser

Talked about it in slack. Only the base stack element is thread safe so adding a comment to explain why we are using it instead of the more common ex.sessionData().

@ecwall
Copy link
Contributor Author

ecwall commented Feb 1, 2023

bors r=rafiss,yuzefovich

@craig
Copy link
Contributor

craig bot commented Feb 1, 2023

This PR was included in a batch that was canceled, it will be automatically retried

@craig
Copy link
Contributor

craig bot commented Feb 2, 2023

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Feb 2, 2023

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Feb 2, 2023

Build succeeded:

@craig craig bot merged commit 22244a7 into cockroachdb:master Feb 2, 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: use RLock in connExecutor.CancelQuery and connExecutor.CancelActiveQueries
4 participants