Skip to content

Commit

Permalink
fix dead lock bug and optimize. (pingcap#3)
Browse files Browse the repository at this point in the history
* fix shadow error
* reduce number of keys to lock
  • Loading branch information
coocood authored and tiancaiamao committed Apr 15, 2019
1 parent 5824500 commit 6cd2e21
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 3 additions & 3 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,9 @@ func (a *ExecStmt) handleNoDelayExecutor(ctx context.Context, sctx sessionctx.Co
}
if txn.Valid() && txn.IsPessimistic() {
p := txn.(pessimisticTxn)
keys, err := p.FreshModifiedKeys()
if err != nil {
return nil, err
keys, err1 := p.FreshModifiedKeys()
if err1 != nil {
return nil, err1
}
if len(keys) == 0 {
return nil, nil
Expand Down
21 changes: 21 additions & 0 deletions session/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package session

import (
"bytes"
"context"
"fmt"
"strings"
Expand All @@ -27,6 +28,7 @@ import (
"github.com/pingcap/tidb/sessionctx/binloginfo"
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tipb/go-binlog"
Expand Down Expand Up @@ -305,6 +307,10 @@ func (st *TxnState) cleanup() {
func (st *TxnState) FreshModifiedKeys() ([]kv.Key, error) {
keys := make([]kv.Key, 0, st.buf.Len())
if err := kv.WalkMemBuffer(st.buf, func(k kv.Key, v []byte) error {
if noNeedToLock(k, v) {
// We don't need to lock non-unique index.
return nil
}
mb := st.Transaction.GetMemBuffer()
if mb == nil {
keys = append(keys, k)
Expand All @@ -323,6 +329,21 @@ func (st *TxnState) FreshModifiedKeys() ([]kv.Key, error) {
return keys, nil
}

func noNeedToLock(k, v []byte) bool {
if !bytes.HasPrefix(k, tablecodec.TablePrefix()) {
return false
}
if len(v) == 1 && v[0] == '0' {
// create an non-unique index doesn't need to lock.
return true
}
if len(v) != 0 {
return false
}
// delete an index doesn't need to lock.
return k[10] == 'i'
}

func getBinlogMutation(ctx sessionctx.Context, tableID int64) *binlog.TableMutation {
bin := binloginfo.GetPrewriteValue(ctx, true)
for i := range bin.Mutations {
Expand Down

0 comments on commit 6cd2e21

Please sign in to comment.