Skip to content

Commit

Permalink
cherry pick pingcap#14342 to release-3.0
Browse files Browse the repository at this point in the history
Signed-off-by: sre-bot <sre-bot@pingcap.com>
  • Loading branch information
zimulala authored and sre-bot committed Mar 5, 2020
1 parent 3bfee86 commit 71480f6
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 114 deletions.
16 changes: 11 additions & 5 deletions cmd/ddltest/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
zaplog "github.com/pingcap/log"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/session"
Expand Down Expand Up @@ -104,9 +105,8 @@ func (s *TestDDLSuite) SetUpSuite(c *C) {
// Make sure the schema lease of this session is equal to other TiDB servers'.
session.SetSchemaLease(time.Duration(*lease) * time.Second)

dom, err := session.BootstrapSession(s.store)
s.dom, err = session.BootstrapSession(s.store)
c.Assert(err, IsNil)
s.dom = dom

s.s, err = session.CreateSession(s.store)
c.Assert(err, IsNil)
Expand All @@ -116,14 +116,20 @@ func (s *TestDDLSuite) SetUpSuite(c *C) {
_, err = s.s.Execute(goCtx, "create database if not exists test_ddl")
c.Assert(err, IsNil)

_, err = s.s.Execute(goCtx, "use test_ddl")
c.Assert(err, IsNil)

s.Bootstrap(c)

// Stop current DDL worker, so that we can't be the owner now.
err = domain.GetDomain(s.ctx).DDL().Stop()
c.Assert(err, IsNil)
ddl.RunWorker = false
session.ResetStoreForWithTiKVTest(s.store)
s.s, err = session.CreateSession(s.store)
c.Assert(err, IsNil)
s.dom, err = session.BootstrapSession(s.store)
c.Assert(err, IsNil)
s.ctx = s.s.(sessionctx.Context)
_, err = s.s.Execute(goCtx, "use test_ddl")
c.Assert(err, IsNil)

addEnvPath("..")

Expand Down
4 changes: 0 additions & 4 deletions ddl/column_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ func (s *testColumnChangeSuite) TestColumnChange(c *C) {
}

func (s *testColumnChangeSuite) testAddColumnNoDefault(c *C, ctx sessionctx.Context, d *ddl, tblInfo *model.TableInfo) {
d.Stop()
tc := &TestDDLCallback{}
// set up hook
prevState := model.StateNone
Expand Down Expand Up @@ -190,14 +189,12 @@ func (s *testColumnChangeSuite) testAddColumnNoDefault(c *C, ctx sessionctx.Cont
}
}
d.SetHook(tc)
d.start(context.Background(), nil)
job := testCreateColumn(c, ctx, d, s.dbInfo, tblInfo, "c3", &ast.ColumnPosition{Tp: ast.ColumnPositionNone}, nil)
c.Assert(errors.ErrorStack(checkErr), Equals, "")
testCheckJobDone(c, d, job, true)
}

func (s *testColumnChangeSuite) testColumnDrop(c *C, ctx sessionctx.Context, d *ddl, tbl table.Table) {
d.Stop()
dropCol := tbl.Cols()[2]
tc := &TestDDLCallback{}
// set up hook
Expand All @@ -219,7 +216,6 @@ func (s *testColumnChangeSuite) testColumnDrop(c *C, ctx sessionctx.Context, d *
}
}
d.SetHook(tc)
d.start(context.Background(), nil)
c.Assert(errors.ErrorStack(checkErr), Equals, "")
testDropColumn(c, ctx, d, s.dbInfo, tbl.Meta(), dropCol.Name.L, false)
}
Expand Down
126 changes: 66 additions & 60 deletions ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,26 @@ var _ = Suite(&testColumnSuite{})
type testColumnSuite struct {
store kv.Storage
dbInfo *model.DBInfo

d *ddl
}

