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

Addition of @@enable_system_settings to vtgate #7300

Merged
merged 10 commits into from
Jan 20, 2021
185 changes: 98 additions & 87 deletions go/vt/proto/vtgate/vtgate.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions go/vt/sqlparser/ast_rewriting.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func (er *expressionRewriter) sysVarRewrite(cursor *Cursor, node *ColName) {
sysvars.Workload.Name,
sysvars.DDLStrategy.Name,
sysvars.SessionUUID.Name,
sysvars.SessionEnableSystemSettings.Name,
sysvars.ReadAfterWriteGTID.Name,
sysvars.ReadAfterWriteTimeOut.Name,
sysvars.VitessVersion.Name,
Expand Down
7 changes: 6 additions & 1 deletion go/vt/sqlparser/ast_rewriting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
type myTestCase struct {
in, expected string
liid, db, foundRows, rowCount, rawGTID, rawTimeout, sessTrackGTID bool
ddlStrategy, sessionUUID bool
ddlStrategy, sessionUUID, sessionEnableSystemSettings bool
udv int
autocommit, clientFoundRows, skipQueryPlanCache bool
sqlSelectLimit, transactionMode, workload, vitessVersion bool
Expand All @@ -44,6 +44,10 @@ func TestRewrites(in *testing.T) {
in: "SELECT @@vitess_version",
expected: "SELECT :__vtvitess_version as `@@vitess_version`",
vitessVersion: true,
}, {
in: "SELECT @@enable_system_settings",
expected: "SELECT :__vtenable_system_settings as `@@enable_system_settings`",
sessionEnableSystemSettings: true,
}, {
in: "SELECT last_insert_id()",
expected: "SELECT :__lastInsertId as `last_insert_id()`",
Expand Down Expand Up @@ -199,6 +203,7 @@ func TestRewrites(in *testing.T) {
assert.Equal(tc.workload, result.NeedsSysVar(sysvars.Workload.Name), "should need :__vtworkload")
assert.Equal(tc.ddlStrategy, result.NeedsSysVar(sysvars.DDLStrategy.Name), "should need ddlStrategy")
assert.Equal(tc.sessionUUID, result.NeedsSysVar(sysvars.SessionUUID.Name), "should need sessionUUID")
assert.Equal(tc.sessionEnableSystemSettings, result.NeedsSysVar(sysvars.SessionEnableSystemSettings.Name), "should need sessionEnableSystemSettings")
assert.Equal(tc.rawGTID, result.NeedsSysVar(sysvars.ReadAfterWriteGTID.Name), "should need rawGTID")
assert.Equal(tc.rawTimeout, result.NeedsSysVar(sysvars.ReadAfterWriteTimeOut.Name), "should need rawTimeout")
assert.Equal(tc.sessTrackGTID, result.NeedsSysVar(sysvars.SessionTrackGTIDs.Name), "should need sessTrackGTID")
Expand Down
24 changes: 13 additions & 11 deletions go/vt/sysvars/sysvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,18 @@ var (
off = "0"
utf8 = "'utf8'"

Autocommit = SystemVariable{Name: "autocommit", IsBoolean: true, Default: on}
ClientFoundRows = SystemVariable{Name: "client_found_rows", IsBoolean: true, Default: off}
SkipQueryPlanCache = SystemVariable{Name: "skip_query_plan_cache", IsBoolean: true, Default: off}
TxReadOnly = SystemVariable{Name: "tx_read_only", IsBoolean: true, Default: off}
TransactionReadOnly = SystemVariable{Name: "transaction_read_only", IsBoolean: true, Default: off}
SQLSelectLimit = SystemVariable{Name: "sql_select_limit", Default: off}
TransactionMode = SystemVariable{Name: "transaction_mode", IdentifierAsString: true}
Workload = SystemVariable{Name: "workload", IdentifierAsString: true}
Charset = SystemVariable{Name: "charset", Default: utf8, IdentifierAsString: true}
Names = SystemVariable{Name: "names", Default: utf8, IdentifierAsString: true}
SessionUUID = SystemVariable{Name: "session_uuid", IdentifierAsString: true}
Autocommit = SystemVariable{Name: "autocommit", IsBoolean: true, Default: on}
ClientFoundRows = SystemVariable{Name: "client_found_rows", IsBoolean: true, Default: off}
SkipQueryPlanCache = SystemVariable{Name: "skip_query_plan_cache", IsBoolean: true, Default: off}
TxReadOnly = SystemVariable{Name: "tx_read_only", IsBoolean: true, Default: off}
TransactionReadOnly = SystemVariable{Name: "transaction_read_only", IsBoolean: true, Default: off}
SQLSelectLimit = SystemVariable{Name: "sql_select_limit", Default: off}
TransactionMode = SystemVariable{Name: "transaction_mode", IdentifierAsString: true}
Workload = SystemVariable{Name: "workload", IdentifierAsString: true}
Charset = SystemVariable{Name: "charset", Default: utf8, IdentifierAsString: true}
Names = SystemVariable{Name: "names", Default: utf8, IdentifierAsString: true}
SessionUUID = SystemVariable{Name: "session_uuid", IdentifierAsString: true}
SessionEnableSystemSettings = SystemVariable{Name: "enable_system_settings", IsBoolean: true, Default: on}
// Online DDL
DDLStrategy = SystemVariable{Name: "ddl_strategy", IdentifierAsString: true}
VitessVersion = SystemVariable{Name: "vitess_version", IdentifierAsString: true}
Expand All @@ -76,6 +77,7 @@ var (
Charset,
Names,
SessionUUID,
SessionEnableSystemSettings,
ReadAfterWriteGTID,
ReadAfterWriteTimeOut,
SessionTrackGTIDs,
Expand Down
8 changes: 8 additions & 0 deletions go/vt/vtgate/engine/fake_vcursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ func (t noopVCursor) SetReadAfterWriteGTID(s string) {
panic("implement me")
}

func (t noopVCursor) SetSessionEnableSystemSettings(allow bool) error {
panic("implement me")
}

func (t noopVCursor) GetSessionEnableSystemSettings() bool {
panic("implement me")
}

func (t noopVCursor) SetReadAfterWriteTimeout(f float64) {
panic("implement me")
}
Expand Down
3 changes: 3 additions & 0 deletions go/vt/vtgate/engine/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ type (

GetSessionUUID() string

SetSessionEnableSystemSettings(bool) error
GetSessionEnableSystemSettings() bool

// SetReadAfterWriteGTID sets the GTID that the user expects a replica to have caught up with before answering a query
SetReadAfterWriteGTID(string)
SetReadAfterWriteTimeout(float64)
Expand Down
2 changes: 2 additions & 0 deletions go/vt/vtgate/engine/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ func (svss *SysVarSetAware) Execute(vcursor VCursor, env evalengine.ExpressionEn
return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "invalid DDL strategy: %s", str)
}
vcursor.Session().SetDDLStrategy(str)
case sysvars.SessionEnableSystemSettings.Name:
err = svss.setBoolSysVar(env, vcursor.Session().SetSessionEnableSystemSettings)
case sysvars.Charset.Name, sysvars.Names.Name:
str, err := svss.evalAsString(env)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions go/vt/vtgate/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ func (e *Executor) addNeededBindVars(bindVarNeeds *sqlparser.BindVarNeeds, bindV
bindVars[key] = sqltypes.StringBindVariable(session.DDLStrategy)
case sysvars.SessionUUID.Name:
bindVars[key] = sqltypes.StringBindVariable(session.SessionUUID)
case sysvars.SessionEnableSystemSettings.Name:
bindVars[key] = sqltypes.BoolBindVariable(session.EnableSystemSettings)
case sysvars.ReadAfterWriteGTID.Name:
var v string
ifReadAfterWriteExist(session, func(raw *vtgatepb.ReadAfterWrite) {
Expand Down
101 changes: 67 additions & 34 deletions go/vt/vtgate/executor_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,24 @@ func TestExecutorSet(t *testing.T) {
}, {
in: "set session transaction read write",
out: &vtgatepb.Session{Autocommit: true},
}, {
in: "set @@enable_system_settings = on",
out: &vtgatepb.Session{Autocommit: true, EnableSystemSettings: true},
}, {
in: "set @@enable_system_settings = off",
out: &vtgatepb.Session{Autocommit: true, EnableSystemSettings: false},
}, {
in: "set @@enable_system_settings = 1",
out: &vtgatepb.Session{Autocommit: true, EnableSystemSettings: true},
}, {
in: "set @@enable_system_settings = 0",
out: &vtgatepb.Session{Autocommit: true, EnableSystemSettings: false},
}, {
in: "set @@enable_system_settings = true",
out: &vtgatepb.Session{Autocommit: true, EnableSystemSettings: true},
}, {
in: "set @@enable_system_settings = false",
out: &vtgatepb.Session{Autocommit: true, EnableSystemSettings: false},
}}
for i, tcase := range testcases {
t.Run(fmt.Sprintf("%d-%s", i, tcase.in), func(t *testing.T) {
Expand All @@ -247,76 +265,91 @@ func TestExecutorSetOp(t *testing.T) {
executor, _, _, sbclookup := createLegacyExecutorEnv()
*sysVarSetEnabled = true

sbclookup.SetResults([]*sqltypes.Result{
sqltypes.MakeTestResult(sqltypes.MakeTestFields("sql_mode", "varchar"), "STRICT_ALL_TABLES,NO_AUTO_UPDATES"),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("sql_safe_updates", "int64"), "1"),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("tx_isolation", "varchar"), "read-committed"),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("sql_quote_show_create", "int64"), "0"),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("foreign_key_checks", "int64")),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("unique_checks", "int64"), "0"),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("net_write_timeout", "int64"), "600"),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("net_read_timeout", "int64"), "300"),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("character_set_client", "varchar"), "utf8"),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("character_set_results", "varchar")),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("character_set_results", "varchar")),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("character_set_results", "varchar")),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("character_set_results", "varchar")),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("character_set_results", "varchar")),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("character_set_results", "varchar")),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("client_found_rows", "int64")),
sqltypes.MakeTestResult(sqltypes.MakeTestFields("client_found_rows", "int64")),
})
returnResult := func(columnName, typ, value string) *sqltypes.Result {
return sqltypes.MakeTestResult(sqltypes.MakeTestFields(columnName, typ), value)
}
returnNoResult := func(columnName, typ string) *sqltypes.Result {
return sqltypes.MakeTestResult(sqltypes.MakeTestFields(columnName, typ))
}

