Skip to content

Commit

Permalink
Merge branch 'master' into bucket-ndv-index-hist
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros committed Jan 7, 2021
2 parents e5f83c6 + 18d3135 commit 6fa4644
Show file tree
Hide file tree
Showing 68 changed files with 2,204 additions and 991 deletions.
7 changes: 6 additions & 1 deletion .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ sig/planner:
component/statistics:
- statistics/*

sig/DDL:
sig/infra:
- ddl/*
- domain/*
- infoschema/*
- session/*
- server/*
- privilege/*
- plugin/*
- config/*
- meta/*
- owner/*

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/assign_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
- name: Run issues assignment to project SIG DDL Kanban
uses: srggrs/assign-one-project-github-action@1.2.0
if: |
contains(github.event.issue.labels.*.name, 'sig/DDL') ||
contains(github.event.issue.labels.*.name, 'sig/infra') ||
contains(github.event.issue.labels.*.name, 'component/binlog') ||
contains(github.event.issue.labels.*.name, 'component/charset') ||
contains(github.event.issue.labels.*.name, 'component/infoschema') ||
Expand Down
312 changes: 199 additions & 113 deletions bindinfo/bind_test.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bindinfo/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (br *BindRecord) shallowCopy() *BindRecord {
}

func (br *BindRecord) isSame(other *BindRecord) bool {
return br.OriginalSQL == other.OriginalSQL && br.Db == other.Db
return br.OriginalSQL == other.OriginalSQL
}

var statusIndex = map[string]int{
Expand Down
23 changes: 11 additions & 12 deletions bindinfo/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (h *BindHandle) DropBindRecord(originalSQL, db string, binding *Binding) (e
func (h *BindHandle) lockBindInfoTable() error {
// h.sctx already locked.
exec, _ := h.sctx.Context.(sqlexec.SQLExecutor)
_, err := exec.ExecuteInternal(context.TODO(), h.lockBindInfoSQL())
_, err := exec.ExecuteInternal(context.TODO(), h.LockBindInfoSQL())
return err
}

Expand Down Expand Up @@ -539,7 +539,7 @@ func (c cache) removeDeletedBindRecord(hash string, meta *BindRecord) {
func (c cache) setBindRecord(hash string, meta *BindRecord) {
metas := c[hash]
for i := range metas {
if metas[i].Db == meta.Db && metas[i].OriginalSQL == meta.OriginalSQL {
if metas[i].OriginalSQL == meta.OriginalSQL {
metas[i] = meta
return
}
Expand Down Expand Up @@ -568,7 +568,7 @@ func copyBindRecordUpdateMap(oldMap map[string]*bindRecordUpdate) map[string]*bi
func (c cache) getBindRecord(hash, normdOrigSQL, db string) *BindRecord {
bindRecords := c[hash]
for _, bindRecord := range bindRecords {
if bindRecord.OriginalSQL == normdOrigSQL && bindRecord.Db == db {
if bindRecord.OriginalSQL == normdOrigSQL {
return bindRecord
}
}
Expand All @@ -577,9 +577,8 @@ func (c cache) getBindRecord(hash, normdOrigSQL, db string) *BindRecord {

func (h *BindHandle) deleteBindInfoSQL(normdOrigSQL, db, bindSQL string) string {
sql := fmt.Sprintf(
`DELETE FROM mysql.bind_info WHERE original_sql=%s AND LOWER(default_db)=%s`,
`DELETE FROM mysql.bind_info WHERE original_sql=%s`,
expression.Quote(normdOrigSQL),
expression.Quote(db),
)
if bindSQL == "" {
return sql
Expand All @@ -601,20 +600,19 @@ func (h *BindHandle) insertBindInfoSQL(orignalSQL string, db string, info Bindin
)
}

// lockBindInfoSQL simulates LOCK TABLE by updating a same row in each pessimistic transaction.
func (h *BindHandle) lockBindInfoSQL() string {
// LockBindInfoSQL simulates LOCK TABLE by updating a same row in each pessimistic transaction.
func (h *BindHandle) LockBindInfoSQL() string {
return fmt.Sprintf("UPDATE mysql.bind_info SET source=%s WHERE original_sql=%s",
expression.Quote(Builtin),
expression.Quote(BuiltinPseudoSQL4BindLock))
}

func (h *BindHandle) logicalDeleteBindInfoSQL(originalSQL, db string, updateTs types.Time, bindingSQL string) string {
updateTsStr := updateTs.String()
sql := fmt.Sprintf(`UPDATE mysql.bind_info SET status=%s,update_time=%s WHERE original_sql=%s and LOWER(default_db)=%s and update_time<%s`,
sql := fmt.Sprintf(`UPDATE mysql.bind_info SET status=%s,update_time=%s WHERE original_sql=%s and update_time<%s`,
expression.Quote(deleted),
expression.Quote(updateTsStr),
expression.Quote(originalSQL),
expression.Quote(db),
expression.Quote(updateTsStr))
if bindingSQL == "" {
return sql
Expand All @@ -635,12 +633,12 @@ func (h *BindHandle) CaptureBaselines() {
if insertStmt, ok := stmt.(*ast.InsertStmt); ok && insertStmt.Select == nil {
continue
}
normalizedSQL, digest := parser.NormalizeDigest(bindableStmt.Query)
dbName := utilparser.GetDefaultDB(stmt, bindableStmt.Schema)
normalizedSQL, digest := parser.NormalizeDigest(utilparser.RestoreWithDefaultDB(stmt, dbName))
if r := h.GetBindRecord(digest, normalizedSQL, dbName); r != nil && r.HasUsingBinding() {
continue
}
bindSQL := GenerateBindSQL(context.TODO(), stmt, bindableStmt.PlanHint, true)
bindSQL := GenerateBindSQL(context.TODO(), stmt, bindableStmt.PlanHint, true, dbName)
if bindSQL == "" {
continue
}
Expand Down Expand Up @@ -680,7 +678,7 @@ func getHintsForSQL(sctx sessionctx.Context, sql string) (string, error) {
}

// GenerateBindSQL generates binding sqls from stmt node and plan hints.
func GenerateBindSQL(ctx context.Context, stmtNode ast.StmtNode, planHint string, captured bool) string {
func GenerateBindSQL(ctx context.Context, stmtNode ast.StmtNode, planHint string, captured bool, defaultDB string) string {
// If would be nil for very simple cases such as point get, we do not need to evolve for them.
if planHint == "" {
return ""
Expand All @@ -699,6 +697,7 @@ func GenerateBindSQL(ctx context.Context, stmtNode ast.StmtNode, planHint string
hint.BindHint(stmtNode, &hint.HintsSet{})
var sb strings.Builder
restoreCtx := format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)
restoreCtx.DefaultDB = defaultDB
err := stmtNode.Restore(restoreCtx)
if err != nil {
logutil.Logger(ctx).Debug("[sql-bind] restore SQL failed when generating bind SQL", zap.Error(err))
Expand Down
2 changes: 1 addition & 1 deletion bindinfo/session_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (h *SessionHandle) GetBindRecord(normdOrigSQL, db string) *BindRecord {
hash := parser.DigestNormalized(normdOrigSQL)
bindRecords := h.ch[hash]
for _, bindRecord := range bindRecords {
if bindRecord.OriginalSQL == normdOrigSQL && bindRecord.Db == db {
if bindRecord.OriginalSQL == normdOrigSQL {
return bindRecord
}
}
Expand Down
2 changes: 1 addition & 1 deletion ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ func adjustColumnInfoInModifyColumn(
func checkAndApplyNewAutoRandomBits(job *model.Job, t *meta.Meta, tblInfo *model.TableInfo,
newCol *model.ColumnInfo, oldName *model.CIStr, newAutoRandBits uint64) error {
schemaID := job.SchemaID
newLayout := autoid.NewAutoRandomIDLayout(&newCol.FieldType, newAutoRandBits)
newLayout := autoid.NewShardIDLayout(&newCol.FieldType, newAutoRandBits)

// GenAutoRandomID first to prevent concurrent update.
_, err := t.GenAutoRandomID(schemaID, tblInfo.ID, 1)
Expand Down
46 changes: 23 additions & 23 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func (s *testSerialDBSuite) TestAddExpressionIndexRollback(c *C) {
_, checkErr = tk1.Exec("use test_db")

d := s.dom.DDL()
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: s.dom}
var currJob *model.Job
ctx := mock.NewContext()
ctx.Store = s.store
Expand Down Expand Up @@ -447,7 +447,7 @@ LOOP:
func (s *testDBSuite5) TestCancelAddPrimaryKey(c *C) {
idxName := "primary"
addIdxSQL := "alter table t1 add primary key idx_c2 (c2);"
testCancelAddIndex(c, s.store, s.dom.DDL(), s.lease, idxName, addIdxSQL, "")
testCancelAddIndex(c, s.store, s.dom.DDL(), s.lease, idxName, addIdxSQL, "", s.dom)

// Check the column's flag when the "add primary key" failed.
tk := testkit.NewTestKit(c, s.store)
Expand All @@ -463,14 +463,14 @@ func (s *testDBSuite5) TestCancelAddPrimaryKey(c *C) {
func (s *testDBSuite3) TestCancelAddIndex(c *C) {
idxName := "c3_index "
addIdxSQL := "create unique index c3_index on t1 (c3)"
testCancelAddIndex(c, s.store, s.dom.DDL(), s.lease, idxName, addIdxSQL, "")
testCancelAddIndex(c, s.store, s.dom.DDL(), s.lease, idxName, addIdxSQL, "", s.dom)

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test_db")
tk.MustExec("drop table t1")
}

func testCancelAddIndex(c *C, store kv.Storage, d ddl.DDL, lease time.Duration, idxName, addIdxSQL, sqlModeSQL string) {
func testCancelAddIndex(c *C, store kv.Storage, d ddl.DDL, lease time.Duration, idxName, addIdxSQL, sqlModeSQL string, dom *domain.Domain) {
tk := testkit.NewTestKit(c, store)
tk.MustExec("use test_db")
tk.MustExec("drop table if exists t1")
Expand All @@ -492,7 +492,7 @@ func testCancelAddIndex(c *C, store kv.Storage, d ddl.DDL, lease time.Duration,
}

var c3IdxInfo *model.IndexInfo
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: dom}
originBatchSize := tk.MustQuery("select @@global.tidb_ddl_reorg_batch_size")
// Set batch size to lower try to slow down add-index reorganization, This if for hook to cancel this ddl job.
tk.MustExec("set @@global.tidb_ddl_reorg_batch_size = 32")
Expand Down Expand Up @@ -558,7 +558,7 @@ func (s *testDBSuite4) TestCancelAddIndex1(c *C) {
}

var checkErr error
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: s.dom}
hook.OnJobRunBeforeExported = func(job *model.Job) {
if job.Type == model.ActionAddIndex && job.State == model.JobStateRunning && job.SchemaState == model.StateWriteReorganization && job.SnapshotVer == 0 {
jobIDs := []int64{job.ID}
Expand Down Expand Up @@ -612,19 +612,19 @@ func (s *testDBSuite4) TestCancelDropPrimaryKey(c *C) {
idxName := "primary"
addIdxSQL := "alter table t add primary key idx_c2 (c2);"
dropIdxSQL := "alter table t drop primary key;"
testCancelDropIndex(c, s.store, s.dom.DDL(), idxName, addIdxSQL, dropIdxSQL)
testCancelDropIndex(c, s.store, s.dom.DDL(), idxName, addIdxSQL, dropIdxSQL, s.dom)
}

// TestCancelDropIndex tests cancel ddl job which type is drop index.
func (s *testDBSuite5) TestCancelDropIndex(c *C) {
idxName := "idx_c2"
addIdxSQL := "alter table t add index idx_c2 (c2);"
dropIdxSQL := "alter table t drop index idx_c2;"
testCancelDropIndex(c, s.store, s.dom.DDL(), idxName, addIdxSQL, dropIdxSQL)
testCancelDropIndex(c, s.store, s.dom.DDL(), idxName, addIdxSQL, dropIdxSQL, s.dom)
}

// testCancelDropIndex tests cancel ddl job which type is drop index.
func testCancelDropIndex(c *C, store kv.Storage, d ddl.DDL, idxName, addIdxSQL, dropIdxSQL string) {
func testCancelDropIndex(c *C, store kv.Storage, d ddl.DDL, idxName, addIdxSQL, dropIdxSQL string, dom *domain.Domain) {
tk := testkit.NewTestKit(c, store)
tk.MustExec("use test_db")
tk.MustExec("drop table if exists t")
Expand All @@ -647,7 +647,7 @@ func testCancelDropIndex(c *C, store kv.Storage, d ddl.DDL, idxName, addIdxSQL,
{true, model.JobStateRunning, model.StateDeleteReorganization, false},
}
var checkErr error
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: dom}
var jobID int64
testCase := &testCases[0]
hook.OnJobRunBeforeExported = func(job *model.Job) {
Expand Down Expand Up @@ -722,7 +722,7 @@ func (s *testDBSuite5) TestCancelTruncateTable(c *C) {
s.mustExec(tk, c, "create table t(c1 int, c2 int)")
defer s.mustExec(tk, c, "drop table t;")
var checkErr error
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: s.dom}
hook.OnJobRunBeforeExported = func(job *model.Job) {
if job.Type == model.ActionTruncateTable && job.State == model.JobStateNone {
jobIDs := []int64{job.ID}
Expand Down Expand Up @@ -765,7 +765,7 @@ func (s *testDBSuite5) TestParallelDropSchemaAndDropTable(c *C) {
s.mustExec(tk, c, "use test_drop_schema_table")
s.mustExec(tk, c, "create table t(c1 int, c2 int)")
var checkErr error
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: s.dom}
dbInfo := testGetSchemaByName(c, tk.Se, "test_drop_schema_table")
done := false
var wg sync.WaitGroup
Expand Down Expand Up @@ -827,7 +827,7 @@ func (s *testDBSuite1) TestCancelRenameIndex(c *C) {
}
s.mustExec(tk, c, "alter table t add index idx_c2(c2)")
var checkErr error
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: s.dom}
hook.OnJobRunBeforeExported = func(job *model.Job) {
if job.Type == model.ActionRenameIndex && job.State == model.JobStateNone {
jobIDs := []int64{job.ID}
Expand Down Expand Up @@ -894,7 +894,7 @@ func (s *testDBSuite2) TestCancelDropTableAndSchema(c *C) {
{true, model.ActionDropSchema, model.JobStateRunning, model.StateDeleteOnly, false},
}
var checkErr error
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: s.dom}
var jobID int64
testCase := &testCases[0]
s.mustExec(tk, c, "create database if not exists test_drop_db")
Expand Down Expand Up @@ -1336,7 +1336,7 @@ func (s *testDBSuite1) TestCancelAddTableAndDropTablePartition(c *C) {
{model.ActionAddTablePartition, model.JobStateRunning, model.StateReplicaOnly, true},
}
var checkErr error
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: s.dom}
testCase := &testCases[0]
var jobID int64
hook.OnJobRunBeforeExported = func(job *model.Job) {
Expand Down Expand Up @@ -1507,7 +1507,7 @@ func (s *testDBSuite3) TestCancelDropColumn(c *C) {
{true, model.JobStateRunning, model.StateDeleteReorganization, false},
}
var checkErr error
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: s.dom}
var jobID int64
testCase := &testCases[0]
hook.OnJobRunBeforeExported = func(job *model.Job) {
Expand Down Expand Up @@ -1614,7 +1614,7 @@ func (s *testDBSuite3) TestCancelDropColumns(c *C) {
{true, model.JobStateRunning, model.StateDeleteReorganization, false},
}
var checkErr error
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: s.dom}
var jobID int64
testCase := &testCases[0]
hook.OnJobRunBeforeExported = func(job *model.Job) {
Expand Down Expand Up @@ -2465,7 +2465,7 @@ func (s *testSerialDBSuite) TestCreateTableWithLike2(c *C) {

tbl1 := testGetTableByName(c, s.s, "test_db", "t1")
doneCh := make(chan error, 2)
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: s.dom}
var onceChecker sync.Map
hook.OnJobRunBeforeExported = func(job *model.Job) {
if job.Type != model.ActionAddColumn && job.Type != model.ActionDropColumn &&
Expand Down Expand Up @@ -2745,7 +2745,7 @@ func (s *testSerialDBSuite) TestRepairTable(c *C) {
// Repaired tableInfo has been filtered by `domain.InfoSchema()`, so get it in repairInfo.
originTableInfo, _ := domainutil.RepairInfo.GetRepairedTableInfoByTableName("test", "origin")

hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: s.dom}
var repairErr error
hook.OnJobRunBeforeExported = func(job *model.Job) {
if job.Type != model.ActionRepairTable {
Expand Down Expand Up @@ -3832,7 +3832,7 @@ func (s *testDBSuite5) TestModifyColumnRollBack(c *C) {

var c2 *table.Column
var checkErr error
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: s.dom}
hook.OnJobUpdatedExported = func(job *model.Job) {
if checkErr != nil {
return
Expand Down Expand Up @@ -3930,7 +3930,7 @@ func (s *testSerialDBSuite) TestModifyColumnnReorgInfo(c *C) {
defer s.dom.DDL().(ddl.DDLForTest).SetHook(originalHook)

// Check insert null before job first update.
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: s.dom}
var checkErr error
var currJob *model.Job
var elements []*meta.Element
Expand Down Expand Up @@ -4155,7 +4155,7 @@ func testModifyColumnNullToNotNull(c *C, s *testDBSuite, enableChangeColumnType

// Check insert null before job first update.
times := 0
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: s.dom}
tk.MustExec("delete from t1")
var checkErr error
hook.OnJobRunBeforeExported = func(job *model.Job) {
Expand Down Expand Up @@ -4284,7 +4284,7 @@ func (s *testDBSuite3) TestTransactionWithWriteOnlyColumn(c *C) {

originHook := s.dom.DDL().GetHook()
defer s.dom.DDL().(ddl.DDLForTest).SetHook(originHook)
hook := &ddl.TestDDLCallback{}
hook := &ddl.TestDDLCallback{Do: s.dom}
var checkErr error
hook.OnJobRunBeforeExported = func(job *model.Job) {
if checkErr != nil {
Expand Down
4 changes: 2 additions & 2 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ func setTableAutoRandomBits(ctx sessionctx.Context, tbInfo *model.TableInfo, col
return errors.Trace(err)
}

layout := autoid.NewAutoRandomIDLayout(col.Tp, autoRandBits)
layout := autoid.NewShardIDLayout(col.Tp, autoRandBits)
if autoRandBits == 0 {
return ErrInvalidAutoRandom.GenWithStackByArgs(autoid.AutoRandomNonPositive)
} else if autoRandBits > autoid.MaxAutoRandomBits {
Expand Down Expand Up @@ -2526,7 +2526,7 @@ func (d *ddl) RebaseAutoID(ctx sessionctx.Context, ident ast.Ident, newBase int6
break
}
}
layout := autoid.NewAutoRandomIDLayout(&autoRandColTp, tbInfo.AutoRandomBits)
layout := autoid.NewShardIDLayout(&autoRandColTp, tbInfo.AutoRandomBits)
if layout.IncrementalMask()&newBase != newBase {
errMsg := fmt.Sprintf(autoid.AutoRandomRebaseOverflow, newBase, layout.IncrementalBitsCapacity())
return errors.Trace(ErrInvalidAutoRandom.GenWithStackByArgs(errMsg))
Expand Down
Loading

0 comments on commit 6fa4644

Please sign in to comment.