Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into add-bootstrap-sql
Browse files Browse the repository at this point in the history
* upstream/master:
  planner: make some `show stmt` more fine-grained privilege check (pingcap#35493)
  session: improve bootstrap code (pingcap#34755)
  *: upgrade go-proxyprotocol version (pingcap#35560)
  planner: introduce new cost formula for MPPAggs (pingcap#35436)
  • Loading branch information
morgo committed Jun 21, 2022
2 parents 0bb545e + ff1b6ff commit d5c463f
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 67 deletions.
4 changes: 2 additions & 2 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ def go_deps():
name = "com_github_blacktear23_go_proxyprotocol",
build_file_proto_mode = "disable_global",
importpath = "github.com/blacktear23/go-proxyprotocol",
sum = "h1:rQlvB2AYWme2bIB18r/SipGiMEVJYE9U0z+MGoU/LtQ=",
version = "v0.0.0-20180807104634-af7a81e8dd0d",
sum = "h1:WmMmtZanGEfIHnJN9N3A4Pl6mM69D+GxEph2eOaCf7g=",
version = "v1.0.0",
)
go_repository(
name = "com_github_burntsushi_toml",
Expand Down
26 changes: 19 additions & 7 deletions executor/showtest/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,20 +795,32 @@ func TestShowStatsPrivilege(t *testing.T) {
tk1 := testkit.NewTestKit(t, store)

require.True(t, tk1.Session().Auth(&auth.UserIdentity{Username: "show_stats", Hostname: "%"}, nil, nil))
e := "[planner:1142]SHOW command denied to user 'show_stats'@'%' for table"
err := tk1.ExecToErr("show stats_meta")
require.ErrorContains(t, err, e)
err = tk1.ExecToErr("SHOW STATS_BUCKETS")
require.ErrorContains(t, err, e)
err = tk1.ExecToErr("SHOW STATS_HISTOGRAMS")
require.ErrorContains(t, err, e)

eqErr := plannercore.ErrDBaccessDenied.GenWithStackByArgs("show_stats", "%", mysql.SystemDB)
_, err := tk1.Exec("show stats_meta")
require.EqualError(t, err, eqErr.Error())
_, err = tk1.Exec("SHOW STATS_BUCKETS")
require.EqualError(t, err, eqErr.Error())
_, err = tk1.Exec("SHOW STATS_HEALTHY")
require.EqualError(t, err, eqErr.Error())
_, err = tk1.Exec("SHOW STATS_HISTOGRAMS")
err = tk1.ExecToErr("SHOW STATS_HEALTHY")
require.EqualError(t, err, eqErr.Error())
tk.MustExec("grant select on mysql.* to show_stats")
tk1.MustExec("show stats_meta")
tk1.MustExec("SHOW STATS_BUCKETS")
tk1.MustExec("SHOW STATS_HEALTHY")
tk1.MustExec("SHOW STATS_HISTOGRAMS")

tk.MustExec("create user a@'%' identified by '';")
require.True(t, tk1.Session().Auth(&auth.UserIdentity{Username: "a", Hostname: "%"}, nil, nil))
tk.MustExec("grant select on mysql.stats_meta to a@'%';")
tk.MustExec("grant select on mysql.stats_buckets to a@'%';")
tk.MustExec("grant select on mysql.stats_histograms to a@'%';")
tk1.MustExec("show stats_meta")
tk1.MustExec("SHOW STATS_BUCKETS")
tk1.MustExec("SHOW STATS_HISTOGRAMS")

}

func TestIssue18878(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/Jeffail/gabs/v2 v2.5.1
github.com/Shopify/sarama v1.29.0
github.com/aws/aws-sdk-go v1.35.3
github.com/blacktear23/go-proxyprotocol v0.0.0-20180807104634-af7a81e8dd0d
github.com/blacktear23/go-proxyprotocol v1.0.0
github.com/carlmjohnson/flagext v0.21.0
github.com/cheggaaa/pb/v3 v3.0.8
github.com/cheynewallace/tabby v1.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/blacktear23/go-proxyprotocol v0.0.0-20180807104634-af7a81e8dd0d h1:rQlvB2AYWme2bIB18r/SipGiMEVJYE9U0z+MGoU/LtQ=
github.com/blacktear23/go-proxyprotocol v0.0.0-20180807104634-af7a81e8dd0d/go.mod h1:VKt7CNAQxpFpSDz3sXyj9hY/GbVsQCr0sB3w59nE7lU=
github.com/blacktear23/go-proxyprotocol v1.0.0 h1:WmMmtZanGEfIHnJN9N3A4Pl6mM69D+GxEph2eOaCf7g=
github.com/blacktear23/go-proxyprotocol v1.0.0/go.mod h1:fbqiWSHMxaW0KsJ3SHjpxOMbTpIaQSMRn1GRd+oPyEw=
github.com/carlmjohnson/flagext v0.21.0 h1:/c4uK3ie786Z7caXLcIMvePNSSiH3bQVGDvmGLMme60=
github.com/carlmjohnson/flagext v0.21.0/go.mod h1:Eenv0epIUAr4NuedNmkzI8WmBmjIxZC239XcKxYS2ac=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
6 changes: 3 additions & 3 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19010,19 +19010,19 @@ yynewstate:
}
case 1896:
{
parser.yyVAL.item = &ast.ShowStmt{Tp: ast.ShowStatsMeta}
parser.yyVAL.item = &ast.ShowStmt{Tp: ast.ShowStatsMeta, Table: &ast.TableName{Name: model.NewCIStr("STATS_META"), Schema: model.NewCIStr(mysql.SystemDB)}}
}
case 1897:
{
parser.yyVAL.item = &ast.ShowStmt{Tp: ast.ShowStatsHistograms}
parser.yyVAL.item = &ast.ShowStmt{Tp: ast.ShowStatsHistograms, Table: &ast.TableName{Name: model.NewCIStr("STATS_HISTOGRAMS"), Schema: model.NewCIStr(mysql.SystemDB)}}
}
case 1898:
{
parser.yyVAL.item = &ast.ShowStmt{Tp: ast.ShowStatsTopN}
}
case 1899:
{
parser.yyVAL.item = &ast.ShowStmt{Tp: ast.ShowStatsBuckets}
parser.yyVAL.item = &ast.ShowStmt{Tp: ast.ShowStatsBuckets, Table: &ast.TableName{Name: model.NewCIStr("STATS_BUCKETS"), Schema: model.NewCIStr(mysql.SystemDB)}}
}
case 1900:
{
Expand Down
6 changes: 3 additions & 3 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -10821,19 +10821,19 @@ ShowTargetFilterable:
}
| "STATS_META"
{
$$ = &ast.ShowStmt{Tp: ast.ShowStatsMeta}
$$ = &ast.ShowStmt{Tp: ast.ShowStatsMeta, Table: &ast.TableName{Name: model.NewCIStr("STATS_META"), Schema: model.NewCIStr(mysql.SystemDB)}}
}
| "STATS_HISTOGRAMS"
{
$$ = &ast.ShowStmt{Tp: ast.ShowStatsHistograms}
$$ = &ast.ShowStmt{Tp: ast.ShowStatsHistograms, Table: &ast.TableName{Name: model.NewCIStr("STATS_HISTOGRAMS"), Schema: model.NewCIStr(mysql.SystemDB)}}
}
| "STATS_TOPN"
{
$$ = &ast.ShowStmt{Tp: ast.ShowStatsTopN}
}
| "STATS_BUCKETS"
{
$$ = &ast.ShowStmt{Tp: ast.ShowStatsBuckets}
$$ = &ast.ShowStmt{Tp: ast.ShowStatsBuckets, Table: &ast.TableName{Name: model.NewCIStr("STATS_BUCKETS"), Schema: model.NewCIStr(mysql.SystemDB)}}
}
| "STATS_HEALTHY"
{
Expand Down
31 changes: 27 additions & 4 deletions planner/core/plan_cost.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,12 @@ func (p *PhysicalTableReader) GetPlanCost(taskType property.TaskType, costFlag u
concurrency = float64(p.ctx.GetSessionVars().DistSQLScanConcurrency())
rowSize = getTblStats(p.tablePlan).GetAvgRowSize(p.ctx, p.tablePlan.Schema().Columns, false, false)
seekCost = estimateNetSeekCost(p.tablePlan)
childCost, err := p.tablePlan.GetPlanCost(property.CopSingleReadTaskType, costFlag)
tType := property.MppTaskType
if p.ctx.GetSessionVars().CostModelVersion == modelVer1 {
// regard the underlying tasks as cop-task on modelVer1 for compatibility
tType = property.CopSingleReadTaskType
}
childCost, err := p.tablePlan.GetPlanCost(tType, costFlag)
if err != nil {
return 0, err
}
Expand All @@ -326,7 +331,8 @@ func (p *PhysicalTableReader) GetPlanCost(taskType property.TaskType, costFlag u
// consider concurrency
p.planCost /= concurrency
// consider tidb_enforce_mpp
if isMPP && p.ctx.GetSessionVars().IsMPPEnforced() {
if isMPP && p.ctx.GetSessionVars().IsMPPEnforced() &&
!hasCostFlag(costFlag, CostFlagRecalculate) { // show the real cost in explain-statements
p.planCost /= 1000000000
}
}
Expand Down Expand Up @@ -892,12 +898,19 @@ func (p *PhysicalHashJoin) GetPlanCost(taskType property.TaskType, costFlag uint
}

// GetCost computes cost of stream aggregation considering CPU/memory.
func (p *PhysicalStreamAgg) GetCost(inputRows float64, isRoot bool, costFlag uint64) float64 {
func (p *PhysicalStreamAgg) GetCost(inputRows float64, isRoot, isMPP bool, costFlag uint64) float64 {
aggFuncFactor := p.getAggFuncCostFactor(false)
var cpuCost float64
sessVars := p.ctx.GetSessionVars()
if isRoot {
cpuCost = inputRows * sessVars.GetCPUFactor() * aggFuncFactor
} else if isMPP {
if p.ctx.GetSessionVars().CostModelVersion == modelVer2 {
// use the dedicated CPU factor for TiFlash on modelVer2
cpuCost = inputRows * sessVars.GetTiFlashCPUFactor() * aggFuncFactor
} else {
cpuCost = inputRows * sessVars.GetCopCPUFactor() * aggFuncFactor
}
} else {
cpuCost = inputRows * sessVars.GetCopCPUFactor() * aggFuncFactor
}
Expand All @@ -916,7 +929,7 @@ func (p *PhysicalStreamAgg) GetPlanCost(taskType property.TaskType, costFlag uin
return 0, err
}
p.planCost = childCost
p.planCost += p.GetCost(getCardinality(p.children[0], costFlag), taskType == property.RootTaskType, costFlag)
p.planCost += p.GetCost(getCardinality(p.children[0], costFlag), taskType == property.RootTaskType, taskType == property.MppTaskType, costFlag)
p.planCostInit = true
return p.planCost, nil
}
Expand All @@ -936,6 +949,13 @@ func (p *PhysicalHashAgg) GetCost(inputRows float64, isRoot, isMPP bool, costFla
// Cost of additional goroutines.
cpuCost += (con + 1) * sessVars.GetConcurrencyFactor()
}
} else if isMPP {
if p.ctx.GetSessionVars().CostModelVersion == modelVer2 {
// use the dedicated CPU factor for TiFlash on modelVer2
cpuCost = inputRows * sessVars.GetTiFlashCPUFactor() * aggFuncFactor
} else {
cpuCost = inputRows * sessVars.GetCopCPUFactor() * aggFuncFactor
}
} else {
cpuCost = inputRows * sessVars.GetCopCPUFactor() * aggFuncFactor
}
Expand Down Expand Up @@ -1144,6 +1164,9 @@ func (p *PhysicalExchangeReceiver) GetPlanCost(taskType property.TaskType, costF
}

func getOperatorActRows(operator PhysicalPlan) float64 {
if operator == nil {
return 0
}
runtimeInfo := operator.SCtx().GetSessionVars().StmtCtx.RuntimeStatsColl
id := operator.ID()
actRows := 0.0
Expand Down
11 changes: 8 additions & 3 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2986,13 +2986,18 @@ func (b *PlanBuilder) buildShow(ctx context.Context, show *ast.ShowStmt) (Plan,
p.setSchemaAndNames(buildShowNextRowID())
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SelectPriv, show.Table.Schema.L, show.Table.Name.L, "", ErrPrivilegeCheckFail)
return p, nil
case ast.ShowStatsBuckets, ast.ShowStatsHistograms, ast.ShowStatsMeta, ast.ShowStatsExtended, ast.ShowStatsHealthy, ast.ShowStatsTopN, ast.ShowHistogramsInFlight, ast.ShowColumnStatsUsage:
user := b.ctx.GetSessionVars().User
case ast.ShowStatsExtended, ast.ShowStatsHealthy, ast.ShowStatsTopN, ast.ShowHistogramsInFlight, ast.ShowColumnStatsUsage:
var err error
if user != nil {
if user := b.ctx.GetSessionVars().User; user != nil {
err = ErrDBaccessDenied.GenWithStackByArgs(user.AuthUsername, user.AuthHostname, mysql.SystemDB)
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SelectPriv, mysql.SystemDB, "", "", err)
case ast.ShowStatsBuckets, ast.ShowStatsHistograms, ast.ShowStatsMeta:
var err error
if user := b.ctx.GetSessionVars().User; user != nil {
err = ErrTableaccessDenied.GenWithStackByArgs("SHOW", user.AuthUsername, user.AuthHostname, show.Table.Name.L)
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SelectPriv, show.Table.Schema.L, show.Table.Name.L, "", err)
case ast.ShowRegions:
tableInfo, err := b.is.TableByName(show.Table.Schema, show.Table.Name)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions planner/core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,7 @@ func (p *PhysicalStreamAgg) attach2Task(tasks ...task) task {
partialAgg.SetChildren(cop.indexPlan)
cop.indexPlan = partialAgg
}
cop.addCost(partialAgg.(*PhysicalStreamAgg).GetCost(inputRows, false, 0))
cop.addCost(partialAgg.(*PhysicalStreamAgg).GetCost(inputRows, false, false, 0))
partialAgg.SetCost(cop.cost())
}
t = cop.convertToRootTask(p.ctx)
Expand All @@ -1678,7 +1678,7 @@ func (p *PhysicalStreamAgg) attach2Task(tasks ...task) task {
} else {
attachPlan2Task(p, t)
}
t.addCost(final.GetCost(inputRows, true, 0))
t.addCost(final.GetCost(inputRows, true, false, 0))
t.plan().SetCost(t.cost())
return t
}
Expand Down
2 changes: 2 additions & 0 deletions plugin/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,11 +485,13 @@ func TestAuditLogNormal(t *testing.T) {
sql: "show stats_histograms",
stmtType: "Show",
dbs: "mysql",
tables: "stats_histograms",
},
{
sql: "show stats_meta",
stmtType: "Show",
dbs: "mysql",
tables: "stats_meta",
},
{
sql: "show status",
Expand Down
83 changes: 43 additions & 40 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,12 @@ func doBootstrapSQLFile(s Session) {
}
}

// inTestSuite checks if we are bootstrapping in the context of tests.
// There are some historical differences in behavior between tests and non-tests.
func inTestSuite() bool {
return flag.Lookup("test.v") != nil || flag.Lookup("check.v") != nil
}

// doDMLWorks executes DML statements in bootstrap stage.
// All the statements run in a single transaction.
// TODO: sanitize.
Expand All @@ -2022,58 +2028,55 @@ func doDMLWorks(s Session) {
("%", "root", "", "mysql_native_password", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "N", "Y", "Y", "Y", "Y", "Y")`)
}

// Init global system variables table.
// For GLOBAL scoped system variables, insert the initial value
// into the mysql.global_variables table. This is only run on initial
// bootstrap, and in some cases we will use a different default value
// for new installs versus existing installs.

values := make([]string, 0, len(variable.GetSysVars()))
for k, v := range variable.GetSysVars() {
// Only global variables should be inserted.
if v.HasGlobalScope() {
vVal := v.Value
if v.Name == variable.TiDBTxnMode && config.GetGlobalConfig().Store == "tikv" {
if !v.HasGlobalScope() {
continue
}
vVal := v.Value
switch v.Name {
case variable.TiDBTxnMode:
if config.GetGlobalConfig().Store == "tikv" {
vVal = "pessimistic"
}
if v.Name == variable.TiDBRowFormatVersion {
vVal = strconv.Itoa(variable.DefTiDBRowFormatV2)
}
if v.Name == variable.TiDBPartitionPruneMode {
vVal = variable.DefTiDBPartitionPruneMode
if flag.Lookup("test.v") != nil || flag.Lookup("check.v") != nil || config.CheckTableBeforeDrop {
// enable Dynamic Prune by default in test case.
vVal = string(variable.Dynamic)
}
}
if v.Name == variable.TiDBMemOOMAction {
if flag.Lookup("test.v") != nil || flag.Lookup("check.v") != nil {
// Change the OOM action to log for the test suite.
vVal = variable.OOMActionLog
}
}
if v.Name == variable.TiDBEnableChangeMultiSchema {
vVal = variable.Off
if flag.Lookup("test.v") != nil || flag.Lookup("check.v") != nil {
// enable change multi schema in test case for compatibility with old cases.
vVal = variable.On
}
}
if v.Name == variable.TiDBEnableAsyncCommit && config.GetGlobalConfig().Store == "tikv" {
case variable.TiDBEnableAsyncCommit, variable.TiDBEnable1PC:
if config.GetGlobalConfig().Store == "tikv" {
vVal = variable.On
}
if v.Name == variable.TiDBEnable1PC && config.GetGlobalConfig().Store == "tikv" {
vVal = variable.On
case variable.TiDBPartitionPruneMode:
if inTestSuite() || config.CheckTableBeforeDrop {
vVal = string(variable.Dynamic)
}
if v.Name == variable.TiDBEnableMutationChecker {
case variable.TiDBEnableChangeMultiSchema:
if inTestSuite() {
vVal = variable.On
}
if v.Name == variable.TiDBEnableAutoAnalyze {
if flag.Lookup("test.v") != nil || flag.Lookup("check.v") != nil {
vVal = variable.Off
}
case variable.TiDBMemOOMAction:
if inTestSuite() {
vVal = variable.OOMActionLog
}
if v.Name == variable.TiDBTxnAssertionLevel {
vVal = variable.AssertionFastStr
case variable.TiDBEnableAutoAnalyze:
if inTestSuite() {
vVal = variable.Off
}
value := fmt.Sprintf(`("%s", "%s")`, strings.ToLower(k), vVal)
values = append(values, value)
// For the following sysvars, we change the default
// FOR NEW INSTALLS ONLY. In most cases you don't want to do this.
// It is better to change the value in the Sysvar struct, so that
// all installs will have the same value.
case variable.TiDBRowFormatVersion:
vVal = strconv.Itoa(variable.DefTiDBRowFormatV2)
case variable.TiDBTxnAssertionLevel:
vVal = variable.AssertionFastStr
case variable.TiDBEnableMutationChecker:
vVal = variable.On
}
value := fmt.Sprintf(`("%s", "%s")`, strings.ToLower(k), vVal)
values = append(values, value)
}
sql := fmt.Sprintf("INSERT HIGH_PRIORITY INTO %s.%s VALUES %s;", mysql.SystemDB, mysql.GlobalVariablesTable,
strings.Join(values, ", "))
Expand Down

0 comments on commit d5c463f

Please sign in to comment.