From d2b4ad8814904e94eeb68c6fce259ff9d032ae5b Mon Sep 17 00:00:00 2001 From: zimulala Date: Fri, 29 Jul 2016 18:45:55 +0800 Subject: [PATCH] *: Pass make race (#1521) --- Makefile | 2 +- ddl/column_test.go | 6 ++++++ executor/new_executor_xapi.go | 4 +++- privilege/privileges/privileges_test.go | 5 ++++- store/tikv/coprocessor.go | 17 ++++++++++++----- store/tikv/txn_committer.go | 9 +++++---- 6 files changed, 31 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index fc25bc6671ec9..b7a32b54dec37 100644 --- a/Makefile +++ b/Makefile @@ -120,7 +120,7 @@ gotest: race: rm -rf vendor && ln -s _vendor/vendor vendor - $(GO) test --race -cover $(PACKAGES) + $(GO) test --race $(PACKAGES) rm -rf vendor tikv_integration_test: diff --git a/ddl/column_test.go b/ddl/column_test.go index 603a5093bedce..ee2c4674f72f1 100644 --- a/ddl/column_test.go +++ b/ddl/column_test.go @@ -15,6 +15,7 @@ package ddl import ( "reflect" + "sync" "github.com/juju/errors" . "github.com/pingcap/check" @@ -821,6 +822,7 @@ func (s *testColumnSuite) TestDropColumn(c *C) { c.Assert(err, IsNil) checkOK := false + var mu sync.Mutex oldCol := &table.Column{} tc := &testDDLCallback{} @@ -831,7 +833,9 @@ func (s *testColumnSuite) TestDropColumn(c *C) { t := testGetTable(c, d, s.dbInfo.ID, tblInfo.ID).(*tables.Table) col := table.FindCol(t.Columns, colName) if col == nil { + mu.Lock() checkOK = true + mu.Unlock() return } oldCol = col @@ -847,7 +851,9 @@ func (s *testColumnSuite) TestDropColumn(c *C) { job := testDropColumn(c, ctx, s.d, s.dbInfo, tblInfo, colName, false) testCheckJobDone(c, d, job, false) + mu.Lock() c.Assert(checkOK, IsTrue) + mu.Unlock() _, err = ctx.GetTxn(true) c.Assert(err, IsNil) diff --git a/executor/new_executor_xapi.go b/executor/new_executor_xapi.go index 518d3f9d5c387..0e53e6a8b250d 100644 --- a/executor/new_executor_xapi.go +++ b/executor/new_executor_xapi.go @@ -121,7 +121,9 @@ func (e *NewXSelectIndexExec) Close() error { e.result = nil e.subResult = nil e.taskCursor = 0 + e.mu.Lock() e.tasks = nil + e.mu.Unlock() e.indexOrder = make(map[int64]int) return nil } @@ -270,8 +272,8 @@ func (e *NewXSelectIndexExec) runTableTasks(n int) { func (e *NewXSelectIndexExec) pickAndExecTask() { for { // Pick a new task. - e.mu.Lock() var task *lookupTableTask + e.mu.Lock() for _, t := range e.tasks { if t.status == taskNew { task = t diff --git a/privilege/privileges/privileges_test.go b/privilege/privileges/privileges_test.go index 5302a9346674c..b76f1377356a4 100644 --- a/privilege/privileges/privileges_test.go +++ b/privilege/privileges/privileges_test.go @@ -52,8 +52,11 @@ type testPrivilegeSuite struct { createColumnPrivTableSQL string } -func (s *testPrivilegeSuite) SetUpTest(c *C) { +func (s *testPrivilegeSuite) SetUpSuit(c *C) { log.SetLevelByString("error") +} + +func (s *testPrivilegeSuite) SetUpTest(c *C) { s.dbName = "test" s.store = newStore(c, s.dbName) se := newSession(c, s.store, s.dbName) diff --git a/store/tikv/coprocessor.go b/store/tikv/coprocessor.go index baad42962aca7..8c02448d85204 100644 --- a/store/tikv/coprocessor.go +++ b/store/tikv/coprocessor.go @@ -99,9 +99,7 @@ func (c *CopClient) Send(req *kv.Request) kv.Response { } it.errChan = make(chan error, 1) if len(it.tasks) == 0 { - it.mu.Lock() it.Close() - it.mu.Unlock() } it.run() return it @@ -275,10 +273,10 @@ func (it *copIterator) Next() (io.ReadCloser, error) { break } } + it.mu.Unlock() if task == nil { it.Close() } - it.mu.Unlock() if task == nil { return nil, nil } @@ -294,12 +292,12 @@ func (it *copIterator) Next() (io.ReadCloser, error) { it.mu.Lock() defer it.mu.Unlock() if err != nil { - it.Close() + it.finished = true return nil, errors.Trace(err) } it.respGot++ if it.respGot == len(it.tasks) { - it.Close() + it.finished = true } return ioutil.NopCloser(bytes.NewBuffer(resp.Data)), nil } @@ -307,6 +305,13 @@ func (it *copIterator) Next() (io.ReadCloser, error) { // Handle single copTask. func (it *copIterator) handleTask(bo *Backoffer, task *copTask) (*coprocessor.Response, error) { for { + it.mu.RLock() + if it.finished { + it.mu.RUnlock() + return nil, nil + } + it.mu.RUnlock() + req := &coprocessor.Request{ Context: task.region.GetContext(), Tp: proto.Int64(it.req.Tp), @@ -394,7 +399,9 @@ func (it *copIterator) rebuildCurrentTask(bo *Backoffer, task *copTask) error { } func (it *copIterator) Close() error { + it.mu.Lock() it.finished = true + it.mu.Unlock() return nil } diff --git a/store/tikv/txn_committer.go b/store/tikv/txn_committer.go index 6bd614d25b29e..9be54070c9375 100644 --- a/store/tikv/txn_committer.go +++ b/store/tikv/txn_committer.go @@ -320,11 +320,12 @@ func (c *txnCommitter) cleanupKeys(bo *Backoffer, keys [][]byte) error { func (c *txnCommitter) Commit() error { defer func() { // Always clean up all written keys if the txn does not commit. - if !c.committed { + c.mu.RLock() + writtenKeys := c.writtenKeys + committed := c.committed + c.mu.RUnlock() + if !committed { go func() { - c.mu.RLock() - writtenKeys := c.writtenKeys - c.mu.RUnlock() c.cleanupKeys(NewBackoffer(cleanupMaxBackoff), writtenKeys) log.Infof("txn clean up done, tid: %d", c.startTS) }()