From 84138e4b876908274f68cedbb4c5e4c57472ee34 Mon Sep 17 00:00:00 2001 From: okJiang <819421878@qq.com> Date: Thu, 22 Aug 2024 15:27:13 +0800 Subject: [PATCH] heartbeat: reduce unnecessary `RunTask` (#8559) ref tikv/pd#7897 Signed-off-by: okJiang <819421878@qq.com> Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com> --- pkg/cluster/cluster.go | 32 +++++++++++++--------------- pkg/mcs/scheduling/server/cluster.go | 21 ++++++++++-------- server/cluster/cluster.go | 26 +++++++++++----------- 3 files changed, 41 insertions(+), 38 deletions(-) diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index ddba8f89fb6..b1abe4677df 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -74,22 +74,20 @@ func HandleOverlaps(ctx context.Context, c Cluster, overlaps []*core.RegionInfo) } // Collect collects the cluster information. -func Collect(ctx context.Context, c Cluster, region *core.RegionInfo, hasRegionStats bool) { - if hasRegionStats { - // get region again from root tree. make sure the observed region is the latest. - bc := c.GetBasicCluster() - if bc == nil { - return - } - region = bc.GetRegion(region.GetID()) - if region == nil { - return - } - select { - case <-ctx.Done(): - return - default: - } - c.GetRegionStats().Observe(region, c.GetBasicCluster().GetRegionStores(region)) +func Collect(ctx context.Context, c Cluster, region *core.RegionInfo) { + // get region again from root tree. make sure the observed region is the latest. + bc := c.GetBasicCluster() + if bc == nil { + return + } + region = bc.GetRegion(region.GetID()) + if region == nil { + return + } + select { + case <-ctx.Done(): + return + default: } + c.GetRegionStats().Observe(region, c.GetBasicCluster().GetRegionStores(region)) } diff --git a/pkg/mcs/scheduling/server/cluster.go b/pkg/mcs/scheduling/server/cluster.go index 31120ae6082..0dcb26a1a1f 100644 --- a/pkg/mcs/scheduling/server/cluster.go +++ b/pkg/mcs/scheduling/server/cluster.go @@ -631,7 +631,7 @@ func (c *Cluster) processRegionHeartbeat(ctx *core.MetaProcessContext, region *c regionID, ratelimit.ObserveRegionStatsAsync, func(ctx context.Context) { - cluster.Collect(ctx, c, region, hasRegionStats) + cluster.Collect(ctx, c, region) }, ) } @@ -679,14 +679,17 @@ func (c *Cluster) processRegionHeartbeat(ctx *core.MetaProcessContext, region *c ) } tracer.OnSaveCacheFinished() - // handle region stats - ctx.TaskRunner.RunTask( - regionID, - ratelimit.CollectRegionStatsAsync, - func(ctx context.Context) { - cluster.Collect(ctx, c, region, hasRegionStats) - }, - ) + if hasRegionStats { + // handle region stats + ctx.TaskRunner.RunTask( + regionID, + ratelimit.CollectRegionStatsAsync, + func(ctx context.Context) { + cluster.Collect(ctx, c, region) + }, + ) + } + tracer.OnCollectRegionStatsFinished() return nil } diff --git a/server/cluster/cluster.go b/server/cluster/cluster.go index 9e12b158f42..9e85a0f1d84 100644 --- a/server/cluster/cluster.go +++ b/server/cluster/cluster.go @@ -1062,7 +1062,7 @@ func (c *RaftCluster) processRegionHeartbeat(ctx *core.MetaProcessContext, regio regionID, ratelimit.ObserveRegionStatsAsync, func(ctx context.Context) { - cluster.Collect(ctx, c, region, hasRegionStats) + cluster.Collect(ctx, c, region) }, ) } @@ -1119,17 +1119,19 @@ func (c *RaftCluster) processRegionHeartbeat(ctx *core.MetaProcessContext, regio } tracer.OnSaveCacheFinished() - // handle region stats - ctx.MiscRunner.RunTask( - regionID, - ratelimit.CollectRegionStatsAsync, - func(ctx context.Context) { - // TODO: Due to the accuracy requirements of the API "/regions/check/xxx", - // region stats needs to be collected in API mode. - // We need to think of a better way to reduce this part of the cost in the future. - cluster.Collect(ctx, c, region, hasRegionStats) - }, - ) + if hasRegionStats { + // handle region stats + ctx.MiscRunner.RunTask( + regionID, + ratelimit.CollectRegionStatsAsync, + func(ctx context.Context) { + // TODO: Due to the accuracy requirements of the API "/regions/check/xxx", + // region stats needs to be collected in API mode. + // We need to think of a better way to reduce this part of the cost in the future. + cluster.Collect(ctx, c, region) + }, + ) + } tracer.OnCollectRegionStatsFinished() if c.storage != nil {