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: Support for the status command of MySQL Shell #22752

Merged
merged 6 commits into from
Feb 23, 2021

Conversation

dveeden
Copy link
Contributor

@dveeden dveeden commented Feb 6, 2021

What problem does this PR solve?

Fixes #8841

Problem Summary:

When running MySQL Shell (mysqlsh mysql://root:@127.0.0.1:4000 --sql) the \status (aka \s) doesn't work.

Example:

 MySQL  127.0.0.1:4000  SQL > \status
MySQL Shell version 8.0.23

get_int(6): field type is String

What is changed and how it works?

What's Changed:

The type of @@port was changed from a string type to an integer type.

This can be checked by running mysqlsh mysql://root:@127.0.0.1:4000 --sql --column-type-info and then running select @@port. The change is from VAR_STRING to LONGLONG.

This will also affect the type of other unsigned system variables.

In addition to this COM_STATISTICS has been implemented to return
Uptime: 0 Threads: 0 Questions: 0 Slow queries: 0 Opens: 0 Flush tables: 0 Open tables: 0 Queries per second avg: 0.000
Note that all values are always zero. Also note that mysqlsh parses out the integer for the Uptime.

This can also be checked with mysqladmin status -u root -P 4000 -h 127.0.0.1 -p.

How it Works:

  1. GetNativeValType now has a case or TypeUnsigned. This corrects the data type for variables with TypeUnsigned.
  2. dispatch now has a case for mysql.ComStatistics.
  3. The Port global variable now has Type: TypeUnsigned
  4. writeStats now has a very basic implementation of COM_STATISTICS

Check List

Tests

  • Manual test (add detailed scripts or steps below)

Side effects

Release note

  • Support for COM_STATISTICS was added
  • The type of unsigned global variables has been corrected

@ti-srebot ti-srebot added the first-time-contributor Indicates that the PR was contributed by an external member and is a first-time contributor. label Feb 6, 2021
@CLAassistant
Copy link

CLAassistant commented Feb 6, 2021

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added the sig/sql-infra SIG: SQL Infra label Feb 6, 2021
@dveeden
Copy link
Contributor Author

dveeden commented Feb 6, 2021

Looks like CI detected that values larger than LLONG_MAX (9223372036854775807) are not handled correctly. I'll have a look at this later.

@morgo morgo self-requested a review February 6, 2021 23:47
@@ -1066,6 +1068,19 @@ func (cc *clientConn) dispatch(ctx context.Context, data []byte) error {
}
}

func (cc *clientConn) writeStats(ctx context.Context) error {
msg := []byte("Uptime: 0 Threads: 0 Questions: 0 Slow queries: 0 Opens: 0 Flush tables: 0 Open tables: 0 Queries per second avg: 0.000")
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume clients will be okay if NULL is mentioned instead of some of the values here? I'm not worried about lying on Opens/Open tables, but it does seem possible that on QPS and Uptime it could cause problems.

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 could modify it like this:

$ git diff -- server/conn.go util/sys/linux/sys_linux.go
diff --git a/server/conn.go b/server/conn.go
index 698571dfd..0f1c50631 100644
--- a/server/conn.go
+++ b/server/conn.go
@@ -81,6 +81,7 @@ import (
        "github.com/pingcap/tidb/util/logutil"
        "github.com/pingcap/tidb/util/memory"
        "github.com/pingcap/tidb/util/sqlexec"
+       "github.com/pingcap/tidb/util/sys/linux"
        "github.com/prometheus/client_golang/prometheus"
        "go.uber.org/zap"
 )
@@ -1069,7 +1070,11 @@ func (cc *clientConn) dispatch(ctx context.Context, data []byte) error {
 }
 
 func (cc *clientConn) writeStats(ctx context.Context) error {
-       msg := []byte("Uptime: 0  Threads: 0  Questions: 0  Slow queries: 0  Opens: 0  Flush tables: 0  Open tables: 0  Queries per second avg: 0.000")
+       // Note that `mysqlsh` (MySQL Shell) parses out the `Uptime` when running `\status`.
+       // for this to work this needs to be an integer with one space before and two spaces
+       // after it.
+       stat := fmt.Sprintf("Uptime: %d  ", linux.Uptime())
+       msg := []byte(stat)
        data := cc.alloc.AllocWithLen(4, len(msg))
        data = append(data, msg...)
 
diff --git a/util/sys/linux/sys_linux.go b/util/sys/linux/sys_linux.go
index 976815956..f361eb3f9 100644
--- a/util/sys/linux/sys_linux.go
+++ b/util/sys/linux/sys_linux.go
@@ -52,3 +52,9 @@ func SetAffinity(cpus []int) error {
        }
        return unix.SchedSetaffinity(unix.Getpid(), &cpuSet)
 }
+
+func Uptime() int64 {
+       si := &unix.Sysinfo_t{}
+       unix.Sysinfo(si)
+       return si.Uptime
+}

This would return the system (not TiDB) uptime of the host running this process. This might not be the best thing, but it is more informative than always returning 0. Removing the other values seem to work just fine, at least for mysqlsh.

This seems to be slightly related to #8842 as that should probably return the same value for consistency.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I prefer this - but lets see what other reviewers prefer too. I commented in #8842 that it is possible to retrieve the correct uptime from prometheus, but because it is an optional component it might not be a good fit here.

sessionctx/variable/sysvar.go Outdated Show resolved Hide resolved
@morgo
Copy link
Contributor

morgo commented Feb 7, 2021

Looks like CI detected that values larger than LLONG_MAX (9223372036854775807) are not handled correctly. I'll have a look at this later.

This looks to be caused by this cast:
https://github.com/pingcap/tidb/blob/master/types/datum.go#L153

Internally i needs to be an int64, but I'm not sure why the precision is just lost here. I appologize in advance that reviews will be a little bit slow, as it's now Spring Festival in China :-) Another option you have is that you could set Port to TypeInt. This is not strictly MySQL compatible of course.