func (s *testColumnSuite) SetUpSuite(c *C) {
s.store = testCreateStore(c, "test_column")
<<<<<<< HEAD
s.d = testNewDDL(context.Background(), nil, s.store, nil, nil, testLease)

s.dbInfo = testSchemaInfo(c, s.d, "test_column")
testCreateSchema(c, testNewContext(s.d), s.d, s.dbInfo)
=======
d := newDDL(
context.Background(),
WithStore(s.store),
WithLease(testLease),
)
>>>>>>> 6ccdf64... ddl: add a channel to limit multiple DDL jobs writing at the same time (#14342)

s.dbInfo = testSchemaInfo(c, d, "test_column")
testCreateSchema(c, testNewContext(d), d, s.dbInfo)
d.Stop()
}

func (s *testColumnSuite) TearDownSuite(c *C) {
testDropSchema(c, testNewContext(s.d), s.d, s.dbInfo)
s.d.Stop()

err := s.store.Close()
c.Assert(err, IsNil)
}
Expand Down Expand Up @@ -114,11 +118,18 @@ func testDropColumn(c *C, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo,
}

func (s *testColumnSuite) TestColumn(c *C) {
tblInfo := testTableInfo(c, s.d, "t1", 3)
ctx := testNewContext(s.d)
d := newDDL(
context.Background(),
WithStore(s.store),
WithLease(testLease),
)
defer d.Stop()

tblInfo := testTableInfo(c, d, "t1", 3)
ctx := testNewContext(d)

testCreateTable(c, ctx, s.d, s.dbInfo, tblInfo)
t := testGetTable(c, s.d, s.dbInfo.ID, tblInfo.ID)
testCreateTable(c, ctx, d, s.dbInfo, tblInfo)
t := testGetTable(c, d, s.dbInfo.ID, tblInfo.ID)

num := 10
for i := 0; i < num; i++ {
Expand All @@ -143,10 +154,10 @@ func (s *testColumnSuite) TestColumn(c *C) {

c.Assert(table.FindCol(t.Cols(), "c4"), IsNil)

job := testCreateColumn(c, ctx, s.d, s.dbInfo, tblInfo, "c4", &ast.ColumnPosition{Tp: ast.ColumnPositionAfter, RelativeColumn: &ast.ColumnName{Name: model.NewCIStr("c3")}}, 100)
testCheckJobDone(c, s.d, job, true)
job := testCreateColumn(c, ctx, d, s.dbInfo, tblInfo, "c4", &ast.ColumnPosition{Tp: ast.ColumnPositionAfter, RelativeColumn: &ast.ColumnName{Name: model.NewCIStr("c3")}}, 100)
testCheckJobDone(c, d, job, true)

t = testGetTable(c, s.d, s.dbInfo.ID, tblInfo.ID)
t = testGetTable(c, d, s.dbInfo.ID, tblInfo.ID)
c.Assert(table.FindCol(t.Cols(), "c4"), NotNil)

i = int64(0)
Expand All @@ -173,40 +184,40 @@ func (s *testColumnSuite) TestColumn(c *C) {
c.Assert(values, HasLen, 4)
c.Assert(values[3].GetInt64(), Equals, int64(14))

job = testDropColumn(c, ctx, s.d, s.dbInfo, tblInfo, "c4", false)
testCheckJobDone(c, s.d, job, false)
job = testDropColumn(c, ctx, d, s.dbInfo, tblInfo, "c4", false)
testCheckJobDone(c, d, job, false)

t = testGetTable(c, s.d, s.dbInfo.ID, tblInfo.ID)
t = testGetTable(c, d, s.dbInfo.ID, tblInfo.ID)
values, err = t.RowWithCols(ctx, h, t.Cols())
c.Assert(err, IsNil)

c.Assert(values, HasLen, 3)
c.Assert(values[2].GetInt64(), Equals, int64(13))

job = testCreateColumn(c, ctx, s.d, s.dbInfo, tblInfo, "c4", &ast.ColumnPosition{Tp: ast.ColumnPositionNone}, 111)
testCheckJobDone(c, s.d, job, true)
job = testCreateColumn(c, ctx, d, s.dbInfo, tblInfo, "c4", &ast.ColumnPosition{Tp: ast.ColumnPositionNone}, 111)
testCheckJobDone(c, d, job, true)

t = testGetTable(c, s.d, s.dbInfo.ID, tblInfo.ID)
t = testGetTable(c, d, s.dbInfo.ID, tblInfo.ID)
values, err = t.RowWithCols(ctx, h, t.Cols())
c.Assert(err, IsNil)

c.Assert(values, HasLen, 4)
c.Assert(values[3].GetInt64(), Equals, int64(111))

job = testCreateColumn(c, ctx, s.d, s.dbInfo, tblInfo, "c5", &ast.ColumnPosition{Tp: ast.ColumnPositionNone}, 101)
testCheckJobDone(c, s.d, job, true)
job = testCreateColumn(c, ctx, d, s.dbInfo, tblInfo, "c5", &ast.ColumnPosition{Tp: ast.ColumnPositionNone}, 101)
testCheckJobDone(c, d, job, true)

t = testGetTable(c, s.d, s.dbInfo.ID, tblInfo.ID)
t = testGetTable(c, d, s.dbInfo.ID, tblInfo.ID)
values, err = t.RowWithCols(ctx, h, t.Cols())
c.Assert(err, IsNil)

c.Assert(values, HasLen, 5)
c.Assert(values[4].GetInt64(), Equals, int64(101))

job = testCreateColumn(c, ctx, s.d, s.dbInfo, tblInfo, "c6", &ast.ColumnPosition{Tp: ast.ColumnPositionFirst}, 202)
testCheckJobDone(c, s.d, job, true)
job = testCreateColumn(c, ctx, d, s.dbInfo, tblInfo, "c6", &ast.ColumnPosition{Tp: ast.ColumnPositionFirst}, 202)
testCheckJobDone(c, d, job, true)

t = testGetTable(c, s.d, s.dbInfo.ID, tblInfo.ID)
t = testGetTable(c, d, s.dbInfo.ID, tblInfo.ID)
cols := t.Cols()
c.Assert(cols, HasLen, 6)
c.Assert(cols[0].Offset, Equals, 0)
Expand All @@ -229,40 +240,40 @@ func (s *testColumnSuite) TestColumn(c *C) {
c.Assert(values[0].GetInt64(), Equals, int64(202))
c.Assert(values[5].GetInt64(), Equals, int64(101))

job = testDropColumn(c, ctx, s.d, s.dbInfo, tblInfo, "c2", false)
testCheckJobDone(c, s.d, job, false)
job = testDropColumn(c, ctx, d, s.dbInfo, tblInfo, "c2", false)
testCheckJobDone(c, d, job, false)

t = testGetTable(c, s.d, s.dbInfo.ID, tblInfo.ID)
t = testGetTable(c, d, s.dbInfo.ID, tblInfo.ID)

values, err = t.RowWithCols(ctx, h, t.Cols())
c.Assert(err, IsNil)
c.Assert(values, HasLen, 5)
c.Assert(values[0].GetInt64(), Equals, int64(202))
c.Assert(values[4].GetInt64(), Equals, int64(101))

job = testDropColumn(c, ctx, s.d, s.dbInfo, tblInfo, "c1", false)
testCheckJobDone(c, s.d, job, false)
job = testDropColumn(c, ctx, d, s.dbInfo, tblInfo, "c1", false)
testCheckJobDone(c, d, job, false)

job = testDropColumn(c, ctx, s.d, s.dbInfo, tblInfo, "c3", false)
testCheckJobDone(c, s.d, job, false)
job = testDropColumn(c, ctx, d, s.dbInfo, tblInfo, "c3", false)
testCheckJobDone(c, d, job, false)

job = testDropColumn(c, ctx, s.d, s.dbInfo, tblInfo, "c4", false)
testCheckJobDone(c, s.d, job, false)
job = testDropColumn(c, ctx, d, s.dbInfo, tblInfo, "c4", false)
testCheckJobDone(c, d, job, false)

job = testCreateIndex(c, ctx, s.d, s.dbInfo, tblInfo, false, "c5_idx", "c5")
testCheckJobDone(c, s.d, job, true)
job = testCreateIndex(c, ctx, d, s.dbInfo, tblInfo, false, "c5_idx", "c5")
testCheckJobDone(c, d, job, true)

testDropColumn(c, ctx, s.d, s.dbInfo, tblInfo, "c5", true)
testDropColumn(c, ctx, d, s.dbInfo, tblInfo, "c5", true)

testDropIndex(c, ctx, s.d, s.dbInfo, tblInfo, "c5_idx")
testCheckJobDone(c, s.d, job, true)
testDropIndex(c, ctx, d, s.dbInfo, tblInfo, "c5_idx")
testCheckJobDone(c, d, job, true)

job = testDropColumn(c, ctx, s.d, s.dbInfo, tblInfo, "c5", false)
testCheckJobDone(c, s.d, job, false)
job = testDropColumn(c, ctx, d, s.dbInfo, tblInfo, "c5", false)
testCheckJobDone(c, d, job, false)

testDropColumn(c, ctx, s.d, s.dbInfo, tblInfo, "c6", true)
testDropColumn(c, ctx, d, s.dbInfo, tblInfo, "c6", true)

testDropTable(c, ctx, s.d, s.dbInfo, tblInfo)
testDropTable(c, ctx, d, s.dbInfo, tblInfo)
}

func (s *testColumnSuite) checkColumnKVExist(ctx sessionctx.Context, t table.Table, handle int64, col *table.Column, columnValue interface{}, isExist bool) error {
Expand Down Expand Up @@ -810,12 +821,6 @@ func (s *testColumnSuite) TestAddColumn(c *C) {

d.SetHook(tc)

// Use local ddl for callback test.
s.d.Stop()

d.Stop()
d.start(context.Background(), nil)

job := testCreateColumn(c, ctx, d, s.dbInfo, tblInfo, newColName, &ast.ColumnPosition{Tp: ast.ColumnPositionNone}, defaultColValue)

testCheckJobDone(c, d, job, true)
Expand All @@ -838,12 +843,20 @@ func (s *testColumnSuite) TestAddColumn(c *C) {
c.Assert(err, IsNil)

d.Stop()
s.d.start(context.Background(), nil)
}

func (s *testColumnSuite) TestDropColumn(c *C) {
<<<<<<< HEAD
d := testNewDDL(context.Background(), nil, s.store, nil, nil, testLease)
tblInfo := testTableInfo(c, d, "t", 4)
=======
d := newDDL(
context.Background(),
WithStore(s.store),
WithLease(testLease),
)
tblInfo := testTableInfo(c, d, "t2", 4)
>>>>>>> 6ccdf64... ddl: add a channel to limit multiple DDL jobs writing at the same time (#14342)
ctx := testNewContext(d)

err := ctx.NewTxn(context.Background())
Expand Down Expand Up @@ -888,13 +901,7 @@ func (s *testColumnSuite) TestDropColumn(c *C) {

d.SetHook(tc)

// Use local ddl for callback test.
s.d.Stop()

d.Stop()
d.start(context.Background(), nil)

job := testDropColumn(c, ctx, s.d, s.dbInfo, tblInfo, colName, false)
job := testDropColumn(c, ctx, d, s.dbInfo, tblInfo, colName, false)
testCheckJobDone(c, d, job, false)
mu.Lock()
hErr := hookErr
Expand All @@ -915,7 +922,6 @@ func (s *testColumnSuite) TestDropColumn(c *C) {
c.Assert(err, IsNil)

d.Stop()
s.d.start(context.Background(), nil)
}

func (s *testColumnSuite) TestModifyColumn(c *C) {
Expand Down
Loading

0 comments on commit 71480f6

Please sign in to comment.