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

executor: kill tidb [session id] can't stop executors and release resources quickly #9844

Merged
merged 16 commits into from
Apr 1, 2019
Prev Previous commit
Next Next commit
address comments
  • Loading branch information
qw4990 committed Mar 29, 2019
commit d51bbc3980235e0c5a3de8fc2e74bf30e776a3b7
6 changes: 6 additions & 0 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 1050,12 @@ func (cc *clientConn) handleQuery(ctx context.Context, sql string) (err error) {
}
cc.mu.Lock()
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
cc.mu.resultSets = rs
status := atomic.LoadInt32(&cc.status)
if status == connStatusShutdown || status == connStatusWaitShutdown {
cc.mu.Unlock()
killConn(cc)
return errors.New("killed by another connection")
}
cc.mu.Unlock()
if rs != nil {
if len(rs) == 1 {
Expand Down
6 changes: 3 additions & 3 deletions server/driver_tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 17,7 @@ import (
"context"
"crypto/tls"
"fmt"
"sync/atomic"
"time"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -354,7 355,7 @@ type tidbResultSet struct {
recordSet sqlexec.RecordSet
columns []*ColumnInfo
rows []chunk.Row
closed bool
closed int32
}

func (trs *tidbResultSet) NewRecordBatch() *chunk.RecordBatch {
Expand All @@ -377,10 378,9 @@ func (trs *tidbResultSet) GetFetchedRows() []chunk.Row {
}

func (trs *tidbResultSet) Close() error {
if trs.closed {
if !atomic.CompareAndSwapInt32(&trs.closed, 0, 1) {
return nil
}
trs.closed = true
return trs.recordSet.Close()
}

Expand Down
7 changes: 4 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,14 518,15 @@ func (s *Server) Kill(connectionID uint64, query bool) {

func killConn(conn *clientConn) {
conn.mu.RLock()
for _, resultSet := range conn.mu.resultSets {
resultSets := conn.mu.resultSets
cancelFunc := conn.mu.cancelFunc
conn.mu.RUnlock()
for _, resultSet := range resultSets {
// resultSet.Close() is reentrant so it's safe to kill a same connID multiple times
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
if err := resultSet.Close(); err != nil {
logutil.Logger(context.Background()).Error("close result set error", zap.Uint32("connID", conn.connectionID), zap.Error(err))
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
}
}
cancelFunc := conn.mu.cancelFunc
conn.mu.RUnlock()
if cancelFunc != nil {
cancelFunc()
}
Expand Down