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

unistore: Adjust some behaviors to be consistent with TiKV (#43397) #43470

Closed
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
6,208 changes: 6,208 additions & 0 deletions DEPS.bzl

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ require (
github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c
github.com/pingcap/failpoint v0.0.0-20220423142525-ae43b7f4e5c3
github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059
<<<<<<< HEAD
github.com/pingcap/kvproto v0.0.0-20220517085838-12e2f5a9d167
github.com/pingcap/log v1.1.1-0.20221116035753-734d527bc87c
github.com/pingcap/sysutil v0.0.0-20220114020952-ea68d2dbf5b4
=======
github.com/pingcap/kvproto v0.0.0-20230424092600-14ac513b9eff
github.com/pingcap/log v1.1.1-0.20230317032135-a0d097d16e22
github.com/pingcap/sysutil v1.0.1-0.20230407040306-fb007c5aff21
>>>>>>> 9b9796fc5c8 (unistore: Adjust some behaviors to be consistent with TiKV (#43397))
github.com/pingcap/tidb/parser v0.0.0-20211011031125-9b13dc409c5e
github.com/pingcap/tipb v0.0.0-20220314125451-bfb5c2c55188
github.com/prometheus/client_golang v1.11.0
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,14 @@ github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059/go.mod h1:fMRU1BA1y+r89
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989 h1:surzm05a8C9dN8dIUmo4Be2+pMRb6f55i+UIYrluu2E=
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989/go.mod h1:O17XtbryoCJhkKGbT62+L2OlrniwqiGLSqrmdHCMzZw=
github.com/pingcap/kvproto v0.0.0-20191211054548-3c6b38ea5107/go.mod h1:WWLmULLO7l8IOcQG+t+ItJ3fEcrL5FxF0Wu+HrMy26w=
<<<<<<< HEAD
github.com/pingcap/kvproto v0.0.0-20220302110454-c696585a961b/go.mod h1:IOdRDPLyda8GX2hE/jO7gqaCV/PNFh8BZQCQZXfIOqI=
github.com/pingcap/kvproto v0.0.0-20220517085838-12e2f5a9d167 h1:dsMpneacHyuVslSVndgUfJKrXFNG7VPdXip2ulG6glo=
github.com/pingcap/kvproto v0.0.0-20220517085838-12e2f5a9d167/go.mod h1:OYtxs0786qojVTmkVeufx93xe+jUgm56GUYRIKnmaGI=
=======
github.com/pingcap/kvproto v0.0.0-20230424092600-14ac513b9eff h1:58k95xMDOJkpwKs2ULr/KbEZmU2+UprcguoR/pYy6MA=
github.com/pingcap/kvproto v0.0.0-20230424092600-14ac513b9eff/go.mod h1:guCyM5N+o+ru0TsoZ1hi9lDjUMs2sIBjW3ARTEpVbnk=
>>>>>>> 9b9796fc5c8 (unistore: Adjust some behaviors to be consistent with TiKV (#43397))
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/log v0.0.0-20200511115504-543df19646ad/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/log v0.0.0-20210625125904-98ed8e2eb1c7/go.mod h1:8AanEdAHATuRurdGxZXBz0At+9avep+ub7U1AGYLIMM=
Expand Down
14 changes: 12 additions & 2 deletions store/mockstore/unistore/tikv/kverrors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func BuildLockErr(key []byte, lock *mvcc.Lock) *ErrLocked {
func (e *ErrLocked) Error() string {
lock := e.Lock
return fmt.Sprintf(
"key is locked, key: %q, Type: %v, primary: %q, startTS: %v, forUpdateTS: %v, useAsyncCommit: %v",
e.Key, lock.Op, lock.Primary, lock.StartTS, lock.ForUpdateTS, lock.UseAsyncCommit,
"key is locked, key: %v, lock: %v",
hex.EncodeToString(e.Key), lock.String(),
)
}

Expand Down Expand Up @@ -147,3 +147,13 @@ func (e *ErrAssertionFailed) Error() string {
return fmt.Sprintf("AssertionFailed { StartTS: %v, Key: %v, Assertion: %v, ExistingStartTS: %v, ExistingCommitTS: %v }",
e.StartTS, hex.EncodeToString(e.Key), e.Assertion.String(), e.ExistingStartTS, e.ExistingCommitTS)
}

// ErrPrimaryMismatch is returned if CheckTxnStatus request is sent to a secondary lock.
type ErrPrimaryMismatch struct {
Key []byte
Lock *mvcc.Lock
}

func (e *ErrPrimaryMismatch) Error() string {
return fmt.Sprintf("primary mismatch, key: %v, lock: %v", hex.EncodeToString(e.Key), e.Lock.String())
}
18 changes: 8 additions & 10 deletions store/mockstore/unistore/tikv/mvcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@ func (store *MVCCStore) CheckTxnStatus(reqCtx *requestCtx,
lock := store.getLock(reqCtx, req.PrimaryKey)
batch := store.dbWriter.NewWriteBatch(req.LockTs, 0, reqCtx.rpcCtx)
if lock != nil && lock.StartTS == req.LockTs {
if !bytes.Equal(req.PrimaryKey, lock.Primary) {
return TxnStatus{}, &kverrors.ErrPrimaryMismatch{
Key: req.PrimaryKey,
Lock: lock,
}
}

// For an async-commit lock, never roll it back or push forward it MinCommitTS.
if lock.UseAsyncCommit && !req.ForceSyncCommit {
log.S().Debugf("async commit startTS=%v secondaries=%v minCommitTS=%v", lock.StartTS, lock.Secondaries, lock.MinCommitTS)
Expand Down Expand Up @@ -989,10 +996,6 @@ func (store *MVCCStore) Commit(req *requestCtx, keys [][]byte, startTS, commitTS
Key: key,
}
}
if lock.Op == uint8(kvrpcpb.Op_PessimisticLock) {
log.Warn("commit a pessimistic lock with Lock type", zap.Binary("key", key), zap.Uint64("start ts", startTS), zap.Uint64("commit ts", commitTS))
lock.Op = uint8(kvrpcpb.Op_Lock)
}
isPessimisticTxn = lock.ForUpdateTS > 0
tmpDiff += len(key) + len(lock.Value)
batch.Commit(key, &lock)
Expand Down Expand Up @@ -1267,12 +1270,7 @@ func (store *MVCCStore) Cleanup(reqCtx *requestCtx, key []byte, startTS, current
func (store *MVCCStore) appendScannedLock(locks []*kvrpcpb.LockInfo, it *lockstore.Iterator, maxTS uint64) []*kvrpcpb.LockInfo {
lock := mvcc.DecodeLock(it.Value())
if lock.StartTS < maxTS {
locks = append(locks, &kvrpcpb.LockInfo{
PrimaryLock: lock.Primary,
LockVersion: lock.StartTS,
Key: safeCopy(it.Key()),
LockTtl: uint64(lock.TTL),
})
locks = append(locks, lock.ToLockInfo(append([]byte{}, it.Key()...)))
}
return locks
}
Expand Down
14 changes: 14 additions & 0 deletions store/mockstore/unistore/tikv/mvcc/mvcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package mvcc

import (
"encoding/binary"
"encoding/hex"
"fmt"
"unsafe"

"github.com/pingcap/kvproto/pkg/kvrpcpb"
Expand Down Expand Up @@ -114,6 +116,18 @@ func (l *Lock) ToLockInfo(key []byte) *kvrpcpb.LockInfo {
}
}

// String implements fmt.Stringer for Lock.
func (l *Lock) String() string {
return fmt.Sprintf(
"Lock { Type: %v, StartTS: %v, ForUpdateTS: %v, Primary: %v, UseAsyncCommit: %v }",
kvrpcpb.Op(l.Op).String(),
l.StartTS,
l.ForUpdateTS,
hex.EncodeToString(l.Primary),
l.UseAsyncCommit,
)
}

// UserMeta value for lock.
const (
LockUserMetaNoneByte = 0
Expand Down
9 changes: 9 additions & 0 deletions store/mockstore/unistore/tikv/mvcc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,15 @@ func TestCheckTxnStatus(t *testing.T) {
require.Equal(t, uint64(41), resCommitTs)
require.NoError(t, err)
require.Equal(t, kvrpcpb.Action_NoAction, action)

// check on mismatching primary
startTs = 43
callerStartTs = 44
currentTs = 44
MustAcquirePessimisticLock([]byte("another_key"), pk, startTs, startTs, store)
_, _, _, err = CheckTxnStatus(pk, startTs, callerStartTs, currentTs, true, store)
require.IsType(t, &kverrors.ErrPrimaryMismatch{}, errors.Cause(err))
MustPessimisticRollback(pk, startTs, startTs, store)
}

func TestCheckSecondaryLocksStatus(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions store/mockstore/unistore/tikv/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,12 @@ func convertToKeyError(err error) *kvrpcpb.KeyError {
ExistingCommitTs: x.ExistingCommitTS,
},
}
case *kverrors.ErrPrimaryMismatch:
return &kvrpcpb.KeyError{
PrimaryMismatch: &kvrpcpb.PrimaryMismatch{
LockInfo: x.Lock.ToLockInfo(x.Key),
},
}
default:
return &kvrpcpb.KeyError{
Abort: err.Error(),
Expand Down
7 changes: 6 additions & 1 deletion store/mockstore/unistore/tikv/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (
"github.com/pingcap/badger"
"github.com/pingcap/badger/y"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/log"
"github.com/pingcap/tidb/store/mockstore/unistore/lockstore"
"github.com/pingcap/tidb/store/mockstore/unistore/tikv/dbreader"
"github.com/pingcap/tidb/store/mockstore/unistore/tikv/mvcc"
"github.com/pingcap/tidb/util/mathutil"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -249,7 +251,10 @@ func (wb *writeBatch) Prewrite(key []byte, lock *mvcc.Lock) {
func (wb *writeBatch) Commit(key []byte, lock *mvcc.Lock) {
userMeta := mvcc.NewDBUserMeta(wb.startTS, wb.commitTS)
k := y.KeyWithTs(key, wb.commitTS)
if lock.Op != uint8(kvrpcpb.Op_Lock) {
if lock.Op == uint8(kvrpcpb.Op_PessimisticLock) {
log.Info("removing a pessimistic lock when committing", zap.Binary("key", key), zap.Uint64("startTS", wb.startTS), zap.Uint64("commitTS", wb.commitTS))
// Write nothing as if PessimisticRollback is called.
} else if lock.Op != uint8(kvrpcpb.Op_Lock) {
wb.dbBatch.set(k, lock.Value, userMeta)
} else if bytes.Equal(key, lock.Primary) {
opLockKey := y.KeyWithTs(mvcc.EncodeExtraTxnStatusKey(key, wb.startTS), wb.startTS)
Expand Down