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

Merged
merged 1 commit into from
Sep 15, 2022

Conversation

ti-srebot
Copy link
Contributor

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

cherry-pick #32809 to release-5.4
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/37834

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

git push [email protected]:ti-srebot/tidb.git pr/37834:release-5.4-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

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Sep 15, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • hawkingrei
  • 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-srebot
Copy link
Contributor Author

/run-all-tests

@ti-chi-bot ti-chi-bot added do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. 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 Sep 15, 2022
@ti-srebot ti-srebot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. type/5.4-cherry-pick labels Sep 15, 2022
@ti-srebot ti-srebot added this to the v5.4.1 milestone Sep 15, 2022
@ti-srebot
Copy link
Contributor Author

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

@VelocityLight VelocityLight added cherry-pick-approved Cherry pick PR approved by release team. and removed do-not-merge/cherry-pick-not-approved labels Sep 15, 2022
@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Sep 15, 2022
@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 Sep 15, 2022
@bb7133
Copy link
Member

bb7133 commented Sep 15, 2022

/merge

@ti-chi-bot
Copy link
Member

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

Commit hash: 755d16e

@hawkingrei
Copy link
Member

/merge

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Sep 15, 2022
@ti-chi-bot ti-chi-bot merged commit cd5db42 into pingcap:release-5.4 Sep 15, 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/5.4-cherry-pick
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants