Skip to content

Commit

Permalink
bugfix(metric): fix metric expire
Browse files Browse the repository at this point in the history
  • Loading branch information
nightmeng committed Jan 22, 2024
1 parent df34d1e commit 18cfe31
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ func SetSockMemLimit(conf *coreconfig.Configuration,
for _, containerStatus := range pod.Status.ContainerStatuses {
podUID, containerID := string(pod.UID), native.TrimContainerIDPrefix(containerStatus.ContainerID)

memLimit, err := helper.IgnoreMetricValueExpired(helper.GetPodMetric(metaServer.MetricsFetcher, emitter, pod, coreconsts.MetricMemLimitContainer, -1))
memLimit, err := helper.GetPodMetric(metaServer.MetricsFetcher, emitter, pod, coreconsts.MetricMemLimitContainer, -1)
if err != nil {
general.Infof("memory limit not found:%v..\n", podUID)
continue
}

memTCPLimit, err := helper.IgnoreMetricValueExpired(helper.GetPodMetric(metaServer.MetricsFetcher, emitter, pod, coreconsts.MetricMemTCPLimitContainer, -1))
memTCPLimit, err := helper.GetPodMetric(metaServer.MetricsFetcher, emitter, pod, coreconsts.MetricMemTCPLimitContainer, -1)
if err != nil {
general.Infof("memory tcp.limit not found:%v..\n", podUID)
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func NewCacheReaper(conf *config.Configuration, extraConfig interface{}, metaRea
func (cp *cacheReaper) selectContainers(containers []*types.ContainerInfo, cacheToReap resource.Quantity, numaID int, metricName string) []*types.ContainerInfo {
general.NewMultiSorter(func(s1, s2 interface{}) int {
c1, c2 := s1.(*types.ContainerInfo), s2.(*types.ContainerInfo)
c1Metric, c1Err := helper.IgnoreMetricValueExpired(helper.GetContainerMetric(cp.metaServer.MetricsFetcher, cp.emitter, c1.PodUID, c1.ContainerName, metricName, numaID))
c2Metric, c2Err := helper.IgnoreMetricValueExpired(helper.GetContainerMetric(cp.metaServer.MetricsFetcher, cp.emitter, c2.PodUID, c2.ContainerName, metricName, numaID))
c1Metric, c1Err := helper.GetContainerMetric(cp.metaServer.MetricsFetcher, cp.emitter, c1.PodUID, c1.ContainerName, metricName, numaID)
c2Metric, c2Err := helper.GetContainerMetric(cp.metaServer.MetricsFetcher, cp.emitter, c2.PodUID, c2.ContainerName, metricName, numaID)
if c1Err != nil || c2Err != nil {
return general.CmpError(c1Err, c2Err)
}
Expand All @@ -78,7 +78,7 @@ func (cp *cacheReaper) selectContainers(containers []*types.ContainerInfo, cache
sum := resource.NewQuantity(0, resource.BinarySI)

for _, ci := range containers {
metric, err := helper.IgnoreMetricValueExpired(helper.GetContainerMetric(cp.metaServer.MetricsFetcher, cp.emitter, ci.PodUID, ci.ContainerName, metricName, numaID))
metric, err := helper.GetContainerMetric(cp.metaServer.MetricsFetcher, cp.emitter, ci.PodUID, ci.ContainerName, metricName, numaID)
if err != nil {
general.Errorf("failed to get metric %v for pod %v/%v container %v on numa %v err %v", metricName, ci.PodNamespace, ci.PodName, ci.ContainerName, numaID, err)
continue
Expand Down
36 changes: 0 additions & 36 deletions pkg/metaserver/agent/metric/helper/expire.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/metaserver/agent/metric/metric_expire.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
)

func IsMetricDataExpired(err error) bool {
return err == errMetricDataExpired
return errors.Is(err, errMetricDataExpired)
}

type CheckMetricDataExpireFunc func(utilmetric.MetricData, error) (utilmetric.MetricData, error)
Expand All @@ -53,7 +53,7 @@ func checkMetricDataExpireFunc(metricsInsurancePeriod time.Duration) CheckMetric
return metricData, err
}

if metricData.Time == nil || metricData.Time.IsZero() {
if metricData.Time == nil || metricData.Time.IsZero() || metricData.Time.Unix() == 0 {
return metricData, nil
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/metaserver/agent/metric/metric_expire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,21 @@ func TestMetricExpire(t *testing.T) {
expectErr := errors.New("test")
_, err = checkMetricDataExpire(metricData, expectErr)
assert.Equal(t, expectErr, err)

unixTime := time.Unix(0, 0)
metricData.Time = &unixTime
checkMetricDataExpire = checkMetricDataExpireFunc(66 * time.Second)
_, err = checkMetricDataExpire(metricData, nil)
assert.NoError(t, err)

emptyTime := time.Time{}
metricData.Time = &emptyTime
checkMetricDataExpire = checkMetricDataExpireFunc(66 * time.Second)
_, err = checkMetricDataExpire(metricData, nil)
assert.NoError(t, err)

metricData.Time = nil
checkMetricDataExpire = checkMetricDataExpireFunc(66 * time.Second)
_, err = checkMetricDataExpire(metricData, nil)
assert.NoError(t, err)
}

0 comments on commit 18cfe31

Please sign in to comment.