From 796bea5d7c59957e6c440b9b7c06e5429f5bdefc Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Wed, 13 Dec 2023 12:50:36 +0800 Subject: [PATCH] fix(monitor): consider whether the target host is in source host of mem balancer --- pkg/monitor/controller/balancer/cpu.go | 2 +- pkg/monitor/controller/balancer/mem.go | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/pkg/monitor/controller/balancer/cpu.go b/pkg/monitor/controller/balancer/cpu.go index b9a2dc4389..a614844ed5 100644 --- a/pkg/monitor/controller/balancer/cpu.go +++ b/pkg/monitor/controller/balancer/cpu.go @@ -108,7 +108,7 @@ func (m *cpuCondition) IsFitTarget(settings *monitor.MigrationAlertSettings, t I // only when srcHostIds isn't empty if len(srcHostIds) != 0 { if !ltThreshold && !sets.NewString(srcHostIds...).Has(t.GetId()) && tScore < MAX_THRESHOLD { - // if target host is not in source specified hosts and calculate score less than MAX_THRESHOLD + // if target host is not in source specified hosts and calculated score is less than MAX_THRESHOLD log.Infof("let host:%s:current(%f) + guest:%s:score(%f) < MAX_THRESHOLD(%f) to fit target, because it's not in source specified hosts", t.GetName(), t.GetCurrent(), c.GetName(), c.GetScore(), MAX_THRESHOLD) return nil } diff --git a/pkg/monitor/controller/balancer/mem.go b/pkg/monitor/controller/balancer/mem.go index fc4a860ef0..b36b15dddf 100644 --- a/pkg/monitor/controller/balancer/mem.go +++ b/pkg/monitor/controller/balancer/mem.go @@ -16,7 +16,9 @@ package balancer import ( "yunion.io/x/jsonutils" + "yunion.io/x/log" "yunion.io/x/pkg/errors" + "yunion.io/x/pkg/util/sets" "yunion.io/x/onecloud/pkg/apis/monitor" "yunion.io/x/onecloud/pkg/monitor/tsdb" @@ -81,9 +83,28 @@ func (m *memCondition) GetSourceThresholdDelta(threshold float64, host IHost) fl } func (m *memCondition) IsFitTarget(settings *monitor.MigrationAlertSettings, t ITarget, c ICandidate) error { - if t.GetCurrent()-c.GetScore() > m.GetThreshold() { + tScore := t.GetCurrent() - c.GetScore() + gtThreshold := tScore > m.GetThreshold() + if gtThreshold { return nil } + + // 1G = 1 * 1024 * 1024 * 1024 (bytes) + MAX_THRESHOLD := float64(1 * 1024 * 1024 * 1024) + src := settings.Source + srcHostIds := []string{} + if src != nil { + srcHostIds = src.HostIds + } + // only when srcHostIds isn't empty + if len(srcHostIds) != 0 { + if !gtThreshold && !sets.NewString(srcHostIds...).Has(t.GetId()) && tScore > MAX_THRESHOLD { + // if target host is not in source specified hosts and calculated score is greater than MAX_THRESHOLD + log.Infof("let host:%s:current(%f) + guest:%s:score(%f) > MAX_THRESHOLD(%f) to fit target, because it's not in source specified hosts", t.GetName(), t.GetCurrent(), c.GetName(), c.GetScore(), MAX_THRESHOLD) + return nil + } + } + return errors.Errorf("host:%s:current(%f) - guest:%s:score(%f) <= threshold(%f)", t.GetName(), t.GetCurrent(), c.GetName(), c.GetScore(), m.GetThreshold()) }