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

ddl: fix ErrGCTooEarly when adding an index to the partition table (#14029) #14132

Merged
merged 4 commits into from
Dec 19, 2019
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
21 changes: 21 additions & 0 deletions ddl/failtest/fail_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,24 @@ func (s *testFailDBSuite) TestRunDDLJobPanic(c *C) {
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[ddl:12]cancelled DDL job")
}

func (s *testFailDBSuite) TestPartitionAddIndexGC(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec(`create table partition_add_idx (
id int not null,
hired date not null
)
partition by range( year(hired) ) (
partition p1 values less than (1991),
partition p5 values less than (2008),
partition p7 values less than (2018)
);`)
tk.MustExec("insert into partition_add_idx values(1, '2010-01-01'), (2, '1990-01-01'), (3, '2001-01-01')")

c.Assert(failpoint.Enable("github.com/pingcap/tidb/ddl/mockUpdateCachedSafePoint", `return(true)`), IsNil)
defer func() {
c.Assert(failpoint.Disable("github.com/pingcap/tidb/ddl/mockUpdateCachedSafePoint"), IsNil)
}()
tk.MustExec("alter table partition_add_idx add index idx (id, hired)")
}
16 changes: 15 additions & 1 deletion ddl/index.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/tablecodec"
Expand Down Expand Up @@ -1423,7 +1424,20 @@ func (w *worker) updateReorgInfo(t table.PartitionedTable, reorg *reorgInfo) (bo
return true, nil
}

start, end, err := getTableRange(reorg.d, t.GetPartition(pid), reorg.Job.SnapshotVer, reorg.Job.Priority)
failpoint.Inject("mockUpdateCachedSafePoint", func(val failpoint.Value) {
if val.(bool) {
// 18 is for the logical time.
ts := oracle.GetPhysical(time.Now()) << 18
s := reorg.d.store.(tikv.Storage)
s.UpdateSPCache(uint64(ts), time.Now())
time.Sleep(time.Millisecond * 3)
}
})
currentVer, err := getValidCurrentVersion(reorg.d.store)
if err != nil {
return false, errors.Trace(err)
}
start, end, err := getTableRange(reorg.d, t.GetPartition(pid), currentVer.Ver, reorg.Job.Priority)
if err != nil {
return false, errors.Trace(err)
}
Expand Down
19 changes: 13 additions & 6 deletions ddl/reorg.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,18 @@ func getTableRange(d *ddlCtx, tbl table.PhysicalTable, snapshotVer uint64, prior
return
}

func getValidCurrentVersion(store kv.Storage) (ver kv.Version, err error) {
ver, err = store.CurrentVersion()
if err != nil {
return ver, errors.Trace(err)
} else if ver.Ver <= 0 {
return ver, errInvalidStoreVer.GenWithStack("invalid storage current version %d", ver.Ver)
}
return ver, nil
}

func getReorgInfo(d *ddlCtx, t *meta.Meta, job *model.Job, tbl table.Table) (*reorgInfo, error) {
var (
err error
start int64
end int64
pid int64
Expand All @@ -352,12 +361,9 @@ func getReorgInfo(d *ddlCtx, t *meta.Meta, job *model.Job, tbl table.Table) (*re
if job.SnapshotVer == 0 {
info.first = true
// get the current version for reorganization if we don't have
var ver kv.Version
ver, err = d.store.CurrentVersion()
ver, err := getValidCurrentVersion(d.store)
if err != nil {
return nil, errors.Trace(err)
} else if ver.Ver <= 0 {
return nil, errInvalidStoreVer.GenWithStack("invalid storage current version %d", ver.Ver)
}
tblInfo := tbl.Meta()
pid = tblInfo.ID
Expand All @@ -384,6 +390,7 @@ func getReorgInfo(d *ddlCtx, t *meta.Meta, job *model.Job, tbl table.Table) (*re
// Update info should after data persistent.
job.SnapshotVer = ver.Ver
} else {
var err error
start, end, pid, err = t.GetDDLReorgHandle(job)
if err != nil {
return nil, errors.Trace(err)
Expand All @@ -395,7 +402,7 @@ func getReorgInfo(d *ddlCtx, t *meta.Meta, job *model.Job, tbl table.Table) (*re
info.EndHandle = end
info.PhysicalTableID = pid

return &info, errors.Trace(err)
return &info, nil
}

func (r *reorgInfo) UpdateReorgMeta(txn kv.Transaction, startHandle, endHandle, physicalTableID int64) error {
Expand Down