From 6d72b725144e943a68426648ddac8a16643367bf Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Fri, 9 Oct 2020 10:41:36 +0800 Subject: [PATCH] Revert "cherry pick #19547 to release-4.0" This reverts commit 9c355f929ee408a5300030bfcd965f6ace388313. --- executor/adapter.go | 18 +-- executor/executor.go | 8 +- executor/executor_test.go | 41 ----- kv/kv.go | 5 - sessionctx/stmtctx/stmtctx.go | 11 -- store/tikv/2pc.go | 8 +- store/tikv/pessimistic.go | 226 --------------------------- store/tikv/txn.go | 23 --- util/execdetails/execdetails.go | 174 +-------------------- util/execdetails/execdetails_test.go | 72 --------- 10 files changed, 5 insertions(+), 581 deletions(-) delete mode 100644 store/tikv/pessimistic.go diff --git a/executor/adapter.go b/executor/adapter.go index f3490589a0131..226527b5373ab 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -555,13 +555,8 @@ func (a *ExecStmt) handlePessimisticDML(ctx context.Context, e Executor) error { } seVars := sctx.GetSessionVars() lockCtx := newLockCtx(seVars, seVars.LockWaitTimeout) - var lockKeyStats *execdetails.LockKeysDetails - ctx = context.WithValue(ctx, execdetails.LockKeysDetailCtxKey, &lockKeyStats) startLocking := time.Now() err = txn.LockKeys(ctx, lockCtx, keys...) - if lockKeyStats != nil { - seVars.StmtCtx.MergeLockKeysExecDetails(lockKeyStats) - } if err == nil { return nil } @@ -799,21 +794,10 @@ func (a *ExecStmt) CloseRecordSet(txnStartTS uint64, lastErr error) { // 3. record execute duration metric. // 4. update the `PrevStmt` in session variable. func (a *ExecStmt) FinishExecuteStmt(txnTS uint64, succ bool, hasMoreResults bool) { - sessVars := a.Ctx.GetSessionVars() - execDetail := sessVars.StmtCtx.GetExecDetails() - // Attach commit/lockKeys runtime stats to executor runtime stats. - if (execDetail.CommitDetail != nil || execDetail.LockKeysDetail != nil) && sessVars.StmtCtx.RuntimeStatsColl != nil { - stats := sessVars.StmtCtx.RuntimeStatsColl.GetRootStats(a.Plan.ID()) - statsWithCommit := &execdetails.RuntimeStatsWithCommit{ - RuntimeStats: stats, - Commit: execDetail.CommitDetail, - LockKeys: execDetail.LockKeysDetail, - } - sessVars.StmtCtx.RuntimeStatsColl.RegisterStats(a.Plan.ID(), statsWithCommit) - } // `LowSlowQuery` and `SummaryStmt` must be called before recording `PrevStmt`. a.LogSlowQuery(txnTS, succ, hasMoreResults) a.SummaryStmt(succ) + sessVars := a.Ctx.GetSessionVars() prevStmt := a.GetTextToLog() if config.RedactLogEnabled() { sessVars.PrevStmt = FormatSQL(prevStmt, nil) diff --git a/executor/executor.go b/executor/executor.go index c0601119fb811..c30fb18846637 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -953,13 +953,7 @@ func doLockKeys(ctx context.Context, se sessionctx.Context, lockCtx *kv.LockCtx, if err != nil { return err } - var lockKeyStats *execdetails.LockKeysDetails - ctx = context.WithValue(ctx, execdetails.LockKeysDetailCtxKey, &lockKeyStats) - err = txn.LockKeys(sessionctx.SetCommitCtx(ctx, se), lockCtx, keys...) - if lockKeyStats != nil { - sctx.MergeLockKeysExecDetails(lockKeyStats) - } - return err + return txn.LockKeys(sessionctx.SetCommitCtx(ctx, se), lockCtx, keys...) } // LimitExec represents limit executor diff --git a/executor/executor_test.go b/executor/executor_test.go index 9a48bd714ef60..cf83ebd453ca1 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -6003,47 +6003,6 @@ func (s *testSuite) TestIssue19372(c *C) { tk.MustQuery("select (select t2.c_str from t2 where t2.c_str <= t1.c_str and t2.c_int in (1, 2) order by t2.c_str limit 1) x from t1 order by c_int;").Check(testkit.Rows("a", "a", "a")) } -<<<<<<< HEAD -======= -func (s *testSuite) TestCollectDMLRuntimeStats(c *C) { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t1") - tk.MustExec("create table t1 (a int, b int, unique index (a))") - - testSQLs := []string{ - "insert ignore into t1 values (5,5);", - "insert into t1 values (5,5) on duplicate key update a=a+1;", - "replace into t1 values (5,6),(6,7)", - "update t1 set a=a+1 where a=6;", - } - - getRootStats := func() string { - info := tk.Se.ShowProcess() - c.Assert(info, NotNil) - p, ok := info.Plan.(plannercore.Plan) - c.Assert(ok, IsTrue) - stats := tk.Se.GetSessionVars().StmtCtx.RuntimeStatsColl.GetRootStats(p.ID()) - return stats.String() - } - for _, sql := range testSQLs { - tk.MustExec(sql) - c.Assert(getRootStats(), Matches, "time.*loops.*Get.*num_rpc.*total_time.*") - } - - // Test for lock keys stats. - tk.MustExec("begin pessimistic") - tk.MustExec("update t1 set b=b+1") - c.Assert(getRootStats(), Matches, "time.*lock_keys.*time.* region.* keys.* lock_rpc:.* rpc_count.*") - tk.MustExec("rollback") - - tk.MustExec("begin pessimistic") - tk.MustQuery("select * from t1 for update").Check(testkit.Rows("5 6", "7 7")) - c.Assert(getRootStats(), Matches, "time.*lock_keys.*time.* region.* keys.* lock_rpc:.* rpc_count.*") - tk.MustExec("rollback") -} - ->>>>>>> 915d84d... executor: add pessimistic lock keys runtime information (#19547) func (s *testSuite) TestIssue13758(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") diff --git a/kv/kv.go b/kv/kv.go index eed0cd33d2de9..ef91524f480d7 100644 --- a/kv/kv.go +++ b/kv/kv.go @@ -20,7 +20,6 @@ import ( "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/store/tikv/oracle" - "github.com/pingcap/tidb/util/execdetails" "github.com/pingcap/tidb/util/memory" ) @@ -236,11 +235,7 @@ type LockCtx struct { Values map[string]ReturnedValue ValuesLock sync.Mutex LockExpired *uint32 -<<<<<<< HEAD CheckKeyExists map[string]struct{} -======= - Stats *execdetails.LockKeysDetails ->>>>>>> 915d84d... executor: add pessimistic lock keys runtime information (#19547) } // ReturnedValue pairs the Value and AlreadyLocked flag for PessimisticLock return values result. diff --git a/sessionctx/stmtctx/stmtctx.go b/sessionctx/stmtctx/stmtctx.go index a51e57ad2b1b4..2089e48c3588d 100644 --- a/sessionctx/stmtctx/stmtctx.go +++ b/sessionctx/stmtctx/stmtctx.go @@ -497,17 +497,6 @@ func (sc *StatementContext) MergeExecDetails(details *execdetails.ExecDetails, c sc.mu.Unlock() } -// MergeLockKeysExecDetails merges lock keys execution details into self. -func (sc *StatementContext) MergeLockKeysExecDetails(lockKeys *execdetails.LockKeysDetails) { - sc.mu.Lock() - if sc.mu.execDetails.LockKeysDetail == nil { - sc.mu.execDetails.LockKeysDetail = lockKeys - } else { - sc.mu.execDetails.LockKeysDetail.Merge(lockKeys) - } - sc.mu.Unlock() -} - // GetExecDetails gets the execution details for the statement. func (sc *StatementContext) GetExecDetails() execdetails.ExecDetails { var details execdetails.ExecDetails diff --git a/store/tikv/2pc.go b/store/tikv/2pc.go index ac1f016608740..ce874c890d265 100644 --- a/store/tikv/2pc.go +++ b/store/tikv/2pc.go @@ -555,9 +555,7 @@ func (c *twoPhaseCommitter) doActionOnGroupMutations(bo *Backoffer, action twoPh var batches []batchMutations var sizeFunc = c.keySize - - switch act := action.(type) { - case actionPrewrite: + if _, ok := action.(actionPrewrite); ok { // Do not update regionTxnSize on retries. They are not used when building a PrewriteRequest. if len(bo.errors) == 0 { for _, group := range groups { @@ -566,10 +564,6 @@ func (c *twoPhaseCommitter) doActionOnGroupMutations(bo *Backoffer, action twoPh } sizeFunc = c.keyValueSize atomic.AddInt32(&c.getDetail().PrewriteRegionNum, int32(len(groups))) - case actionPessimisticLock: - if act.LockCtx.Stats != nil { - act.LockCtx.Stats.RegionNum = int32(len(groups)) - } } primaryIdx := -1 diff --git a/store/tikv/pessimistic.go b/store/tikv/pessimistic.go deleted file mode 100644 index df5836f7ee6c5..0000000000000 --- a/store/tikv/pessimistic.go +++ /dev/null @@ -1,226 +0,0 @@ -// Copyright 2020 PingCAP, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// See the License for the specific language governing permissions and -// limitations under the License. - -package tikv - -import ( - "sync/atomic" - "time" - - "github.com/pingcap/errors" - "github.com/pingcap/failpoint" - pb "github.com/pingcap/kvproto/pkg/kvrpcpb" - "github.com/pingcap/tidb/kv" - "github.com/pingcap/tidb/metrics" - "github.com/pingcap/tidb/store/tikv/tikvrpc" - "github.com/prometheus/client_golang/prometheus" -) - -type actionPessimisticLock struct { - *kv.LockCtx -} -type actionPessimisticRollback struct{} - -var ( - _ twoPhaseCommitAction = actionPessimisticLock{} - _ twoPhaseCommitAction = actionPessimisticRollback{} - - tiKVTxnRegionsNumHistogramPessimisticLock = metrics.TiKVTxnRegionsNumHistogram.WithLabelValues(metricsTag("pessimistic_lock")) - tiKVTxnRegionsNumHistogramPessimisticRollback = metrics.TiKVTxnRegionsNumHistogram.WithLabelValues(metricsTag("pessimistic_rollback")) -) - -func (actionPessimisticLock) String() string { - return "pessimistic_lock" -} - -func (actionPessimisticLock) tiKVTxnRegionsNumHistogram() prometheus.Observer { - return tiKVTxnRegionsNumHistogramPessimisticLock -} - -func (actionPessimisticRollback) String() string { - return "pessimistic_rollback" -} - -func (actionPessimisticRollback) tiKVTxnRegionsNumHistogram() prometheus.Observer { - return tiKVTxnRegionsNumHistogramPessimisticRollback -} - -func (action actionPessimisticLock) handleSingleBatch(c *twoPhaseCommitter, bo *Backoffer, batch batchMutations) error { - m := &batch.mutations - mutations := make([]*pb.Mutation, m.len()) - for i := range m.keys { - mut := &pb.Mutation{ - Op: pb.Op_PessimisticLock, - Key: m.keys[i], - } - if c.txn.us.HasPresumeKeyNotExists(m.keys[i]) { - mut.Assertion = pb.Assertion_NotExist - } - mutations[i] = mut - } - elapsed := uint64(time.Since(c.txn.startTime) / time.Millisecond) - req := tikvrpc.NewRequest(tikvrpc.CmdPessimisticLock, &pb.PessimisticLockRequest{ - Mutations: mutations, - PrimaryLock: c.primary(), - StartVersion: c.startTS, - ForUpdateTs: c.forUpdateTS, - LockTtl: elapsed + atomic.LoadUint64(&ManagedLockTTL), - IsFirstLock: c.isFirstLock, - WaitTimeout: action.LockWaitTime, - ReturnValues: action.ReturnValues, - MinCommitTs: c.forUpdateTS + 1, - }, pb.Context{Priority: c.priority, SyncLog: c.syncLog}) - lockWaitStartTime := action.WaitStartTime - for { - // if lockWaitTime set, refine the request `WaitTimeout` field based on timeout limit - if action.LockWaitTime > 0 { - timeLeft := action.LockWaitTime - (time.Since(lockWaitStartTime)).Milliseconds() - if timeLeft <= 0 { - req.PessimisticLock().WaitTimeout = kv.LockNoWait - } else { - req.PessimisticLock().WaitTimeout = timeLeft - } - } - failpoint.Inject("PessimisticLockErrWriteConflict", func() error { - time.Sleep(300 * time.Millisecond) - return kv.ErrWriteConflict - }) - startTime := time.Now() - resp, err := c.store.SendReq(bo, req, batch.region, readTimeoutShort) - if action.LockCtx.Stats != nil { - atomic.AddInt64(&action.LockCtx.Stats.LockRPCTime, int64(time.Since(startTime))) - atomic.AddInt64(&action.LockCtx.Stats.LockRPCCount, 1) - } - if err != nil { - return errors.Trace(err) - } - regionErr, err := resp.GetRegionError() - if err != nil { - return errors.Trace(err) - } - if regionErr != nil { - err = bo.Backoff(BoRegionMiss, errors.New(regionErr.String())) - if err != nil { - return errors.Trace(err) - } - err = c.pessimisticLockMutations(bo, action.LockCtx, batch.mutations) - return errors.Trace(err) - } - if resp.Resp == nil { - return errors.Trace(ErrBodyMissing) - } - lockResp := resp.Resp.(*pb.PessimisticLockResponse) - keyErrs := lockResp.GetErrors() - if len(keyErrs) == 0 { - if action.ReturnValues { - action.ValuesLock.Lock() - for i, mutation := range mutations { - action.Values[string(mutation.Key)] = kv.ReturnedValue{Value: lockResp.Values[i]} - } - action.ValuesLock.Unlock() - } - return nil - } - var locks []*Lock - for _, keyErr := range keyErrs { - // Check already exists error - if alreadyExist := keyErr.GetAlreadyExist(); alreadyExist != nil { - key := alreadyExist.GetKey() - return c.extractKeyExistsErr(key) - } - if deadlock := keyErr.Deadlock; deadlock != nil { - return &ErrDeadlock{Deadlock: deadlock} - } - - // Extract lock from key error - lock, err1 := extractLockFromKeyErr(keyErr) - if err1 != nil { - return errors.Trace(err1) - } - locks = append(locks, lock) - } - // Because we already waited on tikv, no need to Backoff here. - // tikv default will wait 3s(also the maximum wait value) when lock error occurs - startTime = time.Now() - msBeforeTxnExpired, _, err := c.store.lockResolver.ResolveLocks(bo, 0, locks) - if err != nil { - return errors.Trace(err) - } - if action.LockCtx.Stats != nil { - atomic.AddInt64(&action.LockCtx.Stats.ResolveLockTime, int64(time.Since(startTime))) - } - - // If msBeforeTxnExpired is not zero, it means there are still locks blocking us acquiring - // the pessimistic lock. We should return acquire fail with nowait set or timeout error if necessary. - if msBeforeTxnExpired > 0 { - if action.LockWaitTime == kv.LockNoWait { - return ErrLockAcquireFailAndNoWaitSet - } else if action.LockWaitTime == kv.LockAlwaysWait { - // do nothing but keep wait - } else { - // the lockWaitTime is set, we should return wait timeout if we are still blocked by a lock - if time.Since(lockWaitStartTime).Milliseconds() >= action.LockWaitTime { - return errors.Trace(ErrLockWaitTimeout) - } - } - if action.LockCtx.PessimisticLockWaited != nil { - atomic.StoreInt32(action.LockCtx.PessimisticLockWaited, 1) - } - } - - // Handle the killed flag when waiting for the pessimistic lock. - // When a txn runs into LockKeys() and backoff here, it has no chance to call - // executor.Next() and check the killed flag. - if action.Killed != nil { - // Do not reset the killed flag here! - // actionPessimisticLock runs on each region parallelly, we have to consider that - // the error may be dropped. - if atomic.LoadUint32(action.Killed) == 1 { - return errors.Trace(ErrQueryInterrupted) - } - } - } -} - -func (actionPessimisticRollback) handleSingleBatch(c *twoPhaseCommitter, bo *Backoffer, batch batchMutations) error { - req := tikvrpc.NewRequest(tikvrpc.CmdPessimisticRollback, &pb.PessimisticRollbackRequest{ - StartVersion: c.startTS, - ForUpdateTs: c.forUpdateTS, - Keys: batch.mutations.keys, - }) - resp, err := c.store.SendReq(bo, req, batch.region, readTimeoutShort) - if err != nil { - return errors.Trace(err) - } - regionErr, err := resp.GetRegionError() - if err != nil { - return errors.Trace(err) - } - if regionErr != nil { - err = bo.Backoff(BoRegionMiss, errors.New(regionErr.String())) - if err != nil { - return errors.Trace(err) - } - err = c.pessimisticRollbackMutations(bo, batch.mutations) - return errors.Trace(err) - } - return nil -} - -func (c *twoPhaseCommitter) pessimisticLockMutations(bo *Backoffer, lockCtx *kv.LockCtx, mutations CommitterMutations) error { - return c.doActionOnMutations(bo, actionPessimisticLock{lockCtx}, mutations) -} - -func (c *twoPhaseCommitter) pessimisticRollbackMutations(bo *Backoffer, mutations CommitterMutations) error { - return c.doActionOnMutations(bo, actionPessimisticRollback{}, mutations) -} diff --git a/store/tikv/txn.go b/store/tikv/txn.go index f4f0539561951..2f41dd1cb6d20 100644 --- a/store/tikv/txn.go +++ b/store/tikv/txn.go @@ -367,12 +367,6 @@ func (txn *tikvTxn) LockKeys(ctx context.Context, lockCtx *kv.LockCtx, keysInput // Exclude keys that are already locked. var err error keys := make([][]byte, 0, len(keysInput)) -<<<<<<< HEAD -======= - startTime := time.Now() - txn.mu.Lock() - defer txn.mu.Unlock() ->>>>>>> 915d84d... executor: add pessimistic lock keys runtime information (#19547) defer func() { if err == nil { if lockCtx.PessimisticLockWaited != nil { @@ -386,14 +380,6 @@ func (txn *tikvTxn) LockKeys(ctx context.Context, lockCtx *kv.LockCtx, keysInput if lockCtx.LockKeysCount != nil { *lockCtx.LockKeysCount += int32(len(keys)) } - if lockCtx.Stats != nil { - lockCtx.Stats.TotalTime = time.Since(startTime) - ctxValue := ctx.Value(execdetails.LockKeysDetailCtxKey) - if ctxValue != nil { - lockKeysDetail := ctxValue.(**execdetails.LockKeysDetails) - *lockKeysDetail = lockCtx.Stats - } - } }() for _, key := range keysInput { // The value of lockedMap is only used by pessimistic transactions. @@ -442,21 +428,12 @@ func (txn *tikvTxn) LockKeys(ctx context.Context, lockCtx *kv.LockCtx, keysInput assignedPrimaryKey = true } - lockCtx.Stats = &execdetails.LockKeysDetails{ - LockKeys: int32(len(keys)), - } bo := NewBackofferWithVars(ctx, pessimisticLockMaxBackoff, txn.vars) txn.committer.forUpdateTS = lockCtx.ForUpdateTS // If the number of keys greater than 1, it can be on different region, // concurrently execute on multiple regions may lead to deadlock. txn.committer.isFirstLock = len(txn.lockKeys) == 0 && len(keys) == 1 err = txn.committer.pessimisticLockMutations(bo, lockCtx, CommitterMutations{keys: keys}) - if bo.totalSleep > 0 { - atomic.AddInt64(&lockCtx.Stats.BackoffTime, int64(bo.totalSleep)*int64(time.Millisecond)) - lockCtx.Stats.Mu.Lock() - lockCtx.Stats.Mu.BackoffTypes = append(lockCtx.Stats.Mu.BackoffTypes, bo.types...) - lockCtx.Stats.Mu.Unlock() - } if lockCtx.Killed != nil { // If the kill signal is received during waiting for pessimisticLock, // pessimisticLockKeys would handle the error but it doesn't reset the flag. diff --git a/util/execdetails/execdetails.go b/util/execdetails/execdetails.go index 13d8dc58889f5..16034787d070b 100644 --- a/util/execdetails/execdetails.go +++ b/util/execdetails/execdetails.go @@ -27,15 +27,9 @@ import ( ) type commitDetailCtxKeyType struct{} -type lockKeysDetailCtxKeyType struct{} -var ( - // CommitDetailCtxKey presents CommitDetail info key in context. - CommitDetailCtxKey = commitDetailCtxKeyType{} - - // LockKeysDetailCtxKey presents LockKeysDetail info key in context. - LockKeysDetailCtxKey = lockKeysDetailCtxKeyType{} -) +// CommitDetailCtxKey presents CommitDetail info key in context. +var CommitDetailCtxKey = commitDetailCtxKeyType{} // ExecDetails contains execution detail information. type ExecDetails struct { @@ -51,7 +45,6 @@ type ExecDetails struct { TotalKeys int64 ProcessedKeys int64 CommitDetail *CommitDetails - LockKeysDetail *LockKeysDetails } type stmtExecDetailKeyType struct{} @@ -87,35 +80,6 @@ type CommitDetails struct { TxnRetry int } -// LockKeysDetails contains pessimistic lock keys detail information. -type LockKeysDetails struct { - TotalTime time.Duration - RegionNum int32 - LockKeys int32 - ResolveLockTime int64 - BackoffTime int64 - Mu struct { - sync.Mutex - BackoffTypes []fmt.Stringer - } - LockRPCTime int64 - LockRPCCount int64 - RetryCount int -} - -// Merge merges lock keys execution details into self. -func (ld *LockKeysDetails) Merge(lockKey *LockKeysDetails) { - ld.TotalTime += lockKey.TotalTime - ld.RegionNum += lockKey.RegionNum - ld.LockKeys += lockKey.LockKeys - ld.ResolveLockTime += lockKey.ResolveLockTime - ld.BackoffTime += lockKey.BackoffTime - ld.LockRPCTime += lockKey.LockRPCTime - ld.LockRPCCount += ld.LockRPCCount - ld.Mu.BackoffTypes = append(ld.Mu.BackoffTypes, lockKey.Mu.BackoffTypes...) - ld.RetryCount++ -} - const ( // CopTimeStr represents the sum of cop-task time spend in TiDB distSQL. CopTimeStr = "Cop_time" @@ -527,137 +491,3 @@ func (e *RuntimeStatsWithConcurrencyInfo) String() string { } return result } -<<<<<<< HEAD -======= - -// RuntimeStatsWithCommit is the RuntimeStats with commit detail. -type RuntimeStatsWithCommit struct { - RuntimeStats - Commit *CommitDetails - LockKeys *LockKeysDetails -} - -func (e *RuntimeStatsWithCommit) String() string { - buf := bytes.NewBuffer(make([]byte, 0, 32)) - if e.RuntimeStats != nil { - buf.WriteString(e.RuntimeStats.String()) - } - if e.Commit != nil { - buf.WriteString(", commit_txn: {") - if e.Commit.PrewriteTime > 0 { - buf.WriteString("prewrite:") - buf.WriteString(e.Commit.PrewriteTime.String()) - } - if e.Commit.WaitPrewriteBinlogTime > 0 { - buf.WriteString(", wait_prewrite_binlog:") - buf.WriteString(e.Commit.WaitPrewriteBinlogTime.String()) - } - if e.Commit.GetCommitTsTime > 0 { - buf.WriteString(", get_commit_ts:") - buf.WriteString(e.Commit.GetCommitTsTime.String()) - } - if e.Commit.CommitTime > 0 { - buf.WriteString(", commit:") - buf.WriteString(e.Commit.CommitTime.String()) - } - commitBackoffTime := atomic.LoadInt64(&e.Commit.CommitBackoffTime) - if commitBackoffTime > 0 { - buf.WriteString(", backoff: {time: ") - buf.WriteString(time.Duration(commitBackoffTime).String()) - e.Commit.Mu.Lock() - if len(e.Commit.Mu.BackoffTypes) > 0 { - buf.WriteString(", type: ") - buf.WriteString(e.formatBackoff(e.Commit.Mu.BackoffTypes)) - } - e.Commit.Mu.Unlock() - buf.WriteString("}") - } - if e.Commit.ResolveLockTime > 0 { - buf.WriteString(", resolve_lock: ") - buf.WriteString(time.Duration(e.Commit.ResolveLockTime).String()) - } - - prewriteRegionNum := atomic.LoadInt32(&e.Commit.PrewriteRegionNum) - if prewriteRegionNum > 0 { - buf.WriteString(", region_num:") - buf.WriteString(strconv.FormatInt(int64(prewriteRegionNum), 10)) - } - if e.Commit.WriteKeys > 0 { - buf.WriteString(", write_keys:") - buf.WriteString(strconv.FormatInt(int64(e.Commit.WriteKeys), 10)) - } - if e.Commit.WriteSize > 0 { - buf.WriteString(", write_byte:") - buf.WriteString(strconv.FormatInt(int64(e.Commit.WriteSize), 10)) - } - if e.Commit.TxnRetry > 0 { - buf.WriteString(", txn_retry:") - buf.WriteString(strconv.FormatInt(int64(e.Commit.TxnRetry), 10)) - } - buf.WriteString("}") - } - if e.LockKeys != nil { - buf.WriteString(", lock_keys: {") - if e.LockKeys.TotalTime > 0 { - buf.WriteString("time:") - buf.WriteString(e.LockKeys.TotalTime.String()) - } - if e.LockKeys.RegionNum > 0 { - buf.WriteString(", region:") - buf.WriteString(strconv.FormatInt(int64(e.LockKeys.RegionNum), 10)) - } - if e.LockKeys.LockKeys > 0 { - buf.WriteString(", keys:") - buf.WriteString(strconv.FormatInt(int64(e.LockKeys.LockKeys), 10)) - } - if e.LockKeys.ResolveLockTime > 0 { - buf.WriteString(", resolve_lock:") - buf.WriteString(time.Duration(e.LockKeys.ResolveLockTime).String()) - } - if e.LockKeys.BackoffTime > 0 { - buf.WriteString(", backoff: {time: ") - buf.WriteString(time.Duration(e.LockKeys.BackoffTime).String()) - e.LockKeys.Mu.Lock() - if len(e.LockKeys.Mu.BackoffTypes) > 0 { - buf.WriteString(", type: ") - buf.WriteString(e.formatBackoff(e.LockKeys.Mu.BackoffTypes)) - } - e.LockKeys.Mu.Unlock() - buf.WriteString("}") - } - if e.LockKeys.LockRPCTime > 0 { - buf.WriteString(", lock_rpc:") - buf.WriteString(time.Duration(e.LockKeys.LockRPCTime).String()) - } - if e.LockKeys.LockRPCCount > 0 { - buf.WriteString(", rpc_count:") - buf.WriteString(strconv.FormatInt(e.LockKeys.LockRPCCount, 10)) - } - if e.LockKeys.RetryCount > 0 { - buf.WriteString(", retry_count:") - buf.WriteString(strconv.FormatInt(int64(e.LockKeys.RetryCount), 10)) - } - buf.WriteString("}") - } - return buf.String() -} - -func (e *RuntimeStatsWithCommit) formatBackoff(backoffTypes []fmt.Stringer) string { - if len(backoffTypes) == 0 { - return "" - } - tpMap := make(map[string]struct{}) - tpArray := []string{} - for _, tp := range backoffTypes { - tpStr := tp.String() - _, ok := tpMap[tpStr] - if ok { - continue - } - tpMap[tpStr] = struct{}{} - tpArray = append(tpArray, tpStr) - } - sort.Strings(tpArray) - return fmt.Sprintf("%v", tpArray) -} ->>>>>>> 915d84d... executor: add pessimistic lock keys runtime information (#19547) diff --git a/util/execdetails/execdetails_test.go b/util/execdetails/execdetails_test.go index 3ef2b625b0f61..d39fc5058b477 100644 --- a/util/execdetails/execdetails_test.go +++ b/util/execdetails/execdetails_test.go @@ -121,7 +121,6 @@ func TestCopRuntimeStats(t *testing.T) { } } -<<<<<<< HEAD func TestCopRuntimeStatsForTiFlash(t *testing.T) { stats := NewRuntimeStatsColl() tableScanID := 1 @@ -157,76 +156,5 @@ func TestCopRuntimeStatsForTiFlash(t *testing.T) { } if stats.ExistsRootStats(tableReaderID) == false { t.Fatal("table_reader not exists") -======= -func TestRuntimeStatsWithCommit(t *testing.T) { - basicStats := &BasicRuntimeStats{ - loop: 1, - consume: int64(time.Second), - } - commitDetail := &CommitDetails{ - GetCommitTsTime: time.Second, - PrewriteTime: time.Second, - CommitTime: time.Second, - CommitBackoffTime: int64(time.Second), - Mu: struct { - sync.Mutex - BackoffTypes []fmt.Stringer - }{BackoffTypes: []fmt.Stringer{ - stringutil.MemoizeStr(func() string { - return "backoff1" - }), - stringutil.MemoizeStr(func() string { - return "backoff2" - }), - stringutil.MemoizeStr(func() string { - return "backoff1" - }), - }}, - ResolveLockTime: int64(time.Second), - WriteKeys: 3, - WriteSize: 66, - PrewriteRegionNum: 5, - TxnRetry: 2, - } - stats := &RuntimeStatsWithCommit{ - RuntimeStats: basicStats, - Commit: commitDetail, - } - expect := "time:1s, loops:1, commit_txn: {prewrite:1s, get_commit_ts:1s, commit:1s, backoff: {time: 1s, type: [backoff1 backoff2]}, resolve_lock: 1s, region_num:5, write_keys:3, write_byte:66, txn_retry:2}" - if stats.String() != expect { - t.Fatalf("%v != %v", stats.String(), expect) - } - lockDetail := &LockKeysDetails{ - TotalTime: time.Second, - RegionNum: 2, - LockKeys: 10, - ResolveLockTime: int64(time.Second * 2), - BackoffTime: int64(time.Second * 3), - Mu: struct { - sync.Mutex - BackoffTypes []fmt.Stringer - }{BackoffTypes: []fmt.Stringer{ - stringutil.MemoizeStr(func() string { - return "backoff4" - }), - stringutil.MemoizeStr(func() string { - return "backoff5" - }), - stringutil.MemoizeStr(func() string { - return "backoff5" - }), - }}, - LockRPCTime: int64(time.Second * 5), - LockRPCCount: 50, - RetryCount: 2, - } - stats = &RuntimeStatsWithCommit{ - RuntimeStats: basicStats, - LockKeys: lockDetail, - } - expect = "time:1s, loops:1, lock_keys: {time:1s, region:2, keys:10, resolve_lock:2s, backoff: {time: 3s, type: [backoff4 backoff5]}, lock_rpc:5s, rpc_count:50, retry_count:2}" - if stats.String() != expect { - t.Fatalf("%v != %v", stats.String(), expect) ->>>>>>> 915d84d... executor: add pessimistic lock keys runtime information (#19547) } }