Skip to content

Commit

Permalink
*: implement "kill tidb xxx" statement (#2768)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored Mar 8, 2017
1 parent abe4413 commit a623e67
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
8 changes: 7 additions & 1 deletion executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,13 @@ func (e *SimpleExec) executeSetPwd(s *ast.SetPwdStmt) error {
}

func (e *SimpleExec) executeKillStmt(s *ast.KillStmt) error {
// TODO: Implement it.
if s.TiDBExtension {
sm := e.ctx.GetSessionManager()
if sm == nil {
return nil
}
sm.Kill(s.ConnectionID, s.Query)
}
return nil
}

Expand Down
1 change: 1 addition & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,7 @@ func (s *testParserSuite) TestSessionManage(c *C) {
{"kill tidb 23123", true},
{"kill tidb connection 23123", true},
{"kill tidb query 23123", true},
{"show processlist", true},
}
s.RunTest(c, table)
}
Expand Down
3 changes: 2 additions & 1 deletion server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type clientConn struct {
lastCmd string // latest sql query string, currently used for logging error.
ctx QueryCtx // an interface to execute sql statements.
attrs map[string]string // attributes parsed from client handshake response, not used for now.
killed bool
}

func (cc *clientConn) String() string {
Expand Down Expand Up @@ -336,7 +337,7 @@ func (cc *clientConn) Run() {
cc.Close()
}()

for {
for !cc.killed {
cc.alloc.Reset()
data, err := cc.readPacket()
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions server/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ type QueryCtx interface {
ShowProcess() util.ProcessInfo

SetSessionManager(util.SessionManager)

// Cancel the execution of current transaction.
Cancel()
}

// PreparedStatement is the interface to use a prepared statement.
Expand Down
5 changes: 5 additions & 0 deletions server/driver_tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ func (tc *TiDBContext) ShowProcess() util.ProcessInfo {
return tc.session.ShowProcess()
}

// Cancel implements QueryCtx Cancel method.
func (tc *TiDBContext) Cancel() {
tc.session.Cancel()
}

type tidbResultSet struct {
recordSet ast.RecordSet
}
Expand Down
12 changes: 12 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ func (s *Server) ShowProcessList() []util.ProcessInfo {

// Kill implements the SessionManager interface.
func (s *Server) Kill(connectionID uint64, query bool) {
s.rwlock.Lock()
defer s.rwlock.Unlock()

conn, ok := s.clients[uint32(connectionID)]
if !ok {
return
}

conn.ctx.Cancel()
if !query {
conn.killed = true
}
}

var once sync.Once
Expand Down

0 comments on commit a623e67

Please sign in to comment.