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

server: a better way to handle killed connection (#32809) #38888

Merged

Conversation

ti-chi-bot
Copy link
Member

This is an automated cherry-pick of #32809

What problem does this PR solve?

Issue Number: close #24031, this PR also reverts #29212.

Problem Summary:

What is changed and how it works?

The root cause of #24031 is that when a connection is idle, the goroutine is blocked at:

https://github.com/pingcap/tidb/blob/4a0d387e1ff1b508bbb60d484d97e4ac2a5ef2c7/server/conn.go#L1068

And the stack:

#	0x13fc361	bufio.(*Reader).Read+0x221					/home/bb7133/Softwares/go/src/bufio/bufio.go:227
#	0x34c8fba	github.com/pingcap/tidb/server.bufferedReadConn.Read+0x5a	/home/bb7133/Projects/gopath/src/github.com/pingcap/tidb/server/buffered_read_conn.go:31
#	0x1342886	io.ReadAtLeast+0x86						/home/bb7133/Softwares/go/src/io/io.go:328
#	0x34a96e4	io.ReadFull+0x84						/home/bb7133/Softwares/go/src/io/io.go:347
#	0x34a96ab	github.com/pingcap/tidb/server.(*packetIO).readOnePacket+0x4b	/home/bb7133/Projects/gopath/src/github.com/pingcap/tidb/server/packetio.go:86
#	0x34a9aee	github.com/pingcap/tidb/server.(*packetIO).readPacket+0x4e	/home/bb7133/Projects/gopath/src/github.com/pingcap/tidb/server/packetio.go:117
#	0x3479624	github.com/pingcap/tidb/server.(*clientConn).readPacket+0x1e4	/home/bb7133/Projects/gopath/src/github.com/pingcap/tidb/server/conn.go:397
#	0x34795ea	github.com/pingcap/tidb/server.(*clientConn).Run+0x1aa		/home/bb7133/Projects/gopath/src/github.com/pingcap/tidb/server/conn.go:1068
#	0x34b2d1d	github.com/pingcap/tidb/server.(*Server).onConn+0x12bd		/home/bb7133/Projects/gopath/src/github.com/pingcap/tidb/server/server.go:554

Because of that, the goroutine is not able to deal with the KILLED flag, release the resource it is holding and stop itself immediately.

In order to solve that, we need to make conn.Read() interruptable but there is no straightforward way in Go to do that. Some references:

  1. proposal: io/v2: add Context parameter to Reader, etc. golang/go#20280: a lot of discussions/arguments without a clear conclusion.
  2. Canceling I/O in Go Cap’n Proto: mentioned in go/issues/20280
  3. Simplify pipestream by correctly interrupting a read on context cancellation. google/mtail#497: a context-based implementation for canceling the Read()

For the approach introduced in 2 and 3, they are generally the same as this PR: setting SetReadDeadline in another goroutine. I cannot find any material describing if doing so is thread-safe, so it should be implementation-dependent and might not be safe, but it might not be a real problem considering we're about the kill the connection and the read buffer/status will be abandoned.

Alternatives

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Case1:

Session1> (in idle state, with PROCESS_ID=3)
Session2> KILL TIDB 3;
Session2> SHOW PROCESSLIST; (Can be confirmed that Session1 is killed)

Case2:

Session1> CREATE TABLE t1(a INT);
Session1> INSERT INTO t1 values (1);
Session1> BEGIN PESSIMISTIC;
Session1> SELECT * FROM t1 WHERE a=1 FOR UPDATE;
Session1> (in idle state, with PROCESS_ID=3)
Session2> BEGIN PESSIMISTIC;
Session2> SELECT * FROM t1 WHERE a=1 FOR UPDATE; (Session 2 is blocked and waiting for the lock)
Session3> KILL TIDB 3; (Can be confirmed that Session 1 is killed and Session2 is able to acquire the lock immediately).

Side effects

  • None

Release note

fix the issue that `KILL TIDB` doesn't take effect immediately on idle connections

@ti-chi-bot
Copy link
Member Author

ti-chi-bot commented Nov 4, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • Defined2014
  • xhebox

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. type/cherry-pick-for-release-5.1 do-not-merge/cherry-pick-not-approved labels Nov 4, 2022
@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Nov 4, 2022
@Defined2014 Defined2014 requested a review from xhebox November 4, 2022 06:39
@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Nov 4, 2022
@VelocityLight VelocityLight added cherry-pick-approved Cherry pick PR approved by release team. and removed do-not-merge/cherry-pick-not-approved labels Nov 4, 2022
@Defined2014
Copy link
Contributor

/merge

@ti-chi-bot
Copy link
Member Author

This pull request has been accepted and is ready to merge.

Commit hash: edce4f8

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Nov 4, 2022
@ti-chi-bot ti-chi-bot merged commit 5d411f5 into pingcap:release-5.1 Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherry-pick-approved Cherry pick PR approved by release team. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2. type/cherry-pick-for-release-5.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants