Files
cloudpods/pkg/controller/autoscaling/timer.go
Rain 03868d28b5 fix(region): Fix some problem for Auto Scaling
1. 主机组的 vpc 和 backendgroup 可以为空,修复了使用 brand
过滤出现的问题
2. ScalingPolicy 的执行,区分手动和非手动
3. 拒绝执行或者执行失败的 ScalingAvtivity 不刷新冷却时间
4. 告警策略增加 Cycle 的检查和默认值
5. 修复 monitor 的一个数组越界问题
2020-04-18 14:13:43 +08:00

99 lines
3.3 KiB
Go

// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package autoscaling
import (
"context"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/onecloud/pkg/mcclient/modules"
)
type STimeScope struct {
Start time.Time
End time.Time
Median time.Time
}
func (asc *SASController) timeScope(median time.Time, interval time.Duration) STimeScope {
ri := interval / 2
return STimeScope{
Start: median.Add(-ri),
End: median.Add(ri),
Median: median,
}
}
func (asc *SASController) Timer(ctx context.Context, userCred mcclient.TokenCredential, isStart bool) {
// 60 is for fault tolerance
interval := asc.options.TimerInterval + 30
timeScope := asc.timeScope(time.Now(), time.Duration(interval)*time.Second)
spSubQ := models.ScalingPolicyManager.Query("id").Equals("status", compute.SP_STATUS_READY).SubQuery()
q := models.ScalingTimerManager.Query().
LT("next_time", timeScope.End).
IsFalse("is_expired").In("scaling_policy_id", spSubQ)
scalingTimers := make([]models.SScalingTimer, 0, 5)
err := db.FetchModelObjects(models.ScalingTimerManager, q, &scalingTimers)
if err != nil {
log.Errorf("db.FetchModelObjects error: %s", err.Error())
return
}
log.Debugf("total %d need to exec, %v", len(scalingTimers), scalingTimers)
log.Debugf("timeScope: start: %s, end: %s", timeScope.Start, timeScope.End)
session := auth.GetSession(ctx, userCred, "", "")
triggerParams := jsonutils.NewDict()
for i := range scalingTimers {
scalingTimer := scalingTimers[i]
asc.timerQueue <- struct{}{}
go func(ctx context.Context) {
defer func() {
<-asc.timerQueue
}()
if scalingTimer.NextTime.Before(timeScope.Start) {
// For unknown reasons, the scalingTimer did not execute at the specified time
scalingTimer.Update(timeScope.Start)
// scalingTimer should not exec for now.
if scalingTimer.NextTime.After(timeScope.End) || scalingTimer.IsExpired {
err = models.ScalingTimerManager.TableSpec().InsertOrUpdate(&scalingTimer)
if err != nil {
log.Errorf("update ScalingTimer whose ScalingPolicyId is %s error: %s",
scalingTimer.ScalingPolicyId, err.Error())
}
return
}
}
_, err = modules.ScalingPolicy.PerformAction(session, scalingTimer.ScalingPolicyId, "trigger",
triggerParams)
if err != nil {
log.Errorf("unable to request to trigger ScalingPolicy '%s'", scalingTimer.ScalingPolicyId)
}
scalingTimer.Update(timeScope.End)
err = models.ScalingTimerManager.TableSpec().InsertOrUpdate(&scalingTimer)
if err != nil {
log.Errorf("update ScalingTimer whose ScalingPolicyId is %s error: %s",
scalingTimer.ScalingPolicyId, err.Error())
}
}(ctx)
}
}