-
Notifications
You must be signed in to change notification settings - Fork 381
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
Query information_schema compatible with MySQL 8 #2652
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ import ( | |
"context" | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/google/trillian/quota" | ||
) | ||
|
@@ -97,6 +99,10 @@ func (m *QuotaManager) countUnsequenced(ctx context.Context) (int, error) { | |
} | ||
|
||
func countFromInformationSchema(ctx context.Context, db *sql.DB) (int, error) { | ||
// turn off statistics caching for MySQL 8 | ||
if err := turnOffInformationSchemaCache(ctx, db); err != nil { | ||
return 0, err | ||
} | ||
// information_schema.tables doesn't have an explicit PK, so let's play it safe and ensure | ||
// the cursor returns a single row. | ||
rows, err := db.QueryContext(ctx, countFromInformationSchemaQuery, "Unsequenced", "BASE TABLE") | ||
|
@@ -124,3 +130,29 @@ func countFromTable(ctx context.Context, db *sql.DB) (int, error) { | |
} | ||
return count, nil | ||
} | ||
|
||
// turnOffInformationSchemaCache turn off statistics caching for MySQL 8 | ||
// To always retrieve the latest statistics directly from the storage engine and bypass cached values, set information_schema_stats_expiry to 0. | ||
// See https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_information_schema_stats_expiry | ||
func turnOffInformationSchemaCache(ctx context.Context, db *sql.DB) error { | ||
opt := "information_schema_stats_expiry" | ||
res := db.QueryRowContext(ctx, "SHOW VARIABLES LIKE '%"+opt+"%'") | ||
var none, expiry string | ||
err := res.Scan(&none, &expiry) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Can you inline this with the check on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks very much for your reminding. Indeed, “expiry” should be scanned into When this variable does not exist (versions before MySQL 8.0), |
||
if err != nil { | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Failed to get variable %q: %v", opt, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: "Failed" -> "failed" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
} | ||
|
||
exp, err := strconv.Atoi(expiry) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can inline here too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed |
||
if err != nil || exp != 0 { | ||
if _, err := db.ExecContext(ctx, "SET SESSION "+opt+"=0"); err != nil { | ||
return fmt.Errorf("Failed to set variable %q: %v", opt, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: "Failed" -> "failed" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
} | ||
} | ||
|
||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this fail safely for all versions of MySQL prior to 8 or should we check the version and only execute on version 8+?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it will fail safely for versions before MySQL 8.0. This is because this function uses
SHOW VARIABLES LIKE
statement. Executing this statement in versions before MySQL 8 is equivalent to searching a system variable that does not exist, and it will only return empty result rather than error.Furthermore, tests of 5.6.51, 5.7.36 and 8.0.27 have already passed locally.