testcases := []struct {
in string
warning []*querypb.QueryWarning
sysVars map[string]string
in string
warning []*querypb.QueryWarning
sysVars map[string]string
disallowResConn bool
result *sqltypes.Result
}{{
in: "set big_tables = 1", //ignore
}, {
in: "set sql_mode = 'STRICT_ALL_TABLES,NO_AUTO_UPDATES'",
sysVars: map[string]string{"sql_mode": "'STRICT_ALL_TABLES,NO_AUTO_UPDATES'"},
result: returnResult("sql_mode", "varchar", "STRICT_ALL_TABLES,NO_AUTO_UPDATES"),
}, {
// even though the tablet is saying that the value has changed,
// useReservedConn is false, so we won't allow this change
in: "set sql_mode = 'STRICT_ALL_TABLES,NO_AUTO_UPDATES'",
result: returnResult("sql_mode", "varchar", "STRICT_ALL_TABLES,NO_AUTO_UPDATES"),
sysVars: nil,
disallowResConn: true,
}, {
in: "set sql_safe_updates = 1",
sysVars: map[string]string{"sql_safe_updates": "1"},
result: returnResult("sql_safe_updates", "int64", "1"),
}, {
in: "set tx_isolation = 'read-committed'",
sysVars: map[string]string{"tx_isolation": "'read-committed'"},
result: returnResult("tx_isolation", "varchar", "read-committed"),
}, {
in: "set sql_quote_show_create = 0",
sysVars: map[string]string{"sql_quote_show_create": "0"},
result: returnResult("sql_quote_show_create", "int64", "0"),
}, {
in: "set foreign_key_checks = 1",
in: "set foreign_key_checks = 1",
result: returnNoResult("foreign_key_checks", "int64"),
}, {
in: "set unique_checks = 0",
sysVars: map[string]string{"unique_checks": "0"},
result: returnResult("unique_checks", "int64", "0"),
}, {
in: "set net_write_timeout = 600",
in: "set net_write_timeout = 600",
result: returnResult("net_write_timeout", "int64", "600"),
}, {
in: "set net_read_timeout = 600",
in: "set net_read_timeout = 600",
result: returnResult("net_read_timeout", "int64", "300"),
}, {
in: "set character_set_client = utf8",
in: "set character_set_client = utf8",
result: returnResult("character_set_client", "varchar", "utf8"),
}, {
in: "set character_set_results=null",
in: "set character_set_results=null",
result: returnNoResult("character_set_results", "varchar"),
}, {
in: "set character_set_results='binary'",
in: "set character_set_results='binary'",
result: returnNoResult("character_set_results", "varchar"),
}, {
in: "set character_set_results='utf8'",
in: "set character_set_results='utf8'",
result: returnNoResult("character_set_results", "varchar"),
}, {
in: "set character_set_results=utf8mb4",
in: "set character_set_results=utf8mb4",
result: returnNoResult("character_set_results", "varchar"),
}, {
in: "set character_set_results='latin1'",
in: "set character_set_results='latin1'",
result: returnNoResult("character_set_results", "varchar"),
}, {
in: "set character_set_results='abcd'",
in: "set character_set_results='abcd'",
result: returnNoResult("character_set_results", "varchar"),
}, {
in: "set @@global.client_found_rows = 1",
in: "set @@global.client_found_rows = 1",
result: returnNoResult("client_found_rows", "int64"),
}, {
in: "set global client_found_rows = 1",
in: "set global client_found_rows = 1",
result: returnNoResult("client_found_rows", "int64"),
}}
for _, tcase := range testcases {
t.Run(tcase.in, func(t *testing.T) {
session := NewAutocommitSession(masterSession)
session.TargetString = KsTestUnsharded
session.EnableSystemSettings = !tcase.disallowResConn
sbclookup.SetResults([]*sqltypes.Result{tcase.result})
_, err := executor.Execute(
context.Background(),
"TestExecute",
Expand Down
7 changes: 4 additions & 3 deletions go/vt/vtgate/plugin_mysql_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,10 @@ func (vh *vtgateHandler) session(c *mysql.Conn) *vtgatepb.Session {
IncludedFields: querypb.ExecuteOptions_ALL,
Workload: querypb.ExecuteOptions_Workload(mysqlDefaultWorkload),
},
Autocommit: true,
DDLStrategy: *defaultDDLStrategy,
SessionUUID: u.String(),
Autocommit: true,
DDLStrategy: *defaultDDLStrategy,
SessionUUID: u.String(),
EnableSystemSettings: *sysVarSetEnabled,
}
if c.Capabilities&mysql.CapabilityClientFoundRows != 0 {
session.Options.ClientFoundRows = true
Expand Down
14 changes: 14 additions & 0 deletions go/vt/vtgate/safe_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,20 @@ func (session *SafeSession) GetSessionUUID() string {
return session.SessionUUID
}

// SetSessionEnableSystemSettings set the SessionEnableSystemSettings setting.
func (session *SafeSession) SetSessionEnableSystemSettings(allow bool) {
session.mu.Lock()
defer session.mu.Unlock()
session.EnableSystemSettings = allow
}

// GetSessionEnableSystemSettings returns the SessionEnableSystemSettings value.
func (session *SafeSession) GetSessionEnableSystemSettings() bool {
session.mu.Lock()
defer session.mu.Unlock()
return session.EnableSystemSettings
}

// SetReadAfterWriteGTID set the ReadAfterWriteGtid setting.
func (session *SafeSession) SetReadAfterWriteGTID(vtgtid string) {
session.mu.Lock()
Expand Down
13 changes: 12 additions & 1 deletion go/vt/vtgate/vcursor_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (vc *vcursorImpl) FirstSortedKeyspace() (*vindexes.Keyspace, error) {

// SysVarSetEnabled implements the ContextVSchema interface
func (vc *vcursorImpl) SysVarSetEnabled() bool {
return *sysVarSetEnabled
return vc.GetSessionEnableSystemSettings() && *sysVarSetEnabled
frouioui marked this conversation as resolved.
Show resolved Hide resolved
}

// KeyspaceExists provides whether the keyspace exists or not.
Expand Down Expand Up @@ -661,6 +661,17 @@ func (vc *vcursorImpl) GetSessionUUID() string {
return vc.safeSession.GetSessionUUID()
}

// SetSessionEnableSystemSettings implements the SessionActions interface
func (vc *vcursorImpl) SetSessionEnableSystemSettings(allow bool) error {
vc.safeSession.SetSessionEnableSystemSettings(allow)
return nil
}

// GetSessionEnableSystemSettings implements the SessionActions interface
func (vc *vcursorImpl) GetSessionEnableSystemSettings() bool {
return vc.safeSession.GetSessionEnableSystemSettings()
}

// SetReadAfterWriteGTID implements the SessionActions interface
func (vc *vcursorImpl) SetReadAfterWriteGTID(vtgtid string) {
vc.safeSession.SetReadAfterWriteGTID(vtgtid)
Expand Down
3 changes: 3 additions & 0 deletions proto/vtgate.proto
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ message Session {

// Session UUID
string SessionUUID = 22;

// enable_system_settings defines if we can use reserved connections.
bool enable_system_settings = 23;
}

// ReadAfterWrite contains information regarding gtid set and timeout
Expand Down