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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/server/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3056,7 +3056,7 @@ func (s *statusServer) CancelSession(
}, nil
}

if err := s.checkCancelPrivilege(ctx, reqUsername, session.BaseSessionUser()); err != nil {
if err := s.checkCancelPrivilege(ctx, reqUsername, session.SessionUser()); err != nil {
// NB: not using serverError() here since the priv checker
// already returns a proper gRPC error status.
return nil, err
Expand Down Expand Up @@ -3109,7 +3109,7 @@ func (s *statusServer) CancelQuery(
}, nil
}

if err := s.checkCancelPrivilege(ctx, reqUsername, session.BaseSessionUser()); err != nil {
if err := s.checkCancelPrivilege(ctx, reqUsername, session.SessionUser()); err != nil {
// NB: not using serverError() here since the priv checker
// already returns a proper gRPC error status.
return nil, err
Expand Down
27 changes: 14 additions & 13 deletions pkg/sql/conn_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3144,22 +3144,26 @@ func (ex *connExecutor) hasQuery(queryID clusterunique.ID) bool {

// CancelQuery is part of the RegistrySession interface.
func (ex *connExecutor) CancelQuery(queryID clusterunique.ID) bool {
ex.mu.Lock()
defer ex.mu.Unlock()
// RLock can be used because map deletion happens in
// connExecutor.removeActiveQuery.
ex.mu.RLock()
defer ex.mu.RUnlock()
if queryMeta, exists := ex.mu.ActiveQueries[queryID]; exists {
queryMeta.cancel()
queryMeta.cancelQuery()
return true
}
return false
}

// CancelActiveQueries is part of the RegistrySession interface.
func (ex *connExecutor) CancelActiveQueries() bool {
ex.mu.Lock()
defer ex.mu.Unlock()
// RLock can be used because map deletion happens in
// connExecutor.removeActiveQuery.
ex.mu.RLock()
defer ex.mu.RUnlock()
canceled := false
for _, queryMeta := range ex.mu.ActiveQueries {
queryMeta.cancel()
queryMeta.cancelQuery()
canceled = true
}
return canceled
Expand All @@ -3174,13 +3178,10 @@ func (ex *connExecutor) CancelSession() {
ex.onCancelSession()
}

// user is part of the RegistrySession interface.
func (ex *connExecutor) user() username.SQLUsername {
return ex.sessionData().User()
}

// BaseSessionUser is part of the RegistrySession interface.
func (ex *connExecutor) BaseSessionUser() username.SQLUsername {
// SessionUser is part of the RegistrySession interface.
func (ex *connExecutor) SessionUser() username.SQLUsername {
// SessionUser is the same for all elements in the stack so use Base()
// to avoid needing a lock and race conditions.
return ex.sessionDataStack.Base().SessionUser()
}

Expand Down
11 changes: 2 additions & 9 deletions pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

q.cancelQuery()
}

// SessionDefaults mirrors fields in Session, for restoring default
// configuration values in SET ... TO DEFAULT (or RESET ...) statements.
type SessionDefaults map[string]string
Expand Down Expand Up @@ -2138,9 +2132,8 @@ func (r *SessionRegistry) deregister(
}

type RegistrySession interface {
user() username.SQLUsername
// BaseSessionUser returns the base session's username.
BaseSessionUser() username.SQLUsername
// SessionUser returns the session user's username.
SessionUser() username.SQLUsername
hasQuery(queryID clusterunique.ID) bool
// CancelQuery cancels the query specified by queryID if it exists.
CancelQuery(queryID clusterunique.ID) bool
Expand Down