forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ddl: fix can't cancel a job when add index meets panic (pingcap#8621)
- Loading branch information
Showing
4 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2019 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ddl_test | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
. "github.com/pingcap/check" | ||
"github.com/pingcap/errors" | ||
gofail "github.com/pingcap/gofail/runtime" | ||
"github.com/pingcap/parser/model" | ||
"github.com/pingcap/tidb/ddl" | ||
"github.com/pingcap/tidb/domain" | ||
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/session" | ||
"github.com/pingcap/tidb/store/mockstore" | ||
"github.com/pingcap/tidb/util/admin" | ||
"github.com/pingcap/tidb/util/mock" | ||
"github.com/pingcap/tidb/util/testkit" | ||
"github.com/pingcap/tidb/util/testleak" | ||
) | ||
|
||
var _ = SerialSuites(&testSerialSuite{}) | ||
|
||
type testSerialSuite struct { | ||
store kv.Storage | ||
dom *domain.Domain | ||
} | ||
|
||
func (s *testSerialSuite) SetUpSuite(c *C) { | ||
testleak.BeforeTest() | ||
session.SetSchemaLease(200 * time.Millisecond) | ||
session.SetStatsLease(0) | ||
|
||
ddl.WaitTimeWhenErrorOccured = 1 * time.Microsecond | ||
var err error | ||
s.store, err = mockstore.NewMockTikvStore() | ||
c.Assert(err, IsNil) | ||
|
||
s.dom, err = session.BootstrapSession(s.store) | ||
c.Assert(err, IsNil) | ||
} | ||
|
||
func (s *testSerialSuite) TearDownSuite(c *C) { | ||
if s.dom != nil { | ||
s.dom.Close() | ||
} | ||
if s.store != nil { | ||
s.store.Close() | ||
} | ||
testleak.AfterTest(c)() | ||
} | ||
|
||
// TestCancelAddIndex1 tests canceling ddl job when the add index worker is not started. | ||
func (s *testSerialSuite) TestCancelAddIndexPanic(c *C) { | ||
gofail.Enable("github.com/pingcap/tidb/ddl/errorMockPanic", `return(true)`) | ||
defer gofail.Disable("github.com/pingcap/tidb/ddl/errorMockPanic") | ||
tk := testkit.NewTestKit(c, s.store) | ||
tk.MustExec("use test") | ||
tk.MustExec("drop table if exists t") | ||
tk.MustExec("create table t(c1 int, c2 int)") | ||
defer tk.MustExec("drop table t;") | ||
for i := 0; i < 5; i++ { | ||
tk.MustExec("insert into t values (?, ?)", i, i) | ||
} | ||
var checkErr error | ||
oldReorgWaitTimeout := ddl.ReorgWaitTimeout | ||
ddl.ReorgWaitTimeout = 50 * time.Millisecond | ||
defer func() { ddl.ReorgWaitTimeout = oldReorgWaitTimeout }() | ||
hook := &ddl.TestDDLCallback{} | ||
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} | ||
hookCtx := mock.NewContext() | ||
hookCtx.Store = s.store | ||
err := hookCtx.NewTxn(context.Background()) | ||
if err != nil { | ||
checkErr = errors.Trace(err) | ||
return | ||
} | ||
txn, err := hookCtx.Txn(true) | ||
if err != nil { | ||
checkErr = errors.Trace(err) | ||
return | ||
} | ||
errs, err := admin.CancelJobs(txn, jobIDs) | ||
if err != nil { | ||
checkErr = errors.Trace(err) | ||
return | ||
} | ||
if errs[0] != nil { | ||
checkErr = errors.Trace(errs[0]) | ||
return | ||
} | ||
txn, err = hookCtx.Txn(true) | ||
if err != nil { | ||
checkErr = errors.Trace(err) | ||
return | ||
} | ||
checkErr = txn.Commit(context.Background()) | ||
} | ||
} | ||
origHook := s.dom.DDL().GetHook() | ||
defer s.dom.DDL().(ddl.DDLForTest).SetHook(origHook) | ||
s.dom.DDL().(ddl.DDLForTest).SetHook(hook) | ||
rs, err := tk.Exec("alter table t add index idx_c2(c2)") | ||
if rs != nil { | ||
rs.Close() | ||
} | ||
c.Assert(checkErr, IsNil) | ||
c.Assert(err, NotNil) | ||
c.Assert(err.Error(), Equals, "[ddl:12]cancelled DDL job") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters