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

[release-1.0]ddl: update the column's offset when we do modify column #6269

Merged
merged 2 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 9 additions & 4 deletions ddl/callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ import (
type TestDDLCallback struct {
*BaseCallback

onJobRunBefore func(*model.Job)
onJobUpdated func(*model.Job)
OnJobUpdatedExported func(*model.Job)
onWatched func(ctx goctx.Context)
onJobRunBefore func(*model.Job)
OnJobRunBeforeExported func(*model.Job)
onJobUpdated func(*model.Job)
OnJobUpdatedExported func(*model.Job)
onWatched func(ctx goctx.Context)
}

func (tc *TestDDLCallback) OnJobRunBefore(job *model.Job) {
if tc.OnJobRunBeforeExported != nil {
tc.OnJobRunBeforeExported(job)
return
}
if tc.onJobRunBefore != nil {
tc.onJobRunBefore(job)
return
Expand Down
11 changes: 7 additions & 4 deletions ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func (d *ddl) onModifyColumn(t *meta.Meta, job *model.Job) (ver int64, _ error)
}

// doModifyColumn updates the column information and reorders all columns.
func (d *ddl) doModifyColumn(t *meta.Meta, job *model.Job, col *model.ColumnInfo, oldName *model.CIStr, pos *ast.ColumnPosition) (ver int64, _ error) {
func (d *ddl) doModifyColumn(t *meta.Meta, job *model.Job, newCol *model.ColumnInfo, oldName *model.CIStr, pos *ast.ColumnPosition) (ver int64, _ error) {
tblInfo, err := getTableInfo(t, job, job.SchemaID)
if err != nil {
return ver, errors.Trace(err)
Expand All @@ -450,6 +450,9 @@ func (d *ddl) doModifyColumn(t *meta.Meta, job *model.Job, col *model.ColumnInfo
return ver, infoschema.ErrColumnNotExists.GenByArgs(oldName, tblInfo.Name)
}

// We need the latest column's offset and state. This information can be obtained from the store.
Copy link
Contributor

@winkyao winkyao Apr 12, 2018

Choose a reason for hiding this comment

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

We can't set the offset in ddl.ModifyColumn, because the offset may be changed when we execute the ddl.doModifyColumn.

newCol.Offset = oldCol.Offset
newCol.State = oldCol.State
// Calculate column's new position.
oldPos, newPos := oldCol.Offset, oldCol.Offset
if pos.Tp == ast.ColumnPositionAfter {
Expand All @@ -475,10 +478,10 @@ func (d *ddl) doModifyColumn(t *meta.Meta, job *model.Job, col *model.ColumnInfo
}

columnChanged := make(map[string]*model.ColumnInfo)
columnChanged[oldName.L] = col
columnChanged[oldName.L] = newCol

if newPos == oldPos {
tblInfo.Columns[newPos] = col
tblInfo.Columns[newPos] = newCol
} else {
cols := tblInfo.Columns

Expand All @@ -488,7 +491,7 @@ func (d *ddl) doModifyColumn(t *meta.Meta, job *model.Job, col *model.ColumnInfo
} else {
copy(cols[oldPos:], cols[oldPos+1:newPos+1])
}
cols[newPos] = col
cols[newPos] = newCol

for i, col := range tblInfo.Columns {
if col.Offset != i {
Expand Down
2 changes: 0 additions & 2 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1260,8 +1260,6 @@ func (d *ddl) getModifiableColumnJob(ctx context.Context, ident ast.Ident, origi

newCol := table.ToColumn(&model.ColumnInfo{
ID: col.ID,
Offset: col.Offset,
State: col.State,
OriginDefaultValue: col.OriginDefaultValue,
FieldType: *spec.NewColumn.Tp,
Name: spec.NewColumn.Name.Name,
Expand Down
72 changes: 72 additions & 0 deletions ddl/ddl_db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
package ddl_test

import (
"github.com/pingcap/tidb/meta"
"strings"
"sync"
"time"

"github.com/juju/errors"
Expand Down Expand Up @@ -357,3 +359,73 @@ func (s *testStateChangeSuite) runTestInSchemaState(c *C, state model.SchemaStat
callback = &ddl.TestDDLCallback{}
d.SetHook(callback)
}

func (s *testStateChangeSuite) TestParallelDDL(c *C) {
defer testleak.AfterTest(c)()
_, err := s.se.Execute("use test_db_state")
c.Assert(err, IsNil)
_, err = s.se.Execute("create table t(a int, b int, c int)")
c.Assert(err, IsNil)
defer s.se.Execute("drop table t")

callback := &ddl.TestDDLCallback{}
times := 0
callback.OnJobUpdatedExported = func(job *model.Job) {
if times != 0 {
return
}
var qLen int64
var err error
for {
kv.RunInNewTxn(s.store, false, func(txn kv.Transaction) error {
m := meta.NewMeta(txn)
qLen, err = m.DDLJobQueueLen()
if err != nil {
return err
}
return nil
})
if qLen == 2 {
break
}
time.Sleep(5 * time.Millisecond)
}
times++
}
d := s.dom.DDL()
d.SetHook(callback)

wg := sync.WaitGroup{}
var err1 error
var err2 error
se, err := tidb.CreateSession(s.store)
c.Assert(err, IsNil)
_, err = se.Execute("use test_db_state")
c.Assert(err, IsNil)
se1, err := tidb.CreateSession(s.store)
c.Assert(err, IsNil)
_, err = se1.Execute("use test_db_state")
c.Assert(err, IsNil)
go func() {
wg.Add(1)
defer wg.Done()
_, err1 = se.Execute("ALTER TABLE t MODIFY COLUMN b int FIRST;")
}()

go func() {
wg.Add(1)
defer wg.Done()
_, err2 = se1.Execute("ALTER TABLE t MODIFY COLUMN b int FIRST;")
}()

time.Sleep(1 * time.Second)
wg.Wait()
c.Assert(err1, IsNil)
c.Assert(err2, IsNil)

_, err = s.se.Execute("select * from t")
c.Assert(err, IsNil, Commentf("err:%v", errors.ErrorStack(err)))

callback = &ddl.TestDDLCallback{}
d.SetHook(callback)
}