Skip to content

Commit

Permalink
do not use version to empty the cache
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay committed Aug 7, 2023
1 parent 01195ce commit 6710c49
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 32 deletions.
1 change: 1 addition & 0 deletions go/vt/servenv/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func MySQLServerVersion() string {
// flag. It is intended for use in tests that require a specific MySQL server
// version (for example, collations) that cannot specify that via the command
// line.
// Please note that semantics.ClearCachedInfoSchemaInfo() should be called any time this method is called
func SetMySQLServerVersionForTest(version string) {
mySQLServerVersion = version
}
Expand Down
12 changes: 6 additions & 6 deletions go/vt/vtgate/engine/memory_sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ import (
"context"
"testing"

"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/vtgate/evalengine"

"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/test/utils"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/test/utils"
querypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/vtgate/evalengine"
"vitess.io/vitess/go/vt/vtgate/semantics"
)

func init() {
// We require MySQL 8.0 collations for the comparisons in the tests
mySQLVersion := "8.0.0"
servenv.SetMySQLServerVersionForTest(mySQLVersion)
semantics.ClearCachedInfoSchemaInfo()
collationEnv = collations.NewEnvironment(mySQLVersion)
}

Expand Down
3 changes: 2 additions & 1 deletion go/vt/vtgate/engine/ordered_aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/test/utils"

binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
querypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/servenv"
. "vitess.io/vitess/go/vt/vtgate/engine/opcode"
"vitess.io/vitess/go/vt/vtgate/semantics"
)

var collationEnv *collations.Environment
Expand All @@ -41,6 +41,7 @@ func init() {
// We require MySQL 8.0 collations for the comparisons in the tests
mySQLVersion := "8.0.0"
servenv.SetMySQLServerVersionForTest(mySQLVersion)
semantics.ClearCachedInfoSchemaInfo()
collationEnv = collations.NewEnvironment(mySQLVersion)
}

Expand Down
17 changes: 7 additions & 10 deletions go/vt/vtgate/engine/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,18 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"vitess.io/vitess/go/mysql/sqlerror"
"github.com/stretchr/testify/require"

"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/mysql/sqlerror"
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/vterrors"

"vitess.io/vitess/go/vt/sqlparser"

"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/evalengine"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/vtgate/semantics"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)

Expand All @@ -55,6 +51,7 @@ func init() {
// We require MySQL 8.0 collations for the comparisons in the tests
mySQLVersion := "8.0.0"
servenv.SetMySQLServerVersionForTest(mySQLVersion)
semantics.ClearCachedInfoSchemaInfo()
collationEnv = collations.NewEnvironment(mySQLVersion)
}

Expand Down
3 changes: 3 additions & 0 deletions go/vt/vtgate/evalengine/api_compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"testing"
"time"

"vitess.io/vitess/go/vt/vtgate/semantics"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/mysql/collations"
Expand Down Expand Up @@ -56,6 +58,7 @@ func init() {
// We require MySQL 8.0 collations for the comparisons in the tests
mySQLVersion := "8.0.0"
servenv.SetMySQLServerVersionForTest(mySQLVersion)
semantics.ClearCachedInfoSchemaInfo()
collationEnv = collations.NewEnvironment(mySQLVersion)
}

Expand Down
3 changes: 3 additions & 0 deletions go/vt/vtgate/evalengine/integration/comparison_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"testing"
"time"

"vitess.io/vitess/go/vt/vtgate/semantics"

"github.com/spf13/pflag"

"vitess.io/vitess/go/mysql/format"
Expand Down Expand Up @@ -256,6 +258,7 @@ func TestMySQL(t *testing.T) {
// We require MySQL 8.0 collations for the comparisons in the tests

servenv.SetMySQLServerVersionForTest(conn.ServerVersion)
semantics.ClearCachedInfoSchemaInfo()
collationEnv = collations.NewEnvironment(conn.ServerVersion)
servenv.OnParse(registerFlags)
initTimezoneData(t, conn)
Expand Down
14 changes: 12 additions & 2 deletions go/vt/vtgate/planbuilder/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,13 @@ func TestPlan(t *testing.T) {

func TestSystemTables57(t *testing.T) {
// first we move everything to use 5.7 logic
oldVer := servenv.MySQLServerVersion()
servenv.SetMySQLServerVersionForTest("5.7")
defer servenv.SetMySQLServerVersionForTest("")
semantics.ClearCachedInfoSchemaInfo()
defer func() {
servenv.SetMySQLServerVersionForTest(oldVer)
semantics.ClearCachedInfoSchemaInfo()
}()
vschemaWrapper := &vschemaWrapper{v: loadSchema(t, "vschemas/schema.json", true)}
testOutputTempDir := makeTestOutput(t)
testFile(t, "info_schema57_cases.json", testOutputTempDir, vschemaWrapper, false)
Expand Down Expand Up @@ -193,8 +198,13 @@ func TestOneWithTPCHVSchema(t *testing.T) {

func TestOneWith57Version(t *testing.T) {
// first we move everything to use 5.7 logic
oldVer := servenv.MySQLServerVersion()
servenv.SetMySQLServerVersionForTest("5.7")
defer servenv.SetMySQLServerVersionForTest("")
semantics.ClearCachedInfoSchemaInfo()
defer func() {
servenv.SetMySQLServerVersionForTest(oldVer)
semantics.ClearCachedInfoSchemaInfo()
}()
vschema := &vschemaWrapper{v: loadSchema(t, "vschemas/schema.json", true)}

testFile(t, "onecase.json", "", vschema, false)
Expand Down
31 changes: 18 additions & 13 deletions go/vt/vtgate/semantics/info_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1671,27 +1671,32 @@ type infoSchemaWithColumns struct {
infoSchemaData map[string][]vindexes.Column
}

// We cache this information. If the registered mysql version is changed, we flush the cache
// We cache this information. If the version is changed in a test, this field also needs to be cleared
var cacheSchemaInfo map[string][]vindexes.Column
var cachedVersion string

// newSchemaInfo returns a SchemaInformation that has the column information for all info_schema tables
func newSchemaInfo(inner SchemaInformation) SchemaInformation {
version := servenv.MySQLServerVersion()

if version != cachedVersion || cacheSchemaInfo == nil {
// we don't have a valid cached info_schema map with column information
if strings.HasPrefix(version, "5.7") {
cacheSchemaInfo = getInfoSchema57()
} else {
cacheSchemaInfo = getInfoSchema80()
}
cachedVersion = version
if cacheSchemaInfo == nil {
// yes, this is slightly racy, but the worst that can happen is that we build it more times
// than needed. It's not going to lead to any actual issues
loadSchemaInfo()
}

return &infoSchemaWithColumns{inner: inner, infoSchemaData: cacheSchemaInfo}
}

func ClearCachedInfoSchemaInfo() {
cacheSchemaInfo = nil
}

func loadSchemaInfo() {
version := servenv.MySQLServerVersion()
if strings.HasPrefix(version, "5.7") {
cacheSchemaInfo = getInfoSchema57()
} else {
cacheSchemaInfo = getInfoSchema80()
}
}

// FindTableOrVindex implements the SchemaInformation interface
func (i *infoSchemaWithColumns) FindTableOrVindex(tbl sqlparser.TableName) (*vindexes.Table, vindexes.Vindex, string, topodatapb.TabletType, key.Destination, error) {
if !strings.EqualFold(tbl.Qualifier.String(), "information_schema") {
Expand Down

0 comments on commit 6710c49

Please sign in to comment.