Skip to content

Commit

Permalink
Merge branch 'master' into metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
disksing committed Feb 4, 2021
2 parents 0581d3c + 343c412 commit a59e79a
Show file tree
Hide file tree
Showing 48 changed files with 890 additions and 160 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- [**Slack Channel**](https://slack.tidb.io/invite?team=tidb-community&channel=everyone&ref=pingcap-tidb)
- **Twitter**: [@PingCAP](https://twitter.com/PingCAP)
- [**Reddit**](https://www.reddit.com/r/TiDB/)
- **Mailing list**: [Google Group](https://groups.google.com/forum/#!forum/tidb-user)
- **Mailing list**: [lists.tidb.io](https://lists.tidb.io/g/main/subgroups)
- [**For support, please contact PingCAP**](http://bit.ly/contact_us_via_github)

## What is TiDB?
Expand Down
9 changes: 4 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,16 @@ type Config struct {
EnableTelemetry bool `toml:"enable-telemetry" json:"enable-telemetry"`
// Labels indicates the labels set for the tidb server. The labels describe some specific properties for the tidb
// server like `zone`/`rack`/`host`. Currently, labels won't affect the tidb server except for some special
// label keys. Now we only have `group` as a special label key.
// Note that: 'group' is a special label key which should be automatically set by tidb-operator. We don't suggest
// label keys. Now we have following special keys:
// 1. 'group' is a special label key which should be automatically set by tidb-operator. We don't suggest
// users to set 'group' in labels.
// 2. 'zone' is a special key that indicates the DC location of this tidb-server. If it is set, the value for this
// key will be the default value of the session variable `txn_scope` for this tidb-server.
Labels map[string]string `toml:"labels" json:"labels"`
// EnableGlobalIndex enables creating global index.
EnableGlobalIndex bool `toml:"enable-global-index" json:"enable-global-index"`
// DeprecateIntegerDisplayWidth indicates whether deprecating the max display length for integer.
DeprecateIntegerDisplayWidth bool `toml:"deprecate-integer-display-length" json:"deprecate-integer-display-length"`
// TxnScope indicates the default value for session variable txn_scope
TxnScope string `toml:"txn-scope" json:"txn-scope"`
// EnableEnumLengthLimit indicates whether the enum/set element length is limited.
// According to MySQL 8.0 Refman:
// The maximum supported length of an individual SET element is M <= 255 and (M x w) <= 1020,
Expand Down Expand Up @@ -661,7 +661,6 @@ var defaultConf = Config{
SpilledFileEncryptionMethod: SpilledFileEncryptionMethodPlaintext,
},
DeprecateIntegerDisplayWidth: false,
TxnScope: DefTxnScope,
EnableEnumLengthLimit: true,
StoresRefreshInterval: defTiKVCfg.StoresRefreshInterval,
}
Expand Down
3 changes: 0 additions & 3 deletions config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ enable-telemetry = true
# a warning like `Integer display width is deprecated and will be removed in a future release`.
deprecate-integer-display-length = false

# txn-scope indicates the default value for the `txn_scope` session variable.
txn-scope = "global"

# enable-enum-length-limit is used to deal with compatibility issues. When true, the enum/set element length is limited.
# According to MySQL 8.0 Refman:
# The maximum supported length of an individual SET element is M <= 255 and (M x w) <= 1020,
Expand Down
6 changes: 3 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ index-limit = 70
table-column-count-limit = 4000
skip-register-to-dashboard = true
deprecate-integer-display-length = true
txn-scope = "dc-1"
enable-enum-length-limit = false
stores-refresh-interval = 30
[performance]
Expand Down Expand Up @@ -221,6 +220,7 @@ engines = ["tiflash"]
[labels]
foo= "bar"
group= "abc"
zone= "dc-1"
[security]
spilled-file-encryption-method = "plaintext"
`)
Expand Down Expand Up @@ -267,12 +267,12 @@ spilled-file-encryption-method = "plaintext"
c.Assert(conf.IndexLimit, Equals, 70)
c.Assert(conf.TableColumnCountLimit, Equals, uint32(4000))
c.Assert(conf.SkipRegisterToDashboard, Equals, true)
c.Assert(len(conf.Labels), Equals, 2)
c.Assert(len(conf.Labels), Equals, 3)
c.Assert(conf.Labels["foo"], Equals, "bar")
c.Assert(conf.Labels["group"], Equals, "abc")
c.Assert(conf.Labels["zone"], Equals, "dc-1")
c.Assert(conf.Security.SpilledFileEncryptionMethod, Equals, SpilledFileEncryptionMethodPlaintext)
c.Assert(conf.DeprecateIntegerDisplayWidth, Equals, true)
c.Assert(conf.TxnScope, Equals, "dc-1")
c.Assert(conf.EnableEnumLengthLimit, Equals, false)
c.Assert(conf.StoresRefreshInterval, Equals, uint64(30))

Expand Down
9 changes: 9 additions & 0 deletions config/config_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,12 @@ func flatten(flatMap map[string]interface{}, nested interface{}, prefix string)
flatMap[prefix] = nested
}
}

// GetTxnScopeFromConfig extract txn_scope default value from config
func GetTxnScopeFromConfig() string {
v, ok := GetGlobalConfig().Labels["zone"]
if ok {
return v
}
return DefTxnScope
}
15 changes: 15 additions & 0 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1636,6 +1636,15 @@ func (s *testIntegrationSuite7) TestAlterTableExchangePartition(c *C) {
id INT NOT NULL
);`)
tk.MustExec(`INSERT INTO e VALUES (1669),(337),(16),(2005)`)
// test disable exchange partition
tk.MustExec("ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 8200 Exchange Partition is disabled, please set 'tidb_enable_exchange_partition' if you need to need to enable it"))
tk.MustQuery("select * from e").Check(testkit.Rows("16", "1669", "337", "2005"))
tk.MustQuery("select * from e2").Check(testkit.Rows())

// enable exchange partition
tk.MustExec("set @@tidb_enable_exchange_partition=1")
defer tk.MustExec("set @@tidb_enable_exchange_partition=0")
tk.MustExec("ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2")
tk.MustQuery("select * from e2").Check(testkit.Rows("16"))
tk.MustQuery("select * from e").Check(testkit.Rows("1669", "337", "2005"))
Expand Down Expand Up @@ -2022,6 +2031,7 @@ func (s *testIntegrationSuite4) TestExchangePartitionTableCompatiable(c *C) {

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.Se.GetSessionVars().SetSystemVar("tidb_enable_exchange_partition", "1")
for i, t := range cases {
tk.MustExec(t.ptSQL)
tk.MustExec(t.ntSQL)
Expand All @@ -2035,6 +2045,7 @@ func (s *testIntegrationSuite4) TestExchangePartitionTableCompatiable(c *C) {
tk.MustExec(t.exchangeSQL)
}
}
tk.Se.GetSessionVars().SetSystemVar("tidb_enable_exchange_partition", "0")
}

func (s *testIntegrationSuite7) TestExchangePartitionExpressIndex(c *C) {
Expand All @@ -2043,6 +2054,8 @@ func (s *testIntegrationSuite7) TestExchangePartitionExpressIndex(c *C) {
})
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_exchange_partition=1")
defer tk.MustExec("set @@tidb_enable_exchange_partition=0")
tk.MustExec("drop table if exists pt1;")
tk.MustExec("create table pt1(a int, b int, c int) PARTITION BY hash (a) partitions 1;")
tk.MustExec("alter table pt1 add index idx((a+c));")
Expand Down Expand Up @@ -3172,6 +3185,8 @@ func (s *testIntegrationSuite7) TestCommitWhenSchemaChange(c *C) {
)`)
tk2 := testkit.NewTestKit(c, s.store)
tk2.MustExec("use test")
tk2.MustExec("set @@tidb_enable_exchange_partition=1")
defer tk2.MustExec("set @@tidb_enable_exchange_partition=0")

tk.MustExec("begin")
tk.MustExec("insert into schema_change values (1, '2019-12-25 13:27:42')")
Expand Down
2 changes: 2 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6541,6 +6541,7 @@ func (s *testDBSuite4) TestIssue22207(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test;")
tk.MustExec("set @@session.tidb_enable_table_partition = nightly;")
tk.MustExec("set @@session.tidb_enable_exchange_partition = 1;")
tk.MustExec("drop table if exists t1;")
tk.MustExec("drop table if exists t2;")
tk.MustExec("create table t1(id char(10)) partition by list columns(id) (partition p0 values in ('a'), partition p1 values in ('b'));")
Expand All @@ -6560,4 +6561,5 @@ func (s *testDBSuite4) TestIssue22207(c *C) {
tk.MustExec("ALTER TABLE t1 EXCHANGE PARTITION p0 WITH TABLE t2;")
tk.MustQuery("select * from t2").Check(testkit.Rows("1", "2", "3"))
c.Assert(len(tk.MustQuery("select * from t1").Rows()), Equals, 0)
tk.MustExec("set @@session.tidb_enable_exchange_partition = 0;")
}
4 changes: 4 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3158,6 +3158,10 @@ func checkExchangePartition(pt *model.TableInfo, nt *model.TableInfo) error {
}

func (d *ddl) ExchangeTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *ast.AlterTableSpec) error {
if !ctx.GetSessionVars().TiDBEnableExchangePartition {
ctx.GetSessionVars().StmtCtx.AppendWarning(errExchangePartitionDisabled)
return nil
}
ptSchema, pt, err := d.getSchemaAndTableByIdent(ctx, ident)
if err != nil {
return errors.Trace(err)
Expand Down
2 changes: 2 additions & 0 deletions ddl/ddl_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@ func (s *testDDLSerialSuite) TestCancelJob(c *C) {
ctx := testNewContext(d)
err := ctx.NewTxn(context.Background())
c.Assert(err, IsNil)
ctx.GetSessionVars().SetSystemVar("tidb_enable_exchange_partition", "1")
defer ctx.GetSessionVars().SetSystemVar("tidb_enable_exchange_partition", "0")
testCreateTable(c, ctx, d, dbInfo, partitionTblInfo)
tableAutoID := int64(100)
shardRowIDBits := uint64(5)
Expand Down
2 changes: 2 additions & 0 deletions ddl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,6 @@ var (

// ErrUnknownEngine is returned when the table engine is unknown.
ErrUnknownEngine = dbterror.ClassDDL.NewStd(mysql.ErrUnknownStorageEngine)

errExchangePartitionDisabled = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("Exchange Partition is disabled, please set 'tidb_enable_exchange_partition' if you need to need to enable it", nil))
)
2 changes: 2 additions & 0 deletions ddl/failtest/fail_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ func (s *testFailDBSuite) TestHalfwayCancelOperations(c *C) {
tk.MustExec("insert into pt values(1), (3), (5)")
tk.MustExec("create table nt(a int)")
tk.MustExec("insert into nt values(7)")
tk.MustExec("set @@tidb_enable_exchange_partition=1")
defer tk.MustExec("set @@tidb_enable_exchange_partition=0")
_, err = tk.Exec("alter table pt exchange partition p1 with table nt")
c.Assert(err, NotNil)

Expand Down
2 changes: 2 additions & 0 deletions ddl/placement_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,9 @@ func (s *testDBSuite1) TestPlacementPolicyCache(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.Se.GetSessionVars().EnableAlterPlacement = true
tk.MustExec("set @@tidb_enable_exchange_partition = 1")
defer func() {
tk.MustExec("set @@tidb_enable_exchange_partition = 0")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t2")
tk.Se.GetSessionVars().EnableAlterPlacement = false
Expand Down
3 changes: 3 additions & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,9 @@ func (s *testSerialSuite) TestAutoRandomExchangePartition(c *C) {

tk.MustExec("use auto_random_db")

tk.MustExec("set @@tidb_enable_exchange_partition=1")
defer tk.MustExec("set @@tidb_enable_exchange_partition=0")

tk.MustExec("drop table if exists e1, e2, e3, e4;")

tk.MustExec("create table e1 (a bigint primary key auto_random(3)) partition by hash(a) partitions 1;")
Expand Down
13 changes: 9 additions & 4 deletions executor/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -1232,9 +1232,14 @@ type analyzeIndexIncrementalExec struct {
AnalyzeIndexExec
oldHist *statistics.Histogram
oldCMS *statistics.CMSketch
oldTopN *statistics.TopN
}

func analyzeIndexIncremental(idxExec *analyzeIndexIncrementalExec) analyzeResult {
var statsVer = statistics.Version1
if idxExec.analyzePB.IdxReq.Version != nil {
statsVer = int(*idxExec.analyzePB.IdxReq.Version)
}
startPos := idxExec.oldHist.GetUpper(idxExec.oldHist.Len() - 1)
values, _, err := codec.DecodeRange(startPos.GetBytes(), len(idxExec.idxInfo.Columns), nil, nil)
if err != nil {
Expand All @@ -1245,7 +1250,7 @@ func analyzeIndexIncremental(idxExec *analyzeIndexIncrementalExec) analyzeResult
if err != nil {
return analyzeResult{Err: err, job: idxExec.job}
}
hist, err = statistics.MergeHistograms(idxExec.ctx.GetSessionVars().StmtCtx, idxExec.oldHist, hist, int(idxExec.opts[ast.AnalyzeOptNumBuckets]), statistics.Version1)
hist, err = statistics.MergeHistograms(idxExec.ctx.GetSessionVars().StmtCtx, idxExec.oldHist, hist, int(idxExec.opts[ast.AnalyzeOptNumBuckets]), statsVer)
if err != nil {
return analyzeResult{Err: err, job: idxExec.job}
}
Expand All @@ -1256,9 +1261,9 @@ func analyzeIndexIncremental(idxExec *analyzeIndexIncrementalExec) analyzeResult
}
cms.CalcDefaultValForAnalyze(uint64(hist.NDV))
}
var statsVer = statistics.Version1
if idxExec.analyzePB.IdxReq.Version != nil {
statsVer = int(*idxExec.analyzePB.IdxReq.Version)
if statsVer == statistics.Version2 {
poped := statistics.MergeTopN(topN, idxExec.oldTopN, cms, uint32(idxExec.opts[ast.AnalyzeOptNumTopN]), false)
hist.AddIdxVals(poped)
}
result := analyzeResult{
TableID: idxExec.tableID,
Expand Down
11 changes: 8 additions & 3 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1943,7 +1943,7 @@ func (b *executorBuilder) buildAnalyzeIndexPushdown(task plannercore.AnalyzeInde
topNSize := new(int32)
*topNSize = int32(opts[ast.AnalyzeOptNumTopN])
statsVersion := new(int32)
*statsVersion = int32(b.ctx.GetSessionVars().AnalyzeVersion)
*statsVersion = int32(task.StatsVersion)
e.analyzePB.IdxReq = &tipb.AnalyzeIndexReq{
BucketSize: int64(opts[ast.AnalyzeOptNumBuckets]),
NumColumns: int32(len(task.IndexInfo.Columns)),
Expand Down Expand Up @@ -1988,9 +1988,14 @@ func (b *executorBuilder) buildAnalyzeIndexIncremental(task plannercore.AnalyzeI
}
oldHist = idx.TruncateHistogram(bktID)
}
var oldTopN *statistics.TopN
if analyzeTask.idxExec.analyzePB.IdxReq.GetVersion() == statistics.Version2 {
oldTopN = idx.TopN.Copy()
oldTopN.RemoveVal(oldHist.Bounds.GetRow(len(oldHist.Buckets)*2 - 1).GetBytes(0))
}
oldHist = oldHist.RemoveUpperBound()
analyzeTask.taskType = idxIncrementalTask
analyzeTask.idxIncrementalExec = &analyzeIndexIncrementalExec{AnalyzeIndexExec: *analyzeTask.idxExec, oldHist: oldHist, oldCMS: idx.CMSketch}
analyzeTask.idxIncrementalExec = &analyzeIndexIncrementalExec{AnalyzeIndexExec: *analyzeTask.idxExec, oldHist: oldHist, oldCMS: idx.CMSketch, oldTopN: oldTopN}
analyzeTask.job = &statistics.AnalyzeJob{DBName: task.DBName, TableName: task.TableName, PartitionName: task.PartitionName, JobInfo: "analyze incremental index " + task.IndexInfo.Name.O}
return analyzeTask
}
Expand Down Expand Up @@ -2023,7 +2028,7 @@ func (b *executorBuilder) buildAnalyzeColumnsPushdown(task plannercore.AnalyzeCo
TimeZoneOffset: offset,
},
opts: opts,
analyzeVer: b.ctx.GetSessionVars().AnalyzeVersion,
analyzeVer: task.StatsVersion,
}
depth := int32(opts[ast.AnalyzeOptCMSketchDepth])
width := int32(opts[ast.AnalyzeOptCMSketchWidth])
Expand Down
49 changes: 49 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7487,6 +7487,8 @@ func (s *testSuite) TestIssue15563(c *C) {
}

func (s *testSuite) TestStalenessTransaction(c *C) {
c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer", "return(false)"), IsNil)
defer failpoint.Disable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer")
testcases := []struct {
name string
preSQL string
Expand Down Expand Up @@ -7562,6 +7564,8 @@ func (s *testSuite) TestStalenessTransaction(c *C) {
}

func (s *testSuite) TestStalenessAndHistoryRead(c *C) {
c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer", "return(false)"), IsNil)
defer failpoint.Disable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer")
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
Expand Down Expand Up @@ -7607,3 +7611,48 @@ func (s *testSuite) TestIssue22201(c *C) {
tk.MustQuery("SELECT HEX(WEIGHT_STRING('ab' AS char(1000000000000000000)));").Check(testkit.Rows("<nil>"))
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1301 Result of weight_string() was larger than max_allowed_packet (67108864) - truncated"))
}

func (s *testSerialSuite) TestStalenessTransactionSchemaVer(c *C) {
testcases := []struct {
name string
sql string
expectErr error
}{
{
name: "ddl change before stale txn",
sql: `START TRANSACTION READ ONLY WITH TIMESTAMP BOUND EXACT STALENESS '00:00:03'`,
expectErr: errors.New("schema version changed after the staleness startTS"),
},
{
name: "ddl change before stale txn",
sql: fmt.Sprintf("START TRANSACTION READ ONLY WITH TIMESTAMP BOUND READ TIMESTAMP '%v'",
time.Now().Truncate(3*time.Second).Format("2006-01-02 15:04:05")),
expectErr: errors.New(".*schema version changed after the staleness startTS.*"),
},
{
name: "ddl change before stale txn",
sql: `START TRANSACTION READ ONLY WITH TIMESTAMP BOUND EXACT STALENESS '00:00:03'`,
expectErr: nil,
},
}
tk := testkit.NewTestKitWithInit(c, s.store)
for _, testcase := range testcases {
check := func() {
if testcase.expectErr != nil {
c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer", "return(true)"), IsNil)
defer failpoint.Disable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer")
} else {
c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer", "return(false)"), IsNil)
defer failpoint.Disable("github.com/pingcap/tidb/executor/mockStalenessTxnSchemaVer")
}
_, err := tk.Exec(testcase.sql)
if testcase.expectErr != nil {
c.Assert(err, NotNil)
c.Assert(err.Error(), Matches, testcase.expectErr.Error())
} else {
c.Assert(err, IsNil)
}
}
check()
}
}
24 changes: 15 additions & 9 deletions executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,21 @@ func (e *ExecuteExec) Next(ctx context.Context, req *chunk.Chunk) error {
// Build builds a prepared statement into an executor.
// After Build, e.StmtExec will be used to do the real execution.
func (e *ExecuteExec) Build(b *executorBuilder) error {
ok, err := plannercore.IsPointGetWithPKOrUniqueKeyByAutoCommit(e.ctx, e.plan)
if err != nil {
return err
}
if ok {
err = e.ctx.InitTxnWithStartTS(math.MaxUint64)
}
if err != nil {
return err
if snapshotTS := e.ctx.GetSessionVars().SnapshotTS; snapshotTS != 0 {
if err := e.ctx.InitTxnWithStartTS(snapshotTS); err != nil {
return err
}
} else {
ok, err := plannercore.IsPointGetWithPKOrUniqueKeyByAutoCommit(e.ctx, e.plan)
if err != nil {
return err
}
if ok {
err = e.ctx.InitTxnWithStartTS(math.MaxUint64)
if err != nil {
return err
}
}
}
stmtExec := b.build(e.plan)
if b.err != nil {
Expand Down
Loading

0 comments on commit a59e79a

Please sign in to comment.