From 8ac19ac3c6ad117f7fcdedd076628c4805e00174 Mon Sep 17 00:00:00 2001 From: jiyfhust Date: Wed, 20 Mar 2024 17:11:13 +0800 Subject: [PATCH 1/4] not merge unconsumed CopStats if not user session --- pkg/distsql/select_result.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/distsql/select_result.go b/pkg/distsql/select_result.go index 63a71b3779628..82a6bec7135c8 100644 --- a/pkg/distsql/select_result.go +++ b/pkg/distsql/select_result.go @@ -603,11 +603,13 @@ func (r *selectResult) Close() error { if respSize > 0 { r.memConsume(-respSize) } - if unconsumed, ok := r.resp.(copr.HasUnconsumedCopRuntimeStats); ok && unconsumed != nil { - unconsumedCopStats := unconsumed.CollectUnconsumedCopRuntimeStats() - for _, copStats := range unconsumedCopStats { - _ = r.updateCopRuntimeStats(context.Background(), copStats, time.Duration(0)) - r.ctx.GetSessionVars().StmtCtx.MergeExecDetails(&copStats.ExecDetails, nil) + if r.ctx != nil { + if unconsumed, ok := r.resp.(copr.HasUnconsumedCopRuntimeStats); ok && unconsumed != nil { + unconsumedCopStats := unconsumed.CollectUnconsumedCopRuntimeStats() + for _, copStats := range unconsumedCopStats { + _ = r.updateCopRuntimeStats(context.Background(), copStats, time.Duration(0)) + r.ctx.GetSessionVars().StmtCtx.MergeExecDetails(&copStats.ExecDetails, nil) + } } } if r.stats != nil { From 595b3365af26effc94ad2f0dae1d7e86245c2f1f Mon Sep 17 00:00:00 2001 From: jiyfhust Date: Wed, 20 Mar 2024 17:33:24 +0800 Subject: [PATCH 2/4] fix --- pkg/distsql/select_result.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/pkg/distsql/select_result.go b/pkg/distsql/select_result.go index 82a6bec7135c8..16a2dd30bd200 100644 --- a/pkg/distsql/select_result.go +++ b/pkg/distsql/select_result.go @@ -603,15 +603,6 @@ func (r *selectResult) Close() error { if respSize > 0 { r.memConsume(-respSize) } - if r.ctx != nil { - if unconsumed, ok := r.resp.(copr.HasUnconsumedCopRuntimeStats); ok && unconsumed != nil { - unconsumedCopStats := unconsumed.CollectUnconsumedCopRuntimeStats() - for _, copStats := range unconsumedCopStats { - _ = r.updateCopRuntimeStats(context.Background(), copStats, time.Duration(0)) - r.ctx.GetSessionVars().StmtCtx.MergeExecDetails(&copStats.ExecDetails, nil) - } - } - } if r.stats != nil { defer func() { if ci, ok := r.resp.(copr.CopInfo); ok { @@ -623,6 +614,13 @@ func (r *selectResult) Close() error { } r.ctx.GetSessionVars().StmtCtx.RuntimeStatsColl.RegisterStats(r.rootPlanID, r.stats) }() + if unconsumed, ok := r.resp.(copr.HasUnconsumedCopRuntimeStats); ok && unconsumed != nil { + unconsumedCopStats := unconsumed.CollectUnconsumedCopRuntimeStats() + for _, copStats := range unconsumedCopStats { + _ = r.updateCopRuntimeStats(context.Background(), copStats, time.Duration(0)) + r.ctx.GetSessionVars().StmtCtx.MergeExecDetails(&copStats.ExecDetails, nil) + } + } } return r.resp.Close() } From d1596e480cd4193bc62810a718dc7918f92bc8b3 Mon Sep 17 00:00:00 2001 From: jiyfhust Date: Wed, 20 Mar 2024 17:42:21 +0800 Subject: [PATCH 3/4] Revert "fix" This reverts commit 595b3365af26effc94ad2f0dae1d7e86245c2f1f. --- pkg/distsql/select_result.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/pkg/distsql/select_result.go b/pkg/distsql/select_result.go index 16a2dd30bd200..82a6bec7135c8 100644 --- a/pkg/distsql/select_result.go +++ b/pkg/distsql/select_result.go @@ -603,6 +603,15 @@ func (r *selectResult) Close() error { if respSize > 0 { r.memConsume(-respSize) } + if r.ctx != nil { + if unconsumed, ok := r.resp.(copr.HasUnconsumedCopRuntimeStats); ok && unconsumed != nil { + unconsumedCopStats := unconsumed.CollectUnconsumedCopRuntimeStats() + for _, copStats := range unconsumedCopStats { + _ = r.updateCopRuntimeStats(context.Background(), copStats, time.Duration(0)) + r.ctx.GetSessionVars().StmtCtx.MergeExecDetails(&copStats.ExecDetails, nil) + } + } + } if r.stats != nil { defer func() { if ci, ok := r.resp.(copr.CopInfo); ok { @@ -614,13 +623,6 @@ func (r *selectResult) Close() error { } r.ctx.GetSessionVars().StmtCtx.RuntimeStatsColl.RegisterStats(r.rootPlanID, r.stats) }() - if unconsumed, ok := r.resp.(copr.HasUnconsumedCopRuntimeStats); ok && unconsumed != nil { - unconsumedCopStats := unconsumed.CollectUnconsumedCopRuntimeStats() - for _, copStats := range unconsumedCopStats { - _ = r.updateCopRuntimeStats(context.Background(), copStats, time.Duration(0)) - r.ctx.GetSessionVars().StmtCtx.MergeExecDetails(&copStats.ExecDetails, nil) - } - } } return r.resp.Close() } From 8e0d4ccd7c2ae0022907d0b74763e89c3472462c Mon Sep 17 00:00:00 2001 From: jiyfhust Date: Thu, 21 Mar 2024 16:45:53 +0800 Subject: [PATCH 4/4] fix --- pkg/distsql/select_result.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/distsql/select_result.go b/pkg/distsql/select_result.go index 82a6bec7135c8..d298be1ff4b61 100644 --- a/pkg/distsql/select_result.go +++ b/pkg/distsql/select_result.go @@ -612,7 +612,7 @@ func (r *selectResult) Close() error { } } } - if r.stats != nil { + if r.stats != nil && r.ctx != nil { defer func() { if ci, ok := r.resp.(copr.CopInfo); ok { r.stats.buildTaskDuration = ci.GetBuildTaskElapsed()