Skip to content

Commit

Permalink
Query information_schema compatible with MySQL 8 (#2652)
Browse files Browse the repository at this point in the history
* Query information_schema compatible with MySQL 8

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

* Optimize and normalize code

Standardize error messages and code style.
More rigorous SQL statements.

* Update CHANGELOG file
  • Loading branch information
px3303 authored Jan 13, 2022
1 parent a159c07 commit 43d5e38
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
`merkle` libraries in this repository. This is not a breaking change, but
we recommend clients also migrate over to this library at the earliest
convenient time; the long term plan is to remove `merkle` from this repo.
* `countFromInformationSchema` function to add support for MySQL 8.

## v1.4.0

Expand Down
33 changes: 33 additions & 0 deletions quota/mysqlqm/mysql_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"database/sql"
"errors"
"fmt"

"github.com/google/trillian/quota"
)
Expand Down Expand Up @@ -97,6 +98,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")
Expand Down Expand Up @@ -124,3 +129,31 @@ 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
// MySQL versions prior to 8 will fail safely.
func turnOffInformationSchemaCache(ctx context.Context, db *sql.DB) error {
opt := "information_schema_stats_expiry"
res := db.QueryRowContext(ctx, "SHOW VARIABLES LIKE '"+opt+"'")
var none string
var expiry int

if err := res.Scan(&none, &expiry); err != nil {
// fail safely for all versions of MySQL prior to 8
if errors.Is(err, sql.ErrNoRows) {
return nil
}

return fmt.Errorf("failed to get variable %q: %v", opt, err)
}

if expiry != 0 {
if _, err := db.ExecContext(ctx, "SET SESSION "+opt+"=0"); err != nil {
return fmt.Errorf("failed to set variable %q: %v", opt, err)
}
}

return nil
}

0 comments on commit 43d5e38

Please sign in to comment.