diff --git a/distsql/distsql_test.go b/distsql/distsql_test.go index 5a5cfa8230db1..2d47892f4fc12 100644 --- a/distsql/distsql_test.go +++ b/distsql/distsql_test.go @@ -28,6 +28,7 @@ import ( "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/statistics" "github.com/pingcap/tidb/store/tikv" + "github.com/pingcap/tidb/store/tikv/tikvrpc" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/chunk" "github.com/pingcap/tidb/util/codec" @@ -179,6 +180,17 @@ func (s *testSuite) TestSelectResultRuntimeStats(c *C) { c.Assert(stats.String(), Equals, expect) // Test for idempotence. c.Assert(stats.String(), Equals, expect) + + s1.rpcStat.Stats[tikvrpc.CmdCop] = &tikv.RPCRuntimeStats{ + Count: 1, + Consume: int64(time.Second), + } + stmtStats.RegisterStats(2, s1) + stats = stmtStats.GetRootStats(2) + expect = "cop_task: {num: 2, max: 1s, min: 1ms, avg: 500.5ms, p95: 1s, max_proc_keys: 200, p95_proc_keys: 200, tot_proc: 1s, tot_wait: 1s, rpc_num: 1, rpc_time: 1s, copr_cache_hit_ratio: 0.00}, backoff{RegionMiss: 1ms}" + c.Assert(stats.String(), Equals, expect) + // Test for idempotence. + c.Assert(stats.String(), Equals, expect) } func (s *testSuite) createSelectStreaming(batch, totalRows int, c *C) (*streamResult, []*types.FieldType) { diff --git a/distsql/select_result.go b/distsql/select_result.go index 61853c7e2687f..e819b2e890550 100644 --- a/distsql/select_result.go +++ b/distsql/select_result.go @@ -387,6 +387,7 @@ func (s *selectResultRuntimeStats) Merge(rs execdetails.RuntimeStats) { func (s *selectResultRuntimeStats) String() string { buf := bytes.NewBuffer(nil) + rpcStat := s.rpcStat if len(s.copRespTime) > 0 { size := len(s.copRespTime) if size == 1 { @@ -424,9 +425,10 @@ func (s *selectResultRuntimeStats) String() string { } } } - copRPC := s.rpcStat.Stats[tikvrpc.CmdCop] + copRPC := rpcStat.Stats[tikvrpc.CmdCop] if copRPC != nil && copRPC.Count > 0 { - delete(s.rpcStat.Stats, tikvrpc.CmdCop) + rpcStat = rpcStat.Clone() + delete(rpcStat.Stats, tikvrpc.CmdCop) buf.WriteString(", rpc_num: ") buf.WriteString(strconv.FormatInt(copRPC.Count, 10)) buf.WriteString(", rpc_time: ") @@ -437,7 +439,7 @@ func (s *selectResultRuntimeStats) String() string { buf.WriteString("}") } - rpcStatsStr := s.rpcStat.String() + rpcStatsStr := rpcStat.String() if len(rpcStatsStr) > 0 { buf.WriteString(", ") buf.WriteString(rpcStatsStr) diff --git a/store/tikv/region_request.go b/store/tikv/region_request.go index fbd7f1d5f75fe..8d563870f1e18 100644 --- a/store/tikv/region_request.go +++ b/store/tikv/region_request.go @@ -98,6 +98,18 @@ func (r *RegionRequestRuntimeStats) String() string { return buf.String() } +// Clone returns a copy of itself. +func (r *RegionRequestRuntimeStats) Clone() RegionRequestRuntimeStats { + newRs := NewRegionRequestRuntimeStats() + for cmd, v := range r.Stats { + newRs.Stats[cmd] = &RPCRuntimeStats{ + Count: v.Count, + Consume: v.Consume, + } + } + return newRs +} + // Merge merges other RegionRequestRuntimeStats. func (r *RegionRequestRuntimeStats) Merge(rs RegionRequestRuntimeStats) { for cmd, v := range rs.Stats {