@dveeden dveeden requested review from a team as code owners February 8, 2021 17:26
@dveeden dveeden requested review from lzmhhh123 and removed request for a team February 8, 2021 17:26
@dveeden
Copy link
Contributor Author

dveeden commented Feb 8, 2021

Looks like CI detected that values larger than LLONG_MAX (9223372036854775807) are not handled correctly. I'll have a look at this later.

This looks to be caused by this cast:
https://github.com/pingcap/tidb/blob/master/types/datum.go#L153

Internally i needs to be an int64, but I'm not sure why the precision is just lost here. I appologize in advance that reviews will be a little bit slow, as it's now Spring Festival in China :-) Another option you have is that you could set Port to TypeInt. This is not strictly MySQL compatible of course.

It looks like the issue is that the mysql.UnsignedFlag is not set for this field in the response while it should. I've modified GetNativeValType to return this flag for TypeUnsigned. Then rewriteVariable passes this on to DatumToConstant which sets this on the Constant it returns.

@morgo
Copy link
Contributor

morgo commented Feb 20, 2021

LGTM

@ti-srebot ti-srebot added the status/LGT1 Indicates that a PR has LGTM 1. label Feb 20, 2021
@bb7133
Copy link
Member

bb7133 commented Feb 22, 2021

PTAL @jackysp , thanks!

Copy link
Member

@jackysp jackysp 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-srebot ti-srebot removed the status/LGT1 Indicates that a PR has LGTM 1. label Feb 22, 2021
@ti-srebot ti-srebot added the status/LGT2 Indicates that a PR has LGTM 2. label Feb 22, 2021
@jackysp
Copy link
Member

jackysp commented Feb 22, 2021

/merge

@ti-srebot ti-srebot added the status/can-merge Indicates a PR has been approved by a committer. label Feb 22, 2021
@ti-srebot
Copy link
Contributor

/run-all-tests

@bb7133 bb7133 removed the sig/infra label Feb 22, 2021
@ti-srebot
Copy link
Contributor

@dveeden merge failed.

@ti-chi-bot ti-chi-bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed status/can-merge Indicates a PR has been approved by a committer. labels Feb 22, 2021
@morgo
Copy link
Contributor

morgo commented Feb 22, 2021

/merge

@ti-chi-bot
Copy link
Member

@morgo: It seems you want to merge this PR, I will help you trigger all the tests:

/run-all-tests

You only need to trigger /merge once, and if the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

If you have any questions about the PR merge process, please refer to pr process.

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 ti-community-infra/tichi repository.

@ti-chi-bot
Copy link
Member

@morgo: /merge in this pull request requires 3 /lgtm.

In response to this:

/merge

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 ti-community-infra/tichi repository.

@zz-jason
Copy link
Member

zz-jason commented Feb 22, 2021

@morgo: /merge in this pull request requires 3 /lgtm.

In response to this:

/merge

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 ti-community-infra/tichi repository.

@hi-rustin I think we only need 2 LGTMs to merge this PR. Is it caused by the incorrect config?

@morgo
Copy link
Contributor

morgo commented Feb 23, 2021

/run-all-tests

@morgo
Copy link
Contributor

morgo commented Feb 23, 2021

/merge

@ti-chi-bot
Copy link
Member

@morgo: It seems you want to merge this PR, I will help you trigger all the tests:

/run-all-tests

You only need to trigger /merge once, and if the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

If you have any questions about the PR merge process, please refer to pr process.

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 ti-community-infra/tichi repository.

@ti-chi-bot
Copy link
Member

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

Commit hash: 6553213

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Feb 23, 2021
@ti-chi-bot
Copy link
Member

@dveeden: Your PR has out-of-dated, I have automatically updated it for you.

At the same time I will also trigger all tests for you:

/run-all-tests

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 ti-community-infra/tichi repository.

@ti-chi-bot ti-chi-bot merged commit 001b34e into pingcap:master Feb 23, 2021
@bb7133
Copy link
Member

bb7133 commented Feb 25, 2021

Thanks for your contribution! @dveeden

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression first-time-contributor Indicates that the PR was contributed by an external member and is a first-time contributor. sig/sql-infra SIG: SQL Infra size/M Denotes a PR that changes 30-99 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.

tidb does not implement protocol rpc comStatistics
8 participants