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

store/tikv: fix performance issue under mixed transaction mode #10847

Merged
merged 7 commits into from
Jun 20, 2019
Merged
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
11 changes: 6 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import (

// Config number limitations
const (
MaxLogFileSize = 4096 // MB
MaxLogFileSize = 4096 // MB
MinPessimisticTTL = time.Second * 15
MaxPessimisticTTL = time.Second * 60
)

// Valid config maps
Expand Down Expand Up @@ -568,10 +570,9 @@ func (c *Config) Valid() error {
if err != nil {
return err
}
minDur := time.Second * 15
maxDur := time.Second * 60
if dur < minDur || dur > maxDur {
return fmt.Errorf("pessimistic transaction ttl %s out of range [%s, %s]", dur, minDur, maxDur)
if dur < MinPessimisticTTL || dur > MaxPessimisticTTL {
return fmt.Errorf("pessimistic transaction ttl %s out of range [%s, %s]",
dur, MinPessimisticTTL, MaxPessimisticTTL)
}
}
return nil
Expand Down
2 changes: 2 additions & 0 deletions session/pessimistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/mockstore/mocktikv"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/testkit"
Expand All @@ -52,6 +53,7 @@ func (s *testPessimisticSuite) SetUpSuite(c *C) {
mockstore.WithCluster(s.cluster),
mockstore.WithMVCCStore(s.mvccStore),
)
tikv.PessimisticLockTTL = uint64(config.MinPessimisticTTL / time.Millisecond)
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
Expand Down
13 changes: 13 additions & 0 deletions store/tikv/2pc.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,19 @@ func (c *twoPhaseCommitter) prewriteSingleBatch(bo *Backoffer, batch batchKeys)
if err1 != nil {
return errors.Trace(err1)
}
if !c.isPessimistic && c.lockTTL < lock.TTL && lock.TTL >= uint64(config.MinPessimisticTTL/time.Millisecond) {
// An optimistic prewrite meets a pessimistic or large transaction lock.
// If we wait for the lock, other written optimistic locks would block reads for long time.
// And it is very unlikely this transaction would succeed after wait for the long TTL lock.
// Return write conflict error to cleanup locks.
return newWriteConflictError(&pb.WriteConflict{
StartTs: c.startTS,
ConflictTs: lock.TxnID,
ConflictCommitTs: 0,
Key: lock.Key,
Primary: lock.Primary,
})
}
logutil.Logger(context.Background()).Debug("prewrite encounters lock",
zap.Uint64("conn", c.connID),
zap.Stringer("lock", lock))
Expand Down
17 changes: 17 additions & 0 deletions store/tikv/2pc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/mockstore/mocktikv"
"github.com/pingcap/tidb/store/tikv/tikvrpc"
Expand All @@ -38,6 +39,7 @@ var _ = Suite(&testCommitterSuite{})

func (s *testCommitterSuite) SetUpTest(c *C) {
s.cluster = mocktikv.NewCluster()
PessimisticLockTTL = uint64(config.MinPessimisticTTL / time.Millisecond)
mocktikv.BootstrapWithMultiRegions(s.cluster, []byte("a"), []byte("b"), []byte("c"))
mvccStore, err := mocktikv.NewMVCCLevelDB("")
c.Assert(err, IsNil)
Expand Down Expand Up @@ -468,3 +470,18 @@ func (s *testCommitterSuite) TestPessimisticLockedKeysDedup(c *C) {
c.Assert(err, IsNil)
c.Assert(txn.lockKeys, HasLen, 2)
}

func (s *testCommitterSuite) TestPessimistOptimisticConflict(c *C) {
txnPes := s.begin(c)
txnPes.SetOption(kv.Pessimistic, true)
err := txnPes.LockKeys(context.Background(), txnPes.startTS, kv.Key("pes"))
c.Assert(err, IsNil)
c.Assert(txnPes.IsPessimistic(), IsTrue)
c.Assert(txnPes.lockKeys, HasLen, 1)
txnOpt := s.begin(c)
err = txnOpt.Set(kv.Key("pes"), []byte("v"))
c.Assert(err, IsNil)
err = txnOpt.Commit(context.Background())
c.Assert(kv.ErrWriteConflict.Equal(err), IsTrue)
c.Assert(txnPes.Commit(context.Background()), IsNil)
}
9 changes: 2 additions & 7 deletions store/tikv/lock_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,12 @@ func (l *Lock) String() string {

// NewLock creates a new *Lock.
func NewLock(l *kvrpcpb.LockInfo) *Lock {
ttl := l.GetLockTtl()
if ttl == 0 {
ttl = defaultLockTTL
}
txnSize := l.GetTxnSize()
return &Lock{
Key: l.GetKey(),
Primary: l.GetPrimaryLock(),
TxnID: l.GetLockVersion(),
TTL: ttl,
TxnSize: txnSize,
TTL: l.GetLockTtl(),
TxnSize: l.GetTxnSize(),
}
}

Expand Down
5 changes: 5 additions & 0 deletions store/tikv/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ func (s *testLockSuite) TestLockTTL(c *C) {
s.ttlEquals(c, l.TTL, defaultLockTTL+uint64(time.Since(start)/time.Millisecond))
}

func (s *testLockSuite) TestNewLockZeroTTL(c *C) {
l := NewLock(&kvrpcpb.LockInfo{})
c.Assert(l.TTL, Equals, uint64(0))
}

func init() {
// Speed up tests.
defaultLockTTL = 3
Expand Down