-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
cache.go
218 lines (182 loc) · 5.01 KB
/
cache.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package index
import (
"database/sql"
"fmt"
"log"
"strconv"
"strings"
"sync"
"time"
tcache "github.com/toolkits/cache/localcache/timedcache"
cmodel "github.com/open-falcon/falcon-plus/common/model"
cutils "github.com/open-falcon/falcon-plus/common/utils"
"github.com/open-falcon/falcon-plus/modules/graph/g"
"github.com/open-falcon/falcon-plus/modules/graph/proc"
)
const (
DefaultMaxCacheSize = 5000000 // 默认 最多500w个,太大了内存会耗尽
DefaultCacheProcUpdateTaskSleepInterval = time.Duration(1) * time.Second
)
// item缓存
var (
IndexedItemCache = NewIndexCacheBase(DefaultMaxCacheSize)
unIndexedItemCache = NewIndexCacheBase(DefaultMaxCacheSize)
)
// db本地缓存
var (
// endpoint表的内存缓存, key:endpoint(string) / value:id(int64)
dbEndpointCache = tcache.New(600*time.Second, 60*time.Second)
// endpoint_counter表的内存缓存, key:endpoint_id-counter(string) / val:dstype-step(string)
dbEndpointCounterCache = tcache.New(600*time.Second, 60*time.Second)
)
// 初始化cache
func InitCache() {
go startCacheProcUpdateTask()
}
// USED WHEN QUERY
func GetTypeAndStep(endpoint string, counter string) (dsType string, step int, found bool) {
// get it from index cache
pk := cutils.Md5(fmt.Sprintf("%s/%s", endpoint, counter))
if icitem := IndexedItemCache.Get(pk); icitem != nil {
if item := icitem.(*IndexCacheItem).Item; item != nil {
dsType = item.DsType
step = item.Step
found = true
return
}
}
// statistics
proc.GraphLoadDbCnt.Incr()
// get it from db, this should rarely happen
var endpointId int64 = -1
if endpointId, found = GetEndpointFromCache(endpoint); found {
if dsType, step, found = GetCounterFromCache(endpointId, counter); found {
//found = true
return
}
}
// do not find it, this must be a bad request
found = false
return
}
// Return EndpointId if Found
func GetEndpointFromCache(endpoint string) (int64, bool) {
// get from cache
endpointId, found := dbEndpointCache.Get(endpoint)
if found {
return endpointId.(int64), true
}
// get from db
var id int64 = -1
err := g.DB.QueryRow("SELECT id FROM endpoint WHERE endpoint = ?", endpoint).Scan(&id)
if err != nil && err != sql.ErrNoRows {
log.Println("query endpoint id fail,", err)
return -1, false
}
if err == sql.ErrNoRows || id < 0 {
return -1, false
}
// update cache
dbEndpointCache.Set(endpoint, id, 0)
return id, true
}
// Return DsType Step if Found
func GetCounterFromCache(endpointId int64, counter string) (dsType string, step int, found bool) {
var err error
// get from cache
key := fmt.Sprintf("%d-%s", endpointId, counter)
dsTypeStep, found := dbEndpointCounterCache.Get(key)
if found {
arr := strings.Split(dsTypeStep.(string), "_")
step, err = strconv.Atoi(arr[1])
if err != nil {
found = false
return
}
dsType = arr[0]
return
}
// get from db
err = g.DB.QueryRow("SELECT type, step FROM endpoint_counter WHERE endpoint_id = ? and counter = ?",
endpointId, counter).Scan(&dsType, &step)
if err != nil && err != sql.ErrNoRows {
log.Println("query type and step fail", err)
return
}
if err == sql.ErrNoRows {
return
}
// update cache
dbEndpointCounterCache.Set(key, fmt.Sprintf("%s_%d", dsType, step), 0)
found = true
return
}
// 更新 cache的统计信息
func startCacheProcUpdateTask() {
for {
time.Sleep(DefaultCacheProcUpdateTaskSleepInterval)
proc.IndexedItemCacheCnt.SetCnt(int64(IndexedItemCache.Size()))
proc.UnIndexedItemCacheCnt.SetCnt(int64(unIndexedItemCache.Size()))
proc.EndpointCacheCnt.SetCnt(int64(dbEndpointCache.Size()))
proc.CounterCacheCnt.SetCnt(int64(dbEndpointCounterCache.Size()))
}
}
// INDEX CACHE
// 索引缓存的元素数据结构
type IndexCacheItem struct {
UUID string
Item *cmodel.GraphItem
}
func NewIndexCacheItem(uuid string, item *cmodel.GraphItem) *IndexCacheItem {
return &IndexCacheItem{UUID: uuid, Item: item}
}
// 索引缓存-基本缓存容器
type IndexCacheBase struct {
sync.RWMutex
maxSize int
data map[string]interface{}
}
func NewIndexCacheBase(max int) *IndexCacheBase {
return &IndexCacheBase{maxSize: max, data: make(map[string]interface{})}
}
func (this *IndexCacheBase) GetMaxSize() int {
return this.maxSize
}
func (this *IndexCacheBase) Put(key string, item interface{}) {
this.Lock()
defer this.Unlock()
this.data[key] = item
}
func (this *IndexCacheBase) Remove(key string) {
this.Lock()
defer this.Unlock()
delete(this.data, key)
}
func (this *IndexCacheBase) Get(key string) interface{} {
this.RLock()
defer this.RUnlock()
return this.data[key]
}
func (this *IndexCacheBase) ContainsKey(key string) bool {
this.RLock()
defer this.RUnlock()
return this.data[key] != nil
}
func (this *IndexCacheBase) Size() int {
this.RLock()
defer this.RUnlock()
return len(this.data)
}
func (this *IndexCacheBase) Keys() []string {
this.RLock()
defer this.RUnlock()
count := len(this.data)
if count == 0 {
return []string{}
}
keys := make([]string, 0, count)
for key := range this.data {
keys = append(keys, key)
}
return keys
}