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) #33076

Closed

Conversation

ti-srebot
Copy link
Contributor

@ti-srebot ti-srebot commented Mar 15, 2022

cherry-pick #32809 to release-5.2
You can switch your code base to this Pull Request by using git-extras:

# In tidb repo:
git pr https://github.com/pingcap/tidb/pull/33076

After apply modifications, you can push your change to this PR via:

git push git@github.com:ti-srebot/tidb.git pr/33076:release-5.2-403dcfd32d84

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

Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-chi-bot
Copy link
Member

[REVIEW NOTIFICATION]

This pull request has not been approved.

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
Copy link
Member

@ti-srebot: This cherry pick PR is for a release branch and has not yet been approved by release team.
Adding the do-not-merge/cherry-pick-not-approved label.

To merge this cherry pick, it must first be approved by the collaborators.

AFTER it has been approved by collaborators, please ping the release team in a comment to request a cherry pick review.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@ti-chi-bot ti-chi-bot added the do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. label Mar 15, 2022
@ti-srebot
Copy link
Contributor Author

/run-all-tests

@ti-chi-bot ti-chi-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Mar 15, 2022
@ti-srebot ti-srebot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. type/5.2-cherry-pick labels Mar 15, 2022
@ti-srebot
Copy link
Contributor Author

@bb7133 you're already a collaborator in bot's repo.

@bb7133 bb7133 closed this Sep 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-merge/cherry-pick-not-approved 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/5.2-cherry-pick
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants