|
|
@ -13,7 +13,7 @@ import (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// cached时间周期
|
|
|
|
// cached时间周期
|
|
|
|
const CACHED_DURATION = 60
|
|
|
|
const cachedDuration = 60
|
|
|
|
|
|
|
|
|
|
|
|
type counterCache struct {
|
|
|
|
type counterCache struct {
|
|
|
|
sync.RWMutex
|
|
|
|
sync.RWMutex
|
|
|
@ -31,13 +31,6 @@ func init() {
|
|
|
|
go CleanLoop()
|
|
|
|
go CleanLoop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *counterCache) AddPoint(tms int64, value float64) {
|
|
|
|
|
|
|
|
this.Lock()
|
|
|
|
|
|
|
|
tmsStr := fmt.Sprintf("%d", tms)
|
|
|
|
|
|
|
|
this.Points[tmsStr] = value
|
|
|
|
|
|
|
|
this.Unlock()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func PostToCache(paramPoints []*dataobj.MetricValue) {
|
|
|
|
func PostToCache(paramPoints []*dataobj.MetricValue) {
|
|
|
|
for _, point := range paramPoints {
|
|
|
|
for _, point := range paramPoints {
|
|
|
|
globalPushPoints.AddPoint(point)
|
|
|
|
globalPushPoints.AddPoint(point)
|
|
|
@ -62,69 +55,76 @@ func GetCachedAll() string {
|
|
|
|
return string(str)
|
|
|
|
return string(str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *counterCache) GetKeys() []string {
|
|
|
|
func (cc *counterCache) AddPoint(tms int64, value float64) {
|
|
|
|
this.RLock()
|
|
|
|
cc.Lock()
|
|
|
|
|
|
|
|
tmsStr := fmt.Sprintf("%d", tms)
|
|
|
|
|
|
|
|
cc.Points[tmsStr] = value
|
|
|
|
|
|
|
|
cc.Unlock()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (cc *counterCache) GetKeys() []string {
|
|
|
|
|
|
|
|
cc.RLock()
|
|
|
|
retList := make([]string, 0)
|
|
|
|
retList := make([]string, 0)
|
|
|
|
for k := range this.Points {
|
|
|
|
for k := range cc.Points {
|
|
|
|
retList = append(retList, k)
|
|
|
|
retList = append(retList, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.RUnlock()
|
|
|
|
cc.RUnlock()
|
|
|
|
return retList
|
|
|
|
return retList
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *counterCache) RemoveTms(tms string) {
|
|
|
|
func (cc *counterCache) RemoveTms(tms string) {
|
|
|
|
this.Lock()
|
|
|
|
cc.Lock()
|
|
|
|
delete(this.Points, tms)
|
|
|
|
delete(cc.Points, tms)
|
|
|
|
this.Unlock()
|
|
|
|
cc.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *pushPointsCache) AddCounter(counter string) {
|
|
|
|
func (pc *pushPointsCache) AddCounter(counter string) {
|
|
|
|
this.Lock()
|
|
|
|
pc.Lock()
|
|
|
|
tmp := new(counterCache)
|
|
|
|
tmp := new(counterCache)
|
|
|
|
tmp.Points = make(map[string]float64, 0)
|
|
|
|
tmp.Points = make(map[string]float64)
|
|
|
|
this.Counters[counter] = tmp
|
|
|
|
pc.Counters[counter] = tmp
|
|
|
|
this.Unlock()
|
|
|
|
pc.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *pushPointsCache) GetCounters() []string {
|
|
|
|
func (pc *pushPointsCache) GetCounters() []string {
|
|
|
|
ret := make([]string, 0)
|
|
|
|
ret := make([]string, 0)
|
|
|
|
this.RLock()
|
|
|
|
pc.RLock()
|
|
|
|
for k := range this.Counters {
|
|
|
|
for k := range pc.Counters {
|
|
|
|
ret = append(ret, k)
|
|
|
|
ret = append(ret, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.RUnlock()
|
|
|
|
pc.RUnlock()
|
|
|
|
return ret
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *pushPointsCache) RemoveCounter(counter string) {
|
|
|
|
func (pc *pushPointsCache) RemoveCounter(counter string) {
|
|
|
|
this.Lock()
|
|
|
|
pc.Lock()
|
|
|
|
delete(this.Counters, counter)
|
|
|
|
delete(pc.Counters, counter)
|
|
|
|
this.Unlock()
|
|
|
|
pc.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *pushPointsCache) GetCounterObj(key string) (*counterCache, bool) {
|
|
|
|
func (pc *pushPointsCache) GetCounterObj(key string) (*counterCache, bool) {
|
|
|
|
this.RLock()
|
|
|
|
pc.RLock()
|
|
|
|
Points, ok := this.Counters[key]
|
|
|
|
Points, ok := pc.Counters[key]
|
|
|
|
this.RUnlock()
|
|
|
|
pc.RUnlock()
|
|
|
|
|
|
|
|
|
|
|
|
return Points, ok
|
|
|
|
return Points, ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *pushPointsCache) AddPoint(point *dataobj.MetricValue) {
|
|
|
|
func (pc *pushPointsCache) AddPoint(point *dataobj.MetricValue) {
|
|
|
|
counter := calcCounter(point)
|
|
|
|
counter := calcCounter(point)
|
|
|
|
if _, ok := this.GetCounterObj(counter); !ok {
|
|
|
|
if _, ok := pc.GetCounterObj(counter); !ok {
|
|
|
|
this.AddCounter(counter)
|
|
|
|
pc.AddCounter(counter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
counterPoints, exists := this.GetCounterObj(counter)
|
|
|
|
counterPoints, exists := pc.GetCounterObj(counter)
|
|
|
|
if exists {
|
|
|
|
if exists {
|
|
|
|
counterPoints.AddPoint(point.Timestamp, point.Value)
|
|
|
|
counterPoints.AddPoint(point.Timestamp, point.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *pushPointsCache) CleanOld() {
|
|
|
|
func (pc *pushPointsCache) CleanOld() {
|
|
|
|
counters := this.GetCounters()
|
|
|
|
counters := pc.GetCounters()
|
|
|
|
for _, counter := range counters {
|
|
|
|
for _, counter := range counters {
|
|
|
|
counterObj, exists := this.GetCounterObj(counter)
|
|
|
|
counterObj, exists := pc.GetCounterObj(counter)
|
|
|
|
if !exists {
|
|
|
|
if !exists {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -132,16 +132,17 @@ func (this *pushPointsCache) CleanOld() {
|
|
|
|
|
|
|
|
|
|
|
|
//如果列表为空,清理掉这个counter
|
|
|
|
//如果列表为空,清理掉这个counter
|
|
|
|
if len(tmsList) == 0 {
|
|
|
|
if len(tmsList) == 0 {
|
|
|
|
this.RemoveCounter(counter)
|
|
|
|
pc.RemoveCounter(counter)
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
for _, tmsStr := range tmsList {
|
|
|
|
}
|
|
|
|
tms, err := strconv.Atoi(tmsStr)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
for _, tmsStr := range tmsList {
|
|
|
|
logger.Errorf("clean cached point, atoi error : [%v]", err)
|
|
|
|
tms, err := strconv.Atoi(tmsStr)
|
|
|
|
counterObj.RemoveTms(tmsStr)
|
|
|
|
if err != nil {
|
|
|
|
} else if (time.Now().Unix() - int64(tms)) > CACHED_DURATION {
|
|
|
|
logger.Errorf("clean cached point, atoi error : [%v]", err)
|
|
|
|
counterObj.RemoveTms(tmsStr)
|
|
|
|
counterObj.RemoveTms(tmsStr)
|
|
|
|
}
|
|
|
|
} else if (time.Now().Unix() - int64(tms)) > cachedDuration {
|
|
|
|
|
|
|
|
counterObj.RemoveTms(tmsStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|