提交 6d9846f1 编写于 作者: U Ulric Qin

sync busi_group

上级 c9be9b05
......@@ -313,3 +313,15 @@ func BusiGroupAdd(name string, labelEnable int, labelValue string, members []Bus
return nil
})
}
func BusiGroupStatistics() (*Statistics, error) {
session := DB().Model(&BusiGroup{}).Select("count(*) as total", "max(update_at) as last_updated")
var stats []*Statistics
err := session.Find(&stats).Error
if err != nil {
return nil, err
}
return stats[0], nil
}
......@@ -127,19 +127,6 @@ func TargetGetsByCluster(cluster string) ([]*Target, error) {
var lst []*Target
err := session.Find(&lst).Error
if err == nil {
bgcache, err := BusiGroupGetMap()
if err != nil {
return nil, err
}
for i := 0; i < len(lst); i++ {
err = lst[i].FillGroup(bgcache)
if err != nil {
return nil, err
}
}
}
return lst, err
}
......
package memsto
import (
"fmt"
"sync"
"time"
"github.com/pkg/errors"
"github.com/toolkits/pkg/logger"
"github.com/didi/nightingale/v5/src/models"
"github.com/didi/nightingale/v5/src/server/config"
promstat "github.com/didi/nightingale/v5/src/server/stat"
)
type BusiGroupCacheType struct {
statTotal int64
statLastUpdated int64
sync.RWMutex
ugs map[int64]*models.BusiGroup // key: id
}
var BusiGroupCache = BusiGroupCacheType{
statTotal: -1,
statLastUpdated: -1,
ugs: make(map[int64]*models.BusiGroup),
}
func (c *BusiGroupCacheType) StatChanged(total, lastUpdated int64) bool {
if c.statTotal == total && c.statLastUpdated == lastUpdated {
return false
}
return true
}
func (c *BusiGroupCacheType) Set(ugs map[int64]*models.BusiGroup, total, lastUpdated int64) {
c.Lock()
c.ugs = ugs
c.Unlock()
// only one goroutine used, so no need lock
c.statTotal = total
c.statLastUpdated = lastUpdated
}
func (c *BusiGroupCacheType) GetByBusiGroupId(id int64) *models.BusiGroup {
c.RLock()
defer c.RUnlock()
return c.ugs[id]
}
func SyncBusiGroups() {
err := syncBusiGroups()
if err != nil {
fmt.Println("failed to sync busi groups:", err)
exit(1)
}
go loopSyncBusiGroups()
}
func loopSyncBusiGroups() {
duration := time.Duration(9000) * time.Millisecond
for {
time.Sleep(duration)
if err := syncBusiGroups(); err != nil {
logger.Warning("failed to sync busi groups:", err)
}
}
}
func syncBusiGroups() error {
start := time.Now()
stat, err := models.BusiGroupStatistics()
if err != nil {
return errors.WithMessage(err, "failed to exec BusiGroupStatistics")
}
if !BusiGroupCache.StatChanged(stat.Total, stat.LastUpdated) {
promstat.GaugeCronDuration.WithLabelValues(config.C.ClusterName, "sync_busi_groups").Set(0)
promstat.GaugeSyncNumber.WithLabelValues(config.C.ClusterName, "sync_busi_groups").Set(0)
logger.Debug("busi_group not changed")
return nil
}
m, err := models.BusiGroupGetMap()
if err != nil {
return errors.WithMessage(err, "failed to exec BusiGroupGetMap")
}
BusiGroupCache.Set(m, stat.Total, stat.LastUpdated)
ms := time.Since(start).Milliseconds()
promstat.GaugeCronDuration.WithLabelValues(config.C.ClusterName, "sync_busi_groups").Set(float64(ms))
promstat.GaugeSyncNumber.WithLabelValues(config.C.ClusterName, "sync_busi_groups").Set(float64(len(m)))
logger.Infof("timer: sync busi groups done, cost: %dms, number: %d", ms, len(m))
return nil
}
......@@ -12,10 +12,11 @@ func exit(code int) {
}
func Sync() {
SyncTargets()
SyncBusiGroups()
SyncUsers()
SyncUserGroups()
SyncAlertMutes()
SyncAlertSubscribes()
SyncAlertRules()
SyncTargets()
}
......@@ -125,8 +125,15 @@ func syncTargets() error {
// handle BusiGroup's LabelValue
// BusiGroup的LabelValue就相当于一个特殊的标签来对待
if lst[i].GroupObj != nil && lst[i].GroupObj.LabelEnable == 1 {
lst[i].TagsMap["busigroup"] = lst[i].GroupObj.LabelValue
if lst[i].GroupId > 0 {
bg := BusiGroupCache.GetByBusiGroupId(lst[i].GroupId)
if bg == nil {
return errors.New("busi group cache not ready")
}
if bg.LabelEnable == 1 {
lst[i].TagsMap["busigroup"] = bg.LabelValue
}
}
m[lst[i].Ident] = lst[i]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册