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, sessionctx: improved mysql compatibility with support for init_connect #23713

Merged
merged 23 commits into from
Apr 2, 2021

Conversation

morgo
Copy link
Contributor

@morgo morgo commented Mar 30, 2021

What problem does this PR solve?

Issue Number: close #18894

Problem Summary:

MySQL supports the ability to execute a set of statements on first connection. This PR adds the same feature to TiDB.

It can be useful for some applications, since SQL modes can be set, user variables, etc. It is also used extensively in the mysql-test testsuite.

What is changed and how it works?

Proposal: xxx

What's Changed:

How it Works:

Related changes

  • PR to update pingcap/docs/pingcap/docs-cn:
  • Need to cherry-pick to the release branch

Check List

Tests

  • Integration test: server integration tests.

Side effects

  • Performance regression
    • Regrettably checking if initconnect returned an error is in the query-serving path. This was required to return the client an error.

Release note

  • TiDB now supports the mysql system variable init_connect and associated functionality.

@ti-chi-bot ti-chi-bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Mar 30, 2021
@github-actions github-actions bot added the sig/sql-infra SIG: SQL Infra label Mar 30, 2021
@xhebox
Copy link
Contributor

xhebox commented Mar 31, 2021

/cc @AilinKid @xhebox

@ti-chi-bot ti-chi-bot requested review from AilinKid and xhebox March 31, 2021 05:47
Copy link
Contributor

@xhebox xhebox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest LGTM

server/conn.go Outdated
@@ -766,6 +826,14 @@ func (cc *clientConn) Run(ctx context.Context) {
cc.pkt.setReadTimeout(time.Duration(waitTimeout) * time.Second)
start := time.Now()
data, err := cc.readPacket()
if initConnectFailed {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the error is not returned immediately? Because of some dependencies on cc variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am trying to match the semantic of when MySQL disconnects the client - but actually I just checked and it's not quite correct. See TiDB:

mysql> select 1;
ERROR 2013 (HY000): Lost connection to MySQL server during query

vs MySQL:

mysql [localhost:8023] {u1} ((none)) > select 1;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    48
Current database: *** NONE ***

ERROR 1184 (08S01): Aborted connection 48 to db: 'unconnected' user: 'u1' host: 'localhost' (init_connect command failed)

I might have misunderstood it, and it might occur in the initial handshake.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed it to the handshake stage. It is not quite identical to MySQL because MySQL seems to allow the client to initially connect based on capabilities, and I'm not sure we have all the logic to do that yet. So now it is:

$ mysql -e "create user u1;set global init_connect='fail';"; mysql -uu1
ERROR 1184 (08S01): Aborted connection 5 to db: 'unconnected' user: 'u1' host: '127.0.0.1' (init_connect command failed)

@AilinKid
Copy link
Contributor

AilinKid commented Apr 1, 2021

/lgtm

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Apr 1, 2021
Copy link
Contributor

@xhebox xhebox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ti-chi-bot
Copy link
Member

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • AilinKid
  • 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 writing /lgtm in a comment.
Reviewer can cancel approval by writing /lgtm cancel in a comment.

@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Apr 2, 2021
@ti-chi-bot ti-chi-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 2, 2021
@ti-chi-bot ti-chi-bot removed the status/can-merge Indicates a PR has been approved by a committer. label Apr 2, 2021
@morgo
Copy link
Contributor Author

morgo commented Apr 2, 2021

/merge

@ti-chi-bot
Copy link
Member

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

Commit hash: dbc8ce0

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Apr 2, 2021
@morgo
Copy link
Contributor Author

morgo commented Apr 2, 2021

/run-unit-test

@morgo
Copy link
Contributor Author

morgo commented Apr 2, 2021

/run-unit-test

@ti-chi-bot ti-chi-bot merged commit 9c75cfa into pingcap:master Apr 2, 2021
@lzmhhh123
Copy link
Contributor

/run-cherry-picker

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Jul 8, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-4.0 in PR #26031

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Jul 8, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-5.0 in PR #26072

@morgo morgo deleted the init-connect branch July 8, 2021 21:50
@morgo
Copy link
Contributor Author

morgo commented Jul 8, 2021

@lzmhhh123 be careful cherry picking to 4.0 but not the other branches in between (in this case 5.0). It leads to a bad user experience because the feature appears and then dissapears.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-cherry-pick-release-5.0 sig/sql-infra SIG: SQL Infra size/L Denotes a PR that changes 100-499 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support init_connect
6 participants