forked from open-falcon-archive/graph
/
index.go
76 lines (64 loc) · 1.9 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package index
import (
"fmt"
"log"
cmodel "github.com/Cepave/common/model"
"github.com/Cepave/graph/g"
)
// 初始化索引功能模块
func Start() {
InitCache()
go StartIndexUpdateIncrTask()
log.Println("index.Start ok")
}
// index收到一条新上报的监控数据,尝试用于增量更新索引
func ReceiveItem(item *cmodel.GraphItem, md5 string) {
if item == nil {
return
}
uuid := item.UUID()
// 已上报过的数据
if indexedItemCache.ContainsKey(md5) {
old := indexedItemCache.Get(md5).(*IndexCacheItem)
if uuid == old.UUID { // dsType+step没有发生变化,只更新缓存 TODO 存在线程安全的问题
old.Item = item
} else { // dsType+step变化了,当成一个新的增量来处理(甚至,不用rrd文件来过滤)
//indexedItemCache.Remove(md5)
unIndexedItemCache.Put(md5, NewIndexCacheItem(uuid, item))
}
return
}
// 是否有rrdtool文件存在,如果有 认为已建立索引
// 针对 索引缓存重建场景 做的优化, 结合索引全量更新 来保证一致性
rrdFileName := g.RrdFileName(g.Config().RRD.Storage, md5, item.DsType, item.Step)
if g.IsRrdFileExist(rrdFileName) {
indexedItemCache.Put(md5, NewIndexCacheItem(uuid, item))
return
}
// 缓存未命中, 放入增量更新队列
unIndexedItemCache.Put(md5, NewIndexCacheItem(uuid, item))
}
//
func GetIndexedItemCache(endpoint string, metric string, tags map[string]string, dstype string, step int) (r *cmodel.GraphItem, rerr error) {
itemDemo := &cmodel.GraphItem{
Endpoint: endpoint,
Metric: metric,
Tags: tags,
DsType: dstype,
Step: step,
}
md5 := itemDemo.Checksum()
uuid := itemDemo.UUID()
cached := indexedItemCache.Get(md5)
if cached == nil {
rerr = fmt.Errorf("not found")
return
}
icitem := cached.(*IndexCacheItem)
if icitem.UUID != uuid {
rerr = fmt.Errorf("counter found, uuid not found: bad step or type")
return
}
r = icitem.Item
return
}