From b4d72e0a3ceff02ad80903e251d78c99e6d15d1a Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Wed, 25 Apr 2018 09:11:53 -0500 Subject: [PATCH] store/tikv: make LockKeys() function thread safe (#6340) (#6384) --- store/tikv/txn.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/store/tikv/txn.go b/store/tikv/txn.go index 0495d94c0b1f0..879e7b5c43791 100644 --- a/store/tikv/txn.go +++ b/store/tikv/txn.go @@ -15,6 +15,7 @@ package tikv import ( "fmt" + "sync" "time" "github.com/juju/errors" @@ -40,6 +41,7 @@ type tikvTxn struct { commitTS uint64 valid bool lockKeys [][]byte + mu sync.Mutex // For thread-safe LockKeys function. dirty bool setCnt int64 } @@ -214,9 +216,11 @@ func (txn *tikvTxn) Rollback() error { func (txn *tikvTxn) LockKeys(keys ...kv.Key) error { metrics.TiKVTxnCmdCounter.WithLabelValues("lock_keys").Inc() + txn.mu.Lock() for _, key := range keys { txn.lockKeys = append(txn.lockKeys, key) } + txn.mu.Unlock() return nil }