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

*: rename and add tidb_wait_split_region_finish_backoff session var to set wait split region backoff time. #10797

Merged
merged 17 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,9 @@ func (d *ddl) CreateTable(ctx sessionctx.Context, s *ast.CreateTableStmt) (err e
var preSplitAndScatter func()
// do pre-split and scatter.
if tbInfo.ShardRowIDBits > 0 && tbInfo.PreSplitRegions > 0 {
preSplitAndScatter = func() { preSplitTableRegion(d.store, tbInfo, ctx.GetSessionVars().WaitTableSplitFinish) }
preSplitAndScatter = func() {
preSplitTableShardRowIDBitsRegion(ctx, d.store, tbInfo, ctx.GetSessionVars().WaitSplitRegionFinish)
}
} else if atomic.LoadUint32(&EnableSplitTableRegion) != 0 {
pi := tbInfo.GetPartitionInfo()
if pi != nil {
Expand All @@ -1304,7 +1306,7 @@ func (d *ddl) CreateTable(ctx sessionctx.Context, s *ast.CreateTableStmt) (err e
}
}
if preSplitAndScatter != nil {
if ctx.GetSessionVars().WaitTableSplitFinish {
if ctx.GetSessionVars().WaitSplitRegionFinish {
preSplitAndScatter()
} else {
go preSplitAndScatter()
Expand Down
7 changes: 4 additions & 3 deletions ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/tablecodec"
Expand Down Expand Up @@ -343,7 +344,7 @@ func checkSafePoint(w *worker, snapshotTS uint64) error {
type splitableStore interface {
SplitRegion(splitKey kv.Key) error
SplitRegionAndScatter(splitKey kv.Key) (uint64, error)
WaitScatterRegionFinish(regionID uint64) error
WaitScatterRegionFinish(regionID uint64, backoffTime int) error
}

func splitPartitionTableRegion(store kv.Storage, pi *model.PartitionInfo) {
Expand All @@ -365,7 +366,7 @@ func splitTableRegion(store kv.Storage, tableID int64) {
}
}

func preSplitTableRegion(store kv.Storage, tblInfo *model.TableInfo, waitTableSplitFinish bool) {
func preSplitTableShardRowIDBitsRegion(ctx sessionctx.Context, store kv.Storage, tblInfo *model.TableInfo, waitTableSplitFinish bool) {
s, ok := store.(splitableStore)
if !ok {
return
Expand Down Expand Up @@ -426,7 +427,7 @@ func preSplitTableRegion(store kv.Storage, tblInfo *model.TableInfo, waitTableSp
return
}
for _, regionID := range regionIDs {
err := s.WaitScatterRegionFinish(regionID)
err := s.WaitScatterRegionFinish(regionID, ctx.GetSessionVars().BackoffTimeVars.GetWaitScatterRegionFinishBackoff())
if err != nil {
logutil.Logger(ddlLogCtx).Warn("[ddl] wait scatter region failed", zap.Uint64("regionID", regionID), zap.Error(err))
}
Expand Down
22 changes: 16 additions & 6 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package executor_test

import (
"context"
"strconv"

. "github.com/pingcap/check"
"github.com/pingcap/parser/terror"
Expand Down Expand Up @@ -341,12 +342,21 @@ func (s *testSuite2) TestSetVar(c *C) {
_, err = tk.Exec("set global read_only = abc")
c.Assert(err, NotNil)

// test for tidb_wait_table_split_finish
tk.MustQuery(`select @@session.tidb_wait_table_split_finish;`).Check(testkit.Rows("0"))
tk.MustExec("set tidb_wait_table_split_finish = 1")
tk.MustQuery(`select @@session.tidb_wait_table_split_finish;`).Check(testkit.Rows("1"))
tk.MustExec("set tidb_wait_table_split_finish = 0")
tk.MustQuery(`select @@session.tidb_wait_table_split_finish;`).Check(testkit.Rows("0"))
// test for tidb_wait_split_region_finish
tk.MustQuery(`select @@session.tidb_wait_split_region_finish;`).Check(testkit.Rows("1"))
tk.MustExec("set tidb_wait_split_region_finish = 1")
tk.MustQuery(`select @@session.tidb_wait_split_region_finish;`).Check(testkit.Rows("1"))
tk.MustExec("set tidb_wait_split_region_finish = 0")
tk.MustQuery(`select @@session.tidb_wait_split_region_finish;`).Check(testkit.Rows("0"))

// test for tidb_wait_split_region_finish_backoff
tk.MustQuery(`select @@session.tidb_wait_split_region_finish_backoff;`).Check(testkit.Rows(strconv.Itoa(variable.DefWaitScatterRegionFinishBackoff)))
tk.MustExec("set tidb_wait_split_region_finish_backoff = 1")
tk.MustQuery(`select @@session.tidb_wait_split_region_finish_backoff;`).Check(testkit.Rows("1"))
_, err = tk.Exec("set tidb_wait_split_region_finish_backoff = 0")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "tidb_wait_split_region_finish_backoff(0) cannot be smaller than 1")
tk.MustQuery(`select @@session.tidb_wait_split_region_finish_backoff;`).Check(testkit.Rows("1"))

tk.MustExec("set session tidb_back_off_weight = 3")
tk.MustQuery("select @@session.tidb_back_off_weight;").Check(testkit.Rows("3"))
Expand Down
10 changes: 5 additions & 5 deletions executor/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type SplitIndexRegionExec struct {

type splitableStore interface {
SplitRegionAndScatter(splitKey kv.Key) (uint64, error)
WaitScatterRegionFinish(regionID uint64) error
WaitScatterRegionFinish(regionID uint64, backoffTime int) error
}

// Next implements the Executor Next interface.
Expand All @@ -73,11 +73,11 @@ func (e *SplitIndexRegionExec) Next(ctx context.Context, _ *chunk.RecordBatch) e
regionIDs = append(regionIDs, regionID)

}
if !e.ctx.GetSessionVars().WaitTableSplitFinish {
if !e.ctx.GetSessionVars().WaitSplitRegionFinish {
return nil
}
for _, regionID := range regionIDs {
err := s.WaitScatterRegionFinish(regionID)
err := s.WaitScatterRegionFinish(regionID, e.ctx.GetSessionVars().BackoffTimeVars.GetWaitScatterRegionFinishBackoff())
if err != nil {
logutil.Logger(context.Background()).Warn("wait scatter region failed",
zap.Uint64("regionID", regionID),
Expand Down Expand Up @@ -239,11 +239,11 @@ func (e *SplitTableRegionExec) Next(ctx context.Context, _ *chunk.RecordBatch) e
}
regionIDs = append(regionIDs, regionID)
}
if !e.ctx.GetSessionVars().WaitTableSplitFinish {
if !e.ctx.GetSessionVars().WaitSplitRegionFinish {
return nil
}
for _, regionID := range regionIDs {
err := s.WaitScatterRegionFinish(regionID)
err := s.WaitScatterRegionFinish(regionID, e.ctx.GetSessionVars().BackoffTimeVars.GetWaitScatterRegionFinishBackoff())
if err != nil {
logutil.Logger(context.Background()).Warn("wait scatter region failed",
zap.Uint64("regionID", regionID),
Expand Down
33 changes: 29 additions & 4 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ type SessionVars struct {
// DDLReorgPriority is the operation priority of adding indices.
DDLReorgPriority int

// WaitTableSplitFinish defines the create table pre-split behaviour is sync or async.
WaitTableSplitFinish bool
// WaitSplitRegionFinish defines the split region behaviour is sync or async.
WaitSplitRegionFinish bool

// EnableStreaming indicates whether the coprocessor request can use streaming API.
// TODO: remove this after tidb-server configuration "enable-streaming' removed.
Expand Down Expand Up @@ -379,6 +379,9 @@ type SessionVars struct {

// LowResolutionTSO is used for reading data with low resolution TSO which is updated once every two seconds.
LowResolutionTSO bool

// Backoff is used for set the back off time.
BackoffTimeVars BackoffTimer
}

// ConnectionInfo present connection used by audit.
Expand Down Expand Up @@ -429,6 +432,7 @@ func NewSessionVars() *SessionVars {
CommandValue: uint32(mysql.ComSleep),
TiDBOptJoinReorderThreshold: DefTiDBOptJoinReorderThreshold,
SlowQueryFile: config.GetGlobalConfig().Log.SlowQueryFile,
WaitSplitRegionFinish: DefTiDBWaitSplitRegionFinish,
}
vars.Concurrency = Concurrency{
IndexLookupConcurrency: DefIndexLookupConcurrency,
Expand Down Expand Up @@ -792,8 +796,10 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
s.SlowQueryFile = val
case TiDBEnableFastAnalyze:
s.EnableFastAnalyze = TiDBOptOn(val)
case TiDBWaitTableSplitFinish:
s.WaitTableSplitFinish = TiDBOptOn(val)
case TiDBWaitSplitRegionFinish:
s.WaitSplitRegionFinish = TiDBOptOn(val)
case TiDBWaitSplitRegionFinishBackoff:
s.BackoffTimeVars.SetWaitScatterRegionFinishBackoff(tidbOptPositiveInt32(val, DefWaitScatterRegionFinishBackoff))
case TiDBExpensiveQueryTimeThreshold:
atomic.StoreUint64(&ExpensiveQueryTimeThreshold, uint64(tidbOptPositiveInt32(val, DefTiDBExpensiveQueryTimeThreshold)))
case TiDBTxnMode:
Expand Down Expand Up @@ -991,6 +997,25 @@ const (
SlowLogMemMax = "Mem_max"
)

type BackoffTimer struct {
waitScatterRegionFinish int
}

// GetWaitScatterRegionFinishBackoff gets the back off time of waitScatterRegionFinish.
func (b *BackoffTimer) GetWaitScatterRegionFinishBackoff() int {
if b.waitScatterRegionFinish > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use tidbOptPositiveInt32 to set the value. I think we needn't do this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. Thanks.

return b.waitScatterRegionFinish
}
return DefWaitScatterRegionFinishBackoff
}

// SetWaitScatterRegionFinishBackoff sets the back off time of waitScatterRegionFinish.
func (b *BackoffTimer) SetWaitScatterRegionFinishBackoff(t int) {
zimulala marked this conversation as resolved.
Show resolved Hide resolved
if t > 0 {
b.waitScatterRegionFinish = t
}
}

// SlowLogFormat uses for formatting slow log.
// The slow log output is like below:
// # Time: 2019-04-28T15:24:04.309074+08:00
Expand Down
3 changes: 2 additions & 1 deletion sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,8 @@ var defaultSysVars = []*SysVar{
{ScopeGlobal | ScopeSession, TiDBOptJoinReorderThreshold, strconv.Itoa(DefTiDBOptJoinReorderThreshold)},
{ScopeSession, TiDBCheckMb4ValueInUTF8, BoolToIntStr(config.GetGlobalConfig().CheckMb4ValueInUTF8)},
{ScopeSession, TiDBSlowQueryFile, ""},
{ScopeSession, TiDBWaitTableSplitFinish, BoolToIntStr(DefTiDBWaitTableSplitFinish)},
{ScopeSession, TiDBWaitSplitRegionFinish, BoolToIntStr(DefTiDBWaitSplitRegionFinish)},
{ScopeSession, TiDBWaitSplitRegionFinishBackoff, strconv.Itoa(DefWaitScatterRegionFinishBackoff)},
{ScopeSession, TiDBLowResolutionTSO, "0"},
{ScopeSession, TiDBExpensiveQueryTimeThreshold, strconv.Itoa(DefTiDBExpensiveQueryTimeThreshold)},
}
Expand Down
10 changes: 7 additions & 3 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,11 @@ const (
// It can be: PRIORITY_LOW, PRIORITY_NORMAL, PRIORITY_HIGH
TiDBDDLReorgPriority = "tidb_ddl_reorg_priority"

// TiDBWaitTableSplitFinish defines the create table pre-split behaviour is sync or async.
TiDBWaitTableSplitFinish = "tidb_wait_table_split_finish"
// TiDBWaitSplitRegionFinish defines the split region behaviour is sync or async.
TiDBWaitSplitRegionFinish = "tidb_wait_split_region_finish"

// TiDBWaitSplitRegionFinishBackoff uses to set the split and scatter region back off time.
TiDBWaitSplitRegionFinishBackoff = "tidb_wait_split_region_finish_backoff"

// tidb_force_priority defines the operations priority of all statements.
// It can be "NO_PRIORITY", "LOW_PRIORITY", "HIGH_PRIORITY", "DELAYED"
Expand Down Expand Up @@ -333,8 +336,9 @@ const (
DefTiDBDDLSlowOprThreshold = 300
DefTiDBUseFastAnalyze = false
DefTiDBSkipIsolationLevelCheck = false
DefTiDBWaitTableSplitFinish = false
DefTiDBWaitSplitRegionFinish = true
DefTiDBExpensiveQueryTimeThreshold = 60 // 60s
DefWaitScatterRegionFinishBackoff = 120000
)

// Process global variables.
Expand Down
8 changes: 8 additions & 0 deletions sessionctx/variable/varsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,14 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string) (string,
if v < 0 || v >= 64 {
return value, errors.Errorf("tidb_join_order_algo_threshold(%d) cannot be smaller than 0 or larger than 63", v)
}
case TiDBWaitSplitRegionFinishBackoff:
v, err := strconv.Atoi(value)
if err != nil {
return value, ErrWrongTypeForVar.GenWithStackByArgs(name)
}
if v <= 0 {
return value, errors.Errorf("tidb_wait_split_region_finish_backoff(%d) cannot be smaller than 1", v)
}
}
return value, nil
}
Expand Down
1 change: 0 additions & 1 deletion store/tikv/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ const (
rawkvMaxBackoff = 20000
splitRegionBackoff = 20000
scatterRegionBackoff = 20000
waitScatterRegionFinishBackoff = 120000
locateRegionMaxBackoff = 20000
pessimisticLockMaxBackoff = 10000
pessimisticRollbackMaxBackoff = 10000
Expand Down
7 changes: 4 additions & 3 deletions store/tikv/split_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,11 @@ func (s *tikvStore) scatterRegion(regionID uint64) error {
return nil
}

func (s *tikvStore) WaitScatterRegionFinish(regionID uint64) error {
func (s *tikvStore) WaitScatterRegionFinish(regionID uint64, backoffTime int) error {
logutil.Logger(context.Background()).Info("wait scatter region",
zap.Uint64("regionID", regionID))
bo := NewBackoffer(context.Background(), waitScatterRegionFinishBackoff)
zap.Uint64("regionID", regionID),
zap.Int("backoff time", backoffTime))
bo := NewBackoffer(context.Background(), backoffTime)
logFreq := 0
for {
resp, err := s.pdClient.GetOperator(context.Background(), regionID)
Expand